Skip to content

Commit e9e9fee

Browse files
committed
Only compile Type3 glyphs when Path2D is supported
According to MDN `Path2D` is available in all browsers that we currently support, see https://developer.mozilla.org/en-US/docs/Web/API/Path2D#browser_compatibility Hence only Node.js is currently lagging behind here, and requires that we keep the old code as a fallback in the `compileType3Glyph` function. However, there's an open PR in the `node-canvas` repository for adding `Path2D` support. As far as I'm concerned, there's two possible solutions here: - We land this patch now, since it removes unnecessary code in e.g. the Firefox PDF Viewer, which means that compilation of Type3 glyphs will be disabled in Node.js until that PR is landed.[1] If users report bugs about Type3 glyphs looking "inconsistent" in Node.js and/or being slow to render, we could perhaps encourage them to upvote and otherwise help out getting that PR landed? - We wait for the mentioned PR to land *first*, before moving forward with this patch. Given that there's been no updates on that PR for almost two months, this alternative may possibly take a while. --- [1] Note that Type3 fonts are first of all not very common in PDF documents, and secondly that compilation only applies specifically to Type3 glyphs that contain /ImageMask-data (i.e. not all Type3 fonts are affected).
1 parent ab1297f commit e9e9fee

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

src/display/canvas.js

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ const EXECUTION_TIME = 15; // ms
5454
// Defines the number of steps before checking the execution time.
5555
const EXECUTION_STEPS = 10;
5656

57-
const COMPILE_TYPE3_GLYPHS = true;
58-
const MAX_SIZE_TO_COMPILE = 1000;
57+
// To disable Type3 compilation, set the value to `-1`.
58+
const MAX_SIZE_TO_COMPILE =
59+
typeof PDFJSDev !== "undefined" &&
60+
PDFJSDev.test("GENERIC") &&
61+
isNodeJS &&
62+
typeof Path2D === "undefined"
63+
? -1
64+
: 1000;
5965

6066
const FULL_CHUNK_HEIGHT = 16;
6167

@@ -294,11 +300,7 @@ function drawImageAtIntegerCoords(
294300

295301
function compileType3Glyph(imgData) {
296302
const { width, height } = imgData;
297-
if (
298-
!COMPILE_TYPE3_GLYPHS ||
299-
width > MAX_SIZE_TO_COMPILE ||
300-
height > MAX_SIZE_TO_COMPILE
301-
) {
303+
if (width > MAX_SIZE_TO_COMPILE || height > MAX_SIZE_TO_COMPILE) {
302304
return null;
303305
}
304306

@@ -404,12 +406,7 @@ function compileType3Glyph(imgData) {
404406

405407
// building outlines
406408
const steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
407-
let path, outlines, coords;
408-
if (!isNodeJS) {
409-
path = new Path2D();
410-
} else {
411-
outlines = [];
412-
}
409+
const path = new Path2D();
413410

414411
for (i = 0; count && i <= height; i++) {
415412
let p = i * width1;
@@ -420,12 +417,7 @@ function compileType3Glyph(imgData) {
420417
if (p === end) {
421418
continue;
422419
}
423-
424-
if (path) {
425-
path.moveTo(p % width1, i);
426-
} else {
427-
coords = [p % width1, i];
428-
}
420+
path.moveTo(p % width1, i);
429421

430422
const p0 = p;
431423
let type = points[p];
@@ -448,21 +440,12 @@ function compileType3Glyph(imgData) {
448440
// set new type for "future hit"
449441
points[p] &= (type >> 2) | (type << 2);
450442
}
451-
452-
if (path) {
453-
path.lineTo(p % width1, (p / width1) | 0);
454-
} else {
455-
coords.push(p % width1, (p / width1) | 0);
456-
}
443+
path.lineTo(p % width1, (p / width1) | 0);
457444

458445
if (!points[p]) {
459446
--count;
460447
}
461448
} while (p0 !== p);
462-
463-
if (!path) {
464-
outlines.push(coords);
465-
}
466449
--i;
467450
}
468451

@@ -475,18 +458,7 @@ function compileType3Glyph(imgData) {
475458
// the path shall be painted in [0..1]x[0..1] space
476459
c.scale(1 / width, -1 / height);
477460
c.translate(0, -height);
478-
if (path) {
479-
c.fill(path);
480-
} else {
481-
c.beginPath();
482-
for (const o of outlines) {
483-
c.moveTo(o[0], o[1]);
484-
for (let l = 2, ll = o.length; l < ll; l += 2) {
485-
c.lineTo(o[l], o[l + 1]);
486-
}
487-
}
488-
c.fill();
489-
}
461+
c.fill(path);
490462
c.beginPath();
491463
c.restore();
492464
};

0 commit comments

Comments
 (0)