-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract.js
More file actions
218 lines (203 loc) · 7.19 KB
/
Copy pathextract.js
File metadata and controls
218 lines (203 loc) · 7.19 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
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
import { readFileSync } from 'node:fs';
const pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
const { OPS } = pdfjsLib;
export async function extractLayers(pdfPath, targetLayerNames) {
const data = new Uint8Array(readFileSync(pdfPath));
const doc = await pdfjsLib.getDocument({
data,
disableFontFace: true,
useSystemFonts: false,
isEvalSupported: false,
}).promise;
const page = await doc.getPage(1);
const viewport = page.getViewport({ scale: 1 });
const ocConfig = await doc.getOptionalContentConfig();
const groups = ocConfig.getGroups();
const targetOcgIds = new Set();
const ocgIdToLayerName = new Map();
const allByName = {};
for (const [id, group] of Object.entries(groups)) {
allByName[group.name] = (allByName[group.name] || 0) + 1;
if (targetLayerNames.includes(group.name)) {
targetOcgIds.add(id);
ocgIdToLayerName.set(id, group.name);
}
}
console.log(' OCG name counts:', allByName);
console.log(' Target OCG ids:', [...targetOcgIds]);
const opList = await page.getOperatorList();
let ctm = [1, 0, 0, 1, 0, 0];
const ctmStack = [];
const ocgStack = [];
const paths = [];
const texts = [];
// Text state (per BT/ET; reset on BT)
let Tm = [1, 0, 0, 1, 0, 0];
let Tlm = [1, 0, 0, 1, 0, 0];
let textFontSize = 0;
let textCharSpacing = 0;
let textWordSpacing = 0;
let textHScale = 1;
let textRise = 0;
let textLeading = 0;
const mm = (a, b) => [
a[0]*b[0] + a[2]*b[1],
a[1]*b[0] + a[3]*b[1],
a[0]*b[2] + a[2]*b[3],
a[1]*b[2] + a[3]*b[3],
a[0]*b[4] + a[2]*b[5] + a[4],
a[1]*b[4] + a[3]*b[5] + a[5],
];
const tp = (m, x, y) => [m[0]*x + m[2]*y + m[4], m[1]*x + m[3]*y + m[5]];
const currentLayer = () => {
for (let i = ocgStack.length - 1; i >= 0; i--) {
const id = ocgStack[i];
if (id !== null && ocgIdToLayerName.has(id)) return ocgIdToLayerName.get(id);
}
return null;
};
// Process a text-show operator's glyph array: append to `texts` (when in target),
// and advance Tm by the rendered string's horizontal displacement (text-space units).
const showGlyphs = (glyphs) => {
if (!Array.isArray(glyphs)) return;
let str = '';
let txAdvance = 0;
for (const g of glyphs) {
if (typeof g === 'number') {
// TJ-style kerning offset in 1/1000 em (positive moves backward)
txAdvance += (-g / 1000) * textFontSize * textHScale;
} else if (g && g.unicode !== undefined) {
str += g.unicode;
const wEm = (g.width || 0) / 1000;
const spacing = g.isSpace ? textWordSpacing : textCharSpacing;
txAdvance += (wEm * textFontSize + spacing) * textHScale;
}
}
const layer = currentLayer();
if (str && layer) {
const fontMatrix = [textFontSize * textHScale, 0, 0, textFontSize, 0, textRise];
const transform = mm(mm(ctm, Tm), fontMatrix);
texts.push({ layer, str, transform });
}
Tm = mm(Tm, [1, 0, 0, 1, txAdvance, 0]);
};
for (let i = 0; i < opList.fnArray.length; i++) {
const fn = opList.fnArray[i];
const args = opList.argsArray[i];
if (fn === OPS.save) {
ctmStack.push(ctm.slice());
} else if (fn === OPS.restore) {
ctm = ctmStack.pop() || [1, 0, 0, 1, 0, 0];
} else if (fn === OPS.transform) {
ctm = mm(ctm, args);
} else if (fn === OPS.beginMarkedContent) {
ocgStack.push(null);
} else if (fn === OPS.beginMarkedContentProps) {
const props = args[1];
if (props && (props.type === 'OCG' || props.type === 'OC')) {
ocgStack.push(props.id);
} else {
ocgStack.push(null);
}
} else if (fn === OPS.endMarkedContent) {
ocgStack.pop();
} else if (fn === OPS.beginText) {
Tm = [1, 0, 0, 1, 0, 0];
Tlm = [1, 0, 0, 1, 0, 0];
} else if (fn === OPS.endText) {
// nothing
} else if (fn === OPS.setFont) {
textFontSize = args[1];
} else if (fn === OPS.setTextMatrix) {
Tm = args.slice(0, 6);
Tlm = args.slice(0, 6);
} else if (fn === OPS.moveText) {
Tlm = mm(Tlm, [1, 0, 0, 1, args[0], args[1]]);
Tm = Tlm.slice();
} else if (fn === OPS.setLeadingMoveText) {
textLeading = -args[1];
Tlm = mm(Tlm, [1, 0, 0, 1, args[0], args[1]]);
Tm = Tlm.slice();
} else if (fn === OPS.nextLine) {
Tlm = mm(Tlm, [1, 0, 0, 1, 0, -textLeading]);
Tm = Tlm.slice();
} else if (fn === OPS.setLeading) {
textLeading = args[0];
} else if (fn === OPS.setCharSpacing) {
textCharSpacing = args[0];
} else if (fn === OPS.setWordSpacing) {
textWordSpacing = args[0];
} else if (fn === OPS.setHScale) {
textHScale = args[0] / 100;
} else if (fn === OPS.setTextRise) {
textRise = args[0];
} else if (fn === OPS.showText) {
showGlyphs(args[0]);
} else if (fn === OPS.showSpacedText) {
showGlyphs(args[0]);
} else if (fn === OPS.nextLineShowText) {
Tlm = mm(Tlm, [1, 0, 0, 1, 0, -textLeading]);
Tm = Tlm.slice();
showGlyphs(args[0]);
} else if (fn === OPS.nextLineSetSpacingShowText) {
textWordSpacing = args[0];
textCharSpacing = args[1];
Tlm = mm(Tlm, [1, 0, 0, 1, 0, -textLeading]);
Tm = Tlm.slice();
showGlyphs(args[2]);
} else if (fn === OPS.constructPath) {
const layer = currentLayer();
if (!layer) continue;
const subOps = args[0];
const pts = args[1];
let p = 0;
const cmds = [];
for (const sub of subOps) {
if (sub === OPS.moveTo) {
const [x, y] = tp(ctm, pts[p], pts[p+1]);
cmds.push(['M', x, y]); p += 2;
} else if (sub === OPS.lineTo) {
const [x, y] = tp(ctm, pts[p], pts[p+1]);
cmds.push(['L', x, y]); p += 2;
} else if (sub === OPS.curveTo) {
const [x1, y1] = tp(ctm, pts[p], pts[p+1]);
const [x2, y2] = tp(ctm, pts[p+2], pts[p+3]);
const [x3, y3] = tp(ctm, pts[p+4], pts[p+5]);
cmds.push(['C', x1, y1, x2, y2, x3, y3]); p += 6;
} else if (sub === OPS.curveTo2) {
const [x2, y2] = tp(ctm, pts[p], pts[p+1]);
const [x3, y3] = tp(ctm, pts[p+2], pts[p+3]);
cmds.push(['V', x2, y2, x3, y3]); p += 4;
} else if (sub === OPS.curveTo3) {
const [x1, y1] = tp(ctm, pts[p], pts[p+1]);
const [x3, y3] = tp(ctm, pts[p+2], pts[p+3]);
cmds.push(['Y', x1, y1, x3, y3]); p += 4;
} else if (sub === OPS.closePath) {
cmds.push(['Z']);
} else if (sub === OPS.rectangle) {
const x = pts[p], y = pts[p+1], w = pts[p+2], h = pts[p+3];
const c1 = tp(ctm, x, y);
const c2 = tp(ctm, x + w, y);
const c3 = tp(ctm, x + w, y + h);
const c4 = tp(ctm, x, y + h);
cmds.push(['M', c1[0], c1[1]]);
cmds.push(['L', c2[0], c2[1]]);
cmds.push(['L', c3[0], c3[1]]);
cmds.push(['L', c4[0], c4[1]]);
cmds.push(['Z']);
p += 4;
}
}
if (cmds.length) paths.push({ layer, cmds });
}
}
console.log(` Extracted ${texts.length} text items`);
await doc.cleanup();
await doc.destroy();
return {
width: viewport.width,
height: viewport.height,
paths,
texts,
};
}