Compare commits

...

4 Commits

Author SHA1 Message Date
Tim van der Meij
e75a7cfd62
Merge pull request #21441 from Snuffleupagus/JpegImage-isSourcePDF-conditional
Re-factor the `isSourcePDF` handling in the `JpegImage` class
2026-06-13 12:48:00 +02:00
Tim van der Meij
5f8f6b1e40
Merge pull request #21439 from Snuffleupagus/more-getOrInsertComputed
Use `Map.prototype.getOrInsertComputed()` more in the code-base
2026-06-13 12:44:51 +02:00
Jonas Jenwald
ec1e94423b Re-factor the isSourcePDF handling in the JpegImage class
This functionality was added specifically for the standalone image-decoders, and by utilizing the pre-processor we can reduce the amount of "unnecessary" code in the regular builds.

Also, shorten a few loop variables a little bit since less code is always good.
2026-06-13 10:59:02 +02:00
Jonas Jenwald
ffa7ac7a91 Use Map.prototype.getOrInsertComputed() more in the code-base 2026-06-12 23:21:16 +02:00
9 changed files with 56 additions and 63 deletions

View File

@ -346,13 +346,13 @@ class ChunkedStreamManager {
const chunksToRequest = [];
for (const chunk of chunksNeeded) {
let requestIds = this._requestsByChunk.get(chunk);
if (!requestIds) {
requestIds = [];
this._requestsByChunk.set(chunk, requestIds);
chunksToRequest.push(chunk);
}
const requestIds = this._requestsByChunk.getOrInsertComputed(
chunk,
() => {
chunksToRequest.push(chunk);
return [];
}
);
requestIds.push(requestId);
}

View File

@ -37,7 +37,7 @@ import {
RefSetCache,
} from "../primitives.js";
import { incrementalUpdate, writeValue } from "../writer.js";
import { isArrayEqual, stringToBytes } from "../../shared/util.js";
import { isArrayEqual, makeArr, stringToBytes } from "../../shared/util.js";
import { NameTree, NumberTree } from "../name_number_tree.js";
import { stringToAsciiOrUTF16BE, stringToPDFString } from "../string_utils.js";
import { AnnotationFactory } from "../annotation.js";
@ -514,20 +514,15 @@ class PDFEditor {
const bytes = this.#rawStreamBytes(stream);
const key = this.#resourceStreamKey(dictStr, bytes);
let bucket = this.#resourceStreamCache.get(key);
if (bucket) {
// Same key only means "maybe equal": confirm with an exact comparison.
for (const entry of bucket) {
if (
entry.dictStr === dictStr &&
isArrayEqual(this.#rawStreamBytes(entry.stream), bytes)
) {
return entry.ref;
}
const bucket = this.#resourceStreamCache.getOrInsertComputed(key, makeArr);
// Same key only means "maybe equal": confirm with an exact comparison.
for (const entry of bucket) {
if (
entry.dictStr === dictStr &&
isArrayEqual(this.#rawStreamBytes(entry.stream), bytes)
) {
return entry.ref;
}
} else {
bucket = [];
this.#resourceStreamCache.set(key, bucket);
}
const ref = this.newRef;
this.xref[ref.num] = stream;

View File

@ -128,7 +128,6 @@ class JpegStream extends DecodeStream {
height: this.drawHeight,
forceRGBA: this.forceRGBA,
forceRGB: this.forceRGB,
isSourcePDF: true,
});
this.buffer = data;
this.bufferLength = data.length;

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { assert, BaseException, warn } from "../shared/util.js";
import { BaseException, warn } from "../shared/util.js";
import { ColorSpaceUtils } from "./colorspace_utils.js";
import { DeviceCmykCS } from "./colorspace.js";
import { grayToRGBA } from "../shared/image_utils.js";
@ -1203,7 +1203,7 @@ class JpegImage {
return undefined;
}
_getLinearizedBlockData(width, height, isSourcePDF = false) {
#getLinearizedBlockData(width, height, isSourcePDF) {
const scaleX = this.width / width,
scaleY = this.height / height;
@ -1257,11 +1257,18 @@ class JpegImage {
//
// Unfortunately it's not (always) possible to tell, from the image data
// alone, if it needs to be inverted. Thus in an attempt to provide better
// out-of-box behaviour when `JpegImage` is used standalone, default to
// out-of-the-box behaviour when `JpegImage` is used standalone, default to
// inverting JPEG (CMYK) images if and only if the image data does *not*
// come from a PDF file and no `decodeTransform` was passed by the user.
if (!isSourcePDF && numComponents === 4 && !transform) {
transform = new Int32Array([-256, 255, -256, 255, -256, 255, -256, 255]);
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("IMAGE_DECODERS") &&
!isSourcePDF &&
numComponents === 4
) {
transform ||= new Int32Array([
-256, 255, -256, 255, -256, 255, -256, 255,
]);
}
if (transform) {
@ -1308,7 +1315,7 @@ class JpegImage {
_convertYccToRgb(data) {
let Y, Cb, Cr;
for (let i = 0, length = data.length; i < length; i += 3) {
for (let i = 0, ii = data.length; i < ii; i += 3) {
Y = data[i];
Cb = data[i + 1];
Cr = data[i + 2];
@ -1320,7 +1327,7 @@ class JpegImage {
}
_convertYccToRgba(data, out) {
for (let i = 0, j = 0, length = data.length; i < length; i += 3, j += 4) {
for (let i = 0, j = 0, ii = data.length; i < ii; i += 3, j += 4) {
const Y = data[i];
const Cb = data[i + 1];
const Cr = data[i + 2];
@ -1344,7 +1351,7 @@ class JpegImage {
_convertYcckToCmyk(data) {
let Y, Cb, Cr;
for (let i = 0, length = data.length; i < length; i += 4) {
for (let i = 0, ii = data.length; i < ii; i += 4) {
Y = data[i];
Cb = data[i + 1];
Cr = data[i + 2];
@ -1379,19 +1386,14 @@ class JpegImage {
height,
forceRGBA = false,
forceRGB = false,
isSourcePDF = false,
isSourcePDF = typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("IMAGE_DECODERS"),
}) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(
isSourcePDF === true,
'JpegImage.getData: Unexpected "isSourcePDF" value for PDF files.'
);
}
if (this.numComponents > 4) {
throw new JpegError("Unsupported color mode");
}
// Type of data: Uint8ClampedArray(width * height * numComponents)
const data = this._getLinearizedBlockData(width, height, isSourcePDF);
const data = this.#getLinearizedBlockData(width, height, isSourcePDF);
if (this.numComponents === 1 && (forceRGBA || forceRGB)) {
const len = data.length * (forceRGBA ? 4 : 3);

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { assert, shadow, unreachable } from "../shared/util.js";
import { assert, makeArr, shadow, unreachable } from "../shared/util.js";
const CIRCULAR_REF = Symbol("CIRCULAR_REF");
const EOF = Symbol("EOF");
@ -242,11 +242,9 @@ class Dict {
continue;
}
for (const [key, value] of dict.getRawEntries()) {
let property = properties.get(key);
if (property === undefined) {
property = [];
properties.set(key, property);
} else if (!mergeSubDicts || !(value instanceof Dict)) {
const property = properties.getOrInsertComputed(key, makeArr);
if (property.length && !(mergeSubDicts && value instanceof Dict)) {
// Ignore additional entries, if either:
// - This is a "shallow" merge, where only the first element matters.
// - The value is *not* a `Dict`, since other types cannot be merged.

View File

@ -241,9 +241,10 @@ class AnnotationStorage {
continue;
}
const { type } = editorStats;
if (!typeToEditor.has(type)) {
typeToEditor.set(type, Object.getPrototypeOf(value).constructor);
}
typeToEditor.getOrInsertComputed(
type,
() => Object.getPrototypeOf(value).constructor
);
stats ||= Object.create(null);
const map = (stats[type] ||= new Map());
for (const [key, val] of Object.entries(editorStats)) {

View File

@ -23,6 +23,7 @@ import {
FONT_IDENTITY_MATRIX,
ImageKind,
info,
makeArr,
makeMap,
OPS,
shadow,
@ -1507,11 +1508,10 @@ class CanvasGraphics {
}
let knockoutFilter = "none";
if (needsAlphaScaling && this.#knockoutFilterCache instanceof Map) {
knockoutFilter = this.#knockoutFilterCache.get(alpha);
if (!knockoutFilter) {
knockoutFilter = this.filterFactory.addKnockoutFilter(alpha);
this.#knockoutFilterCache.set(alpha, knockoutFilter);
}
knockoutFilter = this.#knockoutFilterCache.getOrInsertComputed(
alpha,
() => this.filterFactory.addKnockoutFilter(alpha)
);
}
if (!needsAlphaScaling || knockoutFilter !== "none") {
@ -3682,11 +3682,10 @@ class CanvasGraphics {
);
const { canvas, context } = this.annotationCanvas;
if (canvasName) {
let canvases = this.annotationCanvasMap.get(id);
if (!canvases) {
canvases = [];
this.annotationCanvasMap.set(id, canvases);
}
const canvases = this.annotationCanvasMap.getOrInsertComputed(
id,
makeArr
);
canvas.setAttribute("data-canvas-name", canvasName);
// Replace any same-named canvas from a previous render so stale
// low-resolution canvases don't pile up across zooms.

View File

@ -23,6 +23,7 @@ import {
AnnotationEditorType,
FeatureTest,
getUuid,
makeArr,
shadow,
SVG_NS,
Util,
@ -550,7 +551,7 @@ class KeyboardManager {
continue;
}
this.callbacks
.getOrInsertComputed(keyName, () => [])
.getOrInsertComputed(keyName, makeArr)
.push({ callback, options, modifiers });
}
}

View File

@ -459,11 +459,9 @@ class Stepper {
const dependents = new Map();
for (const [dependentIdx, { dependencies: ownDependencies }] of metadata) {
for (const dependencyIdx of ownDependencies) {
let ownDependents = dependents.get(dependencyIdx);
if (!ownDependents) {
dependents.set(dependencyIdx, (ownDependents = new Set()));
}
ownDependents.add(dependentIdx);
dependents
.getOrInsertComputed(dependencyIdx, () => new Set())
.add(dependentIdx);
}
}