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>
6 This file is part of the KDE project
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.
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.
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.
27 #include <render_object.h>
29 #include <kcanvas/KCanvas.h>
30 #include <kcanvas/KCanvasPath.h>
31 #include <kcanvas/device/KRenderingDevice.h>
32 #include <kcanvas/device/KRenderingFillPainter.h>
33 #include <kcanvas/device/KRenderingStrokePainter.h>
34 #include <kcanvas/device/KRenderingPaintServerSolid.h>
35 #include <kcanvas/device/KRenderingPaintServerGradient.h>
36 #include <kcanvas/device/KRenderingPaintServer.h>
38 #include "DocumentImpl.h"
39 #include "PlatformString.h"
40 #include "render_style.h"
41 #include "css_valueimpl.h"
44 #include "SVGLengthImpl.h"
45 #include "SVGStyledElementImpl.h"
46 #include "KCanvasRenderingStyle.h"
47 #include "SVGRenderStyle.h"
51 static KRenderingPaintServerSolid* sharedSolidPaintServer()
53 static KRenderingPaintServerSolid* _sharedSolidPaintServer = 0;
54 if (!_sharedSolidPaintServer)
55 _sharedSolidPaintServer = static_cast<KRenderingPaintServerSolid *>(QPainter::renderingDevice()->createPaintServer(PS_SOLID));
56 return _sharedSolidPaintServer;
59 bool KSVGPainterFactory::isFilled(const RenderStyle *style)
61 SVGPaintImpl *fill = style->svgStyle()->fillPaint();
62 if (fill && fill->paintType() == SVG_PAINTTYPE_NONE)
67 KRenderingPaintServer *KSVGPainterFactory::fillPaintServer(const RenderStyle* style, const RenderObject* item)
72 SVGPaintImpl *fill = style->svgStyle()->fillPaint();
74 KRenderingPaintServer *fillPaintServer;
76 // initial value (black)
77 fillPaintServer = sharedSolidPaintServer();
78 static_cast<KRenderingPaintServerSolid *>(fillPaintServer)->setColor(Color::black);
79 } else if (fill->paintType() == SVG_PAINTTYPE_URI) {
80 DOMString id(fill->uri());
81 fillPaintServer = getPaintServerById(item->document(), id.qstring().mid(1));
82 if (item && fillPaintServer && item->isRenderPath())
83 fillPaintServer->addClient(static_cast<const RenderPath*>(item));
85 fillPaintServer = sharedSolidPaintServer();
86 KRenderingPaintServerSolid *fillPaintServerSolid = static_cast<KRenderingPaintServerSolid *>(fillPaintServer);
87 if (fill->paintType() == SVG_PAINTTYPE_CURRENTCOLOR)
88 fillPaintServerSolid->setColor(style->color());
90 fillPaintServerSolid->setColor(fill->color());
93 return fillPaintServer;
97 bool KSVGPainterFactory::isStroked(const RenderStyle *style)
99 SVGPaintImpl *stroke = style->svgStyle()->strokePaint();
100 if (!stroke || stroke->paintType() == SVG_PAINTTYPE_NONE)
105 KRenderingPaintServer *KSVGPainterFactory::strokePaintServer(const RenderStyle* style, const RenderObject* item)
107 if (!isStroked(style))
110 SVGPaintImpl *stroke = style->svgStyle()->strokePaint();
112 KRenderingPaintServer *strokePaintServer;
113 if (stroke && stroke->paintType() == SVG_PAINTTYPE_URI) {
114 DOMString id(stroke->uri());
115 strokePaintServer = getPaintServerById(item->document(), id.qstring().mid(1));
116 if(item && strokePaintServer && item->isRenderPath())
117 strokePaintServer->addClient(static_cast<const RenderPath*>(item));
119 strokePaintServer = sharedSolidPaintServer();
120 KRenderingPaintServerSolid *strokePaintServerSolid = static_cast<KRenderingPaintServerSolid *>(strokePaintServer);
121 if (stroke->paintType() == SVG_PAINTTYPE_CURRENTCOLOR)
122 strokePaintServerSolid->setColor(style->color());
124 strokePaintServerSolid->setColor(stroke->color());
127 return strokePaintServer;
130 double KSVGPainterFactory::cssPrimitiveToLength(const RenderObject* item, CSSValueImpl *value, double defaultValue)
132 CSSPrimitiveValueImpl *primitive = static_cast<CSSPrimitiveValueImpl *>(value);
134 unsigned short cssType = (primitive ? primitive->primitiveType() : (unsigned short) CSSPrimitiveValue::CSS_UNKNOWN);
135 if(!(cssType > CSSPrimitiveValue::CSS_UNKNOWN && cssType <= CSSPrimitiveValue::CSS_PC))
138 if(cssType == CSSPrimitiveValue::CSS_PERCENTAGE)
140 SVGElementImpl *element = static_cast<SVGElementImpl *>(item->element());
141 SVGElementImpl *viewportElement = (element ? element->viewportElement() : 0);
144 double result = primitive->getFloatValue(CSSPrimitiveValue::CSS_PERCENTAGE) / 100.0;
145 return SVGHelper::PercentageOfViewport(result, viewportElement, LM_OTHER);
149 return primitive->computeLengthFloat(const_cast<RenderStyle *>(item->style()));
152 KRenderingStrokePainter KSVGPainterFactory::strokePainter(const RenderStyle* style, const RenderObject* item)
154 KRenderingStrokePainter strokePainter;
156 strokePainter.setOpacity(style->svgStyle()->strokeOpacity());
157 strokePainter.setStrokeWidth(KSVGPainterFactory::cssPrimitiveToLength(item, style->svgStyle()->strokeWidth(), 1.0));
159 CSSValueListImpl *dashes = style->svgStyle()->strokeDashArray();
161 CSSPrimitiveValueImpl *dash = 0;
163 unsigned long len = dashes->length();
164 for (unsigned long i = 0; i < len; i++) {
165 dash = static_cast<CSSPrimitiveValueImpl *>(dashes->item(i));
167 array.append((float) dash->computeLengthFloat(const_cast<RenderStyle *>(style)));
170 strokePainter.setDashArray(array);
171 strokePainter.setDashOffset(KSVGPainterFactory::cssPrimitiveToLength(item, style->svgStyle()->strokeDashOffset(), 0.0));
174 strokePainter.setStrokeMiterLimit(style->svgStyle()->strokeMiterLimit());
175 strokePainter.setStrokeCapStyle((KCCapStyle) style->svgStyle()->capStyle());
176 strokePainter.setStrokeJoinStyle((KCJoinStyle) style->svgStyle()->joinStyle());
178 return strokePainter;
181 KRenderingFillPainter KSVGPainterFactory::fillPainter(const RenderStyle* style, const RenderObject* item)
183 KRenderingFillPainter fillPainter;
185 fillPainter.setFillRule(style->svgStyle()->fillRule() == WR_NONZERO ? RULE_NONZERO : RULE_EVENODD);
186 fillPainter.setOpacity(style->svgStyle()->fillOpacity());
194 #endif // SVG_SUPPORT