@poe-platform/safe-bash 0.1.771 → 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/chunks/{chunk-FSH7QRIC.js → chunk-Z3HX3ZBA.js} +10 -4
- 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/commands/python/host-capabilities.js +15 -6
- package/dist/safe-bash/commands/python/index.browser.js +1 -1
- package/dist/safe-bash/core.browser.js +39 -39
- 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
|
@@ -22512,6 +22512,2614 @@ var getFontBasicMetrics = getLookupTableFactory(function(t) {
|
|
|
22512
22512
|
xHeight: Math.NaN
|
|
22513
22513
|
};
|
|
22514
22514
|
});
|
|
22515
|
+
function resizeRgbImage(src, dest, w1, h1, w2, h2, alpha01) {
|
|
22516
|
+
const COMPONENTS = 3;
|
|
22517
|
+
alpha01 = alpha01 !== 1 ? 0 : alpha01;
|
|
22518
|
+
const xRatio = w1 / w2;
|
|
22519
|
+
const yRatio = h1 / h2;
|
|
22520
|
+
let newIndex = 0, oldIndex;
|
|
22521
|
+
const xScaled = new Uint16Array(w2);
|
|
22522
|
+
const w1Scanline = w1 * COMPONENTS;
|
|
22523
|
+
for (let i = 0; i < w2; i++) {
|
|
22524
|
+
xScaled[i] = Math.floor(i * xRatio) * COMPONENTS;
|
|
22525
|
+
}
|
|
22526
|
+
for (let i = 0; i < h2; i++) {
|
|
22527
|
+
const py = Math.floor(i * yRatio) * w1Scanline;
|
|
22528
|
+
for (let j = 0; j < w2; j++) {
|
|
22529
|
+
oldIndex = py + xScaled[j];
|
|
22530
|
+
dest[newIndex++] = src[oldIndex++];
|
|
22531
|
+
dest[newIndex++] = src[oldIndex++];
|
|
22532
|
+
dest[newIndex++] = src[oldIndex++];
|
|
22533
|
+
newIndex += alpha01;
|
|
22534
|
+
}
|
|
22535
|
+
}
|
|
22536
|
+
}
|
|
22537
|
+
function isDefaultDecodeHelper(decode, expectedLen) {
|
|
22538
|
+
if (!Array.isArray(decode)) {
|
|
22539
|
+
return true;
|
|
22540
|
+
}
|
|
22541
|
+
const decodeLen = decode.length;
|
|
22542
|
+
if (decodeLen < expectedLen) {
|
|
22543
|
+
warn2("Decode map length is too short.");
|
|
22544
|
+
return true;
|
|
22545
|
+
}
|
|
22546
|
+
if (decodeLen > expectedLen) {
|
|
22547
|
+
info("Truncating too long decode map.");
|
|
22548
|
+
decode.length = expectedLen;
|
|
22549
|
+
}
|
|
22550
|
+
return false;
|
|
22551
|
+
}
|
|
22552
|
+
var ColorSpace = class _ColorSpace {
|
|
22553
|
+
static #rgbBuf = new Uint8ClampedArray(3);
|
|
22554
|
+
constructor(name, numComps) {
|
|
22555
|
+
if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && this.constructor === _ColorSpace) {
|
|
22556
|
+
unreachable2("Cannot initialize ColorSpace.");
|
|
22557
|
+
}
|
|
22558
|
+
this.name = name;
|
|
22559
|
+
this.numComps = numComps;
|
|
22560
|
+
}
|
|
22561
|
+
/**
|
|
22562
|
+
* Converts the color value to the RGB color. The color components are
|
|
22563
|
+
* located in the src array starting from the srcOffset. Returns the array
|
|
22564
|
+
* of the rgb components, each value ranging from [0,255].
|
|
22565
|
+
*/
|
|
22566
|
+
getRgb(src, srcOffset, output = new Uint8ClampedArray(3)) {
|
|
22567
|
+
this.getRgbItem(src, srcOffset, output, 0);
|
|
22568
|
+
return output;
|
|
22569
|
+
}
|
|
22570
|
+
getRgbHex(src, srcOffset) {
|
|
22571
|
+
const buffer = this.getRgb(src, srcOffset, _ColorSpace.#rgbBuf);
|
|
22572
|
+
return Util.makeHexColor(buffer[0], buffer[1], buffer[2]);
|
|
22573
|
+
}
|
|
22574
|
+
/**
|
|
22575
|
+
* Converts the color value to the RGB color, similar to the getRgb method.
|
|
22576
|
+
* The result placed into the dest array starting from the destOffset.
|
|
22577
|
+
*/
|
|
22578
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
22579
|
+
unreachable2("Should not call ColorSpace.getRgbItem");
|
|
22580
|
+
}
|
|
22581
|
+
/**
|
|
22582
|
+
* Converts the specified number of the color values to the RGB colors.
|
|
22583
|
+
* The colors are located in the src array starting from the srcOffset.
|
|
22584
|
+
* The result is placed into the dest array starting from the destOffset.
|
|
22585
|
+
* The src array items shall be in [0,2^bits) range, the dest array items
|
|
22586
|
+
* will be in [0,255] range. alpha01 indicates how many alpha components
|
|
22587
|
+
* there are in the dest array; it will be either 0 (RGB array) or 1 (RGBA
|
|
22588
|
+
* array).
|
|
22589
|
+
*/
|
|
22590
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
22591
|
+
unreachable2("Should not call ColorSpace.getRgbBuffer");
|
|
22592
|
+
}
|
|
22593
|
+
/**
|
|
22594
|
+
* Converts `count` unscaled colors to RGB, starting at `destOffset`.
|
|
22595
|
+
* Components use the native color-space ranges expected by `getRgbItem`,
|
|
22596
|
+
* and each output has a `3 + alpha01` byte stride.
|
|
22597
|
+
* Subclasses may override this to batch expensive conversions.
|
|
22598
|
+
*/
|
|
22599
|
+
getRgbItems(src, count, dest, destOffset, alpha01) {
|
|
22600
|
+
const { numComps } = this;
|
|
22601
|
+
for (let i = 0, srcOffset = 0; i < count; i++, srcOffset += numComps) {
|
|
22602
|
+
this.getRgbItem(src, srcOffset, dest, destOffset);
|
|
22603
|
+
destOffset += 3 + alpha01;
|
|
22604
|
+
}
|
|
22605
|
+
}
|
|
22606
|
+
/**
|
|
22607
|
+
* Returns true if source data will be equal the result/output data.
|
|
22608
|
+
*/
|
|
22609
|
+
isPassthrough(bits) {
|
|
22610
|
+
return false;
|
|
22611
|
+
}
|
|
22612
|
+
/**
|
|
22613
|
+
* Refer to the static `ColorSpace.isDefaultDecode` method below.
|
|
22614
|
+
*/
|
|
22615
|
+
isDefaultDecode(decode, bpc) {
|
|
22616
|
+
return _ColorSpace.isDefaultDecode(decode, this.numComps);
|
|
22617
|
+
}
|
|
22618
|
+
/**
|
|
22619
|
+
* Fills in the RGB colors in the destination buffer. alpha01 indicates
|
|
22620
|
+
* how many alpha components there are in the dest array; it will be either
|
|
22621
|
+
* 0 (RGB array) or 1 (RGBA array).
|
|
22622
|
+
*/
|
|
22623
|
+
fillRgb(dest, originalWidth, originalHeight, width, height, actualHeight, bpc, comps, alpha01) {
|
|
22624
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
22625
|
+
assert2(
|
|
22626
|
+
dest instanceof Uint8ClampedArray,
|
|
22627
|
+
'ColorSpace.fillRgb: Unsupported "dest" type.'
|
|
22628
|
+
);
|
|
22629
|
+
}
|
|
22630
|
+
const count = originalWidth * originalHeight;
|
|
22631
|
+
let rgbBuf = null;
|
|
22632
|
+
const numComponentColors = 1 << bpc;
|
|
22633
|
+
const needsResizing = originalHeight !== height || originalWidth !== width;
|
|
22634
|
+
if (this.isPassthrough(bpc)) {
|
|
22635
|
+
rgbBuf = comps;
|
|
22636
|
+
} else if (this.numComps === 1 && count > numComponentColors && this.name !== "DeviceGray" && this.name !== "DeviceRGB") {
|
|
22637
|
+
const allColors = bpc <= 8 ? new Uint8Array(numComponentColors) : new Uint16Array(numComponentColors);
|
|
22638
|
+
for (let i = 0; i < numComponentColors; i++) {
|
|
22639
|
+
allColors[i] = i;
|
|
22640
|
+
}
|
|
22641
|
+
const colorMap = new Uint8ClampedArray(numComponentColors * 3);
|
|
22642
|
+
this.getRgbBuffer(
|
|
22643
|
+
allColors,
|
|
22644
|
+
0,
|
|
22645
|
+
numComponentColors,
|
|
22646
|
+
colorMap,
|
|
22647
|
+
0,
|
|
22648
|
+
bpc,
|
|
22649
|
+
/* alpha01 = */
|
|
22650
|
+
0
|
|
22651
|
+
);
|
|
22652
|
+
if (!needsResizing) {
|
|
22653
|
+
let destPos = 0;
|
|
22654
|
+
for (let i = 0; i < count; ++i) {
|
|
22655
|
+
const key = comps[i] * 3;
|
|
22656
|
+
dest[destPos++] = colorMap[key];
|
|
22657
|
+
dest[destPos++] = colorMap[key + 1];
|
|
22658
|
+
dest[destPos++] = colorMap[key + 2];
|
|
22659
|
+
destPos += alpha01;
|
|
22660
|
+
}
|
|
22661
|
+
} else {
|
|
22662
|
+
rgbBuf = new Uint8Array(count * 3);
|
|
22663
|
+
let rgbPos = 0;
|
|
22664
|
+
for (let i = 0; i < count; ++i) {
|
|
22665
|
+
const key = comps[i] * 3;
|
|
22666
|
+
rgbBuf[rgbPos++] = colorMap[key];
|
|
22667
|
+
rgbBuf[rgbPos++] = colorMap[key + 1];
|
|
22668
|
+
rgbBuf[rgbPos++] = colorMap[key + 2];
|
|
22669
|
+
}
|
|
22670
|
+
}
|
|
22671
|
+
} else if (!needsResizing) {
|
|
22672
|
+
this.getRgbBuffer(comps, 0, width * actualHeight, dest, 0, bpc, alpha01);
|
|
22673
|
+
} else {
|
|
22674
|
+
rgbBuf = new Uint8ClampedArray(count * 3);
|
|
22675
|
+
this.getRgbBuffer(
|
|
22676
|
+
comps,
|
|
22677
|
+
0,
|
|
22678
|
+
count,
|
|
22679
|
+
rgbBuf,
|
|
22680
|
+
0,
|
|
22681
|
+
bpc,
|
|
22682
|
+
/* alpha01 = */
|
|
22683
|
+
0
|
|
22684
|
+
);
|
|
22685
|
+
}
|
|
22686
|
+
if (rgbBuf) {
|
|
22687
|
+
if (needsResizing) {
|
|
22688
|
+
resizeRgbImage(
|
|
22689
|
+
rgbBuf,
|
|
22690
|
+
dest,
|
|
22691
|
+
originalWidth,
|
|
22692
|
+
originalHeight,
|
|
22693
|
+
width,
|
|
22694
|
+
height,
|
|
22695
|
+
alpha01
|
|
22696
|
+
);
|
|
22697
|
+
} else {
|
|
22698
|
+
let destPos = 0, rgbPos = 0;
|
|
22699
|
+
for (let i = 0, ii = width * actualHeight; i < ii; i++) {
|
|
22700
|
+
dest[destPos++] = rgbBuf[rgbPos++];
|
|
22701
|
+
dest[destPos++] = rgbBuf[rgbPos++];
|
|
22702
|
+
dest[destPos++] = rgbBuf[rgbPos++];
|
|
22703
|
+
destPos += alpha01;
|
|
22704
|
+
}
|
|
22705
|
+
}
|
|
22706
|
+
}
|
|
22707
|
+
}
|
|
22708
|
+
/**
|
|
22709
|
+
* True if the colorspace has components in the default range of [0, 1].
|
|
22710
|
+
* This should be true for all colorspaces except for lab color spaces
|
|
22711
|
+
* which are [0,100], [-128, 127], [-128, 127].
|
|
22712
|
+
*/
|
|
22713
|
+
get usesZeroToOneRange() {
|
|
22714
|
+
return shadow2(this, "usesZeroToOneRange", true);
|
|
22715
|
+
}
|
|
22716
|
+
/**
|
|
22717
|
+
* Checks if a decode map matches the default decode map for a color space.
|
|
22718
|
+
* This handles the general decode maps where there are two values per
|
|
22719
|
+
* component, e.g. [0, 1, 0, 1, 0, 1] for a RGB color.
|
|
22720
|
+
* This does not handle Lab, Indexed, or Pattern decode maps since they are
|
|
22721
|
+
* slightly different.
|
|
22722
|
+
* @param {Array} decode - Decode map (usually from an image).
|
|
22723
|
+
* @param {number} numComps - Number of components the color space has.
|
|
22724
|
+
*/
|
|
22725
|
+
static isDefaultDecode(decode, numComps) {
|
|
22726
|
+
if (isDefaultDecodeHelper(decode, numComps * 2)) {
|
|
22727
|
+
return true;
|
|
22728
|
+
}
|
|
22729
|
+
for (let i = 0, ii = decode.length; i < ii; i += 2) {
|
|
22730
|
+
if (decode[i] !== 0 || decode[i + 1] !== 1) {
|
|
22731
|
+
return false;
|
|
22732
|
+
}
|
|
22733
|
+
}
|
|
22734
|
+
return true;
|
|
22735
|
+
}
|
|
22736
|
+
};
|
|
22737
|
+
var DeviceCmykCS = class extends ColorSpace {
|
|
22738
|
+
constructor() {
|
|
22739
|
+
super("DeviceCMYK", 4);
|
|
22740
|
+
}
|
|
22741
|
+
// The coefficients below was found using numerical analysis: the method of
|
|
22742
|
+
// steepest descent for the sum((f_i - color_value_i)^2) for r/g/b colors,
|
|
22743
|
+
// where color_value is the tabular value from the table of sampled RGB colors
|
|
22744
|
+
// from CMYK US Web Coated (SWOP) colorspace, and f_i is the corresponding
|
|
22745
|
+
// CMYK color conversion using the estimation below:
|
|
22746
|
+
// f(A, B,.. N) = Acc+Bcm+Ccy+Dck+c+Fmm+Gmy+Hmk+Im+Jyy+Kyk+Ly+Mkk+Nk+255
|
|
22747
|
+
#toRgb(src, srcOffset, srcScale, dest, destOffset) {
|
|
22748
|
+
const c = src[srcOffset] * srcScale;
|
|
22749
|
+
const m = src[srcOffset + 1] * srcScale;
|
|
22750
|
+
const y = src[srcOffset + 2] * srcScale;
|
|
22751
|
+
const k = src[srcOffset + 3] * srcScale;
|
|
22752
|
+
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);
|
|
22753
|
+
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);
|
|
22754
|
+
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);
|
|
22755
|
+
}
|
|
22756
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
22757
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
22758
|
+
assert2(
|
|
22759
|
+
dest instanceof Uint8ClampedArray,
|
|
22760
|
+
'DeviceCmykCS.getRgbItem: Unsupported "dest" type.'
|
|
22761
|
+
);
|
|
22762
|
+
}
|
|
22763
|
+
this.#toRgb(src, srcOffset, 1, dest, destOffset);
|
|
22764
|
+
}
|
|
22765
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
22766
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
22767
|
+
assert2(
|
|
22768
|
+
dest instanceof Uint8ClampedArray,
|
|
22769
|
+
'DeviceCmykCS.getRgbBuffer: Unsupported "dest" type.'
|
|
22770
|
+
);
|
|
22771
|
+
}
|
|
22772
|
+
const scale = 1 / ((1 << bits) - 1);
|
|
22773
|
+
for (let i = 0; i < count; i++) {
|
|
22774
|
+
this.#toRgb(src, srcOffset, scale, dest, destOffset);
|
|
22775
|
+
srcOffset += 4;
|
|
22776
|
+
destOffset += 3 + alpha01;
|
|
22777
|
+
}
|
|
22778
|
+
}
|
|
22779
|
+
};
|
|
22780
|
+
var CalGrayCS = class extends ColorSpace {
|
|
22781
|
+
constructor(whitePoint, blackPoint, gamma) {
|
|
22782
|
+
super("CalGray", 1);
|
|
22783
|
+
if (!whitePoint) {
|
|
22784
|
+
throw new FormatError(
|
|
22785
|
+
"WhitePoint missing - required for color space CalGray"
|
|
22786
|
+
);
|
|
22787
|
+
}
|
|
22788
|
+
[this.XW, this.YW, this.ZW] = whitePoint;
|
|
22789
|
+
[this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
|
|
22790
|
+
this.G = gamma || 1;
|
|
22791
|
+
if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
|
|
22792
|
+
throw new FormatError(
|
|
22793
|
+
`Invalid WhitePoint components for ${this.name}, no fallback available`
|
|
22794
|
+
);
|
|
22795
|
+
}
|
|
22796
|
+
if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
|
|
22797
|
+
info(`Invalid BlackPoint for ${this.name}, falling back to default.`);
|
|
22798
|
+
this.XB = this.YB = this.ZB = 0;
|
|
22799
|
+
}
|
|
22800
|
+
if (this.XB !== 0 || this.YB !== 0 || this.ZB !== 0) {
|
|
22801
|
+
warn2(
|
|
22802
|
+
`${this.name}, BlackPoint: XB: ${this.XB}, YB: ${this.YB}, ZB: ${this.ZB}, only default values are supported.`
|
|
22803
|
+
);
|
|
22804
|
+
}
|
|
22805
|
+
if (this.G < 1) {
|
|
22806
|
+
info(
|
|
22807
|
+
`Invalid Gamma: ${this.G} for ${this.name}, falling back to default.`
|
|
22808
|
+
);
|
|
22809
|
+
this.G = 1;
|
|
22810
|
+
}
|
|
22811
|
+
}
|
|
22812
|
+
#toRgb(src, srcOffset, dest, destOffset, scale) {
|
|
22813
|
+
const A = src[srcOffset] * scale;
|
|
22814
|
+
const AG = A ** this.G;
|
|
22815
|
+
const L = this.YW * AG;
|
|
22816
|
+
const val = Math.max(295.8 * L ** 0.3333333333333333 - 40.8, 0);
|
|
22817
|
+
dest[destOffset] = val;
|
|
22818
|
+
dest[destOffset + 1] = val;
|
|
22819
|
+
dest[destOffset + 2] = val;
|
|
22820
|
+
}
|
|
22821
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
22822
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
22823
|
+
assert2(
|
|
22824
|
+
dest instanceof Uint8ClampedArray,
|
|
22825
|
+
'CalGrayCS.getRgbItem: Unsupported "dest" type.'
|
|
22826
|
+
);
|
|
22827
|
+
}
|
|
22828
|
+
this.#toRgb(src, srcOffset, dest, destOffset, 1);
|
|
22829
|
+
}
|
|
22830
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
22831
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
22832
|
+
assert2(
|
|
22833
|
+
dest instanceof Uint8ClampedArray,
|
|
22834
|
+
'CalGrayCS.getRgbBuffer: Unsupported "dest" type.'
|
|
22835
|
+
);
|
|
22836
|
+
}
|
|
22837
|
+
const scale = 1 / ((1 << bits) - 1);
|
|
22838
|
+
for (let i = 0; i < count; ++i) {
|
|
22839
|
+
this.#toRgb(src, srcOffset, dest, destOffset, scale);
|
|
22840
|
+
srcOffset += 1;
|
|
22841
|
+
destOffset += 3 + alpha01;
|
|
22842
|
+
}
|
|
22843
|
+
}
|
|
22844
|
+
};
|
|
22845
|
+
var CalRGBCS = class _CalRGBCS extends ColorSpace {
|
|
22846
|
+
// See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html for these
|
|
22847
|
+
// matrices.
|
|
22848
|
+
// prettier-ignore
|
|
22849
|
+
static #BRADFORD_SCALE_MATRIX = new Float32Array([
|
|
22850
|
+
0.8951,
|
|
22851
|
+
0.2664,
|
|
22852
|
+
-0.1614,
|
|
22853
|
+
-0.7502,
|
|
22854
|
+
1.7135,
|
|
22855
|
+
0.0367,
|
|
22856
|
+
0.0389,
|
|
22857
|
+
-0.0685,
|
|
22858
|
+
1.0296
|
|
22859
|
+
]);
|
|
22860
|
+
// prettier-ignore
|
|
22861
|
+
static #BRADFORD_SCALE_INVERSE_MATRIX = new Float32Array([
|
|
22862
|
+
0.9869929,
|
|
22863
|
+
-0.1470543,
|
|
22864
|
+
0.1599627,
|
|
22865
|
+
0.4323053,
|
|
22866
|
+
0.5183603,
|
|
22867
|
+
0.0492912,
|
|
22868
|
+
-85287e-7,
|
|
22869
|
+
0.0400428,
|
|
22870
|
+
0.9684867
|
|
22871
|
+
]);
|
|
22872
|
+
// See http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html.
|
|
22873
|
+
// prettier-ignore
|
|
22874
|
+
static #SRGB_D65_XYZ_TO_RGB_MATRIX = new Float32Array([
|
|
22875
|
+
3.2404542,
|
|
22876
|
+
-1.5371385,
|
|
22877
|
+
-0.4985314,
|
|
22878
|
+
-0.969266,
|
|
22879
|
+
1.8760108,
|
|
22880
|
+
0.041556,
|
|
22881
|
+
0.0556434,
|
|
22882
|
+
-0.2040259,
|
|
22883
|
+
1.0572252
|
|
22884
|
+
]);
|
|
22885
|
+
static #FLAT_WHITEPOINT_MATRIX = new Float32Array([1, 1, 1]);
|
|
22886
|
+
static #tempNormalizeMatrix = new Float32Array(3);
|
|
22887
|
+
static #tempConvertMatrix1 = new Float32Array(3);
|
|
22888
|
+
static #tempConvertMatrix2 = new Float32Array(3);
|
|
22889
|
+
static #DECODE_L_CONSTANT = ((8 + 16) / 116) ** 3 / 8;
|
|
22890
|
+
constructor(whitePoint, blackPoint, gamma, matrix) {
|
|
22891
|
+
super("CalRGB", 3);
|
|
22892
|
+
if (!whitePoint) {
|
|
22893
|
+
throw new FormatError(
|
|
22894
|
+
"WhitePoint missing - required for color space CalRGB"
|
|
22895
|
+
);
|
|
22896
|
+
}
|
|
22897
|
+
const [XW, YW, ZW] = this.whitePoint = whitePoint;
|
|
22898
|
+
const [XB, YB, ZB] = this.blackPoint = blackPoint || new Float32Array(3);
|
|
22899
|
+
[this.GR, this.GG, this.GB] = gamma || new Float32Array([1, 1, 1]);
|
|
22900
|
+
[
|
|
22901
|
+
this.MXA,
|
|
22902
|
+
this.MYA,
|
|
22903
|
+
this.MZA,
|
|
22904
|
+
this.MXB,
|
|
22905
|
+
this.MYB,
|
|
22906
|
+
this.MZB,
|
|
22907
|
+
this.MXC,
|
|
22908
|
+
this.MYC,
|
|
22909
|
+
this.MZC
|
|
22910
|
+
] = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
|
|
22911
|
+
if (XW < 0 || ZW < 0 || YW !== 1) {
|
|
22912
|
+
throw new FormatError(
|
|
22913
|
+
`Invalid WhitePoint components for ${this.name}, no fallback available`
|
|
22914
|
+
);
|
|
22915
|
+
}
|
|
22916
|
+
if (XB < 0 || YB < 0 || ZB < 0) {
|
|
22917
|
+
info(
|
|
22918
|
+
`Invalid BlackPoint for ${this.name} [${XB}, ${YB}, ${ZB}], falling back to default.`
|
|
22919
|
+
);
|
|
22920
|
+
this.blackPoint = new Float32Array(3);
|
|
22921
|
+
}
|
|
22922
|
+
if (this.GR < 0 || this.GG < 0 || this.GB < 0) {
|
|
22923
|
+
info(
|
|
22924
|
+
`Invalid Gamma [${this.GR}, ${this.GG}, ${this.GB}] for ${this.name}, falling back to default.`
|
|
22925
|
+
);
|
|
22926
|
+
this.GR = this.GG = this.GB = 1;
|
|
22927
|
+
}
|
|
22928
|
+
}
|
|
22929
|
+
#matrixProduct(a, b, result) {
|
|
22930
|
+
result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
22931
|
+
result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
|
|
22932
|
+
result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
|
|
22933
|
+
}
|
|
22934
|
+
#toFlat(sourceWhitePoint, LMS, result) {
|
|
22935
|
+
result[0] = LMS[0] * 1 / sourceWhitePoint[0];
|
|
22936
|
+
result[1] = LMS[1] * 1 / sourceWhitePoint[1];
|
|
22937
|
+
result[2] = LMS[2] * 1 / sourceWhitePoint[2];
|
|
22938
|
+
}
|
|
22939
|
+
#toD65(sourceWhitePoint, LMS, result) {
|
|
22940
|
+
const D65X = 0.95047;
|
|
22941
|
+
const D65Y = 1;
|
|
22942
|
+
const D65Z = 1.08883;
|
|
22943
|
+
result[0] = LMS[0] * D65X / sourceWhitePoint[0];
|
|
22944
|
+
result[1] = LMS[1] * D65Y / sourceWhitePoint[1];
|
|
22945
|
+
result[2] = LMS[2] * D65Z / sourceWhitePoint[2];
|
|
22946
|
+
}
|
|
22947
|
+
#sRGBTransferFunction(color) {
|
|
22948
|
+
if (color <= 31308e-7) {
|
|
22949
|
+
return MathClamp(12.92 * color, 0, 1);
|
|
22950
|
+
}
|
|
22951
|
+
return color >= 0.99554525 ? 1 : MathClamp((1 + 0.055) * color ** (1 / 2.4) - 0.055, 0, 1);
|
|
22952
|
+
}
|
|
22953
|
+
#decodeL(L) {
|
|
22954
|
+
if (L < 0) {
|
|
22955
|
+
return -this.#decodeL(-L);
|
|
22956
|
+
}
|
|
22957
|
+
return L > 8 ? ((L + 16) / 116) ** 3 : L * _CalRGBCS.#DECODE_L_CONSTANT;
|
|
22958
|
+
}
|
|
22959
|
+
#compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {
|
|
22960
|
+
if (sourceBlackPoint[0] === 0 && sourceBlackPoint[1] === 0 && sourceBlackPoint[2] === 0) {
|
|
22961
|
+
result[0] = XYZ_Flat[0];
|
|
22962
|
+
result[1] = XYZ_Flat[1];
|
|
22963
|
+
result[2] = XYZ_Flat[2];
|
|
22964
|
+
return;
|
|
22965
|
+
}
|
|
22966
|
+
const zeroDecodeL = this.#decodeL(0);
|
|
22967
|
+
const X_DST = zeroDecodeL;
|
|
22968
|
+
const X_SRC = this.#decodeL(sourceBlackPoint[0]);
|
|
22969
|
+
const Y_DST = zeroDecodeL;
|
|
22970
|
+
const Y_SRC = this.#decodeL(sourceBlackPoint[1]);
|
|
22971
|
+
const Z_DST = zeroDecodeL;
|
|
22972
|
+
const Z_SRC = this.#decodeL(sourceBlackPoint[2]);
|
|
22973
|
+
const X_Scale = (1 - X_DST) / (1 - X_SRC);
|
|
22974
|
+
const X_Offset = 1 - X_Scale;
|
|
22975
|
+
const Y_Scale = (1 - Y_DST) / (1 - Y_SRC);
|
|
22976
|
+
const Y_Offset = 1 - Y_Scale;
|
|
22977
|
+
const Z_Scale = (1 - Z_DST) / (1 - Z_SRC);
|
|
22978
|
+
const Z_Offset = 1 - Z_Scale;
|
|
22979
|
+
result[0] = XYZ_Flat[0] * X_Scale + X_Offset;
|
|
22980
|
+
result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;
|
|
22981
|
+
result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;
|
|
22982
|
+
}
|
|
22983
|
+
#normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {
|
|
22984
|
+
if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {
|
|
22985
|
+
result[0] = XYZ_In[0];
|
|
22986
|
+
result[1] = XYZ_In[1];
|
|
22987
|
+
result[2] = XYZ_In[2];
|
|
22988
|
+
return;
|
|
22989
|
+
}
|
|
22990
|
+
const LMS = result;
|
|
22991
|
+
this.#matrixProduct(_CalRGBCS.#BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
|
|
22992
|
+
const LMS_Flat = _CalRGBCS.#tempNormalizeMatrix;
|
|
22993
|
+
this.#toFlat(sourceWhitePoint, LMS, LMS_Flat);
|
|
22994
|
+
this.#matrixProduct(
|
|
22995
|
+
_CalRGBCS.#BRADFORD_SCALE_INVERSE_MATRIX,
|
|
22996
|
+
LMS_Flat,
|
|
22997
|
+
result
|
|
22998
|
+
);
|
|
22999
|
+
}
|
|
23000
|
+
#normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {
|
|
23001
|
+
const LMS = result;
|
|
23002
|
+
this.#matrixProduct(_CalRGBCS.#BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
|
|
23003
|
+
const LMS_D65 = _CalRGBCS.#tempNormalizeMatrix;
|
|
23004
|
+
this.#toD65(sourceWhitePoint, LMS, LMS_D65);
|
|
23005
|
+
this.#matrixProduct(
|
|
23006
|
+
_CalRGBCS.#BRADFORD_SCALE_INVERSE_MATRIX,
|
|
23007
|
+
LMS_D65,
|
|
23008
|
+
result
|
|
23009
|
+
);
|
|
23010
|
+
}
|
|
23011
|
+
#toRgb(src, srcOffset, dest, destOffset, scale) {
|
|
23012
|
+
const A = MathClamp(src[srcOffset] * scale, 0, 1);
|
|
23013
|
+
const B = MathClamp(src[srcOffset + 1] * scale, 0, 1);
|
|
23014
|
+
const C = MathClamp(src[srcOffset + 2] * scale, 0, 1);
|
|
23015
|
+
const AGR = A === 1 ? 1 : A ** this.GR;
|
|
23016
|
+
const BGG = B === 1 ? 1 : B ** this.GG;
|
|
23017
|
+
const CGB = C === 1 ? 1 : C ** this.GB;
|
|
23018
|
+
const X = this.MXA * AGR + this.MXB * BGG + this.MXC * CGB;
|
|
23019
|
+
const Y = this.MYA * AGR + this.MYB * BGG + this.MYC * CGB;
|
|
23020
|
+
const Z = this.MZA * AGR + this.MZB * BGG + this.MZC * CGB;
|
|
23021
|
+
const XYZ = _CalRGBCS.#tempConvertMatrix1;
|
|
23022
|
+
XYZ[0] = X;
|
|
23023
|
+
XYZ[1] = Y;
|
|
23024
|
+
XYZ[2] = Z;
|
|
23025
|
+
const XYZ_Flat = _CalRGBCS.#tempConvertMatrix2;
|
|
23026
|
+
this.#normalizeWhitePointToFlat(this.whitePoint, XYZ, XYZ_Flat);
|
|
23027
|
+
const XYZ_Black = _CalRGBCS.#tempConvertMatrix1;
|
|
23028
|
+
this.#compensateBlackPoint(this.blackPoint, XYZ_Flat, XYZ_Black);
|
|
23029
|
+
const XYZ_D65 = _CalRGBCS.#tempConvertMatrix2;
|
|
23030
|
+
this.#normalizeWhitePointToD65(
|
|
23031
|
+
_CalRGBCS.#FLAT_WHITEPOINT_MATRIX,
|
|
23032
|
+
XYZ_Black,
|
|
23033
|
+
XYZ_D65
|
|
23034
|
+
);
|
|
23035
|
+
const SRGB = _CalRGBCS.#tempConvertMatrix1;
|
|
23036
|
+
this.#matrixProduct(_CalRGBCS.#SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
|
|
23037
|
+
dest[destOffset] = this.#sRGBTransferFunction(SRGB[0]) * 255;
|
|
23038
|
+
dest[destOffset + 1] = this.#sRGBTransferFunction(SRGB[1]) * 255;
|
|
23039
|
+
dest[destOffset + 2] = this.#sRGBTransferFunction(SRGB[2]) * 255;
|
|
23040
|
+
}
|
|
23041
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
23042
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
23043
|
+
assert2(
|
|
23044
|
+
dest instanceof Uint8ClampedArray,
|
|
23045
|
+
'CalRGBCS.getRgbItem: Unsupported "dest" type.'
|
|
23046
|
+
);
|
|
23047
|
+
}
|
|
23048
|
+
this.#toRgb(src, srcOffset, dest, destOffset, 1);
|
|
23049
|
+
}
|
|
23050
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
23051
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
23052
|
+
assert2(
|
|
23053
|
+
dest instanceof Uint8ClampedArray,
|
|
23054
|
+
'CalRGBCS.getRgbBuffer: Unsupported "dest" type.'
|
|
23055
|
+
);
|
|
23056
|
+
}
|
|
23057
|
+
const scale = 1 / ((1 << bits) - 1);
|
|
23058
|
+
for (let i = 0; i < count; ++i) {
|
|
23059
|
+
this.#toRgb(src, srcOffset, dest, destOffset, scale);
|
|
23060
|
+
srcOffset += 3;
|
|
23061
|
+
destOffset += 3 + alpha01;
|
|
23062
|
+
}
|
|
23063
|
+
}
|
|
23064
|
+
};
|
|
23065
|
+
var LabCS = class extends ColorSpace {
|
|
23066
|
+
constructor(whitePoint, blackPoint, range) {
|
|
23067
|
+
super("Lab", 3);
|
|
23068
|
+
if (!whitePoint) {
|
|
23069
|
+
throw new FormatError(
|
|
23070
|
+
"WhitePoint missing - required for color space Lab"
|
|
23071
|
+
);
|
|
23072
|
+
}
|
|
23073
|
+
[this.XW, this.YW, this.ZW] = whitePoint;
|
|
23074
|
+
[this.amin, this.amax, this.bmin, this.bmax] = range || [
|
|
23075
|
+
-100,
|
|
23076
|
+
100,
|
|
23077
|
+
-100,
|
|
23078
|
+
100
|
|
23079
|
+
];
|
|
23080
|
+
[this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
|
|
23081
|
+
if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
|
|
23082
|
+
throw new FormatError(
|
|
23083
|
+
"Invalid WhitePoint components, no fallback available"
|
|
23084
|
+
);
|
|
23085
|
+
}
|
|
23086
|
+
if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
|
|
23087
|
+
info("Invalid BlackPoint, falling back to default");
|
|
23088
|
+
this.XB = this.YB = this.ZB = 0;
|
|
23089
|
+
}
|
|
23090
|
+
if (this.amin > this.amax || this.bmin > this.bmax) {
|
|
23091
|
+
info("Invalid Range, falling back to defaults");
|
|
23092
|
+
this.amin = -100;
|
|
23093
|
+
this.amax = 100;
|
|
23094
|
+
this.bmin = -100;
|
|
23095
|
+
this.bmax = 100;
|
|
23096
|
+
}
|
|
23097
|
+
}
|
|
23098
|
+
// Function g(x) from spec
|
|
23099
|
+
#fn_g(x) {
|
|
23100
|
+
return x >= 6 / 29 ? x ** 3 : 108 / 841 * (x - 4 / 29);
|
|
23101
|
+
}
|
|
23102
|
+
#decode(value, high1, low2, high2) {
|
|
23103
|
+
return low2 + value * (high2 - low2) / high1;
|
|
23104
|
+
}
|
|
23105
|
+
// If decoding is needed maxVal should be 2^bits per component - 1.
|
|
23106
|
+
#toRgb(src, srcOffset, maxVal, dest, destOffset) {
|
|
23107
|
+
let Ls = src[srcOffset];
|
|
23108
|
+
let as = src[srcOffset + 1];
|
|
23109
|
+
let bs = src[srcOffset + 2];
|
|
23110
|
+
if (maxVal !== false) {
|
|
23111
|
+
Ls = this.#decode(Ls, maxVal, 0, 100);
|
|
23112
|
+
as = this.#decode(as, maxVal, this.amin, this.amax);
|
|
23113
|
+
bs = this.#decode(bs, maxVal, this.bmin, this.bmax);
|
|
23114
|
+
}
|
|
23115
|
+
if (as > this.amax) {
|
|
23116
|
+
as = this.amax;
|
|
23117
|
+
} else if (as < this.amin) {
|
|
23118
|
+
as = this.amin;
|
|
23119
|
+
}
|
|
23120
|
+
if (bs > this.bmax) {
|
|
23121
|
+
bs = this.bmax;
|
|
23122
|
+
} else if (bs < this.bmin) {
|
|
23123
|
+
bs = this.bmin;
|
|
23124
|
+
}
|
|
23125
|
+
const M = (Ls + 16) / 116;
|
|
23126
|
+
const L = M + as / 500;
|
|
23127
|
+
const N = M - bs / 200;
|
|
23128
|
+
const X = this.XW * this.#fn_g(L);
|
|
23129
|
+
const Y = this.YW * this.#fn_g(M);
|
|
23130
|
+
const Z = this.ZW * this.#fn_g(N);
|
|
23131
|
+
let r, g, b;
|
|
23132
|
+
if (this.ZW < 1) {
|
|
23133
|
+
r = X * 3.1339 + Y * -1.617 + Z * -0.4906;
|
|
23134
|
+
g = X * -0.9785 + Y * 1.916 + Z * 0.0333;
|
|
23135
|
+
b = X * 0.072 + Y * -0.229 + Z * 1.4057;
|
|
23136
|
+
} else {
|
|
23137
|
+
r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
|
|
23138
|
+
g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
|
|
23139
|
+
b = X * 0.0557 + Y * -0.204 + Z * 1.057;
|
|
23140
|
+
}
|
|
23141
|
+
dest[destOffset] = Math.sqrt(r) * 255;
|
|
23142
|
+
dest[destOffset + 1] = Math.sqrt(g) * 255;
|
|
23143
|
+
dest[destOffset + 2] = Math.sqrt(b) * 255;
|
|
23144
|
+
}
|
|
23145
|
+
getRgbItem(src, srcOffset, dest, destOffset) {
|
|
23146
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
23147
|
+
assert2(
|
|
23148
|
+
dest instanceof Uint8ClampedArray,
|
|
23149
|
+
'LabCS.getRgbItem: Unsupported "dest" type.'
|
|
23150
|
+
);
|
|
23151
|
+
}
|
|
23152
|
+
this.#toRgb(src, srcOffset, false, dest, destOffset);
|
|
23153
|
+
}
|
|
23154
|
+
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
|
|
23155
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
23156
|
+
assert2(
|
|
23157
|
+
dest instanceof Uint8ClampedArray,
|
|
23158
|
+
'LabCS.getRgbBuffer: Unsupported "dest" type.'
|
|
23159
|
+
);
|
|
23160
|
+
}
|
|
23161
|
+
const maxVal = (1 << bits) - 1;
|
|
23162
|
+
for (let i = 0; i < count; i++) {
|
|
23163
|
+
this.#toRgb(src, srcOffset, maxVal, dest, destOffset);
|
|
23164
|
+
srcOffset += 3;
|
|
23165
|
+
destOffset += 3 + alpha01;
|
|
23166
|
+
}
|
|
23167
|
+
}
|
|
23168
|
+
isDefaultDecode(decode, bpc) {
|
|
23169
|
+
return true;
|
|
23170
|
+
}
|
|
23171
|
+
get usesZeroToOneRange() {
|
|
23172
|
+
return shadow2(this, "usesZeroToOneRange", false);
|
|
23173
|
+
}
|
|
23174
|
+
};
|
|
23175
|
+
var TOKEN = {
|
|
23176
|
+
// Structural tokens — not keyword operators
|
|
23177
|
+
number: 0,
|
|
23178
|
+
lbrace: 1,
|
|
23179
|
+
rbrace: 2,
|
|
23180
|
+
// Boolean literals
|
|
23181
|
+
true: 3,
|
|
23182
|
+
false: 4,
|
|
23183
|
+
// Arithmetic binary operators
|
|
23184
|
+
add: 5,
|
|
23185
|
+
sub: 6,
|
|
23186
|
+
mul: 7,
|
|
23187
|
+
div: 8,
|
|
23188
|
+
idiv: 9,
|
|
23189
|
+
mod: 10,
|
|
23190
|
+
exp: 11,
|
|
23191
|
+
// Comparison binary operators
|
|
23192
|
+
eq: 12,
|
|
23193
|
+
ne: 13,
|
|
23194
|
+
gt: 14,
|
|
23195
|
+
ge: 15,
|
|
23196
|
+
lt: 16,
|
|
23197
|
+
le: 17,
|
|
23198
|
+
// Bitwise / boolean binary operators
|
|
23199
|
+
and: 18,
|
|
23200
|
+
or: 19,
|
|
23201
|
+
xor: 20,
|
|
23202
|
+
bitshift: 21,
|
|
23203
|
+
// Unary arithmetic operators
|
|
23204
|
+
abs: 22,
|
|
23205
|
+
neg: 23,
|
|
23206
|
+
ceiling: 24,
|
|
23207
|
+
floor: 25,
|
|
23208
|
+
round: 26,
|
|
23209
|
+
truncate: 27,
|
|
23210
|
+
// Unary boolean / bitwise operator
|
|
23211
|
+
not: 28,
|
|
23212
|
+
// Mathematical functions — unary
|
|
23213
|
+
sqrt: 29,
|
|
23214
|
+
sin: 30,
|
|
23215
|
+
cos: 31,
|
|
23216
|
+
ln: 32,
|
|
23217
|
+
log: 33,
|
|
23218
|
+
// Mathematical function — binary
|
|
23219
|
+
atan: 34,
|
|
23220
|
+
// Type conversion operators
|
|
23221
|
+
cvi: 35,
|
|
23222
|
+
cvr: 36,
|
|
23223
|
+
// Stack operators
|
|
23224
|
+
dup: 37,
|
|
23225
|
+
exch: 38,
|
|
23226
|
+
pop: 39,
|
|
23227
|
+
copy: 40,
|
|
23228
|
+
index: 41,
|
|
23229
|
+
roll: 42,
|
|
23230
|
+
// Control flow
|
|
23231
|
+
if: 43,
|
|
23232
|
+
ifelse: 44,
|
|
23233
|
+
// End of input
|
|
23234
|
+
eof: 45,
|
|
23235
|
+
// Synthetic: produced by the optimizer, never emitted by the lexer.
|
|
23236
|
+
min: 46,
|
|
23237
|
+
max: 47
|
|
23238
|
+
};
|
|
23239
|
+
var Token = class {
|
|
23240
|
+
constructor(id, value = null) {
|
|
23241
|
+
this.id = id;
|
|
23242
|
+
this.value = value;
|
|
23243
|
+
}
|
|
23244
|
+
};
|
|
23245
|
+
var Lexer = class _Lexer {
|
|
23246
|
+
// Singletons for every non-number token, built lazily on first construction.
|
|
23247
|
+
// Keyword operator tokens carry their name as `value`; structural tokens
|
|
23248
|
+
// (lbrace, rbrace, eof) carry null.
|
|
23249
|
+
static #singletons = null;
|
|
23250
|
+
static #operatorSingletons = null;
|
|
23251
|
+
static #initSingletons() {
|
|
23252
|
+
const singletons = /* @__PURE__ */ Object.create(null);
|
|
23253
|
+
const operatorSingletons = /* @__PURE__ */ Object.create(null);
|
|
23254
|
+
for (const [name, id] of Object.entries(TOKEN)) {
|
|
23255
|
+
if (name === "number") {
|
|
23256
|
+
continue;
|
|
23257
|
+
}
|
|
23258
|
+
const isOperator = id >= TOKEN.true && id <= TOKEN.ifelse;
|
|
23259
|
+
const token = new Token(id, isOperator ? name : null);
|
|
23260
|
+
singletons[name] = token;
|
|
23261
|
+
if (isOperator) {
|
|
23262
|
+
operatorSingletons[name] = token;
|
|
23263
|
+
}
|
|
23264
|
+
}
|
|
23265
|
+
this.#singletons = singletons;
|
|
23266
|
+
this.#operatorSingletons = operatorSingletons;
|
|
23267
|
+
}
|
|
23268
|
+
constructor(data) {
|
|
23269
|
+
if (!_Lexer.#singletons) {
|
|
23270
|
+
_Lexer.#initSingletons();
|
|
23271
|
+
}
|
|
23272
|
+
this.data = data;
|
|
23273
|
+
this.pos = 0;
|
|
23274
|
+
this.len = data.length;
|
|
23275
|
+
this._numberPattern = /[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?/iy;
|
|
23276
|
+
this._identifierPattern = /[a-z]+/y;
|
|
23277
|
+
}
|
|
23278
|
+
// Skip a % comment, advancing past the next \n or \r (or to EOF).
|
|
23279
|
+
_skipComment() {
|
|
23280
|
+
const lf = this.data.indexOf("\n", this.pos);
|
|
23281
|
+
const cr = this.data.indexOf("\r", this.pos);
|
|
23282
|
+
const eol = Math.min(lf < 0 ? this.len : lf, cr < 0 ? this.len : cr);
|
|
23283
|
+
this.pos = Math.min(eol + 1, this.len);
|
|
23284
|
+
}
|
|
23285
|
+
_getNumber() {
|
|
23286
|
+
this._numberPattern.lastIndex = this.pos;
|
|
23287
|
+
const match = this._numberPattern.exec(this.data);
|
|
23288
|
+
if (!match) {
|
|
23289
|
+
return new Token(TOKEN.number, 0);
|
|
23290
|
+
}
|
|
23291
|
+
const number = parseFloat(match[0]);
|
|
23292
|
+
if (!Number.isFinite(number)) {
|
|
23293
|
+
return new Token(TOKEN.number, 0);
|
|
23294
|
+
}
|
|
23295
|
+
this.pos = this._numberPattern.lastIndex;
|
|
23296
|
+
return new Token(TOKEN.number, number);
|
|
23297
|
+
}
|
|
23298
|
+
_getOperator() {
|
|
23299
|
+
this._identifierPattern.lastIndex = this.pos;
|
|
23300
|
+
const match = this._identifierPattern.exec(this.data);
|
|
23301
|
+
if (!match) {
|
|
23302
|
+
return new Token(TOKEN.number, 0);
|
|
23303
|
+
}
|
|
23304
|
+
this.pos = this._identifierPattern.lastIndex;
|
|
23305
|
+
const op = match[0];
|
|
23306
|
+
const token = _Lexer.#operatorSingletons[op];
|
|
23307
|
+
return token ?? new Token(TOKEN.number, 0);
|
|
23308
|
+
}
|
|
23309
|
+
// Return the next token, or Lexer.#singletons.eof at end of input.
|
|
23310
|
+
next() {
|
|
23311
|
+
while (this.pos < this.len) {
|
|
23312
|
+
const ch3 = this.data.charCodeAt(this.pos++);
|
|
23313
|
+
switch (ch3) {
|
|
23314
|
+
// PostScript white-space characters (PDF32000 §7.2.2)
|
|
23315
|
+
case 0:
|
|
23316
|
+
case 9:
|
|
23317
|
+
case 10:
|
|
23318
|
+
case 12:
|
|
23319
|
+
case 13:
|
|
23320
|
+
case 32:
|
|
23321
|
+
break;
|
|
23322
|
+
case 37:
|
|
23323
|
+
this._skipComment();
|
|
23324
|
+
break;
|
|
23325
|
+
case 123:
|
|
23326
|
+
return _Lexer.#singletons.lbrace;
|
|
23327
|
+
case 125:
|
|
23328
|
+
return _Lexer.#singletons.rbrace;
|
|
23329
|
+
case 43:
|
|
23330
|
+
case 45:
|
|
23331
|
+
this.pos--;
|
|
23332
|
+
return this._getNumber();
|
|
23333
|
+
case 46:
|
|
23334
|
+
this.pos--;
|
|
23335
|
+
return this._getNumber();
|
|
23336
|
+
default:
|
|
23337
|
+
if (ch3 >= 48 && ch3 <= 57) {
|
|
23338
|
+
this.pos--;
|
|
23339
|
+
return this._getNumber();
|
|
23340
|
+
}
|
|
23341
|
+
if (ch3 >= 97 && ch3 <= 122) {
|
|
23342
|
+
this.pos--;
|
|
23343
|
+
return this._getOperator();
|
|
23344
|
+
}
|
|
23345
|
+
return new Token(TOKEN.number, 0);
|
|
23346
|
+
}
|
|
23347
|
+
}
|
|
23348
|
+
return _Lexer.#singletons.eof;
|
|
23349
|
+
}
|
|
23350
|
+
};
|
|
23351
|
+
var PS_VALUE_TYPE = {
|
|
23352
|
+
numeric: 0,
|
|
23353
|
+
// known to be a number (f64 in Wasm)
|
|
23354
|
+
boolean: 1,
|
|
23355
|
+
// known to be a boolean (0.0 = false, 1.0 = true in f64)
|
|
23356
|
+
unknown: 2
|
|
23357
|
+
// indeterminate at compile time
|
|
23358
|
+
};
|
|
23359
|
+
var PS_NODE = {
|
|
23360
|
+
// Parser AST node types (produced by Parser / parsePostScriptFunction)
|
|
23361
|
+
program: 0,
|
|
23362
|
+
block: 1,
|
|
23363
|
+
number: 2,
|
|
23364
|
+
operator: 3,
|
|
23365
|
+
if: 4,
|
|
23366
|
+
ifelse: 5,
|
|
23367
|
+
// Tree AST node types (produced by PSStackToTree)
|
|
23368
|
+
arg: 6,
|
|
23369
|
+
const: 7,
|
|
23370
|
+
unary: 8,
|
|
23371
|
+
binary: 9,
|
|
23372
|
+
ternary: 10
|
|
23373
|
+
};
|
|
23374
|
+
var PsNode = class {
|
|
23375
|
+
constructor(type) {
|
|
23376
|
+
this.type = type;
|
|
23377
|
+
}
|
|
23378
|
+
};
|
|
23379
|
+
var PsProgram = class extends PsNode {
|
|
23380
|
+
constructor(body) {
|
|
23381
|
+
super(PS_NODE.program);
|
|
23382
|
+
this.body = body;
|
|
23383
|
+
}
|
|
23384
|
+
};
|
|
23385
|
+
var PsBlock = class extends PsNode {
|
|
23386
|
+
constructor(instructions) {
|
|
23387
|
+
super(PS_NODE.block);
|
|
23388
|
+
this.instructions = instructions;
|
|
23389
|
+
}
|
|
23390
|
+
};
|
|
23391
|
+
var PsNumber = class extends PsNode {
|
|
23392
|
+
/** @param {number} value */
|
|
23393
|
+
constructor(value) {
|
|
23394
|
+
super(PS_NODE.number);
|
|
23395
|
+
this.value = value;
|
|
23396
|
+
}
|
|
23397
|
+
};
|
|
23398
|
+
var PsOperator = class extends PsNode {
|
|
23399
|
+
/** @param {number} op — one of the TOKEN.* constants from lexer.js */
|
|
23400
|
+
constructor(op) {
|
|
23401
|
+
super(PS_NODE.operator);
|
|
23402
|
+
this.op = op;
|
|
23403
|
+
}
|
|
23404
|
+
};
|
|
23405
|
+
var PsIf = class extends PsNode {
|
|
23406
|
+
/** @param {PsBlock} then */
|
|
23407
|
+
constructor(then) {
|
|
23408
|
+
super(PS_NODE.if);
|
|
23409
|
+
this.then = then;
|
|
23410
|
+
}
|
|
23411
|
+
};
|
|
23412
|
+
var PsIfElse = class extends PsNode {
|
|
23413
|
+
/**
|
|
23414
|
+
* @param {PsBlock} then
|
|
23415
|
+
* @param {PsBlock} otherwise
|
|
23416
|
+
*/
|
|
23417
|
+
constructor(then, otherwise) {
|
|
23418
|
+
super(PS_NODE.ifelse);
|
|
23419
|
+
this.then = then;
|
|
23420
|
+
this.otherwise = otherwise;
|
|
23421
|
+
}
|
|
23422
|
+
};
|
|
23423
|
+
var PsArgNode = class extends PsNode {
|
|
23424
|
+
/** @param {number} index */
|
|
23425
|
+
constructor(index) {
|
|
23426
|
+
super(PS_NODE.arg);
|
|
23427
|
+
this.index = index;
|
|
23428
|
+
this.valueType = PS_VALUE_TYPE.numeric;
|
|
23429
|
+
}
|
|
23430
|
+
};
|
|
23431
|
+
var PsConstNode = class extends PsNode {
|
|
23432
|
+
/** @param {number|boolean} value */
|
|
23433
|
+
constructor(value) {
|
|
23434
|
+
super(PS_NODE.const);
|
|
23435
|
+
this.value = value;
|
|
23436
|
+
this.valueType = typeof value === "boolean" ? PS_VALUE_TYPE.boolean : PS_VALUE_TYPE.numeric;
|
|
23437
|
+
}
|
|
23438
|
+
};
|
|
23439
|
+
var PsUnaryNode = class extends PsNode {
|
|
23440
|
+
/**
|
|
23441
|
+
* @param {number} op — TOKEN.* constant
|
|
23442
|
+
* @param {PsNode} operand
|
|
23443
|
+
* @param {number} [valueType]
|
|
23444
|
+
*/
|
|
23445
|
+
constructor(op, operand, valueType = PS_VALUE_TYPE.unknown) {
|
|
23446
|
+
super(PS_NODE.unary);
|
|
23447
|
+
this.op = op;
|
|
23448
|
+
this.operand = operand;
|
|
23449
|
+
this.valueType = valueType;
|
|
23450
|
+
}
|
|
23451
|
+
};
|
|
23452
|
+
var PsBinaryNode = class extends PsNode {
|
|
23453
|
+
/**
|
|
23454
|
+
* @param {number} op — TOKEN.* constant
|
|
23455
|
+
* @param {PsNode} first — was on top of stack
|
|
23456
|
+
* @param {PsNode} second — was below top
|
|
23457
|
+
* @param {number} [valueType]
|
|
23458
|
+
*/
|
|
23459
|
+
constructor(op, first, second, valueType = PS_VALUE_TYPE.unknown) {
|
|
23460
|
+
super(PS_NODE.binary);
|
|
23461
|
+
this.op = op;
|
|
23462
|
+
this.first = first;
|
|
23463
|
+
this.second = second;
|
|
23464
|
+
this.valueType = valueType;
|
|
23465
|
+
}
|
|
23466
|
+
};
|
|
23467
|
+
var PsTernaryNode = class extends PsNode {
|
|
23468
|
+
/**
|
|
23469
|
+
* @param {PsNode} cond
|
|
23470
|
+
* @param {PsNode} then
|
|
23471
|
+
* @param {PsNode} otherwise
|
|
23472
|
+
* @param {number} [valueType]
|
|
23473
|
+
*/
|
|
23474
|
+
constructor(cond, then, otherwise, valueType = PS_VALUE_TYPE.unknown) {
|
|
23475
|
+
super(PS_NODE.ternary);
|
|
23476
|
+
this.cond = cond;
|
|
23477
|
+
this.then = then;
|
|
23478
|
+
this.otherwise = otherwise;
|
|
23479
|
+
this.valueType = valueType;
|
|
23480
|
+
}
|
|
23481
|
+
};
|
|
23482
|
+
var Parser = class _Parser {
|
|
23483
|
+
constructor(lexer) {
|
|
23484
|
+
this.lexer = lexer;
|
|
23485
|
+
this._token = null;
|
|
23486
|
+
}
|
|
23487
|
+
static _isRegularOperator(id) {
|
|
23488
|
+
return id >= TOKEN.true && id < TOKEN.if;
|
|
23489
|
+
}
|
|
23490
|
+
// Fetch the next token from the lexer.
|
|
23491
|
+
_advance() {
|
|
23492
|
+
this._token = this.lexer.next();
|
|
23493
|
+
}
|
|
23494
|
+
// Assert that the current token has the given id, consume it, and return it.
|
|
23495
|
+
_expect(id) {
|
|
23496
|
+
if (this._token.id !== id) {
|
|
23497
|
+
throw new FormatError(
|
|
23498
|
+
`PostScript function: expected token id ${id}, got ${this._token.id}.`
|
|
23499
|
+
);
|
|
23500
|
+
}
|
|
23501
|
+
const tok = this._token;
|
|
23502
|
+
this._advance();
|
|
23503
|
+
return tok;
|
|
23504
|
+
}
|
|
23505
|
+
/**
|
|
23506
|
+
* Parse the full Type 4 function body.
|
|
23507
|
+
*
|
|
23508
|
+
* Grammar (simplified):
|
|
23509
|
+
* program ::= '{' block '}'
|
|
23510
|
+
* block ::= instruction*
|
|
23511
|
+
* instruction ::= number
|
|
23512
|
+
* | operator (any PS_OPERATOR except if / ifelse)
|
|
23513
|
+
* | '{' block '}' 'if'
|
|
23514
|
+
* | '{' block '}' '{' block '}' 'ifelse'
|
|
23515
|
+
* @returns {PsProgram}
|
|
23516
|
+
*/
|
|
23517
|
+
parse() {
|
|
23518
|
+
this._advance();
|
|
23519
|
+
this._expect(TOKEN.lbrace);
|
|
23520
|
+
const block = this._parseBlock();
|
|
23521
|
+
this._expect(TOKEN.rbrace);
|
|
23522
|
+
if (this._token.id !== TOKEN.eof) {
|
|
23523
|
+
warn2("PostScript function: unexpected content after closing brace.");
|
|
23524
|
+
}
|
|
23525
|
+
return new PsProgram(block);
|
|
23526
|
+
}
|
|
23527
|
+
_parseBlock() {
|
|
23528
|
+
const instructions = [];
|
|
23529
|
+
while (true) {
|
|
23530
|
+
const tok = this._token;
|
|
23531
|
+
switch (tok.id) {
|
|
23532
|
+
case TOKEN.number:
|
|
23533
|
+
instructions.push(new PsNumber(tok.value));
|
|
23534
|
+
this._advance();
|
|
23535
|
+
break;
|
|
23536
|
+
case TOKEN.lbrace: {
|
|
23537
|
+
this._advance();
|
|
23538
|
+
const thenBlock = this._parseBlock();
|
|
23539
|
+
this._expect(TOKEN.rbrace);
|
|
23540
|
+
if (this._token.id === TOKEN.if) {
|
|
23541
|
+
this._advance();
|
|
23542
|
+
instructions.push(new PsIf(thenBlock));
|
|
23543
|
+
} else if (this._token.id === TOKEN.lbrace) {
|
|
23544
|
+
this._advance();
|
|
23545
|
+
const elseBlock = this._parseBlock();
|
|
23546
|
+
this._expect(TOKEN.rbrace);
|
|
23547
|
+
this._expect(TOKEN.ifelse);
|
|
23548
|
+
instructions.push(new PsIfElse(thenBlock, elseBlock));
|
|
23549
|
+
} else {
|
|
23550
|
+
throw new FormatError(
|
|
23551
|
+
"PostScript function: a procedure block must be followed by 'if' or '{\u2026} ifelse'."
|
|
23552
|
+
);
|
|
23553
|
+
}
|
|
23554
|
+
break;
|
|
23555
|
+
}
|
|
23556
|
+
case TOKEN.rbrace:
|
|
23557
|
+
case TOKEN.eof:
|
|
23558
|
+
return new PsBlock(instructions);
|
|
23559
|
+
case TOKEN.if:
|
|
23560
|
+
case TOKEN.ifelse:
|
|
23561
|
+
throw new FormatError(
|
|
23562
|
+
`PostScript function: unexpected '${tok.value}' operator.`
|
|
23563
|
+
);
|
|
23564
|
+
default:
|
|
23565
|
+
if (_Parser._isRegularOperator(tok.id)) {
|
|
23566
|
+
instructions.push(new PsOperator(tok.id));
|
|
23567
|
+
this._advance();
|
|
23568
|
+
break;
|
|
23569
|
+
}
|
|
23570
|
+
throw new FormatError(
|
|
23571
|
+
`PostScript function: unexpected token id ${tok.id}.`
|
|
23572
|
+
);
|
|
23573
|
+
}
|
|
23574
|
+
}
|
|
23575
|
+
}
|
|
23576
|
+
};
|
|
23577
|
+
function parsePostScriptFunction(source) {
|
|
23578
|
+
return new Parser(new Lexer(source)).parse();
|
|
23579
|
+
}
|
|
23580
|
+
function _nodesEqual(a, b) {
|
|
23581
|
+
if (a === b) {
|
|
23582
|
+
return true;
|
|
23583
|
+
}
|
|
23584
|
+
if (a.type !== b.type) {
|
|
23585
|
+
return false;
|
|
23586
|
+
}
|
|
23587
|
+
switch (a.type) {
|
|
23588
|
+
case PS_NODE.arg:
|
|
23589
|
+
return a.index === b.index;
|
|
23590
|
+
case PS_NODE.const:
|
|
23591
|
+
return a.value === b.value;
|
|
23592
|
+
case PS_NODE.unary:
|
|
23593
|
+
return a.op === b.op && _nodesEqual(a.operand, b.operand);
|
|
23594
|
+
case PS_NODE.binary:
|
|
23595
|
+
return a.op === b.op && _nodesEqual(a.first, b.first) && _nodesEqual(a.second, b.second);
|
|
23596
|
+
case PS_NODE.ternary:
|
|
23597
|
+
return _nodesEqual(a.cond, b.cond) && _nodesEqual(a.then, b.then) && _nodesEqual(a.otherwise, b.otherwise);
|
|
23598
|
+
default:
|
|
23599
|
+
return false;
|
|
23600
|
+
}
|
|
23601
|
+
}
|
|
23602
|
+
function _evalBinaryConst(op, a, b) {
|
|
23603
|
+
switch (op) {
|
|
23604
|
+
case TOKEN.add:
|
|
23605
|
+
return a + b;
|
|
23606
|
+
case TOKEN.sub:
|
|
23607
|
+
return a - b;
|
|
23608
|
+
case TOKEN.mul:
|
|
23609
|
+
return a * b;
|
|
23610
|
+
case TOKEN.div:
|
|
23611
|
+
return b !== 0 ? a / b : 0;
|
|
23612
|
+
// div by zero → 0
|
|
23613
|
+
case TOKEN.idiv:
|
|
23614
|
+
return b !== 0 ? Math.trunc(a / b) : 0;
|
|
23615
|
+
// div by zero → 0
|
|
23616
|
+
case TOKEN.mod:
|
|
23617
|
+
return b !== 0 ? a - Math.trunc(a / b) * b : 0;
|
|
23618
|
+
// div by zero → 0
|
|
23619
|
+
case TOKEN.exp: {
|
|
23620
|
+
const r = a ** b;
|
|
23621
|
+
return Number.isFinite(r) ? r : void 0;
|
|
23622
|
+
}
|
|
23623
|
+
case TOKEN.atan: {
|
|
23624
|
+
let deg = Math.atan2(a, b) * (180 / Math.PI);
|
|
23625
|
+
if (deg < 0) {
|
|
23626
|
+
deg += 360;
|
|
23627
|
+
}
|
|
23628
|
+
return deg;
|
|
23629
|
+
}
|
|
23630
|
+
case TOKEN.eq:
|
|
23631
|
+
return a === b;
|
|
23632
|
+
case TOKEN.ne:
|
|
23633
|
+
return a !== b;
|
|
23634
|
+
case TOKEN.gt:
|
|
23635
|
+
return a > b;
|
|
23636
|
+
case TOKEN.ge:
|
|
23637
|
+
return a >= b;
|
|
23638
|
+
case TOKEN.lt:
|
|
23639
|
+
return a < b;
|
|
23640
|
+
case TOKEN.le:
|
|
23641
|
+
return a <= b;
|
|
23642
|
+
case TOKEN.and:
|
|
23643
|
+
return typeof a === "boolean" ? a && b : a & b | 0;
|
|
23644
|
+
case TOKEN.or:
|
|
23645
|
+
return typeof a === "boolean" ? a || b : a | b | 0;
|
|
23646
|
+
case TOKEN.xor:
|
|
23647
|
+
return typeof a === "boolean" ? a !== b : a ^ b | 0;
|
|
23648
|
+
case TOKEN.bitshift:
|
|
23649
|
+
return b >= 0 ? a << b | 0 : a >> -b | 0;
|
|
23650
|
+
case TOKEN.min:
|
|
23651
|
+
return Math.min(a, b);
|
|
23652
|
+
case TOKEN.max:
|
|
23653
|
+
return Math.max(a, b);
|
|
23654
|
+
default:
|
|
23655
|
+
return void 0;
|
|
23656
|
+
}
|
|
23657
|
+
}
|
|
23658
|
+
function _evalUnaryConst(op, v) {
|
|
23659
|
+
switch (op) {
|
|
23660
|
+
case TOKEN.abs:
|
|
23661
|
+
return Math.abs(v);
|
|
23662
|
+
case TOKEN.neg:
|
|
23663
|
+
return -v;
|
|
23664
|
+
case TOKEN.ceiling:
|
|
23665
|
+
return Math.ceil(v);
|
|
23666
|
+
case TOKEN.floor:
|
|
23667
|
+
return Math.floor(v);
|
|
23668
|
+
case TOKEN.round:
|
|
23669
|
+
return Math.round(v);
|
|
23670
|
+
case TOKEN.truncate:
|
|
23671
|
+
return Math.trunc(v);
|
|
23672
|
+
case TOKEN.sqrt: {
|
|
23673
|
+
const r = Math.sqrt(v);
|
|
23674
|
+
return Number.isFinite(r) ? r : void 0;
|
|
23675
|
+
}
|
|
23676
|
+
case TOKEN.sin:
|
|
23677
|
+
return Math.sin(v % 360 * Math.PI / 180);
|
|
23678
|
+
case TOKEN.cos:
|
|
23679
|
+
return Math.cos(v % 360 * Math.PI / 180);
|
|
23680
|
+
case TOKEN.ln: {
|
|
23681
|
+
const r = Math.log(v);
|
|
23682
|
+
return Number.isFinite(r) ? r : void 0;
|
|
23683
|
+
}
|
|
23684
|
+
case TOKEN.log: {
|
|
23685
|
+
const r = Math.log10(v);
|
|
23686
|
+
return Number.isFinite(r) ? r : void 0;
|
|
23687
|
+
}
|
|
23688
|
+
case TOKEN.cvi:
|
|
23689
|
+
return Math.trunc(v);
|
|
23690
|
+
case TOKEN.cvr:
|
|
23691
|
+
return v;
|
|
23692
|
+
case TOKEN.not:
|
|
23693
|
+
return typeof v === "boolean" ? !v : ~v;
|
|
23694
|
+
default:
|
|
23695
|
+
return void 0;
|
|
23696
|
+
}
|
|
23697
|
+
}
|
|
23698
|
+
var MAX_STACK_SIZE = 100;
|
|
23699
|
+
function _unaryValueType(op, operandType) {
|
|
23700
|
+
return op === TOKEN.not ? operandType : PS_VALUE_TYPE.numeric;
|
|
23701
|
+
}
|
|
23702
|
+
function _binaryValueType(op, firstType, secondType) {
|
|
23703
|
+
switch (op) {
|
|
23704
|
+
// Comparison operators always produce a boolean.
|
|
23705
|
+
case TOKEN.eq:
|
|
23706
|
+
case TOKEN.ne:
|
|
23707
|
+
case TOKEN.gt:
|
|
23708
|
+
case TOKEN.ge:
|
|
23709
|
+
case TOKEN.lt:
|
|
23710
|
+
case TOKEN.le:
|
|
23711
|
+
return PS_VALUE_TYPE.boolean;
|
|
23712
|
+
// and / or / xor preserve the type when both operands are the same known
|
|
23713
|
+
// type (both boolean or both numeric); otherwise the type is unknown.
|
|
23714
|
+
case TOKEN.and:
|
|
23715
|
+
case TOKEN.or:
|
|
23716
|
+
case TOKEN.xor:
|
|
23717
|
+
return firstType === secondType && firstType !== PS_VALUE_TYPE.unknown ? firstType : PS_VALUE_TYPE.unknown;
|
|
23718
|
+
// All arithmetic / bitshift operators produce a numeric result.
|
|
23719
|
+
default:
|
|
23720
|
+
return PS_VALUE_TYPE.numeric;
|
|
23721
|
+
}
|
|
23722
|
+
}
|
|
23723
|
+
var PSStackToTree = class _PSStackToTree {
|
|
23724
|
+
static #binaryOps = null;
|
|
23725
|
+
static #unaryOps = null;
|
|
23726
|
+
static #idempotentUnary = null;
|
|
23727
|
+
static #negatedComparison = null;
|
|
23728
|
+
static #init() {
|
|
23729
|
+
this.#binaryOps = /* @__PURE__ */ new Set([
|
|
23730
|
+
TOKEN.add,
|
|
23731
|
+
TOKEN.sub,
|
|
23732
|
+
TOKEN.mul,
|
|
23733
|
+
TOKEN.div,
|
|
23734
|
+
TOKEN.idiv,
|
|
23735
|
+
TOKEN.mod,
|
|
23736
|
+
TOKEN.exp,
|
|
23737
|
+
TOKEN.atan,
|
|
23738
|
+
TOKEN.eq,
|
|
23739
|
+
TOKEN.ne,
|
|
23740
|
+
TOKEN.gt,
|
|
23741
|
+
TOKEN.ge,
|
|
23742
|
+
TOKEN.lt,
|
|
23743
|
+
TOKEN.le,
|
|
23744
|
+
TOKEN.and,
|
|
23745
|
+
TOKEN.or,
|
|
23746
|
+
TOKEN.xor,
|
|
23747
|
+
TOKEN.bitshift
|
|
23748
|
+
]);
|
|
23749
|
+
this.#unaryOps = /* @__PURE__ */ new Set([
|
|
23750
|
+
TOKEN.abs,
|
|
23751
|
+
TOKEN.neg,
|
|
23752
|
+
TOKEN.ceiling,
|
|
23753
|
+
TOKEN.floor,
|
|
23754
|
+
TOKEN.round,
|
|
23755
|
+
TOKEN.truncate,
|
|
23756
|
+
TOKEN.sqrt,
|
|
23757
|
+
TOKEN.sin,
|
|
23758
|
+
TOKEN.cos,
|
|
23759
|
+
TOKEN.ln,
|
|
23760
|
+
TOKEN.log,
|
|
23761
|
+
TOKEN.cvi,
|
|
23762
|
+
TOKEN.cvr,
|
|
23763
|
+
TOKEN.not
|
|
23764
|
+
]);
|
|
23765
|
+
this.#idempotentUnary = /* @__PURE__ */ new Set([
|
|
23766
|
+
TOKEN.abs,
|
|
23767
|
+
TOKEN.ceiling,
|
|
23768
|
+
TOKEN.cvi,
|
|
23769
|
+
TOKEN.cvr,
|
|
23770
|
+
TOKEN.floor,
|
|
23771
|
+
TOKEN.round,
|
|
23772
|
+
TOKEN.truncate
|
|
23773
|
+
]);
|
|
23774
|
+
this.#negatedComparison = /* @__PURE__ */ new Map([
|
|
23775
|
+
[TOKEN.eq, TOKEN.ne],
|
|
23776
|
+
[TOKEN.ne, TOKEN.eq],
|
|
23777
|
+
[TOKEN.lt, TOKEN.ge],
|
|
23778
|
+
[TOKEN.le, TOKEN.gt],
|
|
23779
|
+
[TOKEN.gt, TOKEN.le],
|
|
23780
|
+
[TOKEN.ge, TOKEN.lt]
|
|
23781
|
+
]);
|
|
23782
|
+
}
|
|
23783
|
+
/**
|
|
23784
|
+
* @param {PsProgram} program
|
|
23785
|
+
* @param {number} numInputs — number of domain values placed on the stack
|
|
23786
|
+
* before the program runs (i.e. the length of the domain array / 2).
|
|
23787
|
+
* @returns {Array<PsNode>} — one tree node per output value.
|
|
23788
|
+
*/
|
|
23789
|
+
evaluate(program, numInputs) {
|
|
23790
|
+
if (!_PSStackToTree.#binaryOps) {
|
|
23791
|
+
_PSStackToTree.#init();
|
|
23792
|
+
}
|
|
23793
|
+
this._failed = false;
|
|
23794
|
+
if (numInputs > MAX_STACK_SIZE) {
|
|
23795
|
+
return null;
|
|
23796
|
+
}
|
|
23797
|
+
const stack = [];
|
|
23798
|
+
for (let i = 0; i < numInputs; i++) {
|
|
23799
|
+
stack.push(new PsArgNode(i));
|
|
23800
|
+
}
|
|
23801
|
+
this._evalBlock(program.body, stack);
|
|
23802
|
+
if (this._failed) {
|
|
23803
|
+
return null;
|
|
23804
|
+
}
|
|
23805
|
+
_PSStackToTree.#markShared(stack);
|
|
23806
|
+
return stack;
|
|
23807
|
+
}
|
|
23808
|
+
// Set node.shared / sharedCount on non-atomic nodes referenced more than
|
|
23809
|
+
// once. arg/const are excluded — they are cheap to re-emit inline.
|
|
23810
|
+
static #markShared(outputs) {
|
|
23811
|
+
const refCount = /* @__PURE__ */ new Map();
|
|
23812
|
+
const visit = (node) => {
|
|
23813
|
+
if (!node || node.type === PS_NODE.arg || node.type === PS_NODE.const) {
|
|
23814
|
+
return;
|
|
23815
|
+
}
|
|
23816
|
+
const prev = refCount.get(node) ?? 0;
|
|
23817
|
+
refCount.set(node, prev + 1);
|
|
23818
|
+
if (prev > 0) {
|
|
23819
|
+
return;
|
|
23820
|
+
}
|
|
23821
|
+
switch (node.type) {
|
|
23822
|
+
case PS_NODE.unary:
|
|
23823
|
+
visit(node.operand);
|
|
23824
|
+
break;
|
|
23825
|
+
case PS_NODE.binary:
|
|
23826
|
+
visit(node.first);
|
|
23827
|
+
visit(node.second);
|
|
23828
|
+
break;
|
|
23829
|
+
case PS_NODE.ternary:
|
|
23830
|
+
visit(node.cond);
|
|
23831
|
+
visit(node.then);
|
|
23832
|
+
visit(node.otherwise);
|
|
23833
|
+
break;
|
|
23834
|
+
}
|
|
23835
|
+
};
|
|
23836
|
+
for (const output of outputs) {
|
|
23837
|
+
visit(output);
|
|
23838
|
+
}
|
|
23839
|
+
for (const [node, count] of refCount) {
|
|
23840
|
+
if (count > 1) {
|
|
23841
|
+
node.shared = true;
|
|
23842
|
+
node.sharedCount = count;
|
|
23843
|
+
}
|
|
23844
|
+
}
|
|
23845
|
+
}
|
|
23846
|
+
_evalBlock(block, stack) {
|
|
23847
|
+
this._evalBlockFrom(block.instructions, 0, stack);
|
|
23848
|
+
}
|
|
23849
|
+
/**
|
|
23850
|
+
* Core evaluation loop. Processes `instructions[startIdx…]` in order,
|
|
23851
|
+
* mutating `stack` as each instruction executes.
|
|
23852
|
+
*
|
|
23853
|
+
* When a `{ body } if` instruction grows the stack (the PostScript "early
|
|
23854
|
+
* exit / guard" idiom), the remaining instructions in the current array are
|
|
23855
|
+
* evaluated on **both** the true-branch stack and the false-branch stack,
|
|
23856
|
+
* then the two results are merged into PsTernaryNodes. This handles
|
|
23857
|
+
* patterns like:
|
|
23858
|
+
*
|
|
23859
|
+
* cond { pop R G B sentinel } if
|
|
23860
|
+
* … more guards …
|
|
23861
|
+
* sentinel 0 gt { defaultR defaultG defaultB } if
|
|
23862
|
+
*/
|
|
23863
|
+
_evalBlockFrom(instructions, startIdx, stack) {
|
|
23864
|
+
for (let idx = startIdx; idx < instructions.length; idx++) {
|
|
23865
|
+
if (this._failed) {
|
|
23866
|
+
break;
|
|
23867
|
+
}
|
|
23868
|
+
const instr = instructions[idx];
|
|
23869
|
+
switch (instr.type) {
|
|
23870
|
+
case PS_NODE.number:
|
|
23871
|
+
stack.push(new PsConstNode(instr.value));
|
|
23872
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
23873
|
+
this._failed = true;
|
|
23874
|
+
}
|
|
23875
|
+
break;
|
|
23876
|
+
case PS_NODE.operator:
|
|
23877
|
+
this._evalOp(instr.op, stack);
|
|
23878
|
+
break;
|
|
23879
|
+
case PS_NODE.if: {
|
|
23880
|
+
if (stack.length < 1) {
|
|
23881
|
+
this._failed = true;
|
|
23882
|
+
break;
|
|
23883
|
+
}
|
|
23884
|
+
const cond = stack.pop();
|
|
23885
|
+
const saved = stack.slice();
|
|
23886
|
+
this._evalBlock(instr.then, stack);
|
|
23887
|
+
if (this._failed) {
|
|
23888
|
+
break;
|
|
23889
|
+
}
|
|
23890
|
+
if (stack.length === saved.length) {
|
|
23891
|
+
for (let i = 0; i < stack.length; i++) {
|
|
23892
|
+
if (stack[i] !== saved[i]) {
|
|
23893
|
+
stack[i] = this._makeTernary(cond, stack[i], saved[i]);
|
|
23894
|
+
}
|
|
23895
|
+
}
|
|
23896
|
+
} else if (stack.length > saved.length) {
|
|
23897
|
+
if (cond.type === PS_NODE.const) {
|
|
23898
|
+
if (!cond.value) {
|
|
23899
|
+
stack.length = 0;
|
|
23900
|
+
stack.push(...saved);
|
|
23901
|
+
}
|
|
23902
|
+
break;
|
|
23903
|
+
}
|
|
23904
|
+
const trueStack = stack.slice();
|
|
23905
|
+
this._evalBlockFrom(instructions, idx + 1, trueStack);
|
|
23906
|
+
if (this._failed) {
|
|
23907
|
+
break;
|
|
23908
|
+
}
|
|
23909
|
+
const falseStack = saved;
|
|
23910
|
+
this._evalBlockFrom(instructions, idx + 1, falseStack);
|
|
23911
|
+
if (this._failed) {
|
|
23912
|
+
break;
|
|
23913
|
+
}
|
|
23914
|
+
if (trueStack.length !== falseStack.length) {
|
|
23915
|
+
const zero2 = new PsConstNode(0);
|
|
23916
|
+
while (trueStack.length < falseStack.length) {
|
|
23917
|
+
trueStack.push(zero2);
|
|
23918
|
+
}
|
|
23919
|
+
while (falseStack.length < trueStack.length) {
|
|
23920
|
+
falseStack.push(zero2);
|
|
23921
|
+
}
|
|
23922
|
+
}
|
|
23923
|
+
stack.length = 0;
|
|
23924
|
+
for (let i = 0; i < trueStack.length; i++) {
|
|
23925
|
+
stack.push(this._makeTernary(cond, trueStack[i], falseStack[i]));
|
|
23926
|
+
}
|
|
23927
|
+
return;
|
|
23928
|
+
} else {
|
|
23929
|
+
this._failed = true;
|
|
23930
|
+
}
|
|
23931
|
+
break;
|
|
23932
|
+
}
|
|
23933
|
+
case PS_NODE.ifelse: {
|
|
23934
|
+
if (stack.length < 1) {
|
|
23935
|
+
this._failed = true;
|
|
23936
|
+
break;
|
|
23937
|
+
}
|
|
23938
|
+
const cond = stack.pop();
|
|
23939
|
+
const snapshot = stack.slice();
|
|
23940
|
+
const thenStack = snapshot.slice();
|
|
23941
|
+
this._evalBlock(instr.then, thenStack);
|
|
23942
|
+
if (this._failed) {
|
|
23943
|
+
break;
|
|
23944
|
+
}
|
|
23945
|
+
const elseStack = snapshot.slice();
|
|
23946
|
+
this._evalBlock(instr.otherwise, elseStack);
|
|
23947
|
+
if (this._failed) {
|
|
23948
|
+
break;
|
|
23949
|
+
}
|
|
23950
|
+
if (thenStack.length !== elseStack.length) {
|
|
23951
|
+
const zero2 = new PsConstNode(0);
|
|
23952
|
+
while (thenStack.length < elseStack.length) {
|
|
23953
|
+
thenStack.push(zero2);
|
|
23954
|
+
}
|
|
23955
|
+
while (elseStack.length < thenStack.length) {
|
|
23956
|
+
elseStack.push(zero2);
|
|
23957
|
+
}
|
|
23958
|
+
}
|
|
23959
|
+
stack.length = 0;
|
|
23960
|
+
for (let i = 0; i < thenStack.length; i++) {
|
|
23961
|
+
stack.push(this._makeTernary(cond, thenStack[i], elseStack[i]));
|
|
23962
|
+
}
|
|
23963
|
+
break;
|
|
23964
|
+
}
|
|
23965
|
+
}
|
|
23966
|
+
}
|
|
23967
|
+
}
|
|
23968
|
+
_evalOp(op, stack) {
|
|
23969
|
+
if (_PSStackToTree.#binaryOps.has(op)) {
|
|
23970
|
+
if (stack.length < 2) {
|
|
23971
|
+
this._failed = true;
|
|
23972
|
+
return;
|
|
23973
|
+
}
|
|
23974
|
+
const first = stack.pop();
|
|
23975
|
+
const second = stack.pop();
|
|
23976
|
+
stack.push(this._makeBinary(op, first, second));
|
|
23977
|
+
return;
|
|
23978
|
+
}
|
|
23979
|
+
if (_PSStackToTree.#unaryOps.has(op)) {
|
|
23980
|
+
if (stack.length < 1) {
|
|
23981
|
+
this._failed = true;
|
|
23982
|
+
return;
|
|
23983
|
+
}
|
|
23984
|
+
stack.push(this._makeUnary(op, stack.pop()));
|
|
23985
|
+
return;
|
|
23986
|
+
}
|
|
23987
|
+
switch (op) {
|
|
23988
|
+
case TOKEN.true:
|
|
23989
|
+
stack.push(new PsConstNode(true));
|
|
23990
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
23991
|
+
this._failed = true;
|
|
23992
|
+
}
|
|
23993
|
+
break;
|
|
23994
|
+
case TOKEN.false:
|
|
23995
|
+
stack.push(new PsConstNode(false));
|
|
23996
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
23997
|
+
this._failed = true;
|
|
23998
|
+
}
|
|
23999
|
+
break;
|
|
24000
|
+
case TOKEN.dup:
|
|
24001
|
+
if (stack.length < 1) {
|
|
24002
|
+
this._failed = true;
|
|
24003
|
+
break;
|
|
24004
|
+
}
|
|
24005
|
+
stack.push(stack.at(-1));
|
|
24006
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
24007
|
+
this._failed = true;
|
|
24008
|
+
}
|
|
24009
|
+
break;
|
|
24010
|
+
case TOKEN.exch: {
|
|
24011
|
+
if (stack.length < 2) {
|
|
24012
|
+
this._failed = true;
|
|
24013
|
+
break;
|
|
24014
|
+
}
|
|
24015
|
+
const a = stack.pop();
|
|
24016
|
+
const b = stack.pop();
|
|
24017
|
+
stack.push(a, b);
|
|
24018
|
+
break;
|
|
24019
|
+
}
|
|
24020
|
+
case TOKEN.pop:
|
|
24021
|
+
if (stack.length < 1) {
|
|
24022
|
+
this._failed = true;
|
|
24023
|
+
break;
|
|
24024
|
+
}
|
|
24025
|
+
stack.pop();
|
|
24026
|
+
break;
|
|
24027
|
+
case TOKEN.copy: {
|
|
24028
|
+
if (stack.length < 1) {
|
|
24029
|
+
this._failed = true;
|
|
24030
|
+
break;
|
|
24031
|
+
}
|
|
24032
|
+
const nNode = stack.pop();
|
|
24033
|
+
if (nNode.type === PS_NODE.const) {
|
|
24034
|
+
const n = nNode.value | 0;
|
|
24035
|
+
if (n === 0) {
|
|
24036
|
+
} else if (n < 0 || n > stack.length) {
|
|
24037
|
+
this._failed = true;
|
|
24038
|
+
} else {
|
|
24039
|
+
stack.push(...stack.slice(-n));
|
|
24040
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
24041
|
+
this._failed = true;
|
|
24042
|
+
}
|
|
24043
|
+
}
|
|
24044
|
+
} else {
|
|
24045
|
+
this._failed = true;
|
|
24046
|
+
}
|
|
24047
|
+
break;
|
|
24048
|
+
}
|
|
24049
|
+
case TOKEN.index: {
|
|
24050
|
+
if (stack.length < 1) {
|
|
24051
|
+
this._failed = true;
|
|
24052
|
+
break;
|
|
24053
|
+
}
|
|
24054
|
+
const nNode = stack.pop();
|
|
24055
|
+
if (nNode.type === PS_NODE.const) {
|
|
24056
|
+
const n = nNode.value | 0;
|
|
24057
|
+
if (n < 0 || n >= stack.length) {
|
|
24058
|
+
this._failed = true;
|
|
24059
|
+
} else {
|
|
24060
|
+
stack.push(stack.at(-n - 1));
|
|
24061
|
+
}
|
|
24062
|
+
} else {
|
|
24063
|
+
this._failed = true;
|
|
24064
|
+
}
|
|
24065
|
+
break;
|
|
24066
|
+
}
|
|
24067
|
+
case TOKEN.roll: {
|
|
24068
|
+
if (stack.length < 2) {
|
|
24069
|
+
this._failed = true;
|
|
24070
|
+
break;
|
|
24071
|
+
}
|
|
24072
|
+
const jNode = stack.pop();
|
|
24073
|
+
const nNode = stack.pop();
|
|
24074
|
+
if (nNode.type === PS_NODE.const && jNode.type === PS_NODE.const) {
|
|
24075
|
+
const n = nNode.value | 0;
|
|
24076
|
+
if (n === 0) {
|
|
24077
|
+
} else if (n < 0 || n > stack.length) {
|
|
24078
|
+
this._failed = true;
|
|
24079
|
+
} else {
|
|
24080
|
+
const j = ((jNode.value | 0) % n + n) % n;
|
|
24081
|
+
if (j > 0) {
|
|
24082
|
+
const slice = stack.splice(-n, n);
|
|
24083
|
+
stack.push(...slice.slice(n - j), ...slice.slice(0, n - j));
|
|
24084
|
+
}
|
|
24085
|
+
}
|
|
24086
|
+
} else {
|
|
24087
|
+
this._failed = true;
|
|
24088
|
+
}
|
|
24089
|
+
break;
|
|
24090
|
+
}
|
|
24091
|
+
default:
|
|
24092
|
+
this._failed = true;
|
|
24093
|
+
break;
|
|
24094
|
+
}
|
|
24095
|
+
}
|
|
24096
|
+
/**
|
|
24097
|
+
* Create a binary tree node, applying optimizations eagerly:
|
|
24098
|
+
*
|
|
24099
|
+
* 1. Constant folding — both operands are PsConstNode → fold to PsConstNode.
|
|
24100
|
+
* 2. Reflexive simplifications — x−x→0, x xor x→0, x eq x→true, etc.
|
|
24101
|
+
* 3. Algebraic simplifications with one known operand — identity elements
|
|
24102
|
+
* (x+0→x, x*1→x, …), absorbing elements (x*0→0, x and false→false, …),
|
|
24103
|
+
* and strength reductions (x*-1→neg(x), x^0.5→sqrt(x), x^2→x*x, …).
|
|
24104
|
+
*
|
|
24105
|
+
* Recall: `first` was on top of the stack (right operand for non-commutative
|
|
24106
|
+
* ops), `second` was below (left operand). So `a b sub` → second=a, first=b
|
|
24107
|
+
* → a − b.
|
|
24108
|
+
*/
|
|
24109
|
+
_makeBinary(op, first, second) {
|
|
24110
|
+
if (first.type === PS_NODE.const && second.type === PS_NODE.const) {
|
|
24111
|
+
const v = _evalBinaryConst(op, second.value, first.value);
|
|
24112
|
+
if (v !== void 0) {
|
|
24113
|
+
return new PsConstNode(v);
|
|
24114
|
+
}
|
|
24115
|
+
}
|
|
24116
|
+
if (_nodesEqual(first, second)) {
|
|
24117
|
+
switch (op) {
|
|
24118
|
+
case TOKEN.sub:
|
|
24119
|
+
return new PsConstNode(0);
|
|
24120
|
+
// x − x → 0
|
|
24121
|
+
case TOKEN.xor:
|
|
24122
|
+
return new PsConstNode(
|
|
24123
|
+
/* eslint-disable unicorn/prefer-logical-operator-over-ternary */
|
|
24124
|
+
first.valueType === PS_VALUE_TYPE.boolean ? false : 0
|
|
24125
|
+
/* eslint-enable unicorn/prefer-logical-operator-over-ternary */
|
|
24126
|
+
);
|
|
24127
|
+
// TOKEN.mod, TOKEN.div, TOKEN.idiv are NOT simplified here:
|
|
24128
|
+
// x op x is undefined when x = 0, so we cannot fold without knowing
|
|
24129
|
+
// that x is non-zero.
|
|
24130
|
+
case TOKEN.and:
|
|
24131
|
+
case TOKEN.or:
|
|
24132
|
+
return first;
|
|
24133
|
+
// x and x → x; x or x → x
|
|
24134
|
+
case TOKEN.min:
|
|
24135
|
+
case TOKEN.max:
|
|
24136
|
+
return first;
|
|
24137
|
+
// min(x,x) → x; max(x,x) → x
|
|
24138
|
+
case TOKEN.eq:
|
|
24139
|
+
case TOKEN.ge:
|
|
24140
|
+
case TOKEN.le:
|
|
24141
|
+
return new PsConstNode(true);
|
|
24142
|
+
case TOKEN.ne:
|
|
24143
|
+
case TOKEN.gt:
|
|
24144
|
+
case TOKEN.lt:
|
|
24145
|
+
return new PsConstNode(false);
|
|
24146
|
+
}
|
|
24147
|
+
}
|
|
24148
|
+
if (first.type === PS_NODE.const) {
|
|
24149
|
+
const b = first.value;
|
|
24150
|
+
switch (op) {
|
|
24151
|
+
case TOKEN.add:
|
|
24152
|
+
if (b === 0) {
|
|
24153
|
+
return second;
|
|
24154
|
+
}
|
|
24155
|
+
break;
|
|
24156
|
+
case TOKEN.sub:
|
|
24157
|
+
if (b === 0) {
|
|
24158
|
+
return second;
|
|
24159
|
+
}
|
|
24160
|
+
break;
|
|
24161
|
+
case TOKEN.mul:
|
|
24162
|
+
if (b === 1) {
|
|
24163
|
+
return second;
|
|
24164
|
+
}
|
|
24165
|
+
if (b === 0) {
|
|
24166
|
+
return first;
|
|
24167
|
+
}
|
|
24168
|
+
if (b === -1) {
|
|
24169
|
+
return this._makeUnary(TOKEN.neg, second);
|
|
24170
|
+
}
|
|
24171
|
+
break;
|
|
24172
|
+
case TOKEN.div:
|
|
24173
|
+
if (b !== 0) {
|
|
24174
|
+
return this._makeBinary(TOKEN.mul, new PsConstNode(1 / b), second);
|
|
24175
|
+
}
|
|
24176
|
+
break;
|
|
24177
|
+
case TOKEN.idiv:
|
|
24178
|
+
if (b === 1) {
|
|
24179
|
+
return second;
|
|
24180
|
+
}
|
|
24181
|
+
break;
|
|
24182
|
+
case TOKEN.exp:
|
|
24183
|
+
if (b === 1) {
|
|
24184
|
+
return second;
|
|
24185
|
+
}
|
|
24186
|
+
if (b === -1) {
|
|
24187
|
+
return this._makeBinary(TOKEN.div, second, new PsConstNode(1));
|
|
24188
|
+
}
|
|
24189
|
+
if (b === 0.5) {
|
|
24190
|
+
return this._makeUnary(TOKEN.sqrt, second);
|
|
24191
|
+
}
|
|
24192
|
+
if (b === 0.25) {
|
|
24193
|
+
const sqrtOnce = this._makeUnary(TOKEN.sqrt, second);
|
|
24194
|
+
return this._makeUnary(TOKEN.sqrt, sqrtOnce);
|
|
24195
|
+
}
|
|
24196
|
+
if (b === 2) {
|
|
24197
|
+
return this._makeBinary(TOKEN.mul, second, second);
|
|
24198
|
+
}
|
|
24199
|
+
if (b === 3) {
|
|
24200
|
+
return this._makeBinary(
|
|
24201
|
+
TOKEN.mul,
|
|
24202
|
+
this._makeBinary(TOKEN.mul, second, second),
|
|
24203
|
+
second
|
|
24204
|
+
);
|
|
24205
|
+
}
|
|
24206
|
+
if (b === 4) {
|
|
24207
|
+
const square = this._makeBinary(TOKEN.mul, second, second);
|
|
24208
|
+
return this._makeBinary(TOKEN.mul, square, square);
|
|
24209
|
+
}
|
|
24210
|
+
if (b === 0) {
|
|
24211
|
+
return new PsConstNode(1);
|
|
24212
|
+
}
|
|
24213
|
+
break;
|
|
24214
|
+
case TOKEN.and:
|
|
24215
|
+
if (b === true) {
|
|
24216
|
+
return second;
|
|
24217
|
+
}
|
|
24218
|
+
if (b === false) {
|
|
24219
|
+
return first;
|
|
24220
|
+
}
|
|
24221
|
+
break;
|
|
24222
|
+
case TOKEN.or:
|
|
24223
|
+
if (b === false) {
|
|
24224
|
+
return second;
|
|
24225
|
+
}
|
|
24226
|
+
if (b === true) {
|
|
24227
|
+
return first;
|
|
24228
|
+
}
|
|
24229
|
+
break;
|
|
24230
|
+
case TOKEN.min:
|
|
24231
|
+
if (second.type === PS_NODE.binary && second.op === TOKEN.max && second.first.type === PS_NODE.const && second.first.value >= b) {
|
|
24232
|
+
return first;
|
|
24233
|
+
}
|
|
24234
|
+
break;
|
|
24235
|
+
case TOKEN.max:
|
|
24236
|
+
if (second.type === PS_NODE.binary && second.op === TOKEN.min && second.first.type === PS_NODE.const && second.first.value <= b) {
|
|
24237
|
+
return first;
|
|
24238
|
+
}
|
|
24239
|
+
break;
|
|
24240
|
+
}
|
|
24241
|
+
}
|
|
24242
|
+
if (second.type === PS_NODE.const) {
|
|
24243
|
+
const a = second.value;
|
|
24244
|
+
switch (op) {
|
|
24245
|
+
case TOKEN.add:
|
|
24246
|
+
if (a === 0) {
|
|
24247
|
+
return first;
|
|
24248
|
+
}
|
|
24249
|
+
break;
|
|
24250
|
+
case TOKEN.sub:
|
|
24251
|
+
if (a === 0) {
|
|
24252
|
+
return this._makeUnary(TOKEN.neg, first);
|
|
24253
|
+
}
|
|
24254
|
+
break;
|
|
24255
|
+
case TOKEN.mul:
|
|
24256
|
+
if (a === 1) {
|
|
24257
|
+
return first;
|
|
24258
|
+
}
|
|
24259
|
+
if (a === 0) {
|
|
24260
|
+
return second;
|
|
24261
|
+
}
|
|
24262
|
+
if (a === -1) {
|
|
24263
|
+
return this._makeUnary(TOKEN.neg, first);
|
|
24264
|
+
}
|
|
24265
|
+
break;
|
|
24266
|
+
case TOKEN.and:
|
|
24267
|
+
if (a === true) {
|
|
24268
|
+
return first;
|
|
24269
|
+
}
|
|
24270
|
+
if (a === false) {
|
|
24271
|
+
return second;
|
|
24272
|
+
}
|
|
24273
|
+
break;
|
|
24274
|
+
case TOKEN.or:
|
|
24275
|
+
if (a === false) {
|
|
24276
|
+
return first;
|
|
24277
|
+
}
|
|
24278
|
+
if (a === true) {
|
|
24279
|
+
return second;
|
|
24280
|
+
}
|
|
24281
|
+
break;
|
|
24282
|
+
}
|
|
24283
|
+
}
|
|
24284
|
+
return new PsBinaryNode(
|
|
24285
|
+
op,
|
|
24286
|
+
first,
|
|
24287
|
+
second,
|
|
24288
|
+
_binaryValueType(op, first.valueType, second.valueType)
|
|
24289
|
+
);
|
|
24290
|
+
}
|
|
24291
|
+
/**
|
|
24292
|
+
* Create a unary tree node, applying optimizations eagerly:
|
|
24293
|
+
*
|
|
24294
|
+
* 1. Constant folding.
|
|
24295
|
+
* 2. not(comparison) → negated comparison: not(a eq b) → a ne b, etc.
|
|
24296
|
+
* 3. neg(a − b) → b − a.
|
|
24297
|
+
* 4. Double-negation: neg(neg(x)) → x, not(not(x)) → x.
|
|
24298
|
+
* 5. abs(neg(x)) → abs(x).
|
|
24299
|
+
* 6. Idempotent: f(f(x)) → f(x) for abs, ceiling, floor, round, etc.
|
|
24300
|
+
*/
|
|
24301
|
+
_makeUnary(op, operand) {
|
|
24302
|
+
if (operand.type === PS_NODE.const) {
|
|
24303
|
+
const v = _evalUnaryConst(op, operand.value);
|
|
24304
|
+
if (v !== void 0) {
|
|
24305
|
+
return new PsConstNode(v);
|
|
24306
|
+
}
|
|
24307
|
+
}
|
|
24308
|
+
if (op === TOKEN.not && operand.type === PS_NODE.binary) {
|
|
24309
|
+
const negated = _PSStackToTree.#negatedComparison.get(operand.op);
|
|
24310
|
+
if (negated !== void 0) {
|
|
24311
|
+
return new PsBinaryNode(
|
|
24312
|
+
negated,
|
|
24313
|
+
operand.first,
|
|
24314
|
+
operand.second,
|
|
24315
|
+
PS_VALUE_TYPE.boolean
|
|
24316
|
+
);
|
|
24317
|
+
}
|
|
24318
|
+
}
|
|
24319
|
+
if (op === TOKEN.neg && operand.type === PS_NODE.binary && operand.op === TOKEN.sub) {
|
|
24320
|
+
return this._makeBinary(TOKEN.sub, operand.second, operand.first);
|
|
24321
|
+
}
|
|
24322
|
+
if (operand.type === PS_NODE.unary) {
|
|
24323
|
+
if (op === TOKEN.neg && operand.op === TOKEN.neg || op === TOKEN.not && operand.op === TOKEN.not) {
|
|
24324
|
+
return operand.operand;
|
|
24325
|
+
}
|
|
24326
|
+
if (op === TOKEN.abs && operand.op === TOKEN.neg) {
|
|
24327
|
+
return this._makeUnary(TOKEN.abs, operand.operand);
|
|
24328
|
+
}
|
|
24329
|
+
if (_PSStackToTree.#idempotentUnary.has(op) && op === operand.op) {
|
|
24330
|
+
return operand;
|
|
24331
|
+
}
|
|
24332
|
+
}
|
|
24333
|
+
return new PsUnaryNode(op, operand, _unaryValueType(op, operand.valueType));
|
|
24334
|
+
}
|
|
24335
|
+
/**
|
|
24336
|
+
* Create a ternary node, applying optimizations eagerly:
|
|
24337
|
+
*
|
|
24338
|
+
* 1. Constant condition — fold to the live branch.
|
|
24339
|
+
* 2. Identical branches — the condition is irrelevant, return either branch.
|
|
24340
|
+
* 3. Boolean branch constants — `cond ? true : false` → cond,
|
|
24341
|
+
* `cond ? false : true` → not(cond).
|
|
24342
|
+
* 4. Ternary → branchless min/max when the condition compares two numeric
|
|
24343
|
+
* expressions that are also the two branches.
|
|
24344
|
+
*/
|
|
24345
|
+
_makeTernary(cond, then, otherwise) {
|
|
24346
|
+
if (cond.type === PS_NODE.const) {
|
|
24347
|
+
return cond.value ? then : otherwise;
|
|
24348
|
+
}
|
|
24349
|
+
if (_nodesEqual(then, otherwise)) {
|
|
24350
|
+
return then;
|
|
24351
|
+
}
|
|
24352
|
+
if (then.type === PS_NODE.const && otherwise.type === PS_NODE.const) {
|
|
24353
|
+
if (then.value === true && otherwise.value === false) {
|
|
24354
|
+
return cond;
|
|
24355
|
+
}
|
|
24356
|
+
if (then.value === false && otherwise.value === true) {
|
|
24357
|
+
return this._makeUnary(TOKEN.not, cond);
|
|
24358
|
+
}
|
|
24359
|
+
}
|
|
24360
|
+
if (cond.type === PS_NODE.binary) {
|
|
24361
|
+
const { op: cop, first: cf, second: cs } = cond;
|
|
24362
|
+
if (cop === TOKEN.gt || cop === TOKEN.ge) {
|
|
24363
|
+
if (_nodesEqual(then, cf) && _nodesEqual(otherwise, cs)) {
|
|
24364
|
+
return this._makeBinary(TOKEN.min, cf, cs);
|
|
24365
|
+
}
|
|
24366
|
+
if (_nodesEqual(then, cs) && _nodesEqual(otherwise, cf)) {
|
|
24367
|
+
return this._makeBinary(TOKEN.max, cf, cs);
|
|
24368
|
+
}
|
|
24369
|
+
} else if (cop === TOKEN.lt || cop === TOKEN.le) {
|
|
24370
|
+
if (_nodesEqual(then, cf) && _nodesEqual(otherwise, cs)) {
|
|
24371
|
+
return this._makeBinary(TOKEN.max, cf, cs);
|
|
24372
|
+
}
|
|
24373
|
+
if (_nodesEqual(then, cs) && _nodesEqual(otherwise, cf)) {
|
|
24374
|
+
return this._makeBinary(TOKEN.min, cf, cs);
|
|
24375
|
+
}
|
|
24376
|
+
}
|
|
24377
|
+
}
|
|
24378
|
+
return new PsTernaryNode(
|
|
24379
|
+
cond,
|
|
24380
|
+
then,
|
|
24381
|
+
otherwise,
|
|
24382
|
+
then.valueType === otherwise.valueType ? then.valueType : PS_VALUE_TYPE.unknown
|
|
24383
|
+
);
|
|
24384
|
+
}
|
|
24385
|
+
};
|
|
24386
|
+
var OP = {
|
|
24387
|
+
ARG: 0,
|
|
24388
|
+
// [ARG, idx]
|
|
24389
|
+
CONST: 1,
|
|
24390
|
+
// [CONST, val]
|
|
24391
|
+
STORE: 2,
|
|
24392
|
+
// [STORE, slot, min, max] clamp(pop()) → mem[slot]
|
|
24393
|
+
IF: 3,
|
|
24394
|
+
// [IF, target] jump when top-of-stack === 0
|
|
24395
|
+
JUMP: 4,
|
|
24396
|
+
// [JUMP, target] unconditional
|
|
24397
|
+
ABS: 5,
|
|
24398
|
+
NEG: 6,
|
|
24399
|
+
CEIL: 7,
|
|
24400
|
+
FLOOR: 8,
|
|
24401
|
+
ROUND: 9,
|
|
24402
|
+
// floor(x + 0.5)
|
|
24403
|
+
TRUNC: 10,
|
|
24404
|
+
NOT_B: 11,
|
|
24405
|
+
// boolean NOT
|
|
24406
|
+
NOT_N: 12,
|
|
24407
|
+
// bitwise NOT
|
|
24408
|
+
SQRT: 13,
|
|
24409
|
+
SIN: 14,
|
|
24410
|
+
// degrees in/out
|
|
24411
|
+
COS: 15,
|
|
24412
|
+
LN: 16,
|
|
24413
|
+
LOG10: 17,
|
|
24414
|
+
CVI: 18,
|
|
24415
|
+
SHIFT: 19,
|
|
24416
|
+
// [SHIFT, amount] +ve = left, −ve = right
|
|
24417
|
+
// Binary ops: second below, first on top; result = second OP first.
|
|
24418
|
+
ADD: 20,
|
|
24419
|
+
SUB: 21,
|
|
24420
|
+
MUL: 22,
|
|
24421
|
+
DIV: 23,
|
|
24422
|
+
// 0 when divisor is 0
|
|
24423
|
+
IDIV: 24,
|
|
24424
|
+
// 0 when divisor is 0
|
|
24425
|
+
MOD: 25,
|
|
24426
|
+
// 0 when divisor is 0
|
|
24427
|
+
POW: 26,
|
|
24428
|
+
EQ: 27,
|
|
24429
|
+
NE: 28,
|
|
24430
|
+
GT: 29,
|
|
24431
|
+
GE: 30,
|
|
24432
|
+
LT: 31,
|
|
24433
|
+
LE: 32,
|
|
24434
|
+
AND: 33,
|
|
24435
|
+
OR: 34,
|
|
24436
|
+
XOR: 35,
|
|
24437
|
+
ATAN: 36,
|
|
24438
|
+
// atan2(second, first) → degrees [0, 360)
|
|
24439
|
+
MIN: 37,
|
|
24440
|
+
MAX: 38,
|
|
24441
|
+
TEE_TMP: 39,
|
|
24442
|
+
// [TEE_TMP, slot] peek top of stack → tmp[slot], leave on stack
|
|
24443
|
+
LOAD_TMP: 40
|
|
24444
|
+
// [LOAD_TMP, slot] push tmp[slot]
|
|
24445
|
+
};
|
|
24446
|
+
var _DEG_TO_RAD = Math.PI / 180;
|
|
24447
|
+
var _RAD_TO_DEG = 180 / Math.PI;
|
|
24448
|
+
var PsJsCompiler = class {
|
|
24449
|
+
// Safe because JS is single-threaded.
|
|
24450
|
+
static #stack = new Float64Array(64);
|
|
24451
|
+
static #tmp = new Float64Array(64);
|
|
24452
|
+
constructor(domain, range) {
|
|
24453
|
+
this.nIn = domain.length >> 1;
|
|
24454
|
+
this.nOut = range.length >> 1;
|
|
24455
|
+
this.range = range;
|
|
24456
|
+
this.ir = [];
|
|
24457
|
+
this._tmpMap = /* @__PURE__ */ new Map();
|
|
24458
|
+
this._nextTmp = 0;
|
|
24459
|
+
}
|
|
24460
|
+
_compileNode(node) {
|
|
24461
|
+
if (node.shared) {
|
|
24462
|
+
const cached = this._tmpMap.get(node);
|
|
24463
|
+
if (cached !== void 0) {
|
|
24464
|
+
this.ir.push(OP.LOAD_TMP, cached);
|
|
24465
|
+
return true;
|
|
24466
|
+
}
|
|
24467
|
+
if (!this._compileNodeImpl(node)) {
|
|
24468
|
+
return false;
|
|
24469
|
+
}
|
|
24470
|
+
const slot = this._nextTmp++;
|
|
24471
|
+
this._tmpMap.set(node, slot);
|
|
24472
|
+
this.ir.push(OP.TEE_TMP, slot);
|
|
24473
|
+
return true;
|
|
24474
|
+
}
|
|
24475
|
+
return this._compileNodeImpl(node);
|
|
24476
|
+
}
|
|
24477
|
+
_compileNodeImpl(node) {
|
|
24478
|
+
switch (node.type) {
|
|
24479
|
+
case PS_NODE.arg:
|
|
24480
|
+
this.ir.push(OP.ARG, node.index);
|
|
24481
|
+
return true;
|
|
24482
|
+
case PS_NODE.const: {
|
|
24483
|
+
const v = node.value;
|
|
24484
|
+
this.ir.push(OP.CONST, typeof v === "boolean" ? Number(v) : v);
|
|
24485
|
+
return true;
|
|
24486
|
+
}
|
|
24487
|
+
case PS_NODE.unary:
|
|
24488
|
+
return this._compileUnary(node);
|
|
24489
|
+
case PS_NODE.binary:
|
|
24490
|
+
return this._compileBinary(node);
|
|
24491
|
+
case PS_NODE.ternary:
|
|
24492
|
+
return this._compileTernary(node);
|
|
24493
|
+
default:
|
|
24494
|
+
return false;
|
|
24495
|
+
}
|
|
24496
|
+
}
|
|
24497
|
+
_compileUnary(node) {
|
|
24498
|
+
const { op, operand, valueType } = node;
|
|
24499
|
+
if (op === TOKEN.cvr) {
|
|
24500
|
+
return this._compileNode(operand);
|
|
24501
|
+
}
|
|
24502
|
+
if (!this._compileNode(operand)) {
|
|
24503
|
+
return false;
|
|
24504
|
+
}
|
|
24505
|
+
switch (op) {
|
|
24506
|
+
case TOKEN.abs:
|
|
24507
|
+
this.ir.push(OP.ABS);
|
|
24508
|
+
break;
|
|
24509
|
+
case TOKEN.neg:
|
|
24510
|
+
this.ir.push(OP.NEG);
|
|
24511
|
+
break;
|
|
24512
|
+
case TOKEN.ceiling:
|
|
24513
|
+
this.ir.push(OP.CEIL);
|
|
24514
|
+
break;
|
|
24515
|
+
case TOKEN.floor:
|
|
24516
|
+
this.ir.push(OP.FLOOR);
|
|
24517
|
+
break;
|
|
24518
|
+
case TOKEN.round:
|
|
24519
|
+
this.ir.push(OP.ROUND);
|
|
24520
|
+
break;
|
|
24521
|
+
case TOKEN.truncate:
|
|
24522
|
+
this.ir.push(OP.TRUNC);
|
|
24523
|
+
break;
|
|
24524
|
+
case TOKEN.sqrt:
|
|
24525
|
+
this.ir.push(OP.SQRT);
|
|
24526
|
+
break;
|
|
24527
|
+
case TOKEN.sin:
|
|
24528
|
+
this.ir.push(OP.SIN);
|
|
24529
|
+
break;
|
|
24530
|
+
case TOKEN.cos:
|
|
24531
|
+
this.ir.push(OP.COS);
|
|
24532
|
+
break;
|
|
24533
|
+
case TOKEN.ln:
|
|
24534
|
+
this.ir.push(OP.LN);
|
|
24535
|
+
break;
|
|
24536
|
+
case TOKEN.log:
|
|
24537
|
+
this.ir.push(OP.LOG10);
|
|
24538
|
+
break;
|
|
24539
|
+
case TOKEN.cvi:
|
|
24540
|
+
this.ir.push(OP.CVI);
|
|
24541
|
+
break;
|
|
24542
|
+
case TOKEN.not:
|
|
24543
|
+
if (valueType === PS_VALUE_TYPE.boolean) {
|
|
24544
|
+
this.ir.push(OP.NOT_B);
|
|
24545
|
+
} else if (valueType === PS_VALUE_TYPE.numeric) {
|
|
24546
|
+
this.ir.push(OP.NOT_N);
|
|
24547
|
+
} else {
|
|
24548
|
+
return false;
|
|
24549
|
+
}
|
|
24550
|
+
break;
|
|
24551
|
+
default:
|
|
24552
|
+
return false;
|
|
24553
|
+
}
|
|
24554
|
+
return true;
|
|
24555
|
+
}
|
|
24556
|
+
_compileBinary(node) {
|
|
24557
|
+
const { op, first, second } = node;
|
|
24558
|
+
if (op === TOKEN.bitshift) {
|
|
24559
|
+
if (first.type !== PS_NODE.const || !Number.isInteger(first.value) || !this._compileNode(second)) {
|
|
24560
|
+
return false;
|
|
24561
|
+
}
|
|
24562
|
+
this.ir.push(OP.SHIFT, first.value);
|
|
24563
|
+
return true;
|
|
24564
|
+
}
|
|
24565
|
+
if (!this._compileNode(second) || !this._compileNode(first)) {
|
|
24566
|
+
return false;
|
|
24567
|
+
}
|
|
24568
|
+
switch (op) {
|
|
24569
|
+
case TOKEN.add:
|
|
24570
|
+
this.ir.push(OP.ADD);
|
|
24571
|
+
break;
|
|
24572
|
+
case TOKEN.sub:
|
|
24573
|
+
this.ir.push(OP.SUB);
|
|
24574
|
+
break;
|
|
24575
|
+
case TOKEN.mul:
|
|
24576
|
+
this.ir.push(OP.MUL);
|
|
24577
|
+
break;
|
|
24578
|
+
case TOKEN.div:
|
|
24579
|
+
this.ir.push(OP.DIV);
|
|
24580
|
+
break;
|
|
24581
|
+
case TOKEN.idiv:
|
|
24582
|
+
this.ir.push(OP.IDIV);
|
|
24583
|
+
break;
|
|
24584
|
+
case TOKEN.mod:
|
|
24585
|
+
this.ir.push(OP.MOD);
|
|
24586
|
+
break;
|
|
24587
|
+
case TOKEN.exp:
|
|
24588
|
+
this.ir.push(OP.POW);
|
|
24589
|
+
break;
|
|
24590
|
+
case TOKEN.eq:
|
|
24591
|
+
this.ir.push(OP.EQ);
|
|
24592
|
+
break;
|
|
24593
|
+
case TOKEN.ne:
|
|
24594
|
+
this.ir.push(OP.NE);
|
|
24595
|
+
break;
|
|
24596
|
+
case TOKEN.gt:
|
|
24597
|
+
this.ir.push(OP.GT);
|
|
24598
|
+
break;
|
|
24599
|
+
case TOKEN.ge:
|
|
24600
|
+
this.ir.push(OP.GE);
|
|
24601
|
+
break;
|
|
24602
|
+
case TOKEN.lt:
|
|
24603
|
+
this.ir.push(OP.LT);
|
|
24604
|
+
break;
|
|
24605
|
+
case TOKEN.le:
|
|
24606
|
+
this.ir.push(OP.LE);
|
|
24607
|
+
break;
|
|
24608
|
+
case TOKEN.and:
|
|
24609
|
+
this.ir.push(OP.AND);
|
|
24610
|
+
break;
|
|
24611
|
+
case TOKEN.or:
|
|
24612
|
+
this.ir.push(OP.OR);
|
|
24613
|
+
break;
|
|
24614
|
+
case TOKEN.xor:
|
|
24615
|
+
this.ir.push(OP.XOR);
|
|
24616
|
+
break;
|
|
24617
|
+
case TOKEN.atan:
|
|
24618
|
+
this.ir.push(OP.ATAN);
|
|
24619
|
+
break;
|
|
24620
|
+
case TOKEN.min:
|
|
24621
|
+
this.ir.push(OP.MIN);
|
|
24622
|
+
break;
|
|
24623
|
+
case TOKEN.max:
|
|
24624
|
+
this.ir.push(OP.MAX);
|
|
24625
|
+
break;
|
|
24626
|
+
default:
|
|
24627
|
+
return false;
|
|
24628
|
+
}
|
|
24629
|
+
return true;
|
|
24630
|
+
}
|
|
24631
|
+
_compileTernary(node) {
|
|
24632
|
+
if (!this._compileNode(node.cond)) {
|
|
24633
|
+
return false;
|
|
24634
|
+
}
|
|
24635
|
+
this.ir.push(OP.IF, 0);
|
|
24636
|
+
const ifPatch = this.ir.length - 1;
|
|
24637
|
+
if (!this._compileNode(node.then)) {
|
|
24638
|
+
return false;
|
|
24639
|
+
}
|
|
24640
|
+
this.ir.push(OP.JUMP, 0);
|
|
24641
|
+
const jumpPatch = this.ir.length - 1;
|
|
24642
|
+
this.ir[ifPatch] = this.ir.length;
|
|
24643
|
+
if (!this._compileNode(node.otherwise)) {
|
|
24644
|
+
return false;
|
|
24645
|
+
}
|
|
24646
|
+
this.ir[jumpPatch] = this.ir.length;
|
|
24647
|
+
return true;
|
|
24648
|
+
}
|
|
24649
|
+
compile(program) {
|
|
24650
|
+
const outputs = new PSStackToTree().evaluate(program, this.nIn);
|
|
24651
|
+
if (!outputs || outputs.length < this.nOut) {
|
|
24652
|
+
return null;
|
|
24653
|
+
}
|
|
24654
|
+
for (let i = 0; i < this.nOut; i++) {
|
|
24655
|
+
if (!this._compileNode(outputs[i])) {
|
|
24656
|
+
return null;
|
|
24657
|
+
}
|
|
24658
|
+
const min = this.range[i * 2];
|
|
24659
|
+
const max = this.range[i * 2 + 1];
|
|
24660
|
+
this.ir.push(OP.STORE, i, min, max);
|
|
24661
|
+
}
|
|
24662
|
+
return new Float64Array(this.ir);
|
|
24663
|
+
}
|
|
24664
|
+
static execute(ir, src, srcOffset, dest, destOffset) {
|
|
24665
|
+
let ip = 0, sp = 0;
|
|
24666
|
+
const n = ir.length;
|
|
24667
|
+
const stack = this.#stack;
|
|
24668
|
+
const tmp = this.#tmp;
|
|
24669
|
+
while (ip < n) {
|
|
24670
|
+
switch (ir[ip++] | 0) {
|
|
24671
|
+
case OP.ARG:
|
|
24672
|
+
stack[sp++] = src[srcOffset + (ir[ip++] | 0)];
|
|
24673
|
+
break;
|
|
24674
|
+
case OP.CONST:
|
|
24675
|
+
stack[sp++] = ir[ip++];
|
|
24676
|
+
break;
|
|
24677
|
+
case OP.STORE: {
|
|
24678
|
+
const slot = ir[ip++] | 0;
|
|
24679
|
+
const min = ir[ip++];
|
|
24680
|
+
const max = ir[ip++];
|
|
24681
|
+
dest[destOffset + slot] = MathClamp(stack[--sp], min, max);
|
|
24682
|
+
break;
|
|
24683
|
+
}
|
|
24684
|
+
case OP.IF: {
|
|
24685
|
+
const tgt = ir[ip++];
|
|
24686
|
+
if (stack[--sp] === 0) {
|
|
24687
|
+
ip = tgt;
|
|
24688
|
+
}
|
|
24689
|
+
break;
|
|
24690
|
+
}
|
|
24691
|
+
case OP.JUMP:
|
|
24692
|
+
ip = ir[ip];
|
|
24693
|
+
break;
|
|
24694
|
+
case OP.ABS:
|
|
24695
|
+
stack[sp - 1] = Math.abs(stack[sp - 1]);
|
|
24696
|
+
break;
|
|
24697
|
+
case OP.NEG:
|
|
24698
|
+
stack[sp - 1] = -stack[sp - 1];
|
|
24699
|
+
break;
|
|
24700
|
+
case OP.CEIL:
|
|
24701
|
+
stack[sp - 1] = Math.ceil(stack[sp - 1]);
|
|
24702
|
+
break;
|
|
24703
|
+
case OP.FLOOR:
|
|
24704
|
+
stack[sp - 1] = Math.floor(stack[sp - 1]);
|
|
24705
|
+
break;
|
|
24706
|
+
case OP.ROUND:
|
|
24707
|
+
stack[sp - 1] = Math.floor(stack[sp - 1] + 0.5);
|
|
24708
|
+
break;
|
|
24709
|
+
case OP.TRUNC:
|
|
24710
|
+
stack[sp - 1] = Math.trunc(stack[sp - 1]);
|
|
24711
|
+
break;
|
|
24712
|
+
case OP.NOT_B:
|
|
24713
|
+
stack[sp - 1] = stack[sp - 1] !== 0 ? 0 : 1;
|
|
24714
|
+
break;
|
|
24715
|
+
case OP.NOT_N:
|
|
24716
|
+
stack[sp - 1] = ~(stack[sp - 1] | 0);
|
|
24717
|
+
break;
|
|
24718
|
+
case OP.SQRT:
|
|
24719
|
+
stack[sp - 1] = Math.sqrt(stack[sp - 1]);
|
|
24720
|
+
break;
|
|
24721
|
+
case OP.SIN:
|
|
24722
|
+
stack[sp - 1] = Math.sin(stack[sp - 1] % 360 * _DEG_TO_RAD);
|
|
24723
|
+
break;
|
|
24724
|
+
case OP.COS:
|
|
24725
|
+
stack[sp - 1] = Math.cos(stack[sp - 1] % 360 * _DEG_TO_RAD);
|
|
24726
|
+
break;
|
|
24727
|
+
case OP.LN:
|
|
24728
|
+
stack[sp - 1] = Math.log(stack[sp - 1]);
|
|
24729
|
+
break;
|
|
24730
|
+
case OP.LOG10:
|
|
24731
|
+
stack[sp - 1] = Math.log10(stack[sp - 1]);
|
|
24732
|
+
break;
|
|
24733
|
+
case OP.CVI:
|
|
24734
|
+
stack[sp - 1] = Math.trunc(stack[sp - 1]) | 0;
|
|
24735
|
+
break;
|
|
24736
|
+
case OP.SHIFT: {
|
|
24737
|
+
const amt = ir[ip++];
|
|
24738
|
+
const v = stack[sp - 1] | 0;
|
|
24739
|
+
if (amt > 0) {
|
|
24740
|
+
stack[sp - 1] = v << amt;
|
|
24741
|
+
} else if (amt < 0) {
|
|
24742
|
+
stack[sp - 1] = v >> -amt;
|
|
24743
|
+
} else {
|
|
24744
|
+
stack[sp - 1] = v;
|
|
24745
|
+
}
|
|
24746
|
+
break;
|
|
24747
|
+
}
|
|
24748
|
+
case OP.ADD: {
|
|
24749
|
+
const b = stack[--sp];
|
|
24750
|
+
stack[sp - 1] += b;
|
|
24751
|
+
break;
|
|
24752
|
+
}
|
|
24753
|
+
case OP.SUB: {
|
|
24754
|
+
const b = stack[--sp];
|
|
24755
|
+
stack[sp - 1] -= b;
|
|
24756
|
+
break;
|
|
24757
|
+
}
|
|
24758
|
+
case OP.MUL: {
|
|
24759
|
+
const b = stack[--sp];
|
|
24760
|
+
stack[sp - 1] *= b;
|
|
24761
|
+
break;
|
|
24762
|
+
}
|
|
24763
|
+
case OP.DIV: {
|
|
24764
|
+
const b = stack[--sp];
|
|
24765
|
+
stack[sp - 1] = b !== 0 ? stack[sp - 1] / b : 0;
|
|
24766
|
+
break;
|
|
24767
|
+
}
|
|
24768
|
+
case OP.IDIV: {
|
|
24769
|
+
const b = stack[--sp];
|
|
24770
|
+
stack[sp - 1] = b !== 0 ? Math.trunc(stack[sp - 1] / b) : 0;
|
|
24771
|
+
break;
|
|
24772
|
+
}
|
|
24773
|
+
case OP.MOD: {
|
|
24774
|
+
const b = stack[--sp];
|
|
24775
|
+
stack[sp - 1] = b !== 0 ? stack[sp - 1] % b : 0;
|
|
24776
|
+
break;
|
|
24777
|
+
}
|
|
24778
|
+
case OP.POW: {
|
|
24779
|
+
const b = stack[--sp];
|
|
24780
|
+
stack[sp - 1] **= b;
|
|
24781
|
+
break;
|
|
24782
|
+
}
|
|
24783
|
+
case OP.EQ: {
|
|
24784
|
+
const b = stack[--sp];
|
|
24785
|
+
stack[sp - 1] = stack[sp - 1] === b ? 1 : 0;
|
|
24786
|
+
break;
|
|
24787
|
+
}
|
|
24788
|
+
case OP.NE: {
|
|
24789
|
+
const b = stack[--sp];
|
|
24790
|
+
stack[sp - 1] = stack[sp - 1] !== b ? 1 : 0;
|
|
24791
|
+
break;
|
|
24792
|
+
}
|
|
24793
|
+
case OP.GT: {
|
|
24794
|
+
const b = stack[--sp];
|
|
24795
|
+
stack[sp - 1] = stack[sp - 1] > b ? 1 : 0;
|
|
24796
|
+
break;
|
|
24797
|
+
}
|
|
24798
|
+
case OP.GE: {
|
|
24799
|
+
const b = stack[--sp];
|
|
24800
|
+
stack[sp - 1] = stack[sp - 1] >= b ? 1 : 0;
|
|
24801
|
+
break;
|
|
24802
|
+
}
|
|
24803
|
+
case OP.LT: {
|
|
24804
|
+
const b = stack[--sp];
|
|
24805
|
+
stack[sp - 1] = stack[sp - 1] < b ? 1 : 0;
|
|
24806
|
+
break;
|
|
24807
|
+
}
|
|
24808
|
+
case OP.LE: {
|
|
24809
|
+
const b = stack[--sp];
|
|
24810
|
+
stack[sp - 1] = stack[sp - 1] <= b ? 1 : 0;
|
|
24811
|
+
break;
|
|
24812
|
+
}
|
|
24813
|
+
case OP.AND: {
|
|
24814
|
+
const b = stack[--sp] | 0;
|
|
24815
|
+
stack[sp - 1] = (stack[sp - 1] | 0) & b;
|
|
24816
|
+
break;
|
|
24817
|
+
}
|
|
24818
|
+
case OP.OR: {
|
|
24819
|
+
const b = stack[--sp] | 0;
|
|
24820
|
+
stack[sp - 1] = stack[sp - 1] | 0 | b;
|
|
24821
|
+
break;
|
|
24822
|
+
}
|
|
24823
|
+
case OP.XOR: {
|
|
24824
|
+
const b = stack[--sp] | 0;
|
|
24825
|
+
stack[sp - 1] = (stack[sp - 1] | 0) ^ b;
|
|
24826
|
+
break;
|
|
24827
|
+
}
|
|
24828
|
+
case OP.ATAN: {
|
|
24829
|
+
const b = stack[--sp];
|
|
24830
|
+
const deg = Math.atan2(stack[sp - 1], b) * _RAD_TO_DEG;
|
|
24831
|
+
stack[sp - 1] = deg < 0 ? deg + 360 : deg;
|
|
24832
|
+
break;
|
|
24833
|
+
}
|
|
24834
|
+
case OP.MIN: {
|
|
24835
|
+
const b = stack[--sp];
|
|
24836
|
+
stack[sp - 1] = Math.min(stack[sp - 1], b);
|
|
24837
|
+
break;
|
|
24838
|
+
}
|
|
24839
|
+
case OP.MAX: {
|
|
24840
|
+
const b = stack[--sp];
|
|
24841
|
+
stack[sp - 1] = Math.max(stack[sp - 1], b);
|
|
24842
|
+
break;
|
|
24843
|
+
}
|
|
24844
|
+
case OP.TEE_TMP:
|
|
24845
|
+
tmp[ir[ip++] | 0] = stack[sp - 1];
|
|
24846
|
+
break;
|
|
24847
|
+
case OP.LOAD_TMP:
|
|
24848
|
+
stack[sp++] = tmp[ir[ip++] | 0];
|
|
24849
|
+
break;
|
|
24850
|
+
}
|
|
24851
|
+
}
|
|
24852
|
+
}
|
|
24853
|
+
};
|
|
24854
|
+
var PSStackBasedInterpreter = class {
|
|
24855
|
+
// Safe: JS is single-threaded.
|
|
24856
|
+
static #stack = new Float64Array(100);
|
|
24857
|
+
static #sp = 0;
|
|
24858
|
+
static #push(v) {
|
|
24859
|
+
if (this.#sp < this.#stack.length) {
|
|
24860
|
+
this.#stack[this.#sp++] = v;
|
|
24861
|
+
}
|
|
24862
|
+
}
|
|
24863
|
+
static #execOp(op) {
|
|
24864
|
+
const stack = this.#stack;
|
|
24865
|
+
switch (op) {
|
|
24866
|
+
case TOKEN.true:
|
|
24867
|
+
this.#push(1);
|
|
24868
|
+
break;
|
|
24869
|
+
case TOKEN.false:
|
|
24870
|
+
this.#push(0);
|
|
24871
|
+
break;
|
|
24872
|
+
case TOKEN.abs:
|
|
24873
|
+
stack[this.#sp - 1] = Math.abs(stack[this.#sp - 1]);
|
|
24874
|
+
break;
|
|
24875
|
+
case TOKEN.neg:
|
|
24876
|
+
stack[this.#sp - 1] = -stack[this.#sp - 1];
|
|
24877
|
+
break;
|
|
24878
|
+
case TOKEN.ceiling:
|
|
24879
|
+
stack[this.#sp - 1] = Math.ceil(stack[this.#sp - 1]);
|
|
24880
|
+
break;
|
|
24881
|
+
case TOKEN.floor:
|
|
24882
|
+
stack[this.#sp - 1] = Math.floor(stack[this.#sp - 1]);
|
|
24883
|
+
break;
|
|
24884
|
+
case TOKEN.round:
|
|
24885
|
+
stack[this.#sp - 1] = Math.floor(stack[this.#sp - 1] + 0.5);
|
|
24886
|
+
break;
|
|
24887
|
+
case TOKEN.truncate:
|
|
24888
|
+
stack[this.#sp - 1] = Math.trunc(stack[this.#sp - 1]);
|
|
24889
|
+
break;
|
|
24890
|
+
case TOKEN.sqrt:
|
|
24891
|
+
stack[this.#sp - 1] = Math.sqrt(stack[this.#sp - 1]);
|
|
24892
|
+
break;
|
|
24893
|
+
case TOKEN.sin:
|
|
24894
|
+
stack[this.#sp - 1] = Math.sin(
|
|
24895
|
+
stack[this.#sp - 1] % 360 * _DEG_TO_RAD
|
|
24896
|
+
);
|
|
24897
|
+
break;
|
|
24898
|
+
case TOKEN.cos:
|
|
24899
|
+
stack[this.#sp - 1] = Math.cos(
|
|
24900
|
+
stack[this.#sp - 1] % 360 * _DEG_TO_RAD
|
|
24901
|
+
);
|
|
24902
|
+
break;
|
|
24903
|
+
case TOKEN.ln:
|
|
24904
|
+
stack[this.#sp - 1] = Math.log(stack[this.#sp - 1]);
|
|
24905
|
+
break;
|
|
24906
|
+
case TOKEN.log:
|
|
24907
|
+
stack[this.#sp - 1] = Math.log10(stack[this.#sp - 1]);
|
|
24908
|
+
break;
|
|
24909
|
+
case TOKEN.cvi:
|
|
24910
|
+
stack[this.#sp - 1] = Math.trunc(stack[this.#sp - 1]) | 0;
|
|
24911
|
+
break;
|
|
24912
|
+
case TOKEN.cvr:
|
|
24913
|
+
break;
|
|
24914
|
+
// values are already f64
|
|
24915
|
+
case TOKEN.not: {
|
|
24916
|
+
const v = stack[this.#sp - 1];
|
|
24917
|
+
stack[this.#sp - 1] = v === 0 || v === 1 ? 1 - v : ~(v | 0);
|
|
24918
|
+
break;
|
|
24919
|
+
}
|
|
24920
|
+
case TOKEN.add: {
|
|
24921
|
+
const b = stack[--this.#sp];
|
|
24922
|
+
stack[this.#sp - 1] += b;
|
|
24923
|
+
break;
|
|
24924
|
+
}
|
|
24925
|
+
case TOKEN.sub: {
|
|
24926
|
+
const b = stack[--this.#sp];
|
|
24927
|
+
stack[this.#sp - 1] -= b;
|
|
24928
|
+
break;
|
|
24929
|
+
}
|
|
24930
|
+
case TOKEN.mul: {
|
|
24931
|
+
const b = stack[--this.#sp];
|
|
24932
|
+
stack[this.#sp - 1] *= b;
|
|
24933
|
+
break;
|
|
24934
|
+
}
|
|
24935
|
+
case TOKEN.div: {
|
|
24936
|
+
const b = stack[--this.#sp];
|
|
24937
|
+
stack[this.#sp - 1] = b !== 0 ? stack[this.#sp - 1] / b : 0;
|
|
24938
|
+
break;
|
|
24939
|
+
}
|
|
24940
|
+
case TOKEN.idiv: {
|
|
24941
|
+
const b = stack[--this.#sp];
|
|
24942
|
+
stack[this.#sp - 1] = b !== 0 ? Math.trunc(stack[this.#sp - 1] / b) : 0;
|
|
24943
|
+
break;
|
|
24944
|
+
}
|
|
24945
|
+
case TOKEN.mod: {
|
|
24946
|
+
const b = stack[--this.#sp];
|
|
24947
|
+
stack[this.#sp - 1] = b !== 0 ? stack[this.#sp - 1] % b : 0;
|
|
24948
|
+
break;
|
|
24949
|
+
}
|
|
24950
|
+
case TOKEN.exp: {
|
|
24951
|
+
const b = stack[--this.#sp];
|
|
24952
|
+
stack[this.#sp - 1] **= b;
|
|
24953
|
+
break;
|
|
24954
|
+
}
|
|
24955
|
+
case TOKEN.atan: {
|
|
24956
|
+
const dx = stack[--this.#sp];
|
|
24957
|
+
const deg = Math.atan2(stack[this.#sp - 1], dx) * _RAD_TO_DEG;
|
|
24958
|
+
stack[this.#sp - 1] = deg < 0 ? deg + 360 : deg;
|
|
24959
|
+
break;
|
|
24960
|
+
}
|
|
24961
|
+
case TOKEN.eq: {
|
|
24962
|
+
const b = stack[--this.#sp];
|
|
24963
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] === b ? 1 : 0;
|
|
24964
|
+
break;
|
|
24965
|
+
}
|
|
24966
|
+
case TOKEN.ne: {
|
|
24967
|
+
const b = stack[--this.#sp];
|
|
24968
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] !== b ? 1 : 0;
|
|
24969
|
+
break;
|
|
24970
|
+
}
|
|
24971
|
+
case TOKEN.gt: {
|
|
24972
|
+
const b = stack[--this.#sp];
|
|
24973
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] > b ? 1 : 0;
|
|
24974
|
+
break;
|
|
24975
|
+
}
|
|
24976
|
+
case TOKEN.ge: {
|
|
24977
|
+
const b = stack[--this.#sp];
|
|
24978
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] >= b ? 1 : 0;
|
|
24979
|
+
break;
|
|
24980
|
+
}
|
|
24981
|
+
case TOKEN.lt: {
|
|
24982
|
+
const b = stack[--this.#sp];
|
|
24983
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] < b ? 1 : 0;
|
|
24984
|
+
break;
|
|
24985
|
+
}
|
|
24986
|
+
case TOKEN.le: {
|
|
24987
|
+
const b = stack[--this.#sp];
|
|
24988
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] <= b ? 1 : 0;
|
|
24989
|
+
break;
|
|
24990
|
+
}
|
|
24991
|
+
case TOKEN.and: {
|
|
24992
|
+
const b = stack[--this.#sp] | 0;
|
|
24993
|
+
stack[this.#sp - 1] = (stack[this.#sp - 1] | 0) & b;
|
|
24994
|
+
break;
|
|
24995
|
+
}
|
|
24996
|
+
case TOKEN.or: {
|
|
24997
|
+
const b = stack[--this.#sp] | 0;
|
|
24998
|
+
stack[this.#sp - 1] = stack[this.#sp - 1] | 0 | b;
|
|
24999
|
+
break;
|
|
25000
|
+
}
|
|
25001
|
+
case TOKEN.xor: {
|
|
25002
|
+
const b = stack[--this.#sp] | 0;
|
|
25003
|
+
stack[this.#sp - 1] = (stack[this.#sp - 1] | 0) ^ b;
|
|
25004
|
+
break;
|
|
25005
|
+
}
|
|
25006
|
+
case TOKEN.bitshift: {
|
|
25007
|
+
const amt = stack[--this.#sp] | 0;
|
|
25008
|
+
const v = stack[this.#sp - 1] | 0;
|
|
25009
|
+
stack[this.#sp - 1] = amt > 0 ? v << amt : v >> -amt;
|
|
25010
|
+
break;
|
|
25011
|
+
}
|
|
25012
|
+
case TOKEN.min: {
|
|
25013
|
+
const b = stack[--this.#sp];
|
|
25014
|
+
stack[this.#sp - 1] = Math.min(stack[this.#sp - 1], b);
|
|
25015
|
+
break;
|
|
25016
|
+
}
|
|
25017
|
+
case TOKEN.max: {
|
|
25018
|
+
const b = stack[--this.#sp];
|
|
25019
|
+
stack[this.#sp - 1] = Math.max(stack[this.#sp - 1], b);
|
|
25020
|
+
break;
|
|
25021
|
+
}
|
|
25022
|
+
case TOKEN.dup:
|
|
25023
|
+
this.#push(stack[this.#sp - 1]);
|
|
25024
|
+
break;
|
|
25025
|
+
case TOKEN.exch: {
|
|
25026
|
+
const a = stack[--this.#sp];
|
|
25027
|
+
const b = stack[--this.#sp];
|
|
25028
|
+
this.#push(a);
|
|
25029
|
+
this.#push(b);
|
|
25030
|
+
break;
|
|
25031
|
+
}
|
|
25032
|
+
case TOKEN.pop:
|
|
25033
|
+
this.#sp--;
|
|
25034
|
+
break;
|
|
25035
|
+
case TOKEN.copy: {
|
|
25036
|
+
const n = Math.trunc(stack[--this.#sp]);
|
|
25037
|
+
const base = this.#sp - n;
|
|
25038
|
+
for (let k = 0; k < n; k++) {
|
|
25039
|
+
this.#push(stack[base + k]);
|
|
25040
|
+
}
|
|
25041
|
+
break;
|
|
25042
|
+
}
|
|
25043
|
+
case TOKEN.index: {
|
|
25044
|
+
const i = Math.trunc(stack[--this.#sp]);
|
|
25045
|
+
this.#push(stack[this.#sp - 1 - i]);
|
|
25046
|
+
break;
|
|
25047
|
+
}
|
|
25048
|
+
case TOKEN.roll: {
|
|
25049
|
+
const j = Math.trunc(stack[--this.#sp]);
|
|
25050
|
+
const n = Math.trunc(stack[--this.#sp]);
|
|
25051
|
+
if (n > 1 && j !== 0) {
|
|
25052
|
+
const mod = (j % n + n) % n;
|
|
25053
|
+
if (mod !== 0) {
|
|
25054
|
+
const base = this.#sp - n;
|
|
25055
|
+
const sub = stack.slice(base, this.#sp);
|
|
25056
|
+
for (let k = 0; k < n; k++) {
|
|
25057
|
+
stack[base + k] = sub[(k - mod + n) % n];
|
|
25058
|
+
}
|
|
25059
|
+
}
|
|
25060
|
+
}
|
|
25061
|
+
break;
|
|
25062
|
+
}
|
|
25063
|
+
}
|
|
25064
|
+
}
|
|
25065
|
+
static #execBlock(instructions) {
|
|
25066
|
+
for (const instr of instructions) {
|
|
25067
|
+
switch (instr.type) {
|
|
25068
|
+
case PS_NODE.number:
|
|
25069
|
+
this.#push(instr.value);
|
|
25070
|
+
break;
|
|
25071
|
+
case PS_NODE.operator:
|
|
25072
|
+
this.#execOp(instr.op);
|
|
25073
|
+
break;
|
|
25074
|
+
case PS_NODE.if:
|
|
25075
|
+
if (this.#stack[--this.#sp] !== 0) {
|
|
25076
|
+
this.#execBlock(instr.then.instructions);
|
|
25077
|
+
}
|
|
25078
|
+
break;
|
|
25079
|
+
case PS_NODE.ifelse:
|
|
25080
|
+
if (this.#stack[--this.#sp] !== 0) {
|
|
25081
|
+
this.#execBlock(instr.then.instructions);
|
|
25082
|
+
} else {
|
|
25083
|
+
this.#execBlock(instr.otherwise.instructions);
|
|
25084
|
+
}
|
|
25085
|
+
break;
|
|
25086
|
+
}
|
|
25087
|
+
}
|
|
25088
|
+
}
|
|
25089
|
+
/**
|
|
25090
|
+
* @param {import("./ast.js").PsProgram} program
|
|
25091
|
+
* @param {number[]} domain – flat [min0,max0, …]
|
|
25092
|
+
* @param {number[]} range – flat [min0,max0, …]
|
|
25093
|
+
* @returns {Function} – `(src, srcOffset, dest, destOffset) => void`
|
|
25094
|
+
*/
|
|
25095
|
+
static build(program, domain, range) {
|
|
25096
|
+
const nIn = domain.length >> 1;
|
|
25097
|
+
const nOut = range.length >> 1;
|
|
25098
|
+
const { instructions } = program.body;
|
|
25099
|
+
return (src, srcOffset, dest, destOffset) => {
|
|
25100
|
+
this.#sp = 0;
|
|
25101
|
+
for (let i = 0; i < nIn; i++) {
|
|
25102
|
+
this.#push(src[srcOffset + i]);
|
|
25103
|
+
}
|
|
25104
|
+
this.#execBlock(instructions);
|
|
25105
|
+
const base = this.#sp - nOut;
|
|
25106
|
+
for (let i = 0; i < nOut; i++) {
|
|
25107
|
+
const v = base + i >= 0 ? this.#stack[base + i] : 0;
|
|
25108
|
+
dest[destOffset + i] = MathClamp(v, range[i * 2], range[i * 2 + 1]);
|
|
25109
|
+
}
|
|
25110
|
+
};
|
|
25111
|
+
}
|
|
25112
|
+
};
|
|
25113
|
+
function buildPostScriptJsFunction(source, domain, range, forceInterpreter = false) {
|
|
25114
|
+
const program = parsePostScriptFunction(source);
|
|
25115
|
+
const ir = !forceInterpreter && new PsJsCompiler(domain, range).compile(program);
|
|
25116
|
+
if (ir) {
|
|
25117
|
+
return (src, srcOffset, dest, destOffset) => {
|
|
25118
|
+
PsJsCompiler.execute(ir, src, srcOffset, dest, destOffset);
|
|
25119
|
+
};
|
|
25120
|
+
}
|
|
25121
|
+
return PSStackBasedInterpreter.build(program, domain, range);
|
|
25122
|
+
}
|
|
22515
25123
|
var PdfError = class extends Error {
|
|
22516
25124
|
constructor(code, message) {
|
|
22517
25125
|
super(message);
|
|
@@ -33591,6 +36199,25 @@ var __webpack_exports__JpxImage = __webpack_exports__.JpxImage;
|
|
|
33591
36199
|
var __webpack_exports__VerbosityLevel = __webpack_exports__.VerbosityLevel;
|
|
33592
36200
|
var __webpack_exports__getVerbosityLevel = __webpack_exports__.getVerbosityLevel;
|
|
33593
36201
|
var __webpack_exports__setVerbosityLevel = __webpack_exports__.setVerbosityLevel;
|
|
36202
|
+
function createCalibratedColorSpace(doc, family, parameters) {
|
|
36203
|
+
const dict = doc.resolveDict(parameters);
|
|
36204
|
+
const array = (key) => {
|
|
36205
|
+
const value = dict ? doc.resolveArray(dictGet(dict, key)) : void 0;
|
|
36206
|
+
return value?.items.map((item) => {
|
|
36207
|
+
const number = doc.resolve(item);
|
|
36208
|
+
return number?.kind === "number" ? number.value : 0;
|
|
36209
|
+
});
|
|
36210
|
+
};
|
|
36211
|
+
const whitePoint = array("WhitePoint") ?? [0.95047, 1, 1.08883];
|
|
36212
|
+
const blackPoint = array("BlackPoint");
|
|
36213
|
+
if (family === "CalGray") {
|
|
36214
|
+
const gamma = dict ? doc.resolve(dictGet(dict, "Gamma")) : void 0;
|
|
36215
|
+
return new CalGrayCS(whitePoint, blackPoint, gamma?.kind === "number" ? gamma.value : void 0);
|
|
36216
|
+
}
|
|
36217
|
+
if (family === "CalRGB") return new CalRGBCS(whitePoint, blackPoint, array("Gamma"), array("Matrix"));
|
|
36218
|
+
return new LabCS(whitePoint, blackPoint, array("Range"));
|
|
36219
|
+
}
|
|
36220
|
+
var cmykColorSpace = new DeviceCmykCS();
|
|
33594
36221
|
function resolveColorSpaceInfo(doc, csNode, resourcesDict) {
|
|
33595
36222
|
if (!csNode) return { colorSpace: "rgb", colorSpaceLabel: "rgb", components: 3 };
|
|
33596
36223
|
const resolved = doc.resolve(csNode);
|
|
@@ -33631,7 +36258,7 @@ function resolveColorSpaceInfo(doc, csNode, resourcesDict) {
|
|
|
33631
36258
|
palette = lookupNode.bytes;
|
|
33632
36259
|
}
|
|
33633
36260
|
let resolvedBaseComp = baseInfo.components;
|
|
33634
|
-
if (palette && (baseInfo.
|
|
36261
|
+
if (palette && (baseInfo.isSeparation || baseInfo.isDeviceN || baseInfo.calibrated !== void 0)) {
|
|
33635
36262
|
const numEntries = Math.max(1, Math.min(hival + 1, Math.floor(palette.length / Math.max(1, baseInfo.components))));
|
|
33636
36263
|
const rgbaPal = decodeSamplesToRgba(palette, numEntries, 1, 8, baseInfo);
|
|
33637
36264
|
const rgbPal = new Uint8Array(numEntries * 3);
|
|
@@ -33661,74 +36288,21 @@ function resolveColorSpaceInfo(doc, csNode, resourcesDict) {
|
|
|
33661
36288
|
}
|
|
33662
36289
|
return { colorSpace: "rgb", colorSpaceLabel: "icc", components: 3 };
|
|
33663
36290
|
}
|
|
33664
|
-
if (kindName === "CalGray") {
|
|
33665
|
-
const
|
|
33666
|
-
const gammaNode = calDict ? doc.resolve(dictGet(calDict, "Gamma")) : void 0;
|
|
33667
|
-
const gamma = gammaNode?.kind === "number" && gammaNode.value > 0 ? gammaNode.value : 1;
|
|
33668
|
-
return { colorSpace: "gray", colorSpaceLabel: "cal-gray", components: 1, calGrayGamma: gamma };
|
|
33669
|
-
}
|
|
33670
|
-
if (kindName === "CalRGB") {
|
|
33671
|
-
const calDict = doc.resolveDict(resolved.items[1]);
|
|
33672
|
-
const gammaArr = calDict ? doc.resolveArray(dictGet(calDict, "Gamma")) : void 0;
|
|
33673
|
-
let calRgbGamma = [1, 1, 1];
|
|
33674
|
-
if (gammaArr && gammaArr.items.length >= 3) {
|
|
33675
|
-
const gNums = gammaArr.items.slice(0, 3).map((it) => {
|
|
33676
|
-
const r = doc.resolve(it);
|
|
33677
|
-
return r?.kind === "number" && r.value > 0 ? r.value : 1;
|
|
33678
|
-
});
|
|
33679
|
-
calRgbGamma = [gNums[0], gNums[1], gNums[2]];
|
|
33680
|
-
}
|
|
33681
|
-
const matArr = calDict ? doc.resolveArray(dictGet(calDict, "Matrix")) : void 0;
|
|
33682
|
-
let calRgbMatrix;
|
|
33683
|
-
if (matArr && matArr.items.length >= 9) {
|
|
33684
|
-
calRgbMatrix = matArr.items.slice(0, 9).map((it) => {
|
|
33685
|
-
const r = doc.resolve(it);
|
|
33686
|
-
return r?.kind === "number" ? r.value : 0;
|
|
33687
|
-
});
|
|
33688
|
-
}
|
|
36291
|
+
if (kindName === "CalGray" || kindName === "CalRGB" || kindName === "Lab") {
|
|
36292
|
+
const calibrated = createCalibratedColorSpace(doc, kindName, resolved.items[1]);
|
|
33689
36293
|
return {
|
|
33690
|
-
colorSpace: "rgb",
|
|
33691
|
-
colorSpaceLabel: "cal-rgb",
|
|
33692
|
-
components: 3,
|
|
33693
|
-
|
|
33694
|
-
calRgbMatrix
|
|
36294
|
+
colorSpace: kindName === "CalGray" ? "gray" : "rgb",
|
|
36295
|
+
colorSpaceLabel: kindName === "CalGray" ? "cal-gray" : kindName === "CalRGB" ? "cal-rgb" : "lab",
|
|
36296
|
+
components: kindName === "CalGray" ? 1 : 3,
|
|
36297
|
+
calibrated
|
|
33695
36298
|
};
|
|
33696
36299
|
}
|
|
33697
|
-
if (kindName === "Lab") {
|
|
33698
|
-
const labDict = doc.resolveDict(resolved.items[1]);
|
|
33699
|
-
const rangeArr = labDict ? doc.resolveArray(dictGet(labDict, "Range")) : void 0;
|
|
33700
|
-
let labRange = [-100, 100, -100, 100];
|
|
33701
|
-
if (rangeArr && rangeArr.items.length >= 4) {
|
|
33702
|
-
const nums = rangeArr.items.slice(0, 4).map((it) => {
|
|
33703
|
-
const r = doc.resolve(it);
|
|
33704
|
-
return r?.kind === "number" ? r.value : 0;
|
|
33705
|
-
});
|
|
33706
|
-
labRange = [nums[0], nums[1], nums[2], nums[3]];
|
|
33707
|
-
}
|
|
33708
|
-
return { colorSpace: "rgb", colorSpaceLabel: "lab", components: 3, isLab: true, labRange };
|
|
33709
|
-
}
|
|
33710
36300
|
if (kindName === "Separation" || kindName === "DeviceN") {
|
|
33711
36301
|
const isDevN = kindName === "DeviceN";
|
|
33712
36302
|
const namesArr = isDevN ? doc.resolveArray(resolved.items[1]) : void 0;
|
|
33713
36303
|
const devNComponents = isDevN && namesArr ? Math.max(1, namesArr.items.length) : 1;
|
|
33714
36304
|
const altInfo = resolveColorSpaceInfo(doc, resolved.items[2], resourcesDict);
|
|
33715
36305
|
const altSpace = altInfo.colorSpace === "cmyk" ? "cmyk" : altInfo.colorSpace === "gray" ? "gray" : "rgb";
|
|
33716
|
-
const fnResolved = doc.resolve(resolved.items[3]);
|
|
33717
|
-
const fnDict = fnResolved?.kind === "stream" ? fnResolved.dict : fnResolved?.kind === "dict" ? fnResolved : void 0;
|
|
33718
|
-
const ftNode = fnDict ? doc.resolve(dictGet(fnDict, "FunctionType")) : void 0;
|
|
33719
|
-
const tintFunctionType = ftNode?.kind === "number" ? ftNode.value : 2;
|
|
33720
|
-
const c0Arr = fnDict ? doc.resolveArray(dictGet(fnDict, "C0")) : void 0;
|
|
33721
|
-
const c1Arr = fnDict ? doc.resolveArray(dictGet(fnDict, "C1")) : void 0;
|
|
33722
|
-
const nNode = fnDict ? doc.resolve(dictGet(fnDict, "N")) : void 0;
|
|
33723
|
-
const c0 = c0Arr ? c0Arr.items.map((it) => {
|
|
33724
|
-
const r = doc.resolve(it);
|
|
33725
|
-
return r?.kind === "number" ? r.value : 0;
|
|
33726
|
-
}) : altSpace === "cmyk" ? [0, 0, 0, 0] : altSpace === "gray" ? [1] : [1, 1, 1];
|
|
33727
|
-
const c1 = c1Arr ? c1Arr.items.map((it) => {
|
|
33728
|
-
const r = doc.resolve(it);
|
|
33729
|
-
return r?.kind === "number" ? r.value : 0;
|
|
33730
|
-
}) : altSpace === "cmyk" ? [0, 0, 0, 1] : altSpace === "gray" ? [0] : [0, 0, 0];
|
|
33731
|
-
const sepN = nNode?.kind === "number" && nNode.value > 0 ? nNode.value : 1;
|
|
33732
36306
|
return {
|
|
33733
36307
|
colorSpace: altSpace,
|
|
33734
36308
|
colorSpaceLabel: isDevN ? "devn" : "sep",
|
|
@@ -33736,12 +36310,9 @@ function resolveColorSpaceInfo(doc, csNode, resourcesDict) {
|
|
|
33736
36310
|
isSeparation: !isDevN,
|
|
33737
36311
|
isDeviceN: isDevN,
|
|
33738
36312
|
separationAltSpace: altSpace,
|
|
33739
|
-
|
|
33740
|
-
separationC1: c1,
|
|
33741
|
-
separationN: sepN,
|
|
36313
|
+
alternateCalibrated: altInfo.calibrated,
|
|
33742
36314
|
tintFunctionDoc: doc,
|
|
33743
|
-
tintFunctionNode: resolved.items[3]
|
|
33744
|
-
tintFunctionType
|
|
36315
|
+
tintFunctionNode: resolved.items[3]
|
|
33745
36316
|
};
|
|
33746
36317
|
}
|
|
33747
36318
|
}
|
|
@@ -33891,141 +36462,55 @@ function decodeSamplesToRgba(rawSamples, width, height, bpc, csInfo, alphaSample
|
|
|
33891
36462
|
const rgba = new Uint8Array(width * height * 4);
|
|
33892
36463
|
const pixelCount = width * height;
|
|
33893
36464
|
if (csInfo.isSeparation || csInfo.isDeviceN) {
|
|
33894
|
-
const c0 = csInfo.separationC0 ?? [1, 1, 1];
|
|
33895
|
-
const c1 = csInfo.separationC1 ?? [0, 0, 0];
|
|
33896
|
-
const expN = csInfo.separationN ?? 1;
|
|
33897
36465
|
const alt = csInfo.separationAltSpace ?? "rgb";
|
|
33898
36466
|
const numCh = Math.max(1, csInfo.components);
|
|
33899
36467
|
const step = bpc === 16 ? 2 : 1;
|
|
33900
|
-
const useGenericTintFn = csInfo.tintFunctionDoc !== void 0 && csInfo.tintFunctionNode !== void 0 && (csInfo.tintFunctionType === 0 || csInfo.tintFunctionType === 3 || csInfo.tintFunctionType === 4);
|
|
33901
36468
|
for (let p = 0; p < pixelCount; p++) {
|
|
33902
36469
|
const chVals = [];
|
|
33903
|
-
let wSum = 0;
|
|
33904
36470
|
for (let ch3 = 0; ch3 < numCh; ch3++) {
|
|
33905
36471
|
const sRaw = (rawSamples[(p * numCh + ch3) * step] ?? 0) / 255;
|
|
33906
|
-
|
|
33907
|
-
chVals.push(tRemapped);
|
|
33908
|
-
wSum += Math.pow(Math.max(0, Math.min(1, tRemapped)), expN);
|
|
33909
|
-
}
|
|
33910
|
-
if (useGenericTintFn) {
|
|
33911
|
-
const outComps = evalShadingFunctionToComponents(
|
|
33912
|
-
csInfo.tintFunctionDoc,
|
|
33913
|
-
csInfo.tintFunctionNode,
|
|
33914
|
-
chVals
|
|
33915
|
-
);
|
|
33916
|
-
if (alt === "cmyk") {
|
|
33917
|
-
const c = outComps[0] ?? 0;
|
|
33918
|
-
const m = outComps[1] ?? 0;
|
|
33919
|
-
const y = outComps[2] ?? 0;
|
|
33920
|
-
const k = outComps[3] ?? 0;
|
|
33921
|
-
rgba[p * 4] = Math.round(Math.max(0, Math.min(1, (1 - c) * (1 - k))) * 255);
|
|
33922
|
-
rgba[p * 4 + 1] = Math.round(Math.max(0, Math.min(1, (1 - m) * (1 - k))) * 255);
|
|
33923
|
-
rgba[p * 4 + 2] = Math.round(Math.max(0, Math.min(1, (1 - y) * (1 - k))) * 255);
|
|
33924
|
-
} else if (alt === "gray") {
|
|
33925
|
-
const gByte = Math.round(Math.max(0, Math.min(1, outComps[0] ?? 0)) * 255);
|
|
33926
|
-
rgba[p * 4] = gByte;
|
|
33927
|
-
rgba[p * 4 + 1] = gByte;
|
|
33928
|
-
rgba[p * 4 + 2] = gByte;
|
|
33929
|
-
} else {
|
|
33930
|
-
rgba[p * 4] = Math.round(Math.max(0, Math.min(1, outComps[0] ?? 0)) * 255);
|
|
33931
|
-
rgba[p * 4 + 1] = Math.round(Math.max(0, Math.min(1, outComps[1] ?? 0)) * 255);
|
|
33932
|
-
rgba[p * 4 + 2] = Math.round(Math.max(0, Math.min(1, outComps[2] ?? 0)) * 255);
|
|
33933
|
-
}
|
|
33934
|
-
rgba[p * 4 + 3] = 255;
|
|
33935
|
-
continue;
|
|
36472
|
+
chVals.push(remapUnitSampleWithDecode(sRaw, ch3, decodePairs));
|
|
33936
36473
|
}
|
|
33937
|
-
const
|
|
33938
|
-
|
|
33939
|
-
|
|
33940
|
-
|
|
33941
|
-
|
|
33942
|
-
|
|
33943
|
-
rgba
|
|
33944
|
-
|
|
33945
|
-
rgba[
|
|
36474
|
+
const outComps = evalShadingFunctionToComponents(
|
|
36475
|
+
csInfo.tintFunctionDoc,
|
|
36476
|
+
csInfo.tintFunctionNode,
|
|
36477
|
+
chVals
|
|
36478
|
+
);
|
|
36479
|
+
if (csInfo.alternateCalibrated) {
|
|
36480
|
+
rgba.set(csInfo.alternateCalibrated.getRgb(outComps, 0), p * 4);
|
|
36481
|
+
} else if (alt === "cmyk") {
|
|
36482
|
+
rgba.set(cmykColorSpace.getRgb([outComps[0] ?? 0, outComps[1] ?? 0, outComps[2] ?? 0, outComps[3] ?? 0], 0), p * 4);
|
|
33946
36483
|
} else if (alt === "gray") {
|
|
33947
|
-
const
|
|
33948
|
-
const gByte = Math.round(Math.max(0, Math.min(1, gVal)) * 255);
|
|
36484
|
+
const gByte = Math.round(Math.max(0, Math.min(1, outComps[0] ?? 0)) * 255);
|
|
33949
36485
|
rgba[p * 4] = gByte;
|
|
33950
36486
|
rgba[p * 4 + 1] = gByte;
|
|
33951
36487
|
rgba[p * 4 + 2] = gByte;
|
|
33952
36488
|
} else {
|
|
33953
|
-
|
|
33954
|
-
|
|
33955
|
-
|
|
33956
|
-
rgba[p * 4] = Math.round(Math.max(0, Math.min(1, rVal)) * 255);
|
|
33957
|
-
rgba[p * 4 + 1] = Math.round(Math.max(0, Math.min(1, gVal)) * 255);
|
|
33958
|
-
rgba[p * 4 + 2] = Math.round(Math.max(0, Math.min(1, bVal)) * 255);
|
|
36489
|
+
rgba[p * 4] = Math.round(Math.max(0, Math.min(1, outComps[0] ?? 0)) * 255);
|
|
36490
|
+
rgba[p * 4 + 1] = Math.round(Math.max(0, Math.min(1, outComps[1] ?? 0)) * 255);
|
|
36491
|
+
rgba[p * 4 + 2] = Math.round(Math.max(0, Math.min(1, outComps[2] ?? 0)) * 255);
|
|
33959
36492
|
}
|
|
33960
36493
|
rgba[p * 4 + 3] = 255;
|
|
33961
36494
|
}
|
|
33962
|
-
} else if (csInfo.
|
|
33963
|
-
const
|
|
33964
|
-
const
|
|
33965
|
-
const toSrgb = (u) => {
|
|
33966
|
-
const c = u <= 31308e-7 ? 12.92 * u : 1.055 * Math.pow(u, 1 / 2.4) - 0.055;
|
|
33967
|
-
return Math.round(Math.max(0, Math.min(1, c)) * 255);
|
|
33968
|
-
};
|
|
36495
|
+
} else if (csInfo.calibrated?.name === "Lab") {
|
|
36496
|
+
const { amin, amax, bmin, bmax } = csInfo.calibrated;
|
|
36497
|
+
const step = bpc === 16 ? 2 : 1;
|
|
33969
36498
|
for (let p = 0; p < pixelCount; p++) {
|
|
33970
|
-
const
|
|
33971
|
-
const
|
|
33972
|
-
const
|
|
33973
|
-
|
|
33974
|
-
if (L <= 0.01) {
|
|
33975
|
-
rgba[p * 4] = 0;
|
|
33976
|
-
rgba[p * 4 + 1] = 0;
|
|
33977
|
-
rgba[p * 4 + 2] = 0;
|
|
33978
|
-
rgba[p * 4 + 3] = 255;
|
|
33979
|
-
continue;
|
|
33980
|
-
}
|
|
33981
|
-
const a = amin + s1 * (amax - amin);
|
|
33982
|
-
const b = bmin + s2 * (bmax - bmin);
|
|
33983
|
-
const fy = (L + 16) / 116;
|
|
33984
|
-
const fx = fy + a / 500;
|
|
33985
|
-
const fz = fy - b / 200;
|
|
33986
|
-
const X = 0.95047 * invF(fx);
|
|
33987
|
-
const Y = 1 * invF(fy);
|
|
33988
|
-
const Z = 1.08883 * invF(fz);
|
|
33989
|
-
const linR = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
|
|
33990
|
-
const linG = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
|
|
33991
|
-
const linB = X * 0.0557 + Y * -0.204 + Z * 1.057;
|
|
33992
|
-
rgba[p * 4] = toSrgb(linR);
|
|
33993
|
-
rgba[p * 4 + 1] = toSrgb(linG);
|
|
33994
|
-
rgba[p * 4 + 2] = toSrgb(linB);
|
|
36499
|
+
const L = (rawSamples[p * 3 * step] ?? 0) / 255 * 100;
|
|
36500
|
+
const a = amin + (rawSamples[(p * 3 + 1) * step] ?? 128) / 255 * (amax - amin);
|
|
36501
|
+
const b = bmin + (rawSamples[(p * 3 + 2) * step] ?? 128) / 255 * (bmax - bmin);
|
|
36502
|
+
rgba.set(csInfo.calibrated.getRgb([L, a, b], 0), p * 4);
|
|
33995
36503
|
rgba[p * 4 + 3] = 255;
|
|
33996
36504
|
}
|
|
33997
36505
|
} else if (csInfo.colorSpace === "rgb") {
|
|
33998
|
-
const hasCalRgb = Boolean(csInfo.calRgbGamma || csInfo.calRgbMatrix);
|
|
33999
|
-
const toSrgbByte = (u) => {
|
|
34000
|
-
const c = u <= 31308e-7 ? 12.92 * u : 1.055 * Math.pow(u, 1 / 2.4) - 0.055;
|
|
34001
|
-
return Math.round(Math.max(0, Math.min(1, c)) * 255);
|
|
34002
|
-
};
|
|
34003
36506
|
const writeRgbPixel = (p, rByte, gByte, bByte) => {
|
|
34004
|
-
|
|
34005
|
-
|
|
34006
|
-
|
|
34007
|
-
if (
|
|
34008
|
-
|
|
34009
|
-
|
|
34010
|
-
|
|
34011
|
-
const cg = Math.pow(bU, gb);
|
|
34012
|
-
if (csInfo.calRgbMatrix && csInfo.calRgbMatrix.length >= 9) {
|
|
34013
|
-
const m = csInfo.calRgbMatrix;
|
|
34014
|
-
const X = m[0] * ag + m[3] * bg + m[6] * cg;
|
|
34015
|
-
const Y = m[1] * ag + m[4] * bg + m[7] * cg;
|
|
34016
|
-
const Z = m[2] * ag + m[5] * bg + m[8] * cg;
|
|
34017
|
-
const linR = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
|
|
34018
|
-
const linG = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
|
|
34019
|
-
const linB = X * 0.0557 + Y * -0.204 + Z * 1.057;
|
|
34020
|
-
rgba[p * 4] = toSrgbByte(linR);
|
|
34021
|
-
rgba[p * 4 + 1] = toSrgbByte(linG);
|
|
34022
|
-
rgba[p * 4 + 2] = toSrgbByte(linB);
|
|
34023
|
-
rgba[p * 4 + 3] = 255;
|
|
34024
|
-
return;
|
|
34025
|
-
}
|
|
34026
|
-
rU = ag;
|
|
34027
|
-
gU = bg;
|
|
34028
|
-
bU = cg;
|
|
36507
|
+
const rU = remapUnitSampleWithDecode(rByte / 255, 0, decodePairs);
|
|
36508
|
+
const gU = remapUnitSampleWithDecode(gByte / 255, 1, decodePairs);
|
|
36509
|
+
const bU = remapUnitSampleWithDecode(bByte / 255, 2, decodePairs);
|
|
36510
|
+
if (csInfo.calibrated) {
|
|
36511
|
+
rgba.set(csInfo.calibrated.getRgb([rU, gU, bU], 0), p * 4);
|
|
36512
|
+
rgba[p * 4 + 3] = 255;
|
|
36513
|
+
return;
|
|
34029
36514
|
}
|
|
34030
36515
|
rgba[p * 4] = Math.round(rU * 255);
|
|
34031
36516
|
rgba[p * 4 + 1] = Math.round(gU * 255);
|
|
@@ -34045,10 +36530,11 @@ function decodeSamplesToRgba(rawSamples, width, height, bpc, csInfo, alphaSample
|
|
|
34045
36530
|
}
|
|
34046
36531
|
} else if (csInfo.colorSpace === "gray") {
|
|
34047
36532
|
const writeGrayPixel = (p, gByte) => {
|
|
34048
|
-
|
|
34049
|
-
if (csInfo.
|
|
34050
|
-
|
|
34051
|
-
|
|
36533
|
+
const gU = remapUnitSampleWithDecode(gByte / 255, 0, decodePairs);
|
|
36534
|
+
if (csInfo.calibrated) {
|
|
36535
|
+
rgba.set(csInfo.calibrated.getRgb([gU], 0), p * 4);
|
|
36536
|
+
rgba[p * 4 + 3] = 255;
|
|
36537
|
+
return;
|
|
34052
36538
|
}
|
|
34053
36539
|
const outG = Math.round(Math.max(0, Math.min(1, gU)) * 255);
|
|
34054
36540
|
rgba[p * 4] = outG;
|
|
@@ -34104,9 +36590,7 @@ function decodeSamplesToRgba(rawSamples, width, height, bpc, csInfo, alphaSample
|
|
|
34104
36590
|
const m = remapUnitSampleWithDecode((rawSamples[p * stride + chStep] ?? 0) / 255, 1, decodePairs);
|
|
34105
36591
|
const y = remapUnitSampleWithDecode((rawSamples[p * stride + chStep * 2] ?? 0) / 255, 2, decodePairs);
|
|
34106
36592
|
const k = remapUnitSampleWithDecode((rawSamples[p * stride + chStep * 3] ?? 0) / 255, 3, decodePairs);
|
|
34107
|
-
rgba
|
|
34108
|
-
rgba[p * 4 + 1] = Math.round((1 - m) * (1 - k) * 255);
|
|
34109
|
-
rgba[p * 4 + 2] = Math.round((1 - y) * (1 - k) * 255);
|
|
36593
|
+
rgba.set(cmykColorSpace.getRgb([c, m, y, k], 0), p * 4);
|
|
34110
36594
|
rgba[p * 4 + 3] = 255;
|
|
34111
36595
|
}
|
|
34112
36596
|
} else if (csInfo.colorSpace === "index") {
|
|
@@ -34144,9 +36628,7 @@ function decodeSamplesToRgba(rawSamples, width, height, bpc, csInfo, alphaSample
|
|
|
34144
36628
|
const m = pal[idx * 4 + 1] / 255;
|
|
34145
36629
|
const yel = pal[idx * 4 + 2] / 255;
|
|
34146
36630
|
const k = pal[idx * 4 + 3] / 255;
|
|
34147
|
-
rgba
|
|
34148
|
-
rgba[p * 4 + 1] = Math.round((1 - m) * (1 - k) * 255);
|
|
34149
|
-
rgba[p * 4 + 2] = Math.round((1 - yel) * (1 - k) * 255);
|
|
36631
|
+
rgba.set(cmykColorSpace.getRgb([c, m, yel, k], 0), p * 4);
|
|
34150
36632
|
} else {
|
|
34151
36633
|
rgba[p * 4] = pal[idx * baseComp];
|
|
34152
36634
|
rgba[p * 4 + 1] = pal[idx * baseComp + 1];
|
|
@@ -34401,6 +36883,7 @@ function decodeInlineImageNodeToRgba(doc, dict, rawData, activeRes, fillColor) {
|
|
|
34401
36883
|
});
|
|
34402
36884
|
return decodeXObjectImageToRgba(context, { kind: "stream", dict, rawBytes: rawData }, activeRes, fillColor);
|
|
34403
36885
|
}
|
|
36886
|
+
var cmykColorSpace2 = new DeviceCmykCS();
|
|
34404
36887
|
function multiplyMatrices(m1, m2) {
|
|
34405
36888
|
return [
|
|
34406
36889
|
m1[0] * m2[0] + m1[1] * m2[2],
|
|
@@ -34617,193 +37100,7 @@ function resolvePageFonts(doc, resourcesDict) {
|
|
|
34617
37100
|
}
|
|
34618
37101
|
return fonts;
|
|
34619
37102
|
}
|
|
34620
|
-
|
|
34621
|
-
const stack = [...initialStack];
|
|
34622
|
-
const runRange = (start, end) => {
|
|
34623
|
-
let i = start;
|
|
34624
|
-
while (i < end) {
|
|
34625
|
-
const tok = tokens[i];
|
|
34626
|
-
if (tok === "{") {
|
|
34627
|
-
let depth = 1;
|
|
34628
|
-
let j = i + 1;
|
|
34629
|
-
while (j < end && depth > 0) {
|
|
34630
|
-
if (tokens[j] === "{") depth++;
|
|
34631
|
-
else if (tokens[j] === "}") depth--;
|
|
34632
|
-
j++;
|
|
34633
|
-
}
|
|
34634
|
-
const blockStart = i + 1;
|
|
34635
|
-
const blockEnd = j - 1;
|
|
34636
|
-
if (j < end && tokens[j] === "{") {
|
|
34637
|
-
let depth2 = 1;
|
|
34638
|
-
let k = j + 1;
|
|
34639
|
-
while (k < end && depth2 > 0) {
|
|
34640
|
-
if (tokens[k] === "{") depth2++;
|
|
34641
|
-
else if (tokens[k] === "}") depth2--;
|
|
34642
|
-
k++;
|
|
34643
|
-
}
|
|
34644
|
-
if (k < end && tokens[k] === "ifelse") {
|
|
34645
|
-
const cond = stack.pop() ?? 0;
|
|
34646
|
-
if (cond !== 0) runRange(blockStart, blockEnd);
|
|
34647
|
-
else runRange(j + 1, k - 1);
|
|
34648
|
-
i = k + 1;
|
|
34649
|
-
continue;
|
|
34650
|
-
}
|
|
34651
|
-
}
|
|
34652
|
-
if (j < end && tokens[j] === "if") {
|
|
34653
|
-
const cond = stack.pop() ?? 0;
|
|
34654
|
-
if (cond !== 0) runRange(blockStart, blockEnd);
|
|
34655
|
-
i = j + 1;
|
|
34656
|
-
continue;
|
|
34657
|
-
}
|
|
34658
|
-
runRange(blockStart, blockEnd);
|
|
34659
|
-
i = j;
|
|
34660
|
-
continue;
|
|
34661
|
-
}
|
|
34662
|
-
if (tok === "}") {
|
|
34663
|
-
i++;
|
|
34664
|
-
continue;
|
|
34665
|
-
}
|
|
34666
|
-
const numVal2 = Number(tok);
|
|
34667
|
-
if (tok.length > 0 && !Number.isNaN(numVal2)) {
|
|
34668
|
-
stack.push(numVal2);
|
|
34669
|
-
} else if (tok === "true") {
|
|
34670
|
-
stack.push(1);
|
|
34671
|
-
} else if (tok === "false") {
|
|
34672
|
-
stack.push(0);
|
|
34673
|
-
} else if (tok === "add") {
|
|
34674
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34675
|
-
stack.push(a + b);
|
|
34676
|
-
} else if (tok === "sub") {
|
|
34677
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34678
|
-
stack.push(a - b);
|
|
34679
|
-
} else if (tok === "mul") {
|
|
34680
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34681
|
-
stack.push(a * b);
|
|
34682
|
-
} else if (tok === "div") {
|
|
34683
|
-
const b = stack.pop() ?? 1, a = stack.pop() ?? 0;
|
|
34684
|
-
stack.push(Math.abs(b) > 1e-12 ? a / b : 0);
|
|
34685
|
-
} else if (tok === "idiv") {
|
|
34686
|
-
const b = stack.pop() ?? 1, a = stack.pop() ?? 0;
|
|
34687
|
-
stack.push(Math.abs(b) > 1e-12 ? Math.trunc(a / b) : 0);
|
|
34688
|
-
} else if (tok === "mod") {
|
|
34689
|
-
const b = stack.pop() ?? 1, a = stack.pop() ?? 0;
|
|
34690
|
-
stack.push(Math.abs(b) > 1e-12 ? a % b : 0);
|
|
34691
|
-
} else if (tok === "neg") {
|
|
34692
|
-
stack.push(-(stack.pop() ?? 0));
|
|
34693
|
-
} else if (tok === "abs") {
|
|
34694
|
-
stack.push(Math.abs(stack.pop() ?? 0));
|
|
34695
|
-
} else if (tok === "ceiling") {
|
|
34696
|
-
stack.push(Math.ceil(stack.pop() ?? 0));
|
|
34697
|
-
} else if (tok === "floor") {
|
|
34698
|
-
stack.push(Math.floor(stack.pop() ?? 0));
|
|
34699
|
-
} else if (tok === "round") {
|
|
34700
|
-
stack.push(Math.round(stack.pop() ?? 0));
|
|
34701
|
-
} else if (tok === "truncate" || tok === "cvi") {
|
|
34702
|
-
stack.push(Math.trunc(stack.pop() ?? 0));
|
|
34703
|
-
} else if (tok === "sqrt") {
|
|
34704
|
-
stack.push(Math.sqrt(Math.max(0, stack.pop() ?? 0)));
|
|
34705
|
-
} else if (tok === "sin") {
|
|
34706
|
-
stack.push(Math.sin((stack.pop() ?? 0) * Math.PI / 180));
|
|
34707
|
-
} else if (tok === "cos") {
|
|
34708
|
-
stack.push(Math.cos((stack.pop() ?? 0) * Math.PI / 180));
|
|
34709
|
-
} else if (tok === "atan") {
|
|
34710
|
-
const dx = stack.pop() ?? 1, dy = stack.pop() ?? 0;
|
|
34711
|
-
let deg = Math.atan2(dy, dx) * 180 / Math.PI;
|
|
34712
|
-
if (deg < 0) deg += 360;
|
|
34713
|
-
stack.push(deg);
|
|
34714
|
-
} else if (tok === "exp") {
|
|
34715
|
-
const exp = stack.pop() ?? 1, base = stack.pop() ?? 0;
|
|
34716
|
-
stack.push(Math.pow(base, exp));
|
|
34717
|
-
} else if (tok === "ln") {
|
|
34718
|
-
stack.push(Math.log(Math.max(1e-12, stack.pop() ?? 1)));
|
|
34719
|
-
} else if (tok === "log") {
|
|
34720
|
-
stack.push(Math.log10(Math.max(1e-12, stack.pop() ?? 1)));
|
|
34721
|
-
} else if (tok === "dup") {
|
|
34722
|
-
if (stack.length > 0) stack.push(stack[stack.length - 1]);
|
|
34723
|
-
} else if (tok === "exch") {
|
|
34724
|
-
if (stack.length >= 2) {
|
|
34725
|
-
const b = stack.pop(), a = stack.pop();
|
|
34726
|
-
stack.push(b, a);
|
|
34727
|
-
}
|
|
34728
|
-
} else if (tok === "pop") {
|
|
34729
|
-
stack.pop();
|
|
34730
|
-
} else if (tok === "copy") {
|
|
34731
|
-
const n = Math.max(0, Math.trunc(stack.pop() ?? 0));
|
|
34732
|
-
const slice = stack.slice(Math.max(0, stack.length - n));
|
|
34733
|
-
stack.push(...slice);
|
|
34734
|
-
} else if (tok === "index") {
|
|
34735
|
-
const n = Math.max(0, Math.trunc(stack.pop() ?? 0));
|
|
34736
|
-
const idx = stack.length - 1 - n;
|
|
34737
|
-
if (idx >= 0 && idx < stack.length) stack.push(stack[idx]);
|
|
34738
|
-
} else if (tok === "roll") {
|
|
34739
|
-
const jShift = Math.trunc(stack.pop() ?? 0);
|
|
34740
|
-
const n = Math.max(0, Math.trunc(stack.pop() ?? 0));
|
|
34741
|
-
if (n > 0 && stack.length >= n) {
|
|
34742
|
-
const sub = stack.splice(stack.length - n, n);
|
|
34743
|
-
const modShift = (jShift % n + n) % n;
|
|
34744
|
-
const rolled = [...sub.slice(n - modShift), ...sub.slice(0, n - modShift)];
|
|
34745
|
-
stack.push(...rolled);
|
|
34746
|
-
}
|
|
34747
|
-
} else if (tok === "eq") {
|
|
34748
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34749
|
-
stack.push(Math.abs(a - b) < 1e-9 ? 1 : 0);
|
|
34750
|
-
} else if (tok === "ne") {
|
|
34751
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34752
|
-
stack.push(Math.abs(a - b) >= 1e-9 ? 1 : 0);
|
|
34753
|
-
} else if (tok === "gt") {
|
|
34754
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34755
|
-
stack.push(a > b ? 1 : 0);
|
|
34756
|
-
} else if (tok === "ge") {
|
|
34757
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34758
|
-
stack.push(a >= b ? 1 : 0);
|
|
34759
|
-
} else if (tok === "lt") {
|
|
34760
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34761
|
-
stack.push(a < b ? 1 : 0);
|
|
34762
|
-
} else if (tok === "le") {
|
|
34763
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34764
|
-
stack.push(a <= b ? 1 : 0);
|
|
34765
|
-
} else if (tok === "and") {
|
|
34766
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34767
|
-
stack.push(a !== 0 && b !== 0 ? 1 : 0);
|
|
34768
|
-
} else if (tok === "or") {
|
|
34769
|
-
const b = stack.pop() ?? 0, a = stack.pop() ?? 0;
|
|
34770
|
-
stack.push(a !== 0 || b !== 0 ? 1 : 0);
|
|
34771
|
-
} else if (tok === "not") {
|
|
34772
|
-
const a = stack.pop() ?? 0;
|
|
34773
|
-
stack.push(a === 0 ? 1 : 0);
|
|
34774
|
-
}
|
|
34775
|
-
i++;
|
|
34776
|
-
}
|
|
34777
|
-
};
|
|
34778
|
-
runRange(0, tokens.length);
|
|
34779
|
-
return stack;
|
|
34780
|
-
}
|
|
34781
|
-
function tokenizePostScriptCode(bytes) {
|
|
34782
|
-
const tokens = [];
|
|
34783
|
-
let cur = "";
|
|
34784
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
34785
|
-
const ch3 = String.fromCharCode(bytes[i]);
|
|
34786
|
-
if (ch3 === "{" || ch3 === "}") {
|
|
34787
|
-
if (cur.length > 0) {
|
|
34788
|
-
tokens.push(cur);
|
|
34789
|
-
cur = "";
|
|
34790
|
-
}
|
|
34791
|
-
tokens.push(ch3);
|
|
34792
|
-
} else if (ch3 === " " || ch3 === " " || ch3 === "\n" || ch3 === "\r") {
|
|
34793
|
-
if (cur.length > 0) {
|
|
34794
|
-
tokens.push(cur);
|
|
34795
|
-
cur = "";
|
|
34796
|
-
}
|
|
34797
|
-
} else {
|
|
34798
|
-
cur += ch3;
|
|
34799
|
-
}
|
|
34800
|
-
}
|
|
34801
|
-
if (cur.length > 0) tokens.push(cur);
|
|
34802
|
-
if (tokens[0] === "{" && tokens[tokens.length - 1] === "}") {
|
|
34803
|
-
return tokens.slice(1, -1);
|
|
34804
|
-
}
|
|
34805
|
-
return tokens;
|
|
34806
|
-
}
|
|
37103
|
+
var postScriptFunctions = /* @__PURE__ */ new WeakMap();
|
|
34807
37104
|
function evalShadingFunctionToComponents(doc, fnNode, inputs) {
|
|
34808
37105
|
const inArr = typeof inputs === "number" ? [inputs] : inputs;
|
|
34809
37106
|
const t = inArr[0] ?? 0;
|
|
@@ -34850,35 +37147,50 @@ function evalShadingFunctionToComponents(doc, fnNode, inputs) {
|
|
|
34850
37147
|
const decode = getNums("Decode", range);
|
|
34851
37148
|
const nOut = Math.max(1, Math.floor((decode.length || range.length || 6) / 2));
|
|
34852
37149
|
const mIn = Math.max(1, size.length);
|
|
34853
|
-
|
|
37150
|
+
const streamBytes = doc.decodeStream(resolved);
|
|
37151
|
+
let vertices = [{ index: 0, weight: 1 }];
|
|
34854
37152
|
let stride = 1;
|
|
34855
37153
|
for (let i = 0; i < mIn; i++) {
|
|
34856
37154
|
const d02 = dom[i * 2] ?? 0;
|
|
34857
37155
|
const d12 = dom[i * 2 + 1] ?? 1;
|
|
34858
|
-
const sMax = Math.max(
|
|
37156
|
+
const sMax = Math.max(0, (size[i] ?? 2) - 1);
|
|
34859
37157
|
const e0 = encode[i * 2] ?? 0;
|
|
34860
37158
|
const e1 = encode[i * 2 + 1] ?? sMax;
|
|
34861
37159
|
const x = Math.max(d02, Math.min(d12, inArr[i] ?? 0));
|
|
34862
|
-
const u =
|
|
34863
|
-
const
|
|
34864
|
-
|
|
37160
|
+
const u = d12 === d02 ? 0 : (x - d02) / (d12 - d02);
|
|
37161
|
+
const e = Math.max(0, Math.min(sMax, e0 + u * (e1 - e0)));
|
|
37162
|
+
const low = Math.floor(e), fraction = e - low;
|
|
37163
|
+
const next = [];
|
|
37164
|
+
for (const vertex of vertices) {
|
|
37165
|
+
const index = vertex.index + low * stride;
|
|
37166
|
+
if (index * nOut * bps < streamBytes.length * 8) next.push({ index, weight: vertex.weight * (1 - fraction) });
|
|
37167
|
+
if (fraction && (index + stride) * nOut * bps < streamBytes.length * 8) {
|
|
37168
|
+
next.push({ index: index + stride, weight: vertex.weight * fraction });
|
|
37169
|
+
}
|
|
37170
|
+
}
|
|
37171
|
+
vertices = next;
|
|
34865
37172
|
stride *= size[i] ?? 2;
|
|
34866
37173
|
}
|
|
34867
|
-
const
|
|
37174
|
+
const maxSample = 2 ** bps - 1;
|
|
37175
|
+
const readSample = (index) => {
|
|
37176
|
+
let bitOffset = index * bps, remaining = bps, value = 0;
|
|
37177
|
+
while (remaining > 0) {
|
|
37178
|
+
const bitInByte = bitOffset % 8;
|
|
37179
|
+
const take = Math.min(remaining, 8 - bitInByte);
|
|
37180
|
+
const byte = streamBytes[Math.floor(bitOffset / 8)] ?? 0;
|
|
37181
|
+
value = value * 2 ** take + (byte >> 8 - bitInByte - take & (1 << take) - 1);
|
|
37182
|
+
bitOffset += take;
|
|
37183
|
+
remaining -= take;
|
|
37184
|
+
}
|
|
37185
|
+
return value / maxSample;
|
|
37186
|
+
};
|
|
34868
37187
|
const out2 = [];
|
|
34869
|
-
const maxSample = bps === 16 ? 65535 : 255;
|
|
34870
37188
|
for (let j = 0; j < nOut; j++) {
|
|
34871
|
-
|
|
34872
|
-
|
|
34873
|
-
if (bps === 16) {
|
|
34874
|
-
rawVal = (streamBytes[sampleIdx * 2] ?? 0) << 8 | (streamBytes[sampleIdx * 2 + 1] ?? 0);
|
|
34875
|
-
} else {
|
|
34876
|
-
rawVal = streamBytes[sampleIdx] ?? 0;
|
|
34877
|
-
}
|
|
34878
|
-
const frac = rawVal / maxSample;
|
|
37189
|
+
let value = 0;
|
|
37190
|
+
for (const vertex of vertices) value += readSample(vertex.index * nOut + j) * vertex.weight;
|
|
34879
37191
|
const dec0 = decode[j * 2] ?? 0;
|
|
34880
37192
|
const dec1 = decode[j * 2 + 1] ?? 1;
|
|
34881
|
-
out2.push(dec0 +
|
|
37193
|
+
out2.push(dec0 + value * (dec1 - dec0));
|
|
34882
37194
|
}
|
|
34883
37195
|
return clampRange(out2);
|
|
34884
37196
|
}
|
|
@@ -34888,34 +37200,40 @@ function evalShadingFunctionToComponents(doc, fnNode, inputs) {
|
|
|
34888
37200
|
const d12 = dom[i * 2 + 1] ?? 1;
|
|
34889
37201
|
return Math.max(d02, Math.min(d12, v));
|
|
34890
37202
|
});
|
|
34891
|
-
const
|
|
34892
|
-
|
|
34893
|
-
|
|
37203
|
+
const source = bytesToString3(doc.decodeStream(resolved));
|
|
37204
|
+
let cached = postScriptFunctions.get(resolved);
|
|
37205
|
+
if (!cached || cached.source !== source || cached.domain.length !== dom.length || cached.domain.some((value, index) => value !== dom[index]) || cached.range.length !== range.length || cached.range.some((value, index) => value !== range[index])) {
|
|
37206
|
+
cached = { source, domain: dom, range, evaluate: buildPostScriptJsFunction(source, dom, range) };
|
|
37207
|
+
postScriptFunctions.set(resolved, cached);
|
|
37208
|
+
}
|
|
37209
|
+
const output = [];
|
|
37210
|
+
cached.evaluate(clampedInputs, 0, output, 0);
|
|
37211
|
+
return output;
|
|
34894
37212
|
}
|
|
34895
37213
|
if (ft === 3) {
|
|
34896
37214
|
const fnsArr = doc.resolveArray(dictGet(fnDict, "Functions"));
|
|
34897
37215
|
const bounds = getNums("Bounds", []);
|
|
34898
37216
|
const encode = getNums("Encode", []);
|
|
34899
37217
|
if (fnsArr && fnsArr.items.length > 0) {
|
|
37218
|
+
const input = Math.max(dom[0] ?? 0, Math.min(dom[1] ?? 1, t));
|
|
34900
37219
|
let segIdx = 0;
|
|
34901
|
-
while (segIdx < bounds.length &&
|
|
37220
|
+
while (segIdx < bounds.length && input >= bounds[segIdx]) segIdx++;
|
|
34902
37221
|
segIdx = Math.min(fnsArr.items.length - 1, segIdx);
|
|
34903
37222
|
const b0 = segIdx === 0 ? dom[0] ?? 0 : bounds[segIdx - 1];
|
|
34904
37223
|
const b1 = segIdx < bounds.length ? bounds[segIdx] : dom[1] ?? 1;
|
|
34905
37224
|
const e0 = encode[segIdx * 2] ?? 0;
|
|
34906
37225
|
const e1 = encode[segIdx * 2 + 1] ?? 1;
|
|
34907
|
-
const localT =
|
|
34908
|
-
return evalShadingFunctionToComponents(doc, fnsArr.items[segIdx],
|
|
37226
|
+
const localT = b1 !== b0 ? e0 + (input - b0) / (b1 - b0) * (e1 - e0) : e0;
|
|
37227
|
+
return clampRange(evalShadingFunctionToComponents(doc, fnsArr.items[segIdx], localT));
|
|
34909
37228
|
}
|
|
34910
37229
|
}
|
|
34911
|
-
const c0 = getNums("C0", [0
|
|
34912
|
-
const c1 = getNums("C1", [1
|
|
37230
|
+
const c0 = getNums("C0", [0]);
|
|
37231
|
+
const c1 = getNums("C1", [1]);
|
|
34913
37232
|
const nNode = doc.resolve(dictGet(fnDict, "N"));
|
|
34914
|
-
const expN = nNode?.kind === "number"
|
|
37233
|
+
const expN = nNode?.kind === "number" ? nNode.value : 1;
|
|
34915
37234
|
const d0 = dom[0] ?? 0;
|
|
34916
37235
|
const d1 = dom[1] ?? 1;
|
|
34917
|
-
const
|
|
34918
|
-
const w = Math.pow(Math.max(0, Math.min(1, normT)), expN);
|
|
37236
|
+
const w = Math.pow(Math.max(d0, Math.min(d1, t)), expN);
|
|
34919
37237
|
const len = Math.max(c0.length, c1.length);
|
|
34920
37238
|
const out = [];
|
|
34921
37239
|
for (let i = 0; i < len; i++) {
|
|
@@ -34931,30 +37249,14 @@ function shadingComponentsToRgb(csName, comps) {
|
|
|
34931
37249
|
return [v, v, v];
|
|
34932
37250
|
}
|
|
34933
37251
|
if (csName === "DeviceCMYK" || csName === "CMYK" || comps.length >= 4) {
|
|
34934
|
-
const
|
|
34935
|
-
return [
|
|
37252
|
+
const rgb2 = cmykColorSpace2.getRgb([comps[0] ?? 0, comps[1] ?? 0, comps[2] ?? 0, comps[3] ?? 0], 0);
|
|
37253
|
+
return [rgb2[0] / 255, rgb2[1] / 255, rgb2[2] / 255];
|
|
34936
37254
|
}
|
|
34937
37255
|
return [comps[0] ?? 0, comps[1] ?? 0, comps[2] ?? 0];
|
|
34938
37256
|
}
|
|
34939
37257
|
function kClamp(v) {
|
|
34940
37258
|
return Math.max(0, Math.min(1, v));
|
|
34941
37259
|
}
|
|
34942
|
-
function xyzToSrgb(X, Y, Z, Xw = 0.95047, Yw = 1, Zw = 1.08883) {
|
|
34943
|
-
const scaleX = Xw > 1e-6 ? 0.95047 / Xw : 1;
|
|
34944
|
-
const scaleY = Yw > 1e-6 ? 1 / Yw : 1;
|
|
34945
|
-
const scaleZ = Zw > 1e-6 ? 1.08883 / Zw : 1;
|
|
34946
|
-
const x = X * scaleX;
|
|
34947
|
-
const y = Y * scaleY;
|
|
34948
|
-
const z = Z * scaleZ;
|
|
34949
|
-
const rl = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z;
|
|
34950
|
-
const gl = -0.969266 * x + 1.8760108 * y + 0.041556 * z;
|
|
34951
|
-
const bl = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z;
|
|
34952
|
-
const toGamma = (u) => {
|
|
34953
|
-
const c = Math.max(0, Math.min(1, u));
|
|
34954
|
-
return c <= 31308e-7 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
|
|
34955
|
-
};
|
|
34956
|
-
return [toGamma(rl), toGamma(gl), toGamma(bl)];
|
|
34957
|
-
}
|
|
34958
37260
|
function getColorSpaceComponentCount(doc, csNode, activeResources, depth = 0) {
|
|
34959
37261
|
if (!csNode || depth > 4) return 3;
|
|
34960
37262
|
const resolved = doc ? doc.resolve(csNode) : csNode;
|
|
@@ -35011,63 +37313,10 @@ function convertColorSpaceComponentsToRgb(doc, csNode, csNameFallback, comps, ac
|
|
|
35011
37313
|
if (resolved.kind === "array" && resolved.items.length > 0 && doc) {
|
|
35012
37314
|
const familyNode = doc.resolve(resolved.items[0]);
|
|
35013
37315
|
const family = familyNode?.kind === "name" ? familyNode.decoded : "";
|
|
35014
|
-
if (family === "CalGray") {
|
|
35015
|
-
const
|
|
35016
|
-
const
|
|
35017
|
-
|
|
35018
|
-
const a = Math.pow(kClamp(comps[0] ?? 0), gamma);
|
|
35019
|
-
return [a, a, a];
|
|
35020
|
-
}
|
|
35021
|
-
if (family === "CalRGB") {
|
|
35022
|
-
const paramDict = resolved.items[1] ? doc.resolveDict(resolved.items[1]) : void 0;
|
|
35023
|
-
const gammaArr = paramDict ? doc.resolveArray(dictGet(paramDict, "Gamma")) : void 0;
|
|
35024
|
-
const getArrNum = (arr, idx, fb) => {
|
|
35025
|
-
const r = arr && arr.items[idx] ? doc.resolve(arr.items[idx]) : void 0;
|
|
35026
|
-
return r?.kind === "number" ? r.value : fb;
|
|
35027
|
-
};
|
|
35028
|
-
const gr = getArrNum(gammaArr, 0, 1);
|
|
35029
|
-
const gg = getArrNum(gammaArr, 1, 1);
|
|
35030
|
-
const gb = getArrNum(gammaArr, 2, 1);
|
|
35031
|
-
const ag = Math.pow(kClamp(comps[0] ?? 0), gr > 0 ? gr : 1);
|
|
35032
|
-
const bg = Math.pow(kClamp(comps[1] ?? 0), gg > 0 ? gg : 1);
|
|
35033
|
-
const cg = Math.pow(kClamp(comps[2] ?? 0), gb > 0 ? gb : 1);
|
|
35034
|
-
const matArr = paramDict ? doc.resolveArray(dictGet(paramDict, "Matrix")) : void 0;
|
|
35035
|
-
if (matArr && matArr.items.length >= 9) {
|
|
35036
|
-
const m = (i) => getArrNum(matArr, i, 0);
|
|
35037
|
-
const X = m(0) * ag + m(3) * bg + m(6) * cg;
|
|
35038
|
-
const Y = m(1) * ag + m(4) * bg + m(7) * cg;
|
|
35039
|
-
const Z = m(2) * ag + m(5) * bg + m(8) * cg;
|
|
35040
|
-
const wpArr = paramDict ? doc.resolveArray(dictGet(paramDict, "WhitePoint")) : void 0;
|
|
35041
|
-
const Xw = getArrNum(wpArr, 0, 0.95047);
|
|
35042
|
-
const Yw = getArrNum(wpArr, 1, 1);
|
|
35043
|
-
const Zw = getArrNum(wpArr, 2, 1.08883);
|
|
35044
|
-
return xyzToSrgb(X, Y, Z, Xw, Yw, Zw);
|
|
35045
|
-
}
|
|
35046
|
-
return [ag, bg, cg];
|
|
35047
|
-
}
|
|
35048
|
-
if (family === "Lab") {
|
|
35049
|
-
const paramDict = resolved.items[1] ? doc.resolveDict(resolved.items[1]) : void 0;
|
|
35050
|
-
const wpArr = paramDict ? doc.resolveArray(dictGet(paramDict, "WhitePoint")) : void 0;
|
|
35051
|
-
const rangeArr = paramDict ? doc.resolveArray(dictGet(paramDict, "Range")) : void 0;
|
|
35052
|
-
const getNum = (arr, idx, fb) => {
|
|
35053
|
-
const r = arr && arr.items[idx] ? doc.resolve(arr.items[idx]) : void 0;
|
|
35054
|
-
return r?.kind === "number" ? r.value : fb;
|
|
35055
|
-
};
|
|
35056
|
-
const Xw = getNum(wpArr, 0, 0.95047);
|
|
35057
|
-
const Yw = getNum(wpArr, 1, 1);
|
|
35058
|
-
const Zw = getNum(wpArr, 2, 1.08883);
|
|
35059
|
-
const amin = getNum(rangeArr, 0, -100);
|
|
35060
|
-
const amax = getNum(rangeArr, 1, 100);
|
|
35061
|
-
const bmin = getNum(rangeArr, 2, -100);
|
|
35062
|
-
const bmax = getNum(rangeArr, 3, 100);
|
|
35063
|
-
const Lstar = Math.max(0, Math.min(100, comps[0] ?? 0));
|
|
35064
|
-
const astar = Math.max(amin, Math.min(amax, comps[1] ?? 0));
|
|
35065
|
-
const bstar = Math.max(bmin, Math.min(bmax, comps[2] ?? 0));
|
|
35066
|
-
const M = (Lstar + 16) / 116;
|
|
35067
|
-
const L = M + astar / 500;
|
|
35068
|
-
const N = M - bstar / 200;
|
|
35069
|
-
const gFn = (x) => x >= 6 / 29 ? x * x * x : 108 / 841 * (x - 4 / 29);
|
|
35070
|
-
return xyzToSrgb(Xw * gFn(L), Yw * gFn(M), Zw * gFn(N), Xw, Yw, Zw);
|
|
37316
|
+
if (family === "CalGray" || family === "CalRGB" || family === "Lab") {
|
|
37317
|
+
const colorSpace = createCalibratedColorSpace(doc, family, resolved.items[1]);
|
|
37318
|
+
const rgb2 = colorSpace.getRgb(comps, 0);
|
|
37319
|
+
return [rgb2[0] / 255, rgb2[1] / 255, rgb2[2] / 255];
|
|
35071
37320
|
}
|
|
35072
37321
|
if (family === "ICCBased" && resolved.items[1]) {
|
|
35073
37322
|
const iccNode = doc.resolve(resolved.items[1]);
|
|
@@ -35718,7 +37967,8 @@ function evaluateContentStreamToDisplayList(params) {
|
|
|
35718
37967
|
const shDict = shNode?.kind === "dict" ? shNode : shNode?.kind === "stream" ? shNode.dict : void 0;
|
|
35719
37968
|
const shStream = shNode?.kind === "stream" ? shNode : void 0;
|
|
35720
37969
|
if (shDict) {
|
|
35721
|
-
const
|
|
37970
|
+
const [originX, originY] = params.origin ?? [0, 0];
|
|
37971
|
+
const targetBox = st.clipRect ?? [originX, originY, originX + params.width, originY + params.height];
|
|
35722
37972
|
const img = renderShadingDictToImage(
|
|
35723
37973
|
params.cosDoc,
|
|
35724
37974
|
shDict,
|
|
@@ -35757,12 +38007,10 @@ function evaluateContentStreamToDisplayList(params) {
|
|
|
35757
38007
|
} else if (operator === "RG" || operator === "SC" || operator === "SCN") {
|
|
35758
38008
|
st.strokeColor = resolveScColorOperands(operator === "RG" ? "DeviceRGB" : st.strokeColorSpaceName, ops, activeResources);
|
|
35759
38009
|
} else if (operator === "k") {
|
|
35760
|
-
|
|
35761
|
-
st.fillColor = { r: (1 - c) * (1 - k), g: (1 - m) * (1 - k), b: (1 - y) * (1 - k) };
|
|
38010
|
+
st.fillColor = resolveScColorOperands("DeviceCMYK", ops, activeResources);
|
|
35762
38011
|
st.fillPatternName = void 0;
|
|
35763
38012
|
} else if (operator === "K") {
|
|
35764
|
-
|
|
35765
|
-
st.strokeColor = { r: (1 - c) * (1 - k), g: (1 - m) * (1 - k), b: (1 - y) * (1 - k) };
|
|
38013
|
+
st.strokeColor = resolveScColorOperands("DeviceCMYK", ops, activeResources);
|
|
35766
38014
|
} else if (operator === "gs" && params.cosDoc && activeResources && ops[0]?.kind === "name") {
|
|
35767
38015
|
const extDict = params.cosDoc.resolveDict(dictGet(activeResources, "ExtGState"));
|
|
35768
38016
|
const gsDict = extDict ? params.cosDoc.resolveDict(dictGet(extDict, ops[0].decoded)) : void 0;
|
|
@@ -35971,7 +38219,7 @@ function evaluateContentStreamToDisplayList(params) {
|
|
|
35971
38219
|
stateStack.pop();
|
|
35972
38220
|
capturedOperations = parentOperations;
|
|
35973
38221
|
}
|
|
35974
|
-
if (compositeGroup) emit({ kind: "group", value: { operations: children, alpha: st.fillAlpha, blendMode: st.blendMode, clipRect: st.clipRect } });
|
|
38222
|
+
if (compositeGroup) emit({ kind: "group", value: { operations: children, alpha: st.fillAlpha, isolated, bboxClip: nextClipPaths?.[0], blendMode: st.blendMode, clipRect: st.clipRect } });
|
|
35975
38223
|
};
|
|
35976
38224
|
const walkNodes = (nodes, mcid, actualText, activeResources = params.resourcesDict, activeFonts = fonts, depth = 0) => {
|
|
35977
38225
|
for (const node of nodes) {
|
|
@@ -36555,6 +38803,7 @@ function evaluateContentStreamToDisplayList(params) {
|
|
|
36555
38803
|
pageIndex: params.pageIndex,
|
|
36556
38804
|
width: params.width,
|
|
36557
38805
|
height: params.height,
|
|
38806
|
+
...params.origin ? { origin: params.origin } : {},
|
|
36558
38807
|
rotation: params.rotation ?? 0,
|
|
36559
38808
|
glyphs,
|
|
36560
38809
|
paths,
|
|
@@ -38135,7 +40384,7 @@ var PdfPage = class {
|
|
|
38135
40384
|
return [n0.value, n1.value, n2.value, n3.value];
|
|
38136
40385
|
}
|
|
38137
40386
|
}
|
|
38138
|
-
return [0, 0, 612, 792];
|
|
40387
|
+
return key === "MediaBox" ? [0, 0, 612, 792] : this.resolveBox("MediaBox");
|
|
38139
40388
|
}
|
|
38140
40389
|
getMediaBox() {
|
|
38141
40390
|
return this.resolveBox("MediaBox");
|
|
@@ -38808,16 +41057,19 @@ var PdfPage = class {
|
|
|
38808
41057
|
});
|
|
38809
41058
|
}
|
|
38810
41059
|
}
|
|
38811
|
-
|
|
41060
|
+
const mediaBox = this.getMediaBox(), cropBox = this.getCropBox();
|
|
41061
|
+
const originX = Math.min(mediaBox[0], mediaBox[2]), originY = Math.min(mediaBox[1], mediaBox[3]);
|
|
41062
|
+
return { ...evaluateContentStreamToDisplayList({
|
|
38812
41063
|
pageIndex: this.index,
|
|
38813
41064
|
width,
|
|
38814
41065
|
height,
|
|
41066
|
+
origin: [originX, originY],
|
|
38815
41067
|
rotation: this.getRotation(),
|
|
38816
41068
|
nodes: baseNodes,
|
|
38817
41069
|
cosDoc: this.cosDoc,
|
|
38818
41070
|
resourcesDict: evalResourcesDict,
|
|
38819
41071
|
annotations: extractPageAnnotations(this.cosDoc, this.pageDict)
|
|
38820
|
-
});
|
|
41072
|
+
}), cropBox: [cropBox[0] - originX, cropBox[1] - originY, cropBox[2] - originX, cropBox[3] - originY] };
|
|
38821
41073
|
}
|
|
38822
41074
|
extractPage(options) {
|
|
38823
41075
|
const dl = this.evaluateDisplayList();
|
|
@@ -38835,10 +41087,10 @@ var PdfPage = class {
|
|
|
38835
41087
|
return extractTablesFromDisplayList(this.evaluateDisplayList());
|
|
38836
41088
|
}
|
|
38837
41089
|
renderToBitmap(options) {
|
|
38838
|
-
return renderDisplayListToBitmap(this.evaluateDisplayList(), options);
|
|
41090
|
+
return renderDisplayListToBitmap(this.evaluateDisplayList({ hideAnnotations: options?.hideAnnotations }), options);
|
|
38839
41091
|
}
|
|
38840
41092
|
renderToPng(options) {
|
|
38841
|
-
return renderDisplayListToPng(this.evaluateDisplayList(), options);
|
|
41093
|
+
return renderDisplayListToPng(this.evaluateDisplayList({ hideAnnotations: options?.hideAnnotations }), options);
|
|
38842
41094
|
}
|
|
38843
41095
|
};
|
|
38844
41096
|
function createStandardFontHandle(standardName) {
|
|
@@ -39888,7 +42140,7 @@ function prepareStroke(path, scale) {
|
|
|
39888
42140
|
}
|
|
39889
42141
|
return { matrix, width, dashArray, dashPhase };
|
|
39890
42142
|
}
|
|
39891
|
-
function strokeContours(path, pageHeight, scale) {
|
|
42143
|
+
function strokeContours(path, pageHeight, scale, originX = 0, originY = 0) {
|
|
39892
42144
|
const stroke = prepareStroke(path, scale);
|
|
39893
42145
|
if (!stroke) return [];
|
|
39894
42146
|
const inverse = inverseStrokeMatrix(stroke.matrix);
|
|
@@ -39900,8 +42152,8 @@ function strokeContours(path, pageHeight, scale) {
|
|
|
39900
42152
|
(inverse[1] * x + inverse[3] * y + inverse[5]) * strokeScale
|
|
39901
42153
|
];
|
|
39902
42154
|
const toScreen = ([x, y]) => [
|
|
39903
|
-
(a * x / strokeScale + c * y / strokeScale + e) * scale,
|
|
39904
|
-
(pageHeight - b * x / strokeScale - d * y / strokeScale - f) * scale
|
|
42155
|
+
(a * x / strokeScale + c * y / strokeScale + e - originX) * scale,
|
|
42156
|
+
(pageHeight + originY - b * x / strokeScale - d * y / strokeScale - f) * scale
|
|
39905
42157
|
];
|
|
39906
42158
|
const contours = strokeOutlines(
|
|
39907
42159
|
segmentsToScreenPaths(path.segments, pageHeight, scale, project),
|
|
@@ -40059,18 +42311,34 @@ function renderSoftMask(mask, displayList, scale) {
|
|
|
40059
42311
|
}
|
|
40060
42312
|
return bitmap;
|
|
40061
42313
|
}
|
|
40062
|
-
function
|
|
40063
|
-
|
|
42314
|
+
function getDisplayListCropBox(list) {
|
|
42315
|
+
const full = [0, 0, list.width, list.height];
|
|
42316
|
+
const box = list.cropBox;
|
|
42317
|
+
if (!box || !box.every(Number.isFinite)) return full;
|
|
42318
|
+
const x0 = Math.max(0, Math.min(box[0], box[2]));
|
|
42319
|
+
const y0 = Math.max(0, Math.min(box[1], box[3]));
|
|
42320
|
+
const x1 = Math.min(list.width, Math.max(box[0], box[2]));
|
|
42321
|
+
const y1 = Math.min(list.height, Math.max(box[1], box[3]));
|
|
42322
|
+
return x1 > x0 && y1 > y0 ? [x0, y0, x1, y1] : full;
|
|
42323
|
+
}
|
|
42324
|
+
function hasCompositingEffects(operations, includeSoftMasks = false) {
|
|
42325
|
+
return operations.some((operation) => includeSoftMasks && !!operation.value.softMask || !!operation.value.blendMode && operation.value.blendMode !== "Normal" && operation.value.blendMode !== "Compatible" || operation.kind === "group" && hasCompositingEffects(operation.value.operations, includeSoftMasks));
|
|
42326
|
+
}
|
|
42327
|
+
function needsGroupBackdrop(group) {
|
|
42328
|
+
return group.isolated === false && !!group.softMask && hasCompositingEffects(group.operations, true);
|
|
40064
42329
|
}
|
|
40065
42330
|
function renderDisplayListToBitmap(displayList, options = {}) {
|
|
40066
42331
|
const baseScale = options.scale ?? (options.dpi ? options.dpi / 72 : 1.5);
|
|
40067
42332
|
const scaleX = options.dpiX !== void 0 ? options.dpiX / 72 : baseScale;
|
|
40068
42333
|
const scaleY = options.dpiY !== void 0 ? options.dpiY / 72 : baseScale;
|
|
40069
42334
|
const scale = scaleX;
|
|
42335
|
+
const [originX, originY] = displayList.origin ?? [0, 0];
|
|
42336
|
+
const pageTop = originY + displayList.height;
|
|
42337
|
+
const toScreen = (x, y) => [(x - originX) * scale, (pageTop - y) * scale];
|
|
40070
42338
|
const width = Math.max(1, Math.round(displayList.width * scale));
|
|
40071
42339
|
const height = Math.max(1, Math.round(displayList.height * scale));
|
|
40072
42340
|
const rgba = new Uint8Array(width * height * 4);
|
|
40073
|
-
const deferBackground = !options.transparent &&
|
|
42341
|
+
const deferBackground = !options.transparent && hasCompositingEffects(paintOperations(displayList));
|
|
40074
42342
|
const transparent = options.transparent || deferBackground;
|
|
40075
42343
|
const bg = options.background ?? { r: 1, g: 1, b: 1 };
|
|
40076
42344
|
const bgR = transparent ? 0 : Math.round(bg.r * 255);
|
|
@@ -40095,14 +42363,27 @@ function renderDisplayListToBitmap(displayList, options = {}) {
|
|
|
40095
42363
|
let operation = original.kind === "glyph" ? { kind: "path", value: glyphPaint(original.value) } : original;
|
|
40096
42364
|
if (operation.kind === "group") {
|
|
40097
42365
|
const group = operation.value;
|
|
40098
|
-
|
|
42366
|
+
let operations = group.operations;
|
|
42367
|
+
if (needsGroupBackdrop(group)) {
|
|
42368
|
+
operations = [{ kind: "image", value: {
|
|
42369
|
+
name: "GroupBackdrop",
|
|
42370
|
+
width,
|
|
42371
|
+
height,
|
|
42372
|
+
decodedRgba: rgba.slice(),
|
|
42373
|
+
matrix: [width / scale, 0, 0, height / scale, originX, pageTop - height / scale],
|
|
42374
|
+
colorSpace: "DeviceRGB",
|
|
42375
|
+
bitsPerComponent: 8,
|
|
42376
|
+
...group.bboxClip ? { clipPaths: [group.bboxClip] } : {}
|
|
42377
|
+
} }, ...operations];
|
|
42378
|
+
}
|
|
42379
|
+
const bitmap = renderDisplayListToBitmap({ ...displayList, rotation: 0, operations }, { ...options, scale, dpiX: void 0, dpiY: void 0, cropRect: void 0, useCropBox: false, transparent: true });
|
|
40099
42380
|
for (let i = 3; i < bitmap.data.length; i += 4) bitmap.data[i] = Math.round(bitmap.data[i] * group.alpha);
|
|
40100
42381
|
operation = { kind: "image", value: {
|
|
40101
42382
|
name: "TransparencyGroup",
|
|
40102
42383
|
width: bitmap.width,
|
|
40103
42384
|
height: bitmap.height,
|
|
40104
42385
|
decodedRgba: bitmap.data,
|
|
40105
|
-
matrix: [bitmap.width / scale, 0, 0, bitmap.height / scale,
|
|
42386
|
+
matrix: [bitmap.width / scale, 0, 0, bitmap.height / scale, originX, pageTop - bitmap.height / scale],
|
|
40106
42387
|
colorSpace: "DeviceRGB",
|
|
40107
42388
|
bitsPerComponent: 8,
|
|
40108
42389
|
blendMode: group.blendMode,
|
|
@@ -40116,7 +42397,7 @@ function renderDisplayListToBitmap(displayList, options = {}) {
|
|
|
40116
42397
|
for (const clip of clips) {
|
|
40117
42398
|
const { segments, fillRule } = "segments" in clip ? clip : { segments: clip, fillRule: "nonzero" };
|
|
40118
42399
|
const layer = new Uint8Array(width * height * 4);
|
|
40119
|
-
fillEdgesScanline4x4(layer, width, height, pathsToEdges(segmentsToScreenPaths(segments, displayList.height, scale), true), { r: 1, g: 1, b: 1 }, 1, fillRule);
|
|
42400
|
+
fillEdgesScanline4x4(layer, width, height, pathsToEdges(segmentsToScreenPaths(segments, displayList.height, scale, toScreen), true), { r: 1, g: 1, b: 1 }, 1, fillRule);
|
|
40120
42401
|
for (let i = 3; i < layer.length; i += 4) clipMask[i] = Math.round(clipMask[i] * layer[i] / 255);
|
|
40121
42402
|
}
|
|
40122
42403
|
cachedClips = clips;
|
|
@@ -40163,15 +42444,15 @@ function renderDisplayListToBitmap(displayList, options = {}) {
|
|
|
40163
42444
|
if (operation.kind === "path") {
|
|
40164
42445
|
const path = operation.value;
|
|
40165
42446
|
if (path.fillColor) {
|
|
40166
|
-
const edges = pathsToEdges(segmentsToScreenPaths(path.segments, displayList.height, scale), true);
|
|
40167
|
-
const clipScreen = path.clipRect ? [path.clipRect[0] * scale, (
|
|
42447
|
+
const edges = pathsToEdges(segmentsToScreenPaths(path.segments, displayList.height, scale, toScreen), true);
|
|
42448
|
+
const clipScreen = path.clipRect ? [(path.clipRect[0] - originX) * scale, (pageTop - path.clipRect[3]) * scale, (path.clipRect[2] - originX) * scale, (pageTop - path.clipRect[1]) * scale] : void 0;
|
|
40168
42449
|
fillEdgesScanline4x4(rgba, width, height, edges, path.fillColor, path.fillAlpha ?? 1, path.fillRule ?? "nonzero", clipScreen, original.kind === "glyph" ? aaTxt : aaVec, path.blendMode, clipMask);
|
|
40169
42450
|
}
|
|
40170
42451
|
if (path.strokeColor) {
|
|
40171
42452
|
const rawSw = path.strokeWidth * scale * (path.strokeMatrix ? Math.hypot(path.strokeMatrix[0], path.strokeMatrix[1]) : 1);
|
|
40172
42453
|
const strokeAlpha = options.thinLineMode === "shape" && rawSw < 1 ? (path.strokeAlpha ?? 1) * Math.max(0.25, rawSw) : path.strokeAlpha ?? 1;
|
|
40173
|
-
const edges = pathsToEdges(strokeContours(path, displayList.height, scale).map((points) => ({ points, closed: true })));
|
|
40174
|
-
const clipScreen = path.clipRect ? [path.clipRect[0] * scale, (
|
|
42454
|
+
const edges = pathsToEdges(strokeContours(path, displayList.height, scale, originX, originY).map((points) => ({ points, closed: true })));
|
|
42455
|
+
const clipScreen = path.clipRect ? [(path.clipRect[0] - originX) * scale, (pageTop - path.clipRect[3]) * scale, (path.clipRect[2] - originX) * scale, (pageTop - path.clipRect[1]) * scale] : void 0;
|
|
40175
42456
|
fillEdgesScanline4x4(
|
|
40176
42457
|
rgba,
|
|
40177
42458
|
width,
|
|
@@ -40194,12 +42475,12 @@ function renderDisplayListToBitmap(displayList, options = {}) {
|
|
|
40194
42475
|
if (Math.abs(det) <= 1e-8) continue;
|
|
40195
42476
|
const cornersPdfX = [e, a + e, a + c + e, c + e];
|
|
40196
42477
|
const cornersPdfY = [f, b + f, b + d + f, d + f];
|
|
40197
|
-
const cornersPx = cornersPdfX.map((cx) => cx * scale);
|
|
40198
|
-
const cornersPy = cornersPdfY.map((cy) => (
|
|
40199
|
-
const clipMinPx = img.clipRect ? Math.floor(img.clipRect[0] * scale) : 0;
|
|
40200
|
-
const clipMaxPx = img.clipRect ? Math.ceil(img.clipRect[2] * scale) - 1 : width - 1;
|
|
40201
|
-
const clipMinPy = img.clipRect ? Math.floor((
|
|
40202
|
-
const clipMaxPy = img.clipRect ? Math.ceil((
|
|
42478
|
+
const cornersPx = cornersPdfX.map((cx) => (cx - originX) * scale);
|
|
42479
|
+
const cornersPy = cornersPdfY.map((cy) => (pageTop - cy) * scale);
|
|
42480
|
+
const clipMinPx = img.clipRect ? Math.floor((img.clipRect[0] - originX) * scale) : 0;
|
|
42481
|
+
const clipMaxPx = img.clipRect ? Math.ceil((img.clipRect[2] - originX) * scale) - 1 : width - 1;
|
|
42482
|
+
const clipMinPy = img.clipRect ? Math.floor((pageTop - img.clipRect[3]) * scale) : 0;
|
|
42483
|
+
const clipMaxPy = img.clipRect ? Math.ceil((pageTop - img.clipRect[1]) * scale) - 1 : height - 1;
|
|
40203
42484
|
const minPx = Math.max(0, clipMinPx, Math.floor(Math.min(...cornersPx)));
|
|
40204
42485
|
const maxPx = Math.min(width - 1, clipMaxPx, Math.ceil(Math.max(...cornersPx)) - 1);
|
|
40205
42486
|
const minPy = Math.max(0, clipMinPy, Math.floor(Math.min(...cornersPy)));
|
|
@@ -40216,11 +42497,11 @@ function renderDisplayListToBitmap(displayList, options = {}) {
|
|
|
40216
42497
|
const smooth = Math.fround((s1 + s2 + Math.hypot(s1 - s2, 2 * cross)) / 2) <= 1;
|
|
40217
42498
|
const sample = new Float64Array(4);
|
|
40218
42499
|
for (let py = minPy; py <= maxPy; py++) {
|
|
40219
|
-
const yPdf =
|
|
42500
|
+
const yPdf = pageTop - (py + 0.5) / scale;
|
|
40220
42501
|
if (img.clipRect && (yPdf < img.clipRect[1] || yPdf > img.clipRect[3])) continue;
|
|
40221
42502
|
const dyPdf = yPdf - f;
|
|
40222
42503
|
for (let px = minPx; px <= maxPx; px++) {
|
|
40223
|
-
const xPdf = (px + 0.5) / scale;
|
|
42504
|
+
const xPdf = originX + (px + 0.5) / scale;
|
|
40224
42505
|
if (img.clipRect && (xPdf < img.clipRect[0] || xPdf > img.clipRect[2])) continue;
|
|
40225
42506
|
const dxPdf = xPdf - e;
|
|
40226
42507
|
const u = (d * dxPdf - c * dyPdf) / det;
|
|
@@ -40281,6 +42562,15 @@ function renderDisplayListToBitmap(displayList, options = {}) {
|
|
|
40281
42562
|
}
|
|
40282
42563
|
result = { width: targetW, height: targetH, data: resampled };
|
|
40283
42564
|
}
|
|
42565
|
+
if (options.useCropBox) {
|
|
42566
|
+
const box = getDisplayListCropBox(displayList);
|
|
42567
|
+
result = cropRgbaBitmap(result, {
|
|
42568
|
+
x: box[0] * scaleX,
|
|
42569
|
+
y: (displayList.height - box[3]) * scaleY,
|
|
42570
|
+
width: (box[2] - box[0]) * scaleX,
|
|
42571
|
+
height: (box[3] - box[1]) * scaleY
|
|
42572
|
+
});
|
|
42573
|
+
}
|
|
40284
42574
|
result = rotateRgbaBitmapQuarterTurns(result, displayList.rotation ?? 0);
|
|
40285
42575
|
if (options.cropRect) {
|
|
40286
42576
|
result = cropRgbaBitmap(result, options.cropRect);
|