fontisan 0.4.22 → 0.4.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +5 -0
  3. data/lib/fontisan/config/subset_profiles.yml +2 -0
  4. data/lib/fontisan/stitcher/cbdt_propagator.rb +44 -22
  5. data/lib/fontisan/stitcher/glyph_cloner.rb +41 -0
  6. data/lib/fontisan/stitcher/glyph_copier.rb +3 -33
  7. data/lib/fontisan/stitcher/unique_glyph_name.rb +46 -0
  8. data/lib/fontisan/stitcher.rb +2 -0
  9. data/lib/fontisan/subset/shared_state.rb +42 -0
  10. data/lib/fontisan/subset/subset_context.rb +20 -0
  11. data/lib/fontisan/subset/table_strategy/cbdt.rb +30 -0
  12. data/lib/fontisan/subset/table_strategy/cblc.rb +27 -0
  13. data/lib/fontisan/subset/table_strategy/cmap.rb +146 -0
  14. data/lib/fontisan/subset/table_strategy/color_bitmap_placement.rb +20 -0
  15. data/lib/fontisan/subset/table_strategy/color_bitmap_strike_plan.rb +45 -0
  16. data/lib/fontisan/subset/table_strategy/color_bitmap_subset_plan.rb +62 -0
  17. data/lib/fontisan/subset/table_strategy/color_bitmap_subsetter.rb +182 -0
  18. data/lib/fontisan/subset/table_strategy/color_bitmap_subtable_plan.rb +59 -0
  19. data/lib/fontisan/subset/table_strategy/glyf.rb +29 -0
  20. data/lib/fontisan/subset/table_strategy/glyf_loca_builder.rb +145 -0
  21. data/lib/fontisan/subset/table_strategy/head.rb +42 -0
  22. data/lib/fontisan/subset/table_strategy/hhea.rb +67 -0
  23. data/lib/fontisan/subset/table_strategy/hmtx.rb +52 -0
  24. data/lib/fontisan/subset/table_strategy/loca.rb +41 -0
  25. data/lib/fontisan/subset/table_strategy/maxp.rb +23 -0
  26. data/lib/fontisan/subset/table_strategy/name.rb +19 -0
  27. data/lib/fontisan/subset/table_strategy/os2.rb +21 -0
  28. data/lib/fontisan/subset/table_strategy/pass_through.rb +20 -0
  29. data/lib/fontisan/subset/table_strategy/post.rb +37 -0
  30. data/lib/fontisan/subset/table_strategy.rb +75 -0
  31. data/lib/fontisan/subset/table_subsetter.rb +49 -616
  32. data/lib/fontisan/subset.rb +3 -0
  33. data/lib/fontisan/tables/cbdt.rb +66 -121
  34. data/lib/fontisan/tables/cblc.rb +110 -231
  35. data/lib/fontisan/tables/cblc_big_glyph_metrics.rb +28 -0
  36. data/lib/fontisan/tables/cblc_bitmap_size.rb +67 -0
  37. data/lib/fontisan/tables/cblc_glyph_bitmap_location.rb +28 -0
  38. data/lib/fontisan/tables/cblc_index_subtable.rb +104 -0
  39. data/lib/fontisan/tables/cblc_index_subtable_array_entry.rb +20 -0
  40. data/lib/fontisan/tables/cblc_index_subtable_format_parser.rb +150 -0
  41. data/lib/fontisan/tables/cblc_index_subtable_header.rb +18 -0
  42. data/lib/fontisan/tables/cblc_sbit_line_metrics.rb +28 -0
  43. data/lib/fontisan/tables.rb +11 -0
  44. data/lib/fontisan/ufo/glyph_exists_error.rb +23 -0
  45. data/lib/fontisan/ufo/layer.rb +42 -2
  46. data/lib/fontisan/ufo.rb +2 -1
  47. data/lib/fontisan/version.rb +1 -1
  48. metadata +35 -2
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Plan for the subset of color bitmaps: which source strikes have
7
+ # any surviving glyph, and the per-strike runs of surviving new
8
+ # GIDs. Built once by [ColorBitmapSubsetter] from a CBLC + a
9
+ # GlyphMapping, then used to drive both CBDT and CBLC emission.
10
+ class ColorBitmapSubsetPlan
11
+ attr_reader :strikes, :strike_plans, :placements
12
+
13
+ def initialize(mapping)
14
+ @mapping = mapping
15
+ @strikes = []
16
+ @strike_plans = []
17
+ @placements = []
18
+ end
19
+
20
+ # Walk every (gid, strike) location in CBLC and capture the
21
+ # ones whose source gid is in the subset. Remaps the gid via
22
+ # the supplied mapping.
23
+ def collect!(cblc)
24
+ cblc.bitmap_sizes.each do |strike|
25
+ strike_plan = ColorBitmapStrikePlan.new(strike)
26
+
27
+ cblc.sub_tables_for(strike).each do |sub|
28
+ sub.locations.each do |loc|
29
+ new_gid = @mapping.new_id(loc.glyph_id)
30
+ next unless new_gid
31
+
32
+ placement = ColorBitmapPlacement.new(
33
+ source_gid: loc.glyph_id,
34
+ new_gid: new_gid,
35
+ source_offset: loc.cbdt_offset,
36
+ byte_length: loc.byte_length,
37
+ image_format: loc.image_format,
38
+ strike_ppem: strike.ppem,
39
+ )
40
+ @placements << placement
41
+ strike_plan.add(placement)
42
+ end
43
+ end
44
+
45
+ next if strike_plan.empty?
46
+
47
+ @strikes << strike
48
+ @strike_plans << strike_plan
49
+ end
50
+ end
51
+
52
+ def each_placement(&)
53
+ @placements.each(&)
54
+ end
55
+
56
+ def each_strike_plan(&)
57
+ @strike_plans.each(&)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Paired CBDT + CBLC subsetter.
7
+ #
8
+ # CBDT (Color Bitmap Data) and CBLC (Color Bitmap Location) are
9
+ # tightly coupled: CBLC indexes glyph IDs to CBDT byte offsets,
10
+ # and CBDT is just a blob of bitmap blocks. Subsetting requires
11
+ # walking both at once — filtering to subset GIDs, copying the
12
+ # retained bitmap blocks into a fresh CBDT, and rewriting CBLC's
13
+ # IndexSubTable offsets to match.
14
+ #
15
+ # Both the Cbdt and Cblc strategies call into this collaborator,
16
+ # which performs the work once and caches both byte strings. This
17
+ # keeps the strategies themselves trivial and avoids forcing the
18
+ # TableSubsetter to know about CBDT/CBLC ordering.
19
+ #
20
+ # The output CBLC keeps the source BitmapSize records verbatim
21
+ # (ppem, metrics, glyph_range) and rewrites each IndexSubTable's
22
+ # `imageDataOffset` and offset array to point into the new CBDT.
23
+ # Glyph IDs are remapped via the supplied [GlyphMapping] so the
24
+ # subset's CBLC references subset GIDs, not source GIDs.
25
+ #
26
+ # Output IndexSubTables always use format 1 (uint32 offsets) —
27
+ # it has no alignment requirement, so arbitrary bitmap block
28
+ # sizes from the source work without padding.
29
+ #
30
+ # Reference: OpenType CBDT/CBLC specifications.
31
+ class ColorBitmapSubsetter
32
+ # @return [String] new CBDT bytes (header + retained blocks)
33
+ attr_reader :cbdt_bytes
34
+
35
+ # @return [String] new CBLC bytes (header + BitmapSize + ISTA + IST)
36
+ attr_reader :cblc_bytes
37
+
38
+ # @param font [SfntFont]
39
+ # @param mapping [GlyphMapping] old↔new GID map for the subset
40
+ def initialize(font:, mapping:)
41
+ @font = font
42
+ @mapping = mapping
43
+ @cbdt_bytes = +""
44
+ @cblc_bytes = +""
45
+ end
46
+
47
+ # Build the subset CBDT + CBLC bytes. Idempotent.
48
+ #
49
+ # @return [self]
50
+ def build
51
+ return self if @built
52
+
53
+ source_cblc = @font.table_data["CBLC"]
54
+ source_cbdt = @font.table_data["CBDT"]
55
+
56
+ if source_cblc.nil? || source_cbdt.nil?
57
+ @built = true
58
+ return self
59
+ end
60
+
61
+ cblc = Tables::Cblc.read(source_cblc)
62
+ cbdt = Tables::Cbdt.read(source_cbdt)
63
+
64
+ plan = ColorBitmapSubsetPlan.new(@mapping)
65
+ plan.collect!(cblc)
66
+
67
+ emit_cbdt(cbdt, plan)
68
+ emit_cblc(cblc, plan)
69
+
70
+ @built = true
71
+ self
72
+ end
73
+
74
+ private
75
+
76
+ # Emit CBDT: preserve the source 4-byte header, then concatenate
77
+ # every retained bitmap block in (strike, gid) order. The
78
+ # placement's new_offset is recorded so CBLC emission can
79
+ # reference it.
80
+ def emit_cbdt(cbdt, plan)
81
+ @cbdt_bytes = String.new(encoding: Encoding::BINARY)
82
+ @cbdt_bytes << cbdt.raw_data[0, 4] # majorVersion + minorVersion
83
+
84
+ plan.each_placement do |placement|
85
+ block = cbdt.bitmap_data_at(placement.source_offset,
86
+ placement.byte_length)
87
+ next unless block
88
+
89
+ placement.new_offset = @cbdt_bytes.bytesize
90
+ @cbdt_bytes << block
91
+ end
92
+ end
93
+
94
+ # Emit CBLC: header + BitmapSize section + IndexSubTableArray
95
+ # section + IndexSubTable records. The IndexSubTableArray entries
96
+ # are grouped per strike; IndexSubTable records are appended
97
+ # after all ISTA entries. Each IndexSubTable is format 1.
98
+ def emit_cblc(cblc, plan)
99
+ output = String.new(encoding: Encoding::BINARY)
100
+ output << [Integer(cblc.version), plan.strikes.size].pack("NN")
101
+
102
+ bitmap_size_section_end = output.bytesize + (plan.strikes.size * 48)
103
+ ista_offsets = build_ista_offsets(plan, bitmap_size_section_end)
104
+ ista_section_end = ista_offsets.last.to_i +
105
+ (plan.strike_plans.sum(&:subtable_count) * 8)
106
+
107
+ # BitmapSize records (patched array_offset + number_of_subtables).
108
+ plan.strike_plans.each_with_index do |strike_plan, idx|
109
+ output << patch_bitmap_size(strike_plan.source_strike,
110
+ ista_offsets[idx],
111
+ strike_plan.subtable_count)
112
+ end
113
+
114
+ # IndexSubTableArray entries (one per subtable plan).
115
+ ist_cursor = ista_section_end
116
+ plan.strike_plans.each_with_index do |strike_plan, idx|
117
+ strike_ista_offset = ista_offsets[idx]
118
+ strike_plan.subtable_plans.each do |sub|
119
+ additional = ist_cursor - strike_ista_offset
120
+ output << [sub.first_new_gid, sub.last_new_gid,
121
+ additional].pack("nnN")
122
+ ist_cursor += sub.byte_length
123
+ end
124
+ end
125
+
126
+ # IndexSubTable records (format 1).
127
+ plan.each_strike_plan do |strike_plan|
128
+ strike_plan.subtable_plans.each do |sub|
129
+ output << build_index_sub_table(sub)
130
+ end
131
+ end
132
+
133
+ @cblc_bytes = output
134
+ end
135
+
136
+ # Absolute CBLC offset of each survivor strike's IndexSubTableArray.
137
+ # Each strike's ISTA is laid out right after the BitmapSize section
138
+ # and the previous strikes' ISTA entries (8 bytes × subtable count).
139
+ def build_ista_offsets(plan, bitmap_size_section_end)
140
+ offsets = []
141
+ cursor = bitmap_size_section_end
142
+ plan.strike_plans.each do |sp|
143
+ offsets << cursor
144
+ cursor += sp.subtable_count * 8
145
+ end
146
+ offsets
147
+ end
148
+
149
+ # Clone a BitmapSize with patched `index_subtable_array_offset`
150
+ # and `number_of_index_subtables`; everything else preserved
151
+ # from the source.
152
+ def patch_bitmap_size(source_strike, new_ista_offset, subtable_count)
153
+ clone = Tables::CblcBitmapSize.new
154
+ clone.index_subtable_array_offset = new_ista_offset
155
+ clone.index_tables_size = source_strike.index_tables_size
156
+ clone.number_of_index_subtables = subtable_count
157
+ clone.color_ref = source_strike.color_ref
158
+ clone.hori = source_strike.hori
159
+ clone.vert = source_strike.vert
160
+ clone.start_glyph_index = source_strike.start_glyph_index
161
+ clone.end_glyph_index = source_strike.end_glyph_index
162
+ clone.ppem_x = source_strike.ppem_x
163
+ clone.ppem_y = source_strike.ppem_y
164
+ clone.bit_depth = source_strike.bit_depth
165
+ clone.flags = source_strike.flags
166
+ clone.to_binary_s
167
+ end
168
+
169
+ # Format 1 IndexSubTable: 8-byte header + uint32 offsetArray[count+1].
170
+ # imageDataOffset = absolute new CBDT offset of the first glyph's
171
+ # bitmap. offsets[i] = relative offset from imageDataOffset for
172
+ # glyph i.
173
+ def build_index_sub_table(sub)
174
+ bytes = String.new(encoding: Encoding::BINARY)
175
+ bytes << [1, sub.image_format, sub.first_new_offset].pack("nnN")
176
+ bytes << sub.offset_array.pack("N*")
177
+ bytes
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Plan for one IndexSubTable emitted by [ColorBitmapSubsetter]:
7
+ # a contiguous run of new GIDs at a single strike, with the
8
+ # CBDT offsets and lengths needed to emit a format 1 IndexSubTable.
9
+ class ColorBitmapSubtablePlan
10
+ attr_reader :placements, :strike_ppem, :image_format
11
+
12
+ def initialize(strike_ppem:, image_format:)
13
+ @strike_ppem = strike_ppem
14
+ @image_format = image_format
15
+ @placements = []
16
+ end
17
+
18
+ def add(placement)
19
+ @placements << placement
20
+ end
21
+
22
+ def first_new_gid
23
+ @placements.first.new_gid
24
+ end
25
+
26
+ def last_new_gid
27
+ @placements.last.new_gid
28
+ end
29
+
30
+ # Absolute CBDT offset where this subtable's first glyph's
31
+ # bitmap begins. Assigned during CBDT emission.
32
+ #
33
+ # @return [Integer]
34
+ def first_new_offset
35
+ @placements.first.new_offset
36
+ end
37
+
38
+ # IndexSubTable format 1 offset array (relative to
39
+ # imageDataOffset). offsets[0] is 0 (first glyph starts at
40
+ # imageDataOffset); offsets[i+1] = offsets[i] + byte_length.
41
+ #
42
+ # @return [Array<Integer>]
43
+ def offset_array
44
+ arr = [0]
45
+ @placements.each do |p|
46
+ arr << (arr.last + p.byte_length)
47
+ end
48
+ arr
49
+ end
50
+
51
+ # Byte length of this IndexSubTable in the output CBLC:
52
+ # 8-byte header + (count + 1) × 4-byte offsets.
53
+ def byte_length
54
+ 8 + (@placements.size + 1) * 4
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Glyf strategy: extract each subset glyph's bytes from the source
7
+ # glyf table (remapping composite component glyph IDs), build the
8
+ # corresponding loca offsets, and stash both into [SharedState] so
9
+ # the Loca strategy can read them. Also records the union bbox
10
+ # for the Head strategy.
11
+ class Glyf
12
+ # @param context [SubsetContext]
13
+ # @param tag [String] "glyf"
14
+ # @param table [Glyf] parsed glyf table (unused; builder reads
15
+ # the table from `context.font` directly so it sees the same
16
+ # instance)
17
+ # @return [String] binary glyf bytes for the subset
18
+ def self.call(context:, tag:, table:)
19
+ builder = GlyfLocaBuilder.new(font: context.font,
20
+ mapping: context.mapping).build
21
+ context.state.glyf_data = builder.glyf_data
22
+ context.state.loca_offsets = builder.loca_offsets
23
+ context.state.subset_bbox = builder.bbox
24
+ builder.glyf_data
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Glyf + Loca are emitted together because loca is just an offset
7
+ # index into glyf. This builder walks the subset's glyph mapping,
8
+ # extracts each glyph's bytes (remapping composite component refs),
9
+ # and produces the new glyf bytes, the new loca offsets, and the
10
+ # union bounding box over the subset's glyphs.
11
+ #
12
+ # Extracted as a collaborator so both the Glyf and Loca strategies
13
+ # can share a single build pass without coupling to each other or
14
+ # to TableSubsetter internals.
15
+ class GlyfLocaBuilder
16
+ # @param font [SfntFont]
17
+ # @param mapping [GlyphMapping]
18
+ def initialize(font:, mapping:)
19
+ @font = font
20
+ @mapping = mapping
21
+ end
22
+
23
+ # @return [String] new glyf bytes
24
+ attr_reader :glyf_data
25
+
26
+ # @return [Array<Integer>] loca offsets (one per glyph + 1)
27
+ attr_reader :loca_offsets
28
+
29
+ # @return [Array(Integer, Integer, Integer, Integer), nil] union
30
+ # bbox as [x_min, y_min, x_max, y_max], or nil if all glyphs empty
31
+ attr_reader :bbox
32
+
33
+ # Build glyf + loca + bbox. Idempotent — re-calling is a no-op.
34
+ #
35
+ # @return [self]
36
+ def build
37
+ return self if @glyf_data
38
+
39
+ glyf = @font.table("glyf")
40
+ loca = @font.table("loca")
41
+ head = @font.table("head")
42
+
43
+ ensure_loca_parsed!(loca, head)
44
+
45
+ @glyf_data = String.new(encoding: Encoding::BINARY)
46
+ @loca_offsets = []
47
+ current_offset = 0
48
+
49
+ bbox_x_min = 1 << 30
50
+ bbox_y_min = 1 << 30
51
+ bbox_x_max = -(1 << 30)
52
+ bbox_y_max = -(1 << 30)
53
+
54
+ @mapping.old_ids.each do |old_id|
55
+ @loca_offsets << current_offset
56
+
57
+ offset = loca.offset_for(old_id)
58
+ size = loca.size_of(old_id)
59
+ next if size.nil? || size.zero?
60
+
61
+ glyph_data = glyf.raw_data[offset, size]
62
+
63
+ if glyph_data.bytesize >= 10
64
+ _n, gx_min, gy_min, gx_max, gy_max = glyph_data[0, 10].unpack("n5")
65
+ gx_min = to_signed_16(gx_min)
66
+ gy_min = to_signed_16(gy_min)
67
+ gx_max = to_signed_16(gx_max)
68
+ gy_max = to_signed_16(gy_max)
69
+ bbox_x_min = gx_min if gx_min < bbox_x_min
70
+ bbox_y_min = gy_min if gy_min < bbox_y_min
71
+ bbox_x_max = gx_max if gx_max > bbox_x_max
72
+ bbox_y_max = gy_max if gy_max > bbox_y_max
73
+ end
74
+
75
+ glyph_data = remap_compound!(glyph_data) if compound?(glyph_data)
76
+
77
+ @glyf_data << glyph_data
78
+ current_offset += glyph_data.bytesize
79
+ end
80
+
81
+ @loca_offsets << current_offset
82
+
83
+ return if bbox_x_min > bbox_x_max
84
+
85
+ @bbox = [bbox_x_min, bbox_y_min, bbox_x_max, bbox_y_max]
86
+ self
87
+ end
88
+
89
+ private
90
+
91
+ def ensure_loca_parsed!(loca, head)
92
+ return if loca.parsed?
93
+
94
+ maxp = @font.table("maxp")
95
+ loca.parse_with_context(head.index_to_loc_format, maxp.num_glyphs)
96
+ end
97
+
98
+ def compound?(data)
99
+ return false if data.length < 2
100
+
101
+ num_contours = to_signed_16(data[0, 2].unpack1("n"))
102
+ num_contours == -1
103
+ end
104
+
105
+ def remap_compound!(data)
106
+ new_data = data.dup
107
+ offset = 10 # skip 10-byte header
108
+
109
+ loop do
110
+ break if offset >= new_data.length - 4
111
+
112
+ flags = new_data[offset, 2].unpack1("n")
113
+ old_glyph_index = new_data[offset + 2, 2].unpack1("n")
114
+
115
+ new_glyph_index = @mapping.new_id(old_glyph_index)
116
+ unless new_glyph_index
117
+ raise Fontisan::SubsettingError,
118
+ "Component glyph #{old_glyph_index} not in subset"
119
+ end
120
+
121
+ new_data[offset + 2, 2] = [new_glyph_index].pack("n")
122
+ offset += 4 # flags + glyph_index
123
+ offset += (flags & 0x0001).zero? ? 2 : 4 # args
124
+
125
+ if (flags & 0x0080) != 0
126
+ offset += 8 # 2x2 matrix
127
+ elsif (flags & 0x0040) != 0
128
+ offset += 4 # X and Y scale
129
+ elsif (flags & 0x0008) != 0
130
+ offset += 2 # uniform scale
131
+ end
132
+
133
+ break unless (flags & 0x0020) != 0 # MORE_COMPONENTS
134
+ end
135
+
136
+ new_data
137
+ end
138
+
139
+ def to_signed_16(value)
140
+ value > 0x7FFF ? value - 0x10000 : value
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Head strategy: recompute xMin/yMin/xMax/yMax (offsets 36–43) from
7
+ # the subset's actual glyphs. The source TTC's bbox covers every
8
+ # donor font in the collection and is far larger than any per-block
9
+ # subset needs; an inflated bbox makes browsers render glyphs at
10
+ # the wrong visual size. The remaining head fields pass through
11
+ # unchanged — checksumAdjustment is rewritten by FontWriter.
12
+ class Head
13
+ # @param context [SubsetContext]
14
+ # @param tag [String] "head"
15
+ # @param table [Head] parsed head table (unused)
16
+ # @return [String] binary head bytes for the subset
17
+ def self.call(context:, tag:, table:)
18
+ ensure_glyf_built!(context)
19
+
20
+ data = context.font.table_data["head"].dup
21
+ bbox = context.state.subset_bbox
22
+ return data unless bbox
23
+
24
+ x_min, y_min, x_max, y_max = bbox
25
+ data[36, 8] = [x_min, y_min, x_max, y_max].pack("n4")
26
+ data
27
+ end
28
+
29
+ def self.ensure_glyf_built!(context)
30
+ return if context.state.glyf_data
31
+
32
+ builder = GlyfLocaBuilder.new(font: context.font,
33
+ mapping: context.mapping).build
34
+ context.state.glyf_data = builder.glyf_data
35
+ context.state.loca_offsets = builder.loca_offsets
36
+ context.state.subset_bbox = builder.bbox
37
+ end
38
+ private_class_method :ensure_glyf_built!
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Hhea strategy: rewrite numberOfHMetrics (offset 34, uint16) and
7
+ # advanceWidthMax (offset 10, uint16). The source TTC's
8
+ # advanceWidthMax covers every donor font and can be far larger
9
+ # than any per-block subset needs; recomputing from the subset's
10
+ # actual hmtx keeps the value honest.
11
+ #
12
+ # Hhea runs alphabetically before Hmtx, so the Hmtx strategy's
13
+ # SharedState cache hasn't been populated yet — we walk the source
14
+ # hmtx directly to find the max.
15
+ class Hhea
16
+ # @param context [SubsetContext]
17
+ # @param tag [String] "hhea"
18
+ # @param table [Hhea] parsed hhea table
19
+ # @return [String] binary hhea bytes for the subset
20
+ def self.call(context:, tag:, table:)
21
+ data = table.to_binary_s.dup
22
+
23
+ new_num_h_metrics = calculate_number_of_h_metrics(context, table)
24
+ data[34, 2] = [new_num_h_metrics].pack("n")
25
+
26
+ new_max = compute_subset_max_advance(context)
27
+ data[10, 2] = [new_max].pack("n") if new_max.positive?
28
+
29
+ data
30
+ end
31
+
32
+ def self.calculate_number_of_h_metrics(context, _table)
33
+ hmtx = context.font.table("hmtx")
34
+ if hmtx&.parsed? && hmtx.h_metrics
35
+ hmtx.h_metrics.size
36
+ else
37
+ context.mapping.size
38
+ end
39
+ end
40
+
41
+ def self.compute_subset_max_advance(context)
42
+ hmtx = context.font.table("hmtx")
43
+ return 0 unless hmtx
44
+
45
+ unless hmtx.parsed?
46
+ hhea = context.font.table("hhea")
47
+ maxp = context.font.table("maxp")
48
+ hmtx.parse_with_context(hhea.number_of_h_metrics,
49
+ maxp.num_glyphs)
50
+ end
51
+
52
+ max_advance = 0
53
+ context.mapping.old_ids.each do |old_id|
54
+ metric = hmtx.metric_for(old_id)
55
+ next unless metric
56
+
57
+ advance = metric[:advance_width]
58
+ max_advance = advance if advance && advance > max_advance
59
+ end
60
+ max_advance
61
+ end
62
+ private_class_method :calculate_number_of_h_metrics,
63
+ :compute_subset_max_advance
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Hmtx strategy: emit one LongHorMetric per glyph in the subset's
7
+ # glyph mapping, in the same order as the mapping. Also records
8
+ # the maximum advanceWidth into [SharedState] so Hhea (which is
9
+ # processed alphabetically before Hmtx) can read it.
10
+ #
11
+ # Tables are processed in profile order — Hhea comes before Hmtx
12
+ # alphabetically — so Hhea reads its max advance via a private
13
+ # helper that walks the source hmtx directly. Hmtx then stashes
14
+ # the value into SharedState for any later consumer.
15
+ class Hmtx
16
+ # @param context [SubsetContext]
17
+ # @param tag [String] "hmtx"
18
+ # @param table [Hmtx] parsed hmtx table
19
+ # @return [String] binary hmtx bytes for the subset
20
+ def self.call(context:, tag:, table:)
21
+ ensure_parsed!(context, table)
22
+
23
+ data = String.new(encoding: Encoding::BINARY)
24
+ max_advance = 0
25
+
26
+ context.mapping.old_ids.each do |old_id|
27
+ metric = table.metric_for(old_id)
28
+ next unless metric
29
+
30
+ advance = metric[:advance_width]
31
+ max_advance = advance if advance && advance > max_advance
32
+ data << [advance].pack("n")
33
+ data << [metric[:lsb]].pack("n")
34
+ end
35
+
36
+ context.state.subset_max_advance = max_advance
37
+ data
38
+ end
39
+
40
+ def self.ensure_parsed!(context, table)
41
+ return if table.parsed?
42
+
43
+ hhea = context.font.table("hhea")
44
+ maxp = context.font.table("maxp")
45
+ table.parse_with_context(hhea.number_of_h_metrics,
46
+ maxp.num_glyphs)
47
+ end
48
+ private_class_method :ensure_parsed!
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Subset
5
+ module TableStrategy
6
+ # Loca strategy: emit one entry per subset glyph + a final offset,
7
+ # using short or long format based on the source head.index_to_loc_format.
8
+ # Glyph offsets come from [SharedState], populated earlier by the
9
+ # Glyf strategy. If Glyf hasn't run yet (e.g. direct invocation
10
+ # from a spec), this strategy triggers the build lazily.
11
+ class Loca
12
+ # @param context [SubsetContext]
13
+ # @param tag [String] "loca"
14
+ # @param table [Loca] parsed loca table (unused)
15
+ # @return [String] binary loca bytes for the subset
16
+ def self.call(context:, tag:, table:)
17
+ offsets = context.state.loca_offsets
18
+ if offsets.nil?
19
+ builder = GlyfLocaBuilder.new(font: context.font,
20
+ mapping: context.mapping).build
21
+ context.state.glyf_data = builder.glyf_data
22
+ context.state.loca_offsets = builder.loca_offsets
23
+ context.state.subset_bbox = builder.bbox
24
+ offsets = builder.loca_offsets
25
+ end
26
+
27
+ head = context.font.table("head")
28
+ data = String.new(encoding: Encoding::BINARY)
29
+
30
+ if head.index_to_loc_format.zero?
31
+ offsets.each { |o| data << [o / 2].pack("n") }
32
+ else
33
+ offsets.each { |o| data << [o].pack("N") }
34
+ end
35
+
36
+ data
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end