1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Move the various DOM-factories into their own files

- Over time the number and size of these factories have increased, especially the `DOMFilterFactory` class, and this split should thus aid readability/maintainability of the code.

 - By introducing a couple of new import maps we can avoid bundling the `DOMCMapReaderFactory`/`DOMStandardFontDataFactory` classes in the Firefox PDF Viewer, since they are dead code there given that worker-thread fetching is always being used.

 - This patch has been successfully tested, by running `$ ./mach test toolkit/components/pdfjs/`, in a local Firefox artifact-build.

*Note:* This patch reduces the size of the `gulp mozcentral` output by `1.3` kilo-bytes, which isn't a lot but still cannot hurt.
This commit is contained in:
Jonas Jenwald 2024-11-01 12:34:39 +01:00
parent 06f3b2d0a6
commit 4e12906061
24 changed files with 1038 additions and 919 deletions

View file

@ -0,0 +1,111 @@
/* Copyright 2017 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 { DOMCanvasFactory } from "../../src/display/canvas_factory.js";
import { isNodeJS } from "../../src/shared/util.js";
describe("canvas_factory", function () {
describe("DOMCanvasFactory", function () {
let canvasFactory;
beforeAll(function () {
canvasFactory = new DOMCanvasFactory({});
});
afterAll(function () {
canvasFactory = null;
});
it("`create` should throw an error if the dimensions are invalid", function () {
// Invalid width.
expect(function () {
return canvasFactory.create(-1, 1);
}).toThrow(new Error("Invalid canvas size"));
// Invalid height.
expect(function () {
return canvasFactory.create(1, -1);
}).toThrow(new Error("Invalid canvas size"));
});
it("`create` should return a canvas if the dimensions are valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const { canvas, context } = canvasFactory.create(20, 40);
expect(canvas instanceof HTMLCanvasElement).toBe(true);
expect(context instanceof CanvasRenderingContext2D).toBe(true);
expect(canvas.width).toBe(20);
expect(canvas.height).toBe(40);
});
it("`reset` should throw an error if no canvas is provided", function () {
const canvasAndContext = { canvas: null, context: null };
expect(function () {
return canvasFactory.reset(canvasAndContext, 20, 40);
}).toThrow(new Error("Canvas is not specified"));
});
it("`reset` should throw an error if the dimensions are invalid", function () {
const canvasAndContext = { canvas: "foo", context: "bar" };
// Invalid width.
expect(function () {
return canvasFactory.reset(canvasAndContext, -1, 1);
}).toThrow(new Error("Invalid canvas size"));
// Invalid height.
expect(function () {
return canvasFactory.reset(canvasAndContext, 1, -1);
}).toThrow(new Error("Invalid canvas size"));
});
it("`reset` should alter the canvas/context if the dimensions are valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const canvasAndContext = canvasFactory.create(20, 40);
canvasFactory.reset(canvasAndContext, 60, 80);
const { canvas, context } = canvasAndContext;
expect(canvas instanceof HTMLCanvasElement).toBe(true);
expect(context instanceof CanvasRenderingContext2D).toBe(true);
expect(canvas.width).toBe(60);
expect(canvas.height).toBe(80);
});
it("`destroy` should throw an error if no canvas is provided", function () {
expect(function () {
return canvasFactory.destroy({});
}).toThrow(new Error("Canvas is not specified"));
});
it("`destroy` should clear the canvas/context", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const canvasAndContext = canvasFactory.create(20, 40);
canvasFactory.destroy(canvasAndContext);
const { canvas, context } = canvasAndContext;
expect(canvas).toBe(null);
expect(context).toBe(null);
});
});
});

View file

@ -9,6 +9,7 @@
"api_spec.js",
"app_options_spec.js",
"bidi_spec.js",
"canvas_factory_spec.js",
"cff_parser_spec.js",
"cmap_spec.js",
"colorspace_spec.js",
@ -42,6 +43,7 @@
"primitives_spec.js",
"stream_spec.js",
"struct_tree_spec.js",
"svg_factory_spec.js",
"text_layer_spec.js",
"type1_parser_spec.js",
"ui_utils_spec.js",

View file

@ -15,8 +15,6 @@
import { bytesToString, isNodeJS } from "../../src/shared/util.js";
import {
DOMCanvasFactory,
DOMSVGFactory,
getFilenameFromUrl,
getPdfFilenameFromUrl,
isValidFetchUrl,
@ -24,151 +22,6 @@ import {
} from "../../src/display/display_utils.js";
describe("display_utils", function () {
describe("DOMCanvasFactory", function () {
let canvasFactory;
beforeAll(function () {
canvasFactory = new DOMCanvasFactory({});
});
afterAll(function () {
canvasFactory = null;
});
it("`create` should throw an error if the dimensions are invalid", function () {
// Invalid width.
expect(function () {
return canvasFactory.create(-1, 1);
}).toThrow(new Error("Invalid canvas size"));
// Invalid height.
expect(function () {
return canvasFactory.create(1, -1);
}).toThrow(new Error("Invalid canvas size"));
});
it("`create` should return a canvas if the dimensions are valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const { canvas, context } = canvasFactory.create(20, 40);
expect(canvas instanceof HTMLCanvasElement).toBe(true);
expect(context instanceof CanvasRenderingContext2D).toBe(true);
expect(canvas.width).toBe(20);
expect(canvas.height).toBe(40);
});
it("`reset` should throw an error if no canvas is provided", function () {
const canvasAndContext = { canvas: null, context: null };
expect(function () {
return canvasFactory.reset(canvasAndContext, 20, 40);
}).toThrow(new Error("Canvas is not specified"));
});
it("`reset` should throw an error if the dimensions are invalid", function () {
const canvasAndContext = { canvas: "foo", context: "bar" };
// Invalid width.
expect(function () {
return canvasFactory.reset(canvasAndContext, -1, 1);
}).toThrow(new Error("Invalid canvas size"));
// Invalid height.
expect(function () {
return canvasFactory.reset(canvasAndContext, 1, -1);
}).toThrow(new Error("Invalid canvas size"));
});
it("`reset` should alter the canvas/context if the dimensions are valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const canvasAndContext = canvasFactory.create(20, 40);
canvasFactory.reset(canvasAndContext, 60, 80);
const { canvas, context } = canvasAndContext;
expect(canvas instanceof HTMLCanvasElement).toBe(true);
expect(context instanceof CanvasRenderingContext2D).toBe(true);
expect(canvas.width).toBe(60);
expect(canvas.height).toBe(80);
});
it("`destroy` should throw an error if no canvas is provided", function () {
expect(function () {
return canvasFactory.destroy({});
}).toThrow(new Error("Canvas is not specified"));
});
it("`destroy` should clear the canvas/context", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const canvasAndContext = canvasFactory.create(20, 40);
canvasFactory.destroy(canvasAndContext);
const { canvas, context } = canvasAndContext;
expect(canvas).toBe(null);
expect(context).toBe(null);
});
});
describe("DOMSVGFactory", function () {
let svgFactory;
beforeAll(function () {
svgFactory = new DOMSVGFactory();
});
afterAll(function () {
svgFactory = null;
});
it("`create` should throw an error if the dimensions are invalid", function () {
// Invalid width.
expect(function () {
return svgFactory.create(-1, 0);
}).toThrow(new Error("Invalid SVG dimensions"));
// Invalid height.
expect(function () {
return svgFactory.create(0, -1);
}).toThrow(new Error("Invalid SVG dimensions"));
});
it("`create` should return an SVG element if the dimensions are valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const svg = svgFactory.create(20, 40);
expect(svg instanceof SVGSVGElement).toBe(true);
expect(svg.getAttribute("version")).toBe("1.1");
expect(svg.getAttribute("width")).toBe("20px");
expect(svg.getAttribute("height")).toBe("40px");
expect(svg.getAttribute("preserveAspectRatio")).toBe("none");
expect(svg.getAttribute("viewBox")).toBe("0 0 20 40");
});
it("`createElement` should throw an error if the type is not a string", function () {
expect(function () {
return svgFactory.createElement(true);
}).toThrow(new Error("Invalid SVG element type"));
});
it("`createElement` should return an SVG element if the type is valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const svg = svgFactory.createElement("svg:rect");
expect(svg instanceof SVGRectElement).toBe(true);
});
});
describe("getFilenameFromUrl", function () {
it("should get the filename from an absolute URL", function () {
const url = "https://server.org/filename.pdf";

View file

@ -52,6 +52,7 @@ async function initializePDFJS(callback) {
"pdfjs-test/unit/api_spec.js",
"pdfjs-test/unit/app_options_spec.js",
"pdfjs-test/unit/bidi_spec.js",
"pdfjs-test/unit/canvas_factory_spec.js",
"pdfjs-test/unit/cff_parser_spec.js",
"pdfjs-test/unit/cmap_spec.js",
"pdfjs-test/unit/colorspace_spec.js",
@ -86,6 +87,7 @@ async function initializePDFJS(callback) {
"pdfjs-test/unit/scripting_spec.js",
"pdfjs-test/unit/stream_spec.js",
"pdfjs-test/unit/struct_tree_spec.js",
"pdfjs-test/unit/svg_factory_spec.js",
"pdfjs-test/unit/text_layer_spec.js",
"pdfjs-test/unit/type1_parser_spec.js",
"pdfjs-test/unit/ui_utils_spec.js",

View file

@ -41,7 +41,6 @@ import {
version,
} from "../../src/display/api.js";
import {
DOMSVGFactory,
fetchData,
getFilenameFromUrl,
getPdfFilenameFromUrl,
@ -59,6 +58,7 @@ import { AnnotationEditorLayer } from "../../src/display/editor/annotation_edito
import { AnnotationEditorUIManager } from "../../src/display/editor/tools.js";
import { AnnotationLayer } from "../../src/display/annotation_layer.js";
import { ColorPicker } from "../../src/display/editor/color_picker.js";
import { DOMSVGFactory } from "../../src/display/svg_factory.js";
import { DrawLayer } from "../../src/display/draw_layer.js";
import { GlobalWorkerOptions } from "../../src/display/worker_options.js";
import { TextLayer } from "../../src/display/text_layer.js";

View file

@ -0,0 +1,72 @@
/* Copyright 2017 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 { DOMSVGFactory } from "../../src/display/svg_factory.js";
import { isNodeJS } from "../../src/shared/util.js";
describe("svg_factory", function () {
describe("DOMSVGFactory", function () {
let svgFactory;
beforeAll(function () {
svgFactory = new DOMSVGFactory();
});
afterAll(function () {
svgFactory = null;
});
it("`create` should throw an error if the dimensions are invalid", function () {
// Invalid width.
expect(function () {
return svgFactory.create(-1, 0);
}).toThrow(new Error("Invalid SVG dimensions"));
// Invalid height.
expect(function () {
return svgFactory.create(0, -1);
}).toThrow(new Error("Invalid SVG dimensions"));
});
it("`create` should return an SVG element if the dimensions are valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const svg = svgFactory.create(20, 40);
expect(svg instanceof SVGSVGElement).toBe(true);
expect(svg.getAttribute("version")).toBe("1.1");
expect(svg.getAttribute("width")).toBe("20px");
expect(svg.getAttribute("height")).toBe("40px");
expect(svg.getAttribute("preserveAspectRatio")).toBe("none");
expect(svg.getAttribute("viewBox")).toBe("0 0 20 40");
});
it("`createElement` should throw an error if the type is not a string", function () {
expect(function () {
return svgFactory.createElement(true);
}).toThrow(new Error("Invalid SVG element type"));
});
it("`createElement` should return an SVG element if the type is valid", function () {
if (isNodeJS) {
pending("Document is not supported in Node.js.");
}
const svg = svgFactory.createElement("svg:rect");
expect(svg instanceof SVGRectElement).toBe(true);
});
});
});

View file

@ -20,6 +20,8 @@
"fluent-dom": "../../node_modules/@fluent/dom/esm/index.js",
"cached-iterable": "../../node_modules/cached-iterable/src/index.mjs",
"display-cmap_reader_factory": "../../src/display/cmap_reader_factory.js",
"display-standard_fontdata_factory": "../../src/display/standard_fontdata_factory.js",
"display-fetch_stream": "../../src/display/fetch_stream.js",
"display-network": "../../src/display/network.js",
"display-node_stream": "../../src/display/stubs.js",