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