Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/internal_viewer_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
35 changes: 25 additions & 10 deletions src/core/postscript/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PsNode>} — one tree node per output value.
*/
evaluate(program, numInputs) {
evaluate(program, numInputs, numOutputs) {
if (!PSStackToTree.#binaryOps) {
PSStackToTree.#init();
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/core/postscript/js_evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 6 additions & 2 deletions src/core/postscript/wasm_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@
!issue4398.pdf
!issue5421.pdf
!issue5470.pdf
!postscript_type4_stack_depth.pdf
!issue5501.pdf
!issue5599.pdf
!issue17147.pdf
Expand Down
Binary file added test/pdfs/postscript_type4_stack_depth.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions test/unit/postscript_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Loading