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 return CAIRO_FORMAT_ARGB32
;
148 static inline cairo_content_t
149 GfxFormatToCairoContent(SurfaceFormat format
)
153 case SurfaceFormat::B8G8R8A8
:
154 return CAIRO_CONTENT_COLOR_ALPHA
;
155 case SurfaceFormat::B8G8R8X8
:
156 case SurfaceFormat::R5G6B5
: //fall through
157 return CAIRO_CONTENT_COLOR
;
158 case SurfaceFormat::A8
:
159 return CAIRO_CONTENT_ALPHA
;
161 gfxWarning() << "Unknown image format";
162 return CAIRO_CONTENT_COLOR_ALPHA
;
166 static inline cairo_line_join_t
167 GfxLineJoinToCairoLineJoin(JoinStyle style
)
171 case JoinStyle::BEVEL
:
172 return CAIRO_LINE_JOIN_BEVEL
;
173 case JoinStyle::ROUND
:
174 return CAIRO_LINE_JOIN_ROUND
;
175 case JoinStyle::MITER
:
176 return CAIRO_LINE_JOIN_MITER
;
177 case JoinStyle::MITER_OR_BEVEL
:
178 return CAIRO_LINE_JOIN_MITER
;
181 return CAIRO_LINE_JOIN_MITER
;
184 static inline cairo_line_cap_t
185 GfxLineCapToCairoLineCap(CapStyle style
)
190 return CAIRO_LINE_CAP_BUTT
;
191 case CapStyle::ROUND
:
192 return CAIRO_LINE_CAP_ROUND
;
193 case CapStyle::SQUARE
:
194 return CAIRO_LINE_CAP_SQUARE
;
197 return CAIRO_LINE_CAP_BUTT
;
200 static inline SurfaceFormat
201 CairoContentToGfxFormat(cairo_content_t content
)
205 case CAIRO_CONTENT_COLOR_ALPHA
:
206 return SurfaceFormat::B8G8R8A8
;
207 case CAIRO_CONTENT_COLOR
:
208 // BEWARE! format may be 565
209 return SurfaceFormat::B8G8R8X8
;
210 case CAIRO_CONTENT_ALPHA
:
211 return SurfaceFormat::A8
;
214 return SurfaceFormat::B8G8R8A8
;
217 static inline SurfaceFormat
218 CairoFormatToGfxFormat(cairo_format_t format
)
221 case CAIRO_FORMAT_ARGB32
:
222 return SurfaceFormat::B8G8R8A8
;
223 case CAIRO_FORMAT_RGB24
:
224 return SurfaceFormat::B8G8R8X8
;
225 case CAIRO_FORMAT_A8
:
226 return SurfaceFormat::A8
;
227 case CAIRO_FORMAT_RGB16_565
:
228 return SurfaceFormat::R5G6B5
;
230 gfxWarning() << "Unknown cairo format";
231 MOZ_ASSERT(false, "Unknown cairo format");
232 return SurfaceFormat::UNKNOWN
;
236 static inline SurfaceFormat
237 GfxFormatForCairoSurface(cairo_surface_t
* surface
)
239 if (cairo_surface_get_type(surface
) == CAIRO_SURFACE_TYPE_IMAGE
) {
240 return CairoFormatToGfxFormat(cairo_image_surface_get_format(surface
));
243 return CairoContentToGfxFormat(cairo_surface_get_content(surface
));
247 GfxMatrixToCairoMatrix(const Matrix
& mat
, cairo_matrix_t
& retval
)
249 cairo_matrix_init(&retval
, mat
._11
, mat
._12
, mat
._21
, mat
._22
, mat
._31
, mat
._32
);
253 SetCairoStrokeOptions(cairo_t
* aCtx
, const StrokeOptions
& aStrokeOptions
)
255 cairo_set_line_width(aCtx
, aStrokeOptions
.mLineWidth
);
257 cairo_set_miter_limit(aCtx
, aStrokeOptions
.mMiterLimit
);
259 if (aStrokeOptions
.mDashPattern
) {
260 // Convert array of floats to array of doubles
261 std::vector
<double> dashes(aStrokeOptions
.mDashLength
);
262 for (size_t i
= 0; i
< aStrokeOptions
.mDashLength
; ++i
) {
263 dashes
[i
] = aStrokeOptions
.mDashPattern
[i
];
265 cairo_set_dash(aCtx
, &dashes
[0], aStrokeOptions
.mDashLength
,
266 aStrokeOptions
.mDashOffset
);
269 cairo_set_line_join(aCtx
, GfxLineJoinToCairoLineJoin(aStrokeOptions
.mLineJoin
));
271 cairo_set_line_cap(aCtx
, GfxLineCapToCairoLineCap(aStrokeOptions
.mLineCap
));
274 static inline cairo_fill_rule_t
275 GfxFillRuleToCairoFillRule(FillRule rule
)
279 case FillRule::FILL_WINDING
:
280 return CAIRO_FILL_RULE_WINDING
;
281 case FillRule::FILL_EVEN_ODD
:
282 return CAIRO_FILL_RULE_EVEN_ODD
;
285 return CAIRO_FILL_RULE_WINDING
;
288 // RAII class for temporarily changing the cairo matrix transform. It will use
289 // the given matrix transform while it is in scope. When it goes out of scope
290 // it will put the cairo context back the way it was.
292 class CairoTempMatrix
295 CairoTempMatrix(cairo_t
* aCtx
, const Matrix
& aMatrix
)
298 cairo_get_matrix(aCtx
, &mSaveMatrix
);
299 cairo_matrix_t matrix
;
300 GfxMatrixToCairoMatrix(aMatrix
, matrix
);
301 cairo_set_matrix(aCtx
, &matrix
);
306 cairo_set_matrix(mCtx
, &mSaveMatrix
);
311 cairo_matrix_t mSaveMatrix
;
317 #endif /* MOZILLA_GFX_HELPERSCAIRO_H_ */