Class: Fontisan::Models::Ttx::Tables::BinaryTable

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/fontisan/models/ttx/tables/binary_table.rb

Overview

BinaryTable represents unparsed tables in TTX format

Used as a fallback for tables that don't have specific parsers, storing them as hexdata following fonttools format.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_xml(xml_string, tag) ⇒ Object

Parse from XML



36
37
38
39
40
41
42
43
44
45
# File 'lib/fontisan/models/ttx/tables/binary_table.rb', line 36

def self.from_xml(xml_string, tag)
  doc = Nokogiri::XML(xml_string)
  table_elem = doc.at_xpath("//#{tag}")
  return nil unless table_elem

  hexdata_elem = table_elem.at_xpath("hexdata")
  hexdata = hexdata_elem ? parse_hexdata(hexdata_elem.text) : ""

  new(tag: tag, hexdata: hexdata)
end

.parse_hexdata(hex_str) ⇒ Object

Parse hexdata from XML



59
60
61
62
# File 'lib/fontisan/models/ttx/tables/binary_table.rb', line 59

def self.parse_hexdata(hex_str)
  hex_clean = hex_str.gsub(/\s+/, "")
  [hex_clean].pack("H*")
end

Instance Method Details

#to_xml(options = {}) ⇒ Object

Custom serialization since we need dynamic root element based on table tag



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fontisan/models/ttx/tables/binary_table.rb', line 19

def to_xml(options = {})
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.send(tag.to_sym) do
      xml.hexdata do
        xml.text("\n    #{format_hexdata(hexdata)}\n  ")
      end
    end
  end

  if options[:pretty]
    builder.doc.root.to_xml(indent: options.fetch(:indent, 2))
  else
    builder.to_xml
  end
end