Class: Fontisan::Converters::TableCopier

Inherits:
Object
  • Object
show all
Includes:
ConversionStrategy
Defined in:
lib/fontisan/converters/table_copier.rb

Overview

Strategy for same-format font operations (copy/optimize)

[‘TableCopier`](lib/fontisan/converters/table_copier.rb) handles conversions where the source and target formats are the same. This is useful for:

  • Creating a clean copy of a font

  • Re-ordering tables

  • Removing corruption

  • Normalizing structure

The strategy simply copies all tables from the source font and reassembles them with proper checksums and offsets.

Examples:

Using TableCopier

copier = Fontisan::Converters::TableCopier.new
tables = copier.convert(font)
binary = FontWriter.write_font(tables)

Instance Method Summary collapse

Methods included from ConversionStrategy

#supports?

Instance Method Details

#convert(font, _options = {}) ⇒ Hash<String, String>

Convert font by copying all tables

Parameters:

Returns:

  • (Hash<String, String>)

    Map of table tags to binary data

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fontisan/converters/table_copier.rb', line 32

def convert(font, _options = {})
  raise ArgumentError, "Font cannot be nil" if font.nil?

  unless font.respond_to?(:tables)
    raise ArgumentError, "Font must respond to :tables"
  end

  unless font.respond_to?(:table_data)
    raise ArgumentError, "Font must respond to :table_data"
  end

  target_format = detect_format(font)
  validate(font, target_format)

  tables = {}

  # Copy all tables from source font
  font.table_data.each do |tag, data|
    tables[tag] = data if data
  end

  tables
end

#supported_conversionsArray<Array<Symbol>>

Get supported conversions

Supports same-format conversions for TTF and OTF

Returns:

  • (Array<Array<Symbol>>)

    Supported conversion pairs



61
62
63
64
65
66
# File 'lib/fontisan/converters/table_copier.rb', line 61

def supported_conversions
  [
    %i[ttf ttf],
    %i[otf otf],
  ]
end

#validate(font, target_format) ⇒ Boolean

Validate font for copying

Parameters:

  • font (TrueTypeFont, OpenTypeFont)

    Font to validate

  • target_format (Symbol)

    Target format (same as source for copier)

Returns:

  • (Boolean)

    True if valid

Raises:

  • (ArgumentError)

    If font is invalid

  • (Error)

    If formats don’t match



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fontisan/converters/table_copier.rb', line 75

def validate(font, target_format)
  raise ArgumentError, "Font cannot be nil" if font.nil?

  unless font.respond_to?(:tables)
    raise ArgumentError, "Font must respond to :tables"
  end

  unless font.respond_to?(:table_data)
    raise ArgumentError, "Font must respond to :table_data"
  end

  # Detect source format and verify it matches target
  source_format = detect_format(font)
  unless source_format == target_format
    raise Fontisan::Error,
          "TableCopier requires source and target formats to match. " \
          "Got source: #{source_format}, target: #{target_format}"
  end

  true
end