c2e870
--- poppler-0.26.5/poppler/CairoOutputDev.cc
c2e870
+++ poppler-0.26.5/poppler/CairoOutputDev.cc
c2e870
@@ -837,11 +837,17 @@ GBool CairoOutputDev::tilingPatternFill(
c2e870
   cairo_pattern_t *pattern;
c2e870
   cairo_surface_t *surface;
c2e870
   cairo_matrix_t matrix;
c2e870
+  cairo_matrix_t pattern_matrix;
c2e870
   cairo_t *old_cairo;
c2e870
   double xMin, yMin, xMax, yMax;
c2e870
   double width, height;
c2e870
+  double scaleX, scaleY;
c2e870
   int surface_width, surface_height;
c2e870
   StrokePathClip *strokePathTmp;
c2e870
+  GBool adjusted_stroke_width_tmp;
c2e870
+  cairo_pattern_t *maskTmp;
c2e870
+  double xoffset, yoffset;
c2e870
+  double det;
c2e870
 
c2e870
   width = bbox[2] - bbox[0];
c2e870
   height = bbox[3] - bbox[1];
c2e870
@@ -850,8 +856,20 @@ GBool CairoOutputDev::tilingPatternFill(
c2e870
     return gFalse;
c2e870
   /* TODO: implement the other cases here too */
c2e870
 
c2e870
-  surface_width = (int) ceil (width);
c2e870
-  surface_height = (int) ceil (height);
c2e870
+  // Find the width and height of the transformed pattern
c2e870
+  cairo_get_matrix (cairo, &matrix);
c2e870
+  cairo_matrix_init (&pattern_matrix, mat[0], mat[1], mat[2], mat[3], mat[4], mat[5]);
c2e870
+  cairo_matrix_multiply (&matrix, &matrix, &pattern_matrix);
c2e870
+
c2e870
+  double widthX = width, widthY = 0;
c2e870
+  cairo_matrix_transform_distance (&matrix, &widthX, &widthY);
c2e870
+  surface_width = ceil (sqrt (widthX * widthX + widthY * widthY));
c2e870
+
c2e870
+  double heightX = 0, heightY = height;
c2e870
+  cairo_matrix_transform_distance (&matrix, &heightX, &heightY);
c2e870
+  surface_height = ceil (sqrt (heightX * heightX + heightY * heightY));
c2e870
+  scaleX = surface_width / width;
c2e870
+  scaleY = surface_height / height;
c2e870
 
c2e870
   surface = cairo_surface_create_similar (cairo_get_target (cairo),
c2e870
 					  CAIRO_CONTENT_COLOR_ALPHA,
c2e870
@@ -865,9 +883,15 @@ GBool CairoOutputDev::tilingPatternFill(
c2e870
 
c2e870
   box.x1 = bbox[0]; box.y1 = bbox[1];
c2e870
   box.x2 = bbox[2]; box.y2 = bbox[3];
c2e870
+  cairo_scale (cairo, scaleX, scaleY);
c2e870
+  cairo_translate (cairo, -box.x1, -box.y1);
c2e870
+
c2e870
   strokePathTmp = strokePathClip;
c2e870
-  strokePathClip = NULL;
c2e870
-  gfx = new Gfx(doc, this, resDict, &box, NULL, NULL, NULL, gfxA->getXRef());
c2e870
+  strokePathClip = NULL;
c2e870
+  adjusted_stroke_width_tmp = adjusted_stroke_width;
c2e870
+  maskTmp = mask;
c2e870
+  mask = NULL;
c2e870
+  gfx = new Gfx(doc, this, resDict, &box, NULL, NULL, NULL, gfxA->getXRef());
c2e870
   if (paintType == 2)
c2e870
     inUncoloredPattern = gTrue;
c2e870
   gfx->display(str);
c2e870
@@ -875,6 +899,8 @@ GBool CairoOutputDev::tilingPatternFill(
c2e870
     inUncoloredPattern = gFalse;
c2e870
   delete gfx;
c2e870
   strokePathClip = strokePathTmp;
c2e870
+  adjusted_stroke_width = adjusted_stroke_width_tmp;
c2e870
+  mask = maskTmp;
c2e870
 
c2e870
   pattern = cairo_pattern_create_for_surface (cairo_get_target (cairo));
c2e870
   cairo_destroy (cairo);
c2e870
@@ -882,14 +908,23 @@ GBool CairoOutputDev::tilingPatternFill(
c2e870
   if (cairo_pattern_status (pattern))
c2e870
     return gFalse;
c2e870
 
c2e870
+  det = pmat[0] * pmat[3] - pmat[1] * pmat[2];
c2e870
+  if (fabs(det) < 0.000001)
c2e870
+    return gFalse;
c2e870
+
c2e870
+  xoffset = round ((pmat[3] * pmat[4] - pmat[2] * pmat[5]) / (xStep * det));
c2e870
+  yoffset = - round ((pmat[1] * pmat[4] - pmat[0] * pmat[5]) / (yStep * det));
c2e870
+  pattern_matrix.x0 -= xoffset * pattern_matrix.xx * xStep + yoffset * pattern_matrix.xy * yStep;
c2e870
+  pattern_matrix.y0 -= xoffset * pattern_matrix.yx * xStep + yoffset * pattern_matrix.yy * yStep;
c2e870
+
c2e870
   state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
c2e870
   cairo_rectangle (cairo, xMin, yMin, xMax - xMin, yMax - yMin);
c2e870
 
c2e870
-  cairo_matrix_init_scale (&matrix, surface_width / width, surface_height / height);
c2e870
+  cairo_matrix_init_scale (&matrix, scaleX, scaleY);
c2e870
+  cairo_matrix_translate (&matrix, -box.x1, -box.y1);
c2e870
   cairo_pattern_set_matrix (pattern, &matrix);
c2e870
 
c2e870
-  cairo_matrix_init (&matrix, mat[0], mat[1], mat[2], mat[3], mat[4], mat[5]);
c2e870
-  cairo_transform (cairo, &matrix);
c2e870
+  cairo_transform (cairo, &pattern_matrix);
c2e870
   cairo_set_source (cairo, pattern);
c2e870
   cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
c2e870
   if (strokePathClip) {