Class: Ucode::Glyphs::LastResort::CmapIndex
- Inherits:
-
Object
- Object
- Ucode::Glyphs::LastResort::CmapIndex
- 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
-
.parse(path) ⇒ Hash{Integer=>String}
Parse the cmap file at ‘path` and return a frozen Hash.
Instance Method Summary collapse
-
#[](codepoint) ⇒ String?
Glyph name or nil if no entry.
-
#initialize(path) ⇒ CmapIndex
constructor
A new instance of CmapIndex.
- #key?(codepoint) ⇒ Boolean
-
#size ⇒ Integer
Number of entries.
-
#to_h ⇒ Hash{Integer=>String}
Frozen codepoint → glyph name.
Constructor Details
#initialize(path) ⇒ CmapIndex
Returns a new instance of CmapIndex.
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.
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.
55 56 57 |
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 55 def [](codepoint) to_h[codepoint] end |
#key?(codepoint) ⇒ Boolean
60 61 62 |
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 60 def key?(codepoint) to_h.key?(codepoint) end |
#size ⇒ Integer
Returns number of entries.
65 66 67 |
# File 'lib/ucode/glyphs/last_resort/cmap_index.rb', line 65 def size to_h.size end |
#to_h ⇒ Hash{Integer=>String}
Returns 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 |