Skip to content

Commit 9f3de1e

Browse files
committed
Add an interpreter for optimized ps code
It'll be used as a fallback when wasm is disabled. And add in the debugger a view for the generated js code and one for the ps code.
1 parent 58b807d commit 9f3de1e

10 files changed

Lines changed: 1217 additions & 176 deletions

File tree

src/core/document.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,22 @@ class PDFDocument {
21362136
return obj;
21372137
}
21382138

2139+
if (dict.get("FunctionType") === 4) {
2140+
const source = value.getString();
2141+
value.reset();
2142+
const domain = dict.get("Domain") ?? [];
2143+
const range = dict.get("Range") ?? [];
2144+
obj.psFunction = true;
2145+
obj.source = source;
2146+
obj.psLines = InternalViewerUtils.tokenizePSSource(source);
2147+
obj.jsCode = InternalViewerUtils.postScriptToJSCode(
2148+
source,
2149+
domain,
2150+
range
2151+
);
2152+
return obj;
2153+
}
2154+
21392155
obj.bytes = value.getString();
21402156
return obj;
21412157
}

src/core/function.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "../shared/util.js";
2626
import { PostScriptLexer, PostScriptParser } from "./ps_parser.js";
2727
import { BaseStream } from "./base_stream.js";
28+
import { buildPostScriptJsFunction } from "./postscript/js_evaluator.js";
2829
import { buildPostScriptWasmFunction } from "./postscript/wasm_compiler.js";
2930
import { isNumberArray } from "./core_utils.js";
3031
import { LocalFunctionCache } from "./image_utils.js";
@@ -370,8 +371,8 @@ class PDFFunction {
370371
throw new FormatError("No range.");
371372
}
372373

373-
if (factory.useWasm) {
374-
try {
374+
try {
375+
if (factory.useWasm) {
375376
const wasmFn = buildPostScriptWasmFunction(
376377
fn.getString(),
377378
domain,
@@ -380,9 +381,14 @@ class PDFFunction {
380381
if (wasmFn) {
381382
return wasmFn; // (src, srcOffset, dest, destOffset) → void
382383
}
383-
} catch {
384-
// Fall through to the existing interpreter-based path.
384+
} else {
385+
const jsFn = buildPostScriptJsFunction(fn.getString(), domain, range);
386+
if (jsFn) {
387+
return jsFn; // (src, srcOffset, dest, destOffset) → void
388+
}
385389
}
390+
} catch {
391+
// Fall through to the existing interpreter-based path.
386392
}
387393

388394
warn("Unable to compile PS function, using interpreter");

0 commit comments

Comments
 (0)