Class: Fontisan::Tables::Cff::Header

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

Overview

CFF Header structure

The CFF header appears at the beginning of the CFF table and contains basic version and structural information about the CFF data.

Structure (4 bytes minimum):

  • uint8: major version (always 1 for CFF, 2 for CFF2)
  • uint8: minor version (always 0)
  • uint8: hdr_size (header size in bytes, typically 4)
  • uint8: off_size (offset size used throughout CFF, 1-4 bytes)

Reference: CFF specification section 4 "Header" https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf

Examples:

Reading a CFF header

data = File.binread("font.otf", 4, cff_offset)
header = Fontisan::Tables::Cff::Header.read(data)
puts header.major  # => 1
puts header.minor  # => 0
puts header.off_size  # => 4

Instance Method Summary collapse

Methods inherited from Binary::BaseRecord

#raw_data, read

Instance Method Details

#cff2?Boolean

Check if this is a CFF2 header (variable CFF fonts)

Returns:

  • (Boolean)

    True if major version is 2



53
54
55
# File 'lib/fontisan/tables/cff/header.rb', line 53

def cff2?
  major == 2
end

#cff?Boolean

Check if this is a valid CFF version 1.0 header

Returns:

  • (Boolean)

    True if major version is 1 and minor is 0



46
47
48
# File 'lib/fontisan/tables/cff/header.rb', line 46

def cff?
  major == 1 && minor.zero?
end

#valid?Boolean

Validate that the header has correct values

Returns:

  • (Boolean)

    True if header is valid



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fontisan/tables/cff/header.rb', line 67

def valid?
  # Major version must be 1 or 2
  return false unless [1, 2].include?(major)

  # Minor version must be 0
  return false unless minor.zero?

  # Header size must be at least 4 bytes
  return false unless hdr_size >= 4

  # Offset size must be between 1 and 4
  return false unless (1..4).cover?(off_size)

  true
end

#validate!Object

Validate header and raise error if invalid

Raises:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fontisan/tables/cff/header.rb', line 86

def validate!
  return if valid?

  message = "Invalid CFF header: " \
            "version=#{version}, " \
            "hdr_size=#{hdr_size}, " \
            "off_size=#{off_size}"
  error = Fontisan::CorruptedTableError.new(message)
  error.set_backtrace(caller)
  Kernel.raise(error)
end

#versionString

Get the version as a string

Returns:

  • (String)

    Version in "major.minor" format



60
61
62
# File 'lib/fontisan/tables/cff/header.rb', line 60

def version
  "#{major}.#{minor}"
end