Class: Ucode::Glyphs::LastResort::CmapIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/ucode/glyphs/last_resort/cmap_index.rb

Overview

Parses the Last Resort Font ‘cmap-f13.ttx` once into a flat `=> glyph_name` lookup.

The Format 13 cmap has 1,114,112 entries (every codepoint from U+0000 to U+10FFFF). Each entry looks like:

<map code="0x0" name="lastresortlatin"/>

We parse every ‘<map>` child of every `<cmap_format_*>` element, ignore the platform/encoding attributes (Format 13 only here), and build a single Hash. Memory cost is ~80 MB for the parsed Hash on Ruby 3.x — acceptable for the CLI, paid once per run.

For long-running processes (e.g. the site dev server), the parsed index can be cached via the optional ‘cache:` constructor argument. The cache contract is `cache.read(key) -> Hash | nil` and `cache.write(key, hash) -> void`; pass an object with both methods (e.g. `Ucode::Cache`).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CmapIndex

Returns a new instance of CmapIndex.

Parameters:

  • path (String, Pathname, #to_path)

    cmap-f13.ttx path



44
45
46
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 44

def initialize(path)
  @path = Pathname.new(path)
end

Class Method Details

.parse(path) ⇒ Hash{Integer=>String}

Parse the cmap file at ‘path` and return a frozen Hash.

Parameters:

  • path (String, Pathname, #to_path)

    cmap-f13.ttx path

Returns:

  • (Hash{Integer=>String})

    codepoint → glyph name



39
40
41
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 39

def self.parse(path)
  new(path).to_h
end

Instance Method Details

#[](codepoint) ⇒ String?

Returns glyph name or nil if no entry.

Parameters:

  • codepoint (Integer)

Returns:

  • (String, nil)

    glyph name or nil if no entry



55
56
57
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 55

def [](codepoint)
  to_h[codepoint]
end

#key?(codepoint) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 60

def key?(codepoint)
  to_h.key?(codepoint)
end

#sizeInteger

Returns number of entries.

Returns:

  • (Integer)

    number of entries



65
66
67
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 65

def size
  to_h.size
end

#to_hHash{Integer=>String}

Returns frozen codepoint → glyph name.

Returns:

  • (Hash{Integer=>String})

    frozen codepoint → glyph name



49
50
51
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 49

def to_h
  @to_h ||= build_index.freeze
end