Class: Fontisan::Tables::NameTable
- Defined in:
- lib/fontisan/tables/name_table.rb
Overview
OOP representation of the 'name' (Naming) table
The name table contains all naming strings for the font, including font family name, style name, designer, license, etc.
This class extends SfntTable to provide name-specific convenience methods for accessing common name records.
Constant Summary collapse
- FAMILY =
Name record identifiers
These are the name IDs defined in the OpenType spec
1- SUBFAMILY =
2- FULL_NAME =
4- POSTSCRIPT_NAME =
6- PREFERRED_FAMILY =
16- PREFERRED_SUBFAMILY =
17- WWS_FAMILY =
21- WWS_SUBFAMILY =
22- PLATFORM_UNICODE =
Platform IDs
0- PLATFORM_MACINTOSH =
1- PLATFORM_WINDOWS =
3
Instance Attribute Summary
Attributes inherited from SfntTable
Instance Method Summary collapse
-
#all_names_for(name_id) ⇒ Array<Hash>
Get all names for a specific name ID.
-
#english_name(name_id) ⇒ String?
Get English name for a specific name ID.
-
#family_name ⇒ String?
Get font family name.
-
#full_name ⇒ String?
Get full font name.
-
#name_records ⇒ Array<NameRecord>?
Get all name records.
-
#postscript_name ⇒ String?
Get PostScript name.
-
#preferred_family_name ⇒ String?
Get preferred family name.
-
#preferred_subfamily_name ⇒ String?
Get preferred subfamily name.
-
#subfamily_name ⇒ String?
Get font subfamily name.
Methods inherited from SfntTable
#available?, #calculate_checksum, #checksum, #data_loaded?, #human_name, #initialize, #inspect, #length, #load_data!, #offset, #parse, #parsed?, #required?, #tag, #to_s, #validate!
Constructor Details
This class inherits a constructor from Fontisan::SfntTable
Instance Method Details
#all_names_for(name_id) ⇒ Array<Hash>
Get all names for a specific name ID
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/fontisan/tables/name_table.rb', line 133 def all_names_for(name_id) return [] unless parsed parsed.name_records .select { |nr| nr.name_id == name_id } .map do |nr| { platform_id: nr.platform_id, encoding_id: nr.encoding_id, language_id: nr.language_id, string: nr.string, } end end |
#english_name(name_id) ⇒ String?
Get English name for a specific name ID
Searches for an English name record with the given name ID. Prefers Windows (platform 3) over Mac (platform 1) over Unicode (platform 0).
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/fontisan/tables/name_table.rb', line 92 def english_name(name_id) return nil unless parsed # Find all name records with this name_id records = parsed.name_records.select { |nr| nr.name_id == name_id } return nil if records.empty? # Try to find English Windows name first (platform 3, language 0x409) windows = records.find do |nr| nr.platform_id == PLATFORM_WINDOWS && nr.language_id == 0x409 end return windows.string if windows&.string # Try Mac English (platform 1, language 0) mac = records.find do |nr| nr.platform_id == PLATFORM_MACINTOSH && nr.language_id.zero? end return mac.string if mac&.string # Try any English Unicode name (platform 0, language 0) unicode = records.find do |nr| nr.platform_id == PLATFORM_UNICODE && nr.language_id.zero? end return unicode.string if unicode&.string # Fallback to first record with this name_id first = records.first first&.string end |
#family_name ⇒ String?
Get font family name
Attempts to get the preferred family name, falling back to the standard family name if preferred is not available.
43 44 45 |
# File 'lib/fontisan/tables/name_table.rb', line 43 def family_name english_name(PREFERRED_FAMILY) || english_name(FAMILY) end |
#full_name ⇒ String?
Get full font name
60 61 62 |
# File 'lib/fontisan/tables/name_table.rb', line 60 def full_name english_name(FULL_NAME) end |
#name_records ⇒ Array<NameRecord>?
Get all name records
125 126 127 |
# File 'lib/fontisan/tables/name_table.rb', line 125 def name_records parsed&.name_records end |
#postscript_name ⇒ String?
Get PostScript name
67 68 69 |
# File 'lib/fontisan/tables/name_table.rb', line 67 def postscript_name english_name(POSTSCRIPT_NAME) end |
#preferred_family_name ⇒ String?
Get preferred family name
74 75 76 |
# File 'lib/fontisan/tables/name_table.rb', line 74 def preferred_family_name english_name(PREFERRED_FAMILY) end |
#preferred_subfamily_name ⇒ String?
Get preferred subfamily name
81 82 83 |
# File 'lib/fontisan/tables/name_table.rb', line 81 def preferred_subfamily_name english_name(PREFERRED_SUBFAMILY) end |
#subfamily_name ⇒ String?
Get font subfamily name
Attempts to get the preferred subfamily name, falling back to the standard subfamily name if preferred is not available.
53 54 55 |
# File 'lib/fontisan/tables/name_table.rb', line 53 def subfamily_name english_name(PREFERRED_SUBFAMILY) || english_name(SUBFAMILY) end |