Class: Fontisan::Subset::GlyphMapping
- Inherits:
-
Object
- Object
- Fontisan::Subset::GlyphMapping
- Defined in:
- lib/fontisan/subset/glyph_mapping.rb
Overview
Glyph ID mapping management
This class manages the mapping between original glyph IDs (GIDs) in the source font and new GIDs in the subset font. It supports two modes:
-
Compact mode (retain_gids: false): Glyphs are renumbered sequentially, eliminating gaps from removed glyphs. This produces smaller fonts.
-
Retain mode (retain_gids: true): Original glyph IDs are preserved, with removed glyphs leaving empty slots. This maintains glyph references but produces larger fonts.
Instance Attribute Summary collapse
-
#new_to_old ⇒ Hash<Integer, Integer>
readonly
Mapping from new GIDs to old GIDs.
-
#old_to_new ⇒ Hash<Integer, Integer>
readonly
Mapping from old GIDs to new GIDs.
-
#retain_gids ⇒ Boolean
readonly
Whether original GIDs are retained.
Instance Method Summary collapse
-
#each {|old_id, new_id| ... } ⇒ Object
Iterate over all glyph mappings.
-
#include?(old_id) ⇒ Boolean
Check if a glyph is included in the subset.
-
#initialize(old_glyph_ids, retain_gids: false) ⇒ GlyphMapping
constructor
Initialize glyph mapping.
-
#new_id(old_id) ⇒ Integer?
Get new glyph ID for an old glyph ID.
-
#new_ids ⇒ Array<Integer>
Get array of all new glyph IDs in subset.
-
#old_id(new_id) ⇒ Integer?
Get old glyph ID for a new glyph ID.
-
#old_ids ⇒ Array<Integer>
Get array of all old glyph IDs in subset.
-
#size ⇒ Integer
Get number of glyphs in the subset.
Constructor Details
#initialize(old_glyph_ids, retain_gids: false) ⇒ GlyphMapping
Initialize glyph mapping
53 54 55 56 57 58 59 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 53 def initialize(old_glyph_ids, retain_gids: false) @old_to_new = {} @new_to_old = {} @retain_gids = retain_gids build_mappings(old_glyph_ids) end |
Instance Attribute Details
#new_to_old ⇒ Hash<Integer, Integer> (readonly)
Returns mapping from new GIDs to old GIDs.
37 38 39 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 37 def new_to_old @new_to_old end |
#old_to_new ⇒ Hash<Integer, Integer> (readonly)
Returns mapping from old GIDs to new GIDs.
34 35 36 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 34 def old_to_new @old_to_new end |
#retain_gids ⇒ Boolean (readonly)
Returns whether original GIDs are retained.
40 41 42 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 40 def retain_gids @retain_gids end |
Instance Method Details
#each {|old_id, new_id| ... } ⇒ Object
Iterate over all glyph mappings
Yields old_id and new_id pairs in order of old glyph IDs.
157 158 159 160 161 162 163 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 157 def each return enum_for(:each) unless block_given? old_ids.each do |old_id| yield old_id, old_to_new[old_id] end end |
#include?(old_id) ⇒ Boolean
Check if a glyph is included in the subset
114 115 116 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 114 def include?(old_id) old_to_new.key?(old_id) end |
#new_id(old_id) ⇒ Integer?
Get new glyph ID for an old glyph ID
70 71 72 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 70 def new_id(old_id) old_to_new[old_id] end |
#new_ids ⇒ Array<Integer>
Get array of all new glyph IDs in subset
136 137 138 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 136 def new_ids new_to_old.keys.sort end |
#old_id(new_id) ⇒ Integer?
Get old glyph ID for a new glyph ID
83 84 85 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 83 def old_id(new_id) new_to_old[new_id] end |
#old_ids ⇒ Array<Integer>
Get array of all old glyph IDs in subset
125 126 127 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 125 def old_ids old_to_new.keys.sort end |
#size ⇒ Integer
Get number of glyphs in the subset
In compact mode, this is the number of included glyphs. In retain mode, this is the highest old GID + 1.
101 102 103 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 101 def size new_to_old.size end |