-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathname.js
More file actions
141 lines (120 loc) · 3.51 KB
/
Copy pathname.js
File metadata and controls
141 lines (120 loc) · 3.51 KB
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
import * as r from 'restructure';
import {getEncoding, LANGUAGES} from '../encodings';
let NameRecord = new r.Struct({
platformID: r.uint16,
encodingID: r.uint16,
languageID: r.uint16,
nameID: r.uint16,
length: r.uint16,
string: new r.Pointer(r.uint16,
new r.String('length', t => getEncoding(t.platformID, t.encodingID, t.languageID)),
{ type: 'parent', relativeTo: ctx => ctx.parent.stringOffset, allowNull: false }
)
});
let LangTagRecord = new r.Struct({
length: r.uint16,
tag: new r.Pointer(r.uint16, new r.String('length', 'utf16be'), {type: 'parent', relativeTo: ctx => ctx.stringOffset})
});
var NameTable = new r.VersionedStruct(r.uint16, {
0: {
count: r.uint16,
stringOffset: r.uint16,
records: new r.Array(NameRecord, 'count')
},
1: {
count: r.uint16,
stringOffset: r.uint16,
records: new r.Array(NameRecord, 'count'),
langTagCount: r.uint16,
langTags: new r.Array(LangTagRecord, 'langTagCount')
}
});
export default NameTable;
const NAMES = [
'copyright',
'fontFamily',
'fontSubfamily',
'uniqueSubfamily',
'fullName',
'version',
'postscriptName', // Note: A font may have only one PostScript name and that name must be ASCII.
'trademark',
'manufacturer',
'designer',
'description',
'vendorURL',
'designerURL',
'license',
'licenseURL',
null, // reserved
'preferredFamily',
'preferredSubfamily',
'compatibleFull',
'sampleText',
'postscriptCIDFontName',
'wwsFamilyName',
'wwsSubfamilyName'
];
NameTable.process = function(stream) {
let records = {};
for (let record of this.records) {
// find out what language this is for
let language = LANGUAGES[record.platformID][record.languageID];
if (language == null && this.langTags != null && record.languageID >= 0x8000) {
language = this.langTags[record.languageID - 0x8000].tag;
}
if (language == null) {
language = record.platformID + '-' + record.languageID;
}
// if the nameID is >= 256, it is a font feature record (AAT)
let key = record.nameID >= 256 ? 'fontFeatures' : (NAMES[record.nameID] || record.nameID);
let addRecord = (key, record) => {
if (records[key] == null) {
records[key] = {};
}
let obj = records[key];
if (key === 'fontFeatures') {
obj = obj[record.nameID] || (obj[record.nameID] = {});
}
if (typeof record.string === 'string' || typeof obj[language] !== 'string') {
obj[language] = record.string;
}
}
addRecord(key, record);
// if the nameID is 2 or 17, it may also be a font feature record (AAT).
if (record.nameID == 2 || record.nameID == 17) {
addRecord('fontFeatures', record);
}
}
this.records = records;
};
NameTable.preEncode = function() {
if (Array.isArray(this.records)) return;
this.version = 0;
let records = [];
for (let key in this.records) {
let val = this.records[key];
if (key === 'fontFeatures') continue;
records.push({
platformID: 3,
encodingID: 1,
languageID: 0x409,
nameID: NAMES.indexOf(key),
length: val.en.length * 2,
string: val.en
});
if (key === 'postscriptName') {
records.push({
platformID: 1,
encodingID: 0,
languageID: 0,
nameID: NAMES.indexOf(key),
length: val.en.length,
string: val.en
});
}
}
this.records = records;
this.count = records.length;
this.stringOffset = NameTable.size(this, null, false);
};