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.
This commit is contained in:
Jonas Jenwald 2022-05-01 09:56:27 +02:00
parent c2488c7864
commit e658acffbc

View File

@ -451,24 +451,29 @@ function drawImageAtIntegerCoords(
} }
function compileType3Glyph(imgData) { function compileType3Glyph(imgData) {
const { width, height } = imgData;
if (
!COMPILE_TYPE3_GLYPHS ||
width > MAX_SIZE_TO_COMPILE ||
height > MAX_SIZE_TO_COMPILE
) {
return null;
}
const POINT_TO_PROCESS_LIMIT = 1000; const POINT_TO_PROCESS_LIMIT = 1000;
const POINT_TYPES = new Uint8Array([ const POINT_TYPES = new Uint8Array([
0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0, 0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0,
]); ]);
const width = imgData.width, const width1 = width + 1,
height = imgData.height, points = new Uint8Array(width1 * (height + 1));
width1 = width + 1; let i, j, j0;
let i, ii, j, j0;
const points = new Uint8Array(width1 * (height + 1));
// decodes bit-packed mask data // decodes bit-packed mask data
const lineSize = (width + 7) & ~7, const lineSize = (width + 7) & ~7,
data0 = imgData.data; data = new Uint8Array(lineSize * height);
const data = new Uint8Array(lineSize * height);
let pos = 0; let pos = 0;
for (i = 0, ii = data0.length; i < ii; i++) { for (const elem of imgData.data) {
const elem = data0[i];
let mask = 128; let mask = 128;
while (mask > 0) { while (mask > 0) {
data[pos++] = elem & mask ? 0 : 255; data[pos++] = elem & mask ? 0 : 255;
@ -2987,28 +2992,22 @@ class CanvasGraphics {
if (!this.contentVisible) { if (!this.contentVisible) {
return; return;
} }
const count = img.count; const count = img.count;
img = this.getObject(img.data, img); img = this.getObject(img.data, img);
img.count = count; img.count = count;
const ctx = this.ctx; const ctx = this.ctx;
const width = img.width,
height = img.height;
const glyph = this.processingType3; const glyph = this.processingType3;
if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) { if (glyph) {
if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) { if (glyph.compiled === undefined) {
glyph.compiled = compileType3Glyph({ data: img.data, width, height }); glyph.compiled = compileType3Glyph(img);
} else {
glyph.compiled = null;
} }
}
if (glyph?.compiled) { if (glyph.compiled) {
glyph.compiled(ctx); glyph.compiled(ctx);
return; return;
}
} }
const mask = this._createMaskCanvas(img); const mask = this._createMaskCanvas(img);
const maskCanvas = mask.canvas; const maskCanvas = mask.canvas;