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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -4
- data/Cargo.lock +5 -5
- data/README.adoc +22 -5
- data/docs/guide/color-fonts/svg.md +5 -5
- data/docs/guide/engines.md +1 -1
- data/ext/fontisan_cdylib/Cargo.lock +1 -1
- data/ext/fontisan_cdylib/Cargo.toml +1 -1
- data/ext/fontisan_native/Cargo.toml +2 -2
- data/ext/fontisan_native/extconf.rb +22 -9
- data/ext/fontisan_native/src/lib.rs +23 -8
- data/lib/fontisan/byte_ops.rb +113 -18
- data/lib/fontisan/cli.rb +6 -0
- data/lib/fontisan/commands/convert_command.rb +2 -1
- data/lib/fontisan/commands/multi_format_output.rb +6 -1
- data/lib/fontisan/engine.rb +51 -8
- data/lib/fontisan/export/ttx_generator.rb +56 -38
- data/lib/fontisan/export/ttx_parser.rb +11 -11
- data/lib/fontisan/ffi/client.rb +5 -3
- data/lib/fontisan/ffi.rb +57 -13
- data/lib/fontisan/models/ttx/tables/binary_table.rb +14 -9
- data/lib/fontisan/native.rb +10 -6
- data/lib/fontisan/output_guard.rb +40 -0
- data/lib/fontisan/reference.rb +70 -26
- data/lib/fontisan/svg_to_glyf/document.rb +3 -3
- data/lib/fontisan/ufo/glyph.rb +30 -18
- data/lib/fontisan/ufo/plist.rb +22 -24
- data/lib/fontisan/variation/instancing/axis_space.rb +1 -1
- data/lib/fontisan/version.rb +1 -1
- data/lib/fontisan.rb +19 -2
- data/scripts/tier_fallback.rb +21 -12
- data/scripts/tier_smoke.rb +15 -7
- metadata +21 -9
data/lib/fontisan/native.rb
CHANGED
|
@@ -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 = "
|
|
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
|
-
%
|
|
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
|
-
|
|
169
|
-
|
|
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
|
|
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
|
data/lib/fontisan/reference.rb
CHANGED
|
@@ -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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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 =
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 "
|
|
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(
|
|
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 [
|
|
24
|
+
# @param doc [Moxml::Document]
|
|
25
25
|
def initialize(doc)
|
|
26
26
|
@doc = doc
|
|
27
27
|
@source = doc
|
data/lib/fontisan/ufo/glyph.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
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 =
|
|
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 =
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
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>
|
|
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>
|
|
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.
|
|
429
|
-
when false then xml.
|
|
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)
|
data/lib/fontisan/ufo/plist.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
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,
|
|
21
|
+
# @param source [String, Moxml::Document] the XML plist
|
|
22
22
|
# @return [Object] the deserialized value
|
|
23
23
|
def self.parse(source)
|
|
24
|
-
doc =
|
|
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 =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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 [
|
|
49
|
+
# @return [Moxml::Document] the typed root
|
|
50
50
|
def self.parse_document(source)
|
|
51
|
-
source.is_a?(
|
|
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.
|
|
102
|
-
when true then xml.
|
|
103
|
-
when false then xml.
|
|
104
|
-
when Integer then xml.integer(value)
|
|
105
|
-
when Float then xml.real(value)
|
|
106
|
-
|
|
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
|
)
|
data/lib/fontisan/version.rb
CHANGED
data/lib/fontisan.rb
CHANGED
|
@@ -35,9 +35,25 @@ require "stringio"
|
|
|
35
35
|
require "tmpdir"
|
|
36
36
|
require "lutaml/model"
|
|
37
37
|
|
|
38
|
-
#
|
|
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 =
|
|
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
|
|
data/scripts/tier_fallback.rb
CHANGED
|
@@ -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
|
|
21
|
-
# auto_none
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
207
|
-
|
|
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?
|
data/scripts/tier_smoke.rb
CHANGED
|
@@ -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
|
|
8
|
+
# every `ci`-tier corpus font through Fontisan::Engine with FONTISAN_ENGINE=EXPECTED_TIER (strict off),
|
|
9
9
|
# then checks that
|
|
10
|
-
# * the tier
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
54
|
-
|
|
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 #{
|
|
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
|