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) ⇒ 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
55 56 57 58 59 60 61 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 55 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.
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.
159 160 161 162 163 164 165 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 159 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
116 117 118 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 116 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
72 73 74 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 72 def new_id(old_id) old_to_new[old_id] end |
#new_ids ⇒ Array<Integer>
Get array of all new glyph IDs in subset
138 139 140 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 138 def new_ids new_to_old.keys.sort end |
#old_id(new_id) ⇒ Integer?
Get old glyph ID for a new glyph ID
85 86 87 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 85 def old_id(new_id) new_to_old[new_id] end |
#old_ids ⇒ Array<Integer>
Get array of all old glyph IDs in subset
127 128 129 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 127 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.
103 104 105 |
# File 'lib/fontisan/subset/glyph_mapping.rb', line 103 def size new_to_old.size end |