2021-06-07 18:45:16 +09:00
|
|
|
/* Copyright 2015 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 { CMapCompressionType, unreachable } from "../shared/util.js";
|
|
|
|
|
[api-minor] Extend general transfer function support to browsers without `OffscreenCanvas`
This patch extends PR 16115 to work in all browsers, regardless of their `OffscreenCanvas` support, such that transfer functions will be applied to general rendering (and not just image data).
In order to do this we introduce the `BaseFilterFactory` that is then extended in browsers/Node.js environments, similar to all the other factories used in the API, such that we always have the necessary factory available in `src/display/canvas.js`.
These changes help simplify the existing `putBinaryImageData` function, and the new method can easily be stubbed-out in the Firefox PDF Viewer.
*Please note:* This patch removes the old *partial* transfer function support, which only applied to image data, from Node.js environments since the `node-canvas` package currently doesn't support filters. However, this should hopefully be fine given that:
- Transfer functions are not very commonly used in PDF documents.
- Browsers in general, and Firefox in particular, are the *primary* development target for the PDF.js library.
- The FAQ only lists Node.js as *mostly* supported, see https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
2023-03-12 23:47:01 +09:00
|
|
|
class BaseFilterFactory {
|
|
|
|
constructor() {
|
|
|
|
if (this.constructor === BaseFilterFactory) {
|
|
|
|
unreachable("Cannot initialize BaseFilterFactory.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addFilter(maps) {
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
|
2023-03-07 03:09:56 +09:00
|
|
|
addHCMFilter(fgColor, bgColor) {
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(keepHCM = false) {}
|
[api-minor] Extend general transfer function support to browsers without `OffscreenCanvas`
This patch extends PR 16115 to work in all browsers, regardless of their `OffscreenCanvas` support, such that transfer functions will be applied to general rendering (and not just image data).
In order to do this we introduce the `BaseFilterFactory` that is then extended in browsers/Node.js environments, similar to all the other factories used in the API, such that we always have the necessary factory available in `src/display/canvas.js`.
These changes help simplify the existing `putBinaryImageData` function, and the new method can easily be stubbed-out in the Firefox PDF Viewer.
*Please note:* This patch removes the old *partial* transfer function support, which only applied to image data, from Node.js environments since the `node-canvas` package currently doesn't support filters. However, this should hopefully be fine given that:
- Transfer functions are not very commonly used in PDF documents.
- Browsers in general, and Firefox in particular, are the *primary* development target for the PDF.js library.
- The FAQ only lists Node.js as *mostly* supported, see https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
2023-03-12 23:47:01 +09:00
|
|
|
}
|
|
|
|
|
2021-06-07 18:45:16 +09:00
|
|
|
class BaseCanvasFactory {
|
|
|
|
constructor() {
|
|
|
|
if (this.constructor === BaseCanvasFactory) {
|
|
|
|
unreachable("Cannot initialize BaseCanvasFactory.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
create(width, height) {
|
2021-06-11 23:35:42 +09:00
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
throw new Error("Invalid canvas size");
|
|
|
|
}
|
|
|
|
const canvas = this._createCanvas(width, height);
|
|
|
|
return {
|
|
|
|
canvas,
|
|
|
|
context: canvas.getContext("2d"),
|
|
|
|
};
|
2021-06-07 18:45:16 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
reset(canvasAndContext, width, height) {
|
|
|
|
if (!canvasAndContext.canvas) {
|
|
|
|
throw new Error("Canvas is not specified");
|
|
|
|
}
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
throw new Error("Invalid canvas size");
|
|
|
|
}
|
|
|
|
canvasAndContext.canvas.width = width;
|
|
|
|
canvasAndContext.canvas.height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(canvasAndContext) {
|
|
|
|
if (!canvasAndContext.canvas) {
|
|
|
|
throw new Error("Canvas is not specified");
|
|
|
|
}
|
|
|
|
// Zeroing the width and height cause Firefox to release graphics
|
|
|
|
// resources immediately, which can greatly reduce memory consumption.
|
|
|
|
canvasAndContext.canvas.width = 0;
|
|
|
|
canvasAndContext.canvas.height = 0;
|
|
|
|
canvasAndContext.canvas = null;
|
|
|
|
canvasAndContext.context = null;
|
2021-06-11 23:35:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-07 19:55:17 +09:00
|
|
|
* @ignore
|
2021-06-11 23:35:42 +09:00
|
|
|
*/
|
|
|
|
_createCanvas(width, height) {
|
|
|
|
unreachable("Abstract method `_createCanvas` called.");
|
2021-06-07 18:45:16 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BaseCMapReaderFactory {
|
2023-01-30 22:12:06 +09:00
|
|
|
constructor({ baseUrl = null, isCompressed = true }) {
|
2021-06-07 18:45:16 +09:00
|
|
|
if (this.constructor === BaseCMapReaderFactory) {
|
|
|
|
unreachable("Cannot initialize BaseCMapReaderFactory.");
|
|
|
|
}
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.isCompressed = isCompressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetch({ name }) {
|
|
|
|
if (!this.baseUrl) {
|
|
|
|
throw new Error(
|
|
|
|
'The CMap "baseUrl" parameter must be specified, ensure that ' +
|
|
|
|
'the "cMapUrl" and "cMapPacked" API parameters are provided.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
throw new Error("CMap name must be specified.");
|
|
|
|
}
|
|
|
|
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
|
|
|
|
const compressionType = this.isCompressed
|
|
|
|
? CMapCompressionType.BINARY
|
|
|
|
: CMapCompressionType.NONE;
|
|
|
|
|
|
|
|
return this._fetchData(url, compressionType).catch(reason => {
|
|
|
|
throw new Error(
|
|
|
|
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-07 19:55:17 +09:00
|
|
|
* @ignore
|
2021-06-07 18:45:16 +09:00
|
|
|
*/
|
|
|
|
_fetchData(url, compressionType) {
|
|
|
|
unreachable("Abstract method `_fetchData` called.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BaseStandardFontDataFactory {
|
|
|
|
constructor({ baseUrl = null }) {
|
|
|
|
if (this.constructor === BaseStandardFontDataFactory) {
|
|
|
|
unreachable("Cannot initialize BaseStandardFontDataFactory.");
|
|
|
|
}
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetch({ filename }) {
|
|
|
|
if (!this.baseUrl) {
|
|
|
|
throw new Error(
|
|
|
|
'The standard font "baseUrl" parameter must be specified, ensure that ' +
|
|
|
|
'the "standardFontDataUrl" API parameter is provided.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!filename) {
|
|
|
|
throw new Error("Font filename must be specified.");
|
|
|
|
}
|
2021-06-09 03:50:31 +09:00
|
|
|
const url = `${this.baseUrl}${filename}`;
|
2021-06-07 18:45:16 +09:00
|
|
|
|
|
|
|
return this._fetchData(url).catch(reason => {
|
|
|
|
throw new Error(`Unable to load font data at: ${url}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-07 19:55:17 +09:00
|
|
|
* @ignore
|
2021-06-07 18:45:16 +09:00
|
|
|
*/
|
|
|
|
_fetchData(url) {
|
|
|
|
unreachable("Abstract method `_fetchData` called.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 23:50:57 +09:00
|
|
|
class BaseSVGFactory {
|
|
|
|
constructor() {
|
|
|
|
if (this.constructor === BaseSVGFactory) {
|
|
|
|
unreachable("Cannot initialize BaseSVGFactory.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:01:55 +09:00
|
|
|
create(width, height, skipDimensions = false) {
|
2021-06-11 23:50:57 +09:00
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
throw new Error("Invalid SVG dimensions");
|
|
|
|
}
|
|
|
|
const svg = this._createSVG("svg:svg");
|
|
|
|
svg.setAttribute("version", "1.1");
|
2022-06-13 18:01:55 +09:00
|
|
|
|
|
|
|
if (!skipDimensions) {
|
|
|
|
svg.setAttribute("width", `${width}px`);
|
|
|
|
svg.setAttribute("height", `${height}px`);
|
|
|
|
}
|
|
|
|
|
2021-06-11 23:50:57 +09:00
|
|
|
svg.setAttribute("preserveAspectRatio", "none");
|
|
|
|
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
|
|
|
|
|
|
return svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
createElement(type) {
|
|
|
|
if (typeof type !== "string") {
|
|
|
|
throw new Error("Invalid SVG element type");
|
|
|
|
}
|
|
|
|
return this._createSVG(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-07 19:55:17 +09:00
|
|
|
* @ignore
|
2021-06-11 23:50:57 +09:00
|
|
|
*/
|
|
|
|
_createSVG(type) {
|
|
|
|
unreachable("Abstract method `_createSVG` called.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 18:45:16 +09:00
|
|
|
export {
|
|
|
|
BaseCanvasFactory,
|
|
|
|
BaseCMapReaderFactory,
|
[api-minor] Extend general transfer function support to browsers without `OffscreenCanvas`
This patch extends PR 16115 to work in all browsers, regardless of their `OffscreenCanvas` support, such that transfer functions will be applied to general rendering (and not just image data).
In order to do this we introduce the `BaseFilterFactory` that is then extended in browsers/Node.js environments, similar to all the other factories used in the API, such that we always have the necessary factory available in `src/display/canvas.js`.
These changes help simplify the existing `putBinaryImageData` function, and the new method can easily be stubbed-out in the Firefox PDF Viewer.
*Please note:* This patch removes the old *partial* transfer function support, which only applied to image data, from Node.js environments since the `node-canvas` package currently doesn't support filters. However, this should hopefully be fine given that:
- Transfer functions are not very commonly used in PDF documents.
- Browsers in general, and Firefox in particular, are the *primary* development target for the PDF.js library.
- The FAQ only lists Node.js as *mostly* supported, see https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
2023-03-12 23:47:01 +09:00
|
|
|
BaseFilterFactory,
|
2021-06-07 18:45:16 +09:00
|
|
|
BaseStandardFontDataFactory,
|
2021-06-11 23:50:57 +09:00
|
|
|
BaseSVGFactory,
|
2021-06-07 18:45:16 +09:00
|
|
|
};
|