@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +33 -18
- package/{src/display → pdfjs/dist}/display_utils.js +344 -139
- package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
- package/pdfjs/dist/get.js +1857 -0
- package/pdfjs/dist/index.js +767 -0
- package/pdfjs/dist/l10n_utils.js +140 -0
- package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
- package/{src/display → pdfjs/dist}/network.js +149 -87
- package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
- package/{src/display → pdfjs/dist}/node_stream.js +133 -98
- package/pdfjs/dist/pdf.js +12778 -0
- package/pdfjs/dist/pdf_link_service.js +638 -0
- package/pdfjs/dist/pdf_rendering_queue.js +199 -0
- package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
- package/pdfjs/dist/pdf_viewer.js +3598 -0
- package/pdfjs/dist/typeof.js +100 -0
- package/pdfjs/dist/ui_utils.js +1033 -0
- package/{src/shared → pdfjs/dist}/util.js +301 -287
- package/pdfjs/dist/worker.js +62813 -0
- package/src/vcf-pdf-viewer.js +77 -27
- package/theme/lumo/vcf-pdf-viewer-styles.js +1 -1
- package/theme/material/vcf-pdf-viewer.js +2 -2
- package/src/core/.eslintrc +0 -13
- package/src/core/annotation.js +0 -2948
- package/src/core/arithmetic_decoder.js +0 -182
- package/src/core/ascii_85_stream.js +0 -98
- package/src/core/ascii_hex_stream.js +0 -79
- package/src/core/base_stream.js +0 -110
- package/src/core/bidi.js +0 -438
- package/src/core/calibri_factors.js +0 -308
- package/src/core/catalog.js +0 -1459
- package/src/core/ccitt.js +0 -1062
- package/src/core/ccitt_stream.js +0 -60
- package/src/core/cff_font.js +0 -116
- package/src/core/cff_parser.js +0 -1949
- package/src/core/charsets.js +0 -119
- package/src/core/chunked_stream.js +0 -557
- package/src/core/cmap.js +0 -1039
- package/src/core/colorspace.js +0 -1533
- package/src/core/core_utils.js +0 -464
- package/src/core/crypto.js +0 -1900
- package/src/core/decode_stream.js +0 -170
- package/src/core/decrypt_stream.js +0 -59
- package/src/core/default_appearance.js +0 -99
- package/src/core/document.js +0 -1456
- package/src/core/encodings.js +0 -301
- package/src/core/evaluator.js +0 -4601
- package/src/core/file_spec.js +0 -108
- package/src/core/flate_stream.js +0 -402
- package/src/core/font_renderer.js +0 -882
- package/src/core/fonts.js +0 -3260
- package/src/core/fonts_utils.js +0 -221
- package/src/core/function.js +0 -1257
- package/src/core/glyf.js +0 -706
- package/src/core/glyphlist.js +0 -4558
- package/src/core/helvetica_factors.js +0 -353
- package/src/core/image.js +0 -802
- package/src/core/image_utils.js +0 -291
- package/src/core/jbig2.js +0 -2572
- package/src/core/jbig2_stream.js +0 -73
- package/src/core/jpeg_stream.js +0 -105
- package/src/core/jpg.js +0 -1416
- package/src/core/jpx.js +0 -2343
- package/src/core/jpx_stream.js +0 -87
- package/src/core/liberationsans_widths.js +0 -221
- package/src/core/lzw_stream.js +0 -150
- package/src/core/metadata_parser.js +0 -146
- package/src/core/metrics.js +0 -2970
- package/src/core/murmurhash3.js +0 -139
- package/src/core/myriadpro_factors.js +0 -290
- package/src/core/name_number_tree.js +0 -153
- package/src/core/object_loader.js +0 -149
- package/src/core/opentype_file_builder.js +0 -154
- package/src/core/operator_list.js +0 -734
- package/src/core/parser.js +0 -1416
- package/src/core/pattern.js +0 -985
- package/src/core/pdf_manager.js +0 -217
- package/src/core/predictor_stream.js +0 -238
- package/src/core/primitives.js +0 -402
- package/src/core/ps_parser.js +0 -272
- package/src/core/run_length_stream.js +0 -61
- package/src/core/segoeui_factors.js +0 -308
- package/src/core/standard_fonts.js +0 -817
- package/src/core/stream.js +0 -103
- package/src/core/struct_tree.js +0 -335
- package/src/core/to_unicode_map.js +0 -103
- package/src/core/type1_font.js +0 -421
- package/src/core/type1_parser.js +0 -776
- package/src/core/unicode.js +0 -1649
- package/src/core/worker.js +0 -848
- package/src/core/worker_stream.js +0 -135
- package/src/core/writer.js +0 -278
- package/src/core/xfa/bind.js +0 -652
- package/src/core/xfa/builder.js +0 -207
- package/src/core/xfa/config.js +0 -1926
- package/src/core/xfa/connection_set.js +0 -202
- package/src/core/xfa/data.js +0 -82
- package/src/core/xfa/datasets.js +0 -76
- package/src/core/xfa/factory.js +0 -111
- package/src/core/xfa/fonts.js +0 -181
- package/src/core/xfa/formcalc_lexer.js +0 -385
- package/src/core/xfa/formcalc_parser.js +0 -1340
- package/src/core/xfa/html_utils.js +0 -639
- package/src/core/xfa/layout.js +0 -383
- package/src/core/xfa/locale_set.js +0 -345
- package/src/core/xfa/namespaces.js +0 -81
- package/src/core/xfa/parser.js +0 -184
- package/src/core/xfa/setup.js +0 -38
- package/src/core/xfa/signature.js +0 -40
- package/src/core/xfa/som.js +0 -338
- package/src/core/xfa/stylesheet.js +0 -40
- package/src/core/xfa/template.js +0 -6260
- package/src/core/xfa/text.js +0 -290
- package/src/core/xfa/unknown.js +0 -29
- package/src/core/xfa/utils.js +0 -217
- package/src/core/xfa/xdp.js +0 -59
- package/src/core/xfa/xfa_object.js +0 -1130
- package/src/core/xfa/xhtml.js +0 -543
- package/src/core/xfa_fonts.js +0 -208
- package/src/core/xml_parser.js +0 -507
- package/src/core/xref.js +0 -899
- package/src/display/annotation_layer.js +0 -2107
- package/src/display/annotation_storage.js +0 -113
- package/src/display/api.js +0 -3292
- package/src/display/base_factory.js +0 -180
- package/src/display/canvas.js +0 -2828
- package/src/display/font_loader.js +0 -484
- package/src/display/metadata.js +0 -41
- package/src/display/network_utils.js +0 -100
- package/src/display/node_utils.js +0 -83
- package/src/display/optional_content_config.js +0 -189
- package/src/display/pattern_helper.js +0 -659
- package/src/display/svg.js +0 -1709
- package/src/display/text_layer.js +0 -847
- package/src/display/transport_stream.js +0 -303
- package/src/display/worker_options.js +0 -40
- package/src/display/xfa_layer.js +0 -204
- package/src/doc_helper.js +0 -25
- package/src/images/logo.svg +0 -41
- package/src/interfaces.js +0 -169
- package/src/license_header.js +0 -14
- package/src/license_header_libre.js +0 -21
- package/src/pdf.image_decoders.js +0 -46
- package/src/pdf.js +0 -146
- package/src/pdf.sandbox.external.js +0 -181
- package/src/pdf.sandbox.js +0 -151
- package/src/pdf.scripting.js +0 -25
- package/src/pdf.worker.entry.js +0 -19
- package/src/pdf.worker.js +0 -23
- package/src/scripting_api/aform.js +0 -608
- package/src/scripting_api/app.js +0 -621
- package/src/scripting_api/color.js +0 -129
- package/src/scripting_api/common.js +0 -58
- package/src/scripting_api/console.js +0 -38
- package/src/scripting_api/constants.js +0 -208
- package/src/scripting_api/doc.js +0 -1195
- package/src/scripting_api/error.js +0 -23
- package/src/scripting_api/event.js +0 -232
- package/src/scripting_api/field.js +0 -620
- package/src/scripting_api/fullscreen.js +0 -145
- package/src/scripting_api/initialization.js +0 -223
- package/src/scripting_api/pdf_object.js +0 -24
- package/src/scripting_api/print_params.js +0 -146
- package/src/scripting_api/proxy.js +0 -139
- package/src/scripting_api/thermometer.js +0 -69
- package/src/scripting_api/util.js +0 -581
- package/src/shared/.eslintrc +0 -13
- package/src/shared/cffStandardStrings.js +0 -311
- package/src/shared/compatibility.js +0 -114
- package/src/shared/fonts_utils.js +0 -429
- package/src/shared/is_node.js +0 -27
- package/src/shared/scripting_utils.js +0 -85
- package/src/worker_loader.js +0 -32
package/src/core/function.js
DELETED
|
@@ -1,1257 +0,0 @@
|
|
|
1
|
-
/* Copyright 2012 Mozilla Foundation
|
|
2
|
-
*
|
|
3
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { Dict, isDict, isStream, Ref } from "./primitives.js";
|
|
17
|
-
import {
|
|
18
|
-
FormatError,
|
|
19
|
-
info,
|
|
20
|
-
isBool,
|
|
21
|
-
IsEvalSupportedCached,
|
|
22
|
-
shadow,
|
|
23
|
-
unreachable,
|
|
24
|
-
} from "../shared/util.js";
|
|
25
|
-
import { PostScriptLexer, PostScriptParser } from "./ps_parser.js";
|
|
26
|
-
import { LocalFunctionCache } from "./image_utils.js";
|
|
27
|
-
|
|
28
|
-
class PDFFunctionFactory {
|
|
29
|
-
constructor({ xref, isEvalSupported = true }) {
|
|
30
|
-
this.xref = xref;
|
|
31
|
-
this.isEvalSupported = isEvalSupported !== false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
create(fn) {
|
|
35
|
-
const cachedFunction = this.getCached(fn);
|
|
36
|
-
if (cachedFunction) {
|
|
37
|
-
return cachedFunction;
|
|
38
|
-
}
|
|
39
|
-
const parsedFunction = PDFFunction.parse({
|
|
40
|
-
xref: this.xref,
|
|
41
|
-
isEvalSupported: this.isEvalSupported,
|
|
42
|
-
fn: fn instanceof Ref ? this.xref.fetch(fn) : fn,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Attempt to cache the parsed Function, by reference.
|
|
46
|
-
this._cache(fn, parsedFunction);
|
|
47
|
-
|
|
48
|
-
return parsedFunction;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
createFromArray(fnObj) {
|
|
52
|
-
const cachedFunction = this.getCached(fnObj);
|
|
53
|
-
if (cachedFunction) {
|
|
54
|
-
return cachedFunction;
|
|
55
|
-
}
|
|
56
|
-
const parsedFunction = PDFFunction.parseArray({
|
|
57
|
-
xref: this.xref,
|
|
58
|
-
isEvalSupported: this.isEvalSupported,
|
|
59
|
-
fnObj: fnObj instanceof Ref ? this.xref.fetch(fnObj) : fnObj,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Attempt to cache the parsed Function, by reference.
|
|
63
|
-
this._cache(fnObj, parsedFunction);
|
|
64
|
-
|
|
65
|
-
return parsedFunction;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
getCached(cacheKey) {
|
|
69
|
-
let fnRef;
|
|
70
|
-
if (cacheKey instanceof Ref) {
|
|
71
|
-
fnRef = cacheKey;
|
|
72
|
-
} else if (cacheKey instanceof Dict) {
|
|
73
|
-
fnRef = cacheKey.objId;
|
|
74
|
-
} else if (isStream(cacheKey)) {
|
|
75
|
-
fnRef = cacheKey.dict && cacheKey.dict.objId;
|
|
76
|
-
}
|
|
77
|
-
if (fnRef) {
|
|
78
|
-
const localFunction = this._localFunctionCache.getByRef(fnRef);
|
|
79
|
-
if (localFunction) {
|
|
80
|
-
return localFunction;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @private
|
|
88
|
-
*/
|
|
89
|
-
_cache(cacheKey, parsedFunction) {
|
|
90
|
-
if (!parsedFunction) {
|
|
91
|
-
throw new Error(
|
|
92
|
-
'PDFFunctionFactory._cache - expected "parsedFunction" argument.'
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
let fnRef;
|
|
96
|
-
if (cacheKey instanceof Ref) {
|
|
97
|
-
fnRef = cacheKey;
|
|
98
|
-
} else if (cacheKey instanceof Dict) {
|
|
99
|
-
fnRef = cacheKey.objId;
|
|
100
|
-
} else if (isStream(cacheKey)) {
|
|
101
|
-
fnRef = cacheKey.dict && cacheKey.dict.objId;
|
|
102
|
-
}
|
|
103
|
-
if (fnRef) {
|
|
104
|
-
this._localFunctionCache.set(/* name = */ null, fnRef, parsedFunction);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* @private
|
|
110
|
-
*/
|
|
111
|
-
get _localFunctionCache() {
|
|
112
|
-
return shadow(this, "_localFunctionCache", new LocalFunctionCache());
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function toNumberArray(arr) {
|
|
117
|
-
if (!Array.isArray(arr)) {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
const length = arr.length;
|
|
121
|
-
for (let i = 0; i < length; i++) {
|
|
122
|
-
if (typeof arr[i] !== "number") {
|
|
123
|
-
// Non-number is found -- convert all items to numbers.
|
|
124
|
-
const result = new Array(length);
|
|
125
|
-
for (let j = 0; j < length; j++) {
|
|
126
|
-
result[j] = +arr[j];
|
|
127
|
-
}
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return arr;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
class PDFFunction {
|
|
135
|
-
static getSampleArray(size, outputSize, bps, stream) {
|
|
136
|
-
let i, ii;
|
|
137
|
-
let length = 1;
|
|
138
|
-
for (i = 0, ii = size.length; i < ii; i++) {
|
|
139
|
-
length *= size[i];
|
|
140
|
-
}
|
|
141
|
-
length *= outputSize;
|
|
142
|
-
|
|
143
|
-
const array = new Array(length);
|
|
144
|
-
let codeSize = 0;
|
|
145
|
-
let codeBuf = 0;
|
|
146
|
-
// 32 is a valid bps so shifting won't work
|
|
147
|
-
const sampleMul = 1.0 / (2.0 ** bps - 1);
|
|
148
|
-
|
|
149
|
-
const strBytes = stream.getBytes((length * bps + 7) / 8);
|
|
150
|
-
let strIdx = 0;
|
|
151
|
-
for (i = 0; i < length; i++) {
|
|
152
|
-
while (codeSize < bps) {
|
|
153
|
-
codeBuf <<= 8;
|
|
154
|
-
codeBuf |= strBytes[strIdx++];
|
|
155
|
-
codeSize += 8;
|
|
156
|
-
}
|
|
157
|
-
codeSize -= bps;
|
|
158
|
-
array[i] = (codeBuf >> codeSize) * sampleMul;
|
|
159
|
-
codeBuf &= (1 << codeSize) - 1;
|
|
160
|
-
}
|
|
161
|
-
return array;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
static parse({ xref, isEvalSupported, fn }) {
|
|
165
|
-
const dict = fn.dict || fn;
|
|
166
|
-
const typeNum = dict.get("FunctionType");
|
|
167
|
-
|
|
168
|
-
switch (typeNum) {
|
|
169
|
-
case 0:
|
|
170
|
-
return this.constructSampled({ xref, isEvalSupported, fn, dict });
|
|
171
|
-
case 1:
|
|
172
|
-
break;
|
|
173
|
-
case 2:
|
|
174
|
-
return this.constructInterpolated({ xref, isEvalSupported, dict });
|
|
175
|
-
case 3:
|
|
176
|
-
return this.constructStiched({ xref, isEvalSupported, dict });
|
|
177
|
-
case 4:
|
|
178
|
-
return this.constructPostScript({ xref, isEvalSupported, fn, dict });
|
|
179
|
-
}
|
|
180
|
-
throw new FormatError("Unknown type of function");
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
static parseArray({ xref, isEvalSupported, fnObj }) {
|
|
184
|
-
if (!Array.isArray(fnObj)) {
|
|
185
|
-
// not an array -- parsing as regular function
|
|
186
|
-
return this.parse({ xref, isEvalSupported, fn: fnObj });
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const fnArray = [];
|
|
190
|
-
for (let j = 0, jj = fnObj.length; j < jj; j++) {
|
|
191
|
-
fnArray.push(
|
|
192
|
-
this.parse({ xref, isEvalSupported, fn: xref.fetchIfRef(fnObj[j]) })
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
return function (src, srcOffset, dest, destOffset) {
|
|
196
|
-
for (let i = 0, ii = fnArray.length; i < ii; i++) {
|
|
197
|
-
fnArray[i](src, srcOffset, dest, destOffset + i);
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
static constructSampled({ xref, isEvalSupported, fn, dict }) {
|
|
203
|
-
function toMultiArray(arr) {
|
|
204
|
-
const inputLength = arr.length;
|
|
205
|
-
const out = [];
|
|
206
|
-
let index = 0;
|
|
207
|
-
for (let i = 0; i < inputLength; i += 2) {
|
|
208
|
-
out[index++] = [arr[i], arr[i + 1]];
|
|
209
|
-
}
|
|
210
|
-
return out;
|
|
211
|
-
}
|
|
212
|
-
// See chapter 3, page 109 of the PDF reference
|
|
213
|
-
function interpolate(x, xmin, xmax, ymin, ymax) {
|
|
214
|
-
return ymin + (x - xmin) * ((ymax - ymin) / (xmax - xmin));
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
let domain = toNumberArray(dict.getArray("Domain"));
|
|
218
|
-
let range = toNumberArray(dict.getArray("Range"));
|
|
219
|
-
|
|
220
|
-
if (!domain || !range) {
|
|
221
|
-
throw new FormatError("No domain or range");
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const inputSize = domain.length / 2;
|
|
225
|
-
const outputSize = range.length / 2;
|
|
226
|
-
|
|
227
|
-
domain = toMultiArray(domain);
|
|
228
|
-
range = toMultiArray(range);
|
|
229
|
-
|
|
230
|
-
const size = toNumberArray(dict.getArray("Size"));
|
|
231
|
-
const bps = dict.get("BitsPerSample");
|
|
232
|
-
const order = dict.get("Order") || 1;
|
|
233
|
-
if (order !== 1) {
|
|
234
|
-
// No description how cubic spline interpolation works in PDF32000:2008
|
|
235
|
-
// As in poppler, ignoring order, linear interpolation may work as good
|
|
236
|
-
info("No support for cubic spline interpolation: " + order);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
let encode = toNumberArray(dict.getArray("Encode"));
|
|
240
|
-
if (!encode) {
|
|
241
|
-
encode = [];
|
|
242
|
-
for (let i = 0; i < inputSize; ++i) {
|
|
243
|
-
encode.push([0, size[i] - 1]);
|
|
244
|
-
}
|
|
245
|
-
} else {
|
|
246
|
-
encode = toMultiArray(encode);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
let decode = toNumberArray(dict.getArray("Decode"));
|
|
250
|
-
if (!decode) {
|
|
251
|
-
decode = range;
|
|
252
|
-
} else {
|
|
253
|
-
decode = toMultiArray(decode);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const samples = this.getSampleArray(size, outputSize, bps, fn);
|
|
257
|
-
// const mask = 2 ** bps - 1;
|
|
258
|
-
|
|
259
|
-
return function constructSampledFn(src, srcOffset, dest, destOffset) {
|
|
260
|
-
// See chapter 3, page 110 of the PDF reference.
|
|
261
|
-
|
|
262
|
-
// Building the cube vertices: its part and sample index
|
|
263
|
-
// http://rjwagner49.com/Mathematics/Interpolation.pdf
|
|
264
|
-
const cubeVertices = 1 << inputSize;
|
|
265
|
-
const cubeN = new Float64Array(cubeVertices);
|
|
266
|
-
const cubeVertex = new Uint32Array(cubeVertices);
|
|
267
|
-
let i, j;
|
|
268
|
-
for (j = 0; j < cubeVertices; j++) {
|
|
269
|
-
cubeN[j] = 1;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
let k = outputSize,
|
|
273
|
-
pos = 1;
|
|
274
|
-
// Map x_i to y_j for 0 <= i < m using the sampled function.
|
|
275
|
-
for (i = 0; i < inputSize; ++i) {
|
|
276
|
-
// x_i' = min(max(x_i, Domain_2i), Domain_2i+1)
|
|
277
|
-
const domain_2i = domain[i][0];
|
|
278
|
-
const domain_2i_1 = domain[i][1];
|
|
279
|
-
const xi = Math.min(
|
|
280
|
-
Math.max(src[srcOffset + i], domain_2i),
|
|
281
|
-
domain_2i_1
|
|
282
|
-
);
|
|
283
|
-
|
|
284
|
-
// e_i = Interpolate(x_i', Domain_2i, Domain_2i+1,
|
|
285
|
-
// Encode_2i, Encode_2i+1)
|
|
286
|
-
let e = interpolate(
|
|
287
|
-
xi,
|
|
288
|
-
domain_2i,
|
|
289
|
-
domain_2i_1,
|
|
290
|
-
encode[i][0],
|
|
291
|
-
encode[i][1]
|
|
292
|
-
);
|
|
293
|
-
|
|
294
|
-
// e_i' = min(max(e_i, 0), Size_i - 1)
|
|
295
|
-
const size_i = size[i];
|
|
296
|
-
e = Math.min(Math.max(e, 0), size_i - 1);
|
|
297
|
-
|
|
298
|
-
// Adjusting the cube: N and vertex sample index
|
|
299
|
-
const e0 = e < size_i - 1 ? Math.floor(e) : e - 1; // e1 = e0 + 1;
|
|
300
|
-
const n0 = e0 + 1 - e; // (e1 - e) / (e1 - e0);
|
|
301
|
-
const n1 = e - e0; // (e - e0) / (e1 - e0);
|
|
302
|
-
const offset0 = e0 * k;
|
|
303
|
-
const offset1 = offset0 + k; // e1 * k
|
|
304
|
-
for (j = 0; j < cubeVertices; j++) {
|
|
305
|
-
if (j & pos) {
|
|
306
|
-
cubeN[j] *= n1;
|
|
307
|
-
cubeVertex[j] += offset1;
|
|
308
|
-
} else {
|
|
309
|
-
cubeN[j] *= n0;
|
|
310
|
-
cubeVertex[j] += offset0;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
k *= size_i;
|
|
315
|
-
pos <<= 1;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
for (j = 0; j < outputSize; ++j) {
|
|
319
|
-
// Sum all cube vertices' samples portions
|
|
320
|
-
let rj = 0;
|
|
321
|
-
for (i = 0; i < cubeVertices; i++) {
|
|
322
|
-
rj += samples[cubeVertex[i] + j] * cubeN[i];
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1,
|
|
326
|
-
// Decode_2j, Decode_2j+1)
|
|
327
|
-
rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]);
|
|
328
|
-
|
|
329
|
-
// y_j = min(max(r_j, range_2j), range_2j+1)
|
|
330
|
-
dest[destOffset + j] = Math.min(Math.max(rj, range[j][0]), range[j][1]);
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
static constructInterpolated({ xref, isEvalSupported, dict }) {
|
|
336
|
-
const c0 = toNumberArray(dict.getArray("C0")) || [0];
|
|
337
|
-
const c1 = toNumberArray(dict.getArray("C1")) || [1];
|
|
338
|
-
const n = dict.get("N");
|
|
339
|
-
|
|
340
|
-
const diff = [];
|
|
341
|
-
for (let i = 0, ii = c0.length; i < ii; ++i) {
|
|
342
|
-
diff.push(c1[i] - c0[i]);
|
|
343
|
-
}
|
|
344
|
-
const length = diff.length;
|
|
345
|
-
|
|
346
|
-
return function constructInterpolatedFn(src, srcOffset, dest, destOffset) {
|
|
347
|
-
const x = n === 1 ? src[srcOffset] : src[srcOffset] ** n;
|
|
348
|
-
|
|
349
|
-
for (let j = 0; j < length; ++j) {
|
|
350
|
-
dest[destOffset + j] = c0[j] + x * diff[j];
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
static constructStiched({ xref, isEvalSupported, dict }) {
|
|
356
|
-
const domain = toNumberArray(dict.getArray("Domain"));
|
|
357
|
-
|
|
358
|
-
if (!domain) {
|
|
359
|
-
throw new FormatError("No domain");
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
const inputSize = domain.length / 2;
|
|
363
|
-
if (inputSize !== 1) {
|
|
364
|
-
throw new FormatError("Bad domain for stiched function");
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
const fnRefs = dict.get("Functions");
|
|
368
|
-
const fns = [];
|
|
369
|
-
for (let i = 0, ii = fnRefs.length; i < ii; ++i) {
|
|
370
|
-
fns.push(
|
|
371
|
-
this.parse({ xref, isEvalSupported, fn: xref.fetchIfRef(fnRefs[i]) })
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
const bounds = toNumberArray(dict.getArray("Bounds"));
|
|
376
|
-
const encode = toNumberArray(dict.getArray("Encode"));
|
|
377
|
-
const tmpBuf = new Float32Array(1);
|
|
378
|
-
|
|
379
|
-
return function constructStichedFn(src, srcOffset, dest, destOffset) {
|
|
380
|
-
const clip = function constructStichedFromIRClip(v, min, max) {
|
|
381
|
-
if (v > max) {
|
|
382
|
-
v = max;
|
|
383
|
-
} else if (v < min) {
|
|
384
|
-
v = min;
|
|
385
|
-
}
|
|
386
|
-
return v;
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
// clip to domain
|
|
390
|
-
const v = clip(src[srcOffset], domain[0], domain[1]);
|
|
391
|
-
// calculate which bound the value is in
|
|
392
|
-
const length = bounds.length;
|
|
393
|
-
let i;
|
|
394
|
-
for (i = 0; i < length; ++i) {
|
|
395
|
-
if (v < bounds[i]) {
|
|
396
|
-
break;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// encode value into domain of function
|
|
401
|
-
let dmin = domain[0];
|
|
402
|
-
if (i > 0) {
|
|
403
|
-
dmin = bounds[i - 1];
|
|
404
|
-
}
|
|
405
|
-
let dmax = domain[1];
|
|
406
|
-
if (i < bounds.length) {
|
|
407
|
-
dmax = bounds[i];
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
const rmin = encode[2 * i];
|
|
411
|
-
const rmax = encode[2 * i + 1];
|
|
412
|
-
|
|
413
|
-
// Prevent the value from becoming NaN as a result
|
|
414
|
-
// of division by zero (fixes issue6113.pdf).
|
|
415
|
-
tmpBuf[0] =
|
|
416
|
-
dmin === dmax
|
|
417
|
-
? rmin
|
|
418
|
-
: rmin + ((v - dmin) * (rmax - rmin)) / (dmax - dmin);
|
|
419
|
-
|
|
420
|
-
// call the appropriate function
|
|
421
|
-
fns[i](tmpBuf, 0, dest, destOffset);
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
static constructPostScript({ xref, isEvalSupported, fn, dict }) {
|
|
426
|
-
const domain = toNumberArray(dict.getArray("Domain"));
|
|
427
|
-
const range = toNumberArray(dict.getArray("Range"));
|
|
428
|
-
|
|
429
|
-
if (!domain) {
|
|
430
|
-
throw new FormatError("No domain.");
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
if (!range) {
|
|
434
|
-
throw new FormatError("No range.");
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
const lexer = new PostScriptLexer(fn);
|
|
438
|
-
const parser = new PostScriptParser(lexer);
|
|
439
|
-
const code = parser.parse();
|
|
440
|
-
|
|
441
|
-
if (isEvalSupported && IsEvalSupportedCached.value) {
|
|
442
|
-
const compiled = new PostScriptCompiler().compile(code, domain, range);
|
|
443
|
-
if (compiled) {
|
|
444
|
-
// Compiled function consists of simple expressions such as addition,
|
|
445
|
-
// subtraction, Math.max, and also contains 'var' and 'return'
|
|
446
|
-
// statements. See the generation in the PostScriptCompiler below.
|
|
447
|
-
// eslint-disable-next-line no-new-func
|
|
448
|
-
return new Function("src", "srcOffset", "dest", "destOffset", compiled);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
info("Unable to compile PS function");
|
|
452
|
-
|
|
453
|
-
const numOutputs = range.length >> 1;
|
|
454
|
-
const numInputs = domain.length >> 1;
|
|
455
|
-
const evaluator = new PostScriptEvaluator(code);
|
|
456
|
-
// Cache the values for a big speed up, the cache size is limited though
|
|
457
|
-
// since the number of possible values can be huge from a PS function.
|
|
458
|
-
const cache = Object.create(null);
|
|
459
|
-
// The MAX_CACHE_SIZE is set to ~4x the maximum number of distinct values
|
|
460
|
-
// seen in our tests.
|
|
461
|
-
const MAX_CACHE_SIZE = 2048 * 4;
|
|
462
|
-
let cache_available = MAX_CACHE_SIZE;
|
|
463
|
-
const tmpBuf = new Float32Array(numInputs);
|
|
464
|
-
|
|
465
|
-
return function constructPostScriptFn(src, srcOffset, dest, destOffset) {
|
|
466
|
-
let i, value;
|
|
467
|
-
let key = "";
|
|
468
|
-
const input = tmpBuf;
|
|
469
|
-
for (i = 0; i < numInputs; i++) {
|
|
470
|
-
value = src[srcOffset + i];
|
|
471
|
-
input[i] = value;
|
|
472
|
-
key += value + "_";
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
const cachedValue = cache[key];
|
|
476
|
-
if (cachedValue !== undefined) {
|
|
477
|
-
dest.set(cachedValue, destOffset);
|
|
478
|
-
return;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
const output = new Float32Array(numOutputs);
|
|
482
|
-
const stack = evaluator.execute(input);
|
|
483
|
-
const stackIndex = stack.length - numOutputs;
|
|
484
|
-
for (i = 0; i < numOutputs; i++) {
|
|
485
|
-
value = stack[stackIndex + i];
|
|
486
|
-
let bound = range[i * 2];
|
|
487
|
-
if (value < bound) {
|
|
488
|
-
value = bound;
|
|
489
|
-
} else {
|
|
490
|
-
bound = range[i * 2 + 1];
|
|
491
|
-
if (value > bound) {
|
|
492
|
-
value = bound;
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
output[i] = value;
|
|
496
|
-
}
|
|
497
|
-
if (cache_available > 0) {
|
|
498
|
-
cache_available--;
|
|
499
|
-
cache[key] = output;
|
|
500
|
-
}
|
|
501
|
-
dest.set(output, destOffset);
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
function isPDFFunction(v) {
|
|
507
|
-
let fnDict;
|
|
508
|
-
if (typeof v !== "object") {
|
|
509
|
-
return false;
|
|
510
|
-
} else if (isDict(v)) {
|
|
511
|
-
fnDict = v;
|
|
512
|
-
} else if (isStream(v)) {
|
|
513
|
-
fnDict = v.dict;
|
|
514
|
-
} else {
|
|
515
|
-
return false;
|
|
516
|
-
}
|
|
517
|
-
return fnDict.has("FunctionType");
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
class PostScriptStack {
|
|
521
|
-
static get MAX_STACK_SIZE() {
|
|
522
|
-
return shadow(this, "MAX_STACK_SIZE", 100);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
constructor(initialStack) {
|
|
526
|
-
this.stack = !initialStack
|
|
527
|
-
? []
|
|
528
|
-
: Array.prototype.slice.call(initialStack, 0);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
push(value) {
|
|
532
|
-
if (this.stack.length >= PostScriptStack.MAX_STACK_SIZE) {
|
|
533
|
-
throw new Error("PostScript function stack overflow.");
|
|
534
|
-
}
|
|
535
|
-
this.stack.push(value);
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
pop() {
|
|
539
|
-
if (this.stack.length <= 0) {
|
|
540
|
-
throw new Error("PostScript function stack underflow.");
|
|
541
|
-
}
|
|
542
|
-
return this.stack.pop();
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
copy(n) {
|
|
546
|
-
if (this.stack.length + n >= PostScriptStack.MAX_STACK_SIZE) {
|
|
547
|
-
throw new Error("PostScript function stack overflow.");
|
|
548
|
-
}
|
|
549
|
-
const stack = this.stack;
|
|
550
|
-
for (let i = stack.length - n, j = n - 1; j >= 0; j--, i++) {
|
|
551
|
-
stack.push(stack[i]);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
index(n) {
|
|
556
|
-
this.push(this.stack[this.stack.length - n - 1]);
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
// rotate the last n stack elements p times
|
|
560
|
-
roll(n, p) {
|
|
561
|
-
const stack = this.stack;
|
|
562
|
-
const l = stack.length - n;
|
|
563
|
-
const r = stack.length - 1;
|
|
564
|
-
const c = l + (p - Math.floor(p / n) * n);
|
|
565
|
-
|
|
566
|
-
for (let i = l, j = r; i < j; i++, j--) {
|
|
567
|
-
const t = stack[i];
|
|
568
|
-
stack[i] = stack[j];
|
|
569
|
-
stack[j] = t;
|
|
570
|
-
}
|
|
571
|
-
for (let i = l, j = c - 1; i < j; i++, j--) {
|
|
572
|
-
const t = stack[i];
|
|
573
|
-
stack[i] = stack[j];
|
|
574
|
-
stack[j] = t;
|
|
575
|
-
}
|
|
576
|
-
for (let i = c, j = r; i < j; i++, j--) {
|
|
577
|
-
const t = stack[i];
|
|
578
|
-
stack[i] = stack[j];
|
|
579
|
-
stack[j] = t;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
class PostScriptEvaluator {
|
|
585
|
-
constructor(operators) {
|
|
586
|
-
this.operators = operators;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
execute(initialStack) {
|
|
590
|
-
const stack = new PostScriptStack(initialStack);
|
|
591
|
-
let counter = 0;
|
|
592
|
-
const operators = this.operators;
|
|
593
|
-
const length = operators.length;
|
|
594
|
-
let operator, a, b;
|
|
595
|
-
while (counter < length) {
|
|
596
|
-
operator = operators[counter++];
|
|
597
|
-
if (typeof operator === "number") {
|
|
598
|
-
// Operator is really an operand and should be pushed to the stack.
|
|
599
|
-
stack.push(operator);
|
|
600
|
-
continue;
|
|
601
|
-
}
|
|
602
|
-
switch (operator) {
|
|
603
|
-
// non standard ps operators
|
|
604
|
-
case "jz": // jump if false
|
|
605
|
-
b = stack.pop();
|
|
606
|
-
a = stack.pop();
|
|
607
|
-
if (!a) {
|
|
608
|
-
counter = b;
|
|
609
|
-
}
|
|
610
|
-
break;
|
|
611
|
-
case "j": // jump
|
|
612
|
-
a = stack.pop();
|
|
613
|
-
counter = a;
|
|
614
|
-
break;
|
|
615
|
-
|
|
616
|
-
// all ps operators in alphabetical order (excluding if/ifelse)
|
|
617
|
-
case "abs":
|
|
618
|
-
a = stack.pop();
|
|
619
|
-
stack.push(Math.abs(a));
|
|
620
|
-
break;
|
|
621
|
-
case "add":
|
|
622
|
-
b = stack.pop();
|
|
623
|
-
a = stack.pop();
|
|
624
|
-
stack.push(a + b);
|
|
625
|
-
break;
|
|
626
|
-
case "and":
|
|
627
|
-
b = stack.pop();
|
|
628
|
-
a = stack.pop();
|
|
629
|
-
if (isBool(a) && isBool(b)) {
|
|
630
|
-
stack.push(a && b);
|
|
631
|
-
} else {
|
|
632
|
-
stack.push(a & b);
|
|
633
|
-
}
|
|
634
|
-
break;
|
|
635
|
-
case "atan":
|
|
636
|
-
a = stack.pop();
|
|
637
|
-
stack.push(Math.atan(a));
|
|
638
|
-
break;
|
|
639
|
-
case "bitshift":
|
|
640
|
-
b = stack.pop();
|
|
641
|
-
a = stack.pop();
|
|
642
|
-
if (a > 0) {
|
|
643
|
-
stack.push(a << b);
|
|
644
|
-
} else {
|
|
645
|
-
stack.push(a >> b);
|
|
646
|
-
}
|
|
647
|
-
break;
|
|
648
|
-
case "ceiling":
|
|
649
|
-
a = stack.pop();
|
|
650
|
-
stack.push(Math.ceil(a));
|
|
651
|
-
break;
|
|
652
|
-
case "copy":
|
|
653
|
-
a = stack.pop();
|
|
654
|
-
stack.copy(a);
|
|
655
|
-
break;
|
|
656
|
-
case "cos":
|
|
657
|
-
a = stack.pop();
|
|
658
|
-
stack.push(Math.cos(a));
|
|
659
|
-
break;
|
|
660
|
-
case "cvi":
|
|
661
|
-
a = stack.pop() | 0;
|
|
662
|
-
stack.push(a);
|
|
663
|
-
break;
|
|
664
|
-
case "cvr":
|
|
665
|
-
// noop
|
|
666
|
-
break;
|
|
667
|
-
case "div":
|
|
668
|
-
b = stack.pop();
|
|
669
|
-
a = stack.pop();
|
|
670
|
-
stack.push(a / b);
|
|
671
|
-
break;
|
|
672
|
-
case "dup":
|
|
673
|
-
stack.copy(1);
|
|
674
|
-
break;
|
|
675
|
-
case "eq":
|
|
676
|
-
b = stack.pop();
|
|
677
|
-
a = stack.pop();
|
|
678
|
-
stack.push(a === b);
|
|
679
|
-
break;
|
|
680
|
-
case "exch":
|
|
681
|
-
stack.roll(2, 1);
|
|
682
|
-
break;
|
|
683
|
-
case "exp":
|
|
684
|
-
b = stack.pop();
|
|
685
|
-
a = stack.pop();
|
|
686
|
-
stack.push(a ** b);
|
|
687
|
-
break;
|
|
688
|
-
case "false":
|
|
689
|
-
stack.push(false);
|
|
690
|
-
break;
|
|
691
|
-
case "floor":
|
|
692
|
-
a = stack.pop();
|
|
693
|
-
stack.push(Math.floor(a));
|
|
694
|
-
break;
|
|
695
|
-
case "ge":
|
|
696
|
-
b = stack.pop();
|
|
697
|
-
a = stack.pop();
|
|
698
|
-
stack.push(a >= b);
|
|
699
|
-
break;
|
|
700
|
-
case "gt":
|
|
701
|
-
b = stack.pop();
|
|
702
|
-
a = stack.pop();
|
|
703
|
-
stack.push(a > b);
|
|
704
|
-
break;
|
|
705
|
-
case "idiv":
|
|
706
|
-
b = stack.pop();
|
|
707
|
-
a = stack.pop();
|
|
708
|
-
stack.push((a / b) | 0);
|
|
709
|
-
break;
|
|
710
|
-
case "index":
|
|
711
|
-
a = stack.pop();
|
|
712
|
-
stack.index(a);
|
|
713
|
-
break;
|
|
714
|
-
case "le":
|
|
715
|
-
b = stack.pop();
|
|
716
|
-
a = stack.pop();
|
|
717
|
-
stack.push(a <= b);
|
|
718
|
-
break;
|
|
719
|
-
case "ln":
|
|
720
|
-
a = stack.pop();
|
|
721
|
-
stack.push(Math.log(a));
|
|
722
|
-
break;
|
|
723
|
-
case "log":
|
|
724
|
-
a = stack.pop();
|
|
725
|
-
stack.push(Math.log(a) / Math.LN10);
|
|
726
|
-
break;
|
|
727
|
-
case "lt":
|
|
728
|
-
b = stack.pop();
|
|
729
|
-
a = stack.pop();
|
|
730
|
-
stack.push(a < b);
|
|
731
|
-
break;
|
|
732
|
-
case "mod":
|
|
733
|
-
b = stack.pop();
|
|
734
|
-
a = stack.pop();
|
|
735
|
-
stack.push(a % b);
|
|
736
|
-
break;
|
|
737
|
-
case "mul":
|
|
738
|
-
b = stack.pop();
|
|
739
|
-
a = stack.pop();
|
|
740
|
-
stack.push(a * b);
|
|
741
|
-
break;
|
|
742
|
-
case "ne":
|
|
743
|
-
b = stack.pop();
|
|
744
|
-
a = stack.pop();
|
|
745
|
-
stack.push(a !== b);
|
|
746
|
-
break;
|
|
747
|
-
case "neg":
|
|
748
|
-
a = stack.pop();
|
|
749
|
-
stack.push(-a);
|
|
750
|
-
break;
|
|
751
|
-
case "not":
|
|
752
|
-
a = stack.pop();
|
|
753
|
-
if (isBool(a)) {
|
|
754
|
-
stack.push(!a);
|
|
755
|
-
} else {
|
|
756
|
-
stack.push(~a);
|
|
757
|
-
}
|
|
758
|
-
break;
|
|
759
|
-
case "or":
|
|
760
|
-
b = stack.pop();
|
|
761
|
-
a = stack.pop();
|
|
762
|
-
if (isBool(a) && isBool(b)) {
|
|
763
|
-
stack.push(a || b);
|
|
764
|
-
} else {
|
|
765
|
-
stack.push(a | b);
|
|
766
|
-
}
|
|
767
|
-
break;
|
|
768
|
-
case "pop":
|
|
769
|
-
stack.pop();
|
|
770
|
-
break;
|
|
771
|
-
case "roll":
|
|
772
|
-
b = stack.pop();
|
|
773
|
-
a = stack.pop();
|
|
774
|
-
stack.roll(a, b);
|
|
775
|
-
break;
|
|
776
|
-
case "round":
|
|
777
|
-
a = stack.pop();
|
|
778
|
-
stack.push(Math.round(a));
|
|
779
|
-
break;
|
|
780
|
-
case "sin":
|
|
781
|
-
a = stack.pop();
|
|
782
|
-
stack.push(Math.sin(a));
|
|
783
|
-
break;
|
|
784
|
-
case "sqrt":
|
|
785
|
-
a = stack.pop();
|
|
786
|
-
stack.push(Math.sqrt(a));
|
|
787
|
-
break;
|
|
788
|
-
case "sub":
|
|
789
|
-
b = stack.pop();
|
|
790
|
-
a = stack.pop();
|
|
791
|
-
stack.push(a - b);
|
|
792
|
-
break;
|
|
793
|
-
case "true":
|
|
794
|
-
stack.push(true);
|
|
795
|
-
break;
|
|
796
|
-
case "truncate":
|
|
797
|
-
a = stack.pop();
|
|
798
|
-
a = a < 0 ? Math.ceil(a) : Math.floor(a);
|
|
799
|
-
stack.push(a);
|
|
800
|
-
break;
|
|
801
|
-
case "xor":
|
|
802
|
-
b = stack.pop();
|
|
803
|
-
a = stack.pop();
|
|
804
|
-
if (isBool(a) && isBool(b)) {
|
|
805
|
-
stack.push(a !== b);
|
|
806
|
-
} else {
|
|
807
|
-
stack.push(a ^ b);
|
|
808
|
-
}
|
|
809
|
-
break;
|
|
810
|
-
default:
|
|
811
|
-
throw new FormatError(`Unknown operator ${operator}`);
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
return stack.stack;
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
// Most of the PDFs functions consist of simple operations such as:
|
|
819
|
-
// roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.
|
|
820
|
-
//
|
|
821
|
-
// We can compile most of such programs, and at the same moment, we can
|
|
822
|
-
// optimize some expressions using basic math properties. Keeping track of
|
|
823
|
-
// min/max values will allow us to avoid extra Math.min/Math.max calls.
|
|
824
|
-
const PostScriptCompiler = (function PostScriptCompilerClosure() {
|
|
825
|
-
class AstNode {
|
|
826
|
-
constructor(type) {
|
|
827
|
-
this.type = type;
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
visit(visitor) {
|
|
831
|
-
unreachable("abstract method");
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
class AstArgument extends AstNode {
|
|
836
|
-
constructor(index, min, max) {
|
|
837
|
-
super("args");
|
|
838
|
-
this.index = index;
|
|
839
|
-
this.min = min;
|
|
840
|
-
this.max = max;
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
visit(visitor) {
|
|
844
|
-
visitor.visitArgument(this);
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
class AstLiteral extends AstNode {
|
|
849
|
-
constructor(number) {
|
|
850
|
-
super("literal");
|
|
851
|
-
this.number = number;
|
|
852
|
-
this.min = number;
|
|
853
|
-
this.max = number;
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
visit(visitor) {
|
|
857
|
-
visitor.visitLiteral(this);
|
|
858
|
-
}
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
class AstBinaryOperation extends AstNode {
|
|
862
|
-
constructor(op, arg1, arg2, min, max) {
|
|
863
|
-
super("binary");
|
|
864
|
-
this.op = op;
|
|
865
|
-
this.arg1 = arg1;
|
|
866
|
-
this.arg2 = arg2;
|
|
867
|
-
this.min = min;
|
|
868
|
-
this.max = max;
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
visit(visitor) {
|
|
872
|
-
visitor.visitBinaryOperation(this);
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
class AstMin extends AstNode {
|
|
877
|
-
constructor(arg, max) {
|
|
878
|
-
super("max");
|
|
879
|
-
this.arg = arg;
|
|
880
|
-
this.min = arg.min;
|
|
881
|
-
this.max = max;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
visit(visitor) {
|
|
885
|
-
visitor.visitMin(this);
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
class AstVariable extends AstNode {
|
|
890
|
-
constructor(index, min, max) {
|
|
891
|
-
super("var");
|
|
892
|
-
this.index = index;
|
|
893
|
-
this.min = min;
|
|
894
|
-
this.max = max;
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
visit(visitor) {
|
|
898
|
-
visitor.visitVariable(this);
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
class AstVariableDefinition extends AstNode {
|
|
903
|
-
constructor(variable, arg) {
|
|
904
|
-
super("definition");
|
|
905
|
-
this.variable = variable;
|
|
906
|
-
this.arg = arg;
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
visit(visitor) {
|
|
910
|
-
visitor.visitVariableDefinition(this);
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
class ExpressionBuilderVisitor {
|
|
915
|
-
constructor() {
|
|
916
|
-
this.parts = [];
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
visitArgument(arg) {
|
|
920
|
-
this.parts.push(
|
|
921
|
-
"Math.max(",
|
|
922
|
-
arg.min,
|
|
923
|
-
", Math.min(",
|
|
924
|
-
arg.max,
|
|
925
|
-
", src[srcOffset + ",
|
|
926
|
-
arg.index,
|
|
927
|
-
"]))"
|
|
928
|
-
);
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
visitVariable(variable) {
|
|
932
|
-
this.parts.push("v", variable.index);
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
visitLiteral(literal) {
|
|
936
|
-
this.parts.push(literal.number);
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
visitBinaryOperation(operation) {
|
|
940
|
-
this.parts.push("(");
|
|
941
|
-
operation.arg1.visit(this);
|
|
942
|
-
this.parts.push(" ", operation.op, " ");
|
|
943
|
-
operation.arg2.visit(this);
|
|
944
|
-
this.parts.push(")");
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
visitVariableDefinition(definition) {
|
|
948
|
-
this.parts.push("var ");
|
|
949
|
-
definition.variable.visit(this);
|
|
950
|
-
this.parts.push(" = ");
|
|
951
|
-
definition.arg.visit(this);
|
|
952
|
-
this.parts.push(";");
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
visitMin(max) {
|
|
956
|
-
this.parts.push("Math.min(");
|
|
957
|
-
max.arg.visit(this);
|
|
958
|
-
this.parts.push(", ", max.max, ")");
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
toString() {
|
|
962
|
-
return this.parts.join("");
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
function buildAddOperation(num1, num2) {
|
|
967
|
-
if (num2.type === "literal" && num2.number === 0) {
|
|
968
|
-
// optimization: second operand is 0
|
|
969
|
-
return num1;
|
|
970
|
-
}
|
|
971
|
-
if (num1.type === "literal" && num1.number === 0) {
|
|
972
|
-
// optimization: first operand is 0
|
|
973
|
-
return num2;
|
|
974
|
-
}
|
|
975
|
-
if (num2.type === "literal" && num1.type === "literal") {
|
|
976
|
-
// optimization: operands operand are literals
|
|
977
|
-
return new AstLiteral(num1.number + num2.number);
|
|
978
|
-
}
|
|
979
|
-
return new AstBinaryOperation(
|
|
980
|
-
"+",
|
|
981
|
-
num1,
|
|
982
|
-
num2,
|
|
983
|
-
num1.min + num2.min,
|
|
984
|
-
num1.max + num2.max
|
|
985
|
-
);
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
function buildMulOperation(num1, num2) {
|
|
989
|
-
if (num2.type === "literal") {
|
|
990
|
-
// optimization: second operands is a literal...
|
|
991
|
-
if (num2.number === 0) {
|
|
992
|
-
return new AstLiteral(0); // and it's 0
|
|
993
|
-
} else if (num2.number === 1) {
|
|
994
|
-
return num1; // and it's 1
|
|
995
|
-
} else if (num1.type === "literal") {
|
|
996
|
-
// ... and first operands is a literal too
|
|
997
|
-
return new AstLiteral(num1.number * num2.number);
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
if (num1.type === "literal") {
|
|
1001
|
-
// optimization: first operands is a literal...
|
|
1002
|
-
if (num1.number === 0) {
|
|
1003
|
-
return new AstLiteral(0); // and it's 0
|
|
1004
|
-
} else if (num1.number === 1) {
|
|
1005
|
-
return num2; // and it's 1
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
const min = Math.min(
|
|
1009
|
-
num1.min * num2.min,
|
|
1010
|
-
num1.min * num2.max,
|
|
1011
|
-
num1.max * num2.min,
|
|
1012
|
-
num1.max * num2.max
|
|
1013
|
-
);
|
|
1014
|
-
const max = Math.max(
|
|
1015
|
-
num1.min * num2.min,
|
|
1016
|
-
num1.min * num2.max,
|
|
1017
|
-
num1.max * num2.min,
|
|
1018
|
-
num1.max * num2.max
|
|
1019
|
-
);
|
|
1020
|
-
return new AstBinaryOperation("*", num1, num2, min, max);
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
function buildSubOperation(num1, num2) {
|
|
1024
|
-
if (num2.type === "literal") {
|
|
1025
|
-
// optimization: second operands is a literal...
|
|
1026
|
-
if (num2.number === 0) {
|
|
1027
|
-
return num1; // ... and it's 0
|
|
1028
|
-
} else if (num1.type === "literal") {
|
|
1029
|
-
// ... and first operands is a literal too
|
|
1030
|
-
return new AstLiteral(num1.number - num2.number);
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
if (
|
|
1034
|
-
num2.type === "binary" &&
|
|
1035
|
-
num2.op === "-" &&
|
|
1036
|
-
num1.type === "literal" &&
|
|
1037
|
-
num1.number === 1 &&
|
|
1038
|
-
num2.arg1.type === "literal" &&
|
|
1039
|
-
num2.arg1.number === 1
|
|
1040
|
-
) {
|
|
1041
|
-
// optimization for case: 1 - (1 - x)
|
|
1042
|
-
return num2.arg2;
|
|
1043
|
-
}
|
|
1044
|
-
return new AstBinaryOperation(
|
|
1045
|
-
"-",
|
|
1046
|
-
num1,
|
|
1047
|
-
num2,
|
|
1048
|
-
num1.min - num2.max,
|
|
1049
|
-
num1.max - num2.min
|
|
1050
|
-
);
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
function buildMinOperation(num1, max) {
|
|
1054
|
-
if (num1.min >= max) {
|
|
1055
|
-
// optimization: num1 min value is not less than required max
|
|
1056
|
-
return new AstLiteral(max); // just returning max
|
|
1057
|
-
} else if (num1.max <= max) {
|
|
1058
|
-
// optimization: num1 max value is not greater than required max
|
|
1059
|
-
return num1; // just returning an argument
|
|
1060
|
-
}
|
|
1061
|
-
return new AstMin(num1, max);
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
// eslint-disable-next-line no-shadow
|
|
1065
|
-
class PostScriptCompiler {
|
|
1066
|
-
compile(code, domain, range) {
|
|
1067
|
-
const stack = [];
|
|
1068
|
-
const instructions = [];
|
|
1069
|
-
const inputSize = domain.length >> 1,
|
|
1070
|
-
outputSize = range.length >> 1;
|
|
1071
|
-
let lastRegister = 0;
|
|
1072
|
-
let n, j;
|
|
1073
|
-
let num1, num2, ast1, ast2, tmpVar, item;
|
|
1074
|
-
for (let i = 0; i < inputSize; i++) {
|
|
1075
|
-
stack.push(new AstArgument(i, domain[i * 2], domain[i * 2 + 1]));
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
|
-
for (let i = 0, ii = code.length; i < ii; i++) {
|
|
1079
|
-
item = code[i];
|
|
1080
|
-
if (typeof item === "number") {
|
|
1081
|
-
stack.push(new AstLiteral(item));
|
|
1082
|
-
continue;
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
switch (item) {
|
|
1086
|
-
case "add":
|
|
1087
|
-
if (stack.length < 2) {
|
|
1088
|
-
return null;
|
|
1089
|
-
}
|
|
1090
|
-
num2 = stack.pop();
|
|
1091
|
-
num1 = stack.pop();
|
|
1092
|
-
stack.push(buildAddOperation(num1, num2));
|
|
1093
|
-
break;
|
|
1094
|
-
case "cvr":
|
|
1095
|
-
if (stack.length < 1) {
|
|
1096
|
-
return null;
|
|
1097
|
-
}
|
|
1098
|
-
break;
|
|
1099
|
-
case "mul":
|
|
1100
|
-
if (stack.length < 2) {
|
|
1101
|
-
return null;
|
|
1102
|
-
}
|
|
1103
|
-
num2 = stack.pop();
|
|
1104
|
-
num1 = stack.pop();
|
|
1105
|
-
stack.push(buildMulOperation(num1, num2));
|
|
1106
|
-
break;
|
|
1107
|
-
case "sub":
|
|
1108
|
-
if (stack.length < 2) {
|
|
1109
|
-
return null;
|
|
1110
|
-
}
|
|
1111
|
-
num2 = stack.pop();
|
|
1112
|
-
num1 = stack.pop();
|
|
1113
|
-
stack.push(buildSubOperation(num1, num2));
|
|
1114
|
-
break;
|
|
1115
|
-
case "exch":
|
|
1116
|
-
if (stack.length < 2) {
|
|
1117
|
-
return null;
|
|
1118
|
-
}
|
|
1119
|
-
ast1 = stack.pop();
|
|
1120
|
-
ast2 = stack.pop();
|
|
1121
|
-
stack.push(ast1, ast2);
|
|
1122
|
-
break;
|
|
1123
|
-
case "pop":
|
|
1124
|
-
if (stack.length < 1) {
|
|
1125
|
-
return null;
|
|
1126
|
-
}
|
|
1127
|
-
stack.pop();
|
|
1128
|
-
break;
|
|
1129
|
-
case "index":
|
|
1130
|
-
if (stack.length < 1) {
|
|
1131
|
-
return null;
|
|
1132
|
-
}
|
|
1133
|
-
num1 = stack.pop();
|
|
1134
|
-
if (num1.type !== "literal") {
|
|
1135
|
-
return null;
|
|
1136
|
-
}
|
|
1137
|
-
n = num1.number;
|
|
1138
|
-
if (n < 0 || !Number.isInteger(n) || stack.length < n) {
|
|
1139
|
-
return null;
|
|
1140
|
-
}
|
|
1141
|
-
ast1 = stack[stack.length - n - 1];
|
|
1142
|
-
if (ast1.type === "literal" || ast1.type === "var") {
|
|
1143
|
-
stack.push(ast1);
|
|
1144
|
-
break;
|
|
1145
|
-
}
|
|
1146
|
-
tmpVar = new AstVariable(lastRegister++, ast1.min, ast1.max);
|
|
1147
|
-
stack[stack.length - n - 1] = tmpVar;
|
|
1148
|
-
stack.push(tmpVar);
|
|
1149
|
-
instructions.push(new AstVariableDefinition(tmpVar, ast1));
|
|
1150
|
-
break;
|
|
1151
|
-
case "dup":
|
|
1152
|
-
if (stack.length < 1) {
|
|
1153
|
-
return null;
|
|
1154
|
-
}
|
|
1155
|
-
if (
|
|
1156
|
-
typeof code[i + 1] === "number" &&
|
|
1157
|
-
code[i + 2] === "gt" &&
|
|
1158
|
-
code[i + 3] === i + 7 &&
|
|
1159
|
-
code[i + 4] === "jz" &&
|
|
1160
|
-
code[i + 5] === "pop" &&
|
|
1161
|
-
code[i + 6] === code[i + 1]
|
|
1162
|
-
) {
|
|
1163
|
-
// special case of the commands sequence for the min operation
|
|
1164
|
-
num1 = stack.pop();
|
|
1165
|
-
stack.push(buildMinOperation(num1, code[i + 1]));
|
|
1166
|
-
i += 6;
|
|
1167
|
-
break;
|
|
1168
|
-
}
|
|
1169
|
-
ast1 = stack[stack.length - 1];
|
|
1170
|
-
if (ast1.type === "literal" || ast1.type === "var") {
|
|
1171
|
-
// we don't have to save into intermediate variable a literal or
|
|
1172
|
-
// variable.
|
|
1173
|
-
stack.push(ast1);
|
|
1174
|
-
break;
|
|
1175
|
-
}
|
|
1176
|
-
tmpVar = new AstVariable(lastRegister++, ast1.min, ast1.max);
|
|
1177
|
-
stack[stack.length - 1] = tmpVar;
|
|
1178
|
-
stack.push(tmpVar);
|
|
1179
|
-
instructions.push(new AstVariableDefinition(tmpVar, ast1));
|
|
1180
|
-
break;
|
|
1181
|
-
case "roll":
|
|
1182
|
-
if (stack.length < 2) {
|
|
1183
|
-
return null;
|
|
1184
|
-
}
|
|
1185
|
-
num2 = stack.pop();
|
|
1186
|
-
num1 = stack.pop();
|
|
1187
|
-
if (num2.type !== "literal" || num1.type !== "literal") {
|
|
1188
|
-
// both roll operands must be numbers
|
|
1189
|
-
return null;
|
|
1190
|
-
}
|
|
1191
|
-
j = num2.number;
|
|
1192
|
-
n = num1.number;
|
|
1193
|
-
if (
|
|
1194
|
-
n <= 0 ||
|
|
1195
|
-
!Number.isInteger(n) ||
|
|
1196
|
-
!Number.isInteger(j) ||
|
|
1197
|
-
stack.length < n
|
|
1198
|
-
) {
|
|
1199
|
-
// ... and integers
|
|
1200
|
-
return null;
|
|
1201
|
-
}
|
|
1202
|
-
j = ((j % n) + n) % n;
|
|
1203
|
-
if (j === 0) {
|
|
1204
|
-
break; // just skipping -- there are nothing to rotate
|
|
1205
|
-
}
|
|
1206
|
-
Array.prototype.push.apply(
|
|
1207
|
-
stack,
|
|
1208
|
-
stack.splice(stack.length - n, n - j)
|
|
1209
|
-
);
|
|
1210
|
-
break;
|
|
1211
|
-
default:
|
|
1212
|
-
return null; // unsupported operator
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
|
|
1216
|
-
if (stack.length !== outputSize) {
|
|
1217
|
-
return null;
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
const result = [];
|
|
1221
|
-
for (const instruction of instructions) {
|
|
1222
|
-
const statementBuilder = new ExpressionBuilderVisitor();
|
|
1223
|
-
instruction.visit(statementBuilder);
|
|
1224
|
-
result.push(statementBuilder.toString());
|
|
1225
|
-
}
|
|
1226
|
-
for (let i = 0, ii = stack.length; i < ii; i++) {
|
|
1227
|
-
const expr = stack[i],
|
|
1228
|
-
statementBuilder = new ExpressionBuilderVisitor();
|
|
1229
|
-
expr.visit(statementBuilder);
|
|
1230
|
-
const min = range[i * 2],
|
|
1231
|
-
max = range[i * 2 + 1];
|
|
1232
|
-
const out = [statementBuilder.toString()];
|
|
1233
|
-
if (min > expr.min) {
|
|
1234
|
-
out.unshift("Math.max(", min, ", ");
|
|
1235
|
-
out.push(")");
|
|
1236
|
-
}
|
|
1237
|
-
if (max < expr.max) {
|
|
1238
|
-
out.unshift("Math.min(", max, ", ");
|
|
1239
|
-
out.push(")");
|
|
1240
|
-
}
|
|
1241
|
-
out.unshift("dest[destOffset + ", i, "] = ");
|
|
1242
|
-
out.push(";");
|
|
1243
|
-
result.push(out.join(""));
|
|
1244
|
-
}
|
|
1245
|
-
return result.join("\n");
|
|
1246
|
-
}
|
|
1247
|
-
}
|
|
1248
|
-
|
|
1249
|
-
return PostScriptCompiler;
|
|
1250
|
-
})();
|
|
1251
|
-
|
|
1252
|
-
export {
|
|
1253
|
-
isPDFFunction,
|
|
1254
|
-
PDFFunctionFactory,
|
|
1255
|
-
PostScriptCompiler,
|
|
1256
|
-
PostScriptEvaluator,
|
|
1257
|
-
};
|