2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "CairoOperations.h"
33 #include "CairoUtilities.h"
34 #include "GraphicsContext.h"
35 #include "PlatformContextCairo.h"
39 void Gradient::platformDestroy()
43 cairo_pattern_t* Gradient::createPlatformGradient(float globalAlpha)
45 cairo_pattern_t* gradient = WTF::switchOn(m_data,
46 [&] (const LinearData& data) -> cairo_pattern_t* {
47 return cairo_pattern_create_linear(data.point0.x(), data.point0.y(), data.point1.x(), data.point1.y());
49 [&] (const RadialData& data) -> cairo_pattern_t* {
50 return cairo_pattern_create_radial(data.point0.x(), data.point0.y(), data.startRadius, data.point1.x(), data.point1.y(), data.endRadius);
54 for (const auto& stop : m_stops) {
55 if (stop.color.isExtended()) {
56 cairo_pattern_add_color_stop_rgba(gradient, stop.offset, stop.color.asExtended().red(), stop.color.asExtended().green(), stop.color.asExtended().blue(),
57 stop.color.asExtended().alpha() * globalAlpha);
60 stop.color.getRGBA(r, g, b, a);
61 cairo_pattern_add_color_stop_rgba(gradient, stop.offset, r, g, b, a * globalAlpha);
65 switch (m_spreadMethod) {
67 cairo_pattern_set_extend(gradient, CAIRO_EXTEND_PAD);
69 case SpreadMethodReflect:
70 cairo_pattern_set_extend(gradient, CAIRO_EXTEND_REFLECT);
72 case SpreadMethodRepeat:
73 cairo_pattern_set_extend(gradient, CAIRO_EXTEND_REPEAT);
77 cairo_matrix_t matrix = toCairoMatrix(m_gradientSpaceTransformation);
78 cairo_matrix_invert(&matrix);
79 cairo_pattern_set_matrix(gradient, &matrix);
84 void Gradient::fill(GraphicsContext& context, const FloatRect& rect)
86 ASSERT(context.hasPlatformContext());
87 auto& platformContext = *context.platformContext();
89 RefPtr<cairo_pattern_t> platformGradient = adoptRef(createPlatformGradient(1.0));
90 Cairo::save(platformContext);
91 Cairo::fillRect(platformContext, rect, platformGradient.get());
92 Cairo::restore(platformContext);
95 } // namespace WebCore