From 9bc4175bdeb4837b0c0965715cbd7b43f429cece Mon Sep 17 00:00:00 2001 From: Arthur Silber Date: Thu, 11 Dec 2025 17:59:17 +0100 Subject: [PATCH] Improves text rendering performance by skipping unnecessary pattern calculations --- src/display/canvas.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/display/canvas.js b/src/display/canvas.js index 12b556f71..b364a2910 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -2062,7 +2062,20 @@ class CanvasGraphics { } let patternFillTransform, patternStrokeTransform; - if (current.patternFill) { + + // Only compute pattern transforms if the text rendering mode actually + // uses fill/stroke. This avoids expensive pattern calculations each call + // when a patternFill/patternStroke is set, but unused. + const fillStrokeMode = + current.textRenderingMode & TextRenderingMode.FILL_STROKE_MASK; + const needsFill = + fillStrokeMode === TextRenderingMode.FILL || + fillStrokeMode === TextRenderingMode.FILL_STROKE; + const needsStroke = + fillStrokeMode === TextRenderingMode.STROKE || + fillStrokeMode === TextRenderingMode.FILL_STROKE; + + if (needsFill && current.patternFill) { ctx.save(); const pattern = current.fillColor.getPattern( ctx, @@ -2076,7 +2089,7 @@ class CanvasGraphics { ctx.fillStyle = pattern; } - if (current.patternStroke) { + if (needsStroke && current.patternStroke) { ctx.save(); const pattern = current.strokeColor.getPattern( ctx, @@ -2093,12 +2106,7 @@ class CanvasGraphics { let lineWidth = current.lineWidth; const scale = current.textMatrixScale; if (scale === 0 || lineWidth === 0) { - const fillStrokeMode = - current.textRenderingMode & TextRenderingMode.FILL_STROKE_MASK; - if ( - fillStrokeMode === TextRenderingMode.STROKE || - fillStrokeMode === TextRenderingMode.FILL_STROKE - ) { + if (needsStroke) { lineWidth = this.getSinglePixelWidth(); } } else {