Remove the remaining closure in the src/display/canvas.js file

For e.g. the `gulp mozcentral` command, the *built* `pdf.js` file decreases from `304 607` to `301 295` bytes with this patch. The improvement comes mostly from having less overall indentation in the code.
This commit is contained in:
Jonas Jenwald 2021-07-25 12:55:51 +02:00
parent 156762c482
commit fbaafdc4e8

View File

@ -37,6 +37,12 @@ const MIN_FONT_SIZE = 16;
const MAX_FONT_SIZE = 100;
const MAX_GROUP_SIZE = 4096;
// Defines the time the `executeOperatorList`-method is going to be executing
// before it stops and shedules a continue of execution.
const EXECUTION_TIME = 15; // ms
// Defines the number of steps before checking the execution time.
const EXECUTION_STEPS = 10;
const COMPILE_TYPE3_GLYPHS = true;
const MAX_SIZE_TO_COMPILE = 1000;
@ -442,17 +448,7 @@ class CanvasExtraState {
}
}
/**
* @type {any}
*/
const CanvasGraphics = (function CanvasGraphicsClosure() {
// Defines the time the executeOperatorList is going to be executing
// before it stops and shedules a continue of execution.
const EXECUTION_TIME = 15;
// Defines the number of steps before checking the execution time
const EXECUTION_STEPS = 10;
function putBinaryImageData(ctx, imgData, transferMaps = null) {
function putBinaryImageData(ctx, imgData, transferMaps = null) {
if (typeof ImageData !== "undefined" && imgData instanceof ImageData) {
ctx.putImageData(imgData, 0, 0);
return;
@ -518,8 +514,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
}
for (i = 0; i < totalChunks; i++) {
thisChunkHeight =
i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
destPos = 0;
for (j = 0; j < thisChunkHeight; j++) {
const srcDiff = srcLength - srcPos;
@ -650,9 +645,9 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
} else {
throw new Error(`bad image kind: ${imgData.kind}`);
}
}
}
function putBinaryImageMask(ctx, imgData) {
function putBinaryImageMask(ctx, imgData) {
const height = imgData.height,
width = imgData.width;
const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
@ -686,9 +681,9 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
}
ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
}
}
}
function copyCtxState(sourceCtx, destCtx) {
function copyCtxState(sourceCtx, destCtx) {
const properties = [
"strokeStyle",
"fillStyle",
@ -711,9 +706,9 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
destCtx.setLineDash(sourceCtx.getLineDash());
destCtx.lineDashOffset = sourceCtx.lineDashOffset;
}
}
}
function resetCtxToDefault(ctx) {
function resetCtxToDefault(ctx) {
ctx.strokeStyle = "#000000";
ctx.fillStyle = "#000000";
ctx.fillRule = "nonzero";
@ -728,9 +723,9 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.setLineDash([]);
ctx.lineDashOffset = 0;
}
}
}
function composeSMaskBackdrop(bytes, r0, g0, b0) {
function composeSMaskBackdrop(bytes, r0, g0, b0) {
const length = bytes.length;
for (let i = 3; i < length; i += 4) {
const alpha = bytes[i];
@ -745,18 +740,18 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
bytes[i - 1] = (bytes[i - 1] * alpha + b0 * alpha_) >> 8;
}
}
}
}
function composeSMaskAlpha(maskData, layerData, transferMap) {
function composeSMaskAlpha(maskData, layerData, transferMap) {
const length = maskData.length;
const scale = 1 / 255;
for (let i = 3; i < length; i += 4) {
const alpha = transferMap ? transferMap[maskData[i]] : maskData[i];
layerData[i] = (layerData[i] * alpha * scale) | 0;
}
}
}
function composeSMaskLuminosity(maskData, layerData, transferMap) {
function composeSMaskLuminosity(maskData, layerData, transferMap) {
const length = maskData.length;
for (let i = 3; i < length; i += 4) {
const y =
@ -767,9 +762,9 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
? (layerData[i] * transferMap[y >> 8]) >> 8
: (layerData[i] * y) >> 16;
}
}
}
function genericComposeSMask(
function genericComposeSMask(
maskCtx,
layerCtx,
width,
@ -777,7 +772,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
subtype,
backdrop,
transferMap
) {
) {
const hasBackdrop = !!backdrop;
const r0 = hasBackdrop ? backdrop[0] : 0;
const g0 = hasBackdrop ? backdrop[1] : 0;
@ -805,9 +800,9 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
maskCtx.putImageData(layerData, 0, row);
}
}
}
function composeSMask(ctx, smask, layerCtx) {
function composeSMask(ctx, smask, layerCtx) {
const mask = smask.canvas;
const maskCtx = smask.context;
@ -830,15 +825,14 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
smask.transferMap
);
ctx.drawImage(mask, 0, 0);
}
}
const LINE_CAP_STYLES = ["butt", "round", "square"];
const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
const NORMAL_CLIP = {};
const EO_CLIP = {};
const LINE_CAP_STYLES = ["butt", "round", "square"];
const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
const NORMAL_CLIP = {};
const EO_CLIP = {};
// eslint-disable-next-line no-shadow
class CanvasGraphics {
class CanvasGraphics {
constructor(
canvasCtx,
commonObjs,
@ -913,10 +907,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
this.ctx.save();
// The transform can be applied before rendering, transferring it to
// the new canvas.
this.ctx.transform.apply(
this.ctx,
this.compositeCtx.mozCurrentTransform
);
this.ctx.transform.apply(this.ctx, this.compositeCtx.mozCurrentTransform);
}
this.ctx.save();
@ -2617,10 +2608,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
imgToPaint = tmpCanvas.canvas;
}
const scaled = this._scaleImage(
imgToPaint,
ctx.mozCurrentTransformInverse
);
const scaled = this._scaleImage(imgToPaint, ctx.mozCurrentTransformInverse);
ctx.drawImage(
scaled.img,
0,
@ -2773,10 +2761,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
const sqNorm1 = m[0] ** 2 + m[2] ** 2;
const sqNorm2 = m[1] ** 2 + m[3] ** 2;
const pixelHeight = Math.sqrt(Math.max(sqNorm1, sqNorm2)) / absDet;
if (
sqNorm1 !== sqNorm2 &&
this._combinedScaleFactor * pixelHeight > 1
) {
if (sqNorm1 !== sqNorm2 && this._combinedScaleFactor * pixelHeight > 1) {
// The parallelogram isn't a square and at least one height
// is lower than 1 so the resulting line width must be 1
// but it cannot be achieved with one scale: when scaling a pixel
@ -2816,13 +2801,10 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
}
return true;
}
}
}
for (const op in OPS) {
for (const op in OPS) {
CanvasGraphics.prototype[OPS[op]] = CanvasGraphics.prototype[op];
}
return CanvasGraphics;
})();
}
export { CanvasGraphics };