Class: Fontisan::FontWriter
- Inherits:
-
Object
- Object
- Fontisan::FontWriter
- Defined in:
- lib/fontisan/font_writer.rb
Overview
FontWriter handles writing font binaries from table data
This class assembles a complete font binary from individual table data, including:
-
Writing the sfnt header (offset table)
-
Building the table directory
-
Writing table data with proper 4-byte alignment
-
Calculating all checksums
-
Updating the head table’s checksumAdjustment field
Reference: OpenType spec section on font file structure
Constant Summary collapse
- TRUETYPE_TABLE_ORDER =
OpenType/TrueType table ordering (recommended order)
%w[ head hhea maxp OS/2 hmtx LTSH VDMX hdmx cmap fpgm prep cvt loca glyf kern name post gasp PCLT DSIG ].freeze
- OPENTYPE_TABLE_ORDER =
OpenType/CFF table ordering (recommended order)
%w[ head hhea maxp OS/2 name cmap post CFF CFF2 ].freeze
Class Method Summary collapse
-
.detect_sfnt_version(tables_hash) ⇒ Integer
Detect sfnt version based on table presence.
-
.write_font(tables_hash, sfnt_version: nil) ⇒ String
Write complete font binary from table data.
-
.write_to_file(tables_hash, path, sfnt_version: nil) ⇒ Integer
Write font binary to file.
Instance Method Summary collapse
-
#initialize(tables_hash, sfnt_version: 0x00010000) ⇒ FontWriter
constructor
Initialize writer with table data.
-
#write ⇒ String
Write the complete font binary.
Constructor Details
#initialize(tables_hash, sfnt_version: 0x00010000) ⇒ FontWriter
Initialize writer with table data
95 96 97 98 |
# File 'lib/fontisan/font_writer.rb', line 95 def initialize(tables_hash, sfnt_version: 0x00010000) @tables = tables_hash @sfnt_version = sfnt_version end |
Class Method Details
.detect_sfnt_version(tables_hash) ⇒ Integer
Detect sfnt version based on table presence
64 65 66 67 68 69 70 |
# File 'lib/fontisan/font_writer.rb', line 64 def self.detect_sfnt_version(tables_hash) if tables_hash.key?("CFF ") || tables_hash.key?("CFF2") 0x4F54544F # 'OTTO' for OpenType/CFF else 0x00010000 # 1.0 for TrueType end end |
.write_font(tables_hash, sfnt_version: nil) ⇒ String
Write complete font binary from table data
54 55 56 57 58 |
# File 'lib/fontisan/font_writer.rb', line 54 def self.write_font(tables_hash, sfnt_version: nil) # Auto-detect sfnt version if not provided sfnt_version ||= detect_sfnt_version(tables_hash) new(tables_hash, sfnt_version: sfnt_version).write end |
.write_to_file(tables_hash, path, sfnt_version: nil) ⇒ Integer
Write font binary to file
81 82 83 84 85 86 87 88 89 |
# File 'lib/fontisan/font_writer.rb', line 81 def self.write_to_file(tables_hash, path, sfnt_version: nil) binary = write_font(tables_hash, sfnt_version: sfnt_version) # Create parent directories if they don't exist dir = File.dirname(path) FileUtils.mkdir_p(dir) unless Dir.exist?(dir) File.binwrite(path, binary) end |
Instance Method Details
#write ⇒ String
Write the complete font binary
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/fontisan/font_writer.rb', line 103 def write # Order tables according to format = order_tables # Calculate table offsets table_entries = calculate_table_entries() # Build font binary font_data = String.new(encoding: Encoding::BINARY) # Write offset table (sfnt header) font_data << write_offset_table(table_entries.size) # Write table directory (ALL entries first) table_entries.each do |entry| font_data << write_table_entry(entry) end # Write table data (ALL data after directory) table_entries.each do |entry| font_data << entry[:data] font_data << entry[:padding] end # Calculate and update head table checksum adjustment update_checksum_adjustment!(font_data, table_entries) font_data end |