1 | /* |
2 | * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2007 Alp Toker <alp@atoker.com> |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
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. |
13 | * |
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. |
25 | */ |
26 | |
27 | #include "config.h" |
28 | #include "Gradient.h" |
29 | |
30 | #if USE(CAIRO) |
31 | |
32 | #include "CairoOperations.h" |
33 | #include "CairoUtilities.h" |
34 | #include "GraphicsContext.h" |
35 | #include "PlatformContextCairo.h" |
36 | |
37 | namespace WebCore { |
38 | |
39 | void Gradient::platformDestroy() |
40 | { |
41 | } |
42 | |
43 | cairo_pattern_t* Gradient::createPlatformGradient(float globalAlpha) |
44 | { |
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()); |
48 | }, |
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); |
51 | }, |
52 | [&] (const ConicData&) -> cairo_pattern_t* { |
53 | // FIXME: implement conic gradient rendering. |
54 | return nullptr; |
55 | } |
56 | ); |
57 | |
58 | for (const auto& stop : m_stops) { |
59 | if (stop.color.isExtended()) { |
60 | cairo_pattern_add_color_stop_rgba(gradient, stop.offset, stop.color.asExtended().red(), stop.color.asExtended().green(), stop.color.asExtended().blue(), |
61 | stop.color.asExtended().alpha() * globalAlpha); |
62 | } else { |
63 | float r, g, b, a; |
64 | stop.color.getRGBA(r, g, b, a); |
65 | cairo_pattern_add_color_stop_rgba(gradient, stop.offset, r, g, b, a * globalAlpha); |
66 | } |
67 | } |
68 | |
69 | switch (m_spreadMethod) { |
70 | case SpreadMethodPad: |
71 | cairo_pattern_set_extend(gradient, CAIRO_EXTEND_PAD); |
72 | break; |
73 | case SpreadMethodReflect: |
74 | cairo_pattern_set_extend(gradient, CAIRO_EXTEND_REFLECT); |
75 | break; |
76 | case SpreadMethodRepeat: |
77 | cairo_pattern_set_extend(gradient, CAIRO_EXTEND_REPEAT); |
78 | break; |
79 | } |
80 | |
81 | cairo_matrix_t matrix = toCairoMatrix(m_gradientSpaceTransformation); |
82 | cairo_matrix_invert(&matrix); |
83 | cairo_pattern_set_matrix(gradient, &matrix); |
84 | |
85 | return gradient; |
86 | } |
87 | |
88 | void Gradient::fill(GraphicsContext& context, const FloatRect& rect) |
89 | { |
90 | RefPtr<cairo_pattern_t> platformGradient = adoptRef(createPlatformGradient(1.0)); |
91 | if (!platformGradient) |
92 | return; |
93 | |
94 | ASSERT(context.hasPlatformContext()); |
95 | auto& platformContext = *context.platformContext(); |
96 | |
97 | Cairo::save(platformContext); |
98 | Cairo::fillRect(platformContext, rect, platformGradient.get()); |
99 | Cairo::restore(platformContext); |
100 | } |
101 | |
102 | } // namespace WebCore |
103 | |
104 | #endif // USE(CAIRO) |
105 | |