Skip to content

Commit 4adfe12

Browse files
committed
Tweak the QueueOptimizer to recognize repeated OPS.paintImageMaskXObject operators as *repeated* when the "skew" transformation matrix elements are non-zero (issue 8078)
*First of all, I should mention that my understanding of the finer details of the `QueueOptimizer` (and its related `CanvasGraphics` methods) is somewhat limited.* Hence I'm not sure if there's actually a very good reason for *only* considering ImageMasks where the "skew" transformation matrix elements are zero as *repeated*, however simply looking at the code I just don't see why these elements cannot be non-zero as long as they are *all identical* for the ImageMasks. Furthermore, looking at the *group* case (which is what we're currently falling back to), there's no particular limitation placed upon the transformation matrix elements. While this patch obviously isn't enough to *completely* fix the issue, since there should be a visible Pattern rendered as well[1], it seem (at least to me) like enough of an improvement that submitting this is justified. With these changes the referenced PDF document will no longer hang the *entire* browser, and rendering also finishes in a *reasonable* time (< 10 seconds for me) which seem fine given the *huge* number of identical inline images present.[2] --- [1] Temporarily changing the Pattern to a solid color *does* render the correct/expected area, which suggests that the remaining problem is a pre-existing issue related to the Pattern-handling itself rather than the `QueueOptimizer` functionality. [2] The document isn't exactly rendered immediately in e.g. Adobe Reader either.
1 parent 8cfdfb2 commit 4adfe12

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

src/core/operator_list.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,22 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
229229
var isSameImage = false;
230230
var iTransform, transformArgs;
231231
var firstPIMXOArg0 = argsArray[iFirstPIMXO][0];
232-
if (
233-
argsArray[iFirstTransform][1] === 0 &&
234-
argsArray[iFirstTransform][2] === 0
235-
) {
232+
const firstTransformArg0 = argsArray[iFirstTransform][0],
233+
firstTransformArg1 = argsArray[iFirstTransform][1],
234+
firstTransformArg2 = argsArray[iFirstTransform][2],
235+
firstTransformArg3 = argsArray[iFirstTransform][3];
236+
237+
if (firstTransformArg1 === firstTransformArg2) {
236238
isSameImage = true;
237-
var firstTransformArg0 = argsArray[iFirstTransform][0];
238-
var firstTransformArg3 = argsArray[iFirstTransform][3];
239239
iTransform = iFirstTransform + 4;
240240
var iPIMXO = iFirstPIMXO + 4;
241241
for (q = 1; q < count; q++, iTransform += 4, iPIMXO += 4) {
242242
transformArgs = argsArray[iTransform];
243243
if (
244244
argsArray[iPIMXO][0] !== firstPIMXOArg0 ||
245245
transformArgs[0] !== firstTransformArg0 ||
246-
transformArgs[1] !== 0 ||
247-
transformArgs[2] !== 0 ||
246+
transformArgs[1] !== firstTransformArg1 ||
247+
transformArgs[2] !== firstTransformArg2 ||
248248
transformArgs[3] !== firstTransformArg3
249249
) {
250250
if (q < MIN_IMAGES_IN_MASKS_BLOCK) {
@@ -272,6 +272,8 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
272272
argsArray.splice(iFirstSave, count * 4, [
273273
firstPIMXOArg0,
274274
firstTransformArg0,
275+
firstTransformArg1,
276+
firstTransformArg2,
275277
firstTransformArg3,
276278
positions,
277279
]);

src/display/canvas.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -2157,9 +2157,11 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
21572157
this.paintInlineImageXObject(maskCanvas.canvas);
21582158
},
21592159

2160-
paintImageMaskXObjectRepeat: function CanvasGraphics_paintImageMaskXObjectRepeat(
2160+
paintImageMaskXObjectRepeat(
21612161
imgData,
21622162
scaleX,
2163+
skewX = 0,
2164+
skewY = 0,
21632165
scaleY,
21642166
positions
21652167
) {
@@ -2190,7 +2192,14 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
21902192
var ctx = this.ctx;
21912193
for (var i = 0, ii = positions.length; i < ii; i += 2) {
21922194
ctx.save();
2193-
ctx.transform(scaleX, 0, 0, scaleY, positions[i], positions[i + 1]);
2195+
ctx.transform(
2196+
scaleX,
2197+
skewX,
2198+
skewY,
2199+
scaleY,
2200+
positions[i],
2201+
positions[i + 1]
2202+
);
21942203
ctx.scale(1, -1);
21952204
ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
21962205
ctx.restore();

test/pdfs/issue8078.pdf.link

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://bugs.ghostscript.com/attachment.cgi?id=7455

test/test_manifest.json

+8
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,14 @@
711711
"type": "eq",
712712
"about": "Type1 font with |Ref|s in the Differences array of the Encoding dictionary."
713713
},
714+
{ "id": "issue8078",
715+
"file": "pdfs/issue8078.pdf",
716+
"md5": "8b7d74bc24b4157393e4e88a511c05f1",
717+
"link": true,
718+
"rounds": 1,
719+
"lastPage": 1,
720+
"type": "eq"
721+
},
714722
{ "id": "issue8092",
715723
"file": "pdfs/issue8092.pdf",
716724
"md5": "e4f3376b35fd132580246c3db1fbd738",

0 commit comments

Comments
 (0)