fontisan 0.2.23 → 0.3.0

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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fontisan/cli.rb +6 -0
  3. data/lib/fontisan/stitcher/selector/codepoints.rb +29 -0
  4. data/lib/fontisan/stitcher/selector/gid.rb +25 -0
  5. data/lib/fontisan/stitcher/selector/range.rb +30 -0
  6. data/lib/fontisan/stitcher/selector.rb +26 -0
  7. data/lib/fontisan/stitcher/source.rb +97 -0
  8. data/lib/fontisan/stitcher.rb +182 -0
  9. data/lib/fontisan/stitcher_cli.rb +69 -0
  10. data/lib/fontisan/ufo/anchor.rb +17 -0
  11. data/lib/fontisan/ufo/cli.rb +85 -0
  12. data/lib/fontisan/ufo/compile/base_compiler.rb +81 -0
  13. data/lib/fontisan/ufo/compile/cff.rb +224 -0
  14. data/lib/fontisan/ufo/compile/cmap.rb +129 -0
  15. data/lib/fontisan/ufo/compile/filters/cubic_to_quadratic.rb +174 -0
  16. data/lib/fontisan/ufo/compile/filters/decompose_components.rb +33 -0
  17. data/lib/fontisan/ufo/compile/filters/flatten_components.rb +22 -0
  18. data/lib/fontisan/ufo/compile/filters/reverse_contour_direction.rb +27 -0
  19. data/lib/fontisan/ufo/compile/filters.rb +57 -0
  20. data/lib/fontisan/ufo/compile/glyf_loca.rb +145 -0
  21. data/lib/fontisan/ufo/compile/head.rb +98 -0
  22. data/lib/fontisan/ufo/compile/hhea.rb +36 -0
  23. data/lib/fontisan/ufo/compile/hmtx.rb +27 -0
  24. data/lib/fontisan/ufo/compile/maxp.rb +57 -0
  25. data/lib/fontisan/ufo/compile/name.rb +79 -0
  26. data/lib/fontisan/ufo/compile/os2.rb +81 -0
  27. data/lib/fontisan/ufo/compile/otf_compiler.rb +43 -0
  28. data/lib/fontisan/ufo/compile/post.rb +32 -0
  29. data/lib/fontisan/ufo/compile/ttf_compiler.rb +69 -0
  30. data/lib/fontisan/ufo/compile.rb +48 -0
  31. data/lib/fontisan/ufo/component.rb +18 -0
  32. data/lib/fontisan/ufo/contour.rb +29 -0
  33. data/lib/fontisan/ufo/convert/from_bin_data.rb +246 -0
  34. data/lib/fontisan/ufo/convert.rb +18 -0
  35. data/lib/fontisan/ufo/data_set.rb +21 -0
  36. data/lib/fontisan/ufo/features.rb +17 -0
  37. data/lib/fontisan/ufo/font.rb +61 -0
  38. data/lib/fontisan/ufo/glyph.rb +421 -0
  39. data/lib/fontisan/ufo/guideline.rb +19 -0
  40. data/lib/fontisan/ufo/image.rb +16 -0
  41. data/lib/fontisan/ufo/image_set.rb +19 -0
  42. data/lib/fontisan/ufo/info.rb +79 -0
  43. data/lib/fontisan/ufo/kerning.rb +32 -0
  44. data/lib/fontisan/ufo/layer.rb +38 -0
  45. data/lib/fontisan/ufo/layer_set.rb +37 -0
  46. data/lib/fontisan/ufo/lib.rb +24 -0
  47. data/lib/fontisan/ufo/plist.rb +118 -0
  48. data/lib/fontisan/ufo/point.rb +38 -0
  49. data/lib/fontisan/ufo/reader.rb +144 -0
  50. data/lib/fontisan/ufo/transformation.rb +39 -0
  51. data/lib/fontisan/ufo/writer.rb +115 -0
  52. data/lib/fontisan/ufo.rb +44 -0
  53. data/lib/fontisan/version.rb +1 -1
  54. data/lib/fontisan.rb +3 -0
  55. metadata +51 -1
@@ -0,0 +1,224 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stringio"
4
+
5
+ module Fontisan
6
+ module Ufo
7
+ module Compile
8
+ # Builds the OpenType `CFF ` table from UFO glyphs.
9
+ #
10
+ # Pipeline:
11
+ # 1. Convert each UFO glyph's contours to a Models::Outline
12
+ # (cubic Bezier commands).
13
+ # 2. Encode each outline as a Type 2 charstring via the
14
+ # existing `Tables::Cff::CharStringBuilder`.
15
+ # 3. Build the CFF structural INDEXes (Name, Top DICT, String,
16
+ # Global Subr, CharStrings) with correct offsets.
17
+ #
18
+ # The Top DICT references absolute offsets to the charset,
19
+ # charstrings, and private dict. Those offsets depend on the
20
+ # size of everything that comes before them — including the Top
21
+ # DICT itself. We resolve the circular dependency with a
22
+ # fixed-point iteration: build the Top DICT, compute offsets,
23
+ # rebuild the Top DICT with the new offsets, repeat until it
24
+ # converges (typically 2 iterations).
25
+ module Cff
26
+ # @param font [Fontisan::Ufo::Font]
27
+ # @param glyphs [Array<Fontisan::Ufo::Glyph>] in gid order
28
+ # @return [String] CFF table bytes
29
+ def self.build(font, glyphs:)
30
+ name = font.info.postscript_font_name || font.info.family_name || "Untitled"
31
+ charstrings = glyphs.map { |g| charstring_for(g) }
32
+ private_dict = +""
33
+
34
+ layout = compute_layout(name: name, glyphs: glyphs,
35
+ charstrings: charstrings,
36
+ private_dict: private_dict)
37
+
38
+ assemble(layout: layout)
39
+ end
40
+
41
+ # ---------- charstring per-glyph ----------
42
+
43
+ def self.charstring_for(glyph)
44
+ return empty_charstring(glyph.width.to_i) if glyph.contours.empty?
45
+
46
+ builder = Fontisan::Tables::Cff::CharStringBuilder.new
47
+ builder.build(glyph.to_outline, width: glyph.width.to_i)
48
+ rescue StandardError
49
+ # Any failure (e.g., too-short contours, unsupported curve
50
+ # combination): fall back to an empty charstring so the
51
+ # INDEX stays valid.
52
+ empty_charstring(glyph.width.to_i)
53
+ end
54
+
55
+ def self.empty_charstring(width)
56
+ Fontisan::Tables::Cff::CharStringBuilder.build_empty(width: width.zero? ? nil : width)
57
+ end
58
+
59
+ # ---------- layout ----------
60
+
61
+ # Compute the byte offsets of every CFF structural section.
62
+ # Iterates until the Top DICT's encoded size stabilizes.
63
+ def self.compute_layout(name:, glyphs:, charstrings:, private_dict:)
64
+ name_index = index_bytes([name.b])
65
+ string_index = index_bytes([])
66
+ global_subr_index = index_bytes([])
67
+ charset = charset_bytes(glyphs)
68
+ charstrings_index = index_bytes(charstrings)
69
+
70
+ # First guess: Top DICT encoded with all-zero offsets.
71
+ top_dict = top_dict_bytes(0, 0, 0, 0)
72
+ top_dict_index = index_bytes([top_dict])
73
+
74
+ charset_offset = 0
75
+ charstrings_offset = 0
76
+ private_offset = 0
77
+
78
+ 10.times do
79
+ header_size = 4
80
+ post_top_dict = header_size + name_index.bytesize +
81
+ top_dict_index.bytesize + string_index.bytesize +
82
+ global_subr_index.bytesize
83
+
84
+ charset_offset = post_top_dict
85
+ charstrings_offset = charset_offset + charset.bytesize
86
+ private_offset = charstrings_offset + charstrings_index.bytesize
87
+
88
+ new_top_dict = top_dict_bytes(
89
+ charset_offset,
90
+ charstrings_offset,
91
+ private_dict.bytesize,
92
+ private_offset,
93
+ )
94
+ new_top_dict_index = index_bytes([new_top_dict])
95
+
96
+ if new_top_dict_index.bytesize == top_dict_index.bytesize
97
+ # Converged. Use the freshly-encoded Top DICT so the
98
+ # layout hash is self-consistent with the offsets.
99
+ top_dict_index = new_top_dict_index
100
+ break
101
+ end
102
+
103
+ top_dict_index = new_top_dict_index
104
+ end
105
+
106
+ {
107
+ name_index: name_index,
108
+ top_dict_index: top_dict_index,
109
+ string_index: string_index,
110
+ global_subr_index: global_subr_index,
111
+ charset_offset: charset_offset,
112
+ charstrings_offset: charstrings_offset,
113
+ private_offset: private_offset,
114
+ charset: charset,
115
+ charstrings_index: charstrings_index,
116
+ private_dict: private_dict,
117
+ }
118
+ end
119
+
120
+ # ---------- byte emission ----------
121
+
122
+ def self.assemble(layout:)
123
+ io = StringIO.new("".b)
124
+
125
+ io.write([1, 0, 4, 1].pack("CCCC")) # CFF header (v1.0, hdrSize=4, offSize=1)
126
+
127
+ io.write(layout[:name_index])
128
+ io.write(layout[:top_dict_index])
129
+ io.write(layout[:string_index])
130
+ io.write(layout[:global_subr_index])
131
+
132
+ io.write(layout[:charset])
133
+ io.write(layout[:charstrings_index])
134
+ io.write(layout[:private_dict])
135
+
136
+ io.string
137
+ end
138
+
139
+ # Top DICT bytes with the three standard offset operators.
140
+ # Operators: charset(15), CharStrings(17), Private(18).
141
+ # Encoding (operator 16) is implicit for OpenType fonts.
142
+ def self.top_dict_bytes(charset_offset, charstrings_offset,
143
+ private_size, private_offset)
144
+ bytes = +""
145
+ bytes << encode_int(charset_offset) << "\x0f" # charset
146
+ bytes << encode_int(charstrings_offset) << "\x11" # CharStrings
147
+ bytes << encode_int(private_size) << encode_int(private_offset) << "\x12" # Private
148
+ bytes
149
+ end
150
+
151
+ # Format 0 charset: 1 format byte + (n-1) SIDs.
152
+ # Gid 0 is implicit .notdef.
153
+ def self.charset_bytes(glyphs)
154
+ return +"" if glyphs.size <= 1
155
+
156
+ bytes = +"\x00"
157
+ (1...glyphs.size).each { |_| bytes << [0].pack("n") } # SID 0 placeholder
158
+ bytes
159
+ end
160
+
161
+ # ---------- INDEX helpers ----------
162
+
163
+ def self.index_bytes(items)
164
+ io = StringIO.new("".b)
165
+ if items.empty?
166
+ io.write([0].pack("n"))
167
+ return io.string
168
+ end
169
+
170
+ count = items.size
171
+ offsets = [1]
172
+ items.each { |item| offsets << offsets.last + item.bytesize }
173
+ max_offset = offsets.last
174
+ off_size = byte_size_for(max_offset)
175
+
176
+ io.write([count, off_size].pack("nC"))
177
+ offsets.each { |o| io.write(pack_offset(o, off_size)) }
178
+ items.each { |item| io.write(item) }
179
+ io.string
180
+ end
181
+
182
+ def self.byte_size_for(max_value)
183
+ return 1 if max_value <= 0xFF
184
+ return 2 if max_value <= 0xFFFF
185
+ return 3 if max_value <= 0xFFFFFF
186
+
187
+ 4
188
+ end
189
+
190
+ def self.pack_offset(value, size)
191
+ case size
192
+ when 1 then [value].pack("C")
193
+ when 2 then [value].pack("n")
194
+ when 3
195
+ [(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF].pack("CCC")
196
+ when 4 then [value].pack("N")
197
+ end
198
+ end
199
+
200
+ # CFF DICT integer encoding (CFF spec section 3.1).
201
+ def self.encode_int(value)
202
+ if value.between?(-107, 107)
203
+ [value + 139].pack("C")
204
+ elsif value.between?(108, 1131)
205
+ v = value - 108
206
+ [(v / 256) + 247, v % 256].pack("CC")
207
+ elsif value.between?(-1131, -108)
208
+ v = -value - 108
209
+ [-(v / 256) - 247, v % 256].pack("CC")
210
+ elsif value.between?(-32_768, 32_767)
211
+ [28, value].pack("Cn")
212
+ else
213
+ [29, value].pack("CN")
214
+ end
215
+ end
216
+
217
+ private_class_method :charstring_for, :empty_charstring,
218
+ :compute_layout, :assemble, :top_dict_bytes,
219
+ :charset_bytes, :index_bytes, :byte_size_for,
220
+ :pack_offset, :encode_int
221
+ end
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Ufo
5
+ module Compile
6
+ # Builds the OpenType `cmap` (character-to-glyph mapping) table.
7
+ # Emits two subtables:
8
+ # - Format 4 (BMP), platform 3 encoding 1 (Windows Unicode BMP)
9
+ # - Format 12 (full Unicode), platform 3 encoding 10 (Windows Unicode full)
10
+ #
11
+ # Both subtables share the same segment list (capped to BMP for
12
+ # format 4); format 4 is required by Windows even though format
13
+ # 12 is more capable.
14
+ # @see https://learn.microsoft.com/en-us/typography/opentype/spec/cmap
15
+ module Cmap
16
+ PLATFORM_WINDOWS = 3
17
+ ENCODING_WINDOWS_BMP = 1
18
+ ENCODING_WINDOWS_FULL = 10
19
+
20
+ # @param _font [Fontisan::Ufo::Font]
21
+ # @param glyphs [Array<Fontisan::Ufo::Glyph>] in gid order
22
+ # @return [String] cmap table bytes
23
+ def self.build(_font, glyphs:)
24
+ mappings = {}
25
+ glyphs.each_with_index do |glyph, gid|
26
+ glyph.unicodes.each do |cp|
27
+ mappings[cp] = gid unless mappings.key?(cp)
28
+ end
29
+ end
30
+
31
+ subtable_bmp = format4_subtable(mappings.reject { |cp, _| cp > 0xFFFF })
32
+ subtable_full = format12_subtable(mappings)
33
+
34
+ header_size = 4 + (2 * 8) # version + numTables + 2 records
35
+ offset_bmp = header_size
36
+ offset_full = header_size + subtable_bmp.bytesize
37
+
38
+ header = [0, 2].pack("nn")
39
+ header << subtable_record(PLATFORM_WINDOWS, ENCODING_WINDOWS_BMP, offset_bmp)
40
+ header << subtable_record(PLATFORM_WINDOWS, ENCODING_WINDOWS_FULL, offset_full)
41
+ header + subtable_bmp + subtable_full
42
+ end
43
+
44
+ def self.subtable_record(platform_id, encoding_id, offset)
45
+ [platform_id, encoding_id, offset].pack("nnN")
46
+ end
47
+
48
+ # Group adjacent (cp, gid) pairs into contiguous ranges where
49
+ # both cp and gid advance by 1 each step.
50
+ # @return [Array<Range>] Array of inclusive cp ranges
51
+ def self.build_segments(cp_to_gid)
52
+ sorted = cp_to_gid.sort
53
+ return [] if sorted.empty?
54
+
55
+ segments = []
56
+ seg_start_cp, prev_gid = sorted.first
57
+ prev_cp = seg_start_cp
58
+
59
+ sorted[1..].each do |cp, gid|
60
+ contiguous = cp == prev_cp + 1 && gid == prev_gid + 1
61
+ unless contiguous
62
+ segments << (seg_start_cp..prev_cp)
63
+ seg_start_cp = cp
64
+ end
65
+ prev_cp = cp
66
+ prev_gid = gid
67
+ end
68
+ segments << (seg_start_cp..prev_cp)
69
+ segments
70
+ end
71
+
72
+ # Format 4 segment-encoded cmap subtable for the BMP.
73
+ def self.format4_subtable(mappings)
74
+ segments = build_segments(mappings) + [0xFFFF..0xFFFF] # sentinel
75
+ seg_count = segments.size
76
+ search_range = largest_pow2_le(seg_count) * 2
77
+ entry_selector = (Math.log([1, search_range / 2].max) / Math.log(2)).to_i
78
+ range_shift = seg_count * 2 - search_range
79
+
80
+ end_codes = segments.map(&:end)
81
+ start_codes = segments.map(&:begin)
82
+ # id_delta[i] = (gid_at_start - start_code) mod 65536
83
+ id_deltas = segments.map do |range|
84
+ start_cp = range.begin
85
+ gid = mappings.fetch(start_cp, 0)
86
+ # For the sentinel segment (0xFFFF..0xFFFF) with no mapping,
87
+ # delta 1 maps 0xFFFF → gid 0 (.notdef).
88
+ gid_delta = start_cp == 0xFFFF && gid.zero? ? 1 : (gid - start_cp)
89
+ gid_delta & 0xFFFF
90
+ end
91
+
92
+ body = +""
93
+ body << [seg_count * 2, search_range, entry_selector, range_shift].pack("nnnn")
94
+ body << end_codes.pack("n*")
95
+ body << [0].pack("n") # reservedPad
96
+ body << start_codes.pack("n*")
97
+ body << id_deltas.pack("n*")
98
+ body << Array.new(seg_count, 0).pack("n*") # idRangeOffset (all 0)
99
+
100
+ length = 14 + body.bytesize # 14-byte header
101
+ [4, length, 0].pack("nnn") + body
102
+ end
103
+
104
+ # Format 12 sparse-coverage subtable for full Unicode.
105
+ def self.format12_subtable(mappings)
106
+ segments = build_segments(mappings)
107
+
108
+ body = +""
109
+ segments.each do |range|
110
+ start_gid = mappings.fetch(range.begin, 0)
111
+ body << [range.begin, range.end, start_gid].pack("NNN")
112
+ end
113
+
114
+ length = 16 + body.bytesize # 16-byte header
115
+ [12, 0].pack("nn") + [length, 0, segments.size].pack("NNN") + body
116
+ end
117
+
118
+ def self.largest_pow2_le(n)
119
+ return 0 if n <= 0
120
+
121
+ 1 << (n.bit_length - 1)
122
+ end
123
+ private_class_method :subtable_record, :build_segments,
124
+ :format4_subtable, :format12_subtable,
125
+ :largest_pow2_le
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Ufo
5
+ module Compile
6
+ module Filters
7
+ # Converts cubic Bezier curves to quadratic. TrueType outlines
8
+ # only support quadratic Beziers; UFO sources typically use
9
+ # cubic. This filter walks each contour, finds cubic segments
10
+ # (two consecutive off-curve points followed by an on-curve),
11
+ # and replaces them with one or more quadratic approximations.
12
+ #
13
+ # Algorithm (fontTools-compatible):
14
+ # For cubic (P0, C1, C2, P3):
15
+ # Q1 = (3*C1 - P0) / 2
16
+ # Q2 = (3*C2 - P3) / 2
17
+ # Q = midpoint(Q1, Q2)
18
+ # Error ≈ |Q1 - Q2| / 3
19
+ # If error ≤ tolerance: emit one quadratic (P0, Q, P3)
20
+ # Else: subdivide at t=0.5, recurse each half.
21
+ module CubicToQuadratic
22
+ DEFAULT_TOLERANCE = 1.0
23
+
24
+ # @param glyphs [Array<Fontisan::Ufo::Glyph>]
25
+ # @param tolerance [Float] max deviation in font units
26
+ # @return [Array<Fontisan::Ufo::Glyph>] the same array, mutated
27
+ def self.run(glyphs, tolerance: DEFAULT_TOLERANCE, **_opts)
28
+ glyphs.each do |glyph|
29
+ glyph.contours.each_with_index do |contour, _ci|
30
+ contour.points = convert_contour(contour.points, tolerance)
31
+ end
32
+ end
33
+ glyphs
34
+ end
35
+
36
+ # Walk a contour's point list and replace cubic segments
37
+ # with quadratic approximations. Preserves on-curve points
38
+ # (lines, moves); only touches (off, off, on) triplets.
39
+ #
40
+ # @param points [Array<Fontisan::Ufo::Point>]
41
+ # @param tolerance [Float]
42
+ # @return [Array<Fontisan::Ufo::Point>] new point array
43
+ def self.convert_contour(points, tolerance)
44
+ return points if points.size < 4
45
+
46
+ result = []
47
+ i = 0
48
+
49
+ while i < points.size
50
+ # Check if we have a cubic segment: off, off, on-curve
51
+ if i + 2 < points.size &&
52
+ off_curve?(points[i]) &&
53
+ off_curve?(points[i + 1]) &&
54
+ on_curve?(points[i + 2])
55
+ # The previous on-curve point (P0) is the last on-curve
56
+ # before this segment. If result is empty, use the last
57
+ # point in the original contour (contour wraps around).
58
+ p0 = result.empty? ? last_on_curve(points) : result.last
59
+
60
+ if p0
61
+ c1 = points[i]
62
+ c2 = points[i + 1]
63
+ p3 = points[i + 2]
64
+
65
+ quads = subdivide_cubic(p0, c1, c2, p3, tolerance)
66
+ quads.each { |q| result << q }
67
+ i += 3
68
+ else
69
+ result << points[i]
70
+ i += 1
71
+ end
72
+ else
73
+ result << points[i]
74
+ i += 1
75
+ end
76
+ end
77
+
78
+ result
79
+ end
80
+
81
+ # Recursive cubic-to-quadratic subdivision.
82
+ # Returns a list of Points: alternating off-curve control
83
+ # points and on-curve endpoints.
84
+ def self.subdivide_cubic(p0, c1, c2, p3, tolerance)
85
+ q1x = (3.0 * c1.x - p0.x) / 2.0
86
+ q1y = (3.0 * c1.y - p0.y) / 2.0
87
+ q2x = (3.0 * c2.x - p3.x) / 2.0
88
+ q2y = (3.0 * c2.y - p3.y) / 2.0
89
+
90
+ # Error metric: half the distance between Q1 and Q2
91
+ dx = (q1x - q2x).abs
92
+ dy = (q1y - q2y).abs
93
+ error = [dx, dy].max / 3.0
94
+
95
+ if error <= tolerance
96
+ # Single quadratic approximation
97
+ mid_x = (q1x + q2x) / 2.0
98
+ mid_y = (q1y + q2y) / 2.0
99
+ [
100
+ Point.new(x: mid_x, y: mid_y, type: "offcurve"),
101
+ Point.new(x: p3.x, y: p3.y, type: p3.type, smooth: p3.smooth),
102
+ ]
103
+ else
104
+ # Subdivide at t=0.5
105
+ midpoint_on_cubic(p0, c1, c2, p3, 0.5)
106
+
107
+ # Left half: P0, L1, L2, M
108
+ Point.new(x: (p0.x + c1.x) / 2.0,
109
+ y: (p0.y + c1.y) / 2.0, type: "offcurve")
110
+ Point.new(x: (c1.x + c2.x) / 4.0 + (p0.x + c1.x) / 4.0,
111
+ y: (c1.y + c2.y) / 4.0 + (p0.y + c1.y) / 4.0,
112
+ type: "offcurve")
113
+ # Actually, proper De Casteljau subdivision:
114
+ l1x = (p0.x + c1.x) / 2.0
115
+ l1y = (p0.y + c1.y) / 2.0
116
+ mx = (c1.x + c2.x) / 2.0
117
+ my = (c1.y + c2.y) / 2.0
118
+ r2x = (c2.x + p3.x) / 2.0
119
+ r2y = (c2.y + p3.y) / 2.0
120
+ l2x = (l1x + mx) / 2.0
121
+ l2y = (l1y + my) / 2.0
122
+ r1x = (mx + r2x) / 2.0
123
+ r1y = (my + r2y) / 2.0
124
+ mid_x = (l2x + r1x) / 2.0
125
+ mid_y = (l2y + r1y) / 2.0
126
+
127
+ left_p0 = p0
128
+ left_c1 = Point.new(x: l1x, y: l1y, type: "offcurve")
129
+ left_c2 = Point.new(x: l2x, y: l2y, type: "offcurve")
130
+ left_p3 = Point.new(x: mid_x, y: mid_y, type: "qcurve")
131
+
132
+ right_p0 = left_p3
133
+ right_c1 = Point.new(x: r1x, y: r1y, type: "offcurve")
134
+ right_c2 = Point.new(x: r2x, y: r2y, type: "offcurve")
135
+ right_p3 = p3
136
+
137
+ left = subdivide_cubic(left_p0, left_c1, left_c2, left_p3, tolerance)
138
+ right = subdivide_cubic(right_p0, right_c1, right_c2, right_p3, tolerance)
139
+
140
+ # Drop the duplicated midpoint point between halves
141
+ left + right
142
+ end
143
+ end
144
+
145
+ # Evaluate a point on a cubic Bezier at parameter t.
146
+ def self.midpoint_on_cubic(p0, c1, c2, p3, t)
147
+ mt = 1.0 - t
148
+ x = mt * mt * mt * p0.x + 3 * mt * mt * t * c1.x +
149
+ 3 * mt * t * t * c2.x + t * t * t * p3.x
150
+ y = mt * mt * mt * p0.y + 3 * mt * mt * t * c1.y +
151
+ 3 * mt * t * t * c2.y + t * t * t * p3.y
152
+ Point.new(x: x, y: y, type: "qcurve")
153
+ end
154
+
155
+ def self.last_on_curve(points)
156
+ points.reverse_each.find { |p| on_curve?(p) }
157
+ end
158
+
159
+ def self.on_curve?(point)
160
+ ["line", "move", "curve", "qcurve"].include?(point.type)
161
+ end
162
+
163
+ def self.off_curve?(point)
164
+ point.type == "offcurve"
165
+ end
166
+
167
+ private_class_method :convert_contour, :subdivide_cubic,
168
+ :midpoint_on_cubic, :last_on_curve,
169
+ :on_curve?, :off_curve?
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Ufo
5
+ module Compile
6
+ module Filters
7
+ # Decomposes composite glyphs (those with Components) into
8
+ # simple glyphs by resolving each component reference to its
9
+ # base glyph's contours and applying the component's
10
+ # transformation matrix.
11
+ #
12
+ # For MVP: components are silently dropped (the glyph retains
13
+ # its own contours but loses component-derived outlines).
14
+ # Full resolution requires the full glyph-name → glyph lookup
15
+ # that the compiler provides. TODO.full/07b will wire this.
16
+ module DecomposeComponents
17
+ def self.run(glyphs, **_opts)
18
+ glyphs.each do |glyph|
19
+ # Drop components; keep contours only.
20
+ # Full implementation would:
21
+ # 1. Look up each component's base glyph by name
22
+ # 2. Clone its contours
23
+ # 3. Apply the component's Transformation matrix
24
+ # 4. Merge into this glyph's contours
25
+ glyph.components.clear
26
+ end
27
+ glyphs
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Ufo
5
+ module Compile
6
+ module Filters
7
+ # Flattens nested component references. If glyph A references
8
+ # glyph B which references glyph C, this filter makes A
9
+ # directly reference C (one level deep only).
10
+ #
11
+ # For MVP: same as DecomposeComponents — components are
12
+ # cleared. Full implementation lands with TODO.full/07b.
13
+ module FlattenComponents
14
+ def self.run(glyphs, **_opts)
15
+ glyphs.each { |g| g.components.clear }
16
+ glyphs
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Ufo
5
+ module Compile
6
+ module Filters
7
+ # Reverses the point order in every contour. TrueType fonts
8
+ # use clockwise winding for outer contours (opposite of the
9
+ # PostScript/UFO convention). Applying this filter before
10
+ # TTF compilation ensures correct glyph rendering.
11
+ module ReverseContourDirection
12
+ # @param glyphs [Array<Fontisan::Ufo::Glyph>]
13
+ # @return [Array<Fontisan::Ufo::Glyph>] the same array,
14
+ # mutated in place
15
+ def self.run(glyphs, **_opts)
16
+ glyphs.each do |glyph|
17
+ glyph.contours.each do |contour|
18
+ contour.points.reverse!
19
+ end
20
+ end
21
+ glyphs
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Ufo
5
+ module Compile
6
+ # Glyph-processing filters applied between the UFO model and the
7
+ # binary table compilers. Each filter is a stateless module
8
+ # with `.run(glyphs, **opts)` that mutates the glyph list
9
+ # (or returns a new one).
10
+ #
11
+ # OCP: adding a filter = new module + one REGISTRY entry.
12
+ # The compiler picks which filters to run based on the output
13
+ # format — not a switch statement in compiler code.
14
+ module Filters
15
+ autoload :ReverseContourDirection,
16
+ "fontisan/ufo/compile/filters/reverse_contour_direction"
17
+ autoload :CubicToQuadratic,
18
+ "fontisan/ufo/compile/filters/cubic_to_quadratic"
19
+ autoload :DecomposeComponents,
20
+ "fontisan/ufo/compile/filters/decompose_components"
21
+ autoload :FlattenComponents,
22
+ "fontisan/ufo/compile/filters/flatten_components"
23
+
24
+ # Filters that MUST run for TTF output (TrueType only
25
+ # supports quadratic curves + clockwise outer winding).
26
+ TTF_REQUIRED = %i[
27
+ cubic_to_quadratic
28
+ reverse_contour_direction
29
+ ].freeze
30
+
31
+ # Filters that MUST run for OTF output (CFF handles cubic
32
+ # natively, so no curve conversion needed; winding is
33
+ # already correct for PostScript).
34
+ OTF_REQUIRED = [].freeze
35
+
36
+ REGISTRY = {
37
+ reverse_contour_direction: ReverseContourDirection,
38
+ cubic_to_quadratic: CubicToQuadratic,
39
+ decompose_components: DecomposeComponents,
40
+ flatten_components: FlattenComponents,
41
+ }.freeze
42
+
43
+ # @param names [Array<Symbol>] filter names from REGISTRY
44
+ # @param glyphs [Array<Fontisan::Ufo::Glyph>] glyphs to filter
45
+ # @param opts [Hash] per-filter options
46
+ # @return [Array<Fontisan::Ufo::Glyph>] the (possibly mutated) glyphs
47
+ def self.apply(names, glyphs, **)
48
+ Array(names).reduce(glyphs) do |current, name|
49
+ klass = REGISTRY[name.to_sym] or
50
+ raise ArgumentError, "unknown filter: #{name.inspect}"
51
+ klass.run(current, **)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end