Skip to content

Commit ba5b667

Browse files
committed
Slightly re-factor the compileType3Glyph function
This moves the `COMPILE_TYPE3_GLYPHS`/`MAX_SIZE_TO_COMPILE`-checks into the `compileType3Glyph` function itself, which allows for some simplification at the call-site. These changes also mean that the `COMPILE_TYPE3_GLYPHS`-check is now done *once* per Type3-glyph, rather than everytime that the glyph is being rendered.
1 parent 826e391 commit ba5b667

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/display/canvas.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -451,24 +451,29 @@ function drawImageAtIntegerCoords(
451451
}
452452

453453
function compileType3Glyph(imgData) {
454+
const { width, height } = imgData;
455+
if (
456+
!COMPILE_TYPE3_GLYPHS ||
457+
width > MAX_SIZE_TO_COMPILE ||
458+
height > MAX_SIZE_TO_COMPILE
459+
) {
460+
return null;
461+
}
462+
454463
const POINT_TO_PROCESS_LIMIT = 1000;
455464
const POINT_TYPES = new Uint8Array([
456465
0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0,
457466
]);
458467

459-
const width = imgData.width,
460-
height = imgData.height,
461-
width1 = width + 1;
462-
let i, ii, j, j0;
463-
const points = new Uint8Array(width1 * (height + 1));
468+
const width1 = width + 1,
469+
points = new Uint8Array(width1 * (height + 1));
470+
let i, j, j0;
464471

465472
// decodes bit-packed mask data
466473
const lineSize = (width + 7) & ~7,
467-
data0 = imgData.data;
468-
const data = new Uint8Array(lineSize * height);
474+
data = new Uint8Array(lineSize * height);
469475
let pos = 0;
470-
for (i = 0, ii = data0.length; i < ii; i++) {
471-
const elem = data0[i];
476+
for (const elem of imgData.data) {
472477
let mask = 128;
473478
while (mask > 0) {
474479
data[pos++] = elem & mask ? 0 : 255;
@@ -2987,28 +2992,22 @@ class CanvasGraphics {
29872992
if (!this.contentVisible) {
29882993
return;
29892994
}
2990-
29912995
const count = img.count;
29922996
img = this.getObject(img.data, img);
29932997
img.count = count;
29942998

29952999
const ctx = this.ctx;
2996-
const width = img.width,
2997-
height = img.height;
2998-
29993000
const glyph = this.processingType3;
30003001

3001-
if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) {
3002-
if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) {
3003-
glyph.compiled = compileType3Glyph({ data: img.data, width, height });
3004-
} else {
3005-
glyph.compiled = null;
3002+
if (glyph) {
3003+
if (glyph.compiled === undefined) {
3004+
glyph.compiled = compileType3Glyph(img);
30063005
}
3007-
}
30083006

3009-
if (glyph?.compiled) {
3010-
glyph.compiled(ctx);
3011-
return;
3007+
if (glyph.compiled) {
3008+
glyph.compiled(ctx);
3009+
return;
3010+
}
30123011
}
30133012
const mask = this._createMaskCanvas(img);
30143013
const maskCanvas = mask.canvas;

0 commit comments

Comments
 (0)