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.
- checksums.yaml +4 -4
- data/BENCHMARKS.md +56 -0
- data/CHANGELOG.md +12 -0
- data/Cargo.lock +550 -0
- data/Cargo.toml +12 -0
- data/Gemfile +10 -0
- data/README.adoc +69 -0
- data/Rakefile +5 -1
- data/benchmark/baseline.json +5 -0
- data/benchmark/ci_gate.rb +217 -0
- data/benchmark/corpus/harness.rb +175 -0
- data/benchmark/corpus/ops.rb +150 -0
- data/benchmark/corpus/run.rb +182 -0
- data/benchmark/corpus/scan_child.rb +8 -0
- data/benchmark/corpus/tiers.rb +176 -0
- data/benchmark/tier_baseline.json +12 -0
- data/build_mingw32_stub.rb +15 -0
- data/docs/.vitepress/config.ts +2 -1
- data/docs/guide/engines.md +117 -0
- data/docs/reference-facade.md +130 -0
- data/ext/fontisan_cdylib/Cargo.lock +313 -0
- data/ext/fontisan_cdylib/Cargo.toml +18 -0
- data/ext/fontisan_cdylib/build.rs +22 -0
- data/ext/fontisan_cdylib/extconf.rb +36 -0
- data/ext/fontisan_cdylib/src/lib.rs +13 -0
- data/ext/fontisan_native/Cargo.toml +21 -0
- data/ext/fontisan_native/build.rs +22 -0
- data/ext/fontisan_native/clippy.toml +10 -0
- data/ext/fontisan_native/extconf.rb +59 -0
- data/ext/fontisan_native/src/lib.rs +478 -0
- data/lib/fontisan/audit/checks/cmap_check.rb +7 -0
- data/lib/fontisan/audit/checks/hinting_check.rb +1 -1
- data/lib/fontisan/audit/checks/maxp_check.rb +1 -1
- data/lib/fontisan/audit/checks/opentype_conformance_check.rb +5 -2
- data/lib/fontisan/base_collection.rb +30 -0
- data/lib/fontisan/byte_ops.rb +314 -0
- data/lib/fontisan/cli.rb +11 -5
- data/lib/fontisan/commands/audit_command.rb +38 -43
- data/lib/fontisan/commands/convert_command.rb +55 -1
- data/lib/fontisan/commands/instance_command.rb +75 -0
- data/lib/fontisan/commands/multi_format_output.rb +6 -1
- data/lib/fontisan/commands/subset_command.rb +43 -0
- data/lib/fontisan/commands/unpack_command.rb +56 -0
- data/lib/fontisan/converters/cff_table_builder.rb +1 -1
- data/lib/fontisan/converters/collection_converter.rb +13 -5
- data/lib/fontisan/converters/format_converter.rb +1 -1
- data/lib/fontisan/converters/woff2_encoder.rb +8 -6
- data/lib/fontisan/converters/woff_writer.rb +6 -5
- data/lib/fontisan/engine.rb +393 -0
- data/lib/fontisan/ffi/client.rb +123 -0
- data/lib/fontisan/ffi.rb +387 -0
- data/lib/fontisan/ffi_platform.rb +24 -0
- data/lib/fontisan/font_loader.rb +5 -1
- data/lib/fontisan/font_writer.rb +13 -1
- data/lib/fontisan/native.rb +235 -0
- data/lib/fontisan/output_guard.rb +40 -0
- data/lib/fontisan/reference/table_digests.rb +251 -0
- data/lib/fontisan/reference.rb +663 -0
- data/lib/fontisan/scan.rb +372 -0
- data/lib/fontisan/sfnt_directory.rb +41 -0
- data/lib/fontisan/sfnt_font.rb +16 -6
- data/lib/fontisan/subset/builder.rb +2 -1
- data/lib/fontisan/tables/cff/charstring.rb +16 -0
- data/lib/fontisan/tables/cff.rb +15 -5
- data/lib/fontisan/tables/cmap.rb +11 -1
- data/lib/fontisan/tables/gpos.rb +7 -0
- data/lib/fontisan/tables/gsub.rb +7 -0
- data/lib/fontisan/tables/layout_common.rb +172 -0
- data/lib/fontisan/tables/name.rb +10 -4
- data/lib/fontisan/utilities/brotli_wrapper.rb +39 -4
- data/lib/fontisan/validators/opentype_validator.rb +2 -0
- data/lib/fontisan/variable/instancer.rb +1 -0
- data/lib/fontisan/variable/static_font_builder.rb +4 -1
- data/lib/fontisan/variable/table_updater.rb +4 -2
- data/lib/fontisan/variation/instancing/axis_space.rb +1 -1
- data/lib/fontisan/variation/instancing/true_type_instancer.rb +14 -6
- data/lib/fontisan/version.rb +1 -1
- data/lib/fontisan/woff2/collection_decoder.rb +15 -1
- data/lib/fontisan/woff2/collection_encoder.rb +29 -6
- data/lib/fontisan/woff2/encoder_rules.rb +12 -4
- data/lib/fontisan/woff2/glyf_loca_reconstruct.rb +1 -1
- data/lib/fontisan/woff2_collection.rb +7 -5
- data/lib/fontisan/woff2_font.rb +12 -3
- data/lib/fontisan/woff_container.rb +83 -0
- data/lib/fontisan/woff_font.rb +24 -20
- data/lib/fontisan.rb +25 -1
- data/rakelib/bench.rake +9 -0
- data/rakelib/cdylib.rake +39 -0
- data/rakelib/corpus.rake +272 -0
- data/rakelib/corpus_convert.rake +43 -0
- data/rakelib/corpus_goldens.rake +22 -0
- data/rakelib/corpus_malformed.rake +20 -0
- data/rakelib/corpus_scan.rake +24 -0
- data/rakelib/java.rake +61 -0
- data/rakelib/native.rake +43 -0
- data/scripts/build-woff2.sh +57 -0
- data/scripts/corpus/generate_convert_goldens.rb +478 -0
- data/scripts/corpus/generate_goldens.rb +282 -0
- data/scripts/corpus/generate_malformed_goldens.rb +254 -0
- data/scripts/corpus/generate_scan_goldens.rb +98 -0
- data/scripts/tier_fallback.rb +228 -0
- data/scripts/tier_smoke.rb +72 -0
- metadata +71 -7
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fontisan
|
|
4
|
+
# Engine selection for the {Fontisan::Reference} operations.
|
|
5
|
+
#
|
|
6
|
+
# FONTISAN_ENGINE=ruby|native|ffi|auto (default auto)
|
|
7
|
+
# FONTISAN_ENGINE_STRICT=1 (no fallback)
|
|
8
|
+
#
|
|
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
|
|
13
|
+
# falls back to Ruby silently, or raises {TierUnavailable} at setup when
|
|
14
|
+
# strict. An operation (or a path, e.g. dfont) a tier cannot serve always goes to
|
|
15
|
+
# Ruby, strict or not. FONTISAN_MODE and FONTISAN_LAZY are not read here.
|
|
16
|
+
#
|
|
17
|
+
# The Ruby tier is the unmodified Reference implementation; the gate is a
|
|
18
|
+
# module prepended to Reference's singleton, so Reference bodies are
|
|
19
|
+
# untouched. Its output is unchanged (no served_by key), so goldens stay
|
|
20
|
+
# tier-independent; {Engine.scan} and {Engine.audit_dir} return the same
|
|
21
|
+
# rows with a per-row "served_by".
|
|
22
|
+
module Engine
|
|
23
|
+
class Error < Fontisan::Error; end
|
|
24
|
+
class ConfigError < Error; end
|
|
25
|
+
class TierUnavailable < Error; end
|
|
26
|
+
# A WOFF2 operation fell to the Ruby tier on a platform with no `brotli`.
|
|
27
|
+
class Woff2Unavailable < TierUnavailable; end
|
|
28
|
+
|
|
29
|
+
SELECTIONS = %w[ruby native ffi auto].freeze
|
|
30
|
+
STRICT_ON = %w[1 true yes on].freeze
|
|
31
|
+
STRICT_OFF = ["", "0", "false", "no", "off"].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
|
|
37
|
+
OPS = %i[read outline_hashes audit audit_dir scan convert extract
|
|
38
|
+
instance glyph_ids subset].freeze
|
|
39
|
+
REPLACEABLE_TIERS = %i[native ffi].freeze
|
|
40
|
+
TIER_CONTRACT = %i[available? supports? call].freeze
|
|
41
|
+
FACE_ENTRY = "fontisan/ruby"
|
|
42
|
+
ROW_RECORDER = :fontisan_engine_rows
|
|
43
|
+
IN_OPERATION = :fontisan_engine_in_operation
|
|
44
|
+
|
|
45
|
+
Config = Struct.new(:selection, :strict)
|
|
46
|
+
|
|
47
|
+
# The Ruby tier: the original Reference methods, captured before the
|
|
48
|
+
# gate is prepended.
|
|
49
|
+
class RubyTier
|
|
50
|
+
def initialize(originals)
|
|
51
|
+
@originals = originals
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def available?
|
|
55
|
+
true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def supports?(_operation, _path = nil)
|
|
59
|
+
true
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def call(operation, ...)
|
|
63
|
+
@originals.fetch(operation).call(...)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Prepended to Reference's singleton: one gate per Reference operation.
|
|
68
|
+
module Gate
|
|
69
|
+
OPS.each do |operation|
|
|
70
|
+
define_method(operation) do |*args, **kwargs, &block|
|
|
71
|
+
Engine.dispatch(operation, args, kwargs, &block)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
@mutex = Mutex.new
|
|
77
|
+
@tiers = nil
|
|
78
|
+
@registered_tiers = {}.freeze
|
|
79
|
+
@served = {}
|
|
80
|
+
@counters = nil
|
|
81
|
+
@availability = {}
|
|
82
|
+
|
|
83
|
+
class << self
|
|
84
|
+
# Prepend the gates onto +reference+ (once).
|
|
85
|
+
def install(reference)
|
|
86
|
+
return if reference.singleton_class.include?(Gate)
|
|
87
|
+
|
|
88
|
+
originals = OPS.to_h { |operation| [operation, reference.method(operation)] }
|
|
89
|
+
@tiers = { ruby: RubyTier.new(originals) }.freeze
|
|
90
|
+
reference.singleton_class.prepend(Gate)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Current configuration from the environment; raises at setup for a
|
|
94
|
+
# bad value or a strict forced tier that is not available.
|
|
95
|
+
def setup!
|
|
96
|
+
# The gates are installed when Reference loads (autoload); a fresh
|
|
97
|
+
# process, e.g. the CLI, reaches the engine before anything touched it.
|
|
98
|
+
Fontisan::Reference unless @tiers
|
|
99
|
+
config = config_from_env
|
|
100
|
+
return config unless config.strict && forced?(config.selection)
|
|
101
|
+
|
|
102
|
+
tier = config.selection
|
|
103
|
+
unless available?(tier)
|
|
104
|
+
raise TierUnavailable,
|
|
105
|
+
"FONTISAN_ENGINE=#{tier} with FONTISAN_ENGINE_STRICT=1 " \
|
|
106
|
+
"but the #{tier} tier is not available"
|
|
107
|
+
end
|
|
108
|
+
config
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Tier (Symbol) that serves +operation+ for +path+ under the current config.
|
|
112
|
+
def resolve(operation, path = nil, options = nil)
|
|
113
|
+
config = setup!
|
|
114
|
+
tier = candidates(config).find do |name|
|
|
115
|
+
available?(name) && supports?(name, operation, path, options)
|
|
116
|
+
end || :ruby
|
|
117
|
+
require_woff2_support!(operation, path, options) if tier == :ruby
|
|
118
|
+
tier
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Tier that served the last call of +operation+ in this process; nil if none.
|
|
122
|
+
def served_by(operation)
|
|
123
|
+
@mutex.synchronize { @served[operation.to_sym] }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# { tier => { operation => calls } } since the last {reset!}.
|
|
127
|
+
def counters
|
|
128
|
+
@mutex.synchronize do
|
|
129
|
+
(@counters || {}).transform_values(&:dup)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Scan rows with a "served_by" tier name per row.
|
|
134
|
+
def scan(paths)
|
|
135
|
+
scan_pairs(paths).flat_map do |tier, rows|
|
|
136
|
+
rows.map { |row| row.merge("served_by" => tier.to_s) }
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# audit_dir entries with a "served_by" tier name per entry: the tier
|
|
141
|
+
# that audited that file. A file the serving tier hands back to Ruby
|
|
142
|
+
# (dfont, Type 1 inside a native audit_dir) is recorded as :ruby by the
|
|
143
|
+
# nested call; every other entry carries the tier that served the call.
|
|
144
|
+
def audit_dir(dir, validate: false)
|
|
145
|
+
dir_path = File.path(dir)
|
|
146
|
+
whole = resolve(:audit_dir, dir_path)
|
|
147
|
+
seen = {}
|
|
148
|
+
entries = with_recorder(seen) do
|
|
149
|
+
Reference.audit_dir(dir, validate: validate)
|
|
150
|
+
end
|
|
151
|
+
entries.map do |entry|
|
|
152
|
+
tier = seen[File.join(dir_path, entry["path"])] || whole
|
|
153
|
+
entry.merge("served_by" => tier.to_s)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Run +operation+ on the Ruby tier, whatever is selected (a tier handing
|
|
158
|
+
# a file it cannot read back to Ruby).
|
|
159
|
+
def ruby_call(operation, ...)
|
|
160
|
+
tier_object(:ruby).call(operation, ...)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Called by the gates.
|
|
164
|
+
def dispatch(operation, args, kwargs, &)
|
|
165
|
+
path = args.first
|
|
166
|
+
path = path.to_s if path.respond_to?(:to_path)
|
|
167
|
+
path = nil unless path.is_a?(String)
|
|
168
|
+
return scan_rows(args, kwargs) if operation == :scan && !args.empty?
|
|
169
|
+
|
|
170
|
+
tier = resolve(operation, path, kwargs)
|
|
171
|
+
run(tier, operation, path, args, kwargs, &)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# The object-based entry points (`Subset::Builder.new(font, ...)`,
|
|
175
|
+
# `Variable::Instancer.new(font)`) are never dispatched: a loaded font's
|
|
176
|
+
# state lives in cached parsed tables, so they always run on Ruby. They
|
|
177
|
+
# call this so that {served_by} reports it; inside a gated call (the
|
|
178
|
+
# Ruby tier's own Reference body) the gate has already recorded the tier.
|
|
179
|
+
def record_ruby(operation)
|
|
180
|
+
return if Thread.current[IN_OPERATION]
|
|
181
|
+
|
|
182
|
+
record(:ruby, operation, nil)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Forget probes, counters, served state and temporary tier overrides.
|
|
186
|
+
def reset!
|
|
187
|
+
@mutex.synchronize do
|
|
188
|
+
@served = {}
|
|
189
|
+
@counters = nil
|
|
190
|
+
@availability = {}
|
|
191
|
+
@tiers = @tiers.slice(:ruby) if @tiers
|
|
192
|
+
end
|
|
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
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Override a tier object until reset! (tests, or the extension itself). A bad
|
|
209
|
+
# registration raises here, at setup, never as a silent fallback later.
|
|
210
|
+
def register(name, tier)
|
|
211
|
+
return register_tier(name, tier) if @loading_face
|
|
212
|
+
|
|
213
|
+
validate_registration!(name, tier)
|
|
214
|
+
Fontisan::Reference unless @tiers
|
|
215
|
+
@mutex.synchronize do
|
|
216
|
+
@tiers = @tiers.merge(name.to_sym => tier).freeze
|
|
217
|
+
@availability.delete(name.to_sym)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
private
|
|
222
|
+
|
|
223
|
+
def validate_registration!(name, tier)
|
|
224
|
+
unless REPLACEABLE_TIERS.include?(name.to_s.to_sym)
|
|
225
|
+
raise ConfigError,
|
|
226
|
+
"cannot register tier #{name.inspect}: only " \
|
|
227
|
+
"#{REPLACEABLE_TIERS.join(' and ')} can be registered"
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
missing = TIER_CONTRACT.reject { |method| tier.respond_to?(method) }
|
|
231
|
+
return if missing.empty?
|
|
232
|
+
|
|
233
|
+
raise ConfigError,
|
|
234
|
+
"tier #{name} does not implement #{missing.join(', ')}"
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def config_from_env
|
|
238
|
+
selection = ENV.fetch("FONTISAN_ENGINE", "auto").strip.downcase
|
|
239
|
+
selection = "auto" if selection.empty?
|
|
240
|
+
unless SELECTIONS.include?(selection)
|
|
241
|
+
raise ConfigError,
|
|
242
|
+
"FONTISAN_ENGINE=#{selection.inspect} is not one of " \
|
|
243
|
+
"#{SELECTIONS.join(', ')}"
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
Config.new(selection.to_sym, strict_from_env)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# 1/true/yes/on is strict; unset, empty and 0/false/no/off is not. Any
|
|
250
|
+
# other value raises, as an unknown FONTISAN_ENGINE does: a typo must not
|
|
251
|
+
# silently leave a service non-strict.
|
|
252
|
+
def strict_from_env
|
|
253
|
+
value = ENV.fetch("FONTISAN_ENGINE_STRICT", "").strip.downcase
|
|
254
|
+
return true if STRICT_ON.include?(value)
|
|
255
|
+
return false if STRICT_OFF.include?(value)
|
|
256
|
+
|
|
257
|
+
raise ConfigError,
|
|
258
|
+
"FONTISAN_ENGINE_STRICT=#{value.inspect} is not one of " \
|
|
259
|
+
"#{(STRICT_ON + STRICT_OFF.reject(&:empty?)).join(', ')}"
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# The java-platform gem has no `brotli`: a WOFF2 operation that no other
|
|
263
|
+
# tier took must fail here, at setup, not as a LoadError mid-parse.
|
|
264
|
+
def require_woff2_support!(operation, path, options)
|
|
265
|
+
return unless woff2_operation?(path, options)
|
|
266
|
+
return if Utilities::BrotliWrapper.available?
|
|
267
|
+
|
|
268
|
+
raise Woff2Unavailable,
|
|
269
|
+
"#{operation} on WOFF2 needs the ffi tier or the `brotli` gem; " \
|
|
270
|
+
"neither is available. #{Utilities::BrotliWrapper::MISSING_MESSAGE}"
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def woff2_operation?(path, options)
|
|
274
|
+
File.extname(path.to_s).casecmp?(".woff2") ||
|
|
275
|
+
options&.dig(:to).to_s.casecmp?("woff2")
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def forced?(selection)
|
|
279
|
+
%i[native ffi].include?(selection)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def candidates(config)
|
|
283
|
+
case config.selection
|
|
284
|
+
when :auto then AUTO_ORDER
|
|
285
|
+
when :ruby then [:ruby]
|
|
286
|
+
else [config.selection]
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def tier_object(name)
|
|
291
|
+
case name
|
|
292
|
+
when :ruby then @tiers.fetch(:ruby)
|
|
293
|
+
else @tiers[name] || @registered_tiers[name]
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def available?(name)
|
|
298
|
+
@mutex.synchronize do
|
|
299
|
+
return @availability[name] if @availability.key?(name)
|
|
300
|
+
end
|
|
301
|
+
result = probe(name)
|
|
302
|
+
@mutex.synchronize { @availability[name] = result }
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Any failure while probing is "not available"; never an exception.
|
|
306
|
+
def probe(name)
|
|
307
|
+
load_face if REPLACEABLE_TIERS.include?(name) && !@tiers.key?(name)
|
|
308
|
+
tier_object(name).available? || false
|
|
309
|
+
rescue StandardError, ScriptError
|
|
310
|
+
false
|
|
311
|
+
end
|
|
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
|
+
|
|
330
|
+
# A tier may also vet the call's keyword options (`supports_options?`).
|
|
331
|
+
def supports?(name, operation, path, options = nil)
|
|
332
|
+
tier = tier_object(name)
|
|
333
|
+
return false unless tier.supports?(operation, path)
|
|
334
|
+
return true unless options && tier.respond_to?(:supports_options?)
|
|
335
|
+
|
|
336
|
+
takes_path = tier.method(:supports_options?).arity.abs > 2
|
|
337
|
+
(takes_path ? tier.supports_options?(operation, options, path) : tier.supports_options?(operation, options)) || false
|
|
338
|
+
rescue StandardError, ScriptError
|
|
339
|
+
false
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def run(tier, operation, path, args, kwargs, &)
|
|
343
|
+
record(tier, operation, path)
|
|
344
|
+
previous = Thread.current[IN_OPERATION]
|
|
345
|
+
Thread.current[IN_OPERATION] = true
|
|
346
|
+
begin
|
|
347
|
+
tier_object(tier).call(operation, *args, **kwargs, &)
|
|
348
|
+
ensure
|
|
349
|
+
Thread.current[IN_OPERATION] = previous
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
def record(tier, operation, path)
|
|
354
|
+
@mutex.synchronize do
|
|
355
|
+
@served[operation] = tier
|
|
356
|
+
table = (@counters ||= Hash.new { |h, k| h[k] = Hash.new(0) })
|
|
357
|
+
table[tier][operation] += 1
|
|
358
|
+
end
|
|
359
|
+
recorder = Thread.current[ROW_RECORDER]
|
|
360
|
+
recorder[path] = tier if recorder && path
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def scan_rows(args, kwargs)
|
|
364
|
+
raise ArgumentError, "scan takes the paths only" unless kwargs.empty?
|
|
365
|
+
|
|
366
|
+
setup!
|
|
367
|
+
|
|
368
|
+
scan_pairs(*args).flat_map { |_tier, rows| rows }
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# [[tier, rows], ...] one pair per path, in input order.
|
|
372
|
+
def scan_pairs(paths)
|
|
373
|
+
Array(paths).map do |path|
|
|
374
|
+
path_s = path.respond_to?(:to_path) ? path.to_path : path
|
|
375
|
+
tier = resolve(:scan, path_s.is_a?(String) ? path_s : nil)
|
|
376
|
+
record(tier, :scan, nil)
|
|
377
|
+
[tier, tier_object(tier).call(:scan, [path]).to_a]
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def with_recorder(seen)
|
|
382
|
+
previous = Thread.current[ROW_RECORDER]
|
|
383
|
+
Thread.current[ROW_RECORDER] = seen
|
|
384
|
+
yield
|
|
385
|
+
ensure
|
|
386
|
+
Thread.current[ROW_RECORDER] = previous
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
register_tier(:native, Native)
|
|
391
|
+
register_tier(:ffi, Ffi)
|
|
392
|
+
end
|
|
393
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ffi"
|
|
4
|
+
|
|
5
|
+
module Fontisan
|
|
6
|
+
module Ffi
|
|
7
|
+
# One loaded copy of the cdylib: the C structs of fontisan.h (ABI 1) and
|
|
8
|
+
# the three exported functions. Only this file requires the optional `ffi`
|
|
9
|
+
# gem, and only {Fontisan::Ffi} requires this file.
|
|
10
|
+
class Client
|
|
11
|
+
MAGIC = 0x4E415346 # "FSAN"
|
|
12
|
+
ABI_VERSION = 1
|
|
13
|
+
|
|
14
|
+
OK = 0
|
|
15
|
+
INVALID_ARG = 7
|
|
16
|
+
READ_FAILED = 9
|
|
17
|
+
UNSUPPORTED = 10
|
|
18
|
+
|
|
19
|
+
# The result block of one call; its pointers die with {#close}.
|
|
20
|
+
Reply = Struct.new(:status, :payload, :message, :kind)
|
|
21
|
+
|
|
22
|
+
class Header < FFI::Struct
|
|
23
|
+
layout :magic, :uint32, :abi_version, :uint32, :struct_size, :uint32,
|
|
24
|
+
:reserved, :uint32
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Request < FFI::Struct
|
|
28
|
+
layout :header, Header, :op, :uint32, :flags, :uint32,
|
|
29
|
+
:input, :pointer, :input_len, :size_t
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class ErrorRow < FFI::Struct
|
|
33
|
+
layout :code, :int32, :message, :pointer, :message_len, :size_t
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class Result < FFI::Struct
|
|
37
|
+
layout :header, Header, :status, :int32, :error, ErrorRow,
|
|
38
|
+
:payload, :pointer, :payload_len, :size_t,
|
|
39
|
+
:error_kind, :int32, :reserved, :int32
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Loads +path+ and binds its symbols.
|
|
43
|
+
#
|
|
44
|
+
# @return [Module] an FFI::Library with the exported functions attached
|
|
45
|
+
def self.bind(path)
|
|
46
|
+
Module.new do
|
|
47
|
+
extend FFI::Library
|
|
48
|
+
|
|
49
|
+
ffi_lib path
|
|
50
|
+
attach_function :fontisan_abi_version, [], :uint32
|
|
51
|
+
attach_function :fontisan_call, %i[pointer pointer], :int32
|
|
52
|
+
attach_function :fontisan_result_free, [:pointer], :void
|
|
53
|
+
attach_function :fontisan_cdylib_engine_rev, [], :string
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @raise [LoadError] when the library or one of its symbols is missing; a
|
|
58
|
+
# library from before the engine-rev symbol existed is stale, so it too
|
|
59
|
+
# @raise [Fontisan::Ffi::AbiMismatch] when it speaks another ABI version
|
|
60
|
+
# or was built from another engine rev than {Fontisan::Native::ENGINE_REV}
|
|
61
|
+
def initialize(path)
|
|
62
|
+
@binding = self.class.bind(path)
|
|
63
|
+
found = @binding.fontisan_abi_version
|
|
64
|
+
raise AbiMismatch, "#{path} speaks ABI #{found}, this gem expects #{ABI_VERSION}" unless found == ABI_VERSION
|
|
65
|
+
|
|
66
|
+
rev = @binding.fontisan_cdylib_engine_rev
|
|
67
|
+
return if rev == Fontisan::Native::ENGINE_REV
|
|
68
|
+
|
|
69
|
+
raise AbiMismatch, "#{path} was built from engine #{rev}, this gem expects #{Fontisan::Native::ENGINE_REV}"
|
|
70
|
+
rescue FFI::NotFoundError => e
|
|
71
|
+
raise LoadError, e.message
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Run one operation.
|
|
75
|
+
#
|
|
76
|
+
# @param operation [Integer] a FONTISAN_OP_* number
|
|
77
|
+
# @param input [String] the request bytes
|
|
78
|
+
# @param flags [Integer]
|
|
79
|
+
# @return [Reply]
|
|
80
|
+
def call(operation, input, flags = 0)
|
|
81
|
+
input = input.b
|
|
82
|
+
buffer = FFI::MemoryPointer.new(:uint8, [input.bytesize, 1].max)
|
|
83
|
+
buffer.put_bytes(0, input)
|
|
84
|
+
out = FFI::MemoryPointer.new(:pointer)
|
|
85
|
+
@binding.fontisan_call(request(operation, flags, buffer, input.bytesize), out)
|
|
86
|
+
read_result(out.read_pointer)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def request(operation, flags, buffer, length)
|
|
92
|
+
req = Request.new
|
|
93
|
+
req[:header][:magic] = MAGIC
|
|
94
|
+
req[:header][:abi_version] = ABI_VERSION
|
|
95
|
+
req[:header][:struct_size] = Request.size
|
|
96
|
+
req[:header][:reserved] = 0
|
|
97
|
+
req[:op] = operation
|
|
98
|
+
req[:flags] = flags
|
|
99
|
+
req[:input] = buffer
|
|
100
|
+
req[:input_len] = length
|
|
101
|
+
req
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def read_result(pointer)
|
|
105
|
+
raise Fontisan::Error, "fontisan_call returned no result block" if pointer.null?
|
|
106
|
+
|
|
107
|
+
result = Result.new(pointer)
|
|
108
|
+
Reply.new(result[:status], bytes(result[:payload], result[:payload_len]),
|
|
109
|
+
bytes(result[:error][:message], result[:error][:message_len])
|
|
110
|
+
.force_encoding(Encoding::UTF_8),
|
|
111
|
+
result[:error_kind])
|
|
112
|
+
ensure
|
|
113
|
+
@binding.fontisan_result_free(pointer) unless pointer.null?
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def bytes(pointer, length)
|
|
117
|
+
return "".b if pointer.null? || length.zero?
|
|
118
|
+
|
|
119
|
+
pointer.read_bytes(length)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|