[api-minor] Add a getDocument option to disable ImageDecoder usage
This allows end-users to forcibly disable `ImageDecoder` usage, even if the browser appears to support it (similar to the pre-existing option for `OffscreenCanvas`).
This commit is contained in:
parent
fe5967c84e
commit
65eedfb0fc
@ -72,6 +72,7 @@ import { getGlyphsUnicode } from "./glyphlist.js";
|
|||||||
import { getMetrics } from "./metrics.js";
|
import { getMetrics } from "./metrics.js";
|
||||||
import { getUnicodeForGlyph } from "./unicode.js";
|
import { getUnicodeForGlyph } from "./unicode.js";
|
||||||
import { ImageResizer } from "./image_resizer.js";
|
import { ImageResizer } from "./image_resizer.js";
|
||||||
|
import { JpegStream } from "./jpeg_stream.js";
|
||||||
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
|
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
|
||||||
import { OperatorList } from "./operator_list.js";
|
import { OperatorList } from "./operator_list.js";
|
||||||
import { PDFImage } from "./image.js";
|
import { PDFImage } from "./image.js";
|
||||||
@ -83,6 +84,7 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
|
|||||||
ignoreErrors: false,
|
ignoreErrors: false,
|
||||||
isEvalSupported: true,
|
isEvalSupported: true,
|
||||||
isOffscreenCanvasSupported: false,
|
isOffscreenCanvasSupported: false,
|
||||||
|
isImageDecoderSupported: false,
|
||||||
isChrome: false,
|
isChrome: false,
|
||||||
canvasMaxAreaInBytes: -1,
|
canvasMaxAreaInBytes: -1,
|
||||||
fontExtraProperties: false,
|
fontExtraProperties: false,
|
||||||
@ -233,14 +235,9 @@ class PartialEvaluator {
|
|||||||
|
|
||||||
this._regionalImageCache = new RegionalImageCache();
|
this._regionalImageCache = new RegionalImageCache();
|
||||||
this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
|
this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|
||||||
ImageResizer.setMaxArea(this.options.canvasMaxAreaInBytes);
|
ImageResizer.setOptions(this.options);
|
||||||
} else {
|
JpegStream.setOptions(this.options);
|
||||||
ImageResizer.setOptions({
|
|
||||||
isChrome: this.options.isChrome,
|
|
||||||
maxArea: this.options.canvasMaxAreaInBytes,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -34,7 +34,7 @@ const MAX_ERROR = 128;
|
|||||||
class ImageResizer {
|
class ImageResizer {
|
||||||
static #goodSquareLength = MIN_IMAGE_DIM;
|
static #goodSquareLength = MIN_IMAGE_DIM;
|
||||||
|
|
||||||
static #isChrome = false;
|
static #isImageDecoderSupported = FeatureTest.isImageDecoderSupported;
|
||||||
|
|
||||||
constructor(imgData, isMask) {
|
constructor(imgData, isMask) {
|
||||||
this._imgData = imgData;
|
this._imgData = imgData;
|
||||||
@ -42,15 +42,12 @@ class ImageResizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static get canUseImageDecoder() {
|
static get canUseImageDecoder() {
|
||||||
// TODO: remove the isChrome, once Chrome doesn't crash anymore with
|
|
||||||
// issue6741.pdf.
|
|
||||||
// https://issues.chromium.org/issues/374807001.
|
|
||||||
return shadow(
|
return shadow(
|
||||||
this,
|
this,
|
||||||
"canUseImageDecoder",
|
"canUseImageDecoder",
|
||||||
this.#isChrome || typeof ImageDecoder === "undefined"
|
this.#isImageDecoderSupported
|
||||||
? Promise.resolve(false)
|
? ImageDecoder.isTypeSupported("image/bmp")
|
||||||
: ImageDecoder.isTypeSupported("image/bmp")
|
: Promise.resolve(false)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,19 +118,19 @@ class ImageResizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static setMaxArea(area) {
|
static setOptions({
|
||||||
|
canvasMaxAreaInBytes = -1,
|
||||||
|
isChrome = false,
|
||||||
|
isImageDecoderSupported = false,
|
||||||
|
}) {
|
||||||
if (!this._hasMaxArea) {
|
if (!this._hasMaxArea) {
|
||||||
// Divide by 4 to have the value in pixels.
|
// Divide by 4 to have the value in pixels.
|
||||||
this.MAX_AREA = area >> 2;
|
this.MAX_AREA = canvasMaxAreaInBytes >> 2;
|
||||||
}
|
}
|
||||||
}
|
// TODO: remove the isChrome, once Chrome doesn't crash anymore with
|
||||||
|
// issue6741.pdf.
|
||||||
static setOptions(opts) {
|
// https://issues.chromium.org/issues/374807001.
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
this.#isImageDecoderSupported = isImageDecoderSupported && !isChrome;
|
||||||
throw new Error("Not implemented: setOptions");
|
|
||||||
}
|
|
||||||
this.setMaxArea(opts.maxArea ?? -1);
|
|
||||||
this.#isChrome = opts.isChrome ?? false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static _areGoodDims(width, height) {
|
static _areGoodDims(width, height) {
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { shadow, warn } from "../shared/util.js";
|
import { FeatureTest, shadow, warn } from "../shared/util.js";
|
||||||
import { DecodeStream } from "./decode_stream.js";
|
import { DecodeStream } from "./decode_stream.js";
|
||||||
import { Dict } from "./primitives.js";
|
import { Dict } from "./primitives.js";
|
||||||
import { JpegImage } from "./jpg.js";
|
import { JpegImage } from "./jpg.js";
|
||||||
@ -23,6 +23,8 @@ import { JpegImage } from "./jpg.js";
|
|||||||
* like all the other DecodeStreams.
|
* like all the other DecodeStreams.
|
||||||
*/
|
*/
|
||||||
class JpegStream extends DecodeStream {
|
class JpegStream extends DecodeStream {
|
||||||
|
static #isImageDecoderSupported = FeatureTest.isImageDecoderSupported;
|
||||||
|
|
||||||
constructor(stream, maybeLength, params) {
|
constructor(stream, maybeLength, params) {
|
||||||
super(maybeLength);
|
super(maybeLength);
|
||||||
|
|
||||||
@ -36,12 +38,16 @@ class JpegStream extends DecodeStream {
|
|||||||
return shadow(
|
return shadow(
|
||||||
this,
|
this,
|
||||||
"canUseImageDecoder",
|
"canUseImageDecoder",
|
||||||
typeof ImageDecoder === "undefined"
|
this.#isImageDecoderSupported
|
||||||
? Promise.resolve(false)
|
? ImageDecoder.isTypeSupported("image/jpeg")
|
||||||
: ImageDecoder.isTypeSupported("image/jpeg")
|
: Promise.resolve(false)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static setOptions({ isImageDecoderSupported = false }) {
|
||||||
|
this.#isImageDecoderSupported = isImageDecoderSupported;
|
||||||
|
}
|
||||||
|
|
||||||
get bytes() {
|
get bytes() {
|
||||||
// If `this.maybeLength` is null, we'll get the entire stream.
|
// If `this.maybeLength` is null, we'll get the entire stream.
|
||||||
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
|
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
|
||||||
|
|||||||
@ -48,10 +48,12 @@ class BasePdfManager {
|
|||||||
this._password = args.password;
|
this._password = args.password;
|
||||||
this.enableXfa = args.enableXfa;
|
this.enableXfa = args.enableXfa;
|
||||||
|
|
||||||
// Check `OffscreenCanvas` support once, rather than repeatedly throughout
|
// Check `OffscreenCanvas` and `ImageDecoder` support once,
|
||||||
// the worker-thread code.
|
// rather than repeatedly throughout the worker-thread code.
|
||||||
args.evaluatorOptions.isOffscreenCanvasSupported &&=
|
args.evaluatorOptions.isOffscreenCanvasSupported &&=
|
||||||
FeatureTest.isOffscreenCanvasSupported;
|
FeatureTest.isOffscreenCanvasSupported;
|
||||||
|
args.evaluatorOptions.isImageDecoderSupported &&=
|
||||||
|
FeatureTest.isImageDecoderSupported;
|
||||||
this.evaluatorOptions = Object.freeze(args.evaluatorOptions);
|
this.evaluatorOptions = Object.freeze(args.evaluatorOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -177,6 +177,10 @@ const DefaultStandardFontDataFactory =
|
|||||||
* `OffscreenCanvas` in the worker. Primarily used to improve performance of
|
* `OffscreenCanvas` in the worker. Primarily used to improve performance of
|
||||||
* image conversion/rendering.
|
* image conversion/rendering.
|
||||||
* The default value is `true` in web environments and `false` in Node.js.
|
* The default value is `true` in web environments and `false` in Node.js.
|
||||||
|
* @property {boolean} [isImageDecoderSupported] - Determines if we can use
|
||||||
|
* `ImageDecoder` in the worker. Primarily used to improve performance of
|
||||||
|
* image conversion/rendering.
|
||||||
|
* The default value is `true` in web environments and `false` in Node.js.
|
||||||
* @property {boolean} [isChrome] - Determines if we can use bmp ImageDecoder.
|
* @property {boolean} [isChrome] - Determines if we can use bmp ImageDecoder.
|
||||||
* NOTE: Temporary option until [https://issues.chromium.org/issues/374807001]
|
* NOTE: Temporary option until [https://issues.chromium.org/issues/374807001]
|
||||||
* is fixed.
|
* is fixed.
|
||||||
@ -284,6 +288,10 @@ function getDocument(src = {}) {
|
|||||||
typeof src.isOffscreenCanvasSupported === "boolean"
|
typeof src.isOffscreenCanvasSupported === "boolean"
|
||||||
? src.isOffscreenCanvasSupported
|
? src.isOffscreenCanvasSupported
|
||||||
: !isNodeJS;
|
: !isNodeJS;
|
||||||
|
const isImageDecoderSupported =
|
||||||
|
typeof src.isImageDecoderSupported === "boolean"
|
||||||
|
? src.isImageDecoderSupported
|
||||||
|
: !isNodeJS;
|
||||||
const isChrome =
|
const isChrome =
|
||||||
typeof src.isChrome === "boolean"
|
typeof src.isChrome === "boolean"
|
||||||
? src.isChrome
|
? src.isChrome
|
||||||
@ -395,6 +403,7 @@ function getDocument(src = {}) {
|
|||||||
ignoreErrors,
|
ignoreErrors,
|
||||||
isEvalSupported,
|
isEvalSupported,
|
||||||
isOffscreenCanvasSupported,
|
isOffscreenCanvasSupported,
|
||||||
|
isImageDecoderSupported,
|
||||||
isChrome,
|
isChrome,
|
||||||
canvasMaxAreaInBytes,
|
canvasMaxAreaInBytes,
|
||||||
fontExtraProperties,
|
fontExtraProperties,
|
||||||
|
|||||||
@ -623,6 +623,14 @@ class FeatureTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static get isImageDecoderSupported() {
|
||||||
|
return shadow(
|
||||||
|
this,
|
||||||
|
"isImageDecoderSupported",
|
||||||
|
typeof ImageDecoder !== "undefined"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
static get platform() {
|
static get platform() {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user