Class: Fontisan::Tables::Os2

Inherits:
Binary::BaseRecord show all
Extended by:
Registered
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 Attribute Summary

Attributes inherited from Binary::BaseRecord

#raw_data

Instance Method Summary collapse

Methods included from Registered

register_tag

Methods inherited from Binary::BaseRecord

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



254
255
256
# File 'lib/fontisan/tables/os2.rb', line 254

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



153
154
155
# File 'lib/fontisan/tables/os2.rb', line 153

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



245
246
247
# File 'lib/fontisan/tables/os2.rb', line 245

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



236
237
238
# File 'lib/fontisan/tables/os2.rb', line 236

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



209
210
211
# File 'lib/fontisan/tables/os2.rb', line 209

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)



275
276
277
278
279
# File 'lib/fontisan/tables/os2.rb', line 275

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



161
162
163
164
165
# File 'lib/fontisan/tables/os2.rb', line 161

def lower_optical_point_size
  return nil unless has_optical_point_size?

  us_lower_optical_point_size / 20.0
end

#s_cap_heightObject



98
99
100
101
102
# File 'lib/fontisan/tables/os2.rb', line 98

def s_cap_height
  return nil unless version >= 2

  super
end

#sx_heightObject



92
93
94
95
96
# File 'lib/fontisan/tables/os2.rb', line 92

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)



146
147
148
# File 'lib/fontisan/tables/os2.rb', line 146

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



80
81
82
83
84
# File 'lib/fontisan/tables/os2.rb', line 80

def ul_code_page_range1
  return nil unless version >= 1

  super
end

#ul_code_page_range2Object



86
87
88
89
90
# File 'lib/fontisan/tables/os2.rb', line 86

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



171
172
173
174
175
# File 'lib/fontisan/tables/os2.rb', line 171

def upper_optical_point_size
  return nil unless has_optical_point_size?

  us_upper_optical_point_size / 20.0
end

#us_break_charObject



110
111
112
113
114
# File 'lib/fontisan/tables/os2.rb', line 110

def us_break_char
  return nil unless version >= 2

  super
end

#us_default_charObject



104
105
106
107
108
# File 'lib/fontisan/tables/os2.rb', line 104

def us_default_char
  return nil unless version >= 2

  super
end

#us_lower_optical_point_sizeObject



122
123
124
125
126
# File 'lib/fontisan/tables/os2.rb', line 122

def us_lower_optical_point_size
  return nil unless version >= 5

  super
end

#us_max_contextObject



116
117
118
119
120
# File 'lib/fontisan/tables/os2.rb', line 116

def us_max_context
  return nil unless version >= 2

  super
end

#us_upper_optical_point_sizeObject



128
129
130
131
132
# File 'lib/fontisan/tables/os2.rb', line 128

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



286
287
288
# File 'lib/fontisan/tables/os2.rb', line 286

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



263
264
265
266
267
268
# File 'lib/fontisan/tables/os2.rb', line 263

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



218
219
220
# File 'lib/fontisan/tables/os2.rb', line 218

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



182
183
184
# File 'lib/fontisan/tables/os2.rb', line 182

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



191
192
193
# File 'lib/fontisan/tables/os2.rb', line 191

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



200
201
202
# File 'lib/fontisan/tables/os2.rb', line 200

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



227
228
229
# File 'lib/fontisan/tables/os2.rb', line 227

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



137
138
139
140
141
# File 'lib/fontisan/tables/os2.rb', line 137

def vendor_id
  return "" unless ach_vend_id

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