1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
# frozen_string_literal: true
require_relative '../table'
require 'digest/sha1'
module TTFunk
class Table
# Naming (`name`) table
class Name < Table
# Name Record.
class NameString < ::String
# Platform ID.
# @return [Integer]
attr_reader :platform_id
# Platform-specific encoding ID.
# @return [Integer]
attr_reader :encoding_id
# Language ID.
# @return [Integer]
attr_reader :language_id
# @param text [String]
# @param platform_id [Integer]
# @param encoding_id [Integer]
# @param language_id [Integer]
def initialize(text, platform_id, encoding_id, language_id)
super(text)
@platform_id = platform_id
@encoding_id = encoding_id
@language_id = language_id
end
# Removes chracter incompatible with PostScript.
# @return [String] PostScript-compatible version of this string.
def strip_extended
stripped = gsub(/[\x00-\x19\x80-\xff]/n, '')
stripped = '[not-postscript]' if stripped.empty?
stripped
end
end
# Name records.
# @return [Array<Hash>]
attr_reader :entries
# Name strings.
# @return [Hash{Integer => NameString}]
attr_reader :strings
# Copyright notice.
# @return [Array<NameString>]
attr_reader :copyright
# Font Family names.
# @return [Array<NameString>]
attr_reader :font_family
# Font Subfamily names.
# @return [Array<NameString>]
attr_reader :font_subfamily
# Unique font identifiers.
# @return [Array<NameString>]
attr_reader :unique_subfamily
# Full font names.
# @return [Array<NameString>]
attr_reader :font_name
# Version strings.
# @return [Array<NameString>]
attr_reader :version
# Trademarks.
# @return [Array<NameString>]
attr_reader :trademark
# Manufacturer Names.
# @return [Array<NameString>]
attr_reader :manufacturer
# Designers.
# @return [Array<NameString>]
attr_reader :designer
# Descriptions.
# @return [Array<NameString>]
attr_reader :description
# Vendor URLs.
# @return [Array<NameString>]
attr_reader :vendor_url
# Designer URLs.
# @return [Array<NameString>]
attr_reader :designer_url
# License Descriptions.
# @return [Array<NameString>]
attr_reader :license
# License Info URLs.
# @return [Array<NameString>]
attr_reader :license_url
# Typographic Family names.
# @return [Array<NameString>]
attr_reader :preferred_family
# Typographic Subfamily names.
# @return [Array<NameString>]
attr_reader :preferred_subfamily
# Compatible Full Names.
# @return [Array<NameString>]
attr_reader :compatible_full
# Sample texts.
# @return [Array<NameString>]
attr_reader :sample_text
# Copyright notice ID.
COPYRIGHT_NAME_ID = 0
# Font Family name ID.
FONT_FAMILY_NAME_ID = 1
# Font Subfamily name ID.
FONT_SUBFAMILY_NAME_ID = 2
# Unique font identifier ID.
UNIQUE_SUBFAMILY_NAME_ID = 3
# Full font name that reflects all family and relevant subfamily
# descriptors ID.
FONT_NAME_NAME_ID = 4
# Version string ID.
VERSION_NAME_ID = 5
# PostScript name for the font ID.
POSTSCRIPT_NAME_NAME_ID = 6
# Trademark ID.
TRADEMARK_NAME_ID = 7
# Manufacturer Name ID.
MANUFACTURER_NAME_ID = 8
# Designer ID.
DESIGNER_NAME_ID = 9
# Description ID.
DESCRIPTION_NAME_ID = 10
# Vendor URL ID.
VENDOR_URL_NAME_ID = 11
# Designer URL ID.
DESIGNER_URL_NAME_ID = 12
# License Description ID.
LICENSE_NAME_ID = 13
# License Info URL ID.
LICENSE_URL_NAME_ID = 14
# Typographic Family name ID.
PREFERRED_FAMILY_NAME_ID = 16
# Typographic Subfamily name ID.
PREFERRED_SUBFAMILY_NAME_ID = 17
# Compatible Full ID.
COMPATIBLE_FULL_NAME_ID = 18
# Sample text ID.
SAMPLE_TEXT_NAME_ID = 19
# Encode table.
#
# @param names [TTFunk::Table::Name]
# @param key [String]
# @return [String]
def self.encode(names, key = '')
tag = Digest::SHA1.hexdigest(key)[0, 6]
postscript_name = NameString.new("#{tag}+#{names.postscript_name}", 1, 0, 0)
strings = names.strings.dup
strings[6] = [postscript_name]
str_count = strings.reduce(0) { |sum, (_, list)| sum + list.length }
table = [0, str_count, 6 + (12 * str_count)].pack('n*')
strtable = +''
items = []
strings.each do |id, list|
list.each do |string|
items << [id, string]
end
end
items =
items.sort_by { |id, string|
[string.platform_id, string.encoding_id, string.language_id, id]
}
items.each do |id, string|
table << [
string.platform_id, string.encoding_id, string.language_id, id,
string.length, strtable.length,
].pack('n*')
strtable << string
end
table << strtable
end
# PostScript name for the font.
# @return [String]
def postscript_name
return @postscript_name if @postscript_name
font_family.first || 'unnamed'
end
private
def parse!
count, string_offset = read(6, 'x2n*')
@entries = []
count.times do
platform, encoding, language, id, length, start_offset =
read(12, 'n*')
@entries << {
platform_id: platform,
encoding_id: encoding,
language_id: language,
name_id: id,
length: length,
offset: offset + string_offset + start_offset,
text: nil,
}
end
@strings = Hash.new { |h, k| h[k] = [] }
count.times do |i|
io.pos = @entries[i][:offset]
@entries[i][:text] = io.read(@entries[i][:length])
@strings[@entries[i][:name_id]] << NameString.new(
@entries[i][:text] || '',
@entries[i][:platform_id],
@entries[i][:encoding_id],
@entries[i][:language_id],
)
end
# should only be ONE postscript name
@copyright = @strings[COPYRIGHT_NAME_ID]
@font_family = @strings[FONT_FAMILY_NAME_ID]
@font_subfamily = @strings[FONT_SUBFAMILY_NAME_ID]
@unique_subfamily = @strings[UNIQUE_SUBFAMILY_NAME_ID]
@font_name = @strings[FONT_NAME_NAME_ID]
@version = @strings[VERSION_NAME_ID]
unless @strings[POSTSCRIPT_NAME_NAME_ID].empty?
@postscript_name = @strings[POSTSCRIPT_NAME_NAME_ID]
.first.strip_extended
end
@trademark = @strings[TRADEMARK_NAME_ID]
@manufacturer = @strings[MANUFACTURER_NAME_ID]
@designer = @strings[DESIGNER_NAME_ID]
@description = @strings[DESCRIPTION_NAME_ID]
@vendor_url = @strings[VENDOR_URL_NAME_ID]
@designer_url = @strings[DESIGNER_URL_NAME_ID]
@license = @strings[LICENSE_NAME_ID]
@license_url = @strings[LICENSE_URL_NAME_ID]
@preferred_family = @strings[PREFERRED_FAMILY_NAME_ID]
@preferred_subfamily = @strings[PREFERRED_SUBFAMILY_NAME_ID]
@compatible_full = @strings[COMPATIBLE_FULL_NAME_ID]
@sample_text = @strings[SAMPLE_TEXT_NAME_ID]
end
end
end
end
|