Class: Fontisan::Export::TableSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/export/table_serializer.rb

Overview

TableSerializer handles serialization of individual font tables

Uses strategy pattern to serialize different table types:

  • Fully parsed tables: Use Lutaml::Model serialization

  • Binary tables: Encode as hex or base64

  • Special tables: Custom serialization logic

Examples:

Serializing a parsed table

serializer = TableSerializer.new(binary_format: :hex)
data = serializer.serialize(head_table, "head")

Serializing binary table

data = serializer.serialize_binary(raw_data, "DSIG")

Constant Summary collapse

FULLY_PARSED_TABLES =

Tables that have full Lutaml::Model parsing support

%w[
  head hhea maxp post OS/2 name
  fvar HVAR VVAR MVAR cvar gvar
].freeze
BINARY_ONLY_TABLES =

Tables that should be stored as binary

%w[
  cvt fpgm prep gasp DSIG GDEF GPOS GSUB
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(binary_format: :hex) ⇒ TableSerializer

Initialize table serializer

Parameters:

  • binary_format (Symbol) (defaults to: :hex)

    Format for binary data (:hex or :base64)



36
37
38
39
# File 'lib/fontisan/export/table_serializer.rb', line 36

def initialize(binary_format: :hex)
  @binary_format = binary_format
  validate_binary_format!
end

Instance Method Details

#serialize(table, tag) ⇒ Hash

Serialize a table to exportable format

Parameters:

  • table (Object)

    The table object

  • tag (String)

    The table tag

Returns:

  • (Hash)

    Serialized table data



46
47
48
49
50
51
52
53
54
# File 'lib/fontisan/export/table_serializer.rb', line 46

def serialize(table, tag)
  if fully_parsed?(tag)
    serialize_parsed(table, tag)
  elsif binary_only?(tag)
    serialize_binary(table.to_binary_s, tag)
  else
    serialize_mixed(table, tag)
  end
end

#serialize_binary(data, tag) ⇒ Hash

Serialize a binary-only table

Parameters:

  • data (String)

    Binary data

  • tag (String)

    The table tag

Returns:

  • (Hash)

    Serialized data with binary content



76
77
78
79
80
81
82
83
84
# File 'lib/fontisan/export/table_serializer.rb', line 76

def serialize_binary(data, tag)
  encoded = encode_binary(data)
  {
    tag: tag,
    parsed: false,
    data: encoded,
    fields: nil,
  }
end

#serialize_mixed(table, tag) ⇒ Hash

Serialize tables with mixed content (summary + binary)

Parameters:

  • table (Object)

    The table object

  • tag (String)

    The table tag

Returns:

  • (Hash)

    Serialized data with both fields and binary



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fontisan/export/table_serializer.rb', line 91

def serialize_mixed(table, tag)
  summary = create_summary(table, tag)
  binary = table.respond_to?(:to_binary_s) ? table.to_binary_s : ""

  {
    tag: tag,
    parsed: true,
    fields: summary.to_json,
    data: encode_binary(binary),
  }
end

#serialize_parsed(table, tag) ⇒ Hash

Serialize a parsed table

Parameters:

  • table (Object)

    The table object with Lutaml::Model

  • tag (String)

    The table tag

Returns:

  • (Hash)

    Serialized data with parsed flag



61
62
63
64
65
66
67
68
69
# File 'lib/fontisan/export/table_serializer.rb', line 61

def serialize_parsed(table, tag)
  fields = extract_fields(table)
  {
    tag: tag,
    parsed: true,
    fields: fields.to_json,
    data: nil,
  }
end