2006-09-25 Anders Carlsson <acarlsson@apple.com>
[WebKit-https.git] / WebCore / ksvg2 / misc / KCanvasRenderingStyle.cpp
1 /*
2     Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
3                   2004, 2005 Rob Buis <buis@kde.org>
4                         2006 Alexander Kellett <lypanov@kde.org>
5
6     This file is part of the KDE project
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Library General Public
10     License as published by the Free Software Foundation; either
11     version 2 of the License, or (at your option) any later version.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Library General Public License for more details.
17
18     You should have received a copy of the GNU Library General Public License
19     aint with this library; see the file COPYING.LIB.  If not, write to
20     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21     Boston, MA 02111-1307, USA.
22 */
23
24 #include "config.h"
25 #ifdef SVG_SUPPORT
26
27 #include "CSSValueList.h"
28 #include "Document.h"
29 #include "KCanvasRenderingStyle.h"
30 #include "KRenderingDevice.h"
31 #include "KRenderingFillPainter.h"
32 #include "KRenderingPaintServer.h"
33 #include "KRenderingPaintServerGradient.h"
34 #include "KRenderingPaintServerSolid.h"
35 #include "KRenderingStrokePainter.h"
36 #include "PlatformString.h"
37 #include "RenderObject.h"
38 #include "SVGLength.h"
39 #include "SVGRenderStyle.h"
40 #include "SVGStyledElement.h"
41 #include "ksvg.h"
42 #include "RenderStyle.h"
43
44 namespace WebCore {
45
46 static KRenderingPaintServerSolid* sharedSolidPaintServer()
47 {
48     static KRenderingPaintServerSolid* _sharedSolidPaintServer = 0;
49     if (!_sharedSolidPaintServer)
50         _sharedSolidPaintServer = static_cast<KRenderingPaintServerSolid *>(renderingDevice()->createPaintServer(PS_SOLID));
51     return _sharedSolidPaintServer;
52 }
53
54 bool KSVGPainterFactory::isFilled(const RenderStyle *style)
55 {
56     SVGPaint *fill = style->svgStyle()->fillPaint();
57     if (fill && fill->paintType() == SVGPaint::SVG_PAINTTYPE_NONE)
58         return false;
59     return true;
60 }
61
62 KRenderingPaintServer *KSVGPainterFactory::fillPaintServer(const RenderStyle* style, const RenderObject* item)
63 {
64     if (!isFilled(style))
65         return 0;
66
67     SVGPaint *fill = style->svgStyle()->fillPaint();
68
69     KRenderingPaintServer *fillPaintServer;
70     if (!fill) {
71         // initial value (black)
72         fillPaintServer = sharedSolidPaintServer();
73         static_cast<KRenderingPaintServerSolid *>(fillPaintServer)->setColor(Color::black);
74     } else if (fill->paintType() == SVGPaint::SVG_PAINTTYPE_URI) {
75         String id(fill->uri());
76         fillPaintServer = getPaintServerById(item->document(), AtomicString(id.substring(1)));
77         if (item && fillPaintServer && item->isRenderPath())
78             fillPaintServer->addClient(static_cast<const RenderPath*>(item));
79     } else {
80         fillPaintServer = sharedSolidPaintServer();
81         KRenderingPaintServerSolid *fillPaintServerSolid = static_cast<KRenderingPaintServerSolid *>(fillPaintServer);
82         if (fill->paintType() == SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR)
83             fillPaintServerSolid->setColor(style->color());
84         else
85             fillPaintServerSolid->setColor(fill->color());
86     }
87
88     return fillPaintServer;
89 }
90
91
92 bool KSVGPainterFactory::isStroked(const RenderStyle *style)
93 {
94     SVGPaint *stroke = style->svgStyle()->strokePaint();
95     if (!stroke || stroke->paintType() == SVGPaint::SVG_PAINTTYPE_NONE)
96         return false;
97     return true;
98 }
99
100 KRenderingPaintServer *KSVGPainterFactory::strokePaintServer(const RenderStyle* style, const RenderObject* item)
101 {
102     if (!isStroked(style))
103         return 0;
104
105     SVGPaint *stroke = style->svgStyle()->strokePaint();
106
107     KRenderingPaintServer *strokePaintServer;
108     if (stroke && stroke->paintType() == SVGPaint::SVG_PAINTTYPE_URI) {
109         String id(stroke->uri());
110         strokePaintServer = getPaintServerById(item->document(), AtomicString(id.substring(1)));
111         if(item && strokePaintServer && item->isRenderPath())
112             strokePaintServer->addClient(static_cast<const RenderPath*>(item));
113     } else {
114         strokePaintServer = sharedSolidPaintServer();
115         KRenderingPaintServerSolid *strokePaintServerSolid = static_cast<KRenderingPaintServerSolid *>(strokePaintServer);
116         if (stroke->paintType() == SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR)
117             strokePaintServerSolid->setColor(style->color());
118         else
119             strokePaintServerSolid->setColor(stroke->color());
120     }
121
122     return strokePaintServer;
123 }
124
125 double KSVGPainterFactory::cssPrimitiveToLength(const RenderObject* item, CSSValue *value, double defaultValue)
126 {
127     CSSPrimitiveValue *primitive = static_cast<CSSPrimitiveValue *>(value);
128
129     unsigned short cssType = (primitive ? primitive->primitiveType() : (unsigned short) CSSPrimitiveValue::CSS_UNKNOWN);
130     if(!(cssType > CSSPrimitiveValue::CSS_UNKNOWN && cssType <= CSSPrimitiveValue::CSS_PC))
131         return defaultValue;
132
133     if(cssType == CSSPrimitiveValue::CSS_PERCENTAGE)
134     {
135         SVGElement *element = static_cast<SVGElement *>(item->element());
136         SVGElement *viewportElement = (element ? element->viewportElement() : 0);
137         if(viewportElement)
138         {
139             double result = primitive->getFloatValue() / 100.0;
140             return SVGHelper::PercentageOfViewport(result, viewportElement, LM_OTHER);
141         }
142     }
143
144     return primitive->computeLengthFloat(const_cast<RenderStyle *>(item->style()));
145 }
146
147 KRenderingStrokePainter KSVGPainterFactory::strokePainter(const RenderStyle* style, const RenderObject* item)
148 {
149     KRenderingStrokePainter strokePainter;
150
151     strokePainter.setOpacity(style->svgStyle()->strokeOpacity());
152     strokePainter.setStrokeWidth(KSVGPainterFactory::cssPrimitiveToLength(item, style->svgStyle()->strokeWidth(), 1.0));
153
154     CSSValueList *dashes = style->svgStyle()->strokeDashArray();
155     if (dashes) {
156         CSSPrimitiveValue *dash = 0;
157         KCDashArray array;
158         unsigned long len = dashes->length();
159         for (unsigned long i = 0; i < len; i++) {
160             dash = static_cast<CSSPrimitiveValue *>(dashes->item(i));
161             if (dash)
162                 array.append((float) dash->computeLengthFloat(const_cast<RenderStyle *>(style)));
163         }
164
165         strokePainter.setDashArray(array);
166         strokePainter.setDashOffset(KSVGPainterFactory::cssPrimitiveToLength(item, style->svgStyle()->strokeDashOffset(), 0.0));
167     }
168
169     strokePainter.setStrokeMiterLimit(style->svgStyle()->strokeMiterLimit());
170     strokePainter.setStrokeCapStyle((KCCapStyle) style->svgStyle()->capStyle());
171     strokePainter.setStrokeJoinStyle((KCJoinStyle) style->svgStyle()->joinStyle());
172
173     return strokePainter;
174 }
175
176 KRenderingFillPainter KSVGPainterFactory::fillPainter(const RenderStyle* style, const RenderObject* item)
177 {
178     KRenderingFillPainter fillPainter;
179     
180     fillPainter.setFillRule(style->svgStyle()->fillRule());
181     fillPainter.setOpacity(style->svgStyle()->fillOpacity());
182
183     return fillPainter;
184 }
185
186 }
187
188 // vim:ts=4:noet
189 #endif // SVG_SUPPORT
190