2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "CanvasStyle.h"
32 #include "CSSParser.h"
33 #include "CanvasGradient.h"
34 #include "CanvasPattern.h"
35 #include "GraphicsContext.h"
36 #include <wtf/PassRefPtr.h>
39 #include <CoreGraphics/CGContext.h>
48 #include "NotImplemented.h"
53 CanvasStyle::CanvasStyle(const String& color)
59 CanvasStyle::CanvasStyle(float grayLevel)
62 , m_grayLevel(grayLevel)
66 CanvasStyle::CanvasStyle(const String& color, float alpha)
67 : m_type(ColorStringWithAlpha)
73 CanvasStyle::CanvasStyle(float grayLevel, float alpha)
76 , m_grayLevel(grayLevel)
80 CanvasStyle::CanvasStyle(float r, float g, float b, float a)
81 : m_type(RGBA), m_alpha(a), m_red(r), m_green(g), m_blue(b)
85 CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
86 : m_type(CMYKA), m_alpha(a), m_cyan(c), m_magenta(m), m_yellow(y), m_black(k)
90 CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
91 : m_type(gradient ? Gradient : ColorString)
92 , m_gradient(gradient)
96 CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
97 : m_type(pattern ? ImagePattern : ColorString)
102 void CanvasStyle::applyStrokeColor(GraphicsContext* context)
108 Color c = Color(m_color);
110 context->setStrokeColor(c.rgb());
113 RGBA32 color = 0; // default is transparent black
114 if (CSSParser::parseColor(color, m_color))
115 context->setStrokeColor(color);
118 case ColorStringWithAlpha: {
119 Color c = Color(m_color);
121 context->setStrokeColor(colorWithOverrideAlpha(c.rgb(), m_alpha));
124 RGBA32 color = 0; // default is transparent black
125 if (CSSParser::parseColor(color, m_color))
126 context->setStrokeColor(colorWithOverrideAlpha(color, m_alpha));
130 // We're only supporting 255 levels of gray here. Since this isn't
131 // even part of HTML5, I don't expect anyone will care. If they do
132 // we'll make a fancier Color abstraction.
133 context->setStrokeColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha));
136 context->setStrokeColor(Color(m_red, m_green, m_blue, m_alpha));
139 // FIXME: Do this through platform-independent GraphicsContext API.
140 // We'll need a fancier Color abstraction to support CYMKA correctly
142 CGContextSetCMYKStrokeColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
144 QPen currentPen = context->platformContext()->pen();
146 clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
147 currentPen.setColor(clr);
148 context->platformContext()->setPen(currentPen);
149 #elif PLATFORM(CAIRO)
155 context->setStrokeGradient(canvasGradient()->gradient());
158 context->setStrokePattern(canvasPattern()->pattern());
163 void CanvasStyle::applyFillColor(GraphicsContext* context)
169 Color c = Color(m_color);
171 context->setFillColor(c.rgb());
174 RGBA32 rgba = 0; // default is transparent black
175 if (CSSParser::parseColor(rgba, m_color))
176 context->setFillColor(rgba);
179 case ColorStringWithAlpha: {
180 Color c = Color(m_color);
182 context->setFillColor(colorWithOverrideAlpha(c.rgb(), m_alpha));
185 RGBA32 color = 0; // default is transparent black
186 if (CSSParser::parseColor(color, m_color))
187 context->setFillColor(colorWithOverrideAlpha(color, m_alpha));
191 // We're only supporting 255 levels of gray here. Since this isn't
192 // even part of HTML5, I don't expect anyone will care. If they do
193 // we'll make a fancier Color abstraction.
194 context->setFillColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha));
197 context->setFillColor(Color(m_red, m_green, m_blue, m_alpha));
200 // FIXME: Do this through platform-independent GraphicsContext API.
201 // We'll need a fancier Color abstraction to support CYMKA correctly
203 CGContextSetCMYKFillColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
205 QBrush currentBrush = context->platformContext()->brush();
207 clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
208 currentBrush.setColor(clr);
209 context->platformContext()->setBrush(currentBrush);
214 context->setFillGradient(canvasGradient()->gradient());
217 context->setFillPattern(canvasPattern()->pattern());