fontisan 0.4.52-java → 0.4.53-java
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/docs/guide/engines.md +1 -1
- data/lib/fontisan/byte_ops.rb +14 -0
- data/lib/fontisan/config/conversion_matrix.yml +14 -12
- data/lib/fontisan/converters/cff_table_builder.rb +26 -46
- data/lib/fontisan/converters/type1_converter.rb +177 -63
- data/lib/fontisan/ffi/windows-x86_64/fontisan_cdylib.dll +0 -0
- data/lib/fontisan/ffi.rb +8 -7
- data/lib/fontisan/native.rb +6 -5
- data/lib/fontisan/pipeline/output_writer.rb +16 -0
- data/lib/fontisan/pipeline/transformation_pipeline.rb +30 -3
- data/lib/fontisan/tables/cff/charstring_parser.rb +8 -0
- data/lib/fontisan/type1/cff_to_type1_converter.rb +33 -15
- data/lib/fontisan/type1/charstrings.rb +2 -1
- data/lib/fontisan/type1/decryptor.rb +6 -6
- data/lib/fontisan/type1/pfb_generator.rb +90 -241
- data/lib/fontisan/type1/pfb_writer.rb +153 -0
- data/lib/fontisan/type1/ttf_to_type1_converter.rb +7 -1
- data/lib/fontisan/type1.rb +1 -0
- data/lib/fontisan/version.rb +1 -1
- data/scripts/corpus/generate_convert_goldens.rb +46 -6
- data/scripts/validate_t1_surfaces.rb +14 -4
- data/scripts/verify_release.rb +10 -2
- metadata +2 -17
- data/TODO.improvements/01-cbdt-cblc-gid-stable-propagation.md +0 -73
- data/TODO.improvements/02-collection-outline-priority.md +0 -48
- data/TODO.improvements/03-fontisan-audit-command.md +0 -219
- data/TODO.improvements/04-ufo-composite-glyph-encoding.md +0 -46
- data/TODO.improvements/05-otf-compiler-real-cff.md +0 -63
- data/TODO.improvements/06-cff2-blend-vsindex-operators.md +0 -51
- data/TODO.improvements/07-cpal-v1-header-fields.md +0 -71
- data/TODO.improvements/08-cff-standard-string-table.md +0 -52
- data/TODO.improvements/09-type1-seac-expansion.md +0 -57
- data/TODO.improvements/10-ufo-image-set-feature-writers.md +0 -71
- data/TODO.improvements/11-kern-groups-plist.md +0 -49
- data/TODO.improvements/12-cbdt-fixture-bindata-conversion.md +0 -68
- data/TODO.improvements/13-split-octokit-fetcher.md +0 -45
- data/TODO.improvements/14-rubocop-baseline-chip.md +0 -62
- data/TODO.improvements/15-cff-cff2-subsetter-strategy.md +0 -78
- data/TODO.improvements/README.md +0 -58
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3996fc4a9e8139a3f8814a31db4035bfd8a2bcfff2288f874355abefca964566
|
|
4
|
+
data.tar.gz: b726d961b89ded6ebc80bfbea2cfc2d284c64872deef6aed10f8f96709300774
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d872bbd5ab037dce7baf3a36890c4f6044730ffe4b5c850af7b386c727a2fa8e87df36bba9f4a84e3667d72618bbe645d75e94729880fc741930dd2cb37b5e79
|
|
7
|
+
data.tar.gz: ce4f78f09a02d90bb4c5a154ddf9c44cfe6854b4d0ccee8be591635142b0b5e179d2d27a247e29cebdc090c40061c9fe9e2949819b5d7d6912ed5ef8e6becea0
|
data/docs/guide/engines.md
CHANGED
|
@@ -75,7 +75,7 @@ The full capability matrix (a 2026-11 audit of the engine tier, `lib/fontisan/na
|
|
|
75
75
|
| `read` (metadata/full, lazy) | every format the engine reads, dfont included; a Type 1 file serves the facade's own measured surface — the loader parses the PFB segments, then the read raises the `Type1Font` `NoMethodError` (#192 gap 4) | |
|
|
76
76
|
| `audit`, `audit_dir` (+ `validate`) | engine-readable files and directories; a Type 1 file serves the same surface, and an `audit_dir` walk serves through the tier with its Type 1 rows handed back to Ruby (each tagged `served_by: "ruby"`) | |
|
|
77
77
|
| `scan` | engine-readable files | Type 1 and non-font files |
|
|
78
|
-
| `convert` to ttf/otf/
|
|
78
|
+
| `convert` to ttf/otf/svg; collections to ttc/otc; to `type1` (PFB) | engine-readable inputs; an `.otc` input carries the caller's source hint (#192 gap 7). Type 1 conversions stay with Ruby, which converts all four directions (the measured read surfaces of #192 gap 4 remain for read/audit) | WOFF and WOFF2 encodes (the engine's pure-Rust DEFLATE/Brotli streams differ from the Ruby tier's libz/brotli-gem bytes) and a single font to a collection format |
|
|
79
79
|
| `extract` (collection member) | collections | |
|
|
80
80
|
| `instance` (TTF variable font) | single fonts | |
|
|
81
81
|
| `subset` (web/pdf/minimal) | engine-readable single fonts and collections | |
|
data/lib/fontisan/byte_ops.rb
CHANGED
|
@@ -101,6 +101,20 @@ module Fontisan
|
|
|
101
101
|
target = convert_target(options[:to]) or return false
|
|
102
102
|
return false unless path.is_a?(String)
|
|
103
103
|
|
|
104
|
+
# WOFF encode stays with Ruby for sfnt sources: the engine's pure-Rust
|
|
105
|
+
# DEFLATE produces different streams than the Ruby tier's libz. A WOFF
|
|
106
|
+
# or WOFF2 source keeps the engine - its decode is the brotli-dependent
|
|
107
|
+
# detour (a brotli-less JRuby has no Ruby-side decode or encode, and
|
|
108
|
+
# the engine's bytes for these conversions are identical everywhere).
|
|
109
|
+
if target == :woff
|
|
110
|
+
source = begin
|
|
111
|
+
FontLoader.detect_format(path)
|
|
112
|
+
rescue StandardError
|
|
113
|
+
nil
|
|
114
|
+
end
|
|
115
|
+
return %i[woff woff2].include?(source)
|
|
116
|
+
end
|
|
117
|
+
|
|
104
118
|
(collection_file?(path) ? COLLECTION_TARGETS : SINGLE_TARGETS).include?(target)
|
|
105
119
|
end
|
|
106
120
|
|
|
@@ -70,27 +70,29 @@ conversions:
|
|
|
70
70
|
to: otf
|
|
71
71
|
strategy: type1_converter
|
|
72
72
|
description: "Convert Type 1 to OpenType/CFF format"
|
|
73
|
-
status:
|
|
73
|
+
status: implemented
|
|
74
74
|
notes: >
|
|
75
75
|
Type 1 to OpenType conversion using CharStringConverter. Converts Type 1
|
|
76
|
-
CharStrings to CFF format and builds CFF table
|
|
77
|
-
|
|
76
|
+
CharStrings to CFF format and builds the CFF table (header, Name/Top
|
|
77
|
+
DICT/String/GlobalSubr/CharStrings INDEXes, Private DICT) with a
|
|
78
|
+
fixed-point offset layout. seac composites are expanded. Some hinting
|
|
79
|
+
may not be preserved.
|
|
78
80
|
|
|
79
81
|
- from: otf
|
|
80
82
|
to: type1
|
|
81
83
|
strategy: type1_converter
|
|
82
84
|
description: "Convert OpenType/CFF to Type 1 format"
|
|
83
|
-
status:
|
|
85
|
+
status: implemented
|
|
84
86
|
notes: >
|
|
85
|
-
OpenType to Type 1 conversion.
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
OpenType to Type 1 conversion. CFF CharStrings convert to Type 1
|
|
88
|
+
(coordinates rescaled to 1000 upm); PFBWriter assembles the canonical
|
|
89
|
+
three-segment PFB with deterministic eexec/CharString encryption.
|
|
88
90
|
|
|
89
91
|
- from: type1
|
|
90
92
|
to: ttf
|
|
91
93
|
strategy: type1_converter
|
|
92
94
|
description: "Convert Type 1 to TrueType format"
|
|
93
|
-
status:
|
|
95
|
+
status: implemented
|
|
94
96
|
notes: >
|
|
95
97
|
Two-step conversion: Type 1 → OTF → TTF. First converts to OpenType/CFF,
|
|
96
98
|
then to TrueType with cubic-to-quadratic curve approximation. Hinting may
|
|
@@ -100,11 +102,11 @@ conversions:
|
|
|
100
102
|
to: type1
|
|
101
103
|
strategy: type1_converter
|
|
102
104
|
description: "Convert TrueType to Type 1 format"
|
|
103
|
-
status:
|
|
105
|
+
status: implemented
|
|
104
106
|
notes: >
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
Direct conversion: every glyf glyph becomes a Type 1 CharString
|
|
108
|
+
(quadratic curves become cubics via TTFToType1Converter), assembled into
|
|
109
|
+
the canonical PFB by PFBGenerator with deterministic encryption.
|
|
108
110
|
|
|
109
111
|
# Phase 2 conversions (Milestone 2.1) - WOFF2
|
|
110
112
|
- from: ttf
|
|
@@ -115,52 +115,32 @@ module Fontisan
|
|
|
115
115
|
charstrings_index_data,
|
|
116
116
|
private_dict_size
|
|
117
117
|
)
|
|
118
|
-
#
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
private: [private_dict_size, private_dict_offset],
|
|
145
|
-
}
|
|
146
|
-
top_dict_data = Tables::Cff::DictBuilder.build(top_dict_hash)
|
|
147
|
-
top_dict_index_data = Tables::Cff::IndexBuilder.build([top_dict_data])
|
|
148
|
-
|
|
149
|
-
# Final recalculation
|
|
150
|
-
string_index_start = top_dict_index_start + top_dict_index_data.bytesize
|
|
151
|
-
global_subr_index_start = string_index_start + string_index_data.bytesize
|
|
152
|
-
charstrings_offset = global_subr_index_start + global_subr_index_data.bytesize
|
|
153
|
-
private_dict_offset = charstrings_offset + charstrings_index_data.bytesize
|
|
154
|
-
|
|
155
|
-
# Final Top DICT
|
|
156
|
-
top_dict_hash = {
|
|
157
|
-
charset: 0,
|
|
158
|
-
encoding: 0,
|
|
159
|
-
charstrings: charstrings_offset,
|
|
160
|
-
private: [private_dict_size, private_dict_offset],
|
|
161
|
-
}
|
|
162
|
-
top_dict_data = Tables::Cff::DictBuilder.build(top_dict_hash)
|
|
163
|
-
top_dict_index_data = Tables::Cff::IndexBuilder.build([top_dict_data])
|
|
118
|
+
# Everything after the Top DICT INDEX sits at a position that depends
|
|
119
|
+
# only on the INDEX's own size, and its size depends on the offsets it
|
|
120
|
+
# encodes (operand widths). Iterate to the fixed point: the loop exits
|
|
121
|
+
# when the INDEX built from the offsets is exactly the size used to
|
|
122
|
+
# compute them, so the assembly below is byte-exact.
|
|
123
|
+
base = header_size + name_index_data.bytesize +
|
|
124
|
+
string_index_data.bytesize + global_subr_index_data.bytesize
|
|
125
|
+
top_size = 0
|
|
126
|
+
top_dict_index_data = nil
|
|
127
|
+
charstrings_offset = 0
|
|
128
|
+
private_dict_offset = 0
|
|
129
|
+
|
|
130
|
+
8.times do
|
|
131
|
+
charstrings_offset = base + top_size
|
|
132
|
+
private_dict_offset = charstrings_offset + charstrings_index_data.bytesize
|
|
133
|
+
top_dict_data = Tables::Cff::DictBuilder.build(
|
|
134
|
+
charset: 0,
|
|
135
|
+
encoding: 0,
|
|
136
|
+
charstrings: charstrings_offset,
|
|
137
|
+
private: [private_dict_size, private_dict_offset],
|
|
138
|
+
)
|
|
139
|
+
top_dict_index_data = Tables::Cff::IndexBuilder.build([top_dict_data])
|
|
140
|
+
break if top_dict_index_data.bytesize == top_size
|
|
141
|
+
|
|
142
|
+
top_size = top_dict_index_data.bytesize
|
|
143
|
+
end
|
|
164
144
|
|
|
165
145
|
[top_dict_index_data, charstrings_offset, private_dict_offset]
|
|
166
146
|
end
|
|
@@ -43,6 +43,10 @@ module Fontisan
|
|
|
43
43
|
include ConversionStrategy
|
|
44
44
|
include CffTableBuilder
|
|
45
45
|
|
|
46
|
+
# The fixed head created/modified date for Type 1 conversions
|
|
47
|
+
# (2020-01-01T00:00:00Z, Unix seconds) — conversions are reproducible.
|
|
48
|
+
FIXED_HEAD_TIMESTAMP = 1_577_836_800
|
|
49
|
+
|
|
46
50
|
# Initialize a new Type1Converter
|
|
47
51
|
#
|
|
48
52
|
# @param options [Hash] Converter options
|
|
@@ -299,12 +303,58 @@ module Fontisan
|
|
|
299
303
|
# Build cmap table
|
|
300
304
|
tables["cmap"] = build_cmap_table(font)
|
|
301
305
|
|
|
306
|
+
# Build hmtx (advance width and lsb per glyph, from the converted
|
|
307
|
+
# CharStrings' hsbw) — hhea declares numberOfHMetrics, so a font
|
|
308
|
+
# without hmtx fails sanitisation.
|
|
309
|
+
tables["hmtx"] = build_hmtx_table(cff_charstrings)
|
|
310
|
+
|
|
302
311
|
# Add CFF table
|
|
303
312
|
tables["CFF "] = cff_data
|
|
304
313
|
|
|
305
314
|
tables
|
|
306
315
|
end
|
|
307
316
|
|
|
317
|
+
# Per-glyph (advanceWidth, lsb) rows decoded from the hsbw at the head
|
|
318
|
+
# of each converted CharString, in the CharStrings INDEX order.
|
|
319
|
+
def build_hmtx_table(charstrings)
|
|
320
|
+
data = (+"").b
|
|
321
|
+
charstrings.each_value do |charstring|
|
|
322
|
+
width = hsbw_of(charstring)[1] || 0
|
|
323
|
+
data << [[width, 0].max].pack("n")
|
|
324
|
+
data << [0].pack("s>")
|
|
325
|
+
end
|
|
326
|
+
data
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# Decode the leading numbers of a Type 1 CharString (the hsbw's lsb and
|
|
330
|
+
# width), using the Type 1 variable-length integer encoding.
|
|
331
|
+
def hsbw_of(bytes)
|
|
332
|
+
numbers = []
|
|
333
|
+
i = 0
|
|
334
|
+
while i < bytes.length && numbers.size < 2
|
|
335
|
+
v = bytes.getbyte(i)
|
|
336
|
+
case v
|
|
337
|
+
when 32..246
|
|
338
|
+
numbers << v - 139
|
|
339
|
+
i += 1
|
|
340
|
+
when 247..250
|
|
341
|
+
numbers << (((v - 247) << 8) + bytes.getbyte(i + 1) + 108)
|
|
342
|
+
i += 2
|
|
343
|
+
when 251..254
|
|
344
|
+
numbers << (-((v - 251) << 8) - bytes.getbyte(i + 1) - 108)
|
|
345
|
+
i += 2
|
|
346
|
+
when 255
|
|
347
|
+
raw = bytes.byteslice(i + 1, 4).unpack1("N")
|
|
348
|
+
signed = raw >= 0x80000000 ? raw - (1 << 32) : raw
|
|
349
|
+
numbers << (signed / 65_536.0).round
|
|
350
|
+
i += 5
|
|
351
|
+
else
|
|
352
|
+
i += 1
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
numbers
|
|
356
|
+
end
|
|
357
|
+
|
|
308
358
|
# Convert OpenType/CFF font to Type 1
|
|
309
359
|
#
|
|
310
360
|
# @param font [OpenTypeFont] Source OpenType font
|
|
@@ -325,23 +375,38 @@ module Fontisan
|
|
|
325
375
|
# Get Private DICT for context
|
|
326
376
|
private_dict = cff_table.private_dict(0)
|
|
327
377
|
|
|
328
|
-
# Create CFF to Type 1 converter
|
|
378
|
+
# Create CFF to Type 1 converter, rescaling coordinates to 1000 upm
|
|
379
|
+
upm = font.table(Constants::HEAD_TAG)&.units_per_em.to_i
|
|
380
|
+
scale = upm.positive? ? Rational(1000, upm) : Rational(1)
|
|
329
381
|
converter = Type1::CffToType1Converter.new(
|
|
330
|
-
nominal_width: private_dict&.
|
|
331
|
-
default_width: private_dict&.
|
|
382
|
+
nominal_width: ((private_dict&.nominal_width_x || 0) * scale).round,
|
|
383
|
+
default_width: ((private_dict&.default_width_x || 0) * scale).round,
|
|
384
|
+
scale: scale,
|
|
332
385
|
)
|
|
333
386
|
|
|
334
387
|
# Convert each CFF CharString to Type 1 format
|
|
335
388
|
type1_charstrings = {}
|
|
336
389
|
glyph_count = charstrings_index.count
|
|
337
390
|
|
|
391
|
+
# Hoisted: the charset parses on every call, so a per-glyph lookup
|
|
392
|
+
# would re-parse it once per glyph.
|
|
393
|
+
charset = cff_table.charset(0)
|
|
394
|
+
post_table = font.table("post")
|
|
395
|
+
|
|
338
396
|
glyph_count.times do |glyph_index|
|
|
339
397
|
# Get raw CFF CharString data
|
|
340
398
|
cff_charstring = charstrings_index[glyph_index]
|
|
341
399
|
next unless cff_charstring
|
|
342
400
|
|
|
343
|
-
# Get glyph name
|
|
344
|
-
|
|
401
|
+
# Get glyph name from the CFF charset (fall back to gid order); a
|
|
402
|
+
# name the earlier glyphs already claimed (sub-setted CFFs carry
|
|
403
|
+
# dangling charset SIDs that all resolve to .notdef) falls back to
|
|
404
|
+
# the gid name so every glyph keeps its own CharString.
|
|
405
|
+
glyph_name = charset&.glyph_name(glyph_index) ||
|
|
406
|
+
post_table&.glyph_name_for(glyph_index) ||
|
|
407
|
+
"glyph#{glyph_index}"
|
|
408
|
+
glyph_name = "glyph#{glyph_index}" if glyph_index.positive? &&
|
|
409
|
+
(glyph_name == ".notdef" || type1_charstrings.key?(glyph_name))
|
|
345
410
|
|
|
346
411
|
# Convert CFF CharString to Type 1 format
|
|
347
412
|
private_dict_hash = build_private_dict_hash(private_dict)
|
|
@@ -365,44 +430,24 @@ module Fontisan
|
|
|
365
430
|
otf_tables = convert_type1_to_otf(font, options)
|
|
366
431
|
|
|
367
432
|
# Then use OutlineConverter to convert OTF to TTF
|
|
368
|
-
|
|
369
|
-
temp_otf = OpenTypeFont.new
|
|
370
|
-
otf_tables.each do |tag, data|
|
|
371
|
-
temp_otf.tables[tag] = data
|
|
372
|
-
end
|
|
433
|
+
temp_otf = OpenTypeFont.from_tables(otf_tables)
|
|
373
434
|
|
|
374
435
|
# Use OutlineConverter for OTF → TTF
|
|
375
|
-
outline_converter = OutlineConverter.new
|
|
376
|
-
optimize_cff: @optimize_cff,
|
|
377
|
-
preserve_hints: @preserve_hints,
|
|
378
|
-
target_format: :ttf,
|
|
379
|
-
)
|
|
380
|
-
|
|
436
|
+
outline_converter = OutlineConverter.new
|
|
381
437
|
outline_converter.convert(temp_otf, target_format: :ttf)
|
|
382
438
|
end
|
|
383
439
|
|
|
384
|
-
# Convert TrueType font to Type 1
|
|
440
|
+
# Convert TrueType font to Type 1
|
|
441
|
+
#
|
|
442
|
+
# Direct conversion: every glyf glyph becomes a Type 1 CharString
|
|
443
|
+
# (quadratic curves become cubics), assembled into the canonical PFB
|
|
444
|
+
# by {Type1::PFBGenerator}.
|
|
385
445
|
#
|
|
386
446
|
# @param font [TrueTypeFont] Source TrueType font
|
|
387
|
-
# @
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
optimize_cff: @optimize_cff,
|
|
392
|
-
preserve_hints: @preserve_hints,
|
|
393
|
-
target_format: :otf,
|
|
394
|
-
)
|
|
395
|
-
|
|
396
|
-
otf_tables = outline_converter.convert(font, target_format: :otf)
|
|
397
|
-
|
|
398
|
-
# Create a temporary OTF font object
|
|
399
|
-
temp_otf = OpenTypeFont.new
|
|
400
|
-
otf_tables.each do |tag, data|
|
|
401
|
-
temp_otf.tables[tag] = data
|
|
402
|
-
end
|
|
403
|
-
|
|
404
|
-
# Then convert OTF to Type 1
|
|
405
|
-
convert_otf_to_type1(temp_otf)
|
|
447
|
+
# @param options [Hash] Conversion options
|
|
448
|
+
# @return [Hash] PFB output with the :pfb key
|
|
449
|
+
def convert_ttf_to_type1(font, _options = {})
|
|
450
|
+
{ pfb: Type1::PFBGenerator.generate(font) }
|
|
406
451
|
end
|
|
407
452
|
|
|
408
453
|
# Build CFF font dictionary from Type 1 font
|
|
@@ -489,8 +534,8 @@ module Fontisan
|
|
|
489
534
|
return {} unless private_dict
|
|
490
535
|
|
|
491
536
|
{
|
|
492
|
-
nominal_width: private_dict.
|
|
493
|
-
default_width: private_dict.
|
|
537
|
+
nominal_width: private_dict.nominal_width_x,
|
|
538
|
+
default_width: private_dict.default_width_x,
|
|
494
539
|
blue_values: private_dict.blue_values || [],
|
|
495
540
|
other_blues: private_dict.other_blues || [],
|
|
496
541
|
family_blues: private_dict.family_blues || [],
|
|
@@ -502,32 +547,95 @@ module Fontisan
|
|
|
502
547
|
std_vw: private_dict.std_vw || 0,
|
|
503
548
|
stem_snap_h: private_dict.stem_snap_h || [],
|
|
504
549
|
stem_snap_v: private_dict.stem_snap_v || [],
|
|
505
|
-
force_bold: private_dict.force_bold || false,
|
|
550
|
+
force_bold: private_dict.force_bold? || false,
|
|
506
551
|
language_group: private_dict.language_group || 0,
|
|
507
552
|
expansion_factor: private_dict.expansion_factor || 0.06,
|
|
508
553
|
initial_random_seed: private_dict.initial_random_seed || 0,
|
|
509
554
|
}
|
|
510
555
|
end
|
|
511
556
|
|
|
512
|
-
# Build Type 1 font
|
|
557
|
+
# Build the Type 1 PFB from the converted CharStrings: clear-text font
|
|
558
|
+
# dictionary from the source's tables, Private dictionary from the CFF
|
|
559
|
+
# private dict, and the per-glyph CharStrings assembled by
|
|
560
|
+
# {Type1::PFBWriter} (deterministically encrypted).
|
|
513
561
|
#
|
|
514
562
|
# @param font [OpenTypeFont] Source OpenType font
|
|
515
|
-
# @param charstrings [Hash] Type 1
|
|
516
|
-
#
|
|
517
|
-
# @
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
563
|
+
# @param charstrings [Hash<String, String>] glyph name => plain Type 1
|
|
564
|
+
# CharString bytes
|
|
565
|
+
# @param cff_table [Tables::Cff] CFF table for the private dict
|
|
566
|
+
# @return [Hash] PFB output with the :pfb key
|
|
567
|
+
def build_type1_data(font, charstrings, cff_table)
|
|
568
|
+
Type1::PFBWriter.new(
|
|
569
|
+
psname: psname_of(font),
|
|
570
|
+
version: version_of(font),
|
|
571
|
+
fontinfo: fontinfo_of(font),
|
|
572
|
+
bbox: bbox_of(font),
|
|
573
|
+
private_entries: private_entries_from(cff_table),
|
|
574
|
+
charstrings: charstrings,
|
|
575
|
+
).then { |writer| { pfb: writer.write } }
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
def psname_of(font)
|
|
579
|
+
font.post_script_name || "Untitled"
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
def version_of(font)
|
|
583
|
+
name_table = font.table(Constants::NAME_TAG)
|
|
584
|
+
return "1.0" unless name_table
|
|
585
|
+
|
|
586
|
+
name_table.english_name(Tables::Name::VERSION) || "1.0"
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
def fontinfo_of(font)
|
|
590
|
+
info = { version: version_of(font) }
|
|
591
|
+
name_table = font.table(Constants::NAME_TAG)
|
|
592
|
+
if name_table
|
|
593
|
+
full = name_table.english_name(Tables::Name::FULL_NAME)
|
|
594
|
+
family = name_table.english_name(Tables::Name::FAMILY)
|
|
595
|
+
info[:FullName] = full || psname_of(font)
|
|
596
|
+
info[:FamilyName] = family || psname_of(font)
|
|
597
|
+
copyright = name_table.english_name(Tables::Name::COPYRIGHT)
|
|
598
|
+
info[:Copyright] = copyright if copyright
|
|
599
|
+
end
|
|
600
|
+
post = font.table(Constants::POST_TAG)
|
|
601
|
+
info[:ItalicAngle] = post&.italic_angle.to_f
|
|
602
|
+
info[:isFixedPitch] = (post&.is_fixed_pitch || 0) != 0
|
|
603
|
+
os2 = font.table(Constants::OS2_TAG)
|
|
604
|
+
info[:Weight] = if os2.respond_to?(:weight_class) && os2.weight_class
|
|
605
|
+
PFBGenerator.const_get(:WEIGHT_NAMES)[(os2.weight_class / 100).clamp(1, 9)] || "Regular"
|
|
606
|
+
else
|
|
607
|
+
"Regular"
|
|
608
|
+
end
|
|
609
|
+
info
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
# The source's FontBBox in the same units as the CharStrings (scaled to
|
|
613
|
+
# 1000 upm when the conversion rescaled).
|
|
614
|
+
def bbox_of(font)
|
|
615
|
+
head = font.table(Constants::HEAD_TAG)
|
|
616
|
+
return [0, 0, 0, 0] unless head
|
|
617
|
+
|
|
618
|
+
upm = head.units_per_em.to_i
|
|
619
|
+
scale = upm.positive? ? 1000.0 / upm : 1.0
|
|
620
|
+
[head.x_min || 0, head.y_min || 0, head.x_max || 1000, head.y_max || 1000]
|
|
621
|
+
.map { |v| (v * scale).round }
|
|
622
|
+
end
|
|
623
|
+
|
|
624
|
+
# The Private dictionary entries carried over from the CFF.
|
|
625
|
+
def private_entries_from(cff_table)
|
|
626
|
+
entries = {
|
|
627
|
+
"BlueValues" => [0, 0],
|
|
628
|
+
"password" => 5839,
|
|
629
|
+
"MinFeature" => "{16 16}",
|
|
630
|
+
}
|
|
631
|
+
private_dict = cff_table.private_dict(0)
|
|
632
|
+
if private_dict
|
|
633
|
+
entries["BlueValues"] = private_dict.blue_values if private_dict.blue_values
|
|
634
|
+
entries["StdHW"] = private_dict.std_hw if private_dict.std_hw
|
|
635
|
+
entries["StdVW"] = private_dict.std_vw if private_dict.std_vw
|
|
636
|
+
entries["ForceBold"] = private_dict.force_bold? if private_dict.respond_to?(:force_bold?)
|
|
637
|
+
end
|
|
638
|
+
entries
|
|
531
639
|
end
|
|
532
640
|
|
|
533
641
|
# Build head table from Type 1 font
|
|
@@ -569,11 +677,11 @@ module Fontisan
|
|
|
569
677
|
# Units Per Em (uint16) - Type 1 standard is 1000
|
|
570
678
|
data << [1000].pack("n")
|
|
571
679
|
|
|
572
|
-
# Created (LONGDATETIME) -
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
data << [
|
|
680
|
+
# Created/Modified (LONGDATETIME) - fixed date: a Type 1 source has no
|
|
681
|
+
# head table to carry dates from, and conversions are reproducible,
|
|
682
|
+
# so the output never varies run to run (2020-01-01T00:00:00Z).
|
|
683
|
+
data << [FIXED_HEAD_TIMESTAMP].pack("Q>")
|
|
684
|
+
data << [FIXED_HEAD_TIMESTAMP].pack("Q>")
|
|
577
685
|
|
|
578
686
|
# Bounding box (int16 each)
|
|
579
687
|
data << [font_bbox[0]].pack("s>") # x_min
|
|
@@ -627,7 +735,9 @@ module Fontisan
|
|
|
627
735
|
data << [ascent].pack("s>")
|
|
628
736
|
|
|
629
737
|
# Descent (int16) - Distance from baseline to lowest descender (negative)
|
|
630
|
-
# Use BlueValues[0] or [1] if available, otherwise font_bbox[1]
|
|
738
|
+
# Use BlueValues[0] or [1] if available, otherwise font_bbox[1]; a
|
|
739
|
+
# font without descenders still gets a negative descent (the -200
|
|
740
|
+
# no-descender convention), which the production profile requires.
|
|
631
741
|
descent = if blue_values.length >= 2
|
|
632
742
|
blue_values[0] # Bottom zone bottom (negative)
|
|
633
743
|
elsif blue_values.length >= 1
|
|
@@ -635,6 +745,7 @@ module Fontisan
|
|
|
635
745
|
else
|
|
636
746
|
font_bbox[1] # y_min (should be negative)
|
|
637
747
|
end
|
|
748
|
+
descent = [descent, -1].min
|
|
638
749
|
data << [descent].pack("s>")
|
|
639
750
|
|
|
640
751
|
# Line Gap (int16) - Additional space between lines
|
|
@@ -928,12 +1039,15 @@ module Fontisan
|
|
|
928
1039
|
end
|
|
929
1040
|
data << [typo_ascender].pack("s>")
|
|
930
1041
|
|
|
931
|
-
# sTypoDescender (int16) - Use BlueValues or font bbox (negative)
|
|
1042
|
+
# sTypoDescender (int16) - Use BlueValues or font bbox (negative);
|
|
1043
|
+
# a font without descenders gets the -200 no-descender convention,
|
|
1044
|
+
# which the production profile requires.
|
|
932
1045
|
typo_descender = if blue_values.length >= 2
|
|
933
1046
|
blue_values[0]
|
|
934
1047
|
else
|
|
935
1048
|
font_bbox[1]
|
|
936
1049
|
end
|
|
1050
|
+
typo_descender = [typo_descender, -1].min
|
|
937
1051
|
data << [typo_descender].pack("s>")
|
|
938
1052
|
|
|
939
1053
|
# sTypoLineGap (int16)
|
|
Binary file
|
data/lib/fontisan/ffi.rb
CHANGED
|
@@ -37,8 +37,9 @@ module Fontisan
|
|
|
37
37
|
LIBRARY = "fontisan_cdylib"
|
|
38
38
|
OPERATIONS = %i[read audit audit_dir scan convert extract instance subset
|
|
39
39
|
glyph_ids outline_hashes].freeze
|
|
40
|
-
# Type 1 is served through the op surfaces (#192 gap 4) — but scan
|
|
41
|
-
# Type 1
|
|
40
|
+
# Type 1 is served through the op surfaces (#192 gap 4) — but scan and
|
|
41
|
+
# convert keep Type 1 with Ruby (the goldens record Ruby's rows; Ruby's
|
|
42
|
+
# converters produce the Type 1 conversions).
|
|
42
43
|
RUBY_ONLY_FORMATS = %i[pfa pfb].freeze
|
|
43
44
|
|
|
44
45
|
OP_CONVERT = 1
|
|
@@ -82,14 +83,14 @@ module Fontisan
|
|
|
82
83
|
|
|
83
84
|
# A file the detector cannot name stays with Ruby on every operation
|
|
84
85
|
# (the regular-file check comes first: detection reads bytes, and
|
|
85
|
-
# reading a FIFO would block). scan
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
86
|
+
# reading a FIFO would block). scan and convert keep Type 1 with
|
|
87
|
+
# Ruby — the scan goldens record Ruby's rows, and Ruby's converters
|
|
88
|
+
# produce the Type 1 conversions; the read-side ops serve the
|
|
89
|
+
# measured surfaces (#192 gap 4).
|
|
89
90
|
if path.is_a?(String)
|
|
90
91
|
return false if File.file?(path) && undetectable?(path)
|
|
91
92
|
|
|
92
|
-
return false if operation
|
|
93
|
+
return false if %i[scan convert].include?(operation) && File.file?(path) && type1_file?(path)
|
|
93
94
|
end
|
|
94
95
|
|
|
95
96
|
case operation
|
data/lib/fontisan/native.rb
CHANGED
|
@@ -71,12 +71,13 @@ module Fontisan
|
|
|
71
71
|
return false unless path.is_a?(String)
|
|
72
72
|
|
|
73
73
|
# The regular-file check comes first: detection reads bytes, and
|
|
74
|
-
# reading a FIFO would block. scan
|
|
75
|
-
#
|
|
76
|
-
#
|
|
77
|
-
# decline through
|
|
74
|
+
# reading a FIFO would block. scan and convert keep Type 1 with Ruby
|
|
75
|
+
# (the scan goldens record Ruby's rows; Ruby's converters produce the
|
|
76
|
+
# Type 1 conversions). audit_dir serves the walk and hands the Type 1
|
|
77
|
+
# rows back (audit_with_ruby). Undetectable bytes decline through
|
|
78
|
+
# native_supports_file.
|
|
78
79
|
regular = File.file?(path)
|
|
79
|
-
return false if operation
|
|
80
|
+
return false if %i[scan convert].include?(operation) && regular && path_is_type1?(path)
|
|
80
81
|
|
|
81
82
|
case operation
|
|
82
83
|
when :audit_dir then File.directory?(path)
|
|
@@ -62,6 +62,8 @@ module Fontisan
|
|
|
62
62
|
write_woff2(tables)
|
|
63
63
|
when :svg
|
|
64
64
|
write_svg(tables)
|
|
65
|
+
when :type1
|
|
66
|
+
write_type1(tables)
|
|
65
67
|
else
|
|
66
68
|
raise ArgumentError, "Unsupported output format: #{@format}"
|
|
67
69
|
end
|
|
@@ -83,6 +85,20 @@ module Fontisan
|
|
|
83
85
|
File.write(@output_path, svg_xml)
|
|
84
86
|
end
|
|
85
87
|
|
|
88
|
+
# Write Type 1 format
|
|
89
|
+
#
|
|
90
|
+
# @param result [Hash] Result with the :pfb bytes
|
|
91
|
+
# @return [Integer] Number of bytes written
|
|
92
|
+
def write_type1(result)
|
|
93
|
+
data = result[:pfb] || result["pfb"]
|
|
94
|
+
unless data
|
|
95
|
+
raise ArgumentError,
|
|
96
|
+
"Type 1 result must contain the :pfb bytes"
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
File.binwrite(@output_path, data)
|
|
100
|
+
end
|
|
101
|
+
|
|
86
102
|
# Write SFNT format (TTF or OTF)
|
|
87
103
|
#
|
|
88
104
|
# @param tables [Hash<String, String>] Font tables
|