Class: Fontisan::Tables::Os2
- Inherits:
-
Binary::BaseRecord
- Object
- BinData::Record
- Binary::BaseRecord
- Fontisan::Tables::Os2
- 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
-
#has_embedding_permissions? ⇒ Boolean
Validation helper: Check if embedding permissions are set.
-
#has_optical_point_size? ⇒ Boolean
Check if optical point size information is available.
-
#has_panose? ⇒ Boolean
Validation helper: Check if PANOSE data is present.
-
#has_unicode_ranges? ⇒ Boolean
Validation helper: Check if Unicode ranges are set.
-
#has_vendor_id? ⇒ Boolean
Validation helper: Check if vendor ID is present.
-
#has_x_height_cap_height? ⇒ Boolean
Validation helper: Check if x_height and cap_height are present (v2+).
-
#lower_optical_point_size ⇒ Float?
Get the lower optical point size.
- #s_cap_height ⇒ Object
- #sx_height ⇒ Object
-
#type_flags ⇒ Integer
Get the embedding type flags.
-
#ul_code_page_range1 ⇒ Object
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.
- #ul_code_page_range2 ⇒ Object
-
#upper_optical_point_size ⇒ Float?
Get the upper optical point size.
- #us_break_char ⇒ Object
- #us_default_char ⇒ Object
- #us_lower_optical_point_size ⇒ Object
- #us_max_context ⇒ Object
- #us_upper_optical_point_size ⇒ Object
-
#valid_char_range? ⇒ Boolean
Validation helper: Check if first/last char indices are reasonable.
-
#valid_selection_flags? ⇒ Boolean
Validation helper: Check if selection flags are valid.
-
#valid_typo_metrics? ⇒ Boolean
Validation helper: Check if typo metrics are reasonable.
-
#valid_version? ⇒ Boolean
Validation helper: Check if version is valid.
-
#valid_weight_class? ⇒ Boolean
Validation helper: Check if weight class is valid.
-
#valid_width_class? ⇒ Boolean
Validation helper: Check if width class is valid.
-
#valid_win_metrics? ⇒ Boolean
Validation helper: Check if Win metrics are valid.
-
#vendor_id ⇒ String
Get the vendor ID as a trimmed string.
Methods inherited from Binary::BaseRecord
Instance Method Details
#has_embedding_permissions? ⇒ Boolean
Validation helper: Check if embedding permissions are set
fs_type indicates embedding and subsetting permissions
251 252 253 |
# File 'lib/fontisan/tables/os2.rb', line 251 def !fs_type.nil? end |
#has_optical_point_size? ⇒ Boolean
Check if optical point size information is available
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
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
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
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
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_size ⇒ Float?
Get the lower optical point size
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_height ⇒ Object
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_height ⇒ Object
89 90 91 92 93 |
# File 'lib/fontisan/tables/os2.rb', line 89 def sx_height return nil unless version >= 2 super end |
#type_flags ⇒ Integer
Get the embedding type flags
143 144 145 |
# File 'lib/fontisan/tables/os2.rb', line 143 def type_flags fs_type end |
#ul_code_page_range1 ⇒ Object
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_range2 ⇒ Object
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_size ⇒ Float?
Get the upper optical point size
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_char ⇒ Object
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_char ⇒ Object
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_size ⇒ Object
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_context ⇒ Object
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_size ⇒ Object
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
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
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
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
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
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
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)
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_id ⇒ String
Get the vendor ID as a trimmed string
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 |