Class: Fontisan::Tables::Os2

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

Overview

Parser for the ‘OS/2’ (OS/2 and Windows Metrics) table

The OS/2 table contains OS/2 and Windows-specific metrics that are required by Windows and OS/2. This includes font metrics, character ranges, vendor information, and embedding permissions.

The table has evolved through multiple versions (0-5), with newer versions adding additional fields while maintaining backward compatibility.

Reference: OpenType specification, OS/2 table

Instance Method Summary collapse

Methods inherited from Binary::BaseRecord

#raw_data, read, #valid?

Instance Method Details

#has_embedding_permissions?Boolean

Validation helper: Check if embedding permissions are set

fs_type indicates embedding and subsetting permissions

Returns:

  • (Boolean)

    True if embedding permissions are defined



251
252
253
# File 'lib/fontisan/tables/os2.rb', line 251

def has_embedding_permissions?
  !fs_type.nil?
end

#has_optical_point_size?Boolean

Check if optical point size information is available

Returns:

  • (Boolean)

    True if version >= 5



150
151
152
# File 'lib/fontisan/tables/os2.rb', line 150

def has_optical_point_size?
  version >= 5
end

#has_panose?Boolean

Validation helper: Check if PANOSE data is present

All PANOSE values should not be zero

Returns:

  • (Boolean)

    True if PANOSE seems to be set



242
243
244
# File 'lib/fontisan/tables/os2.rb', line 242

def has_panose?
  panose&.any? { |val| val != 0 }
end

#has_unicode_ranges?Boolean

Validation helper: Check if Unicode ranges are set

At least one Unicode range bit should be set

Returns:

  • (Boolean)

    True if any Unicode range bits are set



233
234
235
# File 'lib/fontisan/tables/os2.rb', line 233

def has_unicode_ranges?
  (ul_unicode_range1 | ul_unicode_range2 | ul_unicode_range3 | ul_unicode_range4) != 0
end

#has_vendor_id?Boolean

Validation helper: Check if vendor ID is present

Vendor ID should be a 4-character code

Returns:

  • (Boolean)

    True if vendor ID exists and is non-empty



206
207
208
# File 'lib/fontisan/tables/os2.rb', line 206

def has_vendor_id?
  !vendor_id.empty?
end

#has_x_height_cap_height?Boolean

Validation helper: Check if x_height and cap_height are present (v2+)

For version 2+, these should be set

Returns:

  • (Boolean)

    True if metrics are present (or not required)



272
273
274
275
276
# File 'lib/fontisan/tables/os2.rb', line 272

def has_x_height_cap_height?
  return true if version < 2 # Not required for v0-1

  !sx_height.nil? && !s_cap_height.nil? && sx_height.positive? && s_cap_height.positive?
end

#lower_optical_point_sizeFloat?

Get the lower optical point size

Returns:

  • (Float, nil)

    The lower optical point size in points, or nil if not available



158
159
160
161
162
# File 'lib/fontisan/tables/os2.rb', line 158

def lower_optical_point_size
  return nil unless has_optical_point_size?

  us_lower_optical_point_size / 20.0
end

#s_cap_heightObject



95
96
97
98
99
# File 'lib/fontisan/tables/os2.rb', line 95

def s_cap_height
  return nil unless version >= 2

  super
end

#sx_heightObject



89
90
91
92
93
# File 'lib/fontisan/tables/os2.rb', line 89

def sx_height
  return nil unless version >= 2

  super
end

#type_flagsInteger

Get the embedding type flags

Returns:

  • (Integer)

    The fs_type value (embedding permissions)



143
144
145
# File 'lib/fontisan/tables/os2.rb', line 143

def type_flags
  fs_type
end

#ul_code_page_range1Object

Override conditional field accessors to return nil when not present BinData’s onlyif fields return default values even when not read, so we need to check the version before accessing them



77
78
79
80
81
# File 'lib/fontisan/tables/os2.rb', line 77

def ul_code_page_range1
  return nil unless version >= 1

  super
end

#ul_code_page_range2Object



83
84
85
86
87
# File 'lib/fontisan/tables/os2.rb', line 83

def ul_code_page_range2
  return nil unless version >= 1

  super
end

#upper_optical_point_sizeFloat?

Get the upper optical point size

Returns:

  • (Float, nil)

    The upper optical point size in points, or nil if not available



168
169
170
171
172
# File 'lib/fontisan/tables/os2.rb', line 168

def upper_optical_point_size
  return nil unless has_optical_point_size?

  us_upper_optical_point_size / 20.0
end

#us_break_charObject



107
108
109
110
111
# File 'lib/fontisan/tables/os2.rb', line 107

def us_break_char
  return nil unless version >= 2

  super
end

#us_default_charObject



101
102
103
104
105
# File 'lib/fontisan/tables/os2.rb', line 101

def us_default_char
  return nil unless version >= 2

  super
end

#us_lower_optical_point_sizeObject



119
120
121
122
123
# File 'lib/fontisan/tables/os2.rb', line 119

def us_lower_optical_point_size
  return nil unless version >= 5

  super
end

#us_max_contextObject



113
114
115
116
117
# File 'lib/fontisan/tables/os2.rb', line 113

def us_max_context
  return nil unless version >= 2

  super
end

#us_upper_optical_point_sizeObject



125
126
127
128
129
# File 'lib/fontisan/tables/os2.rb', line 125

def us_upper_optical_point_size
  return nil unless version >= 5

  super
end

#valid_char_range?Boolean

Validation helper: Check if first/last char indices are reasonable

first should be <= last

Returns:

  • (Boolean)

    True if character range is valid



283
284
285
# File 'lib/fontisan/tables/os2.rb', line 283

def valid_char_range?
  us_first_char_index <= us_last_char_index
end

#valid_selection_flags?Boolean

Validation helper: Check if selection flags are valid

Checks for valid combinations of selection flags

Returns:

  • (Boolean)

    True if fs_selection has valid flags



260
261
262
263
264
265
# File 'lib/fontisan/tables/os2.rb', line 260

def valid_selection_flags?
  return false if fs_selection.nil?

  # Bits 0-9 are defined, others should be zero
  (fs_selection & 0xFC00).zero?
end

#valid_typo_metrics?Boolean

Validation helper: Check if typo metrics are reasonable

Ascent should be positive, descender negative, line gap non-negative

Returns:

  • (Boolean)

    True if typo metrics have correct signs



215
216
217
# File 'lib/fontisan/tables/os2.rb', line 215

def valid_typo_metrics?
  s_typo_ascender.positive? && s_typo_descender.negative? && s_typo_line_gap >= 0
end

#valid_version?Boolean

Validation helper: Check if version is valid

Valid versions are 0 through 5

Returns:

  • (Boolean)

    True if version is 0-5



179
180
181
# File 'lib/fontisan/tables/os2.rb', line 179

def valid_version?
  version&.between?(0, 5)
end

#valid_weight_class?Boolean

Validation helper: Check if weight class is valid

Valid values are 1-1000, common values are multiples of 100

Returns:

  • (Boolean)

    True if weight class is valid



188
189
190
# File 'lib/fontisan/tables/os2.rb', line 188

def valid_weight_class?
  us_weight_class&.between?(1, 1000)
end

#valid_width_class?Boolean

Validation helper: Check if width class is valid

Valid values are 1-9

Returns:

  • (Boolean)

    True if width class is 1-9



197
198
199
# File 'lib/fontisan/tables/os2.rb', line 197

def valid_width_class?
  us_width_class&.between?(1, 9)
end

#valid_win_metrics?Boolean

Validation helper: Check if Win metrics are valid

Both should be positive (unsigned in spec)

Returns:

  • (Boolean)

    True if Win ascent and descent are positive



224
225
226
# File 'lib/fontisan/tables/os2.rb', line 224

def valid_win_metrics?
  us_win_ascent.positive? && us_win_descent.positive?
end

#vendor_idString

Get the vendor ID as a trimmed string

Returns:

  • (String)

    The vendor ID with trailing spaces and nulls removed



134
135
136
137
138
# File 'lib/fontisan/tables/os2.rb', line 134

def vendor_id
  return "" unless ach_vend_id

  ach_vend_id.gsub(/[\x00\s]+$/, "")
end