Class: Fontisan::Tables::Head

Inherits:
Binary::BaseRecord show all
Defined in:
lib/fontisan/tables/head.rb

Overview

BinData structure for the 'head' (Font Header) table

The head table contains global information about the font, including metadata about the font file, bounding box, and indexing information.

Reference: OpenType specification, head table

Examples:

Reading a head table

data = File.binread("font.ttf", 54, head_offset)
head = Fontisan::Tables::Head.read(data)
puts head.units_per_em  # => 2048
puts head.version_number  # => 1.0

Constant Summary collapse

MAGIC_NUMBER =

Magic number that must be present in the head table

0x5F0F3CF5

Instance Method Summary collapse

Methods inherited from Binary::BaseRecord

#raw_data, read

Instance Method Details

#createdTime

Convert created timestamp to Time object

Returns:

  • (Time)

    Creation time



65
66
67
# File 'lib/fontisan/tables/head.rb', line 65

def created
  longdatetime_to_time(created_raw)
end

#font_revisionFloat

Convert font revision from fixed-point to float

Returns:

  • (Float)

    Font revision number



58
59
60
# File 'lib/fontisan/tables/head.rb', line 58

def font_revision
  fixed_to_float(font_revision_raw)
end

#modifiedTime

Convert modified timestamp to Time object

Returns:

  • (Time)

    Modification time



72
73
74
# File 'lib/fontisan/tables/head.rb', line 72

def modified
  longdatetime_to_time(modified_raw)
end

#valid?Boolean

Validate that the magic number is correct

Returns:

  • (Boolean)

    True if magic number is valid



79
80
81
# File 'lib/fontisan/tables/head.rb', line 79

def valid?
  magic_number == MAGIC_NUMBER
end

#valid_bounding_box?Boolean

Validation helper: Check if bounding box is valid

The bounding box should have xMin < xMax and yMin < yMax

Returns:

  • (Boolean)

    True if bounding box coordinates are valid



121
122
123
# File 'lib/fontisan/tables/head.rb', line 121

def valid_bounding_box?
  x_min < x_max && y_min < y_max
end

#valid_glyph_data_format?Boolean

Validation helper: Check if glyph_data_format is valid

Must be 0 for current format

Returns:

  • (Boolean)

    True if format is 0



139
140
141
# File 'lib/fontisan/tables/head.rb', line 139

def valid_glyph_data_format?
  glyph_data_format.zero?
end

#valid_index_to_loc_format?Boolean

Validation helper: Check if index_to_loc_format is valid

Must be 0 (short) or 1 (long)

Returns:

  • (Boolean)

    True if format is 0 or 1



130
131
132
# File 'lib/fontisan/tables/head.rb', line 130

def valid_index_to_loc_format?
  [0, 1].include?(index_to_loc_format)
end

#valid_magic?Boolean

Validation helper: Check if magic number is valid

Returns:

  • (Boolean)

    True if magic number equals 0x5F0F3CF5



86
87
88
# File 'lib/fontisan/tables/head.rb', line 86

def valid_magic?
  magic_number == MAGIC_NUMBER
end

#valid_units_per_em?Boolean

Validation helper: Check if units per em is valid

Units per em should be a power of 2 between 16 and 16384

Returns:

  • (Boolean)

    True if units_per_em is valid



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fontisan/tables/head.rb', line 104

def valid_units_per_em?
  return false if units_per_em.nil? || units_per_em.zero?

  # Must be between 16 and 16384
  return false unless units_per_em.between?(16, 16384)

  # Should be a power of 2 (recommended but not required)
  # Common values: 1000, 1024, 2048
  # We'll allow any value in range for flexibility
  true
end

#valid_version?Boolean

Validation helper: Check if version is valid

OpenType spec requires version to be 1.0

Returns:

  • (Boolean)

    True if version is 1.0



95
96
97
# File 'lib/fontisan/tables/head.rb', line 95

def valid_version?
  version_raw == 0x00010000 # Version 1.0
end

#validate_magic_number!Object Also known as: validate!

Validate magic number and raise error if invalid

Raises:



146
147
148
149
150
151
152
153
154
155
# File 'lib/fontisan/tables/head.rb', line 146

def validate_magic_number!
  return if valid?

  message = "Invalid magic number in head table: " \
            "expected 0x#{MAGIC_NUMBER.to_s(16).upcase}, " \
            "got 0x#{magic_number.to_s(16).upcase}"
  error = Fontisan::CorruptedTableError.new(message)
  error.set_backtrace(caller)
  Kernel.raise(error)
end

#versionFloat

Convert version from fixed-point to float

Returns:

  • (Float)

    Version number (e.g., 1.0)



51
52
53
# File 'lib/fontisan/tables/head.rb', line 51

def version
  fixed_to_float(version_raw)
end