Class: Fontisan::Subset::GlyphMapping
- Inherits:
-
Object
- Object
- Fontisan::Subset::GlyphMapping
- Includes:
- Enumerable
- 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, mapping: nil) ⇒ 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, mapping: nil) ⇒ GlyphMapping
Initialize glyph mapping
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 55 def initialize(old_glyph_ids = [], retain_gids: false, mapping: nil) @old_to_new = {} @new_to_old = {} @retain_gids = retain_gids if mapping @old_to_new = mapping @new_to_old = mapping.invert else build_mappings(old_glyph_ids) end end |
Instance Attribute Details
#new_to_old ⇒ Hash<Integer, Integer> (readonly)
Returns mapping from new GIDs to old GIDs.
39 40 41 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 39 def new_to_old @new_to_old end |
#old_to_new ⇒ Hash<Integer, Integer> (readonly)
Returns mapping from old GIDs to new GIDs.
36 37 38 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 36 def old_to_new @old_to_new end |
#retain_gids ⇒ Boolean (readonly)
Returns whether original GIDs are retained.
42 43 44 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 42 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.
164 165 166 167 168 169 170 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 164 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
121 122 123 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 121 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
77 78 79 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 77 def new_id(old_id) old_to_new[old_id] end |
#new_ids ⇒ Array<Integer>
Get array of all new glyph IDs in subset
143 144 145 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 143 def new_ids new_to_old.keys.sort end |
#old_id(new_id) ⇒ Integer?
Get old glyph ID for a new glyph ID
90 91 92 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 90 def old_id(new_id) new_to_old[new_id] end |
#old_ids ⇒ Array<Integer>
Get array of all old glyph IDs in subset
132 133 134 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 132 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.
108 109 110 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 108 def size new_to_old.size end |