Class: Fontisan::Export::Transformers::HeadTransformer
- Inherits:
-
Object
- Object
- Fontisan::Export::Transformers::HeadTransformer
- 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
-
.format_binary_flags(value, bits) ⇒ String
Format binary flags.
-
.format_fixed(value) ⇒ String
Format fixed-point number (16.16).
-
.format_hex(value, width: 8) ⇒ String
Format hex value.
-
.format_timestamp(timestamp) ⇒ String
Format timestamp.
-
.to_int(value) ⇒ Integer
Convert BinData value to native Ruby integer.
-
.transform(head_table) ⇒ Models::Ttx::Tables::HeadTable
Transform head table to TTX model.
Class Method Details
.format_binary_flags(value, bits) ⇒ String
Format binary flags
77 78 79 80 |
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 77 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)
54 55 56 57 58 59 60 61 |
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 54 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
68 69 70 |
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 68 def self.format_hex(value, width: 8) "0x#{value.to_s(16).rjust(width, '0')}" end |
.format_timestamp(timestamp) ⇒ String
Format timestamp
86 87 88 89 90 91 92 |
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 86 def self.() mac_epoch = Time.utc(1904, 1, 1) time = mac_epoch + 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
46 47 48 |
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 46 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
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/fontisan/export/transformers/head_transformer.rb', line 17 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 = (to_int(head_table.created)) ttx.modified = (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 |