Class: Fontisan::Tables::NameTable

Inherits:
SfntTable
  • Object
show all
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.

Examples:

Accessing name table data

name = font.table("name")  # Returns SfntTable instance
name.family_name  # => "Noto Sans"
name.subfamily_name  # => "Regular"
name.full_name  # => "Noto Sans Regular"
name.postscript_name  # => "NotoSans-Regular"

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

#data, #entry, #font, #parsed

Instance Method Summary collapse

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

Parameters:

  • name_id (Integer)

    The name record ID

Returns:

  • (Array<Hash>)

    Array of hashes with platform, encoding, language, and string



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).

Parameters:

  • name_id (Integer)

    The name record ID

Returns:

  • (String, nil)

    The name string, or nil if not found



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_nameString?

Get font family name

Attempts to get the preferred family name, falling back to the standard family name if preferred is not available.

Returns:

  • (String, nil)

    Family name or nil if not found



43
44
45
# File 'lib/fontisan/tables/name_table.rb', line 43

def family_name
  english_name(PREFERRED_FAMILY) || english_name(FAMILY)
end

#full_nameString?

Get full font name

Returns:

  • (String, nil)

    Full name or nil if not found



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

def full_name
  english_name(FULL_NAME)
end

#name_recordsArray<NameRecord>?

Get all name records

Returns:

  • (Array<NameRecord>, nil)

    Array of name records, or nil if not parsed



125
126
127
# File 'lib/fontisan/tables/name_table.rb', line 125

def name_records
  parsed&.name_records
end

#postscript_nameString?

Get PostScript name

Returns:

  • (String, nil)

    PostScript name or nil if not found



67
68
69
# File 'lib/fontisan/tables/name_table.rb', line 67

def postscript_name
  english_name(POSTSCRIPT_NAME)
end

#preferred_family_nameString?

Get preferred family name

Returns:

  • (String, nil)

    Preferred family name or nil if not found



74
75
76
# File 'lib/fontisan/tables/name_table.rb', line 74

def preferred_family_name
  english_name(PREFERRED_FAMILY)
end

#preferred_subfamily_nameString?

Get preferred subfamily name

Returns:

  • (String, nil)

    Preferred subfamily name or nil if not found



81
82
83
# File 'lib/fontisan/tables/name_table.rb', line 81

def preferred_subfamily_name
  english_name(PREFERRED_SUBFAMILY)
end

#subfamily_nameString?

Get font subfamily name

Attempts to get the preferred subfamily name, falling back to the standard subfamily name if preferred is not available.

Returns:

  • (String, nil)

    Subfamily name or nil if not found



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

def subfamily_name
  english_name(PREFERRED_SUBFAMILY) || english_name(SUBFAMILY)
end