1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 08:38:06 +02:00

Merge pull request #19031 from Snuffleupagus/api-isImageDecoderSupported

[api-minor] Add a `getDocument` option to disable `ImageDecoder` usage
This commit is contained in:
Jonas Jenwald 2024-11-13 09:19:05 +01:00 committed by GitHub
commit 9bf9bbda0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 50 additions and 31 deletions

View file

@ -72,6 +72,7 @@ import { getGlyphsUnicode } from "./glyphlist.js";
import { getMetrics } from "./metrics.js";
import { getUnicodeForGlyph } from "./unicode.js";
import { ImageResizer } from "./image_resizer.js";
import { JpegStream } from "./jpeg_stream.js";
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
import { OperatorList } from "./operator_list.js";
import { PDFImage } from "./image.js";
@ -83,6 +84,7 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
ignoreErrors: false,
isEvalSupported: true,
isOffscreenCanvasSupported: false,
isImageDecoderSupported: false,
isChrome: false,
canvasMaxAreaInBytes: -1,
fontExtraProperties: false,
@ -233,14 +235,9 @@ class PartialEvaluator {
this._regionalImageCache = new RegionalImageCache();
this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
ImageResizer.setMaxArea(this.options.canvasMaxAreaInBytes);
} else {
ImageResizer.setOptions({
isChrome: this.options.isChrome,
maxArea: this.options.canvasMaxAreaInBytes,
});
}
ImageResizer.setOptions(this.options);
JpegStream.setOptions(this.options);
}
/**

View file

@ -34,7 +34,7 @@ const MAX_ERROR = 128;
class ImageResizer {
static #goodSquareLength = MIN_IMAGE_DIM;
static #isChrome = false;
static #isImageDecoderSupported = FeatureTest.isImageDecoderSupported;
constructor(imgData, isMask) {
this._imgData = imgData;
@ -42,15 +42,12 @@ class ImageResizer {
}
static get canUseImageDecoder() {
// TODO: remove the isChrome, once Chrome doesn't crash anymore with
// issue6741.pdf.
// https://issues.chromium.org/issues/374807001.
return shadow(
this,
"canUseImageDecoder",
this.#isChrome || typeof ImageDecoder === "undefined"
? Promise.resolve(false)
: ImageDecoder.isTypeSupported("image/bmp")
this.#isImageDecoderSupported
? 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) {
// Divide by 4 to have the value in pixels.
this.MAX_AREA = area >> 2;
this.MAX_AREA = canvasMaxAreaInBytes >> 2;
}
}
static setOptions(opts) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: setOptions");
}
this.setMaxArea(opts.maxArea ?? -1);
this.#isChrome = opts.isChrome ?? false;
// TODO: remove the isChrome, once Chrome doesn't crash anymore with
// issue6741.pdf.
// https://issues.chromium.org/issues/374807001.
this.#isImageDecoderSupported = isImageDecoderSupported && !isChrome;
}
static _areGoodDims(width, height) {

View file

@ -13,7 +13,7 @@
* 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 { Dict } from "./primitives.js";
import { JpegImage } from "./jpg.js";
@ -23,6 +23,8 @@ import { JpegImage } from "./jpg.js";
* like all the other DecodeStreams.
*/
class JpegStream extends DecodeStream {
static #isImageDecoderSupported = FeatureTest.isImageDecoderSupported;
constructor(stream, maybeLength, params) {
super(maybeLength);
@ -36,12 +38,16 @@ class JpegStream extends DecodeStream {
return shadow(
this,
"canUseImageDecoder",
typeof ImageDecoder === "undefined"
? Promise.resolve(false)
: ImageDecoder.isTypeSupported("image/jpeg")
this.#isImageDecoderSupported
? ImageDecoder.isTypeSupported("image/jpeg")
: Promise.resolve(false)
);
}
static setOptions({ isImageDecoderSupported = false }) {
this.#isImageDecoderSupported = isImageDecoderSupported;
}
get bytes() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));

View file

@ -48,10 +48,12 @@ class BasePdfManager {
this._password = args.password;
this.enableXfa = args.enableXfa;
// Check `OffscreenCanvas` support once, rather than repeatedly throughout
// the worker-thread code.
// Check `OffscreenCanvas` and `ImageDecoder` support once,
// rather than repeatedly throughout the worker-thread code.
args.evaluatorOptions.isOffscreenCanvasSupported &&=
FeatureTest.isOffscreenCanvasSupported;
args.evaluatorOptions.isImageDecoderSupported &&=
FeatureTest.isImageDecoderSupported;
this.evaluatorOptions = Object.freeze(args.evaluatorOptions);
}

View file

@ -177,6 +177,10 @@ const DefaultStandardFontDataFactory =
* `OffscreenCanvas` 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} [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.
* NOTE: Temporary option until [https://issues.chromium.org/issues/374807001]
* is fixed.
@ -284,6 +288,10 @@ function getDocument(src = {}) {
typeof src.isOffscreenCanvasSupported === "boolean"
? src.isOffscreenCanvasSupported
: !isNodeJS;
const isImageDecoderSupported =
typeof src.isImageDecoderSupported === "boolean"
? src.isImageDecoderSupported
: !isNodeJS;
const isChrome =
typeof src.isChrome === "boolean"
? src.isChrome
@ -395,6 +403,7 @@ function getDocument(src = {}) {
ignoreErrors,
isEvalSupported,
isOffscreenCanvasSupported,
isImageDecoderSupported,
isChrome,
canvasMaxAreaInBytes,
fontExtraProperties,

View file

@ -623,6 +623,14 @@ class FeatureTest {
);
}
static get isImageDecoderSupported() {
return shadow(
this,
"isImageDecoderSupported",
typeof ImageDecoder !== "undefined"
);
}
static get platform() {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||