Skip to content

Commit 78de919

Browse files
committed
Improve the Pattern-detection in CanvasGraphics.stroke
The vast majority of the time, unless a Pattern is active, the `strokeColor`-property contains a "simple" colour value represented by a String. Hence it seems somewhat ridiculous to do a `hasOwnProperty` check on a String, and it's should thus be possible to improve things a tiny bit here. Unfortunately using a simple `instanceof` check would only work for `TilingPattern`s, but not for the `ShadingIRs` given how they are implemented; see `src/display/pattern_helper.js`. (While that file could probably do with some clean-up, given the age of some of its code, that probably shouldn't happen here.) Finally, the `this.type = "Pattern"`-property of the various Shadings/TilingPatterns were removed, since I cannot see why it's necessary when we can simply check for a `getPattern` method instead. Note that part of this code even pre-dates the main/worker-thread split, which probably in part explains why it looks the way it does.
1 parent be0794c commit 78de919

File tree

2 files changed

+1
-9
lines changed

2 files changed

+1
-9
lines changed

src/display/canvas.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,11 +1350,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
13501350
// stroking alpha.
13511351
ctx.globalAlpha = this.current.strokeAlpha;
13521352
if (this.contentVisible) {
1353-
if (
1354-
strokeColor &&
1355-
strokeColor.hasOwnProperty("type") &&
1356-
strokeColor.type === "Pattern"
1357-
) {
1353+
if (typeof strokeColor === "object" && strokeColor?.getPattern) {
13581354
// for patterns, we transform to pattern space, calculate
13591355
// the pattern, call stroke, and restore to user space
13601356
ctx.save();

src/display/pattern_helper.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ ShadingIRs.RadialAxial = {
3838
const r0 = raw[6];
3939
const r1 = raw[7];
4040
return {
41-
type: "Pattern",
4241
getPattern: function RadialAxial_getPattern(ctx) {
4342
applyBoundingBox(ctx, bbox);
4443
let grad;
@@ -340,7 +339,6 @@ ShadingIRs.Mesh = {
340339
const bbox = raw[7];
341340
const background = raw[8];
342341
return {
343-
type: "Pattern",
344342
getPattern: function Mesh_getPattern(ctx, owner, shadingFill) {
345343
applyBoundingBox(ctx, bbox);
346344
let scale;
@@ -390,7 +388,6 @@ ShadingIRs.Mesh = {
390388
ShadingIRs.Dummy = {
391389
fromIR: function Dummy_fromIR() {
392390
return {
393-
type: "Pattern",
394391
getPattern: function Dummy_fromIR_getPattern() {
395392
return "hotpink";
396393
},
@@ -429,7 +426,6 @@ const TilingPattern = (function TilingPatternClosure() {
429426
this.color = color;
430427
this.canvasGraphicsFactory = canvasGraphicsFactory;
431428
this.baseTransform = baseTransform;
432-
this.type = "Pattern";
433429
this.ctx = ctx;
434430
}
435431

0 commit comments

Comments
 (0)