From 0aaac4482cd014b25145723e624e0f310049c436 Mon Sep 17 00:00:00 2001 From: Victor Hugo <113398568+zerotohero3@users.noreply.github.com> Date: Mon, 2 Nov 2026 03:05:29 -0300 Subject: [PATCH] Keep the stacks aligned on their top in `PSStackToTree` When the two paths of a stack-growing `if`, or the two branches of an `ifelse`, end with different stack depths, the shorter stack was padded with zeros on its top. The following operators then used these zeros instead of the actual values: for example the tint transform in issue5470.pdf is only correct when its guard fires, i.e. for a tint of 1, and any other tint is rendered in a darker red instead of red. Pad the bottom of the shorter stack instead. And when a function leaves more values on the stack than it has outputs, use the ones at the top, as the interpreter does, instead of the ones at the bottom. --- src/core/internal_viewer_utils.js | 4 +-- src/core/postscript/ast.js | 35 +++++++++++++++------ src/core/postscript/js_evaluator.js | 4 +-- src/core/postscript/wasm_compiler.js | 8 +++-- test/pdfs/.gitignore | 1 + test/pdfs/postscript_type4_stack_depth.pdf | Bin 0 -> 1472 bytes test/test_manifest.json | 7 +++++ test/unit/postscript_spec.js | 34 ++++++++++++++++++++ 8 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 test/pdfs/postscript_type4_stack_depth.pdf diff --git a/src/core/internal_viewer_utils.js b/src/core/internal_viewer_utils.js index 140f7f3885751..0e3b188e45faa 100644 --- a/src/core/internal_viewer_utils.js +++ b/src/core/internal_viewer_utils.js @@ -437,8 +437,8 @@ const InternalViewerUtils = { const program = parsePostScriptFunction(source); const nIn = domain.length >> 1; const nOut = range.length >> 1; - const outputs = new PSStackToTree().evaluate(program, nIn); - if (!outputs || outputs.length < nOut) { + const outputs = new PSStackToTree().evaluate(program, nIn, nOut); + if (!outputs) { return null; } diff --git a/src/core/postscript/ast.js b/src/core/postscript/ast.js index cc11dcda2e165..e849eb36c6f5d 100644 --- a/src/core/postscript/ast.js +++ b/src/core/postscript/ast.js @@ -518,7 +518,11 @@ function _binaryValueType(op, firstType, secondType) { * expressions — one per function output channel. * * Usage: - * const outputs = new PSStackToTree().evaluate(program, numInputs); + * const outputs = new PSStackToTree().evaluate( + * program, + * numInputs, + * numOutputs + * ); */ class PSStackToTree { static #binaryOps = null; @@ -595,9 +599,11 @@ class PSStackToTree { * @param {PsProgram} program * @param {number} numInputs — number of domain values placed on the stack * before the program runs (i.e. the length of the domain array / 2). + * @param {number} [numOutputs] — when defined, only the values at the top + * of the stack are kept as outputs, like the interpreter does. * @returns {Array} — one tree node per output value. */ - evaluate(program, numInputs) { + evaluate(program, numInputs, numOutputs) { if (!PSStackToTree.#binaryOps) { PSStackToTree.#init(); } @@ -615,6 +621,12 @@ class PSStackToTree { if (this._failed) { return null; } + if (numOutputs !== undefined) { + if (stack.length < numOutputs) { + return null; + } + stack.splice(0, stack.length - numOutputs); + } PSStackToTree.#markShared(stack); return stack; } @@ -751,15 +763,17 @@ class PSStackToTree { // well-formed PostScript functions this happens when the // remaining code still has a "default value" guard that fires // unconditionally for one path but not the other. Pad the - // shorter result with PsConstNode(0) so both have the same - // length; the padding zeros end up in ternary branches that - // are never selected at runtime. + // bottom of the shorter result with PsConstNode(0) so both have + // the same length while staying aligned on their top, which is + // where the operators and the outputs take their values from; + // the padding zeros then end up in ternary branches that are + // never selected at runtime. const zero = new PsConstNode(0); while (trueStack.length < falseStack.length) { - trueStack.push(zero); + trueStack.unshift(zero); } while (falseStack.length < trueStack.length) { - falseStack.push(zero); + falseStack.unshift(zero); } } stack.length = 0; @@ -798,15 +812,16 @@ class PSStackToTree { } if (thenStack.length !== elseStack.length) { - // Pad the shorter branch with zeros so both have the same depth. + // Pad the bottom of the shorter branch with zeros so both have the + // same depth while staying aligned on their top. // For well-formed functions the extra zeros land in branches that // are never selected at runtime. const zero = new PsConstNode(0); while (thenStack.length < elseStack.length) { - thenStack.push(zero); + thenStack.unshift(zero); } while (elseStack.length < thenStack.length) { - elseStack.push(zero); + elseStack.unshift(zero); } } stack.length = 0; diff --git a/src/core/postscript/js_evaluator.js b/src/core/postscript/js_evaluator.js index 0b7645f906a4e..8ee4660e6eb97 100644 --- a/src/core/postscript/js_evaluator.js +++ b/src/core/postscript/js_evaluator.js @@ -304,8 +304,8 @@ class PsJsCompiler { } compile(program) { - const outputs = new PSStackToTree().evaluate(program, this.nIn); - if (!outputs || outputs.length < this.nOut) { + const outputs = new PSStackToTree().evaluate(program, this.nIn, this.nOut); + if (!outputs) { return null; } diff --git a/src/core/postscript/wasm_compiler.js b/src/core/postscript/wasm_compiler.js index 695e0342b3882..02d4b92ce2f8e 100644 --- a/src/core/postscript/wasm_compiler.js +++ b/src/core/postscript/wasm_compiler.js @@ -867,8 +867,12 @@ class PsWasmCompiler { * @returns {Uint8Array|null} Wasm binary, or null if compilation failed. */ compile(program) { - const outputs = new PSStackToTree().evaluate(program, this._nIn); - if (!outputs || outputs.length < this._nOut) { + const outputs = new PSStackToTree().evaluate( + program, + this._nIn, + this._nOut + ); + if (!outputs) { return null; } diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index bd39857247df5..8153e882072aa 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -367,6 +367,7 @@ !issue4398.pdf !issue5421.pdf !issue5470.pdf +!postscript_type4_stack_depth.pdf !issue5501.pdf !issue5599.pdf !issue17147.pdf diff --git a/test/pdfs/postscript_type4_stack_depth.pdf b/test/pdfs/postscript_type4_stack_depth.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8d9e965e3dd0670d749bcf9f1ffd5706c8e52a01 GIT binary patch literal 1472 zcmbtUO;6iE5WV|X%mt~HO1zuc`H&EY0BNPRDq!DG4;yC^jLdG>T~i9;zjtQsfb+qr zmSx#9Z)V<`8Sl~M`RrKsMv(*oW_}l)oIsraT_eP2%B#w(Aih*9bkHZjHJqMCs22?6 z@9lsF7k@7o4sHev|4ms;tv$$2Aba)$xr{}EHg)2kxG2@sJi(11e7`S1js)eqMrUeU z5FutdGZYbCBqUss>Tu1<*leu|48F5Wz)hUtT3O{wqan`L#tVqg@v$uM`s)Y^w}hKPT>{GW%Fp0a5d4IS$}?B~Zc2VXO2_5c{|}tRVPb_v!K+H+0+fC34W|-!!MSlOP4;OMc2Eg?s^@ z@>`1t78l?iYFcIY1^g{)P#TO4!!a5^Lw*MrdGrv)yp;v_yq|(AG^d)RkO;ml!m@*C z0K<;~qc+8dIcUaQVlTy2v@3@oLCqfnkEN;5@5u4wV38!oZK;vFYKY9Xnilb<<|9By z&0)y9nr;8p{y3j3E+YZsNW5JzOjBCuCA`}v5WJw@ZRvQ=CMEp_e06XGn*OdWIe;4y t(_P$X+==y8l@;1>{$}Mf!kGSPWBBN7&t2!0^+Av9C*$bo=;GHb`UiIlhbRC5 literal 0 HcmV?d00001 diff --git a/test/test_manifest.json b/test/test_manifest.json index e29a438c802f4..93b3fbf96f771 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -1789,6 +1789,13 @@ "link": false, "type": "eq" }, + { + "id": "postscript-type4-stack-depth", + "file": "pdfs/postscript_type4_stack_depth.pdf", + "md5": "e7ea6f3875e1a977e6a9012e776a64af", + "rounds": 1, + "type": "eq" + }, { "id": "xref_command_missing", "file": "pdfs/xref_command_missing.pdf", diff --git a/test/unit/postscript_spec.js b/test/unit/postscript_spec.js index 24c1b961da017..eefb5a401cbbd 100644 --- a/test/unit/postscript_spec.js +++ b/test/unit/postscript_spec.js @@ -775,6 +775,40 @@ describe("PostScript Type 4 lexer, parser, and Wasm compiler", function () { expect(r[1]).toBeCloseTo(0.7, 9); }); + it("compiles stack-growing if — paths with different stack depths", function () { + // The tint transform from issue5470.pdf: the guard only fires when + // in0 ≈ 1, and the two paths reach the last `if` with a different stack + // depth. + const src = + "{ dup 0 ge { dup 1 sub abs 0.005 lt { pop 0.934 0.1935 0.1406 -5 } if } if" + + " 0 ge { 1 0 0 } if }"; + const domain = [0, 1]; + const range = [0, 1, 0, 1, 0, 1]; + expect(compileAndRun(src, domain, range, [0.5])).toEqual([1, 0, 0]); + const r = compileAndRun(src, domain, range, [1]); + expect(r[0]).toBeCloseTo(0.934, 9); + expect(r[1]).toBeCloseTo(0.1935, 9); + expect(r[2]).toBeCloseTo(0.1406, 9); + }); + + it("compiles ifelse — branches with different stack depths", function () { + // The then-branch pushes a sentinel on top of its outputs, which is + // checked by the second ifelse. + const src = + "{ dup 0.5 gt { pop 1 0 0 -1 } { } ifelse" + + " dup 0 lt { pop } { 0 1 } ifelse }"; + const domain = [0, 1]; + const range = [0, 1, 0, 1, 0, 1]; + expect(compileAndRun(src, domain, range, [0.25])).toEqual([0.25, 0, 1]); + expect(compileAndRun(src, domain, range, [0.75])).toEqual([1, 0, 0]); + }); + + it("uses the values at the top of the stack as outputs", function () { + // The function leaves more values on the stack than its outputs. + const r = compileAndRun("{ 1 2 3 }", [0, 1], [0, 10, 0, 10], [0.5]); + expect(r).toEqual([2, 3]); + }); + // Range clamping. it("clamps output to declared range", async function () {