mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-11-30 16:37:18 +01:00
Prepare callers for loading calculator functions on demand. Keep function streams reusable and reject circular stitching functions.
271 lines
8.3 KiB
JavaScript
271 lines
8.3 KiB
JavaScript
/* Copyright 2026 Mozilla Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { FunctionType, PDFFunctionFactory } from "../../src/core/function.js";
|
|
import { Stream, StringStream } from "../../src/core/stream.js";
|
|
import { Dict } from "../../src/core/primitives.js";
|
|
import { FormatError } from "../../src/shared/util.js";
|
|
import { XRefMock } from "./test_utils.js";
|
|
|
|
describe("function", function () {
|
|
let factory;
|
|
|
|
beforeAll(function () {
|
|
factory = new PDFFunctionFactory({ xref: new XRefMock() });
|
|
});
|
|
|
|
afterAll(function () {
|
|
factory = null;
|
|
});
|
|
|
|
it("evaluates concurrent requests for the same calculator stream", async function () {
|
|
const dict = new Dict();
|
|
dict.set("FunctionType", FunctionType.POSTSCRIPT_CALCULATOR);
|
|
dict.set("Domain", [0, 1]);
|
|
dict.set("Range", [0, 1]);
|
|
const stream = new StringStream("{ dup mul }", dict);
|
|
|
|
const [first, second] = await Promise.all([
|
|
factory.create(stream),
|
|
factory.create(stream),
|
|
]);
|
|
|
|
expect(evaluate(first, 0.5)).toEqual([0.25]);
|
|
expect(evaluate(second, 0.25)).toEqual([0.0625]);
|
|
expect(stream.pos).toEqual(0);
|
|
});
|
|
|
|
function createSampledStream({
|
|
domain,
|
|
range,
|
|
size,
|
|
samples,
|
|
bitsPerSample = 8,
|
|
}) {
|
|
const dict = new Dict();
|
|
dict.set("FunctionType", 0);
|
|
dict.set("Domain", domain);
|
|
dict.set("Range", range);
|
|
dict.set("Size", size);
|
|
dict.set("BitsPerSample", bitsPerSample);
|
|
const bytes = new Uint8Array(samples);
|
|
return new Stream(bytes, 0, bytes.length, dict);
|
|
}
|
|
|
|
function createSampledFunction(params) {
|
|
return factory.create(createSampledStream(params));
|
|
}
|
|
|
|
function createExponentialFunction(c0, c1) {
|
|
const dict = new Dict();
|
|
dict.set("FunctionType", 2);
|
|
dict.set("Domain", [0, 1]);
|
|
dict.set("C0", c0);
|
|
dict.set("C1", c1);
|
|
dict.set("N", 1);
|
|
return dict;
|
|
}
|
|
|
|
function createStitchedFunction(fns) {
|
|
const dict = new Dict();
|
|
dict.set("FunctionType", 3);
|
|
dict.set("Domain", [0, 1]);
|
|
dict.set("Functions", fns);
|
|
dict.set("Bounds", [0.5]);
|
|
dict.set("Encode", [0, 1, 0, 1]);
|
|
return factory.create(dict);
|
|
}
|
|
|
|
function evaluate(fn, ...src) {
|
|
const dest = new Float64Array(fn.numOutputs);
|
|
fn(new Float64Array(src), 0, dest, 0);
|
|
return Array.from(dest);
|
|
}
|
|
|
|
// Samples are 255 where the first two inputs are both 1, and 0 elsewhere.
|
|
function createAndFunction(numInputs) {
|
|
const count = 1 << numInputs;
|
|
const samples = new Array(count).fill(0);
|
|
for (let i = 3; i < count; i += 4) {
|
|
samples[i] = 255;
|
|
}
|
|
return createSampledFunction({
|
|
domain: new Array(numInputs).fill([0, 1]).flat(),
|
|
range: [0, 1],
|
|
size: new Array(numInputs).fill(2),
|
|
samples,
|
|
});
|
|
}
|
|
|
|
describe("Sampled function", function () {
|
|
it("must interpolate multilinearly", async function () {
|
|
const fn = await createAndFunction(2);
|
|
|
|
expect(fn.numInputs).toEqual(2);
|
|
expect(fn.numOutputs).toEqual(1);
|
|
expect(evaluate(fn, 0.5, 0.25)).toEqual([0.125]);
|
|
expect(evaluate(fn, 1, 0.5)).toEqual([0.5]);
|
|
expect(evaluate(fn, 1, 1)).toEqual([1]);
|
|
});
|
|
|
|
it("must use simplex interpolation for many inputs", async function () {
|
|
const fn = await createAndFunction(9);
|
|
|
|
expect(fn.numInputs).toEqual(9);
|
|
expect(evaluate(fn, 0.5, 0.25, 0, 0, 0, 0, 0, 0, 0)).toEqual([0.25]);
|
|
expect(evaluate(fn, 1, 0.5, 0, 0, 0, 0, 0, 0, 0)).toEqual([0.5]);
|
|
expect(evaluate(fn, 1, 1, 0, 1, 0, 1, 0, 1, 0)).toEqual([1]);
|
|
});
|
|
|
|
it("must handle inputs with a single sample", async function () {
|
|
const fn = await createSampledFunction({
|
|
domain: [0, 1, 0, 1, 0, 1],
|
|
range: [0, 1],
|
|
size: [2, 1, 2],
|
|
samples: [0, 51, 102, 255],
|
|
});
|
|
|
|
expect(evaluate(fn, 0.5, 0.3, 0)).toEqual([0.1]);
|
|
expect(evaluate(fn, 1, 0.3, 1)).toEqual([1]);
|
|
});
|
|
|
|
it("must evaluate functions with many single-sample inputs", async function () {
|
|
const numInputs = 18;
|
|
const fn = await createSampledFunction({
|
|
domain: new Array(numInputs).fill([0, 1]).flat(),
|
|
range: [0, 1],
|
|
size: new Array(numInputs).fill(1),
|
|
samples: [51],
|
|
});
|
|
|
|
expect(evaluate(fn, ...new Array(numInputs).fill(0.5))).toEqual([0.2]);
|
|
});
|
|
|
|
it("must interpret 32-bit samples as unsigned", async function () {
|
|
const fn = await createSampledFunction({
|
|
domain: [0, 1],
|
|
range: [0, 1],
|
|
size: [2],
|
|
samples: [0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff],
|
|
bitsPerSample: 32,
|
|
});
|
|
|
|
const sampleMul = 1 / (2 ** 32 - 1);
|
|
expect(evaluate(fn, 0)).toEqual([0x80000000 * sampleMul]);
|
|
expect(evaluate(fn, 1)).toEqual([1]);
|
|
});
|
|
});
|
|
|
|
describe("Array of functions", function () {
|
|
it("must combine 1-out functions", async function () {
|
|
const fn = await factory.create(
|
|
[
|
|
createExponentialFunction([0], [1]),
|
|
createExponentialFunction([1], [0]),
|
|
],
|
|
/* parseArray = */ true
|
|
);
|
|
|
|
expect(fn.numInputs).toEqual(1);
|
|
expect(fn.numOutputs).toEqual(2);
|
|
expect(evaluate(fn, 0.25)).toEqual([0.25, 0.75]);
|
|
});
|
|
|
|
it("must accept a single n-out function", async function () {
|
|
const fn = await factory.create(
|
|
[createExponentialFunction([0, 0, 0], [1, 1, 1])],
|
|
/* parseArray = */ true
|
|
);
|
|
|
|
expect(fn.numInputs).toEqual(1);
|
|
expect(fn.numOutputs).toEqual(3);
|
|
});
|
|
|
|
it("must reject several n-out functions", async function () {
|
|
await expectAsync(
|
|
factory.create(
|
|
[
|
|
createExponentialFunction([0, 0], [1, 1]),
|
|
createExponentialFunction([0, 0], [1, 1]),
|
|
],
|
|
/* parseArray = */ true
|
|
)
|
|
).toBeRejectedWithError(FormatError, "Invalid array of functions.");
|
|
});
|
|
});
|
|
|
|
describe("Stitching function", function () {
|
|
it("must reject a circular sub-function", async function () {
|
|
const dict = new Dict();
|
|
dict.set("FunctionType", FunctionType.STITCHING);
|
|
dict.set("Domain", [0, 1]);
|
|
dict.set("Functions", [dict]);
|
|
|
|
await expectAsync(factory.create(dict)).toBeRejectedWithError(
|
|
FormatError,
|
|
"Circular stitched function"
|
|
);
|
|
});
|
|
|
|
it("must have the outputs of its sub-functions", async function () {
|
|
const fn = await createStitchedFunction([
|
|
createExponentialFunction([0, 0, 0], [1, 1, 1]),
|
|
createExponentialFunction([1, 0, 0], [0, 0, 1]),
|
|
]);
|
|
|
|
expect(fn.numInputs).toEqual(1);
|
|
expect(fn.numOutputs).toEqual(3);
|
|
expect(evaluate(fn, 0.25)).toEqual([0.5, 0.5, 0.5]);
|
|
expect(evaluate(fn, 0.75)).toEqual([0.5, 0, 0.5]);
|
|
});
|
|
|
|
it("must reject sub-functions with several inputs", async function () {
|
|
await expectAsync(
|
|
createStitchedFunction([
|
|
createExponentialFunction([0, 0, 0], [1, 1, 1]),
|
|
createSampledStream({
|
|
domain: [0, 1, 0, 1],
|
|
range: [0, 1, 0, 1, 0, 1],
|
|
size: [2, 2],
|
|
samples: new Array(12).fill(0),
|
|
}),
|
|
])
|
|
).toBeRejectedWithError(
|
|
FormatError,
|
|
"Incompatible sub-functions for stitched function"
|
|
);
|
|
});
|
|
|
|
it("must reject sub-functions with different outputs", async function () {
|
|
await expectAsync(
|
|
createStitchedFunction([
|
|
createExponentialFunction([0, 0, 0], [1, 1, 1]),
|
|
createExponentialFunction([0], [1]),
|
|
])
|
|
).toBeRejectedWithError(
|
|
FormatError,
|
|
"Incompatible sub-functions for stitched function"
|
|
);
|
|
});
|
|
|
|
it("must reject an empty array of sub-functions", async function () {
|
|
await expectAsync(createStitchedFunction([])).toBeRejectedWithError(
|
|
FormatError,
|
|
"Incompatible sub-functions for stitched function"
|
|
);
|
|
});
|
|
});
|
|
});
|