2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * 2007 Rob Buis <buis@kde.org>
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 COMPUTER, 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 COMPUTER, 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.
28 #include "wtf/Platform.h"
31 #include "SVGPaintServer.h"
33 #include "RenderObject.h"
34 #include "RenderStyle.h"
35 #include "SVGPaintServerSolid.h"
36 #include "SVGStyledElement.h"
37 #include "SVGURIReference.h"
41 SVGPaintServer::SVGPaintServer()
45 SVGPaintServer::~SVGPaintServer()
49 /*TextStream& operator<<(TextStream& ts, const SVGPaintServer& paintServer)
51 return paintServer.externalRepresentation(ts);
54 SVGPaintServer
* getPaintServerById(Document
* document
, const AtomicString
& id
)
56 SVGResource
* resource
= getResourceById(document
, id
);
57 if (resource
&& resource
->isPaintServer())
58 return static_cast<SVGPaintServer
*>(resource
);
63 SVGPaintServerSolid
* SVGPaintServer::sharedSolidPaintServer()
65 static SVGPaintServerSolid
* _sharedSolidPaintServer
= SVGPaintServerSolid::create().releaseRef();
67 return _sharedSolidPaintServer
;
70 SVGPaintServer
* SVGPaintServer::fillPaintServer(const RenderStyle
* style
, const RenderObject
* item
)
72 if (!style
->svgStyle()->hasFill())
75 SVGPaint
* fill
= style
->svgStyle()->fillPaint();
77 SVGPaintServer
* fillPaintServer
= 0;
78 SVGPaint::SVGPaintType paintType
= fill
->paintType();
79 if (paintType
== SVGPaint::SVG_PAINTTYPE_URI
||
80 paintType
== SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR
) {
81 AtomicString
id(SVGURIReference::getTarget(fill
->uri()));
82 fillPaintServer
= getPaintServerById(item
->document(), id
);
83 SVGElement
* svgElement
= static_cast<SVGElement
*>(item
->element());
84 ASSERT(svgElement
&& svgElement
->document() && svgElement
->isStyled());
86 if (item
->isRenderPath() && fillPaintServer
)
87 fillPaintServer
->addClient(static_cast<SVGStyledElement
*>(svgElement
));
88 else if (!fillPaintServer
&& paintType
== SVGPaint::SVG_PAINTTYPE_URI
)
89 svgElement
->document()->accessSVGExtensions()->addPendingResource(id
, static_cast<SVGStyledElement
*>(svgElement
));
91 if (paintType
!= SVGPaint::SVG_PAINTTYPE_URI
&& !fillPaintServer
) {
92 fillPaintServer
= sharedSolidPaintServer();
93 SVGPaintServerSolid
* fillPaintServerSolid
= static_cast<SVGPaintServerSolid
*>(fillPaintServer
);
94 if (paintType
== SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR
)
95 fillPaintServerSolid
->setColor(style
->color());
97 fillPaintServerSolid
->setColor(fill
->color());
98 // FIXME: Ideally invalid colors would never get set on the RenderStyle and this could turn into an ASSERT
99 if (!fillPaintServerSolid
->color().isValid())
102 if (!fillPaintServer
) {
103 // default value (black), see bug 11017
104 fillPaintServer
= sharedSolidPaintServer();
105 static_cast<SVGPaintServerSolid
*>(fillPaintServer
)->setColor(/*Color::black*/Qt::black
);
107 return fillPaintServer
;
110 SVGPaintServer
* SVGPaintServer::strokePaintServer(const RenderStyle
* style
, const RenderObject
* item
)
112 if (!style
->svgStyle()->hasStroke())
115 SVGPaint
* stroke
= style
->svgStyle()->strokePaint();
117 SVGPaintServer
* strokePaintServer
= 0;
118 SVGPaint::SVGPaintType paintType
= stroke
->paintType();
119 if (paintType
== SVGPaint::SVG_PAINTTYPE_URI
||
120 paintType
== SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR
) {
121 AtomicString
id(SVGURIReference::getTarget(stroke
->uri()));
122 strokePaintServer
= getPaintServerById(item
->document(), id
);
124 SVGElement
* svgElement
= static_cast<SVGElement
*>(item
->element());
125 ASSERT(svgElement
&& svgElement
->document() && svgElement
->isStyled());
127 if (item
->isRenderPath() && strokePaintServer
)
128 strokePaintServer
->addClient(static_cast<SVGStyledElement
*>(svgElement
));
129 else if (!strokePaintServer
&& paintType
== SVGPaint::SVG_PAINTTYPE_URI
)
130 svgElement
->document()->accessSVGExtensions()->addPendingResource(id
, static_cast<SVGStyledElement
*>(svgElement
));
132 if (paintType
!= SVGPaint::SVG_PAINTTYPE_URI
&& !strokePaintServer
) {
133 strokePaintServer
= sharedSolidPaintServer();
134 SVGPaintServerSolid
* strokePaintServerSolid
= static_cast<SVGPaintServerSolid
*>(strokePaintServer
);
135 if (paintType
== SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR
)
136 strokePaintServerSolid
->setColor(style
->color());
138 strokePaintServerSolid
->setColor(stroke
->color());
139 // FIXME: Ideally invalid colors would never get set on the RenderStyle and this could turn into an ASSERT
140 if (!strokePaintServerSolid
->color().isValid())
141 strokePaintServer
= 0;
144 return strokePaintServer
;
147 DashArray
dashArrayFromRenderingStyle(const RenderStyle
* style
)
151 /*CSSValueList* dashes = style->svgStyle()->strokeDashArray();
153 CSSPrimitiveValue* dash = 0;
154 unsigned long len = dashes->length();
155 for (unsigned long i = 0; i < len; i++) {
156 dash = static_cast<CSSPrimitiveValue*>(dashes->itemWithoutBoundsCheck(i));
160 array.append((float) dash->computeLengthFloat(const_cast<RenderStyle*>(style)));
167 } // namespace WebCore