Commit 47ffce08 authored by Adrian Johnson's avatar Adrian Johnson Committed by Carlos Garcia Campos

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
parent d7717cf1
......@@ -932,6 +932,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;
......
......@@ -112,7 +112,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()?
......@@ -169,6 +169,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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment