Convert src/core/pattern.js to use standard classes

Note that this patch only covers `Pattern` and `MeshStreamReader`, since the `Shadings`-implementation required additional re-factoring.
This commit is contained in:
Jonas Jenwald 2021-05-14 16:24:49 +02:00
parent 612b43852b
commit 6acb2db4be

View File

@ -36,22 +36,12 @@ const ShadingType = {
TENSOR_PATCH_MESH: 7, TENSOR_PATCH_MESH: 7,
}; };
const Pattern = (function PatternClosure() { class Pattern {
// Constructor should define this.getPattern constructor() {
// eslint-disable-next-line no-shadow unreachable("Cannot initialize Pattern.");
function Pattern() {
unreachable("should not call Pattern constructor");
} }
Pattern.prototype = { static parseShading(
// Input: current Canvas context
// Output: the appropriate fillStyle or strokeStyle
getPattern: function Pattern_getPattern(ctx) {
unreachable(`Should not call Pattern.getStyle: ${ctx}`);
},
};
Pattern.parseShading = function (
shading, shading,
matrix, matrix,
xref, xref,
@ -67,7 +57,6 @@ const Pattern = (function PatternClosure() {
switch (type) { switch (type) {
case ShadingType.AXIAL: case ShadingType.AXIAL:
case ShadingType.RADIAL: case ShadingType.RADIAL:
// Both radial and axial shadings are handled by RadialAxial shading.
return new Shadings.RadialAxial( return new Shadings.RadialAxial(
dict, dict,
matrix, matrix,
@ -101,9 +90,8 @@ const Pattern = (function PatternClosure() {
warn(ex); warn(ex);
return new Shadings.Dummy(); return new Shadings.Dummy();
} }
}; }
return Pattern; }
})();
const Shadings = {}; const Shadings = {};
@ -263,20 +251,21 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
// All mesh shading. For now, they will be presented as set of the triangles // All mesh shading. For now, they will be presented as set of the triangles
// to be drawn on the canvas and rgb color for each vertex. // to be drawn on the canvas and rgb color for each vertex.
Shadings.Mesh = (function MeshClosure() { Shadings.Mesh = (function MeshClosure() {
function MeshStreamReader(stream, context) { class MeshStreamReader {
this.stream = stream; constructor(stream, context) {
this.context = context; this.stream = stream;
this.buffer = 0; this.context = context;
this.bufferLength = 0; this.buffer = 0;
this.bufferLength = 0;
const numComps = context.numComps;
this.tmpCompsBuf = new Float32Array(numComps);
const csNumComps = context.colorSpace.numComps;
this.tmpCsCompsBuf = context.colorFn
? new Float32Array(csNumComps)
: this.tmpCompsBuf;
}
const numComps = context.numComps;
this.tmpCompsBuf = new Float32Array(numComps);
const csNumComps = context.colorSpace.numComps;
this.tmpCsCompsBuf = context.colorFn
? new Float32Array(csNumComps)
: this.tmpCompsBuf;
}
MeshStreamReader.prototype = {
get hasData() { get hasData() {
if (this.stream.end) { if (this.stream.end) {
return this.stream.pos < this.stream.end; return this.stream.pos < this.stream.end;
@ -291,8 +280,9 @@ Shadings.Mesh = (function MeshClosure() {
this.buffer = nextByte; this.buffer = nextByte;
this.bufferLength = 8; this.bufferLength = 8;
return true; return true;
}, }
readBits: function MeshStreamReader_readBits(n) {
readBits(n) {
let buffer = this.buffer; let buffer = this.buffer;
let bufferLength = this.bufferLength; let bufferLength = this.bufferLength;
if (n === 32) { if (n === 32) {
@ -329,15 +319,18 @@ Shadings.Mesh = (function MeshClosure() {
this.bufferLength = bufferLength; this.bufferLength = bufferLength;
this.buffer = buffer & ((1 << bufferLength) - 1); this.buffer = buffer & ((1 << bufferLength) - 1);
return buffer >> bufferLength; return buffer >> bufferLength;
}, }
align: function MeshStreamReader_align() {
align() {
this.buffer = 0; this.buffer = 0;
this.bufferLength = 0; this.bufferLength = 0;
}, }
readFlag: function MeshStreamReader_readFlag() {
readFlag() {
return this.readBits(this.context.bitsPerFlag); return this.readBits(this.context.bitsPerFlag);
}, }
readCoordinate: function MeshStreamReader_readCoordinate() {
readCoordinate() {
const bitsPerCoordinate = this.context.bitsPerCoordinate; const bitsPerCoordinate = this.context.bitsPerCoordinate;
const xi = this.readBits(bitsPerCoordinate); const xi = this.readBits(bitsPerCoordinate);
const yi = this.readBits(bitsPerCoordinate); const yi = this.readBits(bitsPerCoordinate);
@ -350,8 +343,9 @@ Shadings.Mesh = (function MeshClosure() {
xi * scale * (decode[1] - decode[0]) + decode[0], xi * scale * (decode[1] - decode[0]) + decode[0],
yi * scale * (decode[3] - decode[2]) + decode[2], yi * scale * (decode[3] - decode[2]) + decode[2],
]; ];
}, }
readComponents: function MeshStreamReader_readComponents() {
readComponents() {
const numComps = this.context.numComps; const numComps = this.context.numComps;
const bitsPerComponent = this.context.bitsPerComponent; const bitsPerComponent = this.context.bitsPerComponent;
const scale = const scale =
@ -369,8 +363,8 @@ Shadings.Mesh = (function MeshClosure() {
this.context.colorFn(components, 0, color, 0); this.context.colorFn(components, 0, color, 0);
} }
return this.context.colorSpace.getRgb(color, 0); return this.context.colorSpace.getRgb(color, 0);
}, }
}; }
function decodeType4Shading(mesh, reader) { function decodeType4Shading(mesh, reader) {
const coords = mesh.coords; const coords = mesh.coords;