2 * Copyright (C) 2010 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CairoUtilities.h"
29 #include "AffineTransform.h"
30 #include "CairoPath.h"
32 #include "FloatPoint.h"
33 #include "FloatRect.h"
35 #include "OwnPtrCairo.h"
37 #include "RefPtrCairo.h"
38 #include <wtf/Vector.h>
42 void copyContextProperties(cairo_t* srcCr, cairo_t* dstCr)
44 cairo_set_antialias(dstCr, cairo_get_antialias(srcCr));
46 size_t dashCount = cairo_get_dash_count(srcCr);
47 Vector<double> dashes(dashCount);
50 cairo_get_dash(srcCr, dashes.data(), &offset);
51 cairo_set_dash(dstCr, dashes.data(), dashCount, offset);
52 cairo_set_line_cap(dstCr, cairo_get_line_cap(srcCr));
53 cairo_set_line_join(dstCr, cairo_get_line_join(srcCr));
54 cairo_set_line_width(dstCr, cairo_get_line_width(srcCr));
55 cairo_set_miter_limit(dstCr, cairo_get_miter_limit(srcCr));
56 cairo_set_fill_rule(dstCr, cairo_get_fill_rule(srcCr));
59 void setSourceRGBAFromColor(cairo_t* context, const Color& color)
61 float red, green, blue, alpha;
62 color.getRGBA(red, green, blue, alpha);
63 cairo_set_source_rgba(context, red, green, blue, alpha);
66 void appendPathToCairoContext(cairo_t* to, cairo_t* from)
68 OwnPtr<cairo_path_t> cairoPath(cairo_copy_path(from));
69 cairo_append_path(to, cairoPath.get());
72 void setPathOnCairoContext(cairo_t* to, cairo_t* from)
75 appendPathToCairoContext(to, from);
78 void appendWebCorePathToCairoContext(cairo_t* context, const Path& path)
80 appendPathToCairoContext(context, path.platformPath()->context());
83 cairo_operator_t toCairoOperator(CompositeOperator op)
87 return CAIRO_OPERATOR_CLEAR;
89 return CAIRO_OPERATOR_SOURCE;
90 case CompositeSourceOver:
91 return CAIRO_OPERATOR_OVER;
92 case CompositeSourceIn:
93 return CAIRO_OPERATOR_IN;
94 case CompositeSourceOut:
95 return CAIRO_OPERATOR_OUT;
96 case CompositeSourceAtop:
97 return CAIRO_OPERATOR_ATOP;
98 case CompositeDestinationOver:
99 return CAIRO_OPERATOR_DEST_OVER;
100 case CompositeDestinationIn:
101 return CAIRO_OPERATOR_DEST_IN;
102 case CompositeDestinationOut:
103 return CAIRO_OPERATOR_DEST_OUT;
104 case CompositeDestinationAtop:
105 return CAIRO_OPERATOR_DEST_ATOP;
107 return CAIRO_OPERATOR_XOR;
108 case CompositePlusDarker:
109 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 10, 0)
110 return CAIRO_OPERATOR_DARKEN;
112 return CAIRO_OPERATOR_SATURATE;
114 case CompositeHighlight:
115 // There is no Cairo equivalent for CompositeHighlight.
116 return CAIRO_OPERATOR_OVER;
117 case CompositePlusLighter:
118 return CAIRO_OPERATOR_ADD;
120 return CAIRO_OPERATOR_SOURCE;
124 void drawPatternToCairoContext(cairo_t* cr, cairo_surface_t* image, const IntSize& imageSize, const FloatRect& tileRect,
125 const AffineTransform& patternTransform, const FloatPoint& phase, cairo_operator_t op, const FloatRect& destRect)
128 if (!isfinite(phase.x()) || !isfinite(phase.y()))
133 RefPtr<cairo_surface_t> clippedImageSurface = 0;
134 if (tileRect.size() != imageSize) {
135 IntRect imageRect = enclosingIntRect(tileRect);
136 clippedImageSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, imageRect.width(), imageRect.height()));
137 RefPtr<cairo_t> clippedImageContext = adoptRef(cairo_create(clippedImageSurface.get()));
138 cairo_set_source_surface(clippedImageContext.get(), image, -tileRect.x(), -tileRect.y());
139 cairo_paint(clippedImageContext.get());
140 image = clippedImageSurface.get();
143 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(image);
144 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
146 cairo_matrix_t patternMatrix = cairo_matrix_t(patternTransform);
147 cairo_matrix_t phaseMatrix = {1, 0, 0, 1, phase.x() + tileRect.x() * patternTransform.a(), phase.y() + tileRect.y() * patternTransform.d()};
148 cairo_matrix_t combined;
149 cairo_matrix_multiply(&combined, &patternMatrix, &phaseMatrix);
150 cairo_matrix_invert(&combined);
151 cairo_pattern_set_matrix(pattern, &combined);
153 cairo_set_operator(cr, op);
154 cairo_set_source(cr, pattern);
155 cairo_pattern_destroy(pattern);
156 cairo_rectangle(cr, destRect.x(), destRect.y(), destRect.width(), destRect.height());
162 } // namespace WebCore