Class: Fontisan::Tables::Name

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

Overview

BinData structure for the ‘name’ (Naming Table) table

The name table allows multilingual strings to be associated with the font. These strings can represent copyright notices, font names, family names, style names, and other information.

Reference: OpenType specification, name table

Examples:

Reading a name table

data = File.binread("font.ttf", length, name_offset)
name = Fontisan::Tables::Name.read(data)
puts name.english_name(Fontisan::Tables::Name::FAMILY)

Constant Summary collapse

0
FAMILY =
1
SUBFAMILY =
2
UNIQUE_ID =
3
FULL_NAME =
4
VERSION =
5
POSTSCRIPT_NAME =
6
TRADEMARK =
7
MANUFACTURER =
8
DESIGNER =
9
DESCRIPTION =
10
VENDOR_URL =
11
DESIGNER_URL =
12
LICENSE_DESCRIPTION =
13
LICENSE_URL =
14
PREFERRED_FAMILY =
16
PREFERRED_SUBFAMILY =
17
COMPATIBLE_FULL =
18
SAMPLE_TEXT =
19
POSTSCRIPT_CID =
20
WWS_FAMILY =
21
WWS_SUBFAMILY =
22
PLATFORM_UNICODE =

Platform IDs

0
PLATFORM_MACINTOSH =
1
PLATFORM_WINDOWS =
3
WINDOWS_LANGUAGE_EN_US =

Windows language ID for US English

0x0409
MAC_LANGUAGE_ENGLISH =

Mac language ID for English

0

Instance Method Summary collapse

Methods inherited from Binary::BaseRecord

read

Instance Method Details

#after_read_hookObject

Hook that gets called after all fields are read



111
112
113
# File 'lib/fontisan/tables/name.rb', line 111

def after_read_hook
  decode_all_strings
end

#countInteger

Get the count of name records (for backward compatibility)

Returns:

  • (Integer)

    Number of name records



124
125
126
# File 'lib/fontisan/tables/name.rb', line 124

def count
  record_count
end

#do_read(io) ⇒ Object

Make sure we call our hook after BinData finishes reading



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

def do_read(io)
  super
  after_read_hook
end

#english_name(name_id) ⇒ String?

Find an English name for the given name ID

Priority: Platform 3 (Windows) with language 0x0409 (US English) Fallback: Platform 1 (Mac) with language 0

Parameters:

  • name_id (Integer)

    The name ID to search for

Returns:

  • (String, nil)

    The decoded string or nil if not found



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fontisan/tables/name.rb', line 135

def english_name(name_id)
  # First try Windows English
  record = name_records.find do |rec|
    rec.name_id == name_id &&
      rec.platform_id == PLATFORM_WINDOWS &&
      rec.language_id == WINDOWS_LANGUAGE_EN_US
  end

  # Fallback to Mac English
  record ||= name_records.find do |rec|
    rec.name_id == name_id &&
      rec.platform_id == PLATFORM_MACINTOSH &&
      rec.language_id == MAC_LANGUAGE_ENGLISH
  end

  record&.string
end

#valid?Boolean

Validate the table

Returns:

  • (Boolean)

    True if the table is valid



156
157
158
159
160
# File 'lib/fontisan/tables/name.rb', line 156

def valid?
  !format.nil?
rescue StandardError
  false
end