Class: Fontisan::Export::Transformers::HeadTransformer

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

Overview

HeadTransformer transforms head table to TTX format

Converts Fontisan::Tables::Head to Models::Ttx::Tables::HeadTable following proper model-to-model transformation principles.

Class Method Summary collapse

Class Method Details

.format_binary_flags(value, bits) ⇒ String

Format binary flags

Parameters:

  • value (Integer)

    Integer value

  • bits (Integer)

    Number of bits

Returns:

  • (String)

    Binary string with spaces every 8 bits



75
76
77
78
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 75

def self.format_binary_flags(value, bits)
  binary = value.to_s(2).rjust(bits, "0")
  binary.scan(/.{1,8}/).join(" ")
end

.format_fixed(value) ⇒ String

Format fixed-point number (16.16)

Parameters:

  • value (Integer)

    Fixed-point value

Returns:

  • (String)

    Decimal string



52
53
54
55
56
57
58
59
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 52

def self.format_fixed(value)
  result = value.to_f / 65536.0
  if result == result.to_i
    "#{result.to_i}.0"
  else
    result.to_s
  end
end

.format_hex(value, width: 8) ⇒ String

Format hex value

Parameters:

  • value (Integer)

    Integer value

  • width (Integer) (defaults to: 8)

    Minimum hex width

Returns:

  • (String)

    Hex string (e.g., "0x1234")



66
67
68
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 66

def self.format_hex(value, width: 8)
  "0x#{value.to_s(16).rjust(width, '0')}"
end

.format_timestamp(timestamp) ⇒ String

Format timestamp

Parameters:

  • timestamp (Integer)

    Mac timestamp (seconds since 1904-01-01)

Returns:

  • (String)

    Human-readable date string



84
85
86
87
88
89
90
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 84

def self.format_timestamp(timestamp)
  mac_epoch = Time.utc(1904, 1, 1)
  time = mac_epoch + timestamp
  time.strftime("%a %b %e %H:%M:%S %Y")
rescue StandardError
  "Invalid Date"
end

.to_int(value) ⇒ Integer

Convert BinData value to native Ruby integer

Parameters:

  • value (Object)

    BinData value or integer

Returns:

  • (Integer)

    Native integer



44
45
46
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 44

def self.to_int(value)
  value.respond_to?(:to_i) ? value.to_i : value
end

.transform(head_table) ⇒ Models::Ttx::Tables::HeadTable

Transform head table to TTX model

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 15

def self.transform(head_table)
  return nil unless head_table

  Models::Ttx::Tables::HeadTable.new.tap do |ttx|
    ttx.table_version = format_fixed(to_int(head_table.version))
    ttx.font_revision = format_fixed(to_int(head_table.font_revision))
    ttx.checksum_adjustment = format_hex(to_int(head_table.checksum_adjustment))
    ttx.magic_number = format_hex(to_int(head_table.magic_number))
    ttx.flags = to_int(head_table.flags).to_s
    ttx.units_per_em = to_int(head_table.units_per_em).to_s
    ttx.created = format_timestamp(to_int(head_table.created))
    ttx.modified = format_timestamp(to_int(head_table.modified))
    ttx.x_min = to_int(head_table.x_min).to_s
    ttx.y_min = to_int(head_table.y_min).to_s
    ttx.x_max = to_int(head_table.x_max).to_s
    ttx.y_max = to_int(head_table.y_max).to_s
    ttx.mac_style = format_binary_flags(to_int(head_table.mac_style),
                                        16)
    ttx.lowest_rec_ppem = to_int(head_table.lowest_rec_ppem).to_s
    ttx.font_direction_hint = to_int(head_table.font_direction_hint).to_s
    ttx.index_to_loc_format = to_int(head_table.index_to_loc_format).to_s
    ttx.glyph_data_format = to_int(head_table.glyph_data_format).to_s
  end
end