Class: Fontisan::Collection::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/collection/writer.rb

Overview

CollectionWriter writes binary TTC/OTC files

Single responsibility: Write complete TTC/OTC binary structure to disk including header, offset table, font directories, and table data. Handles checksums and proper binary formatting.

Examples:

Write collection

writer = CollectionWriter.new(fonts, sharing_map, offsets)
writer.write_to_file("output.ttc")

Constant Summary collapse

TTC_TAG =

TTC signature

"ttcf"
VERSION_1_0_MAJOR =

TTC version 1.0 (major=1, minor=0)

1
VERSION_1_0_MINOR =
0

Instance Method Summary collapse

Constructor Details

#initialize(fonts, sharing_map, offsets, format: :ttc) ⇒ Writer

Initialize writer

Parameters:

  • fonts (Array<TrueTypeFont, OpenTypeFont>)

    Source fonts

  • sharing_map (Hash)

    Sharing map from TableDeduplicator

  • offsets (Hash)

    Offset map from OffsetCalculator

  • format (Symbol) (defaults to: :ttc)

    Format type (:ttc or :otc)

Raises:

  • (ArgumentError)

    if parameters are invalid



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fontisan/collection/writer.rb', line 32

def initialize(fonts, sharing_map, offsets, format: :ttc)
  if fonts.nil? || fonts.empty?
    raise ArgumentError,
          "fonts cannot be nil or empty"
  end
  raise ArgumentError, "sharing_map cannot be nil" if sharing_map.nil?
  raise ArgumentError, "offsets cannot be nil" if offsets.nil?
  raise ArgumentError, "format must be :ttc or :otc" unless %i[ttc
                                                               otc].include?(format)

  @fonts = fonts
  @sharing_map = sharing_map
  @offsets = offsets
  @format = format
end

Instance Method Details

#write_collectionString

Write collection to binary string

Returns:

  • (String)

    Complete collection binary



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fontisan/collection/writer.rb', line 61

def write_collection
  binary = String.new(encoding: Encoding::BINARY)

  # Write TTC header
  binary << write_ttc_header

  # Write offset table (offsets to each font's directory)
  binary << write_offset_table

  # Write each font's table directory
  @fonts.each_with_index do |font, font_index|
    # Pad to expected offset
    pad_to_offset(binary, @offsets[:font_directory_offsets][font_index])

    # Write font directory
    binary << write_font_directory(font, font_index)
  end

  # Write table data (shared tables first, then unique tables)
  write_table_data(binary)

  binary
end

#write_to_file(path) ⇒ Integer

Write collection to file

Parameters:

  • path (String)

    Output file path

Returns:

  • (Integer)

    Number of bytes written



52
53
54
55
56
# File 'lib/fontisan/collection/writer.rb', line 52

def write_to_file(path)
  binary = write_collection
  File.binwrite(path, binary)
  binary.bytesize
end