Skip to content

Commit 4d3eefa

Browse files
carlobeltramediegomura
authored andcommitted
Update restructure to version 2.0.1
Refs diegomura#1451
1 parent b14b788 commit 4d3eefa

5 files changed

Lines changed: 491 additions & 0 deletions

File tree

packages/fontkit/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@react-pdf/fontkit",
3+
"version": "2.1.1",
4+
"description": "An advanced font engine for Node and the browser",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/diegomura/react-pdf.git",
8+
"directory": "packages/fontkit"
9+
},
10+
"scripts": {
11+
"watch": "rimraf ./lib && rollup -c -w",
12+
"build": "npm run trie:data && npm run trie:use && npm run trie:indic && rollup -c",
13+
"trie:use": "babel-node src/opentype/shapers/gen-use.js",
14+
"trie:indic": "babel-node src/opentype/shapers/gen-indic.js",
15+
"trie:data": "babel-node src/opentype/shapers/generate-data.js"
16+
},
17+
"main": "lib/fontkit.cjs.js",
18+
"module": "lib/fontkit.es.js",
19+
"browser": {
20+
"./lib/fontkit.es.js": "./lib/fontkit.browser.es.js",
21+
"./lib/fontkit.cjs.js": "./lib/fontkit.browser.cjs.js"
22+
},
23+
"files": [
24+
"lib"
25+
],
26+
"author": "Devon Govett <devongovett@gmail.com>",
27+
"license": "MIT",
28+
"dependencies": {
29+
"@babel/runtime": "^7.16.4",
30+
"brotli": "^1.2.0",
31+
"clone": "^1.0.4",
32+
"deep-equal": "^1.0.0",
33+
"dfa": "^1.2.0",
34+
"restructure": "^2.0.1",
35+
"tiny-inflate": "^1.0.2",
36+
"unicode-properties": "^1.4.1",
37+
"unicode-trie": "^0.3.0"
38+
},
39+
"devDependencies": {
40+
"codepoints": "^1.2.0",
41+
"concat-stream": "^1.4.6",
42+
"iconv-lite": "^0.4.13"
43+
}
44+
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import r from 'restructure';
2+
import {ScriptList, FeatureList, LookupList, Coverage, ClassDef, Device, Context, ChainingContext} from './opentype';
3+
import {FeatureVariations} from './variations';
4+
5+
let ValueFormat = new r.Bitfield(r.uint16, [
6+
'xPlacement', 'yPlacement',
7+
'xAdvance', 'yAdvance',
8+
'xPlaDevice', 'yPlaDevice',
9+
'xAdvDevice', 'yAdvDevice'
10+
]);
11+
12+
let types = {
13+
xPlacement: r.int16,
14+
yPlacement: r.int16,
15+
xAdvance: r.int16,
16+
yAdvance: r.int16,
17+
xPlaDevice: new r.Pointer(r.uint16, Device, { type: 'global', relativeTo: ctx => ctx.rel }),
18+
yPlaDevice: new r.Pointer(r.uint16, Device, { type: 'global', relativeTo: ctx => ctx.rel }),
19+
xAdvDevice: new r.Pointer(r.uint16, Device, { type: 'global', relativeTo: ctx => ctx.rel }),
20+
yAdvDevice: new r.Pointer(r.uint16, Device, { type: 'global', relativeTo: ctx => ctx.rel })
21+
};
22+
23+
class ValueRecord {
24+
constructor(key = 'valueFormat') {
25+
this.key = key;
26+
}
27+
28+
buildStruct(parent) {
29+
let struct = parent;
30+
while (!struct[this.key] && struct.parent) {
31+
struct = struct.parent;
32+
}
33+
34+
if (!struct[this.key]) return;
35+
36+
let fields = {};
37+
fields.rel = () => struct._startOffset;
38+
39+
let format = struct[this.key];
40+
for (let key in format) {
41+
if (format[key]) {
42+
fields[key] = types[key];
43+
}
44+
}
45+
46+
return new r.Struct(fields);
47+
}
48+
49+
size(val, ctx) {
50+
return this.buildStruct(ctx).size(val, ctx);
51+
}
52+
53+
decode(stream, parent) {
54+
let res = this.buildStruct(parent).decode(stream, parent);
55+
delete res.rel;
56+
return res;
57+
}
58+
}
59+
60+
let PairValueRecord = new r.Struct({
61+
secondGlyph: r.uint16,
62+
value1: new ValueRecord('valueFormat1'),
63+
value2: new ValueRecord('valueFormat2')
64+
});
65+
66+
let PairSet = new r.Array(PairValueRecord, r.uint16);
67+
68+
let Class2Record = new r.Struct({
69+
value1: new ValueRecord('valueFormat1'),
70+
value2: new ValueRecord('valueFormat2')
71+
});
72+
73+
let Anchor = new r.VersionedStruct(r.uint16, {
74+
1: { // Design units only
75+
xCoordinate: r.int16,
76+
yCoordinate: r.int16
77+
},
78+
79+
2: { // Design units plus contour point
80+
xCoordinate: r.int16,
81+
yCoordinate: r.int16,
82+
anchorPoint: r.uint16
83+
},
84+
85+
3: { // Design units plus Device tables
86+
xCoordinate: r.int16,
87+
yCoordinate: r.int16,
88+
xDeviceTable: new r.Pointer(r.uint16, Device),
89+
yDeviceTable: new r.Pointer(r.uint16, Device)
90+
}
91+
});
92+
93+
let EntryExitRecord = new r.Struct({
94+
entryAnchor: new r.Pointer(r.uint16, Anchor, {type: 'parent'}),
95+
exitAnchor: new r.Pointer(r.uint16, Anchor, {type: 'parent'})
96+
});
97+
98+
let MarkRecord = new r.Struct({
99+
class: r.uint16,
100+
markAnchor: new r.Pointer(r.uint16, Anchor, {type: 'parent'})
101+
});
102+
103+
let MarkArray = new r.Array(MarkRecord, r.uint16);
104+
105+
let BaseRecord = new r.Array(new r.Pointer(r.uint16, Anchor), t => t.parent.classCount);
106+
let BaseArray = new r.Array(BaseRecord, r.uint16);
107+
108+
let ComponentRecord = new r.Array(new r.Pointer(r.uint16, Anchor), t => t.parent.parent.classCount);
109+
let LigatureAttach = new r.Array(ComponentRecord, r.uint16);
110+
let LigatureArray = new r.Array(new r.Pointer(r.uint16, LigatureAttach), r.uint16);
111+
112+
let GPOSLookup = new r.VersionedStruct('lookupType', {
113+
1: new r.VersionedStruct(r.uint16, { // Single Adjustment
114+
1: { // Single positioning value
115+
coverage: new r.Pointer(r.uint16, Coverage),
116+
valueFormat: ValueFormat,
117+
value: new ValueRecord()
118+
},
119+
2: {
120+
coverage: new r.Pointer(r.uint16, Coverage),
121+
valueFormat: ValueFormat,
122+
valueCount: r.uint16,
123+
values: new r.LazyArray(new ValueRecord(), 'valueCount')
124+
}
125+
}),
126+
127+
2: new r.VersionedStruct(r.uint16, { // Pair Adjustment Positioning
128+
1: { // Adjustments for glyph pairs
129+
coverage: new r.Pointer(r.uint16, Coverage),
130+
valueFormat1: ValueFormat,
131+
valueFormat2: ValueFormat,
132+
pairSetCount: r.uint16,
133+
pairSets: new r.LazyArray(new r.Pointer(r.uint16, PairSet), 'pairSetCount')
134+
},
135+
136+
2: { // Class pair adjustment
137+
coverage: new r.Pointer(r.uint16, Coverage),
138+
valueFormat1: ValueFormat,
139+
valueFormat2: ValueFormat,
140+
classDef1: new r.Pointer(r.uint16, ClassDef),
141+
classDef2: new r.Pointer(r.uint16, ClassDef),
142+
class1Count: r.uint16,
143+
class2Count: r.uint16,
144+
classRecords: new r.LazyArray(new r.LazyArray(Class2Record, 'class2Count'), 'class1Count')
145+
}
146+
}),
147+
148+
3: { // Cursive Attachment Positioning
149+
format: r.uint16,
150+
coverage: new r.Pointer(r.uint16, Coverage),
151+
entryExitCount: r.uint16,
152+
entryExitRecords: new r.Array(EntryExitRecord, 'entryExitCount')
153+
},
154+
155+
4: { // MarkToBase Attachment Positioning
156+
format: r.uint16,
157+
markCoverage: new r.Pointer(r.uint16, Coverage),
158+
baseCoverage: new r.Pointer(r.uint16, Coverage),
159+
classCount: r.uint16,
160+
markArray: new r.Pointer(r.uint16, MarkArray),
161+
baseArray: new r.Pointer(r.uint16, BaseArray)
162+
},
163+
164+
5: { // MarkToLigature Attachment Positioning
165+
format: r.uint16,
166+
markCoverage: new r.Pointer(r.uint16, Coverage),
167+
ligatureCoverage: new r.Pointer(r.uint16, Coverage),
168+
classCount: r.uint16,
169+
markArray: new r.Pointer(r.uint16, MarkArray),
170+
ligatureArray: new r.Pointer(r.uint16, LigatureArray)
171+
},
172+
173+
6: { // MarkToMark Attachment Positioning
174+
format: r.uint16,
175+
mark1Coverage: new r.Pointer(r.uint16, Coverage),
176+
mark2Coverage: new r.Pointer(r.uint16, Coverage),
177+
classCount: r.uint16,
178+
mark1Array: new r.Pointer(r.uint16, MarkArray),
179+
mark2Array: new r.Pointer(r.uint16, BaseArray)
180+
},
181+
182+
7: Context, // Contextual positioning
183+
8: ChainingContext, // Chaining contextual positioning
184+
185+
9: { // Extension Positioning
186+
posFormat: r.uint16,
187+
lookupType: r.uint16, // cannot also be 9
188+
extension: new r.Pointer(r.uint32, undefined)
189+
}
190+
});
191+
192+
// Fix circular reference
193+
GPOSLookup.versions[9].extension.type = GPOSLookup;
194+
195+
export default new r.VersionedStruct(r.uint32, {
196+
header: {
197+
scriptList: new r.Pointer(r.uint16, ScriptList),
198+
featureList: new r.Pointer(r.uint16, FeatureList),
199+
lookupList: new r.Pointer(r.uint16, new LookupList(GPOSLookup))
200+
},
201+
202+
0x00010000: {},
203+
0x00010001: {
204+
featureVariations: new r.Pointer(r.uint32, FeatureVariations)
205+
}
206+
});
207+
208+
// export GPOSLookup for JSTF table
209+
export { GPOSLookup };
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import r from 'restructure';
2+
3+
let shortFrac = new r.Fixed(16, 'BE', 14);
4+
class Offset {
5+
static decode(stream, parent) {
6+
// In short format, offsets are multiplied by 2.
7+
// This doesn't seem to be documented by Apple, but it
8+
// is implemented this way in Freetype.
9+
return parent.flags
10+
? stream.readUInt32BE()
11+
: stream.readUInt16BE() * 2;
12+
}
13+
}
14+
15+
let gvar = new r.Struct({
16+
version: r.uint16,
17+
reserved: new r.Reserved(r.uint16),
18+
axisCount: r.uint16,
19+
globalCoordCount: r.uint16,
20+
globalCoords: new r.Pointer(r.uint32, new r.Array(new r.Array(shortFrac, 'axisCount'), 'globalCoordCount')),
21+
glyphCount: r.uint16,
22+
flags: r.uint16,
23+
offsetToData: r.uint32,
24+
offsets: new r.Array(new r.Pointer(Offset, 'void', { relativeTo: ctx => ctx.offsetToData, allowNull: false }), t => t.glyphCount + 1)
25+
});
26+
27+
export default gvar;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import r from 'restructure';
2+
import { LookupTable, StateTable1 } from './aat';
3+
4+
let ClassTable = new r.Struct({
5+
length: r.uint16,
6+
coverage: r.uint16,
7+
subFeatureFlags: r.uint32,
8+
stateTable: new StateTable1
9+
});
10+
11+
let WidthDeltaRecord = new r.Struct({
12+
justClass: r.uint32,
13+
beforeGrowLimit: r.fixed32,
14+
beforeShrinkLimit: r.fixed32,
15+
afterGrowLimit: r.fixed32,
16+
afterShrinkLimit: r.fixed32,
17+
growFlags: r.uint16,
18+
shrinkFlags: r.uint16
19+
});
20+
21+
let WidthDeltaCluster = new r.Array(WidthDeltaRecord, r.uint32);
22+
23+
let ActionData = new r.VersionedStruct('actionType', {
24+
0: { // Decomposition action
25+
lowerLimit: r.fixed32,
26+
upperLimit: r.fixed32,
27+
order: r.uint16,
28+
glyphs: new r.Array(r.uint16, r.uint16)
29+
},
30+
31+
1: { // Unconditional add glyph action
32+
addGlyph: r.uint16
33+
},
34+
35+
2: { // Conditional add glyph action
36+
substThreshold: r.fixed32,
37+
addGlyph: r.uint16,
38+
substGlyph: r.uint16
39+
},
40+
41+
3: {}, // Stretch glyph action (no data, not supported by CoreText)
42+
43+
4: { // Ductile glyph action (not supported by CoreText)
44+
variationAxis: r.uint32,
45+
minimumLimit: r.fixed32,
46+
noStretchValue: r.fixed32,
47+
maximumLimit: r.fixed32
48+
},
49+
50+
5: { // Repeated add glyph action
51+
flags: r.uint16,
52+
glyph: r.uint16
53+
}
54+
});
55+
56+
let Action = new r.Struct({
57+
actionClass: r.uint16,
58+
actionType: r.uint16,
59+
actionLength: r.uint32,
60+
actionData: ActionData,
61+
padding: new r.Reserved(r.uint8, t => t.actionLength - t._currentOffset)
62+
});
63+
64+
let PostcompensationAction = new r.Array(Action, r.uint32);
65+
let PostCompensationTable = new r.Struct({
66+
lookupTable: new LookupTable(new r.Pointer(r.uint16, PostcompensationAction))
67+
});
68+
69+
let JustificationTable = new r.Struct({
70+
classTable: new r.Pointer(r.uint16, ClassTable, { type: 'parent' }),
71+
wdcOffset: r.uint16,
72+
postCompensationTable: new r.Pointer(r.uint16, PostCompensationTable, { type: 'parent' }),
73+
widthDeltaClusters: new LookupTable(new r.Pointer(r.uint16, WidthDeltaCluster, { type: 'parent', relativeTo: ctx => ctx.wdcOffset }))
74+
});
75+
76+
export default new r.Struct({
77+
version: r.uint32,
78+
format: r.uint16,
79+
horizontal: new r.Pointer(r.uint16, JustificationTable),
80+
vertical: new r.Pointer(r.uint16, JustificationTable)
81+
});

0 commit comments

Comments
 (0)