-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathfunction.js
More file actions
391 lines (331 loc) · 11.2 KB
/
function.js
File metadata and controls
391 lines (331 loc) · 11.2 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Dict, Ref } from "./primitives.js";
import { FormatError, info, shadow, warn } from "../shared/util.js";
import { BaseStream } from "./base_stream.js";
import { buildPostScriptJsFunction } from "./postscript/js_evaluator.js";
import { buildPostScriptWasmFunction } from "./postscript/wasm_compiler.js";
import { isNumberArray } from "./core_utils.js";
import { LocalFunctionCache } from "./image_utils.js";
import { MathClamp } from "../shared/math_clamp.js";
const FunctionType = {
SAMPLED: 0,
EXPONENTIAL_INTERPOLATION: 2,
STITCHING: 3,
POSTSCRIPT_CALCULATOR: 4,
};
class PDFFunctionFactory {
static #useWasm = true;
static setOptions({ useWasm }) {
this.#useWasm = useWasm;
}
constructor({ xref }) {
this.xref = xref;
}
get useWasm() {
return PDFFunctionFactory.#useWasm;
}
create(fn, parseArray = false) {
let fnRef, parsedFn;
// Check if the Function is cached first, to avoid re-parsing it.
if (fn instanceof Ref) {
fnRef = fn;
} else if (fn instanceof Dict) {
fnRef = fn.objId;
} else if (fn instanceof BaseStream) {
fnRef = fn.dict?.objId;
}
if (fnRef) {
const cachedFn = this._localFunctionCache.getByRef(fnRef);
if (cachedFn) {
return cachedFn;
}
}
const fnObj = this.xref.fetchIfRef(fn);
if (Array.isArray(fnObj)) {
if (!parseArray) {
throw new Error(
'PDFFunctionFactory.create - expected "parseArray" argument.'
);
}
parsedFn = PDFFunction.parseArray(this, fnObj);
} else {
parsedFn = PDFFunction.parse(this, fnObj);
}
// Attempt to cache the parsed Function, by reference.
if (fnRef) {
this._localFunctionCache.set(/* name = */ null, fnRef, parsedFn);
}
return parsedFn;
}
/**
* @private
*/
get _localFunctionCache() {
return shadow(this, "_localFunctionCache", new LocalFunctionCache());
}
}
function toNumberArray(arr) {
if (!Array.isArray(arr)) {
return null;
}
if (!isNumberArray(arr, null)) {
// Non-number is found -- convert all items to numbers.
return arr.map(x => +x);
}
return arr;
}
function getSampleArray(size, outputSize, bps, stream) {
let length = outputSize;
for (const s of size) {
length *= s;
}
const array = new Array(length);
let codeSize = 0;
let codeBuf = 0;
// 32 is a valid bps so shifting won't work
const sampleMul = 1.0 / (2.0 ** bps - 1);
const strBytes = stream.getBytes((length * bps + 7) / 8);
let strIdx = 0;
for (let i = 0; i < length; i++) {
while (codeSize < bps) {
codeBuf <<= 8;
codeBuf |= strBytes[strIdx++];
codeSize += 8;
}
codeSize -= bps;
array[i] = (codeBuf >> codeSize) * sampleMul;
codeBuf &= (1 << codeSize) - 1;
}
return array;
}
class PDFFunction {
static parse(factory, fn) {
const dict = fn.dict || fn;
const typeNum = dict.get("FunctionType");
switch (typeNum) {
case FunctionType.SAMPLED:
return this.constructSampled(factory, fn, dict);
case FunctionType.EXPONENTIAL_INTERPOLATION:
return this.constructInterpolated(factory, dict);
case FunctionType.STITCHING:
return this.constructStiched(factory, dict);
case FunctionType.POSTSCRIPT_CALCULATOR:
return this.constructPostScript(factory, fn, dict);
}
throw new FormatError(`Unknown function type: ${typeNum}`);
}
static parseArray(factory, fnObj) {
const { xref } = factory;
const fnArray = [];
for (const fn of fnObj) {
fnArray.push(this.parse(factory, xref.fetchIfRef(fn)));
}
return function (src, srcOffset, dest, destOffset) {
for (let i = 0, ii = fnArray.length; i < ii; i++) {
fnArray[i](src, srcOffset, dest, destOffset + i);
}
};
}
static constructSampled(factory, fn, dict) {
// See chapter 3, page 109 of the PDF reference
function interpolate(x, xmin, xmax, ymin, ymax) {
return ymin + (x - xmin) * ((ymax - ymin) / (xmax - xmin));
}
const domain = toNumberArray(dict.getArray("Domain"));
const range = toNumberArray(dict.getArray("Range"));
if (!domain || !range) {
throw new FormatError("No domain or range");
}
const inputSize = domain.length / 2;
const outputSize = range.length / 2;
const size = toNumberArray(dict.getArray("Size"));
const bps = dict.get("BitsPerSample");
const order = dict.get("Order") || 1;
if (order !== 1) {
// No description how cubic spline interpolation works in PDF32000:2008
// As in poppler, ignoring order, linear interpolation may work as good
info("No support for cubic spline interpolation: " + order);
}
let encode = toNumberArray(dict.getArray("Encode"));
if (!encode) {
encode = [];
for (let i = 0; i < inputSize; ++i) {
encode.push(0, size[i] - 1);
}
}
const decode = toNumberArray(dict.getArray("Decode")) || range;
const samples = getSampleArray(size, outputSize, bps, fn);
// const mask = 2 ** bps - 1;
return function constructSampledFn(src, srcOffset, dest, destOffset) {
// See chapter 3, page 110 of the PDF reference.
// Building the cube vertices: its part and sample index
// http://rjwagner49.com/Mathematics/Interpolation.pdf
const cubeVertices = 1 << inputSize;
const cubeN = new Float64Array(cubeVertices).fill(1);
const cubeVertex = new Uint32Array(cubeVertices);
let i, j;
let k = outputSize,
pos = 1;
// Map x_i to y_j for 0 <= i < m using the sampled function.
for (i = 0; i < inputSize; ++i) {
// x_i' = min(max(x_i, Domain_2i), Domain_2i+1)
const domain_2i = domain[2 * i];
const domain_2i_1 = domain[2 * i + 1];
const xi = MathClamp(src[srcOffset + i], domain_2i, domain_2i_1);
// e_i = Interpolate(x_i', Domain_2i, Domain_2i+1,
// Encode_2i, Encode_2i+1)
let e = interpolate(
xi,
domain_2i,
domain_2i_1,
encode[2 * i],
encode[2 * i + 1]
);
// e_i' = min(max(e_i, 0), Size_i - 1)
const size_i = size[i];
e = MathClamp(e, 0, size_i - 1);
// Adjusting the cube: N and vertex sample index
const e0 = e < size_i - 1 ? Math.floor(e) : e - 1; // e1 = e0 + 1;
const n0 = e0 + 1 - e; // (e1 - e) / (e1 - e0);
const n1 = e - e0; // (e - e0) / (e1 - e0);
const offset0 = e0 * k;
const offset1 = offset0 + k; // e1 * k
for (j = 0; j < cubeVertices; j++) {
if (j & pos) {
cubeN[j] *= n1;
cubeVertex[j] += offset1;
} else {
cubeN[j] *= n0;
cubeVertex[j] += offset0;
}
}
k *= size_i;
pos <<= 1;
}
for (j = 0; j < outputSize; ++j) {
// Sum all cube vertices' samples portions
let rj = 0;
for (i = 0; i < cubeVertices; i++) {
rj += samples[cubeVertex[i] + j] * cubeN[i];
}
// r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1,
// Decode_2j, Decode_2j+1)
rj = interpolate(rj, 0, 1, decode[2 * j], decode[2 * j + 1]);
// y_j = min(max(r_j, range_2j), range_2j+1)
dest[destOffset + j] = MathClamp(rj, range[2 * j], range[2 * j + 1]);
}
};
}
static constructInterpolated(factory, dict) {
const c0 = toNumberArray(dict.getArray("C0")) || [0];
const c1 = toNumberArray(dict.getArray("C1")) || [1];
const n = dict.get("N");
const domain = toNumberArray(dict.getArray("Domain")) || [0, 1];
const range = toNumberArray(dict.getArray("Range"));
const diff = [];
for (let i = 0, ii = c0.length; i < ii; ++i) {
diff.push(c1[i] - c0[i]);
}
const length = diff.length;
return function constructInterpolatedFn(src, srcOffset, dest, destOffset) {
const clampedX = MathClamp(src[srcOffset], domain[0], domain[1]);
const x = n === 1 ? clampedX : clampedX ** n;
for (let j = 0; j < length; ++j) {
let v = c0[j] + x * diff[j];
if (range) {
v = MathClamp(v, range[2 * j], range[2 * j + 1]);
}
dest[destOffset + j] = v;
}
};
}
static constructStiched(factory, dict) {
const domain = toNumberArray(dict.getArray("Domain"));
if (!domain) {
throw new FormatError("No domain");
}
const inputSize = domain.length / 2;
if (inputSize !== 1) {
throw new FormatError("Bad domain for stiched function");
}
const { xref } = factory;
const fns = [];
for (const fn of dict.get("Functions")) {
fns.push(this.parse(factory, xref.fetchIfRef(fn)));
}
const bounds = toNumberArray(dict.getArray("Bounds"));
const encode = toNumberArray(dict.getArray("Encode"));
const tmpBuf = new Float32Array(1);
return function constructStichedFn(src, srcOffset, dest, destOffset) {
// Clamp to domain.
const v = MathClamp(src[srcOffset], domain[0], domain[1]);
// calculate which bound the value is in
const length = bounds.length;
let i;
for (i = 0; i < length; ++i) {
if (v < bounds[i]) {
break;
}
}
// encode value into domain of function
const dmin = i > 0 ? bounds[i - 1] : domain[0];
const dmax = i < length ? bounds[i] : domain[1];
const rmin = encode[2 * i];
const rmax = encode[2 * i + 1];
// Prevent the value from becoming NaN as a result
// of division by zero (fixes issue6113.pdf).
tmpBuf[0] =
dmin === dmax
? rmin
: rmin + ((v - dmin) * (rmax - rmin)) / (dmax - dmin);
// call the appropriate function
fns[i](tmpBuf, 0, dest, destOffset);
};
}
static constructPostScript(factory, fn, dict) {
const domain = toNumberArray(dict.getArray("Domain"));
const range = toNumberArray(dict.getArray("Range"));
if (!domain) {
throw new FormatError("No domain.");
}
if (!range) {
throw new FormatError("No range.");
}
const psCode = fn.getString();
try {
if (factory.useWasm) {
const wasmFn = buildPostScriptWasmFunction(psCode, domain, range);
if (wasmFn) {
return wasmFn; // (src, srcOffset, dest, destOffset) → void
}
}
} catch {}
warn("Failed to compile PostScript function to wasm, falling back to JS");
return buildPostScriptJsFunction(psCode, domain, range);
}
}
function isPDFFunction(v) {
let fnDict;
if (v instanceof Dict) {
fnDict = v;
} else if (v instanceof BaseStream) {
fnDict = v.dict;
} else {
return false;
}
return fnDict.has("FunctionType");
}
export { FunctionType, getSampleArray, isPDFFunction, PDFFunctionFactory };