@poe-platform/safe-bash 0.1.772 → 0.1.773
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/dist/image-ast/index.browser.js +2801 -556
- package/dist/image-ast/index.js +2801 -556
- package/dist/pdf-ast/ast.d.ts +7 -0
- package/dist/pdf-ast/content/evaluator.d.ts +1 -0
- package/dist/pdf-ast/index.js +2845 -568
- package/dist/pdf-ast/render/raster.d.ts +1 -0
- package/dist/pdf-ast/vendor/pdfjs-fonts.d.mts +24 -0
- package/dist/pdf-ast/vendor/pdfjs-fonts.mjs +2621 -0
- package/dist/safe-bash/command-chunks/{chunk-M2XU2YCK.js → chunk-5VXZSNM2.js} +2 -2
- package/dist/safe-bash/command-chunks/{chunk-C4AAJTXF.js → chunk-B2MM5DIT.js} +2804 -556
- package/dist/safe-bash/command-chunks/{chunk-NBENXODJ.js → chunk-G3NVK25P.js} +1 -1
- package/dist/safe-bash/core.browser.js +38 -38
- package/dist/safe-bash/core.d.ts +1 -1
- package/dist/safe-bash/plugins/composition.d.ts +2 -0
- package/dist/safe-bash/plugins/composition.js +2 -1
- package/dist/safe-bash-command-ffmpeg/index.js +10968 -9719
- package/dist/safe-bash-command-gh/index.d.ts +1 -0
- package/dist/safe-bash-command-gh/index.js +43 -13
- package/dist/safe-bash-command-gh/types.d.ts +1 -0
- package/dist/safe-bash-command-git/git_rust.wasm +0 -0
- package/dist/safe-bash-command-git/index.js +22 -15
- package/dist/safe-bash-command-git/wasm.generated.js +1 -1
- package/dist/safe-bash-command-imagemagick/index.js +2 -2
- package/dist/safe-bash-command-pandoc/command.js +3 -3
- package/dist/safe-bash-command-pandoc/index.js +3 -3
- package/dist/safe-bash-command-pdfimages/index.js +2800 -510
- package/dist/safe-bash-command-pdfinfo/index.js +2842 -568
- package/dist/safe-bash-command-pdftk/index.js +2800 -510
- package/dist/safe-bash-command-pdftoppm/index.js +2847 -637
- package/dist/safe-bash-command-pdftotext/index.js +2800 -510
- package/dist/safe-bash-command-qpdf/index.js +2800 -510
- package/dist/safe-bash-command-sips/index.js +2 -2
- package/dist/safe-bash-command-soffice/index.js +1 -1
- package/dist/safe-bash-command-ssconvert/index.js +2811 -566
- package/dist/safe-bash-command-wkhtmltopdf/index.js +2833 -510
- package/dist/safe-bash-mcp/index.d.ts +8 -2
- package/dist/safe-bash-mcp/index.js +13 -6
- package/package.json +7 -7
- package/third-party/pdf-ast/THIRD_PARTY_NOTICES.md +62 -1
|
@@ -19321,15 +19321,2635 @@ var PageViewport = class _PageViewport {
|
|
|
19321
19321
|
return p;
|
|
19322
19322
|
}
|
|
19323
19323
|
};
|
|
19324
|
+
|
|
19325
|
+
// src/core/colorspace.js
|
|
19326
|
+
function resizeRgbImage(src, dest, w1, h1, w2, h2, alpha01) {
|
|
19327
|
+
const COMPONENTS = 3;
|
|
19328
|
+
alpha01 = alpha01 !== 1 ? 0 : alpha01;
|
|
19329
|
+
const xRatio = w1 / w2;
|
|
19330
|
+
const yRatio = h1 / h2;
|
|
19331
|
+
let newIndex = 0, oldIndex;
|
|
19332
|
+
const xScaled = new Uint16Array(w2);
|
|
19333
|
+
const w1Scanline = w1 * COMPONENTS;
|
|
19334
|
+
for (let i = 0; i < w2; i++) {
|
|
19335
|
+
xScaled[i] = Math.floor(i * xRatio) * COMPONENTS;
|
|
19336
|
+
}
|
|
19337
|
+
for (let i = 0; i < h2; i++) {
|
|
19338
|
+
const py = Math.floor(i * yRatio) * w1Scanline;
|
|
19339
|
+
for (let j = 0; j < w2; j++) {
|
|
19340
|
+
oldIndex = py + xScaled[j];
|
|
19341
|
+
dest[newIndex++] = src[oldIndex++];
|
|
19342
|
+
dest[newIndex++] = src[oldIndex++];
|
|
19343
|
+
dest[newIndex++] = src[oldIndex++];
|
|
19344
|
+
newIndex += alpha01;
|
|
19345
|
+
}
|
|
19346
|
+
}
|
|
19347
|
+
}
|
|
19348
|
+
function isDefaultDecodeHelper(decode, expectedLen) {
|
|
19349
|
+
if (!Array.isArray(decode)) {
|
|
19350
|
+
return true;
|
|
19351
|
+
}
|
|
19352
|
+
const decodeLen = decode.length;
|
|
19353
|
+
if (decodeLen < expectedLen) {
|
|
19354
|
+
warn("Decode map length is too short.");
|
|
19355
|
+
return true;
|
|
19356
|
+
}
|
|
19357
|
+
if (decodeLen > expectedLen) {
|
|
19358
|
+
info("Truncating too long decode map.");
|
|
19359
|
+
decode.length = expectedLen;
|
|
19360
|
+
}
|
|
19361
|
+
return false;
|
|
19362
|
+
}
|
|
19363
|
+
var ColorSpace = class _ColorSpace {
|
|
19364
|
+
static #rgbBuf = new Uint8ClampedArray(3);
|
|
19365
|
+
constructor(name, numComps) {
|
|
19366
|
+
if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && this.constructor === _ColorSpace) {
|
|
19367
|
+
unreachable("Cannot initialize ColorSpace.");
|
|
19368
|
+
}
|
|
19369
|
+
this.name = name;
|
|
19370
|
+
this.numComps = numComps;
|
|
19371
|
+
}
|
|
19372
|
+
/**
|
|
19373
|
+
* Converts the color value to the RGB color. The color components are
|
|
19374
|
+
* located in the src array starting from the srcOffset. Returns the array
|
|
19375
|
+
* of the rgb components, each value ranging from [0,255].
|
|
19376
|
+
*/
|
|
19377
|
+
getRgb(src, srcOffset, output = new Uint8ClampedArray(3)) {
|
|
19378
|
+
this.getRgbItem(src, srcOffset, output, 0);
|
|
19379
|
+
return output;
|
|
19380
|
+
}
|
|
19381
|
+
getRgbHex(src, srcOffset) {
|
|
19382
|
+
const buffer = this.getRgb(src, srcOffset, _ColorSpace.#rgbBuf);
|
|
19383
|
+
return Util.makeHexColor(buffer[0], buffer[1], buffer[2]);
|
|
19384
|
+
}
|
|
19385
|
+
/**
|
|
19386
|
+
* Converts the color value to the RGB color, similar to the getRgb method.
|
|
19387
|
+
* The result placed into the dest array starting from the destOffset.
|
|
19388
|
+
*/
|
|
19389
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
19390
|
+
unreachable("Should not call ColorSpace.getRgbItem");
|
|
19391
|
+
}
|
|
19392
|
+
/**
|
|
19393
|
+
* Converts the specified number of the color values to the RGB colors.
|
|
19394
|
+
* The colors are located in the src array starting from the srcOffset.
|
|
19395
|
+
* The result is placed into the dest array starting from the destOffset.
|
|
19396
|
+
* The src array items shall be in [0,2^bits) range, the dest array items
|
|
19397
|
+
* will be in [0,255] range. alpha01 indicates how many alpha components
|
|
19398
|
+
* there are in the dest array; it will be either 0 (RGB array) or 1 (RGBA
|
|
19399
|
+
* array).
|
|
19400
|
+
*/
|
|
19401
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
19402
|
+
unreachable("Should not call ColorSpace.getRgbBuffer");
|
|
19403
|
+
}
|
|
19404
|
+
/**
|
|
19405
|
+
* Converts `count` unscaled colors to RGB, starting at `destOffset`.
|
|
19406
|
+
* Components use the native color-space ranges expected by `getRgbItem`,
|
|
19407
|
+
* and each output has a `3 + alpha01` byte stride.
|
|
19408
|
+
* Subclasses may override this to batch expensive conversions.
|
|
19409
|
+
*/
|
|
19410
|
+
getRgbItems(src, count, dest, destOffset, alpha01) {
|
|
19411
|
+
const { numComps } = this;
|
|
19412
|
+
for (let i = 0, srcOffset = 0; i < count; i++, srcOffset += numComps) {
|
|
19413
|
+
this.getRgbItem(src, srcOffset, dest, destOffset);
|
|
19414
|
+
destOffset += 3 + alpha01;
|
|
19415
|
+
}
|
|
19416
|
+
}
|
|
19417
|
+
/**
|
|
19418
|
+
* Returns true if source data will be equal the result/output data.
|
|
19419
|
+
*/
|
|
19420
|
+
isPassthrough(bits) {
|
|
19421
|
+
return false;
|
|
19422
|
+
}
|
|
19423
|
+
/**
|
|
19424
|
+
* Refer to the static `ColorSpace.isDefaultDecode` method below.
|
|
19425
|
+
*/
|
|
19426
|
+
isDefaultDecode(decode, bpc) {
|
|
19427
|
+
return _ColorSpace.isDefaultDecode(decode, this.numComps);
|
|
19428
|
+
}
|
|
19429
|
+
/**
|
|
19430
|
+
* Fills in the RGB colors in the destination buffer. alpha01 indicates
|
|
19431
|
+
* how many alpha components there are in the dest array; it will be either
|
|
19432
|
+
* 0 (RGB array) or 1 (RGBA array).
|
|
19433
|
+
*/
|
|
19434
|
+
fillRgb(dest, originalWidth, originalHeight, width, height, actualHeight, bpc, comps, alpha01) {
|
|
19435
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19436
|
+
assert(
|
|
19437
|
+
dest instanceof Uint8ClampedArray,
|
|
19438
|
+
'ColorSpace.fillRgb: Unsupported "dest" type.'
|
|
19439
|
+
);
|
|
19440
|
+
}
|
|
19441
|
+
const count = originalWidth * originalHeight;
|
|
19442
|
+
let rgbBuf = null;
|
|
19443
|
+
const numComponentColors = 1 << bpc;
|
|
19444
|
+
const needsResizing = originalHeight !== height || originalWidth !== width;
|
|
19445
|
+
if (this.isPassthrough(bpc)) {
|
|
19446
|
+
rgbBuf = comps;
|
|
19447
|
+
} else if (this.numComps === 1 && count > numComponentColors && this.name !== "DeviceGray" && this.name !== "DeviceRGB") {
|
|
19448
|
+
const allColors = bpc <= 8 ? new Uint8Array(numComponentColors) : new Uint16Array(numComponentColors);
|
|
19449
|
+
for (let i = 0; i < numComponentColors; i++) {
|
|
19450
|
+
allColors[i] = i;
|
|
19451
|
+
}
|
|
19452
|
+
const colorMap = new Uint8ClampedArray(numComponentColors * 3);
|
|
19453
|
+
this.getRgbBuffer(
|
|
19454
|
+
allColors,
|
|
19455
|
+
0,
|
|
19456
|
+
numComponentColors,
|
|
19457
|
+
colorMap,
|
|
19458
|
+
0,
|
|
19459
|
+
bpc,
|
|
19460
|
+
/* alpha01 = */
|
|
19461
|
+
0
|
|
19462
|
+
);
|
|
19463
|
+
if (!needsResizing) {
|
|
19464
|
+
let destPos = 0;
|
|
19465
|
+
for (let i = 0; i < count; ++i) {
|
|
19466
|
+
const key = comps[i] * 3;
|
|
19467
|
+
dest[destPos++] = colorMap[key];
|
|
19468
|
+
dest[destPos++] = colorMap[key + 1];
|
|
19469
|
+
dest[destPos++] = colorMap[key + 2];
|
|
19470
|
+
destPos += alpha01;
|
|
19471
|
+
}
|
|
19472
|
+
} else {
|
|
19473
|
+
rgbBuf = new Uint8Array(count * 3);
|
|
19474
|
+
let rgbPos = 0;
|
|
19475
|
+
for (let i = 0; i < count; ++i) {
|
|
19476
|
+
const key = comps[i] * 3;
|
|
19477
|
+
rgbBuf[rgbPos++] = colorMap[key];
|
|
19478
|
+
rgbBuf[rgbPos++] = colorMap[key + 1];
|
|
19479
|
+
rgbBuf[rgbPos++] = colorMap[key + 2];
|
|
19480
|
+
}
|
|
19481
|
+
}
|
|
19482
|
+
} else if (!needsResizing) {
|
|
19483
|
+
this.getRgbBuffer(comps, 0, width * actualHeight, dest, 0, bpc, alpha01);
|
|
19484
|
+
} else {
|
|
19485
|
+
rgbBuf = new Uint8ClampedArray(count * 3);
|
|
19486
|
+
this.getRgbBuffer(
|
|
19487
|
+
comps,
|
|
19488
|
+
0,
|
|
19489
|
+
count,
|
|
19490
|
+
rgbBuf,
|
|
19491
|
+
0,
|
|
19492
|
+
bpc,
|
|
19493
|
+
/* alpha01 = */
|
|
19494
|
+
0
|
|
19495
|
+
);
|
|
19496
|
+
}
|
|
19497
|
+
if (rgbBuf) {
|
|
19498
|
+
if (needsResizing) {
|
|
19499
|
+
resizeRgbImage(
|
|
19500
|
+
rgbBuf,
|
|
19501
|
+
dest,
|
|
19502
|
+
originalWidth,
|
|
19503
|
+
originalHeight,
|
|
19504
|
+
width,
|
|
19505
|
+
height,
|
|
19506
|
+
alpha01
|
|
19507
|
+
);
|
|
19508
|
+
} else {
|
|
19509
|
+
let destPos = 0, rgbPos = 0;
|
|
19510
|
+
for (let i = 0, ii = width * actualHeight; i < ii; i++) {
|
|
19511
|
+
dest[destPos++] = rgbBuf[rgbPos++];
|
|
19512
|
+
dest[destPos++] = rgbBuf[rgbPos++];
|
|
19513
|
+
dest[destPos++] = rgbBuf[rgbPos++];
|
|
19514
|
+
destPos += alpha01;
|
|
19515
|
+
}
|
|
19516
|
+
}
|
|
19517
|
+
}
|
|
19518
|
+
}
|
|
19519
|
+
/**
|
|
19520
|
+
* True if the colorspace has components in the default range of [0, 1].
|
|
19521
|
+
* This should be true for all colorspaces except for lab color spaces
|
|
19522
|
+
* which are [0,100], [-128, 127], [-128, 127].
|
|
19523
|
+
*/
|
|
19524
|
+
get usesZeroToOneRange() {
|
|
19525
|
+
return shadow(this, "usesZeroToOneRange", true);
|
|
19526
|
+
}
|
|
19527
|
+
/**
|
|
19528
|
+
* Checks if a decode map matches the default decode map for a color space.
|
|
19529
|
+
* This handles the general decode maps where there are two values per
|
|
19530
|
+
* component, e.g. [0, 1, 0, 1, 0, 1] for a RGB color.
|
|
19531
|
+
* This does not handle Lab, Indexed, or Pattern decode maps since they are
|
|
19532
|
+
* slightly different.
|
|
19533
|
+
* @param {Array} decode - Decode map (usually from an image).
|
|
19534
|
+
* @param {number} numComps - Number of components the color space has.
|
|
19535
|
+
*/
|
|
19536
|
+
static isDefaultDecode(decode, numComps) {
|
|
19537
|
+
if (isDefaultDecodeHelper(decode, numComps * 2)) {
|
|
19538
|
+
return true;
|
|
19539
|
+
}
|
|
19540
|
+
for (let i = 0, ii = decode.length; i < ii; i += 2) {
|
|
19541
|
+
if (decode[i] !== 0 || decode[i + 1] !== 1) {
|
|
19542
|
+
return false;
|
|
19543
|
+
}
|
|
19544
|
+
}
|
|
19545
|
+
return true;
|
|
19546
|
+
}
|
|
19547
|
+
};
|
|
19548
|
+
var DeviceCmykCS = class extends ColorSpace {
|
|
19549
|
+
constructor() {
|
|
19550
|
+
super("DeviceCMYK", 4);
|
|
19551
|
+
}
|
|
19552
|
+
// The coefficients below was found using numerical analysis: the method of
|
|
19553
|
+
// steepest descent for the sum((f_i - color_value_i)^2) for r/g/b colors,
|
|
19554
|
+
// where color_value is the tabular value from the table of sampled RGB colors
|
|
19555
|
+
// from CMYK US Web Coated (SWOP) colorspace, and f_i is the corresponding
|
|
19556
|
+
// CMYK color conversion using the estimation below:
|
|
19557
|
+
// f(A, B,.. N) = Acc+Bcm+Ccy+Dck+c+Fmm+Gmy+Hmk+Im+Jyy+Kyk+Ly+Mkk+Nk+255
|
|
19558
|
+
#toRgb(src, srcOffset, srcScale, dest, destOffset) {
|
|
19559
|
+
const c = src[srcOffset] * srcScale;
|
|
19560
|
+
const m = src[srcOffset + 1] * srcScale;
|
|
19561
|
+
const y = src[srcOffset + 2] * srcScale;
|
|
19562
|
+
const k = src[srcOffset + 3] * srcScale;
|
|
19563
|
+
dest[destOffset] = 255 + c * (-4.387332384609988 * c + 54.48615194189176 * m + 18.82290502165302 * y + 212.25662451639585 * k + -285.2331026137004) + m * (1.7149763477362134 * m - 5.6096736904047315 * y + -17.873870861415444 * k - 5.497006427196366) + y * (-2.5217340131683033 * y - 21.248923337353073 * k + 17.5119270841813) + k * (-21.86122147463605 * k - 189.48180835922747);
|
|
19564
|
+
dest[destOffset + 1] = 255 + c * (8.841041422036149 * c + 60.118027045597366 * m + 6.871425592049007 * y + 31.159100130055922 * k + -79.2970844816548) + m * (-15.310361306967817 * m + 17.575251261109482 * y + 131.35250912493976 * k - 190.9453302588951) + y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) + k * (-20.737325471181034 * k - 187.80453709719578);
|
|
19565
|
+
dest[destOffset + 2] = 255 + c * (0.8842522430003296 * c + 8.078677503112928 * m + 30.89978309703729 * y - 0.23883238689178934 * k + -14.183576799673286) + m * (10.49593273432072 * m + 63.02378494754052 * y + 50.606957656360734 * k - 112.23884253719248) + y * (0.03296041114873217 * y + 115.60384449646641 * k + -193.58209356861505) + k * (-22.33816807309886 * k - 180.12613974708367);
|
|
19566
|
+
}
|
|
19567
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
19568
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19569
|
+
assert(
|
|
19570
|
+
dest instanceof Uint8ClampedArray,
|
|
19571
|
+
'DeviceCmykCS.getRgbItem: Unsupported "dest" type.'
|
|
19572
|
+
);
|
|
19573
|
+
}
|
|
19574
|
+
this.#toRgb(src, srcOffset, 1, dest, destOffset);
|
|
19575
|
+
}
|
|
19576
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
19577
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19578
|
+
assert(
|
|
19579
|
+
dest instanceof Uint8ClampedArray,
|
|
19580
|
+
'DeviceCmykCS.getRgbBuffer: Unsupported "dest" type.'
|
|
19581
|
+
);
|
|
19582
|
+
}
|
|
19583
|
+
const scale = 1 / ((1 << bits) - 1);
|
|
19584
|
+
for (let i = 0; i < count; i++) {
|
|
19585
|
+
this.#toRgb(src, srcOffset, scale, dest, destOffset);
|
|
19586
|
+
srcOffset += 4;
|
|
19587
|
+
destOffset += 3 + alpha01;
|
|
19588
|
+
}
|
|
19589
|
+
}
|
|
19590
|
+
};
|
|
19591
|
+
var CalGrayCS = class extends ColorSpace {
|
|
19592
|
+
constructor(whitePoint, blackPoint, gamma) {
|
|
19593
|
+
super("CalGray", 1);
|
|
19594
|
+
if (!whitePoint) {
|
|
19595
|
+
throw new FormatError(
|
|
19596
|
+
"WhitePoint missing - required for color space CalGray"
|
|
19597
|
+
);
|
|
19598
|
+
}
|
|
19599
|
+
[this.XW, this.YW, this.ZW] = whitePoint;
|
|
19600
|
+
[this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
|
|
19601
|
+
this.G = gamma || 1;
|
|
19602
|
+
if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
|
|
19603
|
+
throw new FormatError(
|
|
19604
|
+
`Invalid WhitePoint components for ${this.name}, no fallback available`
|
|
19605
|
+
);
|
|
19606
|
+
}
|
|
19607
|
+
if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
|
|
19608
|
+
info(`Invalid BlackPoint for ${this.name}, falling back to default.`);
|
|
19609
|
+
this.XB = this.YB = this.ZB = 0;
|
|
19610
|
+
}
|
|
19611
|
+
if (this.XB !== 0 || this.YB !== 0 || this.ZB !== 0) {
|
|
19612
|
+
warn(
|
|
19613
|
+
`${this.name}, BlackPoint: XB: ${this.XB}, YB: ${this.YB}, ZB: ${this.ZB}, only default values are supported.`
|
|
19614
|
+
);
|
|
19615
|
+
}
|
|
19616
|
+
if (this.G < 1) {
|
|
19617
|
+
info(
|
|
19618
|
+
`Invalid Gamma: ${this.G} for ${this.name}, falling back to default.`
|
|
19619
|
+
);
|
|
19620
|
+
this.G = 1;
|
|
19621
|
+
}
|
|
19622
|
+
}
|
|
19623
|
+
#toRgb(src, srcOffset, dest, destOffset, scale) {
|
|
19624
|
+
const A = src[srcOffset] * scale;
|
|
19625
|
+
const AG = A ** this.G;
|
|
19626
|
+
const L = this.YW * AG;
|
|
19627
|
+
const val = Math.max(295.8 * L ** 0.3333333333333333 - 40.8, 0);
|
|
19628
|
+
dest[destOffset] = val;
|
|
19629
|
+
dest[destOffset + 1] = val;
|
|
19630
|
+
dest[destOffset + 2] = val;
|
|
19631
|
+
}
|
|
19632
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
19633
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19634
|
+
assert(
|
|
19635
|
+
dest instanceof Uint8ClampedArray,
|
|
19636
|
+
'CalGrayCS.getRgbItem: Unsupported "dest" type.'
|
|
19637
|
+
);
|
|
19638
|
+
}
|
|
19639
|
+
this.#toRgb(src, srcOffset, dest, destOffset, 1);
|
|
19640
|
+
}
|
|
19641
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
19642
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19643
|
+
assert(
|
|
19644
|
+
dest instanceof Uint8ClampedArray,
|
|
19645
|
+
'CalGrayCS.getRgbBuffer: Unsupported "dest" type.'
|
|
19646
|
+
);
|
|
19647
|
+
}
|
|
19648
|
+
const scale = 1 / ((1 << bits) - 1);
|
|
19649
|
+
for (let i = 0; i < count; ++i) {
|
|
19650
|
+
this.#toRgb(src, srcOffset, dest, destOffset, scale);
|
|
19651
|
+
srcOffset += 1;
|
|
19652
|
+
destOffset += 3 + alpha01;
|
|
19653
|
+
}
|
|
19654
|
+
}
|
|
19655
|
+
};
|
|
19656
|
+
var CalRGBCS = class _CalRGBCS extends ColorSpace {
|
|
19657
|
+
// See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html for these
|
|
19658
|
+
// matrices.
|
|
19659
|
+
// prettier-ignore
|
|
19660
|
+
static #BRADFORD_SCALE_MATRIX = new Float32Array([
|
|
19661
|
+
0.8951,
|
|
19662
|
+
0.2664,
|
|
19663
|
+
-0.1614,
|
|
19664
|
+
-0.7502,
|
|
19665
|
+
1.7135,
|
|
19666
|
+
0.0367,
|
|
19667
|
+
0.0389,
|
|
19668
|
+
-0.0685,
|
|
19669
|
+
1.0296
|
|
19670
|
+
]);
|
|
19671
|
+
// prettier-ignore
|
|
19672
|
+
static #BRADFORD_SCALE_INVERSE_MATRIX = new Float32Array([
|
|
19673
|
+
0.9869929,
|
|
19674
|
+
-0.1470543,
|
|
19675
|
+
0.1599627,
|
|
19676
|
+
0.4323053,
|
|
19677
|
+
0.5183603,
|
|
19678
|
+
0.0492912,
|
|
19679
|
+
-85287e-7,
|
|
19680
|
+
0.0400428,
|
|
19681
|
+
0.9684867
|
|
19682
|
+
]);
|
|
19683
|
+
// See http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html.
|
|
19684
|
+
// prettier-ignore
|
|
19685
|
+
static #SRGB_D65_XYZ_TO_RGB_MATRIX = new Float32Array([
|
|
19686
|
+
3.2404542,
|
|
19687
|
+
-1.5371385,
|
|
19688
|
+
-0.4985314,
|
|
19689
|
+
-0.969266,
|
|
19690
|
+
1.8760108,
|
|
19691
|
+
0.041556,
|
|
19692
|
+
0.0556434,
|
|
19693
|
+
-0.2040259,
|
|
19694
|
+
1.0572252
|
|
19695
|
+
]);
|
|
19696
|
+
static #FLAT_WHITEPOINT_MATRIX = new Float32Array([1, 1, 1]);
|
|
19697
|
+
static #tempNormalizeMatrix = new Float32Array(3);
|
|
19698
|
+
static #tempConvertMatrix1 = new Float32Array(3);
|
|
19699
|
+
static #tempConvertMatrix2 = new Float32Array(3);
|
|
19700
|
+
static #DECODE_L_CONSTANT = ((8 + 16) / 116) ** 3 / 8;
|
|
19701
|
+
constructor(whitePoint, blackPoint, gamma, matrix) {
|
|
19702
|
+
super("CalRGB", 3);
|
|
19703
|
+
if (!whitePoint) {
|
|
19704
|
+
throw new FormatError(
|
|
19705
|
+
"WhitePoint missing - required for color space CalRGB"
|
|
19706
|
+
);
|
|
19707
|
+
}
|
|
19708
|
+
const [XW, YW, ZW] = this.whitePoint = whitePoint;
|
|
19709
|
+
const [XB, YB, ZB] = this.blackPoint = blackPoint || new Float32Array(3);
|
|
19710
|
+
[this.GR, this.GG, this.GB] = gamma || new Float32Array([1, 1, 1]);
|
|
19711
|
+
[
|
|
19712
|
+
this.MXA,
|
|
19713
|
+
this.MYA,
|
|
19714
|
+
this.MZA,
|
|
19715
|
+
this.MXB,
|
|
19716
|
+
this.MYB,
|
|
19717
|
+
this.MZB,
|
|
19718
|
+
this.MXC,
|
|
19719
|
+
this.MYC,
|
|
19720
|
+
this.MZC
|
|
19721
|
+
] = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
|
|
19722
|
+
if (XW < 0 || ZW < 0 || YW !== 1) {
|
|
19723
|
+
throw new FormatError(
|
|
19724
|
+
`Invalid WhitePoint components for ${this.name}, no fallback available`
|
|
19725
|
+
);
|
|
19726
|
+
}
|
|
19727
|
+
if (XB < 0 || YB < 0 || ZB < 0) {
|
|
19728
|
+
info(
|
|
19729
|
+
`Invalid BlackPoint for ${this.name} [${XB}, ${YB}, ${ZB}], falling back to default.`
|
|
19730
|
+
);
|
|
19731
|
+
this.blackPoint = new Float32Array(3);
|
|
19732
|
+
}
|
|
19733
|
+
if (this.GR < 0 || this.GG < 0 || this.GB < 0) {
|
|
19734
|
+
info(
|
|
19735
|
+
`Invalid Gamma [${this.GR}, ${this.GG}, ${this.GB}] for ${this.name}, falling back to default.`
|
|
19736
|
+
);
|
|
19737
|
+
this.GR = this.GG = this.GB = 1;
|
|
19738
|
+
}
|
|
19739
|
+
}
|
|
19740
|
+
#matrixProduct(a, b, result) {
|
|
19741
|
+
result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
19742
|
+
result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
|
|
19743
|
+
result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
|
|
19744
|
+
}
|
|
19745
|
+
#toFlat(sourceWhitePoint, LMS, result) {
|
|
19746
|
+
result[0] = LMS[0] * 1 / sourceWhitePoint[0];
|
|
19747
|
+
result[1] = LMS[1] * 1 / sourceWhitePoint[1];
|
|
19748
|
+
result[2] = LMS[2] * 1 / sourceWhitePoint[2];
|
|
19749
|
+
}
|
|
19750
|
+
#toD65(sourceWhitePoint, LMS, result) {
|
|
19751
|
+
const D65X = 0.95047;
|
|
19752
|
+
const D65Y = 1;
|
|
19753
|
+
const D65Z = 1.08883;
|
|
19754
|
+
result[0] = LMS[0] * D65X / sourceWhitePoint[0];
|
|
19755
|
+
result[1] = LMS[1] * D65Y / sourceWhitePoint[1];
|
|
19756
|
+
result[2] = LMS[2] * D65Z / sourceWhitePoint[2];
|
|
19757
|
+
}
|
|
19758
|
+
#sRGBTransferFunction(color) {
|
|
19759
|
+
if (color <= 31308e-7) {
|
|
19760
|
+
return MathClamp(12.92 * color, 0, 1);
|
|
19761
|
+
}
|
|
19762
|
+
return color >= 0.99554525 ? 1 : MathClamp((1 + 0.055) * color ** (1 / 2.4) - 0.055, 0, 1);
|
|
19763
|
+
}
|
|
19764
|
+
#decodeL(L) {
|
|
19765
|
+
if (L < 0) {
|
|
19766
|
+
return -this.#decodeL(-L);
|
|
19767
|
+
}
|
|
19768
|
+
return L > 8 ? ((L + 16) / 116) ** 3 : L * _CalRGBCS.#DECODE_L_CONSTANT;
|
|
19769
|
+
}
|
|
19770
|
+
#compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {
|
|
19771
|
+
if (sourceBlackPoint[0] === 0 && sourceBlackPoint[1] === 0 && sourceBlackPoint[2] === 0) {
|
|
19772
|
+
result[0] = XYZ_Flat[0];
|
|
19773
|
+
result[1] = XYZ_Flat[1];
|
|
19774
|
+
result[2] = XYZ_Flat[2];
|
|
19775
|
+
return;
|
|
19776
|
+
}
|
|
19777
|
+
const zeroDecodeL = this.#decodeL(0);
|
|
19778
|
+
const X_DST = zeroDecodeL;
|
|
19779
|
+
const X_SRC = this.#decodeL(sourceBlackPoint[0]);
|
|
19780
|
+
const Y_DST = zeroDecodeL;
|
|
19781
|
+
const Y_SRC = this.#decodeL(sourceBlackPoint[1]);
|
|
19782
|
+
const Z_DST = zeroDecodeL;
|
|
19783
|
+
const Z_SRC = this.#decodeL(sourceBlackPoint[2]);
|
|
19784
|
+
const X_Scale = (1 - X_DST) / (1 - X_SRC);
|
|
19785
|
+
const X_Offset = 1 - X_Scale;
|
|
19786
|
+
const Y_Scale = (1 - Y_DST) / (1 - Y_SRC);
|
|
19787
|
+
const Y_Offset = 1 - Y_Scale;
|
|
19788
|
+
const Z_Scale = (1 - Z_DST) / (1 - Z_SRC);
|
|
19789
|
+
const Z_Offset = 1 - Z_Scale;
|
|
19790
|
+
result[0] = XYZ_Flat[0] * X_Scale + X_Offset;
|
|
19791
|
+
result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;
|
|
19792
|
+
result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;
|
|
19793
|
+
}
|
|
19794
|
+
#normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {
|
|
19795
|
+
if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {
|
|
19796
|
+
result[0] = XYZ_In[0];
|
|
19797
|
+
result[1] = XYZ_In[1];
|
|
19798
|
+
result[2] = XYZ_In[2];
|
|
19799
|
+
return;
|
|
19800
|
+
}
|
|
19801
|
+
const LMS = result;
|
|
19802
|
+
this.#matrixProduct(_CalRGBCS.#BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
|
|
19803
|
+
const LMS_Flat = _CalRGBCS.#tempNormalizeMatrix;
|
|
19804
|
+
this.#toFlat(sourceWhitePoint, LMS, LMS_Flat);
|
|
19805
|
+
this.#matrixProduct(
|
|
19806
|
+
_CalRGBCS.#BRADFORD_SCALE_INVERSE_MATRIX,
|
|
19807
|
+
LMS_Flat,
|
|
19808
|
+
result
|
|
19809
|
+
);
|
|
19810
|
+
}
|
|
19811
|
+
#normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {
|
|
19812
|
+
const LMS = result;
|
|
19813
|
+
this.#matrixProduct(_CalRGBCS.#BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
|
|
19814
|
+
const LMS_D65 = _CalRGBCS.#tempNormalizeMatrix;
|
|
19815
|
+
this.#toD65(sourceWhitePoint, LMS, LMS_D65);
|
|
19816
|
+
this.#matrixProduct(
|
|
19817
|
+
_CalRGBCS.#BRADFORD_SCALE_INVERSE_MATRIX,
|
|
19818
|
+
LMS_D65,
|
|
19819
|
+
result
|
|
19820
|
+
);
|
|
19821
|
+
}
|
|
19822
|
+
#toRgb(src, srcOffset, dest, destOffset, scale) {
|
|
19823
|
+
const A = MathClamp(src[srcOffset] * scale, 0, 1);
|
|
19824
|
+
const B = MathClamp(src[srcOffset + 1] * scale, 0, 1);
|
|
19825
|
+
const C = MathClamp(src[srcOffset + 2] * scale, 0, 1);
|
|
19826
|
+
const AGR = A === 1 ? 1 : A ** this.GR;
|
|
19827
|
+
const BGG = B === 1 ? 1 : B ** this.GG;
|
|
19828
|
+
const CGB = C === 1 ? 1 : C ** this.GB;
|
|
19829
|
+
const X = this.MXA * AGR + this.MXB * BGG + this.MXC * CGB;
|
|
19830
|
+
const Y = this.MYA * AGR + this.MYB * BGG + this.MYC * CGB;
|
|
19831
|
+
const Z = this.MZA * AGR + this.MZB * BGG + this.MZC * CGB;
|
|
19832
|
+
const XYZ = _CalRGBCS.#tempConvertMatrix1;
|
|
19833
|
+
XYZ[0] = X;
|
|
19834
|
+
XYZ[1] = Y;
|
|
19835
|
+
XYZ[2] = Z;
|
|
19836
|
+
const XYZ_Flat = _CalRGBCS.#tempConvertMatrix2;
|
|
19837
|
+
this.#normalizeWhitePointToFlat(this.whitePoint, XYZ, XYZ_Flat);
|
|
19838
|
+
const XYZ_Black = _CalRGBCS.#tempConvertMatrix1;
|
|
19839
|
+
this.#compensateBlackPoint(this.blackPoint, XYZ_Flat, XYZ_Black);
|
|
19840
|
+
const XYZ_D65 = _CalRGBCS.#tempConvertMatrix2;
|
|
19841
|
+
this.#normalizeWhitePointToD65(
|
|
19842
|
+
_CalRGBCS.#FLAT_WHITEPOINT_MATRIX,
|
|
19843
|
+
XYZ_Black,
|
|
19844
|
+
XYZ_D65
|
|
19845
|
+
);
|
|
19846
|
+
const SRGB = _CalRGBCS.#tempConvertMatrix1;
|
|
19847
|
+
this.#matrixProduct(_CalRGBCS.#SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
|
|
19848
|
+
dest[destOffset] = this.#sRGBTransferFunction(SRGB[0]) * 255;
|
|
19849
|
+
dest[destOffset + 1] = this.#sRGBTransferFunction(SRGB[1]) * 255;
|
|
19850
|
+
dest[destOffset + 2] = this.#sRGBTransferFunction(SRGB[2]) * 255;
|
|
19851
|
+
}
|
|
19852
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
19853
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19854
|
+
assert(
|
|
19855
|
+
dest instanceof Uint8ClampedArray,
|
|
19856
|
+
'CalRGBCS.getRgbItem: Unsupported "dest" type.'
|
|
19857
|
+
);
|
|
19858
|
+
}
|
|
19859
|
+
this.#toRgb(src, srcOffset, dest, destOffset, 1);
|
|
19860
|
+
}
|
|
19861
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
19862
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19863
|
+
assert(
|
|
19864
|
+
dest instanceof Uint8ClampedArray,
|
|
19865
|
+
'CalRGBCS.getRgbBuffer: Unsupported "dest" type.'
|
|
19866
|
+
);
|
|
19867
|
+
}
|
|
19868
|
+
const scale = 1 / ((1 << bits) - 1);
|
|
19869
|
+
for (let i = 0; i < count; ++i) {
|
|
19870
|
+
this.#toRgb(src, srcOffset, dest, destOffset, scale);
|
|
19871
|
+
srcOffset += 3;
|
|
19872
|
+
destOffset += 3 + alpha01;
|
|
19873
|
+
}
|
|
19874
|
+
}
|
|
19875
|
+
};
|
|
19876
|
+
var LabCS = class extends ColorSpace {
|
|
19877
|
+
constructor(whitePoint, blackPoint, range) {
|
|
19878
|
+
super("Lab", 3);
|
|
19879
|
+
if (!whitePoint) {
|
|
19880
|
+
throw new FormatError(
|
|
19881
|
+
"WhitePoint missing - required for color space Lab"
|
|
19882
|
+
);
|
|
19883
|
+
}
|
|
19884
|
+
[this.XW, this.YW, this.ZW] = whitePoint;
|
|
19885
|
+
[this.amin, this.amax, this.bmin, this.bmax] = range || [
|
|
19886
|
+
-100,
|
|
19887
|
+
100,
|
|
19888
|
+
-100,
|
|
19889
|
+
100
|
|
19890
|
+
];
|
|
19891
|
+
[this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
|
|
19892
|
+
if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
|
|
19893
|
+
throw new FormatError(
|
|
19894
|
+
"Invalid WhitePoint components, no fallback available"
|
|
19895
|
+
);
|
|
19896
|
+
}
|
|
19897
|
+
if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
|
|
19898
|
+
info("Invalid BlackPoint, falling back to default");
|
|
19899
|
+
this.XB = this.YB = this.ZB = 0;
|
|
19900
|
+
}
|
|
19901
|
+
if (this.amin > this.amax || this.bmin > this.bmax) {
|
|
19902
|
+
info("Invalid Range, falling back to defaults");
|
|
19903
|
+
this.amin = -100;
|
|
19904
|
+
this.amax = 100;
|
|
19905
|
+
this.bmin = -100;
|
|
19906
|
+
this.bmax = 100;
|
|
19907
|
+
}
|
|
19908
|
+
}
|
|
19909
|
+
// Function g(x) from spec
|
|
19910
|
+
#fn_g(x) {
|
|
19911
|
+
return x >= 6 / 29 ? x ** 3 : 108 / 841 * (x - 4 / 29);
|
|
19912
|
+
}
|
|
19913
|
+
#decode(value, high1, low2, high2) {
|
|
19914
|
+
return low2 + value * (high2 - low2) / high1;
|
|
19915
|
+
}
|
|
19916
|
+
// If decoding is needed maxVal should be 2^bits per component - 1.
|
|
19917
|
+
#toRgb(src, srcOffset, maxVal, dest, destOffset) {
|
|
19918
|
+
let Ls = src[srcOffset];
|
|
19919
|
+
let as = src[srcOffset + 1];
|
|
19920
|
+
let bs = src[srcOffset + 2];
|
|
19921
|
+
if (maxVal !== false) {
|
|
19922
|
+
Ls = this.#decode(Ls, maxVal, 0, 100);
|
|
19923
|
+
as = this.#decode(as, maxVal, this.amin, this.amax);
|
|
19924
|
+
bs = this.#decode(bs, maxVal, this.bmin, this.bmax);
|
|
19925
|
+
}
|
|
19926
|
+
if (as > this.amax) {
|
|
19927
|
+
as = this.amax;
|
|
19928
|
+
} else if (as < this.amin) {
|
|
19929
|
+
as = this.amin;
|
|
19930
|
+
}
|
|
19931
|
+
if (bs > this.bmax) {
|
|
19932
|
+
bs = this.bmax;
|
|
19933
|
+
} else if (bs < this.bmin) {
|
|
19934
|
+
bs = this.bmin;
|
|
19935
|
+
}
|
|
19936
|
+
const M = (Ls + 16) / 116;
|
|
19937
|
+
const L = M + as / 500;
|
|
19938
|
+
const N = M - bs / 200;
|
|
19939
|
+
const X = this.XW * this.#fn_g(L);
|
|
19940
|
+
const Y = this.YW * this.#fn_g(M);
|
|
19941
|
+
const Z = this.ZW * this.#fn_g(N);
|
|
19942
|
+
let r, g, b;
|
|
19943
|
+
if (this.ZW < 1) {
|
|
19944
|
+
r = X * 3.1339 + Y * -1.617 + Z * -0.4906;
|
|
19945
|
+
g = X * -0.9785 + Y * 1.916 + Z * 0.0333;
|
|
19946
|
+
b = X * 0.072 + Y * -0.229 + Z * 1.4057;
|
|
19947
|
+
} else {
|
|
19948
|
+
r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
|
|
19949
|
+
g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
|
|
19950
|
+
b = X * 0.0557 + Y * -0.204 + Z * 1.057;
|
|
19951
|
+
}
|
|
19952
|
+
dest[destOffset] = Math.sqrt(r) * 255;
|
|
19953
|
+
dest[destOffset + 1] = Math.sqrt(g) * 255;
|
|
19954
|
+
dest[destOffset + 2] = Math.sqrt(b) * 255;
|
|
19955
|
+
}
|
|
19956
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
19957
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19958
|
+
assert(
|
|
19959
|
+
dest instanceof Uint8ClampedArray,
|
|
19960
|
+
'LabCS.getRgbItem: Unsupported "dest" type.'
|
|
19961
|
+
);
|
|
19962
|
+
}
|
|
19963
|
+
this.#toRgb(src, srcOffset, false, dest, destOffset);
|
|
19964
|
+
}
|
|
19965
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
19966
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
19967
|
+
assert(
|
|
19968
|
+
dest instanceof Uint8ClampedArray,
|
|
19969
|
+
'LabCS.getRgbBuffer: Unsupported "dest" type.'
|
|
19970
|
+
);
|
|
19971
|
+
}
|
|
19972
|
+
const maxVal = (1 << bits) - 1;
|
|
19973
|
+
for (let i = 0; i < count; i++) {
|
|
19974
|
+
this.#toRgb(src, srcOffset, maxVal, dest, destOffset);
|
|
19975
|
+
srcOffset += 3;
|
|
19976
|
+
destOffset += 3 + alpha01;
|
|
19977
|
+
}
|
|
19978
|
+
}
|
|
19979
|
+
isDefaultDecode(decode, bpc) {
|
|
19980
|
+
return true;
|
|
19981
|
+
}
|
|
19982
|
+
get usesZeroToOneRange() {
|
|
19983
|
+
return shadow(this, "usesZeroToOneRange", false);
|
|
19984
|
+
}
|
|
19985
|
+
};
|
|
19986
|
+
|
|
19987
|
+
// src/core/postscript/lexer.js
|
|
19988
|
+
var TOKEN = {
|
|
19989
|
+
// Structural tokens — not keyword operators
|
|
19990
|
+
number: 0,
|
|
19991
|
+
lbrace: 1,
|
|
19992
|
+
rbrace: 2,
|
|
19993
|
+
// Boolean literals
|
|
19994
|
+
true: 3,
|
|
19995
|
+
false: 4,
|
|
19996
|
+
// Arithmetic binary operators
|
|
19997
|
+
add: 5,
|
|
19998
|
+
sub: 6,
|
|
19999
|
+
mul: 7,
|
|
20000
|
+
div: 8,
|
|
20001
|
+
idiv: 9,
|
|
20002
|
+
mod: 10,
|
|
20003
|
+
exp: 11,
|
|
20004
|
+
// Comparison binary operators
|
|
20005
|
+
eq: 12,
|
|
20006
|
+
ne: 13,
|
|
20007
|
+
gt: 14,
|
|
20008
|
+
ge: 15,
|
|
20009
|
+
lt: 16,
|
|
20010
|
+
le: 17,
|
|
20011
|
+
// Bitwise / boolean binary operators
|
|
20012
|
+
and: 18,
|
|
20013
|
+
or: 19,
|
|
20014
|
+
xor: 20,
|
|
20015
|
+
bitshift: 21,
|
|
20016
|
+
// Unary arithmetic operators
|
|
20017
|
+
abs: 22,
|
|
20018
|
+
neg: 23,
|
|
20019
|
+
ceiling: 24,
|
|
20020
|
+
floor: 25,
|
|
20021
|
+
round: 26,
|
|
20022
|
+
truncate: 27,
|
|
20023
|
+
// Unary boolean / bitwise operator
|
|
20024
|
+
not: 28,
|
|
20025
|
+
// Mathematical functions — unary
|
|
20026
|
+
sqrt: 29,
|
|
20027
|
+
sin: 30,
|
|
20028
|
+
cos: 31,
|
|
20029
|
+
ln: 32,
|
|
20030
|
+
log: 33,
|
|
20031
|
+
// Mathematical function — binary
|
|
20032
|
+
atan: 34,
|
|
20033
|
+
// Type conversion operators
|
|
20034
|
+
cvi: 35,
|
|
20035
|
+
cvr: 36,
|
|
20036
|
+
// Stack operators
|
|
20037
|
+
dup: 37,
|
|
20038
|
+
exch: 38,
|
|
20039
|
+
pop: 39,
|
|
20040
|
+
copy: 40,
|
|
20041
|
+
index: 41,
|
|
20042
|
+
roll: 42,
|
|
20043
|
+
// Control flow
|
|
20044
|
+
if: 43,
|
|
20045
|
+
ifelse: 44,
|
|
20046
|
+
// End of input
|
|
20047
|
+
eof: 45,
|
|
20048
|
+
// Synthetic: produced by the optimizer, never emitted by the lexer.
|
|
20049
|
+
min: 46,
|
|
20050
|
+
max: 47
|
|
20051
|
+
};
|
|
20052
|
+
var Token = class {
|
|
20053
|
+
constructor(id, value = null) {
|
|
20054
|
+
this.id = id;
|
|
20055
|
+
this.value = value;
|
|
20056
|
+
}
|
|
20057
|
+
};
|
|
20058
|
+
var Lexer = class _Lexer {
|
|
20059
|
+
// Singletons for every non-number token, built lazily on first construction.
|
|
20060
|
+
// Keyword operator tokens carry their name as `value`; structural tokens
|
|
20061
|
+
// (lbrace, rbrace, eof) carry null.
|
|
20062
|
+
static #singletons = null;
|
|
20063
|
+
static #operatorSingletons = null;
|
|
20064
|
+
static #initSingletons() {
|
|
20065
|
+
const singletons = /* @__PURE__ */ Object.create(null);
|
|
20066
|
+
const operatorSingletons = /* @__PURE__ */ Object.create(null);
|
|
20067
|
+
for (const [name, id] of Object.entries(TOKEN)) {
|
|
20068
|
+
if (name === "number") {
|
|
20069
|
+
continue;
|
|
20070
|
+
}
|
|
20071
|
+
const isOperator = id >= TOKEN.true && id <= TOKEN.ifelse;
|
|
20072
|
+
const token = new Token(id, isOperator ? name : null);
|
|
20073
|
+
singletons[name] = token;
|
|
20074
|
+
if (isOperator) {
|
|
20075
|
+
operatorSingletons[name] = token;
|
|
20076
|
+
}
|
|
20077
|
+
}
|
|
20078
|
+
this.#singletons = singletons;
|
|
20079
|
+
this.#operatorSingletons = operatorSingletons;
|
|
20080
|
+
}
|
|
20081
|
+
constructor(data) {
|
|
20082
|
+
if (!_Lexer.#singletons) {
|
|
20083
|
+
_Lexer.#initSingletons();
|
|
20084
|
+
}
|
|
20085
|
+
this.data = data;
|
|
20086
|
+
this.pos = 0;
|
|
20087
|
+
this.len = data.length;
|
|
20088
|
+
this._numberPattern = /[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?/iy;
|
|
20089
|
+
this._identifierPattern = /[a-z]+/y;
|
|
20090
|
+
}
|
|
20091
|
+
// Skip a % comment, advancing past the next \n or \r (or to EOF).
|
|
20092
|
+
_skipComment() {
|
|
20093
|
+
const lf = this.data.indexOf("\n", this.pos);
|
|
20094
|
+
const cr = this.data.indexOf("\r", this.pos);
|
|
20095
|
+
const eol = Math.min(lf < 0 ? this.len : lf, cr < 0 ? this.len : cr);
|
|
20096
|
+
this.pos = Math.min(eol + 1, this.len);
|
|
20097
|
+
}
|
|
20098
|
+
_getNumber() {
|
|
20099
|
+
this._numberPattern.lastIndex = this.pos;
|
|
20100
|
+
const match = this._numberPattern.exec(this.data);
|
|
20101
|
+
if (!match) {
|
|
20102
|
+
return new Token(TOKEN.number, 0);
|
|
20103
|
+
}
|
|
20104
|
+
const number = parseFloat(match[0]);
|
|
20105
|
+
if (!Number.isFinite(number)) {
|
|
20106
|
+
return new Token(TOKEN.number, 0);
|
|
20107
|
+
}
|
|
20108
|
+
this.pos = this._numberPattern.lastIndex;
|
|
20109
|
+
return new Token(TOKEN.number, number);
|
|
20110
|
+
}
|
|
20111
|
+
_getOperator() {
|
|
20112
|
+
this._identifierPattern.lastIndex = this.pos;
|
|
20113
|
+
const match = this._identifierPattern.exec(this.data);
|
|
20114
|
+
if (!match) {
|
|
20115
|
+
return new Token(TOKEN.number, 0);
|
|
20116
|
+
}
|
|
20117
|
+
this.pos = this._identifierPattern.lastIndex;
|
|
20118
|
+
const op = match[0];
|
|
20119
|
+
const token = _Lexer.#operatorSingletons[op];
|
|
20120
|
+
return token ?? new Token(TOKEN.number, 0);
|
|
20121
|
+
}
|
|
20122
|
+
// Return the next token, or Lexer.#singletons.eof at end of input.
|
|
20123
|
+
next() {
|
|
20124
|
+
while (this.pos < this.len) {
|
|
20125
|
+
const ch3 = this.data.charCodeAt(this.pos++);
|
|
20126
|
+
switch (ch3) {
|
|
20127
|
+
// PostScript white-space characters (PDF32000 §7.2.2)
|
|
20128
|
+
case 0:
|
|
20129
|
+
case 9:
|
|
20130
|
+
case 10:
|
|
20131
|
+
case 12:
|
|
20132
|
+
case 13:
|
|
20133
|
+
case 32:
|
|
20134
|
+
break;
|
|
20135
|
+
case 37:
|
|
20136
|
+
this._skipComment();
|
|
20137
|
+
break;
|
|
20138
|
+
case 123:
|
|
20139
|
+
return _Lexer.#singletons.lbrace;
|
|
20140
|
+
case 125:
|
|
20141
|
+
return _Lexer.#singletons.rbrace;
|
|
20142
|
+
case 43:
|
|
20143
|
+
case 45:
|
|
20144
|
+
this.pos--;
|
|
20145
|
+
return this._getNumber();
|
|
20146
|
+
case 46:
|
|
20147
|
+
this.pos--;
|
|
20148
|
+
return this._getNumber();
|
|
20149
|
+
default:
|
|
20150
|
+
if (ch3 >= 48 && ch3 <= 57) {
|
|
20151
|
+
this.pos--;
|
|
20152
|
+
return this._getNumber();
|
|
20153
|
+
}
|
|
20154
|
+
if (ch3 >= 97 && ch3 <= 122) {
|
|
20155
|
+
this.pos--;
|
|
20156
|
+
return this._getOperator();
|
|
20157
|
+
}
|
|
20158
|
+
return new Token(TOKEN.number, 0);
|
|
20159
|
+
}
|
|
20160
|
+
}
|
|
20161
|
+
return _Lexer.#singletons.eof;
|
|
20162
|
+
}
|
|
20163
|
+
};
|
|
20164
|
+
|
|
20165
|
+
// src/core/postscript/ast.js
|
|
20166
|
+
var PS_VALUE_TYPE = {
|
|
20167
|
+
numeric: 0,
|
|
20168
|
+
// known to be a number (f64 in Wasm)
|
|
20169
|
+
boolean: 1,
|
|
20170
|
+
// known to be a boolean (0.0 = false, 1.0 = true in f64)
|
|
20171
|
+
unknown: 2
|
|
20172
|
+
// indeterminate at compile time
|
|
20173
|
+
};
|
|
20174
|
+
var PS_NODE = {
|
|
20175
|
+
// Parser AST node types (produced by Parser / parsePostScriptFunction)
|
|
20176
|
+
program: 0,
|
|
20177
|
+
block: 1,
|
|
20178
|
+
number: 2,
|
|
20179
|
+
operator: 3,
|
|
20180
|
+
if: 4,
|
|
20181
|
+
ifelse: 5,
|
|
20182
|
+
// Tree AST node types (produced by PSStackToTree)
|
|
20183
|
+
arg: 6,
|
|
20184
|
+
const: 7,
|
|
20185
|
+
unary: 8,
|
|
20186
|
+
binary: 9,
|
|
20187
|
+
ternary: 10
|
|
20188
|
+
};
|
|
20189
|
+
var PsNode = class {
|
|
20190
|
+
constructor(type) {
|
|
20191
|
+
this.type = type;
|
|
20192
|
+
}
|
|
20193
|
+
};
|
|
20194
|
+
var PsProgram = class extends PsNode {
|
|
20195
|
+
constructor(body) {
|
|
20196
|
+
super(PS_NODE.program);
|
|
20197
|
+
this.body = body;
|
|
20198
|
+
}
|
|
20199
|
+
};
|
|
20200
|
+
var PsBlock = class extends PsNode {
|
|
20201
|
+
constructor(instructions) {
|
|
20202
|
+
super(PS_NODE.block);
|
|
20203
|
+
this.instructions = instructions;
|
|
20204
|
+
}
|
|
20205
|
+
};
|
|
20206
|
+
var PsNumber = class extends PsNode {
|
|
20207
|
+
/** @param {number} value */
|
|
20208
|
+
constructor(value) {
|
|
20209
|
+
super(PS_NODE.number);
|
|
20210
|
+
this.value = value;
|
|
20211
|
+
}
|
|
20212
|
+
};
|
|
20213
|
+
var PsOperator = class extends PsNode {
|
|
20214
|
+
/** @param {number} op — one of the TOKEN.* constants from lexer.js */
|
|
20215
|
+
constructor(op) {
|
|
20216
|
+
super(PS_NODE.operator);
|
|
20217
|
+
this.op = op;
|
|
20218
|
+
}
|
|
20219
|
+
};
|
|
20220
|
+
var PsIf = class extends PsNode {
|
|
20221
|
+
/** @param {PsBlock} then */
|
|
20222
|
+
constructor(then) {
|
|
20223
|
+
super(PS_NODE.if);
|
|
20224
|
+
this.then = then;
|
|
20225
|
+
}
|
|
20226
|
+
};
|
|
20227
|
+
var PsIfElse = class extends PsNode {
|
|
20228
|
+
/**
|
|
20229
|
+
* @param {PsBlock} then
|
|
20230
|
+
* @param {PsBlock} otherwise
|
|
20231
|
+
*/
|
|
20232
|
+
constructor(then, otherwise) {
|
|
20233
|
+
super(PS_NODE.ifelse);
|
|
20234
|
+
this.then = then;
|
|
20235
|
+
this.otherwise = otherwise;
|
|
20236
|
+
}
|
|
20237
|
+
};
|
|
20238
|
+
var PsArgNode = class extends PsNode {
|
|
20239
|
+
/** @param {number} index */
|
|
20240
|
+
constructor(index) {
|
|
20241
|
+
super(PS_NODE.arg);
|
|
20242
|
+
this.index = index;
|
|
20243
|
+
this.valueType = PS_VALUE_TYPE.numeric;
|
|
20244
|
+
}
|
|
20245
|
+
};
|
|
20246
|
+
var PsConstNode = class extends PsNode {
|
|
20247
|
+
/** @param {number|boolean} value */
|
|
20248
|
+
constructor(value) {
|
|
20249
|
+
super(PS_NODE.const);
|
|
20250
|
+
this.value = value;
|
|
20251
|
+
this.valueType = typeof value === "boolean" ? PS_VALUE_TYPE.boolean : PS_VALUE_TYPE.numeric;
|
|
20252
|
+
}
|
|
20253
|
+
};
|
|
20254
|
+
var PsUnaryNode = class extends PsNode {
|
|
20255
|
+
/**
|
|
20256
|
+
* @param {number} op — TOKEN.* constant
|
|
20257
|
+
* @param {PsNode} operand
|
|
20258
|
+
* @param {number} [valueType]
|
|
20259
|
+
*/
|
|
20260
|
+
constructor(op, operand, valueType = PS_VALUE_TYPE.unknown) {
|
|
20261
|
+
super(PS_NODE.unary);
|
|
20262
|
+
this.op = op;
|
|
20263
|
+
this.operand = operand;
|
|
20264
|
+
this.valueType = valueType;
|
|
20265
|
+
}
|
|
20266
|
+
};
|
|
20267
|
+
var PsBinaryNode = class extends PsNode {
|
|
20268
|
+
/**
|
|
20269
|
+
* @param {number} op — TOKEN.* constant
|
|
20270
|
+
* @param {PsNode} first — was on top of stack
|
|
20271
|
+
* @param {PsNode} second — was below top
|
|
20272
|
+
* @param {number} [valueType]
|
|
20273
|
+
*/
|
|
20274
|
+
constructor(op, first, second, valueType = PS_VALUE_TYPE.unknown) {
|
|
20275
|
+
super(PS_NODE.binary);
|
|
20276
|
+
this.op = op;
|
|
20277
|
+
this.first = first;
|
|
20278
|
+
this.second = second;
|
|
20279
|
+
this.valueType = valueType;
|
|
20280
|
+
}
|
|
20281
|
+
};
|
|
20282
|
+
var PsTernaryNode = class extends PsNode {
|
|
20283
|
+
/**
|
|
20284
|
+
* @param {PsNode} cond
|
|
20285
|
+
* @param {PsNode} then
|
|
20286
|
+
* @param {PsNode} otherwise
|
|
20287
|
+
* @param {number} [valueType]
|
|
20288
|
+
*/
|
|
20289
|
+
constructor(cond, then, otherwise, valueType = PS_VALUE_TYPE.unknown) {
|
|
20290
|
+
super(PS_NODE.ternary);
|
|
20291
|
+
this.cond = cond;
|
|
20292
|
+
this.then = then;
|
|
20293
|
+
this.otherwise = otherwise;
|
|
20294
|
+
this.valueType = valueType;
|
|
20295
|
+
}
|
|
20296
|
+
};
|
|
20297
|
+
var Parser = class _Parser {
|
|
20298
|
+
constructor(lexer) {
|
|
20299
|
+
this.lexer = lexer;
|
|
20300
|
+
this._token = null;
|
|
20301
|
+
}
|
|
20302
|
+
static _isRegularOperator(id) {
|
|
20303
|
+
return id >= TOKEN.true && id < TOKEN.if;
|
|
20304
|
+
}
|
|
20305
|
+
// Fetch the next token from the lexer.
|
|
20306
|
+
_advance() {
|
|
20307
|
+
this._token = this.lexer.next();
|
|
20308
|
+
}
|
|
20309
|
+
// Assert that the current token has the given id, consume it, and return it.
|
|
20310
|
+
_expect(id) {
|
|
20311
|
+
if (this._token.id !== id) {
|
|
20312
|
+
throw new FormatError(
|
|
20313
|
+
`PostScript function: expected token id ${id}, got ${this._token.id}.`
|
|
20314
|
+
);
|
|
20315
|
+
}
|
|
20316
|
+
const tok = this._token;
|
|
20317
|
+
this._advance();
|
|
20318
|
+
return tok;
|
|
20319
|
+
}
|
|
20320
|
+
/**
|
|
20321
|
+
* Parse the full Type 4 function body.
|
|
20322
|
+
*
|
|
20323
|
+
* Grammar (simplified):
|
|
20324
|
+
* program ::= '{' block '}'
|
|
20325
|
+
* block ::= instruction*
|
|
20326
|
+
* instruction ::= number
|
|
20327
|
+
* | operator (any PS_OPERATOR except if / ifelse)
|
|
20328
|
+
* | '{' block '}' 'if'
|
|
20329
|
+
* | '{' block '}' '{' block '}' 'ifelse'
|
|
20330
|
+
* @returns {PsProgram}
|
|
20331
|
+
*/
|
|
20332
|
+
parse() {
|
|
20333
|
+
this._advance();
|
|
20334
|
+
this._expect(TOKEN.lbrace);
|
|
20335
|
+
const block = this._parseBlock();
|
|
20336
|
+
this._expect(TOKEN.rbrace);
|
|
20337
|
+
if (this._token.id !== TOKEN.eof) {
|
|
20338
|
+
warn("PostScript function: unexpected content after closing brace.");
|
|
20339
|
+
}
|
|
20340
|
+
return new PsProgram(block);
|
|
20341
|
+
}
|
|
20342
|
+
_parseBlock() {
|
|
20343
|
+
const instructions = [];
|
|
20344
|
+
while (true) {
|
|
20345
|
+
const tok = this._token;
|
|
20346
|
+
switch (tok.id) {
|
|
20347
|
+
case TOKEN.number:
|
|
20348
|
+
instructions.push(new PsNumber(tok.value));
|
|
20349
|
+
this._advance();
|
|
20350
|
+
break;
|
|
20351
|
+
case TOKEN.lbrace: {
|
|
20352
|
+
this._advance();
|
|
20353
|
+
const thenBlock = this._parseBlock();
|
|
20354
|
+
this._expect(TOKEN.rbrace);
|
|
20355
|
+
if (this._token.id === TOKEN.if) {
|
|
20356
|
+
this._advance();
|
|
20357
|
+
instructions.push(new PsIf(thenBlock));
|
|
20358
|
+
} else if (this._token.id === TOKEN.lbrace) {
|
|
20359
|
+
this._advance();
|
|
20360
|
+
const elseBlock = this._parseBlock();
|
|
20361
|
+
this._expect(TOKEN.rbrace);
|
|
20362
|
+
this._expect(TOKEN.ifelse);
|
|
20363
|
+
instructions.push(new PsIfElse(thenBlock, elseBlock));
|
|
20364
|
+
} else {
|
|
20365
|
+
throw new FormatError(
|
|
20366
|
+
"PostScript function: a procedure block must be followed by 'if' or '{\u2026} ifelse'."
|
|
20367
|
+
);
|
|
20368
|
+
}
|
|
20369
|
+
break;
|
|
20370
|
+
}
|
|
20371
|
+
case TOKEN.rbrace:
|
|
20372
|
+
case TOKEN.eof:
|
|
20373
|
+
return new PsBlock(instructions);
|
|
20374
|
+
case TOKEN.if:
|
|
20375
|
+
case TOKEN.ifelse:
|
|
20376
|
+
throw new FormatError(
|
|
20377
|
+
`PostScript function: unexpected '${tok.value}' operator.`
|
|
20378
|
+
);
|
|
20379
|
+
default:
|
|
20380
|
+
if (_Parser._isRegularOperator(tok.id)) {
|
|
20381
|
+
instructions.push(new PsOperator(tok.id));
|
|
20382
|
+
this._advance();
|
|
20383
|
+
break;
|
|
20384
|
+
}
|
|
20385
|
+
throw new FormatError(
|
|
20386
|
+
`PostScript function: unexpected token id ${tok.id}.`
|
|
20387
|
+
);
|
|
20388
|
+
}
|
|
20389
|
+
}
|
|
20390
|
+
}
|
|
20391
|
+
};
|
|
20392
|
+
function parsePostScriptFunction(source) {
|
|
20393
|
+
return new Parser(new Lexer(source)).parse();
|
|
20394
|
+
}
|
|
20395
|
+
function _nodesEqual(a, b) {
|
|
20396
|
+
if (a === b) {
|
|
20397
|
+
return true;
|
|
20398
|
+
}
|
|
20399
|
+
if (a.type !== b.type) {
|
|
20400
|
+
return false;
|
|
20401
|
+
}
|
|
20402
|
+
switch (a.type) {
|
|
20403
|
+
case PS_NODE.arg:
|
|
20404
|
+
return a.index === b.index;
|
|
20405
|
+
case PS_NODE.const:
|
|
20406
|
+
return a.value === b.value;
|
|
20407
|
+
case PS_NODE.unary:
|
|
20408
|
+
return a.op === b.op && _nodesEqual(a.operand, b.operand);
|
|
20409
|
+
case PS_NODE.binary:
|
|
20410
|
+
return a.op === b.op && _nodesEqual(a.first, b.first) && _nodesEqual(a.second, b.second);
|
|
20411
|
+
case PS_NODE.ternary:
|
|
20412
|
+
return _nodesEqual(a.cond, b.cond) && _nodesEqual(a.then, b.then) && _nodesEqual(a.otherwise, b.otherwise);
|
|
20413
|
+
default:
|
|
20414
|
+
return false;
|
|
20415
|
+
}
|
|
20416
|
+
}
|
|
20417
|
+
function _evalBinaryConst(op, a, b) {
|
|
20418
|
+
switch (op) {
|
|
20419
|
+
case TOKEN.add:
|
|
20420
|
+
return a + b;
|
|
20421
|
+
case TOKEN.sub:
|
|
20422
|
+
return a - b;
|
|
20423
|
+
case TOKEN.mul:
|
|
20424
|
+
return a * b;
|
|
20425
|
+
case TOKEN.div:
|
|
20426
|
+
return b !== 0 ? a / b : 0;
|
|
20427
|
+
// div by zero → 0
|
|
20428
|
+
case TOKEN.idiv:
|
|
20429
|
+
return b !== 0 ? Math.trunc(a / b) : 0;
|
|
20430
|
+
// div by zero → 0
|
|
20431
|
+
case TOKEN.mod:
|
|
20432
|
+
return b !== 0 ? a - Math.trunc(a / b) * b : 0;
|
|
20433
|
+
// div by zero → 0
|
|
20434
|
+
case TOKEN.exp: {
|
|
20435
|
+
const r = a ** b;
|
|
20436
|
+
return Number.isFinite(r) ? r : void 0;
|
|
20437
|
+
}
|
|
20438
|
+
case TOKEN.atan: {
|
|
20439
|
+
let deg = Math.atan2(a, b) * (180 / Math.PI);
|
|
20440
|
+
if (deg < 0) {
|
|
20441
|
+
deg += 360;
|
|
20442
|
+
}
|
|
20443
|
+
return deg;
|
|
20444
|
+
}
|
|
20445
|
+
case TOKEN.eq:
|
|
20446
|
+
return a === b;
|
|
20447
|
+
case TOKEN.ne:
|
|
20448
|
+
return a !== b;
|
|
20449
|
+
case TOKEN.gt:
|
|
20450
|
+
return a > b;
|
|
20451
|
+
case TOKEN.ge:
|
|
20452
|
+
return a >= b;
|
|
20453
|
+
case TOKEN.lt:
|
|
20454
|
+
return a < b;
|
|
20455
|
+
case TOKEN.le:
|
|
20456
|
+
return a <= b;
|
|
20457
|
+
case TOKEN.and:
|
|
20458
|
+
return typeof a === "boolean" ? a && b : a & b | 0;
|
|
20459
|
+
case TOKEN.or:
|
|
20460
|
+
return typeof a === "boolean" ? a || b : a | b | 0;
|
|
20461
|
+
case TOKEN.xor:
|
|
20462
|
+
return typeof a === "boolean" ? a !== b : a ^ b | 0;
|
|
20463
|
+
case TOKEN.bitshift:
|
|
20464
|
+
return b >= 0 ? a << b | 0 : a >> -b | 0;
|
|
20465
|
+
case TOKEN.min:
|
|
20466
|
+
return Math.min(a, b);
|
|
20467
|
+
case TOKEN.max:
|
|
20468
|
+
return Math.max(a, b);
|
|
20469
|
+
default:
|
|
20470
|
+
return void 0;
|
|
20471
|
+
}
|
|
20472
|
+
}
|
|
20473
|
+
function _evalUnaryConst(op, v) {
|
|
20474
|
+
switch (op) {
|
|
20475
|
+
case TOKEN.abs:
|
|
20476
|
+
return Math.abs(v);
|
|
20477
|
+
case TOKEN.neg:
|
|
20478
|
+
return -v;
|
|
20479
|
+
case TOKEN.ceiling:
|
|
20480
|
+
return Math.ceil(v);
|
|
20481
|
+
case TOKEN.floor:
|
|
20482
|
+
return Math.floor(v);
|
|
20483
|
+
case TOKEN.round:
|
|
20484
|
+
return Math.round(v);
|
|
20485
|
+
case TOKEN.truncate:
|
|
20486
|
+
return Math.trunc(v);
|
|
20487
|
+
case TOKEN.sqrt: {
|
|
20488
|
+
const r = Math.sqrt(v);
|
|
20489
|
+
return Number.isFinite(r) ? r : void 0;
|
|
20490
|
+
}
|
|
20491
|
+
case TOKEN.sin:
|
|
20492
|
+
return Math.sin(v % 360 * Math.PI / 180);
|
|
20493
|
+
case TOKEN.cos:
|
|
20494
|
+
return Math.cos(v % 360 * Math.PI / 180);
|
|
20495
|
+
case TOKEN.ln: {
|
|
20496
|
+
const r = Math.log(v);
|
|
20497
|
+
return Number.isFinite(r) ? r : void 0;
|
|
20498
|
+
}
|
|
20499
|
+
case TOKEN.log: {
|
|
20500
|
+
const r = Math.log10(v);
|
|
20501
|
+
return Number.isFinite(r) ? r : void 0;
|
|
20502
|
+
}
|
|
20503
|
+
case TOKEN.cvi:
|
|
20504
|
+
return Math.trunc(v);
|
|
20505
|
+
case TOKEN.cvr:
|
|
20506
|
+
return v;
|
|
20507
|
+
case TOKEN.not:
|
|
20508
|
+
return typeof v === "boolean" ? !v : ~v;
|
|
20509
|
+
default:
|
|
20510
|
+
return void 0;
|
|
20511
|
+
}
|
|
20512
|
+
}
|
|
20513
|
+
var MAX_STACK_SIZE = 100;
|
|
20514
|
+
function _unaryValueType(op, operandType) {
|
|
20515
|
+
return op === TOKEN.not ? operandType : PS_VALUE_TYPE.numeric;
|
|
20516
|
+
}
|
|
20517
|
+
function _binaryValueType(op, firstType, secondType) {
|
|
20518
|
+
switch (op) {
|
|
20519
|
+
// Comparison operators always produce a boolean.
|
|
20520
|
+
case TOKEN.eq:
|
|
20521
|
+
case TOKEN.ne:
|
|
20522
|
+
case TOKEN.gt:
|
|
20523
|
+
case TOKEN.ge:
|
|
20524
|
+
case TOKEN.lt:
|
|
20525
|
+
case TOKEN.le:
|
|
20526
|
+
return PS_VALUE_TYPE.boolean;
|
|
20527
|
+
// and / or / xor preserve the type when both operands are the same known
|
|
20528
|
+
// type (both boolean or both numeric); otherwise the type is unknown.
|
|
20529
|
+
case TOKEN.and:
|
|
20530
|
+
case TOKEN.or:
|
|
20531
|
+
case TOKEN.xor:
|
|
20532
|
+
return firstType === secondType && firstType !== PS_VALUE_TYPE.unknown ? firstType : PS_VALUE_TYPE.unknown;
|
|
20533
|
+
// All arithmetic / bitshift operators produce a numeric result.
|
|
20534
|
+
default:
|
|
20535
|
+
return PS_VALUE_TYPE.numeric;
|
|
20536
|
+
}
|
|
20537
|
+
}
|
|
20538
|
+
var PSStackToTree = class _PSStackToTree {
|
|
20539
|
+
static #binaryOps = null;
|
|
20540
|
+
static #unaryOps = null;
|
|
20541
|
+
static #idempotentUnary = null;
|
|
20542
|
+
static #negatedComparison = null;
|
|
20543
|
+
static #init() {
|
|
20544
|
+
this.#binaryOps = /* @__PURE__ */ new Set([
|
|
20545
|
+
TOKEN.add,
|
|
20546
|
+
TOKEN.sub,
|
|
20547
|
+
TOKEN.mul,
|
|
20548
|
+
TOKEN.div,
|
|
20549
|
+
TOKEN.idiv,
|
|
20550
|
+
TOKEN.mod,
|
|
20551
|
+
TOKEN.exp,
|
|
20552
|
+
TOKEN.atan,
|
|
20553
|
+
TOKEN.eq,
|
|
20554
|
+
TOKEN.ne,
|
|
20555
|
+
TOKEN.gt,
|
|
20556
|
+
TOKEN.ge,
|
|
20557
|
+
TOKEN.lt,
|
|
20558
|
+
TOKEN.le,
|
|
20559
|
+
TOKEN.and,
|
|
20560
|
+
TOKEN.or,
|
|
20561
|
+
TOKEN.xor,
|
|
20562
|
+
TOKEN.bitshift
|
|
20563
|
+
]);
|
|
20564
|
+
this.#unaryOps = /* @__PURE__ */ new Set([
|
|
20565
|
+
TOKEN.abs,
|
|
20566
|
+
TOKEN.neg,
|
|
20567
|
+
TOKEN.ceiling,
|
|
20568
|
+
TOKEN.floor,
|
|
20569
|
+
TOKEN.round,
|
|
20570
|
+
TOKEN.truncate,
|
|
20571
|
+
TOKEN.sqrt,
|
|
20572
|
+
TOKEN.sin,
|
|
20573
|
+
TOKEN.cos,
|
|
20574
|
+
TOKEN.ln,
|
|
20575
|
+
TOKEN.log,
|
|
20576
|
+
TOKEN.cvi,
|
|
20577
|
+
TOKEN.cvr,
|
|
20578
|
+
TOKEN.not
|
|
20579
|
+
]);
|
|
20580
|
+
this.#idempotentUnary = /* @__PURE__ */ new Set([
|
|
20581
|
+
TOKEN.abs,
|
|
20582
|
+
TOKEN.ceiling,
|
|
20583
|
+
TOKEN.cvi,
|
|
20584
|
+
TOKEN.cvr,
|
|
20585
|
+
TOKEN.floor,
|
|
20586
|
+
TOKEN.round,
|
|
20587
|
+
TOKEN.truncate
|
|
20588
|
+
]);
|
|
20589
|
+
this.#negatedComparison = /* @__PURE__ */ new Map([
|
|
20590
|
+
[TOKEN.eq, TOKEN.ne],
|
|
20591
|
+
[TOKEN.ne, TOKEN.eq],
|
|
20592
|
+
[TOKEN.lt, TOKEN.ge],
|
|
20593
|
+
[TOKEN.le, TOKEN.gt],
|
|
20594
|
+
[TOKEN.gt, TOKEN.le],
|
|
20595
|
+
[TOKEN.ge, TOKEN.lt]
|
|
20596
|
+
]);
|
|
20597
|
+
}
|
|
20598
|
+
/**
|
|
20599
|
+
* @param {PsProgram} program
|
|
20600
|
+
* @param {number} numInputs — number of domain values placed on the stack
|
|
20601
|
+
* before the program runs (i.e. the length of the domain array / 2).
|
|
20602
|
+
* @returns {Array<PsNode>} — one tree node per output value.
|
|
20603
|
+
*/
|
|
20604
|
+
evaluate(program, numInputs) {
|
|
20605
|
+
if (!_PSStackToTree.#binaryOps) {
|
|
20606
|
+
_PSStackToTree.#init();
|
|
20607
|
+
}
|
|
20608
|
+
this._failed = false;
|
|
20609
|
+
if (numInputs > MAX_STACK_SIZE) {
|
|
20610
|
+
return null;
|
|
20611
|
+
}
|
|
20612
|
+
const stack = [];
|
|
20613
|
+
for (let i = 0; i < numInputs; i++) {
|
|
20614
|
+
stack.push(new PsArgNode(i));
|
|
20615
|
+
}
|
|
20616
|
+
this._evalBlock(program.body, stack);
|
|
20617
|
+
if (this._failed) {
|
|
20618
|
+
return null;
|
|
20619
|
+
}
|
|
20620
|
+
_PSStackToTree.#markShared(stack);
|
|
20621
|
+
return stack;
|
|
20622
|
+
}
|
|
20623
|
+
// Set node.shared / sharedCount on non-atomic nodes referenced more than
|
|
20624
|
+
// once. arg/const are excluded — they are cheap to re-emit inline.
|
|
20625
|
+
static #markShared(outputs) {
|
|
20626
|
+
const refCount = /* @__PURE__ */ new Map();
|
|
20627
|
+
const visit = (node) => {
|
|
20628
|
+
if (!node || node.type === PS_NODE.arg || node.type === PS_NODE.const) {
|
|
20629
|
+
return;
|
|
20630
|
+
}
|
|
20631
|
+
const prev = refCount.get(node) ?? 0;
|
|
20632
|
+
refCount.set(node, prev + 1);
|
|
20633
|
+
if (prev > 0) {
|
|
20634
|
+
return;
|
|
20635
|
+
}
|
|
20636
|
+
switch (node.type) {
|
|
20637
|
+
case PS_NODE.unary:
|
|
20638
|
+
visit(node.operand);
|
|
20639
|
+
break;
|
|
20640
|
+
case PS_NODE.binary:
|
|
20641
|
+
visit(node.first);
|
|
20642
|
+
visit(node.second);
|
|
20643
|
+
break;
|
|
20644
|
+
case PS_NODE.ternary:
|
|
20645
|
+
visit(node.cond);
|
|
20646
|
+
visit(node.then);
|
|
20647
|
+
visit(node.otherwise);
|
|
20648
|
+
break;
|
|
20649
|
+
}
|
|
20650
|
+
};
|
|
20651
|
+
for (const output of outputs) {
|
|
20652
|
+
visit(output);
|
|
20653
|
+
}
|
|
20654
|
+
for (const [node, count] of refCount) {
|
|
20655
|
+
if (count > 1) {
|
|
20656
|
+
node.shared = true;
|
|
20657
|
+
node.sharedCount = count;
|
|
20658
|
+
}
|
|
20659
|
+
}
|
|
20660
|
+
}
|
|
20661
|
+
_evalBlock(block, stack) {
|
|
20662
|
+
this._evalBlockFrom(block.instructions, 0, stack);
|
|
20663
|
+
}
|
|
20664
|
+
/**
|
|
20665
|
+
* Core evaluation loop. Processes `instructions[startIdx…]` in order,
|
|
20666
|
+
* mutating `stack` as each instruction executes.
|
|
20667
|
+
*
|
|
20668
|
+
* When a `{ body } if` instruction grows the stack (the PostScript "early
|
|
20669
|
+
* exit / guard" idiom), the remaining instructions in the current array are
|
|
20670
|
+
* evaluated on **both** the true-branch stack and the false-branch stack,
|
|
20671
|
+
* then the two results are merged into PsTernaryNodes. This handles
|
|
20672
|
+
* patterns like:
|
|
20673
|
+
*
|
|
20674
|
+
* cond { pop R G B sentinel } if
|
|
20675
|
+
* … more guards …
|
|
20676
|
+
* sentinel 0 gt { defaultR defaultG defaultB } if
|
|
20677
|
+
*/
|
|
20678
|
+
_evalBlockFrom(instructions, startIdx, stack) {
|
|
20679
|
+
for (let idx = startIdx; idx < instructions.length; idx++) {
|
|
20680
|
+
if (this._failed) {
|
|
20681
|
+
break;
|
|
20682
|
+
}
|
|
20683
|
+
const instr = instructions[idx];
|
|
20684
|
+
switch (instr.type) {
|
|
20685
|
+
case PS_NODE.number:
|
|
20686
|
+
stack.push(new PsConstNode(instr.value));
|
|
20687
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
20688
|
+
this._failed = true;
|
|
20689
|
+
}
|
|
20690
|
+
break;
|
|
20691
|
+
case PS_NODE.operator:
|
|
20692
|
+
this._evalOp(instr.op, stack);
|
|
20693
|
+
break;
|
|
20694
|
+
case PS_NODE.if: {
|
|
20695
|
+
if (stack.length < 1) {
|
|
20696
|
+
this._failed = true;
|
|
20697
|
+
break;
|
|
20698
|
+
}
|
|
20699
|
+
const cond = stack.pop();
|
|
20700
|
+
const saved = stack.slice();
|
|
20701
|
+
this._evalBlock(instr.then, stack);
|
|
20702
|
+
if (this._failed) {
|
|
20703
|
+
break;
|
|
20704
|
+
}
|
|
20705
|
+
if (stack.length === saved.length) {
|
|
20706
|
+
for (let i = 0; i < stack.length; i++) {
|
|
20707
|
+
if (stack[i] !== saved[i]) {
|
|
20708
|
+
stack[i] = this._makeTernary(cond, stack[i], saved[i]);
|
|
20709
|
+
}
|
|
20710
|
+
}
|
|
20711
|
+
} else if (stack.length > saved.length) {
|
|
20712
|
+
if (cond.type === PS_NODE.const) {
|
|
20713
|
+
if (!cond.value) {
|
|
20714
|
+
stack.length = 0;
|
|
20715
|
+
stack.push(...saved);
|
|
20716
|
+
}
|
|
20717
|
+
break;
|
|
20718
|
+
}
|
|
20719
|
+
const trueStack = stack.slice();
|
|
20720
|
+
this._evalBlockFrom(instructions, idx + 1, trueStack);
|
|
20721
|
+
if (this._failed) {
|
|
20722
|
+
break;
|
|
20723
|
+
}
|
|
20724
|
+
const falseStack = saved;
|
|
20725
|
+
this._evalBlockFrom(instructions, idx + 1, falseStack);
|
|
20726
|
+
if (this._failed) {
|
|
20727
|
+
break;
|
|
20728
|
+
}
|
|
20729
|
+
if (trueStack.length !== falseStack.length) {
|
|
20730
|
+
const zero = new PsConstNode(0);
|
|
20731
|
+
while (trueStack.length < falseStack.length) {
|
|
20732
|
+
trueStack.push(zero);
|
|
20733
|
+
}
|
|
20734
|
+
while (falseStack.length < trueStack.length) {
|
|
20735
|
+
falseStack.push(zero);
|
|
20736
|
+
}
|
|
20737
|
+
}
|
|
20738
|
+
stack.length = 0;
|
|
20739
|
+
for (let i = 0; i < trueStack.length; i++) {
|
|
20740
|
+
stack.push(this._makeTernary(cond, trueStack[i], falseStack[i]));
|
|
20741
|
+
}
|
|
20742
|
+
return;
|
|
20743
|
+
} else {
|
|
20744
|
+
this._failed = true;
|
|
20745
|
+
}
|
|
20746
|
+
break;
|
|
20747
|
+
}
|
|
20748
|
+
case PS_NODE.ifelse: {
|
|
20749
|
+
if (stack.length < 1) {
|
|
20750
|
+
this._failed = true;
|
|
20751
|
+
break;
|
|
20752
|
+
}
|
|
20753
|
+
const cond = stack.pop();
|
|
20754
|
+
const snapshot = stack.slice();
|
|
20755
|
+
const thenStack = snapshot.slice();
|
|
20756
|
+
this._evalBlock(instr.then, thenStack);
|
|
20757
|
+
if (this._failed) {
|
|
20758
|
+
break;
|
|
20759
|
+
}
|
|
20760
|
+
const elseStack = snapshot.slice();
|
|
20761
|
+
this._evalBlock(instr.otherwise, elseStack);
|
|
20762
|
+
if (this._failed) {
|
|
20763
|
+
break;
|
|
20764
|
+
}
|
|
20765
|
+
if (thenStack.length !== elseStack.length) {
|
|
20766
|
+
const zero = new PsConstNode(0);
|
|
20767
|
+
while (thenStack.length < elseStack.length) {
|
|
20768
|
+
thenStack.push(zero);
|
|
20769
|
+
}
|
|
20770
|
+
while (elseStack.length < thenStack.length) {
|
|
20771
|
+
elseStack.push(zero);
|
|
20772
|
+
}
|
|
20773
|
+
}
|
|
20774
|
+
stack.length = 0;
|
|
20775
|
+
for (let i = 0; i < thenStack.length; i++) {
|
|
20776
|
+
stack.push(this._makeTernary(cond, thenStack[i], elseStack[i]));
|
|
20777
|
+
}
|
|
20778
|
+
break;
|
|
20779
|
+
}
|
|
20780
|
+
}
|
|
20781
|
+
}
|
|
20782
|
+
}
|
|
20783
|
+
_evalOp(op, stack) {
|
|
20784
|
+
if (_PSStackToTree.#binaryOps.has(op)) {
|
|
20785
|
+
if (stack.length < 2) {
|
|
20786
|
+
this._failed = true;
|
|
20787
|
+
return;
|
|
20788
|
+
}
|
|
20789
|
+
const first = stack.pop();
|
|
20790
|
+
const second = stack.pop();
|
|
20791
|
+
stack.push(this._makeBinary(op, first, second));
|
|
20792
|
+
return;
|
|
20793
|
+
}
|
|
20794
|
+
if (_PSStackToTree.#unaryOps.has(op)) {
|
|
20795
|
+
if (stack.length < 1) {
|
|
20796
|
+
this._failed = true;
|
|
20797
|
+
return;
|
|
20798
|
+
}
|
|
20799
|
+
stack.push(this._makeUnary(op, stack.pop()));
|
|
20800
|
+
return;
|
|
20801
|
+
}
|
|
20802
|
+
switch (op) {
|
|
20803
|
+
case TOKEN.true:
|
|
20804
|
+
stack.push(new PsConstNode(true));
|
|
20805
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
20806
|
+
this._failed = true;
|
|
20807
|
+
}
|
|
20808
|
+
break;
|
|
20809
|
+
case TOKEN.false:
|
|
20810
|
+
stack.push(new PsConstNode(false));
|
|
20811
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
20812
|
+
this._failed = true;
|
|
20813
|
+
}
|
|
20814
|
+
break;
|
|
20815
|
+
case TOKEN.dup:
|
|
20816
|
+
if (stack.length < 1) {
|
|
20817
|
+
this._failed = true;
|
|
20818
|
+
break;
|
|
20819
|
+
}
|
|
20820
|
+
stack.push(stack.at(-1));
|
|
20821
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
20822
|
+
this._failed = true;
|
|
20823
|
+
}
|
|
20824
|
+
break;
|
|
20825
|
+
case TOKEN.exch: {
|
|
20826
|
+
if (stack.length < 2) {
|
|
20827
|
+
this._failed = true;
|
|
20828
|
+
break;
|
|
20829
|
+
}
|
|
20830
|
+
const a = stack.pop();
|
|
20831
|
+
const b = stack.pop();
|
|
20832
|
+
stack.push(a, b);
|
|
20833
|
+
break;
|
|
20834
|
+
}
|
|
20835
|
+
case TOKEN.pop:
|
|
20836
|
+
if (stack.length < 1) {
|
|
20837
|
+
this._failed = true;
|
|
20838
|
+
break;
|
|
20839
|
+
}
|
|
20840
|
+
stack.pop();
|
|
20841
|
+
break;
|
|
20842
|
+
case TOKEN.copy: {
|
|
20843
|
+
if (stack.length < 1) {
|
|
20844
|
+
this._failed = true;
|
|
20845
|
+
break;
|
|
20846
|
+
}
|
|
20847
|
+
const nNode = stack.pop();
|
|
20848
|
+
if (nNode.type === PS_NODE.const) {
|
|
20849
|
+
const n = nNode.value | 0;
|
|
20850
|
+
if (n === 0) {
|
|
20851
|
+
} else if (n < 0 || n > stack.length) {
|
|
20852
|
+
this._failed = true;
|
|
20853
|
+
} else {
|
|
20854
|
+
stack.push(...stack.slice(-n));
|
|
20855
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
20856
|
+
this._failed = true;
|
|
20857
|
+
}
|
|
20858
|
+
}
|
|
20859
|
+
} else {
|
|
20860
|
+
this._failed = true;
|
|
20861
|
+
}
|
|
20862
|
+
break;
|
|
20863
|
+
}
|
|
20864
|
+
case TOKEN.index: {
|
|
20865
|
+
if (stack.length < 1) {
|
|
20866
|
+
this._failed = true;
|
|
20867
|
+
break;
|
|
20868
|
+
}
|
|
20869
|
+
const nNode = stack.pop();
|
|
20870
|
+
if (nNode.type === PS_NODE.const) {
|
|
20871
|
+
const n = nNode.value | 0;
|
|
20872
|
+
if (n < 0 || n >= stack.length) {
|
|
20873
|
+
this._failed = true;
|
|
20874
|
+
} else {
|
|
20875
|
+
stack.push(stack.at(-n - 1));
|
|
20876
|
+
}
|
|
20877
|
+
} else {
|
|
20878
|
+
this._failed = true;
|
|
20879
|
+
}
|
|
20880
|
+
break;
|
|
20881
|
+
}
|
|
20882
|
+
case TOKEN.roll: {
|
|
20883
|
+
if (stack.length < 2) {
|
|
20884
|
+
this._failed = true;
|
|
20885
|
+
break;
|
|
20886
|
+
}
|
|
20887
|
+
const jNode = stack.pop();
|
|
20888
|
+
const nNode = stack.pop();
|
|
20889
|
+
if (nNode.type === PS_NODE.const && jNode.type === PS_NODE.const) {
|
|
20890
|
+
const n = nNode.value | 0;
|
|
20891
|
+
if (n === 0) {
|
|
20892
|
+
} else if (n < 0 || n > stack.length) {
|
|
20893
|
+
this._failed = true;
|
|
20894
|
+
} else {
|
|
20895
|
+
const j = ((jNode.value | 0) % n + n) % n;
|
|
20896
|
+
if (j > 0) {
|
|
20897
|
+
const slice = stack.splice(-n, n);
|
|
20898
|
+
stack.push(...slice.slice(n - j), ...slice.slice(0, n - j));
|
|
20899
|
+
}
|
|
20900
|
+
}
|
|
20901
|
+
} else {
|
|
20902
|
+
this._failed = true;
|
|
20903
|
+
}
|
|
20904
|
+
break;
|
|
20905
|
+
}
|
|
20906
|
+
default:
|
|
20907
|
+
this._failed = true;
|
|
20908
|
+
break;
|
|
20909
|
+
}
|
|
20910
|
+
}
|
|
20911
|
+
/**
|
|
20912
|
+
* Create a binary tree node, applying optimizations eagerly:
|
|
20913
|
+
*
|
|
20914
|
+
* 1. Constant folding — both operands are PsConstNode → fold to PsConstNode.
|
|
20915
|
+
* 2. Reflexive simplifications — x−x→0, x xor x→0, x eq x→true, etc.
|
|
20916
|
+
* 3. Algebraic simplifications with one known operand — identity elements
|
|
20917
|
+
* (x+0→x, x*1→x, …), absorbing elements (x*0→0, x and false→false, …),
|
|
20918
|
+
* and strength reductions (x*-1→neg(x), x^0.5→sqrt(x), x^2→x*x, …).
|
|
20919
|
+
*
|
|
20920
|
+
* Recall: `first` was on top of the stack (right operand for non-commutative
|
|
20921
|
+
* ops), `second` was below (left operand). So `a b sub` → second=a, first=b
|
|
20922
|
+
* → a − b.
|
|
20923
|
+
*/
|
|
20924
|
+
_makeBinary(op, first, second) {
|
|
20925
|
+
if (first.type === PS_NODE.const && second.type === PS_NODE.const) {
|
|
20926
|
+
const v = _evalBinaryConst(op, second.value, first.value);
|
|
20927
|
+
if (v !== void 0) {
|
|
20928
|
+
return new PsConstNode(v);
|
|
20929
|
+
}
|
|
20930
|
+
}
|
|
20931
|
+
if (_nodesEqual(first, second)) {
|
|
20932
|
+
switch (op) {
|
|
20933
|
+
case TOKEN.sub:
|
|
20934
|
+
return new PsConstNode(0);
|
|
20935
|
+
// x − x → 0
|
|
20936
|
+
case TOKEN.xor:
|
|
20937
|
+
return new PsConstNode(
|
|
20938
|
+
/* eslint-disable unicorn/prefer-logical-operator-over-ternary */
|
|
20939
|
+
first.valueType === PS_VALUE_TYPE.boolean ? false : 0
|
|
20940
|
+
/* eslint-enable unicorn/prefer-logical-operator-over-ternary */
|
|
20941
|
+
);
|
|
20942
|
+
// TOKEN.mod, TOKEN.div, TOKEN.idiv are NOT simplified here:
|
|
20943
|
+
// x op x is undefined when x = 0, so we cannot fold without knowing
|
|
20944
|
+
// that x is non-zero.
|
|
20945
|
+
case TOKEN.and:
|
|
20946
|
+
case TOKEN.or:
|
|
20947
|
+
return first;
|
|
20948
|
+
// x and x → x; x or x → x
|
|
20949
|
+
case TOKEN.min:
|
|
20950
|
+
case TOKEN.max:
|
|
20951
|
+
return first;
|
|
20952
|
+
// min(x,x) → x; max(x,x) → x
|
|
20953
|
+
case TOKEN.eq:
|
|
20954
|
+
case TOKEN.ge:
|
|
20955
|
+
case TOKEN.le:
|
|
20956
|
+
return new PsConstNode(true);
|
|
20957
|
+
case TOKEN.ne:
|
|
20958
|
+
case TOKEN.gt:
|
|
20959
|
+
case TOKEN.lt:
|
|
20960
|
+
return new PsConstNode(false);
|
|
20961
|
+
}
|
|
20962
|
+
}
|
|
20963
|
+
if (first.type === PS_NODE.const) {
|
|
20964
|
+
const b = first.value;
|
|
20965
|
+
switch (op) {
|
|
20966
|
+
case TOKEN.add:
|
|
20967
|
+
if (b === 0) {
|
|
20968
|
+
return second;
|
|
20969
|
+
}
|
|
20970
|
+
break;
|
|
20971
|
+
case TOKEN.sub:
|
|
20972
|
+
if (b === 0) {
|
|
20973
|
+
return second;
|
|
20974
|
+
}
|
|
20975
|
+
break;
|
|
20976
|
+
case TOKEN.mul:
|
|
20977
|
+
if (b === 1) {
|
|
20978
|
+
return second;
|
|
20979
|
+
}
|
|
20980
|
+
if (b === 0) {
|
|
20981
|
+
return first;
|
|
20982
|
+
}
|
|
20983
|
+
if (b === -1) {
|
|
20984
|
+
return this._makeUnary(TOKEN.neg, second);
|
|
20985
|
+
}
|
|
20986
|
+
break;
|
|
20987
|
+
case TOKEN.div:
|
|
20988
|
+
if (b !== 0) {
|
|
20989
|
+
return this._makeBinary(TOKEN.mul, new PsConstNode(1 / b), second);
|
|
20990
|
+
}
|
|
20991
|
+
break;
|
|
20992
|
+
case TOKEN.idiv:
|
|
20993
|
+
if (b === 1) {
|
|
20994
|
+
return second;
|
|
20995
|
+
}
|
|
20996
|
+
break;
|
|
20997
|
+
case TOKEN.exp:
|
|
20998
|
+
if (b === 1) {
|
|
20999
|
+
return second;
|
|
21000
|
+
}
|
|
21001
|
+
if (b === -1) {
|
|
21002
|
+
return this._makeBinary(TOKEN.div, second, new PsConstNode(1));
|
|
21003
|
+
}
|
|
21004
|
+
if (b === 0.5) {
|
|
21005
|
+
return this._makeUnary(TOKEN.sqrt, second);
|
|
21006
|
+
}
|
|
21007
|
+
if (b === 0.25) {
|
|
21008
|
+
const sqrtOnce = this._makeUnary(TOKEN.sqrt, second);
|
|
21009
|
+
return this._makeUnary(TOKEN.sqrt, sqrtOnce);
|
|
21010
|
+
}
|
|
21011
|
+
if (b === 2) {
|
|
21012
|
+
return this._makeBinary(TOKEN.mul, second, second);
|
|
21013
|
+
}
|
|
21014
|
+
if (b === 3) {
|
|
21015
|
+
return this._makeBinary(
|
|
21016
|
+
TOKEN.mul,
|
|
21017
|
+
this._makeBinary(TOKEN.mul, second, second),
|
|
21018
|
+
second
|
|
21019
|
+
);
|
|
21020
|
+
}
|
|
21021
|
+
if (b === 4) {
|
|
21022
|
+
const square = this._makeBinary(TOKEN.mul, second, second);
|
|
21023
|
+
return this._makeBinary(TOKEN.mul, square, square);
|
|
21024
|
+
}
|
|
21025
|
+
if (b === 0) {
|
|
21026
|
+
return new PsConstNode(1);
|
|
21027
|
+
}
|
|
21028
|
+
break;
|
|
21029
|
+
case TOKEN.and:
|
|
21030
|
+
if (b === true) {
|
|
21031
|
+
return second;
|
|
21032
|
+
}
|
|
21033
|
+
if (b === false) {
|
|
21034
|
+
return first;
|
|
21035
|
+
}
|
|
21036
|
+
break;
|
|
21037
|
+
case TOKEN.or:
|
|
21038
|
+
if (b === false) {
|
|
21039
|
+
return second;
|
|
21040
|
+
}
|
|
21041
|
+
if (b === true) {
|
|
21042
|
+
return first;
|
|
21043
|
+
}
|
|
21044
|
+
break;
|
|
21045
|
+
case TOKEN.min:
|
|
21046
|
+
if (second.type === PS_NODE.binary && second.op === TOKEN.max && second.first.type === PS_NODE.const && second.first.value >= b) {
|
|
21047
|
+
return first;
|
|
21048
|
+
}
|
|
21049
|
+
break;
|
|
21050
|
+
case TOKEN.max:
|
|
21051
|
+
if (second.type === PS_NODE.binary && second.op === TOKEN.min && second.first.type === PS_NODE.const && second.first.value <= b) {
|
|
21052
|
+
return first;
|
|
21053
|
+
}
|
|
21054
|
+
break;
|
|
21055
|
+
}
|
|
21056
|
+
}
|
|
21057
|
+
if (second.type === PS_NODE.const) {
|
|
21058
|
+
const a = second.value;
|
|
21059
|
+
switch (op) {
|
|
21060
|
+
case TOKEN.add:
|
|
21061
|
+
if (a === 0) {
|
|
21062
|
+
return first;
|
|
21063
|
+
}
|
|
21064
|
+
break;
|
|
21065
|
+
case TOKEN.sub:
|
|
21066
|
+
if (a === 0) {
|
|
21067
|
+
return this._makeUnary(TOKEN.neg, first);
|
|
21068
|
+
}
|
|
21069
|
+
break;
|
|
21070
|
+
case TOKEN.mul:
|
|
21071
|
+
if (a === 1) {
|
|
21072
|
+
return first;
|
|
21073
|
+
}
|
|
21074
|
+
if (a === 0) {
|
|
21075
|
+
return second;
|
|
21076
|
+
}
|
|
21077
|
+
if (a === -1) {
|
|
21078
|
+
return this._makeUnary(TOKEN.neg, first);
|
|
21079
|
+
}
|
|
21080
|
+
break;
|
|
21081
|
+
case TOKEN.and:
|
|
21082
|
+
if (a === true) {
|
|
21083
|
+
return first;
|
|
21084
|
+
}
|
|
21085
|
+
if (a === false) {
|
|
21086
|
+
return second;
|
|
21087
|
+
}
|
|
21088
|
+
break;
|
|
21089
|
+
case TOKEN.or:
|
|
21090
|
+
if (a === false) {
|
|
21091
|
+
return first;
|
|
21092
|
+
}
|
|
21093
|
+
if (a === true) {
|
|
21094
|
+
return second;
|
|
21095
|
+
}
|
|
21096
|
+
break;
|
|
21097
|
+
}
|
|
21098
|
+
}
|
|
21099
|
+
return new PsBinaryNode(
|
|
21100
|
+
op,
|
|
21101
|
+
first,
|
|
21102
|
+
second,
|
|
21103
|
+
_binaryValueType(op, first.valueType, second.valueType)
|
|
21104
|
+
);
|
|
21105
|
+
}
|
|
21106
|
+
/**
|
|
21107
|
+
* Create a unary tree node, applying optimizations eagerly:
|
|
21108
|
+
*
|
|
21109
|
+
* 1. Constant folding.
|
|
21110
|
+
* 2. not(comparison) → negated comparison: not(a eq b) → a ne b, etc.
|
|
21111
|
+
* 3. neg(a − b) → b − a.
|
|
21112
|
+
* 4. Double-negation: neg(neg(x)) → x, not(not(x)) → x.
|
|
21113
|
+
* 5. abs(neg(x)) → abs(x).
|
|
21114
|
+
* 6. Idempotent: f(f(x)) → f(x) for abs, ceiling, floor, round, etc.
|
|
21115
|
+
*/
|
|
21116
|
+
_makeUnary(op, operand) {
|
|
21117
|
+
if (operand.type === PS_NODE.const) {
|
|
21118
|
+
const v = _evalUnaryConst(op, operand.value);
|
|
21119
|
+
if (v !== void 0) {
|
|
21120
|
+
return new PsConstNode(v);
|
|
21121
|
+
}
|
|
21122
|
+
}
|
|
21123
|
+
if (op === TOKEN.not && operand.type === PS_NODE.binary) {
|
|
21124
|
+
const negated = _PSStackToTree.#negatedComparison.get(operand.op);
|
|
21125
|
+
if (negated !== void 0) {
|
|
21126
|
+
return new PsBinaryNode(
|
|
21127
|
+
negated,
|
|
21128
|
+
operand.first,
|
|
21129
|
+
operand.second,
|
|
21130
|
+
PS_VALUE_TYPE.boolean
|
|
21131
|
+
);
|
|
21132
|
+
}
|
|
21133
|
+
}
|
|
21134
|
+
if (op === TOKEN.neg && operand.type === PS_NODE.binary && operand.op === TOKEN.sub) {
|
|
21135
|
+
return this._makeBinary(TOKEN.sub, operand.second, operand.first);
|
|
21136
|
+
}
|
|
21137
|
+
if (operand.type === PS_NODE.unary) {
|
|
21138
|
+
if (op === TOKEN.neg && operand.op === TOKEN.neg || op === TOKEN.not && operand.op === TOKEN.not) {
|
|
21139
|
+
return operand.operand;
|
|
21140
|
+
}
|
|
21141
|
+
if (op === TOKEN.abs && operand.op === TOKEN.neg) {
|
|
21142
|
+
return this._makeUnary(TOKEN.abs, operand.operand);
|
|
21143
|
+
}
|
|
21144
|
+
if (_PSStackToTree.#idempotentUnary.has(op) && op === operand.op) {
|
|
21145
|
+
return operand;
|
|
21146
|
+
}
|
|
21147
|
+
}
|
|
21148
|
+
return new PsUnaryNode(op, operand, _unaryValueType(op, operand.valueType));
|
|
21149
|
+
}
|
|
21150
|
+
/**
|
|
21151
|
+
* Create a ternary node, applying optimizations eagerly:
|
|
21152
|
+
*
|
|
21153
|
+
* 1. Constant condition — fold to the live branch.
|
|
21154
|
+
* 2. Identical branches — the condition is irrelevant, return either branch.
|
|
21155
|
+
* 3. Boolean branch constants — `cond ? true : false` → cond,
|
|
21156
|
+
* `cond ? false : true` → not(cond).
|
|
21157
|
+
* 4. Ternary → branchless min/max when the condition compares two numeric
|
|
21158
|
+
* expressions that are also the two branches.
|
|
21159
|
+
*/
|
|
21160
|
+
_makeTernary(cond, then, otherwise) {
|
|
21161
|
+
if (cond.type === PS_NODE.const) {
|
|
21162
|
+
return cond.value ? then : otherwise;
|
|
21163
|
+
}
|
|
21164
|
+
if (_nodesEqual(then, otherwise)) {
|
|
21165
|
+
return then;
|
|
21166
|
+
}
|
|
21167
|
+
if (then.type === PS_NODE.const && otherwise.type === PS_NODE.const) {
|
|
21168
|
+
if (then.value === true && otherwise.value === false) {
|
|
21169
|
+
return cond;
|
|
21170
|
+
}
|
|
21171
|
+
if (then.value === false && otherwise.value === true) {
|
|
21172
|
+
return this._makeUnary(TOKEN.not, cond);
|
|
21173
|
+
}
|
|
21174
|
+
}
|
|
21175
|
+
if (cond.type === PS_NODE.binary) {
|
|
21176
|
+
const { op: cop, first: cf, second: cs } = cond;
|
|
21177
|
+
if (cop === TOKEN.gt || cop === TOKEN.ge) {
|
|
21178
|
+
if (_nodesEqual(then, cf) && _nodesEqual(otherwise, cs)) {
|
|
21179
|
+
return this._makeBinary(TOKEN.min, cf, cs);
|
|
21180
|
+
}
|
|
21181
|
+
if (_nodesEqual(then, cs) && _nodesEqual(otherwise, cf)) {
|
|
21182
|
+
return this._makeBinary(TOKEN.max, cf, cs);
|
|
21183
|
+
}
|
|
21184
|
+
} else if (cop === TOKEN.lt || cop === TOKEN.le) {
|
|
21185
|
+
if (_nodesEqual(then, cf) && _nodesEqual(otherwise, cs)) {
|
|
21186
|
+
return this._makeBinary(TOKEN.max, cf, cs);
|
|
21187
|
+
}
|
|
21188
|
+
if (_nodesEqual(then, cs) && _nodesEqual(otherwise, cf)) {
|
|
21189
|
+
return this._makeBinary(TOKEN.min, cf, cs);
|
|
21190
|
+
}
|
|
21191
|
+
}
|
|
21192
|
+
}
|
|
21193
|
+
return new PsTernaryNode(
|
|
21194
|
+
cond,
|
|
21195
|
+
then,
|
|
21196
|
+
otherwise,
|
|
21197
|
+
then.valueType === otherwise.valueType ? then.valueType : PS_VALUE_TYPE.unknown
|
|
21198
|
+
);
|
|
21199
|
+
}
|
|
21200
|
+
};
|
|
21201
|
+
|
|
21202
|
+
// src/core/postscript/js_evaluator.js
|
|
21203
|
+
var OP = {
|
|
21204
|
+
ARG: 0,
|
|
21205
|
+
// [ARG, idx]
|
|
21206
|
+
CONST: 1,
|
|
21207
|
+
// [CONST, val]
|
|
21208
|
+
STORE: 2,
|
|
21209
|
+
// [STORE, slot, min, max] clamp(pop()) → mem[slot]
|
|
21210
|
+
IF: 3,
|
|
21211
|
+
// [IF, target] jump when top-of-stack === 0
|
|
21212
|
+
JUMP: 4,
|
|
21213
|
+
// [JUMP, target] unconditional
|
|
21214
|
+
ABS: 5,
|
|
21215
|
+
NEG: 6,
|
|
21216
|
+
CEIL: 7,
|
|
21217
|
+
FLOOR: 8,
|
|
21218
|
+
ROUND: 9,
|
|
21219
|
+
// floor(x + 0.5)
|
|
21220
|
+
TRUNC: 10,
|
|
21221
|
+
NOT_B: 11,
|
|
21222
|
+
// boolean NOT
|
|
21223
|
+
NOT_N: 12,
|
|
21224
|
+
// bitwise NOT
|
|
21225
|
+
SQRT: 13,
|
|
21226
|
+
SIN: 14,
|
|
21227
|
+
// degrees in/out
|
|
21228
|
+
COS: 15,
|
|
21229
|
+
LN: 16,
|
|
21230
|
+
LOG10: 17,
|
|
21231
|
+
CVI: 18,
|
|
21232
|
+
SHIFT: 19,
|
|
21233
|
+
// [SHIFT, amount] +ve = left, −ve = right
|
|
21234
|
+
// Binary ops: second below, first on top; result = second OP first.
|
|
21235
|
+
ADD: 20,
|
|
21236
|
+
SUB: 21,
|
|
21237
|
+
MUL: 22,
|
|
21238
|
+
DIV: 23,
|
|
21239
|
+
// 0 when divisor is 0
|
|
21240
|
+
IDIV: 24,
|
|
21241
|
+
// 0 when divisor is 0
|
|
21242
|
+
MOD: 25,
|
|
21243
|
+
// 0 when divisor is 0
|
|
21244
|
+
POW: 26,
|
|
21245
|
+
EQ: 27,
|
|
21246
|
+
NE: 28,
|
|
21247
|
+
GT: 29,
|
|
21248
|
+
GE: 30,
|
|
21249
|
+
LT: 31,
|
|
21250
|
+
LE: 32,
|
|
21251
|
+
AND: 33,
|
|
21252
|
+
OR: 34,
|
|
21253
|
+
XOR: 35,
|
|
21254
|
+
ATAN: 36,
|
|
21255
|
+
// atan2(second, first) → degrees [0, 360)
|
|
21256
|
+
MIN: 37,
|
|
21257
|
+
MAX: 38,
|
|
21258
|
+
TEE_TMP: 39,
|
|
21259
|
+
// [TEE_TMP, slot] peek top of stack → tmp[slot], leave on stack
|
|
21260
|
+
LOAD_TMP: 40
|
|
21261
|
+
// [LOAD_TMP, slot] push tmp[slot]
|
|
21262
|
+
};
|
|
21263
|
+
var _DEG_TO_RAD = Math.PI / 180;
|
|
21264
|
+
var _RAD_TO_DEG = 180 / Math.PI;
|
|
21265
|
+
var PsJsCompiler = class {
|
|
21266
|
+
// Safe because JS is single-threaded.
|
|
21267
|
+
static #stack = new Float64Array(64);
|
|
21268
|
+
static #tmp = new Float64Array(64);
|
|
21269
|
+
constructor(domain, range) {
|
|
21270
|
+
this.nIn = domain.length >> 1;
|
|
21271
|
+
this.nOut = range.length >> 1;
|
|
21272
|
+
this.range = range;
|
|
21273
|
+
this.ir = [];
|
|
21274
|
+
this._tmpMap = /* @__PURE__ */ new Map();
|
|
21275
|
+
this._nextTmp = 0;
|
|
21276
|
+
}
|
|
21277
|
+
_compileNode(node) {
|
|
21278
|
+
if (node.shared) {
|
|
21279
|
+
const cached = this._tmpMap.get(node);
|
|
21280
|
+
if (cached !== void 0) {
|
|
21281
|
+
this.ir.push(OP.LOAD_TMP, cached);
|
|
21282
|
+
return true;
|
|
21283
|
+
}
|
|
21284
|
+
if (!this._compileNodeImpl(node)) {
|
|
21285
|
+
return false;
|
|
21286
|
+
}
|
|
21287
|
+
const slot = this._nextTmp++;
|
|
21288
|
+
this._tmpMap.set(node, slot);
|
|
21289
|
+
this.ir.push(OP.TEE_TMP, slot);
|
|
21290
|
+
return true;
|
|
21291
|
+
}
|
|
21292
|
+
return this._compileNodeImpl(node);
|
|
21293
|
+
}
|
|
21294
|
+
_compileNodeImpl(node) {
|
|
21295
|
+
switch (node.type) {
|
|
21296
|
+
case PS_NODE.arg:
|
|
21297
|
+
this.ir.push(OP.ARG, node.index);
|
|
21298
|
+
return true;
|
|
21299
|
+
case PS_NODE.const: {
|
|
21300
|
+
const v = node.value;
|
|
21301
|
+
this.ir.push(OP.CONST, typeof v === "boolean" ? Number(v) : v);
|
|
21302
|
+
return true;
|
|
21303
|
+
}
|
|
21304
|
+
case PS_NODE.unary:
|
|
21305
|
+
return this._compileUnary(node);
|
|
21306
|
+
case PS_NODE.binary:
|
|
21307
|
+
return this._compileBinary(node);
|
|
21308
|
+
case PS_NODE.ternary:
|
|
21309
|
+
return this._compileTernary(node);
|
|
21310
|
+
default:
|
|
21311
|
+
return false;
|
|
21312
|
+
}
|
|
21313
|
+
}
|
|
21314
|
+
_compileUnary(node) {
|
|
21315
|
+
const { op, operand, valueType } = node;
|
|
21316
|
+
if (op === TOKEN.cvr) {
|
|
21317
|
+
return this._compileNode(operand);
|
|
21318
|
+
}
|
|
21319
|
+
if (!this._compileNode(operand)) {
|
|
21320
|
+
return false;
|
|
21321
|
+
}
|
|
21322
|
+
switch (op) {
|
|
21323
|
+
case TOKEN.abs:
|
|
21324
|
+
this.ir.push(OP.ABS);
|
|
21325
|
+
break;
|
|
21326
|
+
case TOKEN.neg:
|
|
21327
|
+
this.ir.push(OP.NEG);
|
|
21328
|
+
break;
|
|
21329
|
+
case TOKEN.ceiling:
|
|
21330
|
+
this.ir.push(OP.CEIL);
|
|
21331
|
+
break;
|
|
21332
|
+
case TOKEN.floor:
|
|
21333
|
+
this.ir.push(OP.FLOOR);
|
|
21334
|
+
break;
|
|
21335
|
+
case TOKEN.round:
|
|
21336
|
+
this.ir.push(OP.ROUND);
|
|
21337
|
+
break;
|
|
21338
|
+
case TOKEN.truncate:
|
|
21339
|
+
this.ir.push(OP.TRUNC);
|
|
21340
|
+
break;
|
|
21341
|
+
case TOKEN.sqrt:
|
|
21342
|
+
this.ir.push(OP.SQRT);
|
|
21343
|
+
break;
|
|
21344
|
+
case TOKEN.sin:
|
|
21345
|
+
this.ir.push(OP.SIN);
|
|
21346
|
+
break;
|
|
21347
|
+
case TOKEN.cos:
|
|
21348
|
+
this.ir.push(OP.COS);
|
|
21349
|
+
break;
|
|
21350
|
+
case TOKEN.ln:
|
|
21351
|
+
this.ir.push(OP.LN);
|
|
21352
|
+
break;
|
|
21353
|
+
case TOKEN.log:
|
|
21354
|
+
this.ir.push(OP.LOG10);
|
|
21355
|
+
break;
|
|
21356
|
+
case TOKEN.cvi:
|
|
21357
|
+
this.ir.push(OP.CVI);
|
|
21358
|
+
break;
|
|
21359
|
+
case TOKEN.not:
|
|
21360
|
+
if (valueType === PS_VALUE_TYPE.boolean) {
|
|
21361
|
+
this.ir.push(OP.NOT_B);
|
|
21362
|
+
} else if (valueType === PS_VALUE_TYPE.numeric) {
|
|
21363
|
+
this.ir.push(OP.NOT_N);
|
|
21364
|
+
} else {
|
|
21365
|
+
return false;
|
|
21366
|
+
}
|
|
21367
|
+
break;
|
|
21368
|
+
default:
|
|
21369
|
+
return false;
|
|
21370
|
+
}
|
|
21371
|
+
return true;
|
|
21372
|
+
}
|
|
21373
|
+
_compileBinary(node) {
|
|
21374
|
+
const { op, first, second } = node;
|
|
21375
|
+
if (op === TOKEN.bitshift) {
|
|
21376
|
+
if (first.type !== PS_NODE.const || !Number.isInteger(first.value) || !this._compileNode(second)) {
|
|
21377
|
+
return false;
|
|
21378
|
+
}
|
|
21379
|
+
this.ir.push(OP.SHIFT, first.value);
|
|
21380
|
+
return true;
|
|
21381
|
+
}
|
|
21382
|
+
if (!this._compileNode(second) || !this._compileNode(first)) {
|
|
21383
|
+
return false;
|
|
21384
|
+
}
|
|
21385
|
+
switch (op) {
|
|
21386
|
+
case TOKEN.add:
|
|
21387
|
+
this.ir.push(OP.ADD);
|
|
21388
|
+
break;
|
|
21389
|
+
case TOKEN.sub:
|
|
21390
|
+
this.ir.push(OP.SUB);
|
|
21391
|
+
break;
|
|
21392
|
+
case TOKEN.mul:
|
|
21393
|
+
this.ir.push(OP.MUL);
|
|
21394
|
+
break;
|
|
21395
|
+
case TOKEN.div:
|
|
21396
|
+
this.ir.push(OP.DIV);
|
|
21397
|
+
break;
|
|
21398
|
+
case TOKEN.idiv:
|
|
21399
|
+
this.ir.push(OP.IDIV);
|
|
21400
|
+
break;
|
|
21401
|
+
case TOKEN.mod:
|
|
21402
|
+
this.ir.push(OP.MOD);
|
|
21403
|
+
break;
|
|
21404
|
+
case TOKEN.exp:
|
|
21405
|
+
this.ir.push(OP.POW);
|
|
21406
|
+
break;
|
|
21407
|
+
case TOKEN.eq:
|
|
21408
|
+
this.ir.push(OP.EQ);
|
|
21409
|
+
break;
|
|
21410
|
+
case TOKEN.ne:
|
|
21411
|
+
this.ir.push(OP.NE);
|
|
21412
|
+
break;
|
|
21413
|
+
case TOKEN.gt:
|
|
21414
|
+
this.ir.push(OP.GT);
|
|
21415
|
+
break;
|
|
21416
|
+
case TOKEN.ge:
|
|
21417
|
+
this.ir.push(OP.GE);
|
|
21418
|
+
break;
|
|
21419
|
+
case TOKEN.lt:
|
|
21420
|
+
this.ir.push(OP.LT);
|
|
21421
|
+
break;
|
|
21422
|
+
case TOKEN.le:
|
|
21423
|
+
this.ir.push(OP.LE);
|
|
21424
|
+
break;
|
|
21425
|
+
case TOKEN.and:
|
|
21426
|
+
this.ir.push(OP.AND);
|
|
21427
|
+
break;
|
|
21428
|
+
case TOKEN.or:
|
|
21429
|
+
this.ir.push(OP.OR);
|
|
21430
|
+
break;
|
|
21431
|
+
case TOKEN.xor:
|
|
21432
|
+
this.ir.push(OP.XOR);
|
|
21433
|
+
break;
|
|
21434
|
+
case TOKEN.atan:
|
|
21435
|
+
this.ir.push(OP.ATAN);
|
|
21436
|
+
break;
|
|
21437
|
+
case TOKEN.min:
|
|
21438
|
+
this.ir.push(OP.MIN);
|
|
21439
|
+
break;
|
|
21440
|
+
case TOKEN.max:
|
|
21441
|
+
this.ir.push(OP.MAX);
|
|
21442
|
+
break;
|
|
21443
|
+
default:
|
|
21444
|
+
return false;
|
|
21445
|
+
}
|
|
21446
|
+
return true;
|
|
21447
|
+
}
|
|
21448
|
+
_compileTernary(node) {
|
|
21449
|
+
if (!this._compileNode(node.cond)) {
|
|
21450
|
+
return false;
|
|
21451
|
+
}
|
|
21452
|
+
this.ir.push(OP.IF, 0);
|
|
21453
|
+
const ifPatch = this.ir.length - 1;
|
|
21454
|
+
if (!this._compileNode(node.then)) {
|
|
21455
|
+
return false;
|
|
21456
|
+
}
|
|
21457
|
+
this.ir.push(OP.JUMP, 0);
|
|
21458
|
+
const jumpPatch = this.ir.length - 1;
|
|
21459
|
+
this.ir[ifPatch] = this.ir.length;
|
|
21460
|
+
if (!this._compileNode(node.otherwise)) {
|
|
21461
|
+
return false;
|
|
21462
|
+
}
|
|
21463
|
+
this.ir[jumpPatch] = this.ir.length;
|
|
21464
|
+
return true;
|
|
21465
|
+
}
|
|
21466
|
+
compile(program) {
|
|
21467
|
+
const outputs = new PSStackToTree().evaluate(program, this.nIn);
|
|
21468
|
+
if (!outputs || outputs.length < this.nOut) {
|
|
21469
|
+
return null;
|
|
21470
|
+
}
|
|
21471
|
+
for (let i = 0; i < this.nOut; i++) {
|
|
21472
|
+
if (!this._compileNode(outputs[i])) {
|
|
21473
|
+
return null;
|
|
21474
|
+
}
|
|
21475
|
+
const min = this.range[i * 2];
|
|
21476
|
+
const max = this.range[i * 2 + 1];
|
|
21477
|
+
this.ir.push(OP.STORE, i, min, max);
|
|
21478
|
+
}
|
|
21479
|
+
return new Float64Array(this.ir);
|
|
21480
|
+
}
|
|
21481
|
+
static execute(ir, src, srcOffset, dest, destOffset) {
|
|
21482
|
+
let ip = 0, sp = 0;
|
|
21483
|
+
const n = ir.length;
|
|
21484
|
+
const stack = this.#stack;
|
|
21485
|
+
const tmp = this.#tmp;
|
|
21486
|
+
while (ip < n) {
|
|
21487
|
+
switch (ir[ip++] | 0) {
|
|
21488
|
+
case OP.ARG:
|
|
21489
|
+
stack[sp++] = src[srcOffset + (ir[ip++] | 0)];
|
|
21490
|
+
break;
|
|
21491
|
+
case OP.CONST:
|
|
21492
|
+
stack[sp++] = ir[ip++];
|
|
21493
|
+
break;
|
|
21494
|
+
case OP.STORE: {
|
|
21495
|
+
const slot = ir[ip++] | 0;
|
|
21496
|
+
const min = ir[ip++];
|
|
21497
|
+
const max = ir[ip++];
|
|
21498
|
+
dest[destOffset + slot] = MathClamp(stack[--sp], min, max);
|
|
21499
|
+
break;
|
|
21500
|
+
}
|
|
21501
|
+
case OP.IF: {
|
|
21502
|
+
const tgt = ir[ip++];
|
|
21503
|
+
if (stack[--sp] === 0) {
|
|
21504
|
+
ip = tgt;
|
|
21505
|
+
}
|
|
21506
|
+
break;
|
|
21507
|
+
}
|
|
21508
|
+
case OP.JUMP:
|
|
21509
|
+
ip = ir[ip];
|
|
21510
|
+
break;
|
|
21511
|
+
case OP.ABS:
|
|
21512
|
+
stack[sp - 1] = Math.abs(stack[sp - 1]);
|
|
21513
|
+
break;
|
|
21514
|
+
case OP.NEG:
|
|
21515
|
+
stack[sp - 1] = -stack[sp - 1];
|
|
21516
|
+
break;
|
|
21517
|
+
case OP.CEIL:
|
|
21518
|
+
stack[sp - 1] = Math.ceil(stack[sp - 1]);
|
|
21519
|
+
break;
|
|
21520
|
+
case OP.FLOOR:
|
|
21521
|
+
stack[sp - 1] = Math.floor(stack[sp - 1]);
|
|
21522
|
+
break;
|
|
21523
|
+
case OP.ROUND:
|
|
21524
|
+
stack[sp - 1] = Math.floor(stack[sp - 1] + 0.5);
|
|
21525
|
+
break;
|
|
21526
|
+
case OP.TRUNC:
|
|
21527
|
+
stack[sp - 1] = Math.trunc(stack[sp - 1]);
|
|
21528
|
+
break;
|
|
21529
|
+
case OP.NOT_B:
|
|
21530
|
+
stack[sp - 1] = stack[sp - 1] !== 0 ? 0 : 1;
|
|
21531
|
+
break;
|
|
21532
|
+
case OP.NOT_N:
|
|
21533
|
+
stack[sp - 1] = ~(stack[sp - 1] | 0);
|
|
21534
|
+
break;
|
|
21535
|
+
case OP.SQRT:
|
|
21536
|
+
stack[sp - 1] = Math.sqrt(stack[sp - 1]);
|
|
21537
|
+
break;
|
|
21538
|
+
case OP.SIN:
|
|
21539
|
+
stack[sp - 1] = Math.sin(stack[sp - 1] % 360 * _DEG_TO_RAD);
|
|
21540
|
+
break;
|
|
21541
|
+
case OP.COS:
|
|
21542
|
+
stack[sp - 1] = Math.cos(stack[sp - 1] % 360 * _DEG_TO_RAD);
|
|
21543
|
+
break;
|
|
21544
|
+
case OP.LN:
|
|
21545
|
+
stack[sp - 1] = Math.log(stack[sp - 1]);
|
|
21546
|
+
break;
|
|
21547
|
+
case OP.LOG10:
|
|
21548
|
+
stack[sp - 1] = Math.log10(stack[sp - 1]);
|
|
21549
|
+
break;
|
|
21550
|
+
case OP.CVI:
|
|
21551
|
+
stack[sp - 1] = Math.trunc(stack[sp - 1]) | 0;
|
|
21552
|
+
break;
|
|
21553
|
+
case OP.SHIFT: {
|
|
21554
|
+
const amt = ir[ip++];
|
|
21555
|
+
const v = stack[sp - 1] | 0;
|
|
21556
|
+
if (amt > 0) {
|
|
21557
|
+
stack[sp - 1] = v << amt;
|
|
21558
|
+
} else if (amt < 0) {
|
|
21559
|
+
stack[sp - 1] = v >> -amt;
|
|
21560
|
+
} else {
|
|
21561
|
+
stack[sp - 1] = v;
|
|
21562
|
+
}
|
|
21563
|
+
break;
|
|
21564
|
+
}
|
|
21565
|
+
case OP.ADD: {
|
|
21566
|
+
const b = stack[--sp];
|
|
21567
|
+
stack[sp - 1] += b;
|
|
21568
|
+
break;
|
|
21569
|
+
}
|
|
21570
|
+
case OP.SUB: {
|
|
21571
|
+
const b = stack[--sp];
|
|
21572
|
+
stack[sp - 1] -= b;
|
|
21573
|
+
break;
|
|
21574
|
+
}
|
|
21575
|
+
case OP.MUL: {
|
|
21576
|
+
const b = stack[--sp];
|
|
21577
|
+
stack[sp - 1] *= b;
|
|
21578
|
+
break;
|
|
21579
|
+
}
|
|
21580
|
+
case OP.DIV: {
|
|
21581
|
+
const b = stack[--sp];
|
|
21582
|
+
stack[sp - 1] = b !== 0 ? stack[sp - 1] / b : 0;
|
|
21583
|
+
break;
|
|
21584
|
+
}
|
|
21585
|
+
case OP.IDIV: {
|
|
21586
|
+
const b = stack[--sp];
|
|
21587
|
+
stack[sp - 1] = b !== 0 ? Math.trunc(stack[sp - 1] / b) : 0;
|
|
21588
|
+
break;
|
|
21589
|
+
}
|
|
21590
|
+
case OP.MOD: {
|
|
21591
|
+
const b = stack[--sp];
|
|
21592
|
+
stack[sp - 1] = b !== 0 ? stack[sp - 1] % b : 0;
|
|
21593
|
+
break;
|
|
21594
|
+
}
|
|
21595
|
+
case OP.POW: {
|
|
21596
|
+
const b = stack[--sp];
|
|
21597
|
+
stack[sp - 1] **= b;
|
|
21598
|
+
break;
|
|
21599
|
+
}
|
|
21600
|
+
case OP.EQ: {
|
|
21601
|
+
const b = stack[--sp];
|
|
21602
|
+
stack[sp - 1] = stack[sp - 1] === b ? 1 : 0;
|
|
21603
|
+
break;
|
|
21604
|
+
}
|
|
21605
|
+
case OP.NE: {
|
|
21606
|
+
const b = stack[--sp];
|
|
21607
|
+
stack[sp - 1] = stack[sp - 1] !== b ? 1 : 0;
|
|
21608
|
+
break;
|
|
21609
|
+
}
|
|
21610
|
+
case OP.GT: {
|
|
21611
|
+
const b = stack[--sp];
|
|
21612
|
+
stack[sp - 1] = stack[sp - 1] > b ? 1 : 0;
|
|
21613
|
+
break;
|
|
21614
|
+
}
|
|
21615
|
+
case OP.GE: {
|
|
21616
|
+
const b = stack[--sp];
|
|
21617
|
+
stack[sp - 1] = stack[sp - 1] >= b ? 1 : 0;
|
|
21618
|
+
break;
|
|
21619
|
+
}
|
|
21620
|
+
case OP.LT: {
|
|
21621
|
+
const b = stack[--sp];
|
|
21622
|
+
stack[sp - 1] = stack[sp - 1] < b ? 1 : 0;
|
|
21623
|
+
break;
|
|
21624
|
+
}
|
|
21625
|
+
case OP.LE: {
|
|
21626
|
+
const b = stack[--sp];
|
|
21627
|
+
stack[sp - 1] = stack[sp - 1] <= b ? 1 : 0;
|
|
21628
|
+
break;
|
|
21629
|
+
}
|
|
21630
|
+
case OP.AND: {
|
|
21631
|
+
const b = stack[--sp] | 0;
|
|
21632
|
+
stack[sp - 1] = (stack[sp - 1] | 0) & b;
|
|
21633
|
+
break;
|
|
21634
|
+
}
|
|
21635
|
+
case OP.OR: {
|
|
21636
|
+
const b = stack[--sp] | 0;
|
|
21637
|
+
stack[sp - 1] = stack[sp - 1] | 0 | b;
|
|
21638
|
+
break;
|
|
21639
|
+
}
|
|
21640
|
+
case OP.XOR: {
|
|
21641
|
+
const b = stack[--sp] | 0;
|
|
21642
|
+
stack[sp - 1] = (stack[sp - 1] | 0) ^ b;
|
|
21643
|
+
break;
|
|
21644
|
+
}
|
|
21645
|
+
case OP.ATAN: {
|
|
21646
|
+
const b = stack[--sp];
|
|
21647
|
+
const deg = Math.atan2(stack[sp - 1], b) * _RAD_TO_DEG;
|
|
21648
|
+
stack[sp - 1] = deg < 0 ? deg + 360 : deg;
|
|
21649
|
+
break;
|
|
21650
|
+
}
|
|
21651
|
+
case OP.MIN: {
|
|
21652
|
+
const b = stack[--sp];
|
|
21653
|
+
stack[sp - 1] = Math.min(stack[sp - 1], b);
|
|
21654
|
+
break;
|
|
21655
|
+
}
|
|
21656
|
+
case OP.MAX: {
|
|
21657
|
+
const b = stack[--sp];
|
|
21658
|
+
stack[sp - 1] = Math.max(stack[sp - 1], b);
|
|
21659
|
+
break;
|
|
21660
|
+
}
|
|
21661
|
+
case OP.TEE_TMP:
|
|
21662
|
+
tmp[ir[ip++] | 0] = stack[sp - 1];
|
|
21663
|
+
break;
|
|
21664
|
+
case OP.LOAD_TMP:
|
|
21665
|
+
stack[sp++] = tmp[ir[ip++] | 0];
|
|
21666
|
+
break;
|
|
21667
|
+
}
|
|
21668
|
+
}
|
|
21669
|
+
}
|
|
21670
|
+
};
|
|
21671
|
+
var PSStackBasedInterpreter = class {
|
|
21672
|
+
// Safe: JS is single-threaded.
|
|
21673
|
+
static #stack = new Float64Array(100);
|
|
21674
|
+
static #sp = 0;
|
|
21675
|
+
static #push(v) {
|
|
21676
|
+
if (this.#sp < this.#stack.length) {
|
|
21677
|
+
this.#stack[this.#sp++] = v;
|
|
21678
|
+
}
|
|
21679
|
+
}
|
|
21680
|
+
static #execOp(op) {
|
|
21681
|
+
const stack = this.#stack;
|
|
21682
|
+
switch (op) {
|
|
21683
|
+
case TOKEN.true:
|
|
21684
|
+
this.#push(1);
|
|
21685
|
+
break;
|
|
21686
|
+
case TOKEN.false:
|
|
21687
|
+
this.#push(0);
|
|
21688
|
+
break;
|
|
21689
|
+
case TOKEN.abs:
|
|
21690
|
+
stack[this.#sp - 1] = Math.abs(stack[this.#sp - 1]);
|
|
21691
|
+
break;
|
|
21692
|
+
case TOKEN.neg:
|
|
21693
|
+
stack[this.#sp - 1] = -stack[this.#sp - 1];
|
|
21694
|
+
break;
|
|
21695
|
+
case TOKEN.ceiling:
|
|
21696
|
+
stack[this.#sp - 1] = Math.ceil(stack[this.#sp - 1]);
|
|
21697
|
+
break;
|
|
21698
|
+
case TOKEN.floor:
|
|
21699
|
+
stack[this.#sp - 1] = Math.floor(stack[this.#sp - 1]);
|
|
21700
|
+
break;
|
|
21701
|
+
case TOKEN.round:
|
|
21702
|
+
stack[this.#sp - 1] = Math.floor(stack[this.#sp - 1] + 0.5);
|
|
21703
|
+
break;
|
|
21704
|
+
case TOKEN.truncate:
|
|
21705
|
+
stack[this.#sp - 1] = Math.trunc(stack[this.#sp - 1]);
|
|
21706
|
+
break;
|
|
21707
|
+
case TOKEN.sqrt:
|
|
21708
|
+
stack[this.#sp - 1] = Math.sqrt(stack[this.#sp - 1]);
|
|
21709
|
+
break;
|
|
21710
|
+
case TOKEN.sin:
|
|
21711
|
+
stack[this.#sp - 1] = Math.sin(
|
|
21712
|
+
stack[this.#sp - 1] % 360 * _DEG_TO_RAD
|
|
21713
|
+
);
|
|
21714
|
+
break;
|
|
21715
|
+
case TOKEN.cos:
|
|
21716
|
+
stack[this.#sp - 1] = Math.cos(
|
|
21717
|
+
stack[this.#sp - 1] % 360 * _DEG_TO_RAD
|
|
21718
|
+
);
|
|
21719
|
+
break;
|
|
21720
|
+
case TOKEN.ln:
|
|
21721
|
+
stack[this.#sp - 1] = Math.log(stack[this.#sp - 1]);
|
|
21722
|
+
break;
|
|
21723
|
+
case TOKEN.log:
|
|
21724
|
+
stack[this.#sp - 1] = Math.log10(stack[this.#sp - 1]);
|
|
21725
|
+
break;
|
|
21726
|
+
case TOKEN.cvi:
|
|
21727
|
+
stack[this.#sp - 1] = Math.trunc(stack[this.#sp - 1]) | 0;
|
|
21728
|
+
break;
|
|
21729
|
+
case TOKEN.cvr:
|
|
21730
|
+
break;
|
|
21731
|
+
// values are already f64
|
|
21732
|
+
case TOKEN.not: {
|
|
21733
|
+
const v = stack[this.#sp - 1];
|
|
21734
|
+
stack[this.#sp - 1] = v === 0 || v === 1 ? 1 - v : ~(v | 0);
|
|
21735
|
+
break;
|
|
21736
|
+
}
|
|
21737
|
+
case TOKEN.add: {
|
|
21738
|
+
const b = stack[--this.#sp];
|
|
21739
|
+
stack[this.#sp - 1] += b;
|
|
21740
|
+
break;
|
|
21741
|
+
}
|
|
21742
|
+
case TOKEN.sub: {
|
|
21743
|
+
const b = stack[--this.#sp];
|
|
21744
|
+
stack[this.#sp - 1] -= b;
|
|
21745
|
+
break;
|
|
21746
|
+
}
|
|
21747
|
+
case TOKEN.mul: {
|
|
21748
|
+
const b = stack[--this.#sp];
|
|
21749
|
+
stack[this.#sp - 1] *= b;
|
|
21750
|
+
break;
|
|
21751
|
+
}
|
|
21752
|
+
case TOKEN.div: {
|
|
21753
|
+
const b = stack[--this.#sp];
|
|
21754
|
+
stack[this.#sp - 1] = b !== 0 ? stack[this.#sp - 1] / b : 0;
|
|
21755
|
+
break;
|
|
21756
|
+
}
|
|
21757
|
+
case TOKEN.idiv: {
|
|
21758
|
+
const b = stack[--this.#sp];
|
|
21759
|
+
stack[this.#sp - 1] = b !== 0 ? Math.trunc(stack[this.#sp - 1] / b) : 0;
|
|
21760
|
+
break;
|
|
21761
|
+
}
|
|
21762
|
+
case TOKEN.mod: {
|
|
21763
|
+
const b = stack[--this.#sp];
|
|
21764
|
+
stack[this.#sp - 1] = b !== 0 ? stack[this.#sp - 1] % b : 0;
|
|
21765
|
+
break;
|
|
21766
|
+
}
|
|
21767
|
+
case TOKEN.exp: {
|
|
21768
|
+
const b = stack[--this.#sp];
|
|
21769
|
+
stack[this.#sp - 1] **= b;
|
|
21770
|
+
break;
|
|
21771
|
+
}
|
|
21772
|
+
case TOKEN.atan: {
|
|
21773
|
+
const dx = stack[--this.#sp];
|
|
21774
|
+
const deg = Math.atan2(stack[this.#sp - 1], dx) * _RAD_TO_DEG;
|
|
21775
|
+
stack[this.#sp - 1] = deg < 0 ? deg + 360 : deg;
|
|
21776
|
+
break;
|
|
21777
|
+
}
|
|
21778
|
+
case TOKEN.eq: {
|
|
21779
|
+
const b = stack[--this.#sp];
|
|
21780
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] === b ? 1 : 0;
|
|
21781
|
+
break;
|
|
21782
|
+
}
|
|
21783
|
+
case TOKEN.ne: {
|
|
21784
|
+
const b = stack[--this.#sp];
|
|
21785
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] !== b ? 1 : 0;
|
|
21786
|
+
break;
|
|
21787
|
+
}
|
|
21788
|
+
case TOKEN.gt: {
|
|
21789
|
+
const b = stack[--this.#sp];
|
|
21790
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] > b ? 1 : 0;
|
|
21791
|
+
break;
|
|
21792
|
+
}
|
|
21793
|
+
case TOKEN.ge: {
|
|
21794
|
+
const b = stack[--this.#sp];
|
|
21795
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] >= b ? 1 : 0;
|
|
21796
|
+
break;
|
|
21797
|
+
}
|
|
21798
|
+
case TOKEN.lt: {
|
|
21799
|
+
const b = stack[--this.#sp];
|
|
21800
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] < b ? 1 : 0;
|
|
21801
|
+
break;
|
|
21802
|
+
}
|
|
21803
|
+
case TOKEN.le: {
|
|
21804
|
+
const b = stack[--this.#sp];
|
|
21805
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] <= b ? 1 : 0;
|
|
21806
|
+
break;
|
|
21807
|
+
}
|
|
21808
|
+
case TOKEN.and: {
|
|
21809
|
+
const b = stack[--this.#sp] | 0;
|
|
21810
|
+
stack[this.#sp - 1] = (stack[this.#sp - 1] | 0) & b;
|
|
21811
|
+
break;
|
|
21812
|
+
}
|
|
21813
|
+
case TOKEN.or: {
|
|
21814
|
+
const b = stack[--this.#sp] | 0;
|
|
21815
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] | 0 | b;
|
|
21816
|
+
break;
|
|
21817
|
+
}
|
|
21818
|
+
case TOKEN.xor: {
|
|
21819
|
+
const b = stack[--this.#sp] | 0;
|
|
21820
|
+
stack[this.#sp - 1] = (stack[this.#sp - 1] | 0) ^ b;
|
|
21821
|
+
break;
|
|
21822
|
+
}
|
|
21823
|
+
case TOKEN.bitshift: {
|
|
21824
|
+
const amt = stack[--this.#sp] | 0;
|
|
21825
|
+
const v = stack[this.#sp - 1] | 0;
|
|
21826
|
+
stack[this.#sp - 1] = amt > 0 ? v << amt : v >> -amt;
|
|
21827
|
+
break;
|
|
21828
|
+
}
|
|
21829
|
+
case TOKEN.min: {
|
|
21830
|
+
const b = stack[--this.#sp];
|
|
21831
|
+
stack[this.#sp - 1] = Math.min(stack[this.#sp - 1], b);
|
|
21832
|
+
break;
|
|
21833
|
+
}
|
|
21834
|
+
case TOKEN.max: {
|
|
21835
|
+
const b = stack[--this.#sp];
|
|
21836
|
+
stack[this.#sp - 1] = Math.max(stack[this.#sp - 1], b);
|
|
21837
|
+
break;
|
|
21838
|
+
}
|
|
21839
|
+
case TOKEN.dup:
|
|
21840
|
+
this.#push(stack[this.#sp - 1]);
|
|
21841
|
+
break;
|
|
21842
|
+
case TOKEN.exch: {
|
|
21843
|
+
const a = stack[--this.#sp];
|
|
21844
|
+
const b = stack[--this.#sp];
|
|
21845
|
+
this.#push(a);
|
|
21846
|
+
this.#push(b);
|
|
21847
|
+
break;
|
|
21848
|
+
}
|
|
21849
|
+
case TOKEN.pop:
|
|
21850
|
+
this.#sp--;
|
|
21851
|
+
break;
|
|
21852
|
+
case TOKEN.copy: {
|
|
21853
|
+
const n = Math.trunc(stack[--this.#sp]);
|
|
21854
|
+
const base = this.#sp - n;
|
|
21855
|
+
for (let k = 0; k < n; k++) {
|
|
21856
|
+
this.#push(stack[base + k]);
|
|
21857
|
+
}
|
|
21858
|
+
break;
|
|
21859
|
+
}
|
|
21860
|
+
case TOKEN.index: {
|
|
21861
|
+
const i = Math.trunc(stack[--this.#sp]);
|
|
21862
|
+
this.#push(stack[this.#sp - 1 - i]);
|
|
21863
|
+
break;
|
|
21864
|
+
}
|
|
21865
|
+
case TOKEN.roll: {
|
|
21866
|
+
const j = Math.trunc(stack[--this.#sp]);
|
|
21867
|
+
const n = Math.trunc(stack[--this.#sp]);
|
|
21868
|
+
if (n > 1 && j !== 0) {
|
|
21869
|
+
const mod = (j % n + n) % n;
|
|
21870
|
+
if (mod !== 0) {
|
|
21871
|
+
const base = this.#sp - n;
|
|
21872
|
+
const sub = stack.slice(base, this.#sp);
|
|
21873
|
+
for (let k = 0; k < n; k++) {
|
|
21874
|
+
stack[base + k] = sub[(k - mod + n) % n];
|
|
21875
|
+
}
|
|
21876
|
+
}
|
|
21877
|
+
}
|
|
21878
|
+
break;
|
|
21879
|
+
}
|
|
21880
|
+
}
|
|
21881
|
+
}
|
|
21882
|
+
static #execBlock(instructions) {
|
|
21883
|
+
for (const instr of instructions) {
|
|
21884
|
+
switch (instr.type) {
|
|
21885
|
+
case PS_NODE.number:
|
|
21886
|
+
this.#push(instr.value);
|
|
21887
|
+
break;
|
|
21888
|
+
case PS_NODE.operator:
|
|
21889
|
+
this.#execOp(instr.op);
|
|
21890
|
+
break;
|
|
21891
|
+
case PS_NODE.if:
|
|
21892
|
+
if (this.#stack[--this.#sp] !== 0) {
|
|
21893
|
+
this.#execBlock(instr.then.instructions);
|
|
21894
|
+
}
|
|
21895
|
+
break;
|
|
21896
|
+
case PS_NODE.ifelse:
|
|
21897
|
+
if (this.#stack[--this.#sp] !== 0) {
|
|
21898
|
+
this.#execBlock(instr.then.instructions);
|
|
21899
|
+
} else {
|
|
21900
|
+
this.#execBlock(instr.otherwise.instructions);
|
|
21901
|
+
}
|
|
21902
|
+
break;
|
|
21903
|
+
}
|
|
21904
|
+
}
|
|
21905
|
+
}
|
|
21906
|
+
/**
|
|
21907
|
+
* @param {import("./ast.js").PsProgram} program
|
|
21908
|
+
* @param {number[]} domain – flat [min0,max0, …]
|
|
21909
|
+
* @param {number[]} range – flat [min0,max0, …]
|
|
21910
|
+
* @returns {Function} – `(src, srcOffset, dest, destOffset) => void`
|
|
21911
|
+
*/
|
|
21912
|
+
static build(program, domain, range) {
|
|
21913
|
+
const nIn = domain.length >> 1;
|
|
21914
|
+
const nOut = range.length >> 1;
|
|
21915
|
+
const { instructions } = program.body;
|
|
21916
|
+
return (src, srcOffset, dest, destOffset) => {
|
|
21917
|
+
this.#sp = 0;
|
|
21918
|
+
for (let i = 0; i < nIn; i++) {
|
|
21919
|
+
this.#push(src[srcOffset + i]);
|
|
21920
|
+
}
|
|
21921
|
+
this.#execBlock(instructions);
|
|
21922
|
+
const base = this.#sp - nOut;
|
|
21923
|
+
for (let i = 0; i < nOut; i++) {
|
|
21924
|
+
const v = base + i >= 0 ? this.#stack[base + i] : 0;
|
|
21925
|
+
dest[destOffset + i] = MathClamp(v, range[i * 2], range[i * 2 + 1]);
|
|
21926
|
+
}
|
|
21927
|
+
};
|
|
21928
|
+
}
|
|
21929
|
+
};
|
|
21930
|
+
function buildPostScriptJsFunction(source, domain, range, forceInterpreter = false) {
|
|
21931
|
+
const program = parsePostScriptFunction(source);
|
|
21932
|
+
const ir = !forceInterpreter && new PsJsCompiler(domain, range).compile(program);
|
|
21933
|
+
if (ir) {
|
|
21934
|
+
return (src, srcOffset, dest, destOffset) => {
|
|
21935
|
+
PsJsCompiler.execute(ir, src, srcOffset, dest, destOffset);
|
|
21936
|
+
};
|
|
21937
|
+
}
|
|
21938
|
+
return PSStackBasedInterpreter.build(program, domain, range);
|
|
21939
|
+
}
|
|
19324
21940
|
export {
|
|
19325
21941
|
CFFCompiler,
|
|
19326
21942
|
CFFParser,
|
|
19327
21943
|
CFFStrings,
|
|
19328
21944
|
CMap,
|
|
21945
|
+
CalGrayCS,
|
|
21946
|
+
CalRGBCS,
|
|
19329
21947
|
CipherTransformFactory,
|
|
21948
|
+
DeviceCmykCS,
|
|
19330
21949
|
Dict,
|
|
19331
21950
|
DrawOPS,
|
|
19332
21951
|
FlateStream,
|
|
21952
|
+
LabCS,
|
|
19333
21953
|
MacStandardGlyphOrdering,
|
|
19334
21954
|
Name,
|
|
19335
21955
|
PDF17,
|
|
@@ -19343,6 +21963,7 @@ export {
|
|
|
19343
21963
|
Type2Compiled,
|
|
19344
21964
|
WinAnsiEncoding,
|
|
19345
21965
|
ZapfDingbatsEncoding,
|
|
21966
|
+
buildPostScriptJsFunction,
|
|
19346
21967
|
encodeToXmlString,
|
|
19347
21968
|
getDingbatsGlyphsUnicode,
|
|
19348
21969
|
getEncoding,
|