From 085ab36680f7d9ce8030f34f91b5d0aabb71bf7c Mon Sep 17 00:00:00 2001 From: calixteman Date: Mon, 16 Mar 2026 23:19:21 +0100 Subject: [PATCH] Don't throw when a mesh shading is degenerated The problem of throwing an error is that it removes an operation from drawing list which can cause a rendering issue. --- src/core/pattern.js | 9 +-------- src/display/pattern_helper.js | 24 ++++++++++++++---------- test/pdfs/.gitignore | 1 + test/pdfs/mesh_shading_empty.pdf | Bin 0 -> 1407 bytes test/test_manifest.json | 7 +++++++ 5 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 test/pdfs/mesh_shading_empty.pdf diff --git a/src/core/pattern.js b/src/core/pattern.js index 6059fdfcf..62f67618a 100644 --- a/src/core/pattern.js +++ b/src/core/pattern.js @@ -968,20 +968,13 @@ class MeshShading extends BaseShading { } getIR() { - const { bounds } = this; - // Ensure that the shading has non-zero width and height, to prevent errors - // in `pattern_helper.js` (fixes issue17848.pdf). - if (bounds[2] - bounds[0] === 0 || bounds[3] - bounds[1] === 0) { - throw new FormatError(`Invalid MeshShading bounds: [${bounds}].`); - } - return [ "Mesh", this.shadingType, this.coords, this.colors, this.figures, - bounds, + this.bounds, this.bbox, this.background, ]; diff --git a/src/display/pattern_helper.js b/src/display/pattern_helper.js index 5a8da30a5..f66050d1b 100644 --- a/src/display/pattern_helper.js +++ b/src/display/pattern_helper.js @@ -462,16 +462,20 @@ class MeshShadingPattern extends BaseShadingPattern { const boundsWidth = Math.ceil(this._bounds[2]) - offsetX; const boundsHeight = Math.ceil(this._bounds[3]) - offsetY; - const width = Math.min( - Math.ceil(Math.abs(boundsWidth * combinedScale[0] * EXPECTED_SCALE)), - MAX_PATTERN_SIZE - ); - const height = Math.min( - Math.ceil(Math.abs(boundsHeight * combinedScale[1] * EXPECTED_SCALE)), - MAX_PATTERN_SIZE - ); - const scaleX = boundsWidth / width; - const scaleY = boundsHeight / height; + // Ensure that the shading has non-zero width and height, to prevent errors + // in `pattern_helper.js` (fixes issue17848.pdf). + const width = + Math.min( + Math.ceil(Math.abs(boundsWidth * combinedScale[0] * EXPECTED_SCALE)), + MAX_PATTERN_SIZE + ) || 1; + const height = + Math.min( + Math.ceil(Math.abs(boundsHeight * combinedScale[1] * EXPECTED_SCALE)), + MAX_PATTERN_SIZE + ) || 1; + const scaleX = boundsWidth ? boundsWidth / width : 1; + const scaleY = boundsHeight ? boundsHeight / height : 1; const context = { coords: this._coords, diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 8e4b0a508..bcbcdd68a 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -884,3 +884,4 @@ !form_two_pages.pdf !outlines_se.pdf !radial_gradients.pdf +!mesh_shading_empty.pdf diff --git a/test/pdfs/mesh_shading_empty.pdf b/test/pdfs/mesh_shading_empty.pdf new file mode 100644 index 0000000000000000000000000000000000000000..1bc16ce7e58a061f2f590426ff19f86b3bec6f8c GIT binary patch literal 1407 zcmdT^&61Nq5YBm?qAxjW7!t^)DwRVL<3TM`S#Mi=7@@;rWtr44YptI01|EC^eUv=d z+T9Sq;0s8>FnrTr|9sQTaJ$~T(yu3Kc>Z<%eg1L&sWb#&KYvm4xdIq{`gTMZt&BGZ zn=!C88I}-|(M~NE3X7DD`TLqAmP~ncFj{fk}&bV%k^L}Guy zG&f8840i#sq9A}U3dHXYOZ(&!N^d^rM7>8>RC5gz4o_$ZvCI|=PUK+S?a?GCR;#jQ z(K1yh$rhgCmvFfQ(w3>{7cy}BVA8zEAQC@Zi199`GFb2cWW4YAXS>#a z%@%i)Dz7H@(Hg(z3GUu5d10CRa*J+dt)qCxBi#H2KOSwtf_Bl(!DJP%#>A$`+J>a; zW#r`K5uKNf`fNjwpzpEB`~!?*uA%e}&7&F&1~1jqylBEVUG)ODP;Fi}6K_ykdDVgS zZgM5{w6E-xcssVZRyK^Ol&-dH*+%$JwLP6!!qbWMq>)~iIcqCOUt8|tw(@$<;z?)E zF8V@Js?knr150yf&8GA?%4#|93(Ug)aI_Lp2WinLRt)Q=pj& znhWA68b`2)2GcW;RVZ3)N=E}<9ZVDJ8XN0AHqq48SKK)6W7DV?R|i1{^UmZ4baZIa ikDh-=n89dgEuC`QlYEqv-Z(EcY8^&uIDGSQqy7M4!*;X) literal 0 HcmV?d00001 diff --git a/test/test_manifest.json b/test/test_manifest.json index 96245b451..7dac82e9b 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -13998,5 +13998,12 @@ "md5": "80e8bed66b83928698f008c33de47edd", "rounds": 1, "type": "eq" + }, + { + "id": "mesh_shading_empty", + "file": "pdfs/mesh_shading_empty.pdf", + "md5": "5a1bf9cb73010d84b47d91bb66bae27d", + "rounds": 1, + "type": "eq" } ]