fontisan 0.4.48 → 0.4.50

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.
@@ -26,7 +26,7 @@ module Fontisan
26
26
  # engine pin in ext/*/Cargo.toml, which each build embeds. ABI_VERSION does
27
27
  # not move on a repin, so this is what keeps a stale binary from serving old
28
28
  # behaviour. A repin seds it with the Cargo files (repin-push162.sh).
29
- ENGINE_REV = "daffb3f45b67517e924a3e5d0e113a2ecbde9959"
29
+ ENGINE_REV = "f45057a12e15ba9f2c02526cd8ca0603c80edaf9"
30
30
 
31
31
  # Operations served natively. Convert, extract, instance and subset are
32
32
  # path-based only (card 2f): the object-based entry points
@@ -35,7 +35,8 @@ module Fontisan
35
35
  OPERATIONS = %i[read audit audit_dir scan convert extract instance subset].freeze
36
36
 
37
37
  # Raised by the extension for an engine failure; `details` is
38
- # `[class name, message, [[cause class, cause message], ...]]`.
38
+ # `[class name, message, [[cause class, cause message], ...], error kind]`;
39
+ # the kind is the engine's `ErrorKind` number (see {ByteOps::KIND_CLASSES}).
39
40
  class Failure < StandardError
40
41
  attr_reader :details
41
42
  end
@@ -149,7 +150,7 @@ module Fontisan
149
150
  def read_options?(options)
150
151
  return false unless options.keys.all? { |key| %i[mode lazy].include?(key) }
151
152
 
152
- %w[metadata full].include?(options.fetch(:mode, :metadata).to_s) &&
153
+ %i[metadata full].include?(options.fetch(:mode, :metadata)) &&
153
154
  [true, false].include?(options.fetch(:lazy, true))
154
155
  end
155
156
 
@@ -158,6 +159,7 @@ module Fontisan
158
159
  def convert(path, output:, timestamp: nil, **options)
159
160
  path = File.path(path)
160
161
  target = convert_target(options.fetch(:to))
162
+ output = convert_output(path, output, target)
161
163
  bytes = native_convert(path, ByteOps::TARGET_CODES.fetch(target), timestamp ? unix_seconds(timestamp) : Time.now.to_i)
162
164
  finish_convert(path, target, File.path(output), bytes)
163
165
  end
@@ -165,8 +167,10 @@ module Fontisan
165
167
  # Same file layout as UnpackCommand: <output_dir>/<basename>.
166
168
  def extract(path, index:, output_dir:)
167
169
  name, bytes = native_extract(File.path(path), index)
168
- FileUtils.mkdir_p(output_dir)
169
- File.join(output_dir, name).tap { |out| File.binwrite(out, bytes) }
170
+ unpacking_io do
171
+ FileUtils.mkdir_p(output_dir)
172
+ File.join(output_dir, name).tap { |out| File.binwrite(out, bytes) }
173
+ end
170
174
  end
171
175
 
172
176
  def instance(path, coords:, timestamp: nil)
@@ -184,7 +188,7 @@ module Fontisan
184
188
  end
185
189
 
186
190
  def read(path, mode: :metadata, lazy: true)
187
- native_read(File.path(path), mode.to_s == "metadata", lazy)
191
+ native_read(File.path(path), mode == :metadata, lazy)
188
192
  end
189
193
 
190
194
  def audit(path, validate: false)
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ # Refuses to write a conversion over the font it reads.
5
+ #
6
+ # The output is compared with the input after symlinks and `..` are resolved
7
+ # (a symlink to the input, or `dir/../input`, is the input). Every engine tier
8
+ # calls this before it converts, so none of them overwrites its own input.
9
+ module OutputGuard
10
+ # The output path is the input file.
11
+ class Refused < Error; end
12
+
13
+ module_function
14
+
15
+ # @param input [String, #to_path] the font being read
16
+ # @param output [String, #to_path] where the result would be written
17
+ # @raise [Refused] when both name the same file
18
+ # @return [void]
19
+ def check!(input, output)
20
+ source = resolve(File.path(input))
21
+ return if source.nil?
22
+ return unless resolve(File.path(output)) == source
23
+
24
+ raise Refused, "Output path #{File.path(output).inspect} is the input file " \
25
+ "#{File.path(input).inspect}; refusing to overwrite it"
26
+ end
27
+
28
+ # The real path of +path+; for a file that does not exist yet, the real
29
+ # directory plus the file name. nil when neither can be resolved.
30
+ def resolve(path)
31
+ File.realpath(path)
32
+ rescue SystemCallError
33
+ begin
34
+ File.join(File.realpath(File.dirname(path)), File.basename(path))
35
+ rescue SystemCallError
36
+ nil
37
+ end
38
+ end
39
+ end
40
+ end
@@ -50,9 +50,23 @@ module Fontisan
50
50
  is_fixed_pitch min_mem_type42 max_mem_type42 min_mem_type1
51
51
  max_mem_type1].freeze
52
52
 
53
+ # Ruby errors a malformed font can provoke inside the parsers and builders.
54
+ # They are not part of any contract; {.malformed_font} turns them into
55
+ # Fontisan::InvalidFontError. A bare RuntimeError is deliberately absent:
56
+ # the loaders raise "Invalid <Type> file: ..." and the specs and goldens
57
+ # pin that class. EOFError is absent too: the corpus goldens (read, audit,
58
+ # scan of the truncated fonts) pin it as the reference behaviour.
59
+ PARSER_LEAKS = [NoMethodError, IndexError, RangeError, TypeError, ZeroDivisionError].freeze
60
+
53
61
  autoload :TableDigests, "fontisan/reference/table_digests"
54
62
 
55
63
  class << self
64
+ # The InvalidFontError text for a leaked parser error, shared with the
65
+ # engine tiers that mirror the Ruby classes the engine reports.
66
+ def malformed_message(klass, message)
67
+ "Malformed font data (#{klass}): #{message}"
68
+ end
69
+
56
70
  # Read the structure of every face in a font file.
57
71
  #
58
72
  # @param path [String]
@@ -61,18 +75,20 @@ module Fontisan
61
75
  # @return [Array<Hash>] one Hash per face (see docs for the schema)
62
76
  def read(path, mode: :metadata, lazy: true)
63
77
  path = path_arg(path)
64
- sha = Digest::SHA256.file(path).hexdigest
65
- format = FontLoader.detect_format(path)&.to_s
66
- count = face_count(path)
67
- faces = Array.new(count) do |index|
68
- font = FontLoader.load(path, font_index: index, mode: mode, lazy: lazy)
69
- begin
70
- read_face(font, index, sha, format, mode, lazy)
71
- ensure
72
- font.close if font.respond_to?(:close)
78
+ malformed_font do
79
+ sha = Digest::SHA256.file(path).hexdigest
80
+ format = FontLoader.detect_format(path)&.to_s
81
+ count = face_count(path)
82
+ faces = Array.new(count) do |index|
83
+ font = FontLoader.load(path, font_index: index, mode: mode, lazy: lazy)
84
+ begin
85
+ read_face(font, index, sha, format, mode, lazy)
86
+ ensure
87
+ font.close if font.respond_to?(:close)
88
+ end
73
89
  end
90
+ normalize(faces)
74
91
  end
75
- normalize(faces)
76
92
  end
77
93
 
78
94
  # Per-glyph outline hashes of one face (full mode).
@@ -85,12 +101,14 @@ module Fontisan
85
101
  # @return [Hash] `{"kind"=>"glyf"|"cff"|nil, "hashes"=>[hex, ...]}`
86
102
  def outline_hashes(path, index: 0)
87
103
  path = path_arg(path)
88
- font = FontLoader.load(path, font_index: index, mode: :full,
89
- lazy: true)
90
- begin
91
- normalize(outline_data(font))
92
- ensure
93
- font.close if font.respond_to?(:close)
104
+ malformed_font do
105
+ font = FontLoader.load(path, font_index: index, mode: :full,
106
+ lazy: true)
107
+ begin
108
+ normalize(outline_data(font))
109
+ ensure
110
+ font.close if font.respond_to?(:close)
111
+ end
94
112
  end
95
113
  end
96
114
 
@@ -102,9 +120,11 @@ module Fontisan
102
120
  # @return [Array<Hash>] one canonical report per face
103
121
  def audit(path, validate: false)
104
122
  path = path_arg(path)
105
- result = Commands::AuditCommand.new(
106
- path, validate: validate || false, include_codepoints: false
107
- ).run
123
+ result = malformed_font do
124
+ Commands::AuditCommand.new(
125
+ path, validate: validate || false, include_codepoints: false
126
+ ).run
127
+ end
108
128
  normalize(Array(result).map { |report| audit_hash(report) })
109
129
  end
110
130
 
@@ -199,9 +219,11 @@ module Fontisan
199
219
  # @return [String] binary font bytes
200
220
  def instance(path, coords:, timestamp: nil)
201
221
  _ = timestamp
202
- font = FontLoader.load(path_arg(path), mode: :full, lazy: false)
222
+ path = path_arg(path)
223
+ check_sfnt_directory(path)
224
+ font = malformed_font { FontLoader.load(path, mode: :full, lazy: false) }
203
225
  values = coords.to_h { |tag, value| [tag.to_s, Float(value)] }
204
- Variable::Instancer.new(font).instance(values)
226
+ malformed_font { Variable::Instancer.new(font).instance(values) }
205
227
  end
206
228
 
207
229
  # Glyph ids kept by a subset request (fontist's `glyph_ids`).
@@ -213,9 +235,9 @@ module Fontisan
213
235
  # <basename>"
214
236
  def glyph_ids(path, unicodes: nil, text: nil)
215
237
  path = path_arg(path)
216
- font = FontLoader.load(path, mode: :full, lazy: false)
238
+ font = malformed_font { FontLoader.load(path, mode: :full, lazy: false) }
217
239
  codepoints = Array(unicodes).map(&:to_i) + text.to_s.codepoints
218
- gids_for(font, codepoints, File.basename(path))
240
+ malformed_font { gids_for(font, codepoints, File.basename(path)) }
219
241
  end
220
242
 
221
243
  # Subset a font to the glyphs of the given characters.
@@ -227,11 +249,13 @@ module Fontisan
227
249
  def subset(path, unicodes: nil, text: nil, profile: "web", timestamp: nil)
228
250
  _ = timestamp
229
251
  path = path_arg(path)
230
- font = FontLoader.load(path, mode: :full, lazy: false)
252
+ font = malformed_font { FontLoader.load(path, mode: :full, lazy: false) }
231
253
  codepoints = Array(unicodes).map(&:to_i) + text.to_s.codepoints
232
- gids = gids_for(font, codepoints, File.basename(path))
233
254
  options = Subset::Options.new(profile: profile)
234
- Subset::Builder.new(font, gids, options).build
255
+ malformed_font do
256
+ gids = gids_for(font, codepoints, File.basename(path))
257
+ Subset::Builder.new(font, gids, options).build
258
+ end
235
259
  end
236
260
 
237
261
  # Canonical JSON text of a read-type result: sorted keys, two-space
@@ -257,6 +281,26 @@ module Fontisan
257
281
 
258
282
  private
259
283
 
284
+ # Run a parse or build step of a font that may be malformed. A Ruby
285
+ # internal error (see PARSER_LEAKS) becomes Fontisan::InvalidFontError, the
286
+ # class every tier raises for bad font data; the original is the cause.
287
+ def malformed_font
288
+ yield
289
+ rescue *PARSER_LEAKS => e
290
+ raise InvalidFontError, malformed_message(e.class, e.message)
291
+ end
292
+
293
+ # A plain sfnt whose table directory runs past the end of the file fails
294
+ # as convert does (the engine tiers' own check, fontist/fontisan#159),
295
+ # not with whatever the loader or the instancer would trip over.
296
+ def check_sfnt_directory(path)
297
+ return unless File.file?(path) && %i[ttf otf].include?(FontLoader.detect_format(path))
298
+
299
+ SfntDirectory.check!(File.binread(path))
300
+ rescue SfntDirectory::Malformed => e
301
+ raise Error, "Conversion failed: #{e.message}"
302
+ end
303
+
260
304
  def path_arg(path, must_exist: true)
261
305
  unless path.is_a?(String) || path.respond_to?(:to_path)
262
306
  raise TypeError,
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nokogiri"
3
+ require "moxml"
4
4
 
5
5
  module Fontisan
6
6
  module SvgToGlyf
@@ -13,7 +13,7 @@ module Fontisan
13
13
 
14
14
  # @param xml [String] raw SVG XML
15
15
  def self.from_xml(xml)
16
- new(Nokogiri::XML(xml))
16
+ new(Moxml.parse(xml))
17
17
  end
18
18
 
19
19
  # @param path [String] file path to an .svg file
@@ -21,7 +21,7 @@ module Fontisan
21
21
  from_xml(File.read(path))
22
22
  end
23
23
 
24
- # @param doc [Nokogiri::XML::Document]
24
+ # @param doc [Moxml::Document]
25
25
  def initialize(doc)
26
26
  @doc = doc
27
27
  @source = doc
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nokogiri"
3
+ require "moxml"
4
4
 
5
5
  module Fontisan
6
6
  module Ufo
@@ -110,27 +110,39 @@ module Fontisan
110
110
  # @param xml [String] the .glif XML body
111
111
  # @return [Glyph] parsed glyph
112
112
  def self.from_glif(xml)
113
- doc = Nokogiri::XML(xml)
113
+ doc = Moxml.parse(xml)
114
114
  root = doc.root
115
115
  new(name: root["name"]).tap { |g| g.read_from(root) }
116
116
  end
117
117
 
118
118
  # Render this glyph back to .glif XML.
119
+ #
120
+ # The glyph body block is created here (method context) so its
121
+ # self stays this glyph: moxml's #build block is instance_eval'd
122
+ # against the builder, which would turn bare helper calls into
123
+ # tag names.
119
124
  # @return [String] XML text
120
125
  def to_glif
121
- builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
122
- xml.glyph(name: @name, format: "2") do
123
- emit_advance(xml)
124
- emit_unicodes(xml)
125
- emit_outline(xml)
126
- emit_lib(xml)
127
- emit_note(xml)
128
- end
126
+ builder = Moxml::Builder.new(Moxml.new)
127
+ body = -> {
128
+ emit_advance(builder)
129
+ emit_unicodes(builder)
130
+ emit_outline(builder)
131
+ emit_lib(builder)
132
+ emit_note(builder)
133
+ }
134
+ name = @name
135
+ builder.build do
136
+ declaration(version: "1.0", encoding: "UTF-8")
137
+ create_tag("glyph", { "name" => name, "format" => "2" },
138
+ block: body)
129
139
  end
130
- builder.to_xml
140
+ # expand_empty: false keeps the Nokogiri shape (empty elements
141
+ # self-close, e.g. <advance width="0"/>).
142
+ builder.doc.to_xml(expand_empty: false)
131
143
  end
132
144
 
133
- # Populate this glyph from a <glyph> Nokogiri node.
145
+ # Populate this glyph from a <glyph> Moxml node.
134
146
  def read_from(root)
135
147
  read_advance(root)
136
148
  read_unicodes(root)
@@ -325,7 +337,7 @@ module Fontisan
325
337
  @note = note_node.text if note_node
326
338
  end
327
339
 
328
- # Helper: read a <dict> Nokogiri node into a Hash. Reuses the
340
+ # Helper: read a <dict> Moxml node into a Hash. Reuses the
329
341
  # same key/value pair logic as Plist.parse but operates on the
330
342
  # inlined lib plist.
331
343
  def read_dict_to_hash(dict_node)
@@ -422,11 +434,11 @@ module Fontisan
422
434
 
423
435
  def emit_value(xml, v)
424
436
  case v
425
- when String then xml.string(v)
426
- when Integer then xml.integer(v)
427
- when Float then xml.real(v)
428
- when true then xml.__send__(true)
429
- when false then xml.__send__(false)
437
+ when String then v.empty? ? xml.string_ : xml.string(v)
438
+ when Integer then xml.integer(v.to_s)
439
+ when Float then xml.real(v.to_s)
440
+ when true then xml.true_
441
+ when false then xml.false_
430
442
  when Array then xml.array { v.each { |i| emit_value(xml, i) } }
431
443
  when Hash then emit_dict(xml, v)
432
444
  else xml.string(v.to_s)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nokogiri"
3
+ require "moxml"
4
4
 
5
5
  module Fontisan
6
6
  module Ufo
@@ -18,10 +18,10 @@ module Fontisan
18
18
  PLIST_DTD = "-//Apple//DTD PLIST 1.0//EN"
19
19
  PLIST_NS = "http://www.apple.com/DTDs/PropertyList-1.0.dtd"
20
20
 
21
- # @param source [String, Nokogiri::XML::Document] the XML plist
21
+ # @param source [String, Moxml::Document] the XML plist
22
22
  # @return [Object] the deserialized value
23
23
  def self.parse(source)
24
- doc = source.is_a?(Nokogiri::XML::Document) ? source : Nokogiri::XML(source)
24
+ doc = parse_document(source)
25
25
  root = doc.root
26
26
  raise ParseError, "no <plist> root element" unless root&.name == "plist"
27
27
 
@@ -33,22 +33,22 @@ module Fontisan
33
33
  # @param value [Object] the value to serialize
34
34
  # @return [String] the XML plist text
35
35
  def self.emit(value)
36
- builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
37
- create_doc_internal_subset(xml)
38
- xml.plist(version: "1.0") do
39
- emit_value(xml, value)
36
+ builder = Moxml::Builder.new(Moxml.new)
37
+ builder.build do
38
+ doctype("plist", PLIST_DTD, PLIST_NS)
39
+ plist(version: "1.0") do
40
+ Plist.emit_value(builder, value)
40
41
  end
41
42
  end
42
- builder.to_xml(
43
- save_with:
44
- Nokogiri::XML::Node::SaveOptions::AS_XML |
45
- Nokogiri::XML::Node::SaveOptions::NO_DECLARATION,
46
- )
43
+ # indent: 0 keeps the body on the single line Nokogiri's
44
+ # AS_XML produced; expand_empty: false keeps its `<string/>`
45
+ # self-closing shape for empty elements.
46
+ builder.doc.to_xml(indent: 0, expand_empty: false)
47
47
  end
48
48
 
49
- # @return [Nokogiri::XML::Document] the typed root
49
+ # @return [Moxml::Document] the typed root
50
50
  def self.parse_document(source)
51
- source.is_a?(Nokogiri::XML::Document) ? source : Nokogiri::XML(source)
51
+ source.is_a?(Moxml::Document) ? source : Moxml.parse(source)
52
52
  end
53
53
 
54
54
  # ---------- private-ish (called via module_function) ----------
@@ -90,20 +90,18 @@ module Fontisan
90
90
  node.element_children.map { |child| parse_value(child) }
91
91
  end
92
92
 
93
- def self.create_doc_internal_subset(xml)
94
- xml.doc.create_internal_subset("plist", PLIST_DTD, PLIST_NS)
95
- end
96
-
97
93
  def self.emit_value(xml, value)
98
94
  case value
99
95
  when nil
100
96
  # No <nil/> in Apple's plist DTD; use a sentinel string.
101
- xml.string("")
102
- when true then xml.__send__(true)
103
- when false then xml.__send__(false)
104
- when Integer then xml.integer(value)
105
- when Float then xml.real(value)
106
- when String then xml.string(value)
97
+ xml.string_
98
+ when true then xml.true_
99
+ when false then xml.false_
100
+ when Integer then xml.integer(value.to_s)
101
+ when Float then xml.real(value.to_s)
102
+ # An empty string serializes as a self-closed <string/>, like
103
+ # Nokogiri did; moxml keeps a paired form for a text child.
104
+ when String then value.empty? ? xml.string_ : xml.string(value)
107
105
  when Symbol then xml.string(value.to_s)
108
106
  when Array then xml.array { value.each { |v| emit_value(xml, v) } }
109
107
  when Hash then xml.dict { emit_dict_body(xml, value) }
@@ -62,7 +62,7 @@ module Fontisan
62
62
  value = requested.transform_keys(&:to_s)[axis.tag]
63
63
  value = axis.default if value.nil?
64
64
  value = Float(value)
65
- unless value.between?(axis.minimum, axis.maximum)
65
+ unless value.finite? && value.between?(axis.minimum, axis.maximum)
66
66
  raise InvalidCoordinatesError.new(
67
67
  axis: axis.tag, value: value, range: [axis.minimum, axis.maximum],
68
68
  )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fontisan
4
- VERSION = "0.4.48"
4
+ VERSION = "0.4.50"
5
5
  end
data/lib/fontisan.rb CHANGED
@@ -35,9 +35,25 @@ require "stringio"
35
35
  require "tmpdir"
36
36
  require "lutaml/model"
37
37
 
38
- # Configure lutaml-model to use Nokogiri adapter for XML serialization
38
+ # The XML engine behind lutaml-model, chosen by what actually loads on
39
+ # this machine: leptris (pure-C99, precompiled everywhere) when its
40
+ # library loads, then nokogiri if present, then REXML (stdlib). The
41
+ # probe is load-bearing: leptris's precompiled linux builds need
42
+ # glibc >= 2.34, and forcing the adapter on an older glibc would crash
43
+ # every boot — e.g. inside the rake-compiler-dock cross builds.
44
+ xml_adapter = begin
45
+ require "leptris"
46
+ :leptris
47
+ rescue LoadError
48
+ begin
49
+ require "nokogiri"
50
+ :nokogiri
51
+ rescue LoadError
52
+ :rexml
53
+ end
54
+ end
39
55
  Lutaml::Model::Config.configure do |config|
40
- config.xml_adapter_type = :nokogiri
56
+ config.xml_adapter_type = xml_adapter
41
57
  end
42
58
 
43
59
  module Fontisan
@@ -135,6 +151,7 @@ module Fontisan
135
151
  autoload :Woff2Collection, "fontisan/woff2_collection"
136
152
  autoload :Woff2Font, "fontisan/woff2_font"
137
153
  autoload :SfntDirectory, "fontisan/sfnt_directory"
154
+ autoload :OutputGuard, "fontisan/output_guard"
138
155
  autoload :WoffContainer, "fontisan/woff_container"
139
156
  autoload :WoffFont, "fontisan/woff_font"
140
157
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Tier fallback checks with strict mode OFF (fontist/fontisan#156 card 3a).
4
4
  #
5
- # ruby scripts/tier_fallback.rb TIERS [CORPUS_ROOT [SCENARIO]]
5
+ # ruby scripts/tier_fallback.rb TIERS [CORPUS_ROOT [SCENARIO]] [--gem GEM_NAME]
6
6
  #
7
7
  # TIERS is the comma list of non-Ruby tiers this platform is expected to have
8
8
  # (native,ffi | ffi | none). Without SCENARIO it runs every scenario below, each
@@ -17,8 +17,8 @@
17
17
  # ffi_abi_mismatch
18
18
  # mixed dfont and Type 1 rows are served by ruby, the
19
19
  # rest by the forced tier
20
- # auto_all, auto_no_native, `auto` resolves native, then ffi with native
21
- # auto_none removed, then ruby with both removed
20
+ # auto_all, auto_no_native, `auto` resolves to the first Engine::AUTO_ORDER tier
21
+ # auto_none still present (ruby until the #160 gates flip it)
22
22
  #
23
23
  # A scenario about a tier the platform lacks is skipped. Run it against an
24
24
  # INSTALLED gem on the CI matrix (outside the checkout), or locally with
@@ -33,15 +33,23 @@ SCENARIOS = %w[no_native no_ffi native_missing_symbol native_abi_mismatch
33
33
  ffi_missing_symbol ffi_abi_mismatch mixed
34
34
  auto_all auto_no_native auto_none].freeze
35
35
 
36
- usage = "usage: tier_fallback.rb native,ffi|ffi|none [corpus root [scenario]]"
37
- tiers = ARGV.fetch(0) { abort usage }.split(",") - ["none"]
38
- root = File.expand_path(ARGV.fetch(1, File.join(__dir__, "..", "corpus")))
39
- scenario = ARGV[2]
36
+ usage = "usage: tier_fallback.rb native,ffi|ffi|none [corpus root [scenario]] [--gem GEM_NAME]"
37
+ arguments = ARGV.dup
38
+ gem_option = arguments.index("--gem")
39
+ gem_name = "fontisan"
40
+ if gem_option
41
+ gem_name = arguments.fetch(gem_option + 1) { abort usage }
42
+ arguments.slice!(gem_option, 2)
43
+ end
44
+ tiers = arguments.fetch(0) { abort usage }.split(",") - ["none"]
45
+ root = File.expand_path(arguments.fetch(1, File.join(__dir__, "..", "corpus")))
46
+ scenario = arguments[2]
40
47
  abort "unknown tiers #{tiers.inspect}" unless (tiers - %w[native ffi]).empty?
41
48
 
42
49
  unless scenario
43
50
  results = SCENARIOS.to_h do |name|
44
- [name, system(RbConfig.ruby, __FILE__, tiers.join(",").then { |t| t.empty? ? "none" : t }, root, name)]
51
+ [name, system(RbConfig.ruby, __FILE__, tiers.join(",").then { |t| t.empty? ? "none" : t }, root, name,
52
+ "--gem", gem_name)]
45
53
  end
46
54
  failed = results.reject { |_, ok| ok }.keys
47
55
  puts failed.empty? ? "all #{SCENARIOS.size} fallback scenarios hold" : "FAILED: #{failed.join(', ')}"
@@ -74,6 +82,7 @@ end
74
82
 
75
83
  ENV["FONTISAN_ENGINE"] = "auto"
76
84
  ENV.delete("FONTISAN_ENGINE_STRICT")
85
+ gem gem_name
77
86
  case scenario
78
87
  when "no_native", "auto_no_native", "auto_none" then Object.prepend(Sabotage::BlockNative)
79
88
  end
@@ -177,11 +186,12 @@ begin
177
186
  Fontisan::Engine.reset!
178
187
  # dfont and PFB must be ruby's. Ruby's detect_format does not recognise a
179
188
  # PFA (nil), so the tier may take it; its error row equals the golden either way.
189
+ # Anything detect_format cannot name (empty or garbage bytes) is Ruby's too.
180
190
  scan_corpus.call(lambda { |path|
181
191
  case File.extname(path).downcase
182
192
  when ".dfont", ".pfb" then ["ruby"]
183
193
  when ".pfa" then ["ruby", tier]
184
- else [tier]
194
+ else Fontisan::FontLoader.detect_format(path).nil? ? ["ruby"] : [tier]
185
195
  end
186
196
  })
187
197
  end
@@ -203,9 +213,8 @@ begin
203
213
  when "auto_all", "auto_no_native", "auto_none"
204
214
  ENV["FONTISAN_ENGINE"] = "auto"
205
215
  Fontisan::Engine.reset!
206
- want = { "auto_all" => tiers.first || "ruby",
207
- "auto_no_native" => tiers.include?("ffi") ? "ffi" : "ruby",
208
- "auto_none" => "ruby" }.fetch(scenario)
216
+ present = { "auto_all" => tiers, "auto_no_native" => tiers - ["native"], "auto_none" => [] }.fetch(scenario)
217
+ want = Fontisan::Engine::AUTO_ORDER.map(&:to_s).find { |t| t == "ruby" || present.include?(t) }
209
218
  problems << "auto resolved #{resolved.call}, expected #{want}" unless resolved.call == want
210
219
  served = scan_corpus.call(->(path) { [want, *("ruby" if HANDED_TO_RUBY.include?(File.extname(path).downcase))] })
211
220
  problems << "no #{want} row served" if served[want].zero?
@@ -5,9 +5,10 @@
5
5
  # ruby scripts/tier_smoke.rb EXPECTED_TIER [CORPUS_ROOT]
6
6
  #
7
7
  # Run it against an INSTALLED fontisan gem (never the checkout's lib/). It scans
8
- # every `ci`-tier corpus font through Fontisan::Engine under the default engine (FONTISAN_ENGINE unset = auto),
8
+ # every `ci`-tier corpus font through Fontisan::Engine with FONTISAN_ENGINE=EXPECTED_TIER (strict off),
9
9
  # then checks that
10
- # * the tier auto resolves to is EXPECTED_TIER (native, ffi or ruby);
10
+ # * auto resolves to the first tier of Engine::AUTO_ORDER this gem has (ruby until the
11
+ # phase-3 gates flip it, fontist/fontisan#160);
11
12
  # * every row equals the committed scan golden (minus the served_by key);
12
13
  # * served_by is EXPECTED_TIER, except for files the engine hands to Ruby
13
14
  # (dfont, Type 1), which may say ruby; with EXPECTED_TIER=ruby nothing else.
@@ -18,7 +19,7 @@ expected = ARGV.fetch(0) { abort "usage: tier_smoke.rb native|ffi|ruby [corpus r
18
19
  root = File.expand_path(ARGV.fetch(1, File.join(__dir__, "..", "corpus")))
19
20
  abort "unknown tier #{expected.inspect}" unless %w[native ffi ruby].include?(expected)
20
21
 
21
- ENV.delete("FONTISAN_ENGINE") # the default (auto) is what is under test
22
+ ENV["FONTISAN_ENGINE"] = expected # the bundled tier is what is under test
22
23
  ENV.delete("FONTISAN_ENGINE_STRICT")
23
24
  require "fontisan"
24
25
  Fontisan::Reference # autoloading it installs the engine gates
@@ -44,20 +45,27 @@ entries.each do |entry|
44
45
  problems << "#{entry['id']}: rows differ from golden" unless JSON.parse(JSON.generate(actual)) == golden
45
46
 
46
47
  allowed = [expected]
47
- allowed << "ruby" if expected != "ruby" && HANDED_TO_RUBY.include?(File.extname(path).downcase)
48
+ handed = HANDED_TO_RUBY.include?(File.extname(path).downcase) || Fontisan::FontLoader.detect_format(path).nil?
49
+ allowed << "ruby" if expected != "ruby" && handed
48
50
  rows.each do |row|
49
51
  problems << "#{entry['id']}: served_by #{row['served_by']}, expected #{allowed.join('|')}" unless allowed.include?(row["served_by"])
50
52
  end
51
53
  end
52
54
 
53
- resolved = Fontisan::Engine.resolve(:scan, File.join(root, "fonts", "fonttools-test-ttf", "TestTTF.ttf")).to_s
54
- problems << "auto resolves scan to #{resolved}, expected #{expected}" unless resolved == expected
55
+ ttf = File.join(root, "fonts", "fonttools-test-ttf", "TestTTF.ttf")
56
+ resolved = Fontisan::Engine.resolve(:scan, ttf).to_s
57
+ problems << "#{expected} resolves scan to #{resolved}" unless resolved == expected
58
+ ENV["FONTISAN_ENGINE"] = "auto"
59
+ Fontisan::Engine.reset!
60
+ auto_want = Fontisan::Engine::AUTO_ORDER.find { |t| t == :ruby || t.to_s == expected }.to_s
61
+ auto = Fontisan::Engine.resolve(:scan, ttf).to_s
62
+ problems << "auto resolves scan to #{auto}, expected #{auto_want}" unless auto == auto_want
55
63
  problems << "no #{expected} row served" if served[expected].zero?
56
64
 
57
65
  puts "fontisan #{Fontisan::VERSION} from #{lib_dir}"
58
66
  puts "ruby #{RUBY_VERSION} #{RUBY_PLATFORM}; fonts scanned: #{entries.size}; served_by: #{served.inspect}"
59
67
  if problems.empty?
60
- puts "OK: auto resolved to #{resolved}; rows equal the goldens"
68
+ puts "OK: #{expected} served the corpus, auto resolved to #{auto}; rows equal the goldens"
61
69
  else
62
70
  warn problems
63
71
  exit 1