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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/docs/guide/engines.md +1 -1
  3. data/lib/fontisan/byte_ops.rb +14 -0
  4. data/lib/fontisan/config/conversion_matrix.yml +14 -12
  5. data/lib/fontisan/converters/cff_table_builder.rb +26 -46
  6. data/lib/fontisan/converters/type1_converter.rb +177 -63
  7. data/lib/fontisan/ffi/windows-x86_64/fontisan_cdylib.dll +0 -0
  8. data/lib/fontisan/ffi.rb +8 -7
  9. data/lib/fontisan/native.rb +6 -5
  10. data/lib/fontisan/pipeline/output_writer.rb +16 -0
  11. data/lib/fontisan/pipeline/transformation_pipeline.rb +30 -3
  12. data/lib/fontisan/tables/cff/charstring_parser.rb +8 -0
  13. data/lib/fontisan/type1/cff_to_type1_converter.rb +33 -15
  14. data/lib/fontisan/type1/charstrings.rb +2 -1
  15. data/lib/fontisan/type1/decryptor.rb +6 -6
  16. data/lib/fontisan/type1/pfb_generator.rb +90 -241
  17. data/lib/fontisan/type1/pfb_writer.rb +153 -0
  18. data/lib/fontisan/type1/ttf_to_type1_converter.rb +7 -1
  19. data/lib/fontisan/type1.rb +1 -0
  20. data/lib/fontisan/version.rb +1 -1
  21. data/scripts/corpus/generate_convert_goldens.rb +46 -6
  22. data/scripts/validate_t1_surfaces.rb +14 -4
  23. data/scripts/verify_release.rb +10 -2
  24. metadata +2 -17
  25. data/TODO.improvements/01-cbdt-cblc-gid-stable-propagation.md +0 -73
  26. data/TODO.improvements/02-collection-outline-priority.md +0 -48
  27. data/TODO.improvements/03-fontisan-audit-command.md +0 -219
  28. data/TODO.improvements/04-ufo-composite-glyph-encoding.md +0 -46
  29. data/TODO.improvements/05-otf-compiler-real-cff.md +0 -63
  30. data/TODO.improvements/06-cff2-blend-vsindex-operators.md +0 -51
  31. data/TODO.improvements/07-cpal-v1-header-fields.md +0 -71
  32. data/TODO.improvements/08-cff-standard-string-table.md +0 -52
  33. data/TODO.improvements/09-type1-seac-expansion.md +0 -57
  34. data/TODO.improvements/10-ufo-image-set-feature-writers.md +0 -71
  35. data/TODO.improvements/11-kern-groups-plist.md +0 -49
  36. data/TODO.improvements/12-cbdt-fixture-bindata-conversion.md +0 -68
  37. data/TODO.improvements/13-split-octokit-fetcher.md +0 -45
  38. data/TODO.improvements/14-rubocop-baseline-chip.md +0 -62
  39. data/TODO.improvements/15-cff-cff2-subsetter-strategy.md +0 -78
  40. data/TODO.improvements/README.md +0 -58
@@ -80,6 +80,11 @@ module Fontisan
80
80
  font = load_font(detection)
81
81
  log "Loaded: #{font.class.name}"
82
82
 
83
+ # A Type 1 source has no table phase: the conversion strategy
84
+ # consumes the font object itself (the parsed dictionaries and
85
+ # CharStrings), so go straight from load to strategy to write.
86
+ return transform_type1_source(font, detection) if font.is_a?(Type1Font)
87
+
83
88
  # Phase 3: Resolve variation
84
89
  tables = resolve_variation(font, detection)
85
90
  log "Resolved variation using #{@variation_strategy} strategy"
@@ -107,6 +112,27 @@ module Fontisan
107
112
 
108
113
  # Methods below were private; made public for testability (TODO 30)
109
114
 
115
+ # Convert a Type 1 source: run the conversion strategy on the loaded
116
+ # font, then write (no variation resolution, no table copying).
117
+ #
118
+ # @param font [Type1Font] Loaded source
119
+ # @param detection [Hash] Detection results
120
+ # @return [Hash] Transformation result with :success, :output_path, :details
121
+ def transform_type1_source(font, detection)
122
+ @variation_strategy = :preserve
123
+ tables = Converters::FormatConverter.new.convert(font, target_format, @options)
124
+ write_output(tables, detection)
125
+ validate_output if @options[:validate] && !same_format_conversion? && !export_only_format?
126
+
127
+ {
128
+ success: true,
129
+ output_path: @output_path,
130
+ details: build_details(detection),
131
+ }
132
+ rescue StandardError => e
133
+ handle_error(e)
134
+ end
135
+
110
136
  # Detect input format and capabilities
111
137
  #
112
138
  # @return [Hash] Detection results from FormatDetector
@@ -215,8 +241,8 @@ module Fontisan
215
241
  # No conversion needed for same format
216
242
  return tables if source_format == target
217
243
 
218
- # Use FormatConverter for outline conversion
219
- if needs_outline_conversion?(tables, target) || target == :svg
244
+ # Use FormatConverter for outline conversion and Type 1 targets
245
+ if needs_outline_conversion?(tables, target) || target == :svg || target == :type1
220
246
  converter = Converters::FormatConverter.new
221
247
  # Create temporary font object from tables
222
248
  font = build_font_from_tables(tables, source_format)
@@ -284,6 +310,7 @@ module Fontisan
284
310
  when ".otf" then :otf
285
311
  when ".woff" then :woff
286
312
  when ".woff2" then :woff2
313
+ when ".pfb", ".pfa", ".t1" then :type1
287
314
  else
288
315
  raise ArgumentError,
289
316
  "Cannot determine target format from extension: #{ext}"
@@ -400,7 +427,7 @@ module Fontisan
400
427
  #
401
428
  # @return [Boolean] True if format is export-only
402
429
  def export_only_format?
403
- %i[svg woff woff2].include?(target_format)
430
+ %i[svg woff woff2 type1].include?(target_format)
404
431
  end
405
432
  end
406
433
  end
@@ -86,6 +86,7 @@ module Fontisan
86
86
 
87
87
  # Operators that require hint mask bytes
88
88
  HINTMASK_OPERATORS = %i[hintmask cntrmask].freeze
89
+ STEM_OPERATORS = %i[hstem vstem hstemhm vstemhm].freeze
89
90
 
90
91
  # @return [String] Binary CharString data
91
92
  attr_reader :data
@@ -118,6 +119,13 @@ module Fontisan
118
119
  # Operator byte - read operator and create operation
119
120
  operator = read_operator(io, byte)
120
121
 
122
+ # Count stems as they arrive: the hint mask length depends on
123
+ # the number of stems SEEN SO FAR (the CFF spec's running
124
+ # count), not on the Private DICT.
125
+ if STEM_OPERATORS.include?(operator)
126
+ @stem_count += operand_stack.length / 2
127
+ end
128
+
121
129
  # Read hint mask data if needed
122
130
  hint_data = nil
123
131
  if HINTMASK_OPERATORS.include?(operator)
@@ -60,13 +60,24 @@ module Fontisan
60
60
  # Escape code for two-byte operators
61
61
  ESCAPE_BYTE = 12
62
62
 
63
+ # Operators whose operands feed the coordinate walk: an operator of
64
+ # these without operands (malformed CharStrings occur in the wild) is
65
+ # skipped rather than crashing the conversion.
66
+ COORDINATE_OPS = %i[rmoveto hmoveto vmoveto rlineto hlineto vlineto
67
+ rrcurveto hhcurveto hvcurveto vhcurveto rcurveline
68
+ rlinecurve hstem vstem hstemhm vstemhm].freeze
69
+
63
70
  # Initialize a new CffToType1Converter
64
71
  #
65
72
  # @param nominal_width [Integer] Nominal width from CFF Private dict (default: 0)
66
73
  # @param default_width [Integer] Default width from CFF Private dict (default: 0)
67
- def initialize(nominal_width: 0, default_width: 0)
74
+ # @param scale [Float] Multiplier applied to every coordinate operand,
75
+ # for UPM rescaling (1000/upm; 1.0 keeps coordinates as they are)
76
+ def initialize(nominal_width: 0, default_width: 0, scale: 1.0)
68
77
  @nominal_width = nominal_width
69
78
  @default_width = default_width
79
+ @scale = scale
80
+ @rescale = (scale - 1.0).abs > 1e-9
70
81
  end
71
82
 
72
83
  # Convert CFF CharString to Type 1 CharString
@@ -85,6 +96,7 @@ module Fontisan
85
96
  parser = Tables::Cff::CharStringParser.new(cff_charstring,
86
97
  stem_count: private_dict[:stem_count].to_i)
87
98
  operations = parser.parse
99
+ apply_scale!(operations) if @rescale
88
100
 
89
101
  # Extract width from operations (CFF spec: odd stack before first move = width)
90
102
  width = extract_width(operations)
@@ -93,6 +105,14 @@ module Fontisan
93
105
  convert_operations(operations, width)
94
106
  end
95
107
 
108
+ # Multiply every coordinate operand by the scale factor, rounding to
109
+ # whole font units (Type 1 coordinates are integers at 1000 upm).
110
+ def apply_scale!(operations)
111
+ operations.each do |op|
112
+ op[:operands] = op[:operands]&.map { |val| (val * @scale).round }
113
+ end
114
+ end
115
+
96
116
  # Extract width from CFF operations
97
117
  #
98
118
  # In CFF, if there's an odd number of arguments before the first move
@@ -169,6 +189,8 @@ module Fontisan
169
189
  end
170
190
 
171
191
  operations.each do |op|
192
+ next if COORDINATE_OPS.include?(op[:name]) && Array(op[:operands]).empty?
193
+
172
194
  case op[:name]
173
195
  when :hstem, :vstem, :hstemhm, :vstemhm
174
196
  # Hint operators - preserve
@@ -220,6 +242,8 @@ module Fontisan
220
242
  when :rlineto
221
243
  # rlineto dx dy
222
244
  dx, dy = op[:operands]
245
+ next if dy.nil?
246
+
223
247
  x += dx
224
248
  y += dy
225
249
  result << encode_number(dx)
@@ -243,6 +267,8 @@ module Fontisan
243
267
  when :rrcurveto
244
268
  # rrcurveto dx1 dy1 dx2 dy2 dx3 dy3
245
269
  dx1, dy1, dx2, dy2, dx3, dy3 = op[:operands]
270
+ next if dy3.nil?
271
+
246
272
  x += dx1 + dx2 + dx3
247
273
  y += dy1 + dy2 + dy3
248
274
  [dx1, dy1, dx2, dy2, dx3, dy3].each do |val|
@@ -250,12 +276,7 @@ module Fontisan
250
276
  end
251
277
  result << 8 # rrcurveto
252
278
  first_move = false
253
- when :hhcurveto, :hvcurveto, :vhcurveto
254
- # Flexible curve operators
255
- op[:operands].each { |val| result << encode_number(val) }
256
- result << CFF_TO_TYPE1[op[:name]]
257
- first_move = false
258
- when :rcurveline, :rlinecurve
279
+ when :hhcurveto, :hvcurveto, :vhcurveto, :rcurveline, :rlinecurve
259
280
  # Flexible curve operators
260
281
  op[:operands].each { |val| result << encode_number(val) }
261
282
  result << CFF_TO_TYPE1[op[:name]]
@@ -264,15 +285,9 @@ module Fontisan
264
285
  # Control operators
265
286
  result << CFF_TO_TYPE1[op[:name]]
266
287
  first_move = false
267
- when :hintmask, :cntrmask
268
- # Hint mask operators - Type 1 doesn't support these
269
- # Skip them
270
- first_move = false
271
- when :shortint
272
- # Short integer push - handled by operand encoding
273
- first_move = false
274
288
  else
275
- # Unknown operator - skip
289
+ # Hint masks (Type 1 has no equivalent), short integer pushes
290
+ # (operand encoding handled them) and unknown operators - skip
276
291
  first_move = false
277
292
  end
278
293
  end
@@ -294,6 +309,9 @@ module Fontisan
294
309
  # @param num [Integer] Number to encode
295
310
  # @return [String] Encoded bytes
296
311
  def encode_number(num)
312
+ # The parser yields 16.16 fixed values as floats; Type 1 units here
313
+ # are whole font units, so round rather than crash on `>>`.
314
+ num = num.round unless num.is_a?(Integer)
297
315
  if num >= -107 && num <= 107
298
316
  [num + 139].pack("C")
299
317
  else
@@ -220,7 +220,8 @@ module Fontisan
220
220
  text.scan(/\/([^\s]+)\s+(\d+)\s+-\|(.*?)\|-/m) do |match|
221
221
  glyph_name = match[0]
222
222
  _index = match[1].to_i
223
- encrypted_data = match[2]
223
+ # The entry pads the binary with spaces; they are format, not data.
224
+ encrypted_data = match[2].strip
224
225
 
225
226
  @charstrings[glyph_name] = encrypted_data
226
227
  end
@@ -32,6 +32,8 @@ module Fontisan
32
32
  module Decryptor
33
33
  # Default lenIV value (number of random bytes at start of encrypted CharString)
34
34
  DEFAULT_LEN_IV = 4
35
+ # The fixed byte repeated for the lenIV prefix ({DEFAULT_LEN_IV} times).
36
+ LEN_IV_BYTE = "\x00"
35
37
 
36
38
  # eexec encryption key
37
39
  EEXEC_KEY = 55665
@@ -106,7 +108,7 @@ module Fontisan
106
108
  # Encrypt data using CharString cipher
107
109
  #
108
110
  # @param data [String] Plain CharString data
109
- # @param len_iv [Integer] Number of random bytes to prepend
111
+ # @param len_iv [Integer] Number of bytes to prepend
110
112
  # @return [String] Encrypted CharString data
111
113
  def self.charstring_encrypt(data, len_iv: DEFAULT_LEN_IV)
112
114
  return "" if data.nil? || data.empty?
@@ -114,11 +116,9 @@ module Fontisan
114
116
  # Ensure binary encoding before concatenation
115
117
  data = data.b
116
118
 
117
- # Prepend lenIV random bytes
118
- if len_iv.positive?
119
- random_bytes = Array.new(len_iv) { rand(256) }.pack("C*")
120
- data = random_bytes + data
121
- end
119
+ # Prepend lenIV bytes. They are conventionally random and every
120
+ # decryptor skips them; a fixed byte keeps conversions reproducible.
121
+ data = (LEN_IV_BYTE * len_iv) + data if len_iv.positive?
122
122
 
123
123
  encrypt(data, CHARSTRING_KEY)
124
124
  end
@@ -4,14 +4,11 @@ module Fontisan
4
4
  module Type1
5
5
  # PFB (Printer Font Binary) Generator
6
6
  #
7
- # [`PFBGenerator`](lib/fontisan/type1/pfb_generator.rb) generates Type 1 PFB files
8
- # from TrueType fonts.
9
- #
10
- # PFB files are segmented binary files used by Windows for Type 1 fonts.
11
- # They contain:
12
- # - ASCII segment: Font dictionary
13
- # - Binary segment: CharString data
14
- # - ASCII segment: Trailer
7
+ # Generates the canonical Adobe Type 1 PFB from a TrueType font:
8
+ # every glyph is converted through {TTFToType1Converter} (quadratic
9
+ # curves become cubics), the CharStrings are encrypted per glyph, and
10
+ # {PFBWriter} assembles the three-segment file. Output is deterministic:
11
+ # no random bytes, no timestamps, no UUIDs.
15
12
  #
16
13
  # @example Generate PFB from TTF
17
14
  # font = Fontisan::FontLoader.load("font.ttf")
@@ -19,27 +16,14 @@ module Fontisan
19
16
  # File.binwrite("font.pfb", pfb_data)
20
17
  #
21
18
  # @example Generate PFB with custom options
22
- # options = { upm_scale: 1000, format: :pfb }
19
+ # options = { upm_scale: 1000 }
23
20
  # pfb_data = Fontisan::Type1::PFBGenerator.generate(font, options)
24
21
  #
25
22
  # @see https://www.adobe.com/devnet/font/pdfs/5178.Type1.pdf
26
23
  class PFBGenerator
27
- # PFB segment markers
28
- ASCII_SEGMENT = 0x01
29
- BINARY_SEGMENT = 0x02
30
- END_SEGMENT = 0x03
31
-
32
- # Header format string
33
- PFB_HEADER = "%%!PS-AdobeFont-1.0: %s 1.0\n"
34
-
35
- # Generate PFB from TTF font
36
- #
37
- # @param font [Fontisan::Font] Source TTF font
38
- # @param options [Hash] Generation options
39
24
  # @option options [Integer, :native] :upm_scale Target UPM (default: 1000)
40
25
  # @option options [Class] :encoding Encoding class (default: Encodings::AdobeStandard)
41
26
  # @option options [Boolean] :convert_curves Convert quadratic to cubic (default: true)
42
- # @return [String] PFB file content (binary)
43
27
  def self.generate(font, options = {})
44
28
  new(font, options).generate
45
29
  end
@@ -49,7 +33,6 @@ module Fontisan
49
33
  @options = options
50
34
  @metrics = MetricsCalculator.new(font)
51
35
 
52
- # Set up scaler
53
36
  upm_scale = options[:upm_scale] || 1000
54
37
  @scaler = if upm_scale == :native
55
38
  UPMScaler.native(font)
@@ -57,10 +40,7 @@ module Fontisan
57
40
  UPMScaler.new(font, target_upm: upm_scale)
58
41
  end
59
42
 
60
- # Set up encoding
61
43
  @encoding = options[:encoding] || Encodings::AdobeStandard
62
-
63
- # Set up converter options
64
44
  @convert_curves = options.fetch(:convert_curves, true)
65
45
  end
66
46
 
@@ -68,253 +48,122 @@ module Fontisan
68
48
  #
69
49
  # @return [String] PFB binary content
70
50
  def generate
71
- # Build PFB segments
72
- ascii_segment1 = build_ascii_segment_1
73
- binary_segment = build_binary_segment
74
- ascii_segment2 = build_ascii_segment_2
75
-
76
- # Combine with segment headers
77
- [
78
- segment_header(ASCII_SEGMENT, ascii_segment1.bytesize),
79
- ascii_segment1,
80
- segment_header(BINARY_SEGMENT, binary_segment.bytesize),
81
- binary_segment,
82
- segment_header(ASCII_SEGMENT, ascii_segment2.bytesize),
83
- ascii_segment2,
84
- [END_SEGMENT, 0, 0, 0, 0, 0].pack("CV"),
85
- ].join
51
+ PFBWriter.new(
52
+ psname: psname,
53
+ version: version,
54
+ fontinfo: fontinfo,
55
+ bbox: scaled_bbox,
56
+ private_entries: private_entries,
57
+ charstrings: named_charstrings,
58
+ ).write
86
59
  end
87
60
 
61
+ WEIGHT_NAMES = {
62
+ 1 => "Thin", 2 => "ExtraLight", 3 => "Light", 4 => "Regular",
63
+ 5 => "Medium", 6 => "SemiBold", 7 => "Bold", 8 => "ExtraBold", 9 => "Black"
64
+ }.freeze
65
+
88
66
  private
89
67
 
90
- # Build first ASCII segment (font dictionary)
91
- #
92
- # @return [String] ASCII font dictionary
93
- def build_ascii_segment_1
94
- lines = []
95
- lines << format(PFB_HEADER, @font.post_script_name)
96
- lines << build_font_dict
97
- lines << build_private_dict
98
- lines << build_charstrings_dict
99
- lines.join("\n")
68
+ def psname
69
+ @font.post_script_name || "Untitled"
100
70
  end
101
71
 
102
- # Build font dictionary
103
- #
104
- # @return [String] Font dictionary in PostScript
105
- def build_font_dict
106
- dict = []
107
- dict << "10 dict begin"
108
- dict << "/FontType 1 def"
109
- dict << "/FontMatrix [0.001 0 0 0.001 0 0] def"
72
+ def version
73
+ name_table = @font.table(Constants::NAME_TAG)
74
+ return "1.0" unless name_table
75
+
76
+ name_table.english_name(Tables::Name::VERSION) || "1.0"
77
+ end
110
78
 
111
- # Font info
79
+ def fontinfo
80
+ info = { version: version, FullName: full_name, FamilyName: family_name,
81
+ Weight: weight, ItalicAngle: italic_angle, isFixedPitch: fixed_pitch? }
112
82
  name_table = @font.table(Constants::NAME_TAG)
113
83
  if name_table
114
- font_name = name_table.english_name(Tables::Name::POSTSCRIPT_NAME) || @font.post_script_name
115
- dict << "/FontName /#{font_name} def"
116
- end
117
-
118
- # Bounding box
119
- head = @font.table(Constants::HEAD_TAG)
120
- if head
121
- bbox = [
122
- @scaler.scale(head.x_min || 0),
123
- @scaler.scale(head.y_min || 0),
124
- @scaler.scale(head.x_max || 1000),
125
- @scaler.scale(head.y_max || 1000),
126
- ]
127
- dict << "/FontBBox {#{bbox.join(' ')}} def"
84
+ copyright = name_table.english_name(Tables::Name::COPYRIGHT)
85
+ info[:Copyright] = copyright if copyright
128
86
  end
129
-
130
- # Paint type
131
- dict << "/PaintType 0 def"
132
-
133
- # Encoding
134
- if @encoding == Encodings::AdobeStandard
135
- dict << "/Encoding StandardEncoding def"
136
- elsif @encoding == Encodings::ISOLatin1
137
- dict << "/Encoding ISOLatin1Encoding def"
87
+ if post_table&.underline_position
88
+ info[:UnderlinePosition] = @scaler.scale(post_table.underline_position)
89
+ info[:UnderlineThickness] = @scaler.scale(post_table.underline_thickness)
138
90
  end
139
-
140
- dict << "currentdict end"
141
- dict << "dup /FontName get exch definefont pop"
142
-
143
- dict.join("\n")
91
+ info
144
92
  end
145
93
 
146
- # Build Private dictionary
147
- #
148
- # @return [String] Private dictionary in PostScript
149
- def build_private_dict
150
- dict = []
151
- dict << "/Private 15 dict begin"
152
-
153
- # Blue values (for hinting)
154
- # These are typically derived from the font's alignment zones
155
- dict << "/BlueValues [-20 0 500 510] def"
156
- dict << "/BlueScale 0.039625 def"
157
- dict << "/BlueShift 7 def"
158
- dict << "/BlueFuzz 1 def"
159
-
160
- # Stem snap hints
161
- os2 = @font.table(Constants::OS2_TAG)
162
- if os2&.us_weight_class
163
- stem_width = @scaler.scale([100, 80,
164
- 90][os2.us_weight_class / 100] || 80)
165
- dict << "/StemSnapH [#{stem_width}] def"
166
- dict << "/StemSnapV [#{stem_width}] def"
167
- end
168
-
169
- # Force bold flag
170
- dict << if os2&.us_weight_class && os2.us_weight_class >= 700
171
- "/ForceBold true def"
172
- else
173
- "/ForceBold false def"
174
- end
175
-
176
- # Language group
177
- dict << "/LanguageGroup 0 def"
178
-
179
- # Unique ID (random)
180
- dict << "/UniqueID #{rand(1000000..9999999)} def"
94
+ def post_table = @font.table(Constants::POST_TAG)
181
95
 
182
- dict << "currentdict end"
183
- dict << "dup /Private get"
184
-
185
- dict.join("\n")
186
- end
96
+ def full_name
97
+ name_table = @font.table(Constants::NAME_TAG)
98
+ return psname unless name_table
187
99
 
188
- # Build CharStrings dictionary
189
- #
190
- # @return [String] CharStrings dictionary reference
191
- def build_charstrings_dict
192
- # This is a placeholder - actual CharStrings are in the binary segment
193
- "/CharStrings #{@charstrings&.size || 0} dict dup begin\nend"
100
+ name_table.english_name(Tables::Name::FULL_NAME) || psname
194
101
  end
195
102
 
196
- # Build CharStrings dictionary text for eexec encryption
197
- #
198
- # @param charstrings [Hash] Glyph ID to CharString mapping
199
- # @return [String] CharStrings dictionary text
200
- def build_charstrings_dict_text(charstrings)
201
- lines = []
202
- lines << "dup /FontName get exch definefont pop"
203
- lines << "begin"
204
- lines << "/CharStrings #{charstrings.size} dict dup begin"
205
-
206
- charstrings.each do |gid, charstring|
207
- # Convert charstring bytes to hex representation for PostScript
208
- hex_data = charstring.unpack1("H*")
209
- lines << "#{gid} #{charstring.bytesize} #{hex_data} RD"
210
- end
103
+ def family_name
104
+ name_table = @font.table(Constants::NAME_TAG)
105
+ return psname unless name_table
211
106
 
212
- lines << "end"
213
- lines << "end"
214
- lines << "put"
215
- lines.join("\n")
107
+ name_table.english_name(Tables::Name::FAMILY) || psname
216
108
  end
217
109
 
218
- # Build binary segment (CharStrings)
219
- #
220
- # @return [String] Binary CharString data (encrypted with eexec)
221
- def build_binary_segment
222
- # Convert glyphs to Type 1 CharStrings
223
- charstrings = if @convert_curves
224
- TTFToType1Converter.convert(@font, @scaler, @encoding)
225
- else
226
- # For simple curve conversion skip, generate minimal charstrings
227
- generate_simple_charstrings
228
- end
229
-
230
- # Build CharStrings dictionary text
231
- charstrings_dict = build_charstrings_dict_text(charstrings)
110
+ def weight
111
+ os2 = @font.table(Constants::OS2_TAG)
112
+ return "Regular" unless os2.respond_to?(:weight_class) && os2.weight_class
232
113
 
233
- # Encrypt with eexec
234
- Decryptor.eexec_encrypt(charstrings_dict)
114
+ WEIGHT_NAMES[(os2.weight_class / 100).clamp(1, 9)] || "Regular"
235
115
  end
236
116
 
237
- # Generate simple CharStrings (without curve conversion)
238
- #
239
- # @return [Hash<Integer, String>] Glyph ID to CharString mapping
240
- def generate_simple_charstrings
241
- glyf_table = @font.table(Constants::GLYF_TAG)
242
- return {} unless glyf_table
243
-
244
- maxp = @font.table(Constants::MAXP_TAG)
245
- num_glyphs = maxp&.num_glyphs || 0
246
-
247
- charstrings = {}
248
- num_glyphs.times do |gid|
249
- charstrings[gid] = simple_charstring(glyf_table, gid)
250
- end
251
-
252
- charstrings
117
+ def italic_angle
118
+ post_table&.italic_angle.to_f
253
119
  end
254
120
 
255
- # Generate a simple CharString for a glyph
256
- #
257
- # @param glyf_table [Object] TTF glyf table
258
- # @param gid [Integer] Glyph ID
259
- # @return [String] Type 1 CharString data
260
- def simple_charstring(glyf_table, gid)
261
- glyph = glyf_table.glyph(gid)
262
-
263
- # Empty or compound glyph
264
- if glyph.nil? || glyph.num_contours.zero? || glyph.compound?
265
- # Return empty charstring (hsbw + endchar)
266
- return [0, 500, 14].pack("C*")
267
- end
268
-
269
- # For simple glyphs without curve conversion, generate line-based charstring
270
- # This is a simplified implementation
271
- lsb, width = hmtx_metrics_for(gid)
272
- bytes = [0, @scaler.scale(lsb), @scaler.scale(width)] # hsbw
273
-
274
- # Add lines between points (simplified)
275
- points = glyph.points
276
- points.each do |point|
277
- next unless point[:on_curve]
278
-
279
- # This is very simplified - proper implementation would handle curves
280
- end
281
-
282
- bytes << 14 # endchar
283
- bytes.pack("C*")
121
+ def fixed_pitch?
122
+ (post_table&.is_fixed_pitch || 0) != 0
284
123
  end
285
124
 
286
- # Look up LSB and advance width from hmtx for a glyph.
287
- #
288
- # @param gid [Integer] Glyph ID
289
- # @return [Array(Integer, Integer)] [lsb, advance_width]
290
- def hmtx_metrics_for(gid)
291
- hmtx = @font.table(Constants::HMTX_TAG)
292
- return [0, 500] unless hmtx
293
-
294
- metric = hmtx.metric_for(gid)
295
- return [0, 500] unless metric
125
+ def scaled_bbox
126
+ head = @font.table(Constants::HEAD_TAG)
127
+ return [0, 0, 0, 0] unless head
296
128
 
297
- [metric[:lsb] || 0, metric[:advance_width] || 500]
129
+ [
130
+ @scaler.scale(head.x_min || 0),
131
+ @scaler.scale(head.y_min || 0),
132
+ @scaler.scale(head.x_max || 1000),
133
+ @scaler.scale(head.y_max || 1000),
134
+ ]
298
135
  end
299
136
 
300
- # Build second ASCII segment (trailer)
301
- #
302
- # @return [String] ASCII trailer
303
- def build_ascii_segment_2
304
- lines = []
305
- lines << "put" # Put the Private dictionary
306
- lines << "dup /FontName get exch definefont pop"
307
- lines << "% cleartomark"
308
- lines.join("\n")
137
+ # The Private dictionary entries, derived from the source metrics.
138
+ def private_entries
139
+ entries = {
140
+ "BlueValues" => [0, 0],
141
+ "password" => 5839,
142
+ "MinFeature" => "{16 16}",
143
+ }
144
+ os2 = @font.table(Constants::OS2_TAG)
145
+ if os2.respond_to?(:us_weight_class) && os2.us_weight_class
146
+ stem = @scaler.scale([100, 80, 90][os2.us_weight_class / 100] || 80)
147
+ entries["StdHW"] = stem
148
+ entries["StdVW"] = stem
149
+ entries["ForceBold"] = os2.us_weight_class >= 700
150
+ end
151
+ entries
309
152
  end
310
153
 
311
- # Create PFB segment header
312
- #
313
- # @param marker [Integer] Segment type marker
314
- # @param size [Integer] Segment data size
315
- # @return [String] 6-byte segment header
316
- def segment_header(marker, size)
317
- [marker, size].pack("CV")
154
+ # GID-keyed plain CharStrings from the converter, renamed via the post
155
+ # table (falling back to glyphN for unnamed glyphs).
156
+ def named_charstrings
157
+ plain = if @convert_curves
158
+ TTFToType1Converter.convert(@font, @scaler, @encoding)
159
+ else
160
+ {}
161
+ end
162
+ names = post_table&.glyph_names || []
163
+ plain.filter_map do |gid, charstring|
164
+ name = names[gid] || "glyph#{gid}"
165
+ [name, charstring] unless charstring.empty?
166
+ end.to_h
318
167
  end
319
168
  end
320
169
  end