2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2008, 2009 Dirk Schulze <krit@webkit.org>
5 * Copyright (C) 2008 Nuanti Ltd.
6 * Copyright (C) 2009 Brent Fulgham <bfulgham@webkit.org>
7 * Copyright (C) 2010, 2011 Igalia S.L.
8 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
9 * Copyright (C) 2012, Intel Corporation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "GraphicsContext.h"
38 #include "AffineTransform.h"
39 #include "CairoUtilities.h"
40 #include "DrawErrorUnderline.h"
41 #include "FloatConversion.h"
42 #include "FloatRect.h"
43 #include "FloatRoundedRect.h"
45 #include "GraphicsContextPlatformPrivateCairo.h"
47 #include "NotImplemented.h"
50 #include "PlatformContextCairo.h"
51 #include "PlatformPathCairo.h"
52 #include "RefPtrCairo.h"
53 #include "ShadowBlur.h"
54 #include "TransformationMatrix.h"
58 #include <wtf/MathExtras.h>
61 #include <cairo-win32.h>
68 // A helper which quickly fills a rectangle with a simple color fill.
69 static inline void fillRectWithColor(cairo_t* cr, const FloatRect& rect, const Color& color)
71 if (!color.alpha() && cairo_get_operator(cr) == CAIRO_OPERATOR_OVER)
73 setSourceRGBAFromColor(cr, color);
74 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
78 static void addConvexPolygonToContext(cairo_t* context, size_t numPoints, const FloatPoint* points)
80 cairo_move_to(context, points[0].x(), points[0].y());
81 for (size_t i = 1; i < numPoints; i++)
82 cairo_line_to(context, points[i].x(), points[i].y());
83 cairo_close_path(context);
86 enum PathDrawingStyle {
89 FillAndStroke = Fill + Stroke
92 static inline void drawPathShadow(GraphicsContext& context, PathDrawingStyle drawingStyle)
94 ShadowBlur& shadow = context.platformContext()->shadowBlur();
95 if (shadow.type() == ShadowBlur::NoShadow)
98 // Calculate the extents of the rendered solid paths.
99 cairo_t* cairoContext = context.platformContext()->cr();
100 std::unique_ptr<cairo_path_t, void(*)(cairo_path_t*)> path(cairo_copy_path(cairoContext), [](cairo_path_t* path) {
101 cairo_path_destroy(path);
104 FloatRect solidFigureExtents;
109 if (drawingStyle & Stroke) {
110 cairo_stroke_extents(cairoContext, &x0, &y0, &x1, &y1);
111 solidFigureExtents = FloatRect(x0, y0, x1 - x0, y1 - y0);
113 if (drawingStyle & Fill) {
114 cairo_fill_extents(cairoContext, &x0, &y0, &x1, &y1);
115 FloatRect fillExtents(x0, y0, x1 - x0, y1 - y0);
116 solidFigureExtents.unite(fillExtents);
119 GraphicsContext* shadowContext = shadow.beginShadowLayer(context, solidFigureExtents);
123 cairo_t* cairoShadowContext = shadowContext->platformContext()->cr();
125 // It's important to copy the context properties to the new shadow
126 // context to preserve things such as the fill rule and stroke width.
127 copyContextProperties(cairoContext, cairoShadowContext);
129 if (drawingStyle & Fill) {
130 cairo_save(cairoShadowContext);
131 cairo_append_path(cairoShadowContext, path.get());
132 shadowContext->platformContext()->prepareForFilling(context.state(), PlatformContextCairo::NoAdjustment);
133 cairo_fill(cairoShadowContext);
134 cairo_restore(cairoShadowContext);
137 if (drawingStyle & Stroke) {
138 cairo_append_path(cairoShadowContext, path.get());
139 shadowContext->platformContext()->prepareForStroking(context.state(), PlatformContextCairo::DoNotPreserveAlpha);
140 cairo_stroke(cairoShadowContext);
143 // The original path may still be hanging around on the context and endShadowLayer
144 // will take care of properly creating a path to draw the result shadow. We remove the path
145 // temporarily and then restore it.
146 // See: https://bugs.webkit.org/show_bug.cgi?id=108897
147 cairo_new_path(cairoContext);
148 shadow.endShadowLayer(context);
149 cairo_append_path(cairoContext, path.get());
152 static inline void fillCurrentCairoPath(GraphicsContext& context)
154 cairo_t* cr = context.platformContext()->cr();
157 context.platformContext()->prepareForFilling(context.state(), PlatformContextCairo::AdjustPatternForGlobalAlpha);
163 static inline void shadowAndFillCurrentCairoPath(GraphicsContext& context)
165 drawPathShadow(context, Fill);
166 fillCurrentCairoPath(context);
169 static inline void shadowAndStrokeCurrentCairoPath(GraphicsContext& context)
171 drawPathShadow(context, Stroke);
172 context.platformContext()->prepareForStroking(context.state());
173 cairo_stroke(context.platformContext()->cr());
176 GraphicsContext::GraphicsContext(cairo_t* cr)
177 : m_updatingControlTints(false)
178 , m_transparencyCount(0)
180 m_data = new GraphicsContextPlatformPrivateToplevel(new PlatformContextCairo(cr));
183 void GraphicsContext::platformInit(PlatformContextCairo* platformContext)
185 m_data = new GraphicsContextPlatformPrivate(platformContext);
187 m_data->syncContext(platformContext->cr());
189 setPaintingDisabled(true);
192 void GraphicsContext::platformDestroy()
197 void GraphicsContext::resetPlatformCTM()
199 if (platformContext())
200 m_state.ctm = getPlatformCTM();
202 m_state.ctm.makeIdentity();
205 AffineTransform GraphicsContext::getPlatformCTM(IncludeDeviceScale) const
207 if (paintingDisabled())
208 return AffineTransform();
210 cairo_t* cr = platformContext()->cr();
212 cairo_get_matrix(cr, &m);
213 return AffineTransform(m.xx, m.yx, m.xy, m.yy, m.x0, m.y0);
216 PlatformContextCairo* GraphicsContext::platformContext() const
218 return m_data->platformContext;
221 void GraphicsContext::savePlatformState()
223 platformContext()->save();
227 void GraphicsContext::restorePlatformState()
229 platformContext()->restore();
232 platformContext()->shadowBlur().setShadowValues(FloatSize(m_state.shadowBlur, m_state.shadowBlur),
233 m_state.shadowOffset,
235 m_state.shadowColorSpace,
236 m_state.shadowsIgnoreTransforms);
239 // Draws a filled rectangle with a stroked border.
240 void GraphicsContext::drawRect(const FloatRect& rect, float)
242 if (paintingDisabled())
245 ASSERT(!rect.isEmpty());
247 cairo_t* cr = platformContext()->cr();
250 fillRectWithColor(cr, rect, fillColor());
252 if (strokeStyle() != NoStroke) {
253 setSourceRGBAFromColor(cr, strokeColor());
256 cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height());
257 cairo_set_line_width(cr, 1.0);
264 static double calculateStrokePatternOffset(int distance, int patternWidth)
266 // Example: 80 pixels with a width of 30 pixels. Remainder is 20.
267 // The maximum pixels of line we could paint will be 50 pixels.
268 int remainder = distance % patternWidth;
269 int numSegments = (distance - remainder) / patternWidth;
271 // Special case 1px dotted borders for speed.
272 if (patternWidth == 1)
275 bool evenNumberOfSegments = !(numSegments % 2);
277 evenNumberOfSegments = !evenNumberOfSegments;
279 if (evenNumberOfSegments) {
281 return (patternWidth - remainder) + (remainder / 2);
282 return patternWidth / 2;
285 // Odd number of segments.
287 return (patternWidth - remainder) / 2.f;
291 static void drawLineOnCairoContext(GraphicsContext* graphicsContext, cairo_t* context, const FloatPoint& point1, const FloatPoint& point2)
293 StrokeStyle style = graphicsContext->strokeStyle();
294 if (style == NoStroke)
297 const Color& strokeColor = graphicsContext->strokeColor();
298 int strokeThickness = floorf(graphicsContext->strokeThickness());
299 if (graphicsContext->strokeThickness() < 1)
302 int patternWidth = 0;
303 if (style == DottedStroke)
304 patternWidth = strokeThickness;
305 else if (style == DashedStroke)
306 patternWidth = 3 * strokeThickness;
308 bool isVerticalLine = point1.x() == point2.x();
309 FloatPoint point1OnPixelBoundaries = point1;
310 FloatPoint point2OnPixelBoundaries = point2;
311 GraphicsContext::adjustLineToPixelBoundaries(point1OnPixelBoundaries, point2OnPixelBoundaries, strokeThickness, style);
314 // Do a rect fill of our endpoints. This ensures we always have the
315 // appearance of being a border. We then draw the actual dotted/dashed line.
316 FloatRect firstRect(point1OnPixelBoundaries, FloatSize(strokeThickness, strokeThickness));
317 FloatRect secondRect(point2OnPixelBoundaries, FloatSize(strokeThickness, strokeThickness));
318 if (isVerticalLine) {
319 firstRect.move(-strokeThickness / 2, -strokeThickness);
320 secondRect.move(-strokeThickness / 2, 0);
322 firstRect.move(-strokeThickness, -strokeThickness / 2);
323 secondRect.move(0, -strokeThickness / 2);
325 fillRectWithColor(context, firstRect, strokeColor);
326 fillRectWithColor(context, secondRect, strokeColor);
328 int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2 * strokeThickness;
329 double patternOffset = calculateStrokePatternOffset(distance, patternWidth);
330 double patternWidthAsDouble = patternWidth;
331 cairo_set_dash(context, &patternWidthAsDouble, 1, patternOffset);
334 setSourceRGBAFromColor(context, strokeColor);
335 cairo_set_line_width(context, strokeThickness);
336 cairo_move_to(context, point1OnPixelBoundaries.x(), point1OnPixelBoundaries.y());
337 cairo_line_to(context, point2OnPixelBoundaries.x(), point2OnPixelBoundaries.y());
338 cairo_stroke(context);
341 // This is only used to draw borders, so we should not draw shadows.
342 void GraphicsContext::drawLine(const FloatPoint& point1, const FloatPoint& point2)
344 if (paintingDisabled())
347 cairo_t* cairoContext = platformContext()->cr();
348 cairo_save(cairoContext);
349 drawLineOnCairoContext(this, cairoContext, point1, point2);
350 cairo_restore(cairoContext);
353 // This method is only used to draw the little circles used in lists.
354 void GraphicsContext::drawEllipse(const FloatRect& rect)
356 if (paintingDisabled())
359 cairo_t* cr = platformContext()->cr();
361 float yRadius = .5 * rect.height();
362 float xRadius = .5 * rect.width();
363 cairo_translate(cr, rect.x() + xRadius, rect.y() + yRadius);
364 cairo_scale(cr, xRadius, yRadius);
365 cairo_arc(cr, 0., 0., 1., 0., 2 * piFloat);
368 if (fillColor().alpha()) {
369 setSourceRGBAFromColor(cr, fillColor());
370 cairo_fill_preserve(cr);
373 if (strokeStyle() != NoStroke) {
374 setSourceRGBAFromColor(cr, strokeColor());
375 cairo_set_line_width(cr, strokeThickness());
381 void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias)
383 if (paintingDisabled())
389 cairo_t* cr = platformContext()->cr();
392 cairo_set_antialias(cr, shouldAntialias ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
393 addConvexPolygonToContext(cr, npoints, points);
395 if (fillColor().alpha()) {
396 setSourceRGBAFromColor(cr, fillColor());
397 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
398 cairo_fill_preserve(cr);
401 if (strokeStyle() != NoStroke) {
402 setSourceRGBAFromColor(cr, strokeColor());
403 cairo_set_line_width(cr, strokeThickness());
411 void GraphicsContext::clipConvexPolygon(size_t numPoints, const FloatPoint* points, bool antialiased)
413 if (paintingDisabled())
419 cairo_t* cr = platformContext()->cr();
422 cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
423 cairo_antialias_t savedAntialiasRule = cairo_get_antialias(cr);
425 cairo_set_antialias(cr, antialiased ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
426 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
427 addConvexPolygonToContext(cr, numPoints, points);
430 cairo_set_antialias(cr, savedAntialiasRule);
431 cairo_set_fill_rule(cr, savedFillRule);
434 void GraphicsContext::fillPath(const Path& path)
436 if (paintingDisabled() || path.isEmpty())
439 cairo_t* cr = platformContext()->cr();
440 setPathOnCairoContext(cr, path.platformPath()->context());
441 shadowAndFillCurrentCairoPath(*this);
444 void GraphicsContext::strokePath(const Path& path)
446 if (paintingDisabled() || path.isEmpty())
449 cairo_t* cr = platformContext()->cr();
450 setPathOnCairoContext(cr, path.platformPath()->context());
451 shadowAndStrokeCurrentCairoPath(*this);
454 void GraphicsContext::fillRect(const FloatRect& rect)
456 if (paintingDisabled())
459 cairo_t* cr = platformContext()->cr();
460 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
461 shadowAndFillCurrentCairoPath(*this);
464 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace)
466 if (paintingDisabled())
470 platformContext()->shadowBlur().drawRectShadow(*this, FloatRoundedRect(rect));
472 fillRectWithColor(platformContext()->cr(), rect, color);
475 void GraphicsContext::clip(const FloatRect& rect)
477 if (paintingDisabled())
480 cairo_t* cr = platformContext()->cr();
481 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
482 cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
483 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
484 // The rectangular clip function is traditionally not expected to
485 // antialias. If we don't force antialiased clipping here,
486 // edge fringe artifacts may occur at the layer edges
487 // when a transformation is applied to the GraphicsContext
488 // while drawing the transformed layer.
489 cairo_antialias_t savedAntialiasRule = cairo_get_antialias(cr);
490 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
492 cairo_set_fill_rule(cr, savedFillRule);
493 cairo_set_antialias(cr, savedAntialiasRule);
497 void GraphicsContext::clipPath(const Path& path, WindRule clipRule)
499 if (paintingDisabled())
502 cairo_t* cr = platformContext()->cr();
504 setPathOnCairoContext(cr, path.platformPath()->context());
505 cairo_set_fill_rule(cr, clipRule == RULE_EVENODD ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
509 IntRect GraphicsContext::clipBounds() const
511 double x1, x2, y1, y2;
512 cairo_clip_extents(platformContext()->cr(), &x1, &y1, &x2, &y2);
513 return enclosingIntRect(FloatRect(x1, y1, x2 - x1, y2 - y1));
516 static inline void adjustFocusRingColor(Color& color)
519 // Force the alpha to 50%. This matches what the Mac does with outline rings.
520 color.setRGB(makeRGBA(color.red(), color.green(), color.blue(), 127));
526 static inline void adjustFocusRingLineWidth(int& width)
535 static inline StrokeStyle focusRingStrokeStyle()
544 void GraphicsContext::drawFocusRing(const Path& path, int width, int /* offset */, const Color& color)
546 // FIXME: We should draw paths that describe a rectangle with rounded corners
547 // so as to be consistent with how we draw rectangular focus rings.
548 Color ringColor = color;
549 adjustFocusRingColor(ringColor);
550 adjustFocusRingLineWidth(width);
552 cairo_t* cr = platformContext()->cr();
554 appendWebCorePathToCairoContext(cr, path);
555 setSourceRGBAFromColor(cr, ringColor);
556 cairo_set_line_width(cr, width);
557 setPlatformStrokeStyle(focusRingStrokeStyle());
562 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int width, int /* offset */, const Color& color)
564 if (paintingDisabled())
567 cairo_t* cr = platformContext()->cr();
569 cairo_push_group(cr);
573 for (const auto& rect : rects)
574 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
576 unsigned rectCount = rects.size();
577 int radius = (width - 1) / 2;
579 for (unsigned i = 0; i < rectCount; ++i) {
582 path.addRoundedRect(rects[i], FloatSize(radius, radius));
583 appendWebCorePathToCairoContext(cr, path);
586 Color ringColor = color;
587 adjustFocusRingColor(ringColor);
588 adjustFocusRingLineWidth(width);
589 setSourceRGBAFromColor(cr, ringColor);
590 cairo_set_line_width(cr, width);
591 setPlatformStrokeStyle(focusRingStrokeStyle());
593 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
594 cairo_stroke_preserve(cr);
596 cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
597 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
600 cairo_pop_group_to_source(cr);
601 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
606 FloatRect GraphicsContext::computeLineBoundsForText(const FloatPoint& origin, float width, bool printing)
610 return computeLineBoundsAndAntialiasingModeForText(origin, width, printing, dummyBool, dummyColor);
613 void GraphicsContext::drawLineForText(const FloatPoint& origin, float width, bool printing, bool doubleUnderlines)
616 widths.append(width);
618 drawLinesForText(origin, widths, printing, doubleUnderlines);
621 void GraphicsContext::drawLinesForText(const FloatPoint& point, const DashArray& widths, bool printing, bool doubleUnderlines)
623 if (paintingDisabled())
626 if (widths.size() <= 0)
629 Color localStrokeColor(strokeColor());
631 bool shouldAntialiasLine;
632 FloatRect bounds = computeLineBoundsAndAntialiasingModeForText(point, widths.last(), printing, shouldAntialiasLine, localStrokeColor);
634 Vector<FloatRect, 4> dashBounds;
635 ASSERT(!(widths.size() % 2));
636 dashBounds.reserveInitialCapacity(dashBounds.size() / 2);
637 for (size_t i = 0; i < widths.size(); i += 2)
638 dashBounds.append(FloatRect(FloatPoint(bounds.x() + widths[i], bounds.y()), FloatSize(widths[i+1] - widths[i], bounds.height())));
640 if (doubleUnderlines) {
641 // The space between double underlines is equal to the height of the underline
642 for (size_t i = 0; i < widths.size(); i += 2)
643 dashBounds.append(FloatRect(FloatPoint(bounds.x() + widths[i], bounds.y() + 2 * bounds.height()), FloatSize(widths[i+1] - widths[i], bounds.height())));
646 cairo_t* cr = platformContext()->cr();
649 for (auto& dash : dashBounds)
650 fillRectWithColor(cr, dash, localStrokeColor);
655 void GraphicsContext::updateDocumentMarkerResources()
657 // Unnecessary, since our document markers don't use resources.
660 void GraphicsContext::drawLineForDocumentMarker(const FloatPoint& origin, float width, DocumentMarkerLineStyle style)
662 if (paintingDisabled())
665 cairo_t* cr = platformContext()->cr();
669 case DocumentMarkerSpellingLineStyle:
670 cairo_set_source_rgb(cr, 1, 0, 0);
672 case DocumentMarkerGrammarLineStyle:
673 cairo_set_source_rgb(cr, 0, 1, 0);
680 drawErrorUnderline(cr, origin.x(), origin.y(), width, cMisspellingLineThickness);
685 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect, RoundingMode)
688 double x = frect.x();
689 double y = frect.y();
690 cairo_t* cr = platformContext()->cr();
691 cairo_user_to_device(cr, &x, &y);
694 cairo_device_to_user(cr, &x, &y);
695 result.setX(narrowPrecisionToFloat(x));
696 result.setY(narrowPrecisionToFloat(y));
698 // We must ensure width and height are at least 1 (or -1) when
699 // we're given float values in the range between 0 and 1 (or -1 and 0).
700 double width = frect.width();
701 double height = frect.height();
702 cairo_user_to_device_distance(cr, &width, &height);
703 if (width > -1 && width < 0)
705 else if (width > 0 && width < 1)
708 width = round(width);
709 if (height > -1 && width < 0)
711 else if (height > 0 && height < 1)
714 height = round(height);
715 cairo_device_to_user_distance(cr, &width, &height);
716 result.setWidth(narrowPrecisionToFloat(width));
717 result.setHeight(narrowPrecisionToFloat(height));
722 void GraphicsContext::translatePlatformCTM(float x, float y)
724 if (paintingDisabled())
727 cairo_t* cr = platformContext()->cr();
728 cairo_translate(cr, x, y);
729 m_data->translatePlatformCTM(x, y);
732 void GraphicsContext::setPlatformFillColor(const Color&, ColorSpace)
734 // Cairo contexts can't hold separate fill and stroke colors
735 // so we set them just before we actually fill or stroke
738 void GraphicsContext::setPlatformStrokeColor(const Color&, ColorSpace)
740 // Cairo contexts can't hold separate fill and stroke colors
741 // so we set them just before we actually fill or stroke
744 void GraphicsContext::setPlatformStrokeThickness(float strokeThickness)
746 if (paintingDisabled())
749 cairo_set_line_width(platformContext()->cr(), strokeThickness);
752 void GraphicsContext::setPlatformStrokeStyle(StrokeStyle strokeStyle)
754 static const double dashPattern[] = { 5.0, 5.0 };
755 static const double dotPattern[] = { 1.0, 1.0 };
757 if (paintingDisabled())
760 switch (strokeStyle) {
762 // FIXME: is it the right way to emulate NoStroke?
763 cairo_set_line_width(platformContext()->cr(), 0);
767 case WavyStroke: // FIXME: https://bugs.webkit.org/show_bug.cgi?id=94110 - Needs platform support.
768 cairo_set_dash(platformContext()->cr(), 0, 0, 0);
771 cairo_set_dash(platformContext()->cr(), dotPattern, 2, 0);
774 cairo_set_dash(platformContext()->cr(), dashPattern, 2, 0);
779 void GraphicsContext::setURLForRect(const URL&, const IntRect&)
784 void GraphicsContext::concatPlatformCTM(const AffineTransform& transform)
786 if (paintingDisabled())
789 cairo_t* cr = platformContext()->cr();
790 const cairo_matrix_t matrix = cairo_matrix_t(transform);
791 cairo_transform(cr, &matrix);
792 m_data->concatPlatformCTM(transform);
795 void GraphicsContext::setPlatformCTM(const AffineTransform& transform)
797 if (paintingDisabled())
800 cairo_t* cr = platformContext()->cr();
801 const cairo_matrix_t matrix = cairo_matrix_t(transform);
802 cairo_set_matrix(cr, &matrix);
803 m_data->setPlatformCTM(transform);
806 void GraphicsContext::setPlatformShadow(FloatSize const& size, float, Color const&, ColorSpace)
808 if (paintingDisabled())
811 if (m_state.shadowsIgnoreTransforms) {
812 // Meaning that this graphics context is associated with a CanvasRenderingContext
813 // We flip the height since CG and HTML5 Canvas have opposite Y axis
814 m_state.shadowOffset = FloatSize(size.width(), -size.height());
817 // Cairo doesn't support shadows natively, they are drawn manually in the draw* functions using ShadowBlur.
818 platformContext()->shadowBlur().setShadowValues(FloatSize(m_state.shadowBlur, m_state.shadowBlur),
819 m_state.shadowOffset,
821 m_state.shadowColorSpace,
822 m_state.shadowsIgnoreTransforms);
825 void GraphicsContext::clearPlatformShadow()
827 if (paintingDisabled())
830 platformContext()->shadowBlur().clear();
833 void GraphicsContext::beginPlatformTransparencyLayer(float opacity)
835 if (paintingDisabled())
838 cairo_t* cr = platformContext()->cr();
839 cairo_push_group(cr);
840 m_data->layers.append(opacity);
843 void GraphicsContext::endPlatformTransparencyLayer()
845 if (paintingDisabled())
848 cairo_t* cr = platformContext()->cr();
850 cairo_pop_group_to_source(cr);
851 cairo_paint_with_alpha(cr, m_data->layers.last());
852 m_data->layers.removeLast();
855 bool GraphicsContext::supportsTransparencyLayers()
860 void GraphicsContext::clearRect(const FloatRect& rect)
862 if (paintingDisabled())
865 cairo_t* cr = platformContext()->cr();
868 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
869 cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
874 void GraphicsContext::strokeRect(const FloatRect& rect, float width)
876 if (paintingDisabled())
879 cairo_t* cr = platformContext()->cr();
881 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
882 cairo_set_line_width(cr, width);
883 shadowAndStrokeCurrentCairoPath(*this);
887 void GraphicsContext::setLineCap(LineCap lineCap)
889 if (paintingDisabled())
892 cairo_line_cap_t cairoCap = CAIRO_LINE_CAP_BUTT;
898 cairoCap = CAIRO_LINE_CAP_ROUND;
901 cairoCap = CAIRO_LINE_CAP_SQUARE;
904 cairo_set_line_cap(platformContext()->cr(), cairoCap);
907 static inline bool isDashArrayAllZero(const DashArray& dashes)
909 for (auto& dash : dashes) {
916 void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
918 if (isDashArrayAllZero(dashes))
919 cairo_set_dash(platformContext()->cr(), 0, 0, 0);
921 cairo_set_dash(platformContext()->cr(), dashes.data(), dashes.size(), dashOffset);
924 void GraphicsContext::setLineJoin(LineJoin lineJoin)
926 if (paintingDisabled())
929 cairo_line_join_t cairoJoin = CAIRO_LINE_JOIN_MITER;
935 cairoJoin = CAIRO_LINE_JOIN_ROUND;
938 cairoJoin = CAIRO_LINE_JOIN_BEVEL;
941 cairo_set_line_join(platformContext()->cr(), cairoJoin);
944 void GraphicsContext::setMiterLimit(float miter)
946 if (paintingDisabled())
949 cairo_set_miter_limit(platformContext()->cr(), miter);
952 void GraphicsContext::setPlatformAlpha(float alpha)
954 platformContext()->setGlobalAlpha(alpha);
957 void GraphicsContext::setPlatformCompositeOperation(CompositeOperator op, BlendMode blendOp)
959 if (paintingDisabled())
962 cairo_operator_t cairo_op;
963 if (blendOp == BlendModeNormal)
964 cairo_op = toCairoOperator(op);
966 cairo_op = toCairoOperator(blendOp);
968 cairo_set_operator(platformContext()->cr(), cairo_op);
971 void GraphicsContext::clip(const Path& path, WindRule windRule)
973 if (paintingDisabled())
976 cairo_t* cr = platformContext()->cr();
977 if (!path.isNull()) {
978 cairo_path_t* pathCopy = cairo_copy_path(path.platformPath()->context());
979 cairo_append_path(cr, pathCopy);
980 cairo_path_destroy(pathCopy);
982 cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
983 if (windRule == RULE_NONZERO)
984 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
986 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
988 cairo_set_fill_rule(cr, savedFillRule);
992 void GraphicsContext::canvasClip(const Path& path, WindRule windRule)
994 clip(path, windRule);
997 void GraphicsContext::clipOut(const Path& path)
999 if (paintingDisabled())
1002 cairo_t* cr = platformContext()->cr();
1003 double x1, y1, x2, y2;
1004 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
1005 cairo_rectangle(cr, x1, y1, x2 - x1, y2 - y1);
1006 appendWebCorePathToCairoContext(cr, path);
1008 cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
1009 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
1011 cairo_set_fill_rule(cr, savedFillRule);
1014 void GraphicsContext::rotatePlatformCTM(float radians)
1016 if (paintingDisabled())
1019 cairo_rotate(platformContext()->cr(), radians);
1020 m_data->rotatePlatformCTM(radians);
1023 void GraphicsContext::scalePlatformCTM(float x, float y)
1025 if (paintingDisabled())
1028 cairo_scale(platformContext()->cr(), x, y);
1029 m_data->scalePlatformCTM(x, y);
1032 void GraphicsContext::clipOut(const FloatRect& r)
1034 if (paintingDisabled())
1037 cairo_t* cr = platformContext()->cr();
1038 double x1, y1, x2, y2;
1039 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
1040 cairo_rectangle(cr, x1, y1, x2 - x1, y2 - y1);
1041 cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height());
1042 cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
1043 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
1045 cairo_set_fill_rule(cr, savedFillRule);
1048 void GraphicsContext::platformFillRoundedRect(const FloatRoundedRect& rect, const Color& color, ColorSpace)
1050 if (paintingDisabled())
1054 platformContext()->shadowBlur().drawRectShadow(*this, rect);
1056 cairo_t* cr = platformContext()->cr();
1059 path.addRoundedRect(rect);
1060 appendWebCorePathToCairoContext(cr, path);
1061 setSourceRGBAFromColor(cr, color);
1066 void GraphicsContext::fillRectWithRoundedHole(const FloatRect& rect, const FloatRoundedRect& roundedHoleRect, const Color& color, ColorSpace)
1068 if (paintingDisabled() || !color.isValid())
1071 if (this->mustUseShadowBlur())
1072 platformContext()->shadowBlur().drawInsetShadow(*this, rect, roundedHoleRect);
1076 if (!roundedHoleRect.radii().isZero())
1077 path.addRoundedRect(roundedHoleRect);
1079 path.addRect(roundedHoleRect.rect());
1081 cairo_t* cr = platformContext()->cr();
1083 setPathOnCairoContext(platformContext()->cr(), path.platformPath()->context());
1084 fillCurrentCairoPath(*this);
1088 void GraphicsContext::drawPattern(Image& image, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize&, ColorSpace, CompositeOperator op, const FloatRect& destRect, BlendMode)
1090 RefPtr<cairo_surface_t> surface = image.nativeImageForCurrentFrame();
1091 if (!surface) // If it's too early we won't have an image yet.
1094 cairo_t* cr = platformContext()->cr();
1095 drawPatternToCairoContext(cr, surface.get(), IntSize(image.size()), tileRect, patternTransform, phase, toCairoOperator(op), destRect);
1098 void GraphicsContext::setPlatformShouldAntialias(bool enable)
1100 if (paintingDisabled())
1103 // When true, use the default Cairo backend antialias mode (usually this
1104 // enables standard 'grayscale' antialiasing); false to explicitly disable
1105 // antialiasing. This is the same strategy as used in drawConvexPolygon().
1106 cairo_set_antialias(platformContext()->cr(), enable ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
1109 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality quality)
1111 platformContext()->setImageInterpolationQuality(quality);
1114 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
1116 return platformContext()->imageInterpolationQuality();
1119 bool GraphicsContext::isAcceleratedContext() const
1121 return cairo_surface_get_type(cairo_get_target(platformContext()->cr())) == CAIRO_SURFACE_TYPE_GL;
1124 #if ENABLE(3D_TRANSFORMS) && USE(TEXTURE_MAPPER)
1125 TransformationMatrix GraphicsContext::get3DTransform() const
1127 // FIXME: Can we approximate the transformation better than this?
1128 return getCTM().toTransformationMatrix();
1131 void GraphicsContext::concat3DTransform(const TransformationMatrix& transform)
1133 concatCTM(transform.toAffineTransform());
1136 void GraphicsContext::set3DTransform(const TransformationMatrix& transform)
1138 setCTM(transform.toAffineTransform());
1140 #endif // ENABLE(3D_TRANSFORMS) && USE(TEXTURE_MAPPER)
1142 } // namespace WebCore
1144 #endif // USE(CAIRO)