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