Convert src/display/pattern_helper.js to use standard classes

Note that this patch only covers `TilingPattern`, since the `ShadingIRs`-implementation required additional re-factoring.
This commit is contained in:
Jonas Jenwald 2021-05-15 13:02:40 +02:00
parent 438cf1e438
commit 40939d5955

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { FormatError, info, Util } from "../shared/util.js";
import { FormatError, info, shadow, Util } from "../shared/util.js";
const ShadingIRs = {};
@ -430,19 +430,18 @@ function getShadingPatternFromIR(raw) {
return shadingIR.fromIR(raw);
}
/**
* @type {any}
*/
const TilingPattern = (function TilingPatternClosure() {
const PaintType = {
const PaintType = {
COLORED: 1,
UNCOLORED: 2,
};
};
const MAX_PATTERN_SIZE = 3000; // 10in @ 300dpi shall be enough
class TilingPattern {
// 10in @ 300dpi shall be enough.
static get MAX_PATTERN_SIZE() {
return shadow(this, "MAX_PATTERN_SIZE", 3000);
}
// eslint-disable-next-line no-shadow
function TilingPattern(IR, color, ctx, canvasGraphicsFactory, baseTransform) {
constructor(IR, color, ctx, canvasGraphicsFactory, baseTransform) {
this.operatorList = IR[2];
this.matrix = IR[3] || [1, 0, 0, 1, 0, 0];
this.bbox = IR[4];
@ -451,13 +450,12 @@ const TilingPattern = (function TilingPatternClosure() {
this.paintType = IR[7];
this.tilingType = IR[8];
this.color = color;
this.ctx = ctx;
this.canvasGraphicsFactory = canvasGraphicsFactory;
this.baseTransform = baseTransform;
this.ctx = ctx;
}
TilingPattern.prototype = {
createPatternCanvas: function TilinPattern_createPatternCanvas(owner) {
createPatternCanvas(owner) {
const operatorList = this.operatorList;
const bbox = this.bbox;
const xstep = this.xstep;
@ -545,20 +543,16 @@ const TilingPattern = (function TilingPatternClosure() {
scaleX: dimx.scale,
scaleY: dimy.scale,
};
},
}
getSizeAndScale: function TilingPattern_getSizeAndScale(
step,
realOutputSize,
scale
) {
getSizeAndScale(step, realOutputSize, scale) {
// xstep / ystep may be negative -- normalize.
step = Math.abs(step);
// MAX_PATTERN_SIZE is used to avoid OOM situation.
// Use the destination canvas's size if it is bigger than the hard-coded
// limit of MAX_PATTERN_SIZE to avoid clipping patterns that cover the
// whole canvas.
const maxSize = Math.max(MAX_PATTERN_SIZE, realOutputSize);
const maxSize = Math.max(TilingPattern.MAX_PATTERN_SIZE, realOutputSize);
let size = Math.ceil(step * scale);
if (size >= maxSize) {
size = maxSize;
@ -566,9 +560,9 @@ const TilingPattern = (function TilingPatternClosure() {
scale = size / step;
}
return { scale, size };
},
}
clipBbox: function clipBbox(graphics, bbox, x0, y0, x1, y1) {
clipBbox(graphics, bbox, x0, y0, x1, y1) {
if (Array.isArray(bbox) && bbox.length === 4) {
const bboxWidth = x1 - x0;
const bboxHeight = y1 - y0;
@ -576,13 +570,9 @@ const TilingPattern = (function TilingPatternClosure() {
graphics.clip();
graphics.endPath();
}
},
}
setFillAndStrokeStyleToContext: function setFillAndStrokeStyleToContext(
graphics,
paintType,
color
) {
setFillAndStrokeStyleToContext(graphics, paintType, color) {
const context = graphics.ctx,
current = graphics.current;
switch (paintType) {
@ -604,9 +594,9 @@ const TilingPattern = (function TilingPatternClosure() {
default:
throw new FormatError(`Unsupported paint type: ${paintType}`);
}
},
}
getPattern: function TilingPattern_getPattern(ctx, owner, shadingFill) {
getPattern(ctx, owner, shadingFill) {
ctx = this.ctx;
// PDF spec 8.7.2 NOTE 1: pattern's matrix is relative to initial matrix.
let matrix = ctx.mozCurrentTransformInverse;
@ -627,17 +617,11 @@ const TilingPattern = (function TilingPatternClosure() {
1 / temporaryPatternCanvas.scaleY
);
const pattern = ctx.createPattern(
temporaryPatternCanvas.canvas,
"repeat"
);
const pattern = ctx.createPattern(temporaryPatternCanvas.canvas, "repeat");
pattern.setTransform(domMatrix);
return pattern;
},
};
return TilingPattern;
})();
}
}
export { getShadingPatternFromIR, TilingPattern };