Class: Fontisan::Export::TableSerializer

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

Overview

Serializes individual font tables for export.

Tables fall into three categories:

  • Fully parsed: BinData records whose snapshot hash is exported as JSON fields.
  • Binary-only: opaque blobs encoded as hex or base64.
  • Mixed: tables with a small typed summary alongside the binary payload (glyf, loca, cmap, CFF).

All table objects are BinData::Record subclasses; we trust that interface rather than duck-typing each field.

Defined Under Namespace

Modules: CffSummary, CmapSummary, GlyphSummary, LocaSummary

Constant Summary collapse

FULLY_PARSED_TABLES =

Tables whose BinData fields are exported as JSON.

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

Tables stored as opaque binary with no field extraction.

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

Tables that ship a summary alongside the raw binary.

{
  "glyf" => GlyphSummary,
  "loca" => LocaSummary,
  "cmap" => CmapSummary,
  "CFF" => CffSummary,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(binary_format: :hex) ⇒ TableSerializer

Returns a new instance of TableSerializer.

Parameters:

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

    :hex or :base64



40
41
42
43
# File 'lib/fontisan/export/table_serializer.rb', line 40

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

Instance Method Details

#serialize(table, tag) ⇒ Hash

Returns serialized payload.

Parameters:

  • table (BinData::Record)

    the table object

  • tag (String)

    table tag

Returns:

  • (Hash)

    serialized payload



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

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

Parameters:

  • data (String)

    binary bytes

  • tag (String)

    table tag

Returns:

  • (Hash)


61
62
63
# File 'lib/fontisan/export/table_serializer.rb', line 61

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