@@ -21,18 +21,30 @@ import {
2121 MathClamp ,
2222 shadow ,
2323 unreachable ,
24+ warn ,
2425} from "../shared/util.js" ;
2526import { PostScriptLexer , PostScriptParser } from "./ps_parser.js" ;
2627import { BaseStream } from "./base_stream.js" ;
28+ import { buildPostScriptWasmFunction } from "./postscript/wasm_compiler.js" ;
2729import { isNumberArray } from "./core_utils.js" ;
2830import { LocalFunctionCache } from "./image_utils.js" ;
2931
3032class PDFFunctionFactory {
33+ static #useWasm = true ;
34+
35+ static setOptions ( { useWasm } ) {
36+ PDFFunctionFactory . #useWasm = useWasm ;
37+ }
38+
3139 constructor ( { xref, isEvalSupported = true } ) {
3240 this . xref = xref ;
3341 this . isEvalSupported = isEvalSupported !== false ;
3442 }
3543
44+ get useWasm ( ) {
45+ return PDFFunctionFactory . #useWasm;
46+ }
47+
3648 create ( fn , parseArray = false ) {
3749 let fnRef , parsedFn ;
3850
@@ -358,6 +370,24 @@ class PDFFunction {
358370 throw new FormatError ( "No range." ) ;
359371 }
360372
373+ if ( factory . useWasm ) {
374+ try {
375+ const wasmFn = buildPostScriptWasmFunction (
376+ fn . getString ( ) ,
377+ domain ,
378+ range
379+ ) ;
380+ if ( wasmFn ) {
381+ return wasmFn ; // (src, srcOffset, dest, destOffset) → void
382+ }
383+ } catch {
384+ // Fall through to the existing interpreter-based path.
385+ }
386+ }
387+
388+ warn ( "Unable to compile PS function, using interpreter" ) ;
389+ fn . reset ( ) ;
390+
361391 const lexer = new PostScriptLexer ( fn ) ;
362392 const parser = new PostScriptParser ( lexer ) ;
363393 const code = parser . parse ( ) ;
0 commit comments