fontisan 0.4.47 → 0.4.49

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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/BENCHMARKS.md +56 -0
  3. data/CHANGELOG.md +12 -0
  4. data/Cargo.lock +550 -0
  5. data/Cargo.toml +12 -0
  6. data/Gemfile +10 -0
  7. data/README.adoc +69 -0
  8. data/Rakefile +5 -1
  9. data/benchmark/baseline.json +5 -0
  10. data/benchmark/ci_gate.rb +217 -0
  11. data/benchmark/corpus/harness.rb +175 -0
  12. data/benchmark/corpus/ops.rb +150 -0
  13. data/benchmark/corpus/run.rb +182 -0
  14. data/benchmark/corpus/scan_child.rb +8 -0
  15. data/benchmark/corpus/tiers.rb +176 -0
  16. data/benchmark/tier_baseline.json +12 -0
  17. data/build_mingw32_stub.rb +15 -0
  18. data/docs/.vitepress/config.ts +2 -1
  19. data/docs/guide/engines.md +117 -0
  20. data/docs/reference-facade.md +130 -0
  21. data/ext/fontisan_cdylib/Cargo.lock +313 -0
  22. data/ext/fontisan_cdylib/Cargo.toml +18 -0
  23. data/ext/fontisan_cdylib/build.rs +22 -0
  24. data/ext/fontisan_cdylib/extconf.rb +36 -0
  25. data/ext/fontisan_cdylib/src/lib.rs +13 -0
  26. data/ext/fontisan_native/Cargo.toml +21 -0
  27. data/ext/fontisan_native/build.rs +22 -0
  28. data/ext/fontisan_native/clippy.toml +10 -0
  29. data/ext/fontisan_native/extconf.rb +59 -0
  30. data/ext/fontisan_native/src/lib.rs +478 -0
  31. data/lib/fontisan/audit/checks/cmap_check.rb +7 -0
  32. data/lib/fontisan/audit/checks/hinting_check.rb +1 -1
  33. data/lib/fontisan/audit/checks/maxp_check.rb +1 -1
  34. data/lib/fontisan/audit/checks/opentype_conformance_check.rb +5 -2
  35. data/lib/fontisan/base_collection.rb +30 -0
  36. data/lib/fontisan/byte_ops.rb +314 -0
  37. data/lib/fontisan/cli.rb +11 -5
  38. data/lib/fontisan/commands/audit_command.rb +38 -43
  39. data/lib/fontisan/commands/convert_command.rb +55 -1
  40. data/lib/fontisan/commands/instance_command.rb +75 -0
  41. data/lib/fontisan/commands/multi_format_output.rb +6 -1
  42. data/lib/fontisan/commands/subset_command.rb +43 -0
  43. data/lib/fontisan/commands/unpack_command.rb +56 -0
  44. data/lib/fontisan/converters/cff_table_builder.rb +1 -1
  45. data/lib/fontisan/converters/collection_converter.rb +13 -5
  46. data/lib/fontisan/converters/format_converter.rb +1 -1
  47. data/lib/fontisan/converters/woff2_encoder.rb +8 -6
  48. data/lib/fontisan/converters/woff_writer.rb +6 -5
  49. data/lib/fontisan/engine.rb +393 -0
  50. data/lib/fontisan/ffi/client.rb +123 -0
  51. data/lib/fontisan/ffi.rb +387 -0
  52. data/lib/fontisan/ffi_platform.rb +24 -0
  53. data/lib/fontisan/font_loader.rb +5 -1
  54. data/lib/fontisan/font_writer.rb +13 -1
  55. data/lib/fontisan/native.rb +235 -0
  56. data/lib/fontisan/output_guard.rb +40 -0
  57. data/lib/fontisan/reference/table_digests.rb +251 -0
  58. data/lib/fontisan/reference.rb +663 -0
  59. data/lib/fontisan/scan.rb +372 -0
  60. data/lib/fontisan/sfnt_directory.rb +41 -0
  61. data/lib/fontisan/sfnt_font.rb +16 -6
  62. data/lib/fontisan/subset/builder.rb +2 -1
  63. data/lib/fontisan/tables/cff/charstring.rb +16 -0
  64. data/lib/fontisan/tables/cff.rb +15 -5
  65. data/lib/fontisan/tables/cmap.rb +11 -1
  66. data/lib/fontisan/tables/gpos.rb +7 -0
  67. data/lib/fontisan/tables/gsub.rb +7 -0
  68. data/lib/fontisan/tables/layout_common.rb +172 -0
  69. data/lib/fontisan/tables/name.rb +10 -4
  70. data/lib/fontisan/utilities/brotli_wrapper.rb +39 -4
  71. data/lib/fontisan/validators/opentype_validator.rb +2 -0
  72. data/lib/fontisan/variable/instancer.rb +1 -0
  73. data/lib/fontisan/variable/static_font_builder.rb +4 -1
  74. data/lib/fontisan/variable/table_updater.rb +4 -2
  75. data/lib/fontisan/variation/instancing/axis_space.rb +1 -1
  76. data/lib/fontisan/variation/instancing/true_type_instancer.rb +14 -6
  77. data/lib/fontisan/version.rb +1 -1
  78. data/lib/fontisan/woff2/collection_decoder.rb +15 -1
  79. data/lib/fontisan/woff2/collection_encoder.rb +29 -6
  80. data/lib/fontisan/woff2/encoder_rules.rb +12 -4
  81. data/lib/fontisan/woff2/glyf_loca_reconstruct.rb +1 -1
  82. data/lib/fontisan/woff2_collection.rb +7 -5
  83. data/lib/fontisan/woff2_font.rb +12 -3
  84. data/lib/fontisan/woff_container.rb +83 -0
  85. data/lib/fontisan/woff_font.rb +24 -20
  86. data/lib/fontisan.rb +25 -1
  87. data/rakelib/bench.rake +9 -0
  88. data/rakelib/cdylib.rake +39 -0
  89. data/rakelib/corpus.rake +272 -0
  90. data/rakelib/corpus_convert.rake +43 -0
  91. data/rakelib/corpus_goldens.rake +22 -0
  92. data/rakelib/corpus_malformed.rake +20 -0
  93. data/rakelib/corpus_scan.rake +24 -0
  94. data/rakelib/java.rake +61 -0
  95. data/rakelib/native.rake +43 -0
  96. data/scripts/build-woff2.sh +57 -0
  97. data/scripts/corpus/generate_convert_goldens.rb +478 -0
  98. data/scripts/corpus/generate_goldens.rb +282 -0
  99. data/scripts/corpus/generate_malformed_goldens.rb +254 -0
  100. data/scripts/corpus/generate_scan_goldens.rb +98 -0
  101. data/scripts/tier_fallback.rb +228 -0
  102. data/scripts/tier_smoke.rb +72 -0
  103. metadata +71 -7
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
3
4
  require "thor"
4
5
 
5
6
  module Fontisan
@@ -27,10 +28,27 @@ module Fontisan
27
28
  # @example Dry-run to preview
28
29
  # fontisan instance variable.ttf --wght=700 --dry-run
29
30
  class InstanceCommand < BaseCommand
31
+ # Options that make an instance request more than plain axis coordinates.
32
+ NON_PLAIN_OPTIONS = %i[validate list_instances dry_run named_instance
33
+ to optimize progress].freeze
34
+ TRUETYPE_MAGIC = "\x00\x01\x00\x00".b.freeze
35
+
36
+ # The CLI builds the command bare and hands the font to {#execute}, so
37
+ # the font is not loaded here (BaseCommand's constructor loads one).
38
+ #
39
+ # @param font_path [String, nil] unused until {#execute}
40
+ # @param options [Hash] command options
41
+ def initialize(font_path = nil, options = {}) # rubocop:disable Lint/MissingSuper
42
+ @font_path = font_path
43
+ @options = options
44
+ end
45
+
30
46
  # Instance a variable font at specified coordinates
31
47
  #
32
48
  # @param input_path [String] Path to variable font file
33
49
  def execute(input_path, options = {})
50
+ return if write_routed(input_path, options)
51
+
34
52
  # Load variable font
35
53
  font = load_font(input_path)
36
54
 
@@ -72,6 +90,34 @@ module Fontisan
72
90
 
73
91
  private
74
92
 
93
+ # Serve a plain request (axis coordinates, the font's own format, a
94
+ # TrueType-outline sfnt) through the engine selection. Named instances,
95
+ # listing, dry-run, validation, --to, --optimize, --progress, CFF2 or
96
+ # wrapped inputs stay on the Ruby path in {#execute}. The Ruby writer and
97
+ # {Reference.instance} produce identical bytes for these requests.
98
+ #
99
+ # @return [String, nil] the output path when a non-Ruby tier wrote the
100
+ # instance; nil when {#execute} must run the Ruby path
101
+ def write_routed(input_path, options)
102
+ gate = routed_gate(input_path, options)
103
+ return nil unless gate && Engine.resolve(:instance, input_path.to_s, gate) != :ruby
104
+
105
+ output_path = determine_output_path(input_path, options)
106
+ FileUtils.mkdir_p(File.dirname(output_path))
107
+ File.binwrite(output_path, Reference.instance(input_path, **gate))
108
+ puts "Static font instance written to: #{output_path}"
109
+ output_path
110
+ end
111
+
112
+ # Reference.instance keywords for a plain request; nil otherwise.
113
+ def routed_gate(input_path, options)
114
+ return nil if NON_PLAIN_OPTIONS.any? { |key| options[key] }
115
+ return nil unless File.file?(input_path) && File.binread(input_path, 4) == TRUETYPE_MAGIC
116
+
117
+ coords = extract_coordinates(options)
118
+ coords.empty? ? nil : { coords: coords }
119
+ end
120
+
75
121
  # Validate font before generating instance
76
122
  #
77
123
  # @param font [Object] Font object
@@ -164,6 +210,8 @@ module Fontisan
164
210
  # @param output_path [String] Output file path
165
211
  # @param options [Hash] Command options
166
212
  def instance_named(font, instance_index, output_path, options)
213
+ instance_index = parse_instance_index(instance_index, font)
214
+
167
215
  # Generate instance using named instance
168
216
  generator = Variation::InstanceGenerator.new(font)
169
217
  tables = generator.generate_named_instance(instance_index)
@@ -181,6 +229,33 @@ module Fontisan
181
229
  )
182
230
  end
183
231
 
232
+ # Convert the --named-instance value (a string from Thor) to a valid
233
+ # fvar instance index. The generator takes an Integer, so the conversion
234
+ # and range check live here, at the command boundary.
235
+ #
236
+ # @param value [String, Integer] raw option value
237
+ # @param font [Object] Font object
238
+ # @return [Integer] index into the fvar named instances
239
+ # @raise [ArgumentError] when not a non-negative integer or out of range
240
+ def parse_instance_index(value, font)
241
+ index = begin
242
+ Integer(value.to_s, 10)
243
+ rescue ArgumentError
244
+ raise ArgumentError,
245
+ "--named-instance must be an integer index, got #{value.inspect}"
246
+ end
247
+ raise ArgumentError, "--named-instance must be >= 0, got #{index}" if index.negative?
248
+
249
+ fvar = font.table("fvar") if font.respond_to?(:table)
250
+ count = fvar&.instances&.length
251
+ if count && index >= count
252
+ raise ArgumentError,
253
+ "--named-instance #{index} out of range (font has #{count} named instances)"
254
+ end
255
+
256
+ index
257
+ end
258
+
184
259
  # List available named instances
185
260
  #
186
261
  # @param font [Object] Font object
@@ -51,8 +51,13 @@ module Fontisan
51
51
  @target_formats.map { |fmt| "#{@base_path}.#{fmt}" }
52
52
  end
53
53
 
54
+ # A dot file whose whole name is the extension (".ttf") has one: appending
55
+ # the format would write ".ttf.ttf".
54
56
  def has_extension?
55
- !File.extname(@base_path).strip.downcase.empty?
57
+ name = File.basename(@base_path)
58
+ return true if name.length > 1 && name.start_with?(".") && name.count(".") == 1
59
+
60
+ !File.extname(name).strip.downcase.empty?
56
61
  end
57
62
  end
58
63
  end
@@ -33,6 +33,8 @@ module Fontisan
33
33
  # )
34
34
  # command.run
35
35
  class SubsetCommand < BaseCommand
36
+ ROUTED_PROFILES = %w[pdf web minimal].freeze
37
+
36
38
  # Initialize subset command
37
39
  #
38
40
  # @param font_path [String] Path to input font file
@@ -86,8 +88,49 @@ module Fontisan
86
88
  "Subsetting failed: #{e.message}"
87
89
  end
88
90
 
91
+ # Execute through the engine selection (the CLI entry): a tier other
92
+ # than Ruby subsets by text or Unicode codepoints with a profile the
93
+ # engine knows, and writes the bytes {#run} writes. Glyph-id input,
94
+ # retained GIDs, dropped hints or names, disabled Unicode-range pruning
95
+ # and verbose output run {#run} on the Ruby tier.
96
+ #
97
+ # @return [Hash] same as {#run}
98
+ def run_routed
99
+ gate = routed_gate
100
+ return run unless gate && Engine.resolve(:subset, font_path.to_s, gate) != :ruby
101
+
102
+ determine_glyph_ids # same errors as the Ruby path when nothing matches
103
+ bytes = Reference.subset(font_path, **gate)
104
+ FileUtils.mkdir_p(File.dirname(@output_path))
105
+ File.binwrite(@output_path, bytes)
106
+ routed_result(bytes, gate[:profile])
107
+ end
108
+
89
109
  private
90
110
 
111
+ # Reference.subset keywords for a plain request; nil otherwise.
112
+ def routed_gate
113
+ profile = options[:profile] || "pdf"
114
+ return nil if options[:glyphs] || options[:retain_gids] || options[:drop_hints] ||
115
+ options[:drop_names] || options[:unicode_ranges] == false || options[:verbose]
116
+ return nil unless ROUTED_PROFILES.include?(profile)
117
+
118
+ input = options[:text] ? { text: options[:text] } : { unicodes: parse_unicode(options[:unicode]) }
119
+ input.merge(profile: profile)
120
+ end
121
+
122
+ def routed_result(bytes, profile)
123
+ subset = FontLoader.load(@output_path)
124
+ {
125
+ input: font_path,
126
+ output: @output_path,
127
+ original_glyphs: font.table("maxp").num_glyphs,
128
+ subset_glyphs: subset.table("maxp").num_glyphs,
129
+ profile: profile,
130
+ size: bytes.bytesize,
131
+ }
132
+ end
133
+
91
134
  # Validate command options
92
135
  #
93
136
  # @raise [ArgumentError] If options are invalid
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "fileutils"
4
+ require "tmpdir"
4
5
 
5
6
  module Fontisan
6
7
  module Commands
@@ -98,8 +99,63 @@ module Fontisan
98
99
  raise Fontisan::Error, "Unexpected error during unpacking: #{e.message}"
99
100
  end
100
101
 
102
+ # Execute the command through the engine selection (the CLI entry): a
103
+ # tier other than Ruby serves the extraction when it can, and the result
104
+ # and the files written are those of {#run}. Anything the engine cannot
105
+ # honour (a +format+ or +prefix+, verbose output, an OTC input, a
106
+ # non-"ttcf" file, an index out of range) runs {#run} on the Ruby tier.
107
+ #
108
+ # @return [Hash] same as {#run}
109
+ def run_routed
110
+ indices = routed_indices
111
+ return run unless indices
112
+
113
+ FileUtils.mkdir_p(@output_dir)
114
+ files = indices.map { |index| extract_member(index) }
115
+ { collection: @collection_path, output_dir: @output_dir,
116
+ num_fonts: collection_member_count, fonts_extracted: files.size,
117
+ extracted_files: files }
118
+ rescue Fontisan::Error => e
119
+ raise Fontisan::Error, "Collection unpacking failed: #{e.message}"
120
+ end
121
+
101
122
  private
102
123
 
124
+ # Member indices to extract on a non-Ruby tier; nil when Ruby serves.
125
+ def routed_indices
126
+ return nil if @format || @prefix || @verbose || !ttcf_file?
127
+
128
+ count = collection_member_count
129
+ return nil if @font_index && @font_index >= count
130
+
131
+ indices = @font_index ? [@font_index] : (0...count).to_a
132
+ probe = { index: indices.first, output_dir: @output_dir.to_s }
133
+ Engine.resolve(:extract, @collection_path.to_s, probe) == :ruby ? nil : indices
134
+ end
135
+
136
+ def ttcf_file?
137
+ File.extname(@collection_path).casecmp?(".otc") ? false : File.binread(@collection_path, 4) == "ttcf"
138
+ end
139
+
140
+ # numFonts of the TTC header.
141
+ def collection_member_count
142
+ File.binread(@collection_path, 12).unpack1("@8N")
143
+ end
144
+
145
+ # One member through {Reference.extract}. A single requested index keeps
146
+ # the engine's file name; all members get Ruby's "<name>_<index>" form.
147
+ def extract_member(index)
148
+ return Reference.extract(@collection_path, index: index, output_dir: @output_dir) if @font_index
149
+
150
+ Dir.mktmpdir("fontisan-unpack") do |scratch|
151
+ file = Reference.extract(@collection_path, index: index, output_dir: scratch)
152
+ ext = File.extname(file)
153
+ target = File.join(@output_dir, "#{File.basename(file, ext)}_#{index}#{ext}")
154
+ FileUtils.mv(file, target)
155
+ target
156
+ end
157
+ end
158
+
103
159
  # Validate command options
104
160
  #
105
161
  # @raise [ArgumentError] if options are invalid
@@ -188,7 +188,7 @@ module Fontisan
188
188
  return font_name.dup.force_encoding("ASCII-8BIT") if font_name
189
189
  end
190
190
 
191
- "UnnamedFont"
191
+ "UnnamedFont".b
192
192
  end
193
193
  end
194
194
  end
@@ -262,7 +262,9 @@ conv_options = nil)
262
262
  converter = FormatConverter.new
263
263
 
264
264
  # Build converter options, including ConversionOptions if provided
265
- converter_opts = options.dup
265
+ # timestamp is a WOFF2 encoder option; the outline strategy rejects
266
+ # any option key it does not declare, even with a nil value.
267
+ converter_opts = options.except(:timestamp)
266
268
  converter_opts[:options] = conv_options if conv_options
267
269
 
268
270
  begin
@@ -288,7 +290,10 @@ conv_options = nil)
288
290
  note: "Conversion failed, kept original: #{e.message}",
289
291
  }
290
292
  else
291
- raise Error, "Font #{index} conversion failed: #{e.message}"
293
+ # Keep MissingTableError as its class so callers can rescue it
294
+ # by type; the message format is unchanged.
295
+ error_class = e.is_a?(Fontisan::MissingTableError) ? Fontisan::MissingTableError : Fontisan::Error
296
+ raise error_class, "Font #{index} conversion failed: #{e.message}"
292
297
  end
293
298
  end
294
299
  else
@@ -348,7 +353,8 @@ conv_options = nil)
348
353
  elsif font.has_table?("glyf")
349
354
  :ttf
350
355
  else
351
- raise Error, "Cannot detect font format"
356
+ raise Fontisan::MissingTableError,
357
+ "Font has neither glyf nor CFF table"
352
358
  end
353
359
  end
354
360
 
@@ -444,11 +450,13 @@ conv_options = nil)
444
450
  #
445
451
  # @param fonts [Array<Font>] Fonts
446
452
  # @param output_path [String] Output path (.woff2)
447
- # @param options [Hash] Options (looks for :brotli_quality)
453
+ # @param options [Hash] Options (looks for :brotli_quality, :timestamp)
448
454
  # @return [void]
449
455
  def repack_woff2_collection(fonts, output_path, options)
450
456
  brotli_quality = options.fetch(:brotli_quality, 11)
451
- encoder = Woff2::CollectionEncoder.new(brotli_quality:)
457
+ encoder = Woff2::CollectionEncoder.new(
458
+ brotli_quality:, timestamp: options[:timestamp],
459
+ )
452
460
  bytes = encoder.encode_fonts(fonts)
453
461
  File.binwrite(output_path, bytes)
454
462
  end
@@ -515,7 +515,7 @@ _options)
515
515
  raise ArgumentError,
516
516
  "Option(s) #{conflicts.map(&:inspect).join(', ')} do not apply " \
517
517
  "to #{source_format}→#{target_format} " \
518
- "(#{this_class.name.demodulize}). " \
518
+ "(#{this_class.name.split('::').last}). " \
519
519
  "Accepted: #{accepted_list}"
520
520
  end
521
521
 
@@ -54,6 +54,11 @@ module Fontisan
54
54
  cli: "--[no-]transform-tables",
55
55
  desc: "apply glyf/loca and hmtx transformations"
56
56
 
57
+ option :timestamp, type: :integer, default: nil,
58
+ cli: "--timestamp=SECONDS",
59
+ desc: "fixed head.modified (Unix seconds) for " \
60
+ "reproducible output; default is the clock"
61
+
57
62
  # Convert font to WOFF2 format.
58
63
  #
59
64
  # @param font [TrueTypeFont, OpenTypeFont] Source font
@@ -77,7 +82,8 @@ module Fontisan
77
82
 
78
83
  flavor = detect_flavor(font)
79
84
  table_data = collect_tables(font, options)
80
- Woff2::EncoderRules.apply!(table_data)
85
+ Woff2::EncoderRules.apply!(table_data,
86
+ timestamp: resolved[:timestamp])
81
87
 
82
88
  glyf_transform = apply_glyf_loca_transform!(table_data, font)
83
89
 
@@ -266,11 +272,7 @@ module Fontisan
266
272
  # encoder does the same — short loca halves the loca table size
267
273
  # and Chrome's OTS accepts both formats.
268
274
  max_canonical_glyf_size = glyf_data.bytesize + 3 * maxp.num_glyphs
269
- target_format = if max_canonical_glyf_size <= SHORT_LOCA_GLYF_MAX
270
- 0
271
- else
272
- source_format
273
- end
275
+ target_format = max_canonical_glyf_size <= SHORT_LOCA_GLYF_MAX ? 0 : 1
274
276
 
275
277
  canonical = Woff2::GlyfCanonicalizer.new(
276
278
  glyf_data:,
@@ -164,13 +164,14 @@ module Fontisan
164
164
  options.select { |k, _| names.include?(k.to_sym) }
165
165
  end
166
166
 
167
- # Collect all table data from font in input-font order (spec §6: tables
168
- # MUST be stored in the same order as the well-formed input font).
167
+ # Collect all table data from font, sorted ascending by tag (WOFF 1.0
168
+ # §5: the table directory MUST be sorted by tag; data follows directory
169
+ # order). Input-font order is not preserved.
169
170
  #
170
171
  # @param font [TrueTypeFont, OpenTypeFont]
171
172
  # @return [Hash<String, String>]
172
173
  def collect_tables_data(font)
173
- font.table_names.each_with_object({}) do |tag, h|
174
+ font.table_names.sort.each_with_object({}) do |tag, h|
174
175
  data = font.table_data[tag]
175
176
  h[tag] = data if data
176
177
  end
@@ -241,7 +242,7 @@ module Fontisan
241
242
  # emits header → directory → tables → optional metadata → optional
242
243
  # private data.
243
244
  #
244
- # @param compressed_tables [Hash<String, Hash>] in input-font order
245
+ # @param compressed_tables [Hash<String, Hash>] sorted by tag
245
246
  # @param font [TrueTypeFont, OpenTypeFont]
246
247
  # @param compressed_metadata [Hash, nil]
247
248
  # @param private_data [String, nil]
@@ -252,7 +253,7 @@ module Fontisan
252
253
  num_tables = compressed_tables.length
253
254
  data_start = header_size + (num_tables * 20)
254
255
 
255
- # Lay out tables in input-font order with 4-byte alignment padding.
256
+ # Lay out tables in directory (tag) order with 4-byte alignment padding.
256
257
  # Each entry: { tag:, info:, offset:, pad_bytes: }
257
258
  entries = []
258
259
  cursor = data_start