1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_GFX_HELPERSCAIRO_H_
7 #define MOZILLA_GFX_HELPERSCAIRO_H_
16 static inline cairo_operator_t
17 GfxOpToCairoOp(CompositionOp op
)
21 case CompositionOp::OP_OVER
:
22 return CAIRO_OPERATOR_OVER
;
23 case CompositionOp::OP_ADD
:
24 return CAIRO_OPERATOR_ADD
;
25 case CompositionOp::OP_ATOP
:
26 return CAIRO_OPERATOR_ATOP
;
27 case CompositionOp::OP_OUT
:
28 return CAIRO_OPERATOR_OUT
;
29 case CompositionOp::OP_IN
:
30 return CAIRO_OPERATOR_IN
;
31 case CompositionOp::OP_SOURCE
:
32 return CAIRO_OPERATOR_SOURCE
;
33 case CompositionOp::OP_DEST_IN
:
34 return CAIRO_OPERATOR_DEST_IN
;
35 case CompositionOp::OP_DEST_OUT
:
36 return CAIRO_OPERATOR_DEST_OUT
;
37 case CompositionOp::OP_DEST_OVER
:
38 return CAIRO_OPERATOR_DEST_OVER
;
39 case CompositionOp::OP_DEST_ATOP
:
40 return CAIRO_OPERATOR_DEST_ATOP
;
41 case CompositionOp::OP_XOR
:
42 return CAIRO_OPERATOR_XOR
;
43 case CompositionOp::OP_MULTIPLY
:
44 return CAIRO_OPERATOR_MULTIPLY
;
45 case CompositionOp::OP_SCREEN
:
46 return CAIRO_OPERATOR_SCREEN
;
47 case CompositionOp::OP_OVERLAY
:
48 return CAIRO_OPERATOR_OVERLAY
;
49 case CompositionOp::OP_DARKEN
:
50 return CAIRO_OPERATOR_DARKEN
;
51 case CompositionOp::OP_LIGHTEN
:
52 return CAIRO_OPERATOR_LIGHTEN
;
53 case CompositionOp::OP_COLOR_DODGE
:
54 return CAIRO_OPERATOR_COLOR_DODGE
;
55 case CompositionOp::OP_COLOR_BURN
:
56 return CAIRO_OPERATOR_COLOR_BURN
;
57 case CompositionOp::OP_HARD_LIGHT
:
58 return CAIRO_OPERATOR_HARD_LIGHT
;
59 case CompositionOp::OP_SOFT_LIGHT
:
60 return CAIRO_OPERATOR_SOFT_LIGHT
;
61 case CompositionOp::OP_DIFFERENCE
:
62 return CAIRO_OPERATOR_DIFFERENCE
;
63 case CompositionOp::OP_EXCLUSION
:
64 return CAIRO_OPERATOR_EXCLUSION
;
65 case CompositionOp::OP_HUE
:
66 return CAIRO_OPERATOR_HSL_HUE
;
67 case CompositionOp::OP_SATURATION
:
68 return CAIRO_OPERATOR_HSL_SATURATION
;
69 case CompositionOp::OP_COLOR
:
70 return CAIRO_OPERATOR_HSL_COLOR
;
71 case CompositionOp::OP_LUMINOSITY
:
72 return CAIRO_OPERATOR_HSL_LUMINOSITY
;
73 case CompositionOp::OP_COUNT
:
77 return CAIRO_OPERATOR_OVER
;
80 static inline cairo_antialias_t
81 GfxAntialiasToCairoAntialias(AntialiasMode antialias
)
85 case AntialiasMode::NONE
:
86 return CAIRO_ANTIALIAS_NONE
;
87 case AntialiasMode::GRAY
:
88 return CAIRO_ANTIALIAS_GRAY
;
89 case AntialiasMode::SUBPIXEL
:
90 return CAIRO_ANTIALIAS_SUBPIXEL
;
91 case AntialiasMode::DEFAULT
:
92 return CAIRO_ANTIALIAS_DEFAULT
;
94 return CAIRO_ANTIALIAS_DEFAULT
;
97 static inline cairo_filter_t
98 GfxFilterToCairoFilter(Filter filter
)
103 return CAIRO_FILTER_GOOD
;
105 return CAIRO_FILTER_BILINEAR
;
107 return CAIRO_FILTER_NEAREST
;
110 return CAIRO_FILTER_BILINEAR
;
113 static inline cairo_extend_t
114 GfxExtendToCairoExtend(ExtendMode extend
)
118 case ExtendMode::CLAMP
:
119 return CAIRO_EXTEND_PAD
;
120 case ExtendMode::REPEAT
:
121 return CAIRO_EXTEND_REPEAT
;
122 case ExtendMode::REFLECT
:
123 return CAIRO_EXTEND_REFLECT
;
126 return CAIRO_EXTEND_PAD
;
129 static inline cairo_format_t
130 GfxFormatToCairoFormat(SurfaceFormat format
)
134 case SurfaceFormat::B8G8R8A8
:
135 return CAIRO_FORMAT_ARGB32
;
136 case SurfaceFormat::B8G8R8X8
:
137 return CAIRO_FORMAT_RGB24
;
138 case SurfaceFormat::A8
:
139 return CAIRO_FORMAT_A8
;
140 case SurfaceFormat::R5G6B5
:
141 return CAIRO_FORMAT_RGB16_565
;
143 gfxWarning() << "Unknown image format";
144 MOZ_ASSERT(false, "Unknown image format");
145 return CAIRO_FORMAT_ARGB32
;
149 static inline cairo_content_t
150 GfxFormatToCairoContent(SurfaceFormat format
)
154 case SurfaceFormat::B8G8R8A8
:
155 return CAIRO_CONTENT_COLOR_ALPHA
;
156 case SurfaceFormat::B8G8R8X8
:
157 case SurfaceFormat::R5G6B5
: //fall through
158 return CAIRO_CONTENT_COLOR
;
159 case SurfaceFormat::A8
:
160 return CAIRO_CONTENT_ALPHA
;
162 gfxWarning() << "Unknown image format";
163 MOZ_ASSERT(false, "Unknown image format");
164 return CAIRO_CONTENT_COLOR_ALPHA
;
168 static inline cairo_line_join_t
169 GfxLineJoinToCairoLineJoin(JoinStyle style
)
173 case JoinStyle::BEVEL
:
174 return CAIRO_LINE_JOIN_BEVEL
;
175 case JoinStyle::ROUND
:
176 return CAIRO_LINE_JOIN_ROUND
;
177 case JoinStyle::MITER
:
178 return CAIRO_LINE_JOIN_MITER
;
179 case JoinStyle::MITER_OR_BEVEL
:
180 return CAIRO_LINE_JOIN_MITER
;
183 return CAIRO_LINE_JOIN_MITER
;
186 static inline cairo_line_cap_t
187 GfxLineCapToCairoLineCap(CapStyle style
)
192 return CAIRO_LINE_CAP_BUTT
;
193 case CapStyle::ROUND
:
194 return CAIRO_LINE_CAP_ROUND
;
195 case CapStyle::SQUARE
:
196 return CAIRO_LINE_CAP_SQUARE
;
199 return CAIRO_LINE_CAP_BUTT
;
202 static inline SurfaceFormat
203 CairoContentToGfxFormat(cairo_content_t content
)
207 case CAIRO_CONTENT_COLOR_ALPHA
:
208 return SurfaceFormat::B8G8R8A8
;
209 case CAIRO_CONTENT_COLOR
:
210 // BEWARE! format may be 565
211 return SurfaceFormat::B8G8R8X8
;
212 case CAIRO_CONTENT_ALPHA
:
213 return SurfaceFormat::A8
;
216 return SurfaceFormat::B8G8R8A8
;
219 static inline SurfaceFormat
220 CairoFormatToGfxFormat(cairo_format_t format
)
223 case CAIRO_FORMAT_ARGB32
:
224 return SurfaceFormat::B8G8R8A8
;
225 case CAIRO_FORMAT_RGB24
:
226 return SurfaceFormat::B8G8R8X8
;
227 case CAIRO_FORMAT_A8
:
228 return SurfaceFormat::A8
;
229 case CAIRO_FORMAT_RGB16_565
:
230 return SurfaceFormat::R5G6B5
;
232 gfxWarning() << "Unknown cairo format";
233 MOZ_ASSERT(false, "Unknown cairo format");
234 return SurfaceFormat::UNKNOWN
;
238 static inline SurfaceFormat
239 GfxFormatForCairoSurface(cairo_surface_t
* surface
)
241 if (cairo_surface_get_type(surface
) == CAIRO_SURFACE_TYPE_IMAGE
) {
242 return CairoFormatToGfxFormat(cairo_image_surface_get_format(surface
));
245 return CairoContentToGfxFormat(cairo_surface_get_content(surface
));
249 GfxMatrixToCairoMatrix(const Matrix
& mat
, cairo_matrix_t
& retval
)
251 cairo_matrix_init(&retval
, mat
._11
, mat
._12
, mat
._21
, mat
._22
, mat
._31
, mat
._32
);
255 SetCairoStrokeOptions(cairo_t
* aCtx
, const StrokeOptions
& aStrokeOptions
)
257 cairo_set_line_width(aCtx
, aStrokeOptions
.mLineWidth
);
259 cairo_set_miter_limit(aCtx
, aStrokeOptions
.mMiterLimit
);
261 if (aStrokeOptions
.mDashPattern
) {
262 // Convert array of floats to array of doubles
263 std::vector
<double> dashes(aStrokeOptions
.mDashLength
);
264 for (size_t i
= 0; i
< aStrokeOptions
.mDashLength
; ++i
) {
265 dashes
[i
] = aStrokeOptions
.mDashPattern
[i
];
267 cairo_set_dash(aCtx
, &dashes
[0], aStrokeOptions
.mDashLength
,
268 aStrokeOptions
.mDashOffset
);
271 cairo_set_line_join(aCtx
, GfxLineJoinToCairoLineJoin(aStrokeOptions
.mLineJoin
));
273 cairo_set_line_cap(aCtx
, GfxLineCapToCairoLineCap(aStrokeOptions
.mLineCap
));
276 static inline cairo_fill_rule_t
277 GfxFillRuleToCairoFillRule(FillRule rule
)
281 case FillRule::FILL_WINDING
:
282 return CAIRO_FILL_RULE_WINDING
;
283 case FillRule::FILL_EVEN_ODD
:
284 return CAIRO_FILL_RULE_EVEN_ODD
;
287 return CAIRO_FILL_RULE_WINDING
;
290 // RAII class for temporarily changing the cairo matrix transform. It will use
291 // the given matrix transform while it is in scope. When it goes out of scope
292 // it will put the cairo context back the way it was.
294 class CairoTempMatrix
297 CairoTempMatrix(cairo_t
* aCtx
, const Matrix
& aMatrix
)
300 cairo_get_matrix(aCtx
, &mSaveMatrix
);
301 cairo_matrix_t matrix
;
302 GfxMatrixToCairoMatrix(aMatrix
, matrix
);
303 cairo_set_matrix(aCtx
, &matrix
);
308 cairo_set_matrix(mCtx
, &mSaveMatrix
);
313 cairo_matrix_t mSaveMatrix
;
319 #endif /* MOZILLA_GFX_HELPERSCAIRO_H_ */