From 6a1409364cd21a228f27861c38280a74f2096942 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 15 Jan 2015 21:20:05 +1030 Subject: [PATCH] cairo: Implement function shading using mesh gradients Gfx draws function shadings by subdividing the shading until the colors are the same or the maximum subdivision is reached then fills each cell with the color of the mid point of the cell. The solid colors can result in a pixelated appearance. This patch implements a cairo specific version of the function shading that uses mesh gradients to draw each cell. By setting the corner of each patch to the shading color at that point, the mesh gradient will interpolate the colors resulting in a smooth appearance. Bug 88394 --- poppler/CairoOutputDev.cc | 101 ++++++++++++++++++++++++++++++++++++++++++++++ poppler/CairoOutputDev.h | 5 ++- 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/poppler/CairoOutputDev.cc b/poppler/CairoOutputDev.cc index 4e8abcf..a136105 100644 --- a/poppler/CairoOutputDev.cc +++ b/poppler/CairoOutputDev.cc @@ -922,6 +922,107 @@ GBool CairoOutputDev::tilingPatternFill(GfxState *state, Gfx *gfxA, Catalog *cat return gTrue; } +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) +GBool CairoOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) +{ + // Function shaded fills are subdivided to rectangles that are the + // following size in device space. Note when printing this size is + // in points. + const int subdivide_pixels = 10; + + double x_begin, x_end, x1, x2; + double y_begin, y_end, y1, y2; + double x_step; + double y_step; + GfxColor color; + GfxRGB rgb; + double *matrix; + cairo_matrix_t mat; + + matrix = shading->getMatrix(); + mat.xx = matrix[0]; + mat.yx = matrix[1]; + mat.xy = matrix[2]; + mat.yy = matrix[3]; + mat.x0 = matrix[4]; + mat.y0 = matrix[5]; + if (cairo_matrix_invert(&mat)) { + error(errSyntaxWarning, -1, "matrix not invertible\n"); + return gFalse; + } + + // get cell size in pattern space + x_step = y_step = subdivide_pixels; + cairo_matrix_transform_distance (&mat, &x_step, &y_step); + + cairo_pattern_destroy(fill_pattern); + fill_pattern = cairo_pattern_create_mesh (); + cairo_pattern_set_matrix(fill_pattern, &mat); + shading->getDomain(&x_begin, &y_begin, &x_end, &y_end); + + for (x1 = x_begin; x1 < x_end; x1 += x_step) { + x2 = x1 + x_step; + if (x2 > x_end) + x2 = x_end; + + for (y1 = y_begin; y1 < y_end; y1 += y_step) { + y2 = y1 + y_step; + if (y2 > y_end) + y2 = y_end; + + cairo_mesh_pattern_begin_patch (fill_pattern); + cairo_mesh_pattern_move_to (fill_pattern, x1, y1); + cairo_mesh_pattern_line_to (fill_pattern, x2, y1); + cairo_mesh_pattern_line_to (fill_pattern, x2, y2); + cairo_mesh_pattern_line_to (fill_pattern, x1, y2); + + shading->getColor(x1, y1, &color); + shading->getColorSpace()->getRGB(&color, &rgb); + cairo_mesh_pattern_set_corner_color_rgb (fill_pattern, 0, + colToDbl(rgb.r), + colToDbl(rgb.g), + colToDbl(rgb.b)); + + shading->getColor(x2, y1, &color); + shading->getColorSpace()->getRGB(&color, &rgb); + cairo_mesh_pattern_set_corner_color_rgb (fill_pattern, 1, + colToDbl(rgb.r), + colToDbl(rgb.g), + colToDbl(rgb.b)); + + shading->getColor(x2, y2, &color); + shading->getColorSpace()->getRGB(&color, &rgb); + cairo_mesh_pattern_set_corner_color_rgb (fill_pattern, 2, + colToDbl(rgb.r), + colToDbl(rgb.g), + colToDbl(rgb.b)); + + shading->getColor(x1, y2, &color); + shading->getColorSpace()->getRGB(&color, &rgb); + cairo_mesh_pattern_set_corner_color_rgb (fill_pattern, 3, + colToDbl(rgb.r), + colToDbl(rgb.g), + colToDbl(rgb.b)); + + cairo_mesh_pattern_end_patch (fill_pattern); + } + } + + double xMin, yMin, xMax, yMax; + // get the clip region bbox + state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax); + state->moveTo(xMin, yMin); + state->lineTo(xMin, yMax); + state->lineTo(xMax, yMax); + state->lineTo(xMax, yMin); + state->closePath(); + fill(state); + state->clearPath(); + + return gTrue; +} +#endif /* CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) */ + GBool CairoOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading, double tMin, double tMax) { double x0, y0, x1, y1; double dx, dy; diff --git a/poppler/CairoOutputDev.h b/poppler/CairoOutputDev.h index 8de391a..6b88e5a 100644 --- a/poppler/CairoOutputDev.h +++ b/poppler/CairoOutputDev.h @@ -111,7 +111,7 @@ public: #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) virtual GBool useShadedFills(int type) { return type <= 7; } #else - virtual GBool useShadedFills(int type) { return type < 4; } + virtual GBool useShadedFills(int type) { return type > 1 && type < 4; } #endif // Does this device use FillColorStop()? @@ -168,6 +168,9 @@ public: double *mat, double *bbox, int x0, int y0, int x1, int y1, double xStep, double yStep); +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) + virtual GBool functionShadedFill(GfxState *state, GfxFunctionShading *shading); +#endif virtual GBool axialShadedFill(GfxState *state, GfxAxialShading *shading, double tMin, double tMax); virtual GBool axialShadedSupportExtend(GfxState *state, GfxAxialShading *shading); virtual GBool radialShadedFill(GfxState *state, GfxRadialShading *shading, double sMin, double sMax); -- 2.1.4