diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc index ea8aeea..aa95945 100644 --- a/poppler/PSOutputDev.cc +++ b/poppler/PSOutputDev.cc @@ -4729,14 +4729,28 @@ void PSOutputDev::maskToClippingPath(Stream *maskStr, int maskWidth, int maskHei rectsOut[rectsOutLen].y1 = maskHeight - rects0[i].y0 - 1; ++rectsOutLen; } - writePSFmt("{0:d} array 0\n", rectsOutLen * 4); - for (i = 0; i < rectsOutLen; ++i) { - writePSFmt("[{0:d} {1:d} {2:d} {3:d}] pr\n", - rectsOut[i].x0, rectsOut[i].y0, - rectsOut[i].x1 - rectsOut[i].x0, - rectsOut[i].y1 - rectsOut[i].y0); - } - writePSFmt("pop {0:d} {1:d} pdfImClip\n", maskWidth, maskHeight); + if (rectsOutLen < 65536/4) { + writePSFmt("{0:d} array 0\n", rectsOutLen * 4); + for (i = 0; i < rectsOutLen; ++i) { + writePSFmt("[{0:d} {1:d} {2:d} {3:d}] pr\n", + rectsOut[i].x0, rectsOut[i].y0, + rectsOut[i].x1 - rectsOut[i].x0, + rectsOut[i].y1 - rectsOut[i].y0); + } + writePSFmt("pop {0:d} {1:d} pdfImClip\n", maskWidth, maskHeight); + } else { + // would be over the limit of array size. + // make each rectangle path and clip. + writePS("gsave newpath\n"); + for (i = 0; i < rectsOutLen; ++i) { + writePSFmt("{0:.4g} {1:.4g} {2:.4g} {3:.4g} re\n", + ((double)rectsOut[i].x0)/maskWidth, + ((double)rectsOut[i].y0)/maskHeight, + ((double)(rectsOut[i].x1 - rectsOut[i].x0))/maskWidth, + ((double)(rectsOut[i].y1 - rectsOut[i].y0))/maskHeight); + } + writePS("clip\n"); + } gfree(rectsOut); gfree(rects0); gfree(rects1); @@ -4894,14 +4908,28 @@ void PSOutputDev::doImageL2(Object *ref, GfxImageColorMap *colorMap, rectsOut[rectsOutLen].y1 = height - rects0[i].y0 - 1; ++rectsOutLen; } - writePSFmt("{0:d} array 0\n", rectsOutLen * 4); - for (i = 0; i < rectsOutLen; ++i) { - writePSFmt("[{0:d} {1:d} {2:d} {3:d}] pr\n", - rectsOut[i].x0, rectsOut[i].y0, - rectsOut[i].x1 - rectsOut[i].x0, - rectsOut[i].y1 - rectsOut[i].y0); + if (rectsOutLen < 65536/4) { + writePSFmt("{0:d} array 0\n", rectsOutLen * 4); + for (i = 0; i < rectsOutLen; ++i) { + writePSFmt("[{0:d} {1:d} {2:d} {3:d}] pr\n", + rectsOut[i].x0, rectsOut[i].y0, + rectsOut[i].x1 - rectsOut[i].x0, + rectsOut[i].y1 - rectsOut[i].y0); + } + writePSFmt("pop {0:d} {1:d} pdfImClip\n", width, height); + } else { + // would be over the limit of array size. + // make each rectangle path and clip. + writePS("gsave newpath\n"); + for (i = 0; i < rectsOutLen; ++i) { + writePSFmt("{0:.4g} {1:.4g} {2:.4g} {3:.4g} re\n", + ((double)rectsOut[i].x0)/width, + ((double)rectsOut[i].y0)/height, + ((double)(rectsOut[i].x1 - rectsOut[i].x0))/width, + ((double)(rectsOut[i].y1 - rectsOut[i].y0))/height); + } + writePS("clip\n"); } - writePSFmt("pop {0:d} {1:d} pdfImClip\n", width, height); gfree(rectsOut); gfree(rects0); gfree(rects1); @@ -5166,7 +5194,11 @@ void PSOutputDev::doImageL2(Object *ref, GfxImageColorMap *colorMap, } if ((maskColors && colorMap && !inlineImg) || maskStr) { - writePS("pdfImClipEnd\n"); + if (rectsOutLen < 65536/4) { + writePS("pdfImClipEnd\n"); + } else { + writePS("grestore\n"); + } } }