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.
@@ -6,8 +6,10 @@ module Fontisan
6
6
  # FONTISAN_ENGINE=ruby|native|ffi|auto (default auto)
7
7
  # FONTISAN_ENGINE_STRICT=1 (no fallback)
8
8
  #
9
- # `auto` resolves native, then ffi, then ruby: the first tier that is
10
- # available and supports the operation wins. A forced tier that is unavailable
9
+ # `auto` resolves to the Ruby tier until the phase-3 gates pass
10
+ # (fontisan#160); only an explicit native or ffi selection uses the Rust
11
+ # tiers. The post-gate order (native, ffi, ruby) is
12
+ # {NATIVE_PREFERRED_ORDER}. A forced tier that is unavailable
11
13
  # falls back to Ruby silently, or raises {TierUnavailable} at setup when
12
14
  # strict. An operation (or a path, e.g. dfont) a tier cannot serve always goes to
13
15
  # Ruby, strict or not. FONTISAN_MODE and FONTISAN_LAZY are not read here.
@@ -27,11 +29,16 @@ module Fontisan
27
29
  SELECTIONS = %w[ruby native ffi auto].freeze
28
30
  STRICT_ON = %w[1 true yes on].freeze
29
31
  STRICT_OFF = ["", "0", "false", "no", "off"].freeze
30
- AUTO_ORDER = %i[native ffi ruby].freeze
32
+ # The order `auto` will use once the phase-3 gates pass (fontisan#160).
33
+ # Until then `auto` resolves to ruby only; the 2d flip is replacing
34
+ # AUTO_ORDER with this constant.
35
+ NATIVE_PREFERRED_ORDER = %i[native ffi ruby].freeze
36
+ AUTO_ORDER = %i[ruby].freeze
31
37
  OPS = %i[read outline_hashes audit audit_dir scan convert extract
32
38
  instance glyph_ids subset].freeze
33
39
  REPLACEABLE_TIERS = %i[native ffi].freeze
34
40
  TIER_CONTRACT = %i[available? supports? call].freeze
41
+ FACE_ENTRY = "fontisan/ruby"
35
42
  ROW_RECORDER = :fontisan_engine_rows
36
43
  IN_OPERATION = :fontisan_engine_in_operation
37
44
 
@@ -68,6 +75,7 @@ module Fontisan
68
75
 
69
76
  @mutex = Mutex.new
70
77
  @tiers = nil
78
+ @registered_tiers = {}.freeze
71
79
  @served = {}
72
80
  @counters = nil
73
81
  @availability = {}
@@ -174,7 +182,7 @@ module Fontisan
174
182
  record(:ruby, operation, nil)
175
183
  end
176
184
 
177
- # Forget probes, counters, served state and registered tiers.
185
+ # Forget probes, counters, served state and temporary tier overrides.
178
186
  def reset!
179
187
  @mutex.synchronize do
180
188
  @served = {}
@@ -182,14 +190,28 @@ module Fontisan
182
190
  @availability = {}
183
191
  @tiers = @tiers.slice(:ruby) if @tiers
184
192
  end
185
- Native.reset!
186
- Ffi.reset!
193
+ @registered_tiers.each_value do |tier|
194
+ tier.reset! if tier.respond_to?(:reset!)
195
+ end
196
+ end
197
+
198
+ # Register a provider independently of Reference's lazy installation.
199
+ # Providers survive reset!; reset! on a provider is optional.
200
+ def register_tier(name, mod)
201
+ validate_registration!(name, mod)
202
+ @mutex.synchronize do
203
+ @registered_tiers = @registered_tiers.merge(name.to_sym => mod).freeze
204
+ @availability.delete(name.to_sym)
205
+ end
187
206
  end
188
207
 
189
- # Register a tier object (tests, or the extension itself). A bad
208
+ # Override a tier object until reset! (tests, or the extension itself). A bad
190
209
  # registration raises here, at setup, never as a silent fallback later.
191
210
  def register(name, tier)
211
+ return register_tier(name, tier) if @loading_face
212
+
192
213
  validate_registration!(name, tier)
214
+ Fontisan::Reference unless @tiers
193
215
  @mutex.synchronize do
194
216
  @tiers = @tiers.merge(name.to_sym => tier).freeze
195
217
  @availability.delete(name.to_sym)
@@ -268,7 +290,7 @@ module Fontisan
268
290
  def tier_object(name)
269
291
  case name
270
292
  when :ruby then @tiers.fetch(:ruby)
271
- else @tiers[name] || { native: Native, ffi: Ffi }.fetch(name)
293
+ else @tiers[name] || @registered_tiers[name]
272
294
  end
273
295
  end
274
296
 
@@ -282,11 +304,29 @@ module Fontisan
282
304
 
283
305
  # Any failure while probing is "not available"; never an exception.
284
306
  def probe(name)
307
+ load_face if REPLACEABLE_TIERS.include?(name) && !@tiers.key?(name)
285
308
  tier_object(name).available? || false
286
309
  rescue StandardError, ScriptError
287
310
  false
288
311
  end
289
312
 
313
+ # The optional face registers its own providers through register. Keep
314
+ # those registrations across resets, just like the bundled providers.
315
+ def load_face
316
+ return if @face_loaded
317
+
318
+ begin
319
+ @loading_face = true
320
+ require FACE_ENTRY
321
+ @face_loaded = true
322
+ rescue LoadError
323
+ # Bundled providers remain usable until they move to the face gem.
324
+ nil
325
+ ensure
326
+ @loading_face = false
327
+ end
328
+ end
329
+
290
330
  # A tier may also vet the call's keyword options (`supports_options?`).
291
331
  def supports?(name, operation, path, options = nil)
292
332
  tier = tier_object(name)
@@ -346,5 +386,8 @@ module Fontisan
346
386
  Thread.current[ROW_RECORDER] = previous
347
387
  end
348
388
  end
389
+
390
+ register_tier(:native, Native)
391
+ register_tier(:ffi, Ffi)
349
392
  end
350
393
  end
@@ -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 Export
@@ -40,21 +40,35 @@ module Fontisan
40
40
  def generate(options = {})
41
41
  table_list = options[:tables] || :all
42
42
 
43
- builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
44
- xml.ttFont(
45
- "sfntVersion" => format_sfnt_version(to_int(@font.header.sfnt_version)),
46
- "ttLibVersion" => "4.0",
47
- ) do
48
- generate_glyph_order(xml)
49
-
50
- tables_to_generate = select_tables(table_list)
51
- tables_to_generate.each do |tag|
52
- generate_table(xml, tag)
53
- end
43
+ # The ttFont body lambda is created here (method context) so
44
+ # its self stays this generator: moxml's #build block is
45
+ # instance_eval'd against the builder, which would turn bare
46
+ # helper calls into tag names. The zero-indent context keeps
47
+ # the builder markup flat so the pretty reparse in
48
+ # format_output owns all indentation (reindenting
49
+ # already-indented mixed content glues sibling tags together).
50
+ context = Moxml.new { |config| config.default_indent = 0 }
51
+ builder = Moxml::Builder.new(context)
52
+ sfnt_version = format_sfnt_version(to_int(@font.header.sfnt_version))
53
+ root_body = -> {
54
+ generate_glyph_order(builder)
55
+
56
+ tables_to_generate = select_tables(table_list)
57
+ tables_to_generate.each do |tag|
58
+ generate_table(builder, tag)
54
59
  end
60
+ }
61
+ builder.build do
62
+ declaration(version: "1.0", encoding: "UTF-8")
63
+ create_tag("ttFont",
64
+ { "sfntVersion" => sfnt_version,
65
+ "ttLibVersion" => "4.0" },
66
+ block: root_body)
55
67
  end
56
68
 
57
- format_output(builder.to_xml)
69
+ # expand_empty: false keeps the Nokogiri shape (empty elements
70
+ # self-close, e.g. <GlyphID id="0" name=".notdef"/>).
71
+ format_output(builder.doc.to_xml(expand_empty: false))
58
72
  end
59
73
 
60
74
  private
@@ -69,7 +83,7 @@ module Fontisan
69
83
 
70
84
  # Generate GlyphOrder section (required first)
71
85
  #
72
- # @param xml [Nokogiri::XML::Builder] XML builder
86
+ # @param xml [Moxml::Builder] XML builder
73
87
  # @return [void]
74
88
  def generate_glyph_order(xml)
75
89
  xml.GlyphOrder do
@@ -83,7 +97,7 @@ module Fontisan
83
97
 
84
98
  # Generate individual table XML
85
99
  #
86
- # @param xml [Nokogiri::XML::Builder] XML builder
100
+ # @param xml [Moxml::Builder] XML builder
87
101
  public
88
102
 
89
103
  # Per-tag emitter dispatch table. Adding a new table's TTX
@@ -138,7 +152,7 @@ module Fontisan
138
152
 
139
153
  # Generate head table XML
140
154
  #
141
- # @param xml [Nokogiri::XML::Builder] XML builder
155
+ # @param xml [Moxml::Builder] XML builder
142
156
  # @param table [Tables::Head] Head table
143
157
  # @return [void]
144
158
  def generate_head_table(xml, table)
@@ -167,7 +181,7 @@ module Fontisan
167
181
 
168
182
  # Generate hhea table XML
169
183
  #
170
- # @param xml [Nokogiri::XML::Builder] XML builder
184
+ # @param xml [Moxml::Builder] XML builder
171
185
  # @param table [Tables::Hhea] Hhea table
172
186
  # @return [void]
173
187
  def generate_hhea_table(xml, table)
@@ -194,7 +208,7 @@ module Fontisan
194
208
 
195
209
  # Generate maxp table XML
196
210
  #
197
- # @param xml [Nokogiri::XML::Builder] XML builder
211
+ # @param xml [Moxml::Builder] XML builder
198
212
  # @param table [Tables::Maxp] Maxp table
199
213
  # @return [void]
200
214
  def generate_maxp_table(xml, table)
@@ -224,7 +238,7 @@ module Fontisan
224
238
 
225
239
  # Generate post table XML
226
240
  #
227
- # @param xml [Nokogiri::XML::Builder] XML builder
241
+ # @param xml [Moxml::Builder] XML builder
228
242
  # @param table [Tables::Post] Post table
229
243
  # @return [void]
230
244
  def generate_post_table(xml, table)
@@ -243,7 +257,7 @@ module Fontisan
243
257
 
244
258
  # Generate name table XML
245
259
  #
246
- # @param xml [Nokogiri::XML::Builder] XML builder
260
+ # @param xml [Moxml::Builder] XML builder
247
261
  # @param table [Tables::Name] Name table
248
262
  # @return [void]
249
263
  def generate_name_table(xml, table)
@@ -263,7 +277,7 @@ module Fontisan
263
277
 
264
278
  # Generate OS/2 table XML
265
279
  #
266
- # @param xml [Nokogiri::XML::Builder] XML builder
280
+ # @param xml [Moxml::Builder] XML builder
267
281
  # @param table [Tables::Os2] OS/2 table
268
282
  # @return [void]
269
283
  def generate_os2_table(xml, table)
@@ -274,10 +288,10 @@ module Fontisan
274
288
 
275
289
  # Helper to add element with value attribute
276
290
  #
277
- # @param parent [Nokogiri::XML::Element] Parent element
291
+ # @param parent [Moxml::Element] Parent element
278
292
  # @param name [String] Element name
279
293
  # @param value [Object] Value
280
- # @param doc [Nokogiri::XML::Document] Document
294
+ # @param doc [Moxml::Document] Document
281
295
  # @return [void]
282
296
  def add_element_with_value(parent, name, value, doc)
283
297
  elem = doc.create_element(name)
@@ -287,7 +301,7 @@ module Fontisan
287
301
 
288
302
  # Generate binary table as hexdata
289
303
  #
290
- # @param xml [Nokogiri::XML::Builder] XML builder
304
+ # @param xml [Moxml::Builder] XML builder
291
305
  # @param tag [String] Table tag
292
306
  # @param table [Object] Table object
293
307
  # @return [void]
@@ -302,7 +316,7 @@ module Fontisan
302
316
 
303
317
  # Generate binary table from raw data
304
318
  #
305
- # @param xml [Nokogiri::XML::Builder] XML builder
319
+ # @param xml [Moxml::Builder] XML builder
306
320
  # @param tag [String] Table tag
307
321
  # @param data [String] Raw binary data
308
322
  # @return [void]
@@ -318,7 +332,7 @@ module Fontisan
318
332
 
319
333
  # Generate cmap table XML (simplified for now)
320
334
  #
321
- # @param xml [Nokogiri::XML::Builder] XML builder
335
+ # @param xml [Moxml::Builder] XML builder
322
336
  # @param table [Object] Cmap table
323
337
  # @return [void]
324
338
  def generate_cmap_table(xml, table)
@@ -327,7 +341,7 @@ module Fontisan
327
341
 
328
342
  # Generate loca table XML
329
343
  #
330
- # @param xml [Nokogiri::XML::Builder] XML builder
344
+ # @param xml [Moxml::Builder] XML builder
331
345
  # @param table [Object] Loca table
332
346
  # @return [void]
333
347
  def generate_loca_table(xml, _table)
@@ -338,7 +352,7 @@ module Fontisan
338
352
 
339
353
  # Generate glyf table XML (simplified for now)
340
354
  #
341
- # @param xml [Nokogiri::XML::Builder] XML builder
355
+ # @param xml [Moxml::Builder] XML builder
342
356
  # @param table [Object] Glyf table
343
357
  # @return [void]
344
358
  def generate_glyf_table(xml, table)
@@ -347,7 +361,7 @@ module Fontisan
347
361
 
348
362
  # Generate CFF table XML (simplified for now)
349
363
  #
350
- # @param xml [Nokogiri::XML::Builder] XML builder
364
+ # @param xml [Moxml::Builder] XML builder
351
365
  # @param table [Object] CFF table
352
366
  # @return [void]
353
367
  def generate_cff_table(xml, table)
@@ -356,7 +370,7 @@ module Fontisan
356
370
 
357
371
  # Generate hmtx table XML (simplified for now)
358
372
  #
359
- # @param xml [Nokogiri::XML::Builder] XML builder
373
+ # @param xml [Moxml::Builder] XML builder
360
374
  # @param table [Object] Hmtx table
361
375
  # @return [void]
362
376
  def generate_hmtx_table(xml, table)
@@ -365,7 +379,7 @@ module Fontisan
365
379
 
366
380
  # Generate fvar table XML
367
381
  #
368
- # @param xml [Nokogiri::XML::Builder] XML builder
382
+ # @param xml [Moxml::Builder] XML builder
369
383
  # @param table [Tables::Fvar] Fvar table
370
384
  # @return [void]
371
385
  def generate_fvar_table(xml, table)
@@ -376,10 +390,12 @@ module Fontisan
376
390
  table.axes.each do |axis|
377
391
  xml.Axis do
378
392
  xml.AxisTag axis.axis_tag
379
- xml.MinValue to_int(axis.min_value) / 65536.0
380
- xml.DefaultValue to_int(axis.default_value) / 65536.0
381
- xml.MaxValue to_int(axis.max_value) / 65536.0
382
- xml.AxisNameID to_int(axis.axis_name_id)
393
+ # moxml builder text content must be a String (Nokogiri
394
+ # stringified any value); .to_s keeps the same bytes.
395
+ xml.MinValue (to_int(axis.min_value) / 65536.0).to_s
396
+ xml.DefaultValue (to_int(axis.default_value) / 65536.0).to_s
397
+ xml.MaxValue (to_int(axis.max_value) / 65536.0).to_s
398
+ xml.AxisNameID to_int(axis.axis_name_id).to_s
383
399
  end
384
400
  end
385
401
  end
@@ -387,7 +403,7 @@ module Fontisan
387
403
 
388
404
  # Generate variation table XML (gvar, cvar, HVAR, etc.)
389
405
  #
390
- # @param xml [Nokogiri::XML::Builder] XML builder
406
+ # @param xml [Moxml::Builder] XML builder
391
407
  # @param tag [String] Table tag
392
408
  # @param table [Object] Variation table
393
409
  # @return [void]
@@ -514,8 +530,10 @@ module Fontisan
514
530
  # @return [String] Formatted XML
515
531
  def format_output(xml)
516
532
  if @pretty
517
- doc = Nokogiri::XML(xml)
518
- doc.to_xml(indent: @indent)
533
+ doc = Moxml.parse(xml)
534
+ # expand_empty: false keeps the Nokogiri shape (empty
535
+ # elements self-close).
536
+ doc.to_xml(indent: @indent, expand_empty: false)
519
537
  else
520
538
  # Remove extra whitespace for compact format
521
539
  xml.gsub(/>\s+</, "><").gsub(/\n\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 Export
@@ -19,7 +19,7 @@ module Fontisan
19
19
  # @param ttx_xml [String] TTX XML content
20
20
  # @return [Hash] Parsed font data structure
21
21
  def parse(ttx_xml)
22
- doc = Nokogiri::XML(ttx_xml)
22
+ doc = Moxml.parse(ttx_xml)
23
23
  ttfont = doc.at_xpath("/ttFont")
24
24
  raise "No ttFont root element found" unless ttfont
25
25
 
@@ -51,7 +51,7 @@ module Fontisan
51
51
 
52
52
  # Parse GlyphOrder section
53
53
  #
54
- # @param ttfont [Nokogiri::XML::Element] Root element
54
+ # @param ttfont [Moxml::Element] Root element
55
55
  # @return [Array<Hash>] Array of glyph entries
56
56
  def parse_glyph_order(ttfont)
57
57
  glyph_order = ttfont.at_xpath("GlyphOrder")
@@ -67,7 +67,7 @@ module Fontisan
67
67
 
68
68
  # Parse all tables
69
69
  #
70
- # @param ttfont [Nokogiri::XML::Element] Root element
70
+ # @param ttfont [Moxml::Element] Root element
71
71
  # @return [Hash] Hash of table data by tag
72
72
  def parse_tables(ttfont)
73
73
  tables = {}
@@ -88,7 +88,7 @@ module Fontisan
88
88
 
89
89
  # Parse head table
90
90
  #
91
- # @param ttfont [Nokogiri::XML::Element] Root element
91
+ # @param ttfont [Moxml::Element] Root element
92
92
  # @param tables [Hash] Tables hash
93
93
  # @return [void]
94
94
  def parse_head_table(ttfont, tables)
@@ -118,7 +118,7 @@ module Fontisan
118
118
 
119
119
  # Parse hhea table
120
120
  #
121
- # @param ttfont [Nokogiri::XML::Element] Root element
121
+ # @param ttfont [Moxml::Element] Root element
122
122
  # @param tables [Hash] Tables hash
123
123
  # @return [void]
124
124
  def parse_hhea_table(ttfont, tables)
@@ -144,7 +144,7 @@ module Fontisan
144
144
 
145
145
  # Parse maxp table
146
146
  #
147
- # @param ttfont [Nokogiri::XML::Element] Root element
147
+ # @param ttfont [Moxml::Element] Root element
148
148
  # @param tables [Hash] Tables hash
149
149
  # @return [void]
150
150
  def parse_maxp_table(ttfont, tables)
@@ -169,7 +169,7 @@ module Fontisan
169
169
 
170
170
  # Parse name table
171
171
  #
172
- # @param ttfont [Nokogiri::XML::Element] Root element
172
+ # @param ttfont [Moxml::Element] Root element
173
173
  # @param tables [Hash] Tables hash
174
174
  # @return [void]
175
175
  def parse_name_table(ttfont, tables)
@@ -191,7 +191,7 @@ module Fontisan
191
191
 
192
192
  # Parse OS/2 table (stub)
193
193
  #
194
- # @param ttfont [Nokogiri::XML::Element] Root element
194
+ # @param ttfont [Moxml::Element] Root element
195
195
  # @param tables [Hash] Tables hash
196
196
  # @return [void]
197
197
  def parse_os2_table(ttfont, tables)
@@ -204,7 +204,7 @@ module Fontisan
204
204
 
205
205
  # Parse post table (stub)
206
206
  #
207
- # @param ttfont [Nokogiri::XML::Element] Root element
207
+ # @param ttfont [Moxml::Element] Root element
208
208
  # @param tables [Hash] Tables hash
209
209
  # @return [void]
210
210
  def parse_post_table(ttfont, tables)
@@ -222,7 +222,7 @@ module Fontisan
222
222
 
223
223
  # Parse binary tables (fallback)
224
224
  #
225
- # @param ttfont [Nokogiri::XML::Element] Root element
225
+ # @param ttfont [Moxml::Element] Root element
226
226
  # @param tables [Hash] Tables hash
227
227
  # @return [void]
228
228
  def parse_binary_tables(ttfont, tables)
@@ -17,7 +17,7 @@ module Fontisan
17
17
  UNSUPPORTED = 10
18
18
 
19
19
  # The result block of one call; its pointers die with {#close}.
20
- Reply = Struct.new(:status, :payload, :message)
20
+ Reply = Struct.new(:status, :payload, :message, :kind)
21
21
 
22
22
  class Header < FFI::Struct
23
23
  layout :magic, :uint32, :abi_version, :uint32, :struct_size, :uint32,
@@ -35,7 +35,8 @@ module Fontisan
35
35
 
36
36
  class Result < FFI::Struct
37
37
  layout :header, Header, :status, :int32, :error, ErrorRow,
38
- :payload, :pointer, :payload_len, :size_t
38
+ :payload, :pointer, :payload_len, :size_t,
39
+ :error_kind, :int32, :reserved, :int32
39
40
  end
40
41
 
41
42
  # Loads +path+ and binds its symbols.
@@ -106,7 +107,8 @@ module Fontisan
106
107
  result = Result.new(pointer)
107
108
  Reply.new(result[:status], bytes(result[:payload], result[:payload_len]),
108
109
  bytes(result[:error][:message], result[:error][:message_len])
109
- .force_encoding(Encoding::UTF_8))
110
+ .force_encoding(Encoding::UTF_8),
111
+ result[:error_kind])
110
112
  ensure
111
113
  @binding.fontisan_result_free(pointer) unless pointer.null?
112
114
  end
data/lib/fontisan/ffi.rb CHANGED
@@ -83,7 +83,11 @@ module Fontisan
83
83
  # Options the engine honours; anything else is served by Ruby. The
84
84
  # read-side operations check their own options and fall back inside.
85
85
  def supports_options?(operation, options, path = nil)
86
- return byte_options?(operation, options, path) if ByteOps::BYTE_OPERATIONS.include?(operation)
86
+ if ByteOps::BYTE_OPERATIONS.include?(operation)
87
+ return false unless byte_options?(operation, options, path)
88
+
89
+ return operation != :instance || fixed_coordinates?(options[:coords])
90
+ end
87
91
 
88
92
  OPERATIONS.include?(operation)
89
93
  end
@@ -99,6 +103,18 @@ module Fontisan
99
103
 
100
104
  attr_reader :client
101
105
 
106
+ # The instance request carries each coordinate as a signed 16.16 Fixed.
107
+ # A value that is not finite, does not fit, or is not an exact multiple
108
+ # of 1/65536 would be rounded or wrapped (1e9 became -13824.0), so the
109
+ # engine would build a different font than the Ruby and native tiers;
110
+ # those requests go to Ruby.
111
+ def fixed_coordinates?(coords)
112
+ coords.each_value.all? do |value|
113
+ scaled = Float(value) * FIXED_ONE
114
+ scaled.finite? && scaled.abs < 2**31 && scaled == scaled.round
115
+ end
116
+ end
117
+
102
118
  def probe
103
119
  require "fontisan/ffi/client"
104
120
  path = library_path or return false
@@ -125,8 +141,11 @@ module Fontisan
125
141
  bundled && File.file?(bundled) ? bundled : File.expand_path("ffi/#{file}", __dir__)
126
142
  end
127
143
 
144
+ # dfont, Type 1 and anything that is not a font (the native tier hands
145
+ # those to Ruby too, so auto never lands them on this tier).
128
146
  def ruby_only?(path)
129
- RUBY_ONLY_FORMATS.include?(FontLoader.detect_format(path))
147
+ format = FontLoader.detect_format(path)
148
+ format.nil? || RUBY_ONLY_FORMATS.include?(format)
130
149
  rescue StandardError
131
150
  false
132
151
  end
@@ -135,6 +154,7 @@ module Fontisan
135
154
  def serve_convert(path, output:, timestamp: nil, **options)
136
155
  path = File.path(path)
137
156
  target = convert_target(options.fetch(:to))
157
+ output = convert_output(path, output, target)
138
158
  stamp = timestamp ? unix_seconds(timestamp) : Time.now.to_i
139
159
  prefix = [ByteOps::TARGET_CODES.fetch(target), 0, stamp].pack("NNq>")
140
160
  bytes = byte_op(OP_CONVERT, prefix + File.binread(path))
@@ -143,11 +163,13 @@ module Fontisan
143
163
 
144
164
  # Same file layout as UnpackCommand: <output_dir>/<basename>.
145
165
  def serve_extract(path, index:, output_dir:)
146
- payload = byte_op(OP_EXTRACT, [index, 0, 0, 0].pack("NNNN") + File.binread(File.path(path)))
166
+ payload = byte_op(OP_EXTRACT, [index, 0, 0, 0].pack("NNNN") + unpacking_io { File.binread(File.path(path)) })
147
167
  length = payload.unpack1("N")
148
168
  name = payload.byteslice(4, length).force_encoding(Encoding::UTF_8)
149
- FileUtils.mkdir_p(output_dir)
150
- File.join(output_dir, name).tap { |out| File.binwrite(out, payload.byteslice(4 + length..)) }
169
+ unpacking_io do
170
+ FileUtils.mkdir_p(output_dir)
171
+ File.join(output_dir, name).tap { |out| File.binwrite(out, payload.byteslice(4 + length..)) }
172
+ end
151
173
  end
152
174
 
153
175
  def serve_instance(path, coords:, timestamp: nil)
@@ -198,15 +220,27 @@ module Fontisan
198
220
  end
199
221
 
200
222
  def raise_byte_failure(reply)
201
- match = ByteOps::FAILURE_MESSAGE.match(reply.message)
202
- if reply.status == CONVERT_FAILED && match
203
- klass, message = byte_failure(match[1], match[2])
204
- raise build_instance(klass, message)
223
+ if reply.status == CONVERT_FAILED
224
+ class_name, message = reported_failure(reply)
225
+ if class_name
226
+ klass, message = public_failure(*byte_failure(class_name, message, reply.kind))
227
+ raise build_instance(klass, message)
228
+ end
205
229
  end
206
230
 
207
231
  raise Fontisan::Error, "fontisan ffi status #{reply.status}: #{reply.message}"
208
232
  end
209
233
 
234
+ # The class name and message of a failed byte operation: the engine's
235
+ # "<class>: <message>" text, or the bare text under a kind that names its
236
+ # class (byte_failure then takes the class from the kind).
237
+ def reported_failure(reply)
238
+ match = ByteOps::FAILURE_MESSAGE.match(reply.message)
239
+ return [match[1], match[2]] if match
240
+
241
+ [Fontisan::Error.name, reply.message] if ByteOps::KIND_CLASSES.key?(reply.kind)
242
+ end
243
+
210
244
  def serve_read(path, mode: :metadata, lazy: true)
211
245
  return ruby(:read, path, mode: mode, lazy: lazy) unless %i[metadata full].include?(mode)
212
246
 
@@ -222,16 +256,23 @@ module Fontisan
222
256
  return ruby(:audit, path, validate: validate) unless [true, false, nil].include?(validate)
223
257
 
224
258
  path = reference_path(path)
225
- input = [File.basename(path).bytesize].pack("N") + File.basename(path).b + File.binread(path)
259
+ input = [File.basename(path).bytesize].pack("N") + File.basename(path).b + load_font_bytes(path)
226
260
  reply = client.call(OP_AUDIT, input, validate ? FLAG_VALIDATE : 0)
227
261
  return ruby(:audit, path, validate: validate) if reply.status == Client::UNSUPPORTED
228
262
 
229
263
  rows(reply)
230
264
  end
231
265
 
266
+ # An unreadable file is a Fontisan::Error, as the Ruby loader raises it.
267
+ def load_font_bytes(path)
268
+ File.binread(path)
269
+ rescue SystemCallError => e
270
+ raise Fontisan::Error, "Failed to load font: #{e.message}"
271
+ end
272
+
232
273
  def serve_scan(paths)
233
274
  Array(paths).flat_map do |path|
234
- reply = client.call(OP_SCAN, File.path(path))
275
+ reply = client.call(OP_SCAN, Reference.__send__(:path_arg, path, must_exist: false))
235
276
  result = reply.status == Client::OK ? decode(reply.payload) : nil
236
277
  result = nil if result&.any? { |row| row["status"] == "unsupported" }
237
278
  # "status" is the engine's routing marker; Reference rows have none.
@@ -308,8 +349,11 @@ module Fontisan
308
349
 
309
350
  # Raise the reported error as its Ruby class, message and cause chain.
310
351
  def raise_reported(report)
311
- raise_chain([report.slice("class", "message"),
312
- *Array(report["causes"])])
352
+ head = report.slice("class", "message")
353
+ klass, message = public_failure(exception_class(head["class"]), head["message"])
354
+ chain = [head, *Array(report["causes"])]
355
+ chain.unshift({ "class" => klass.name, "message" => message }) if klass == InvalidFontError && head["class"] != klass.name
356
+ raise_chain(chain)
313
357
  end
314
358
 
315
359
  def raise_chain(infos)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "lutaml/model"
4
+ require "moxml"
4
5
 
5
6
  module Fontisan
6
7
  module Models
@@ -17,24 +18,28 @@ module Fontisan
17
18
  # Custom serialization since we need dynamic root element
18
19
  # based on table tag
19
20
  def to_xml(options = {})
20
- builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
21
- xml.public_send(tag.to_sym) do
22
- xml.hexdata do
23
- xml.text("\n #{format_hexdata(hexdata)}\n ")
24
- end
25
- end
26
- end
21
+ builder = Moxml::Builder.new(Moxml.new)
22
+ table_tag = tag
23
+ body = -> {
24
+ builder.create_tag(table_tag, block: -> {
25
+ builder.create_tag(
26
+ "hexdata",
27
+ text_content: "\n #{format_hexdata(hexdata)}\n ",
28
+ )
29
+ })
30
+ }
31
+ builder.build(&body)
27
32
 
28
33
  if options[:pretty]
29
34
  builder.doc.root.to_xml(indent: options.fetch(:indent, 2))
30
35
  else
31
- builder.to_xml
36
+ builder.doc.to_xml(expand_empty: false)
32
37
  end
33
38
  end
34
39
 
35
40
  # Parse from XML
36
41
  def self.from_xml(xml_string, tag)
37
- doc = Nokogiri::XML(xml_string)
42
+ doc = Moxml.parse(xml_string)
38
43
  table_elem = doc.at_xpath("//#{tag}")
39
44
  return nil unless table_elem
40
45