2 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "core/rendering/svg/SVGPathData.h"
23 #include "core/SVGNames.h"
24 #include "core/svg/SVGCircleElement.h"
25 #include "core/svg/SVGEllipseElement.h"
26 #include "core/svg/SVGLineElement.h"
27 #include "core/svg/SVGPathElement.h"
28 #include "core/svg/SVGPathUtilities.h"
29 #include "core/svg/SVGPolygonElement.h"
30 #include "core/svg/SVGPolylineElement.h"
31 #include "core/svg/SVGRectElement.h"
32 #include "platform/graphics/Path.h"
33 #include "wtf/HashMap.h"
37 using namespace SVGNames;
39 static void updatePathFromCircleElement(SVGElement* element, Path& path)
41 SVGCircleElement* circle = toSVGCircleElement(element);
43 SVGLengthContext lengthContext(element);
44 float r = circle->r()->currentValue()->value(lengthContext);
46 path.addEllipse(FloatRect(circle->cx()->currentValue()->value(lengthContext) - r, circle->cy()->currentValue()->value(lengthContext) - r, r * 2, r * 2));
49 static void updatePathFromEllipseElement(SVGElement* element, Path& path)
51 SVGEllipseElement* ellipse = toSVGEllipseElement(element);
53 SVGLengthContext lengthContext(element);
54 float rx = ellipse->rx()->currentValue()->value(lengthContext);
57 float ry = ellipse->ry()->currentValue()->value(lengthContext);
63 path.addEllipse(FloatRect(ellipse->cx()->currentValue()->value(lengthContext) - rx, ellipse->cy()->currentValue()->value(lengthContext) - ry, rx * 2, ry * 2));
66 static void updatePathFromLineElement(SVGElement* element, Path& path)
68 SVGLineElement* line = toSVGLineElement(element);
70 SVGLengthContext lengthContext(element);
71 path.moveTo(FloatPoint(line->x1()->currentValue()->value(lengthContext), line->y1()->currentValue()->value(lengthContext)));
72 path.addLineTo(FloatPoint(line->x2()->currentValue()->value(lengthContext), line->y2()->currentValue()->value(lengthContext)));
75 static void updatePathFromPathElement(SVGElement* element, Path& path)
77 buildPathFromByteStream(toSVGPathElement(element)->pathByteStream(), path);
80 static void updatePathFromPolylineElement(SVGElement* element, Path& path)
82 RefPtr<SVGPointList> points = toSVGPolyElement(element)->points()->currentValue();
83 if (points->isEmpty())
86 SVGPointList::ConstIterator it = points->begin();
87 SVGPointList::ConstIterator itEnd = points->end();
89 path.moveTo(it->value());
92 for (; it != itEnd; ++it)
93 path.addLineTo(it->value());
96 static void updatePathFromPolygonElement(SVGElement* element, Path& path)
98 updatePathFromPolylineElement(element, path);
102 static void updatePathFromRectElement(SVGElement* element, Path& path)
104 SVGRectElement* rect = toSVGRectElement(element);
106 SVGLengthContext lengthContext(element);
107 float width = rect->width()->currentValue()->value(lengthContext);
110 float height = rect->height()->currentValue()->value(lengthContext);
113 if (!width && !height)
115 float x = rect->x()->currentValue()->value(lengthContext);
116 float y = rect->y()->currentValue()->value(lengthContext);
117 float rx = rect->rx()->currentValue()->value(lengthContext);
118 float ry = rect->ry()->currentValue()->value(lengthContext);
121 if (hasRx || hasRy) {
127 path.addRoundedRect(FloatRect(x, y, width, height), FloatSize(rx, ry));
131 path.addRect(FloatRect(x, y, width, height));
134 void updatePathFromGraphicsElement(SVGElement* element, Path& path)
137 ASSERT(path.isEmpty());
139 typedef void (*PathUpdateFunction)(SVGElement*, Path&);
140 static HashMap<StringImpl*, PathUpdateFunction>* map = 0;
142 map = new HashMap<StringImpl*, PathUpdateFunction>;
143 map->set(circleTag.localName().impl(), updatePathFromCircleElement);
144 map->set(ellipseTag.localName().impl(), updatePathFromEllipseElement);
145 map->set(lineTag.localName().impl(), updatePathFromLineElement);
146 map->set(pathTag.localName().impl(), updatePathFromPathElement);
147 map->set(polygonTag.localName().impl(), updatePathFromPolygonElement);
148 map->set(polylineTag.localName().impl(), updatePathFromPolylineElement);
149 map->set(rectTag.localName().impl(), updatePathFromRectElement);
152 if (PathUpdateFunction pathUpdateFunction = map->get(element->localName().impl()))
153 (*pathUpdateFunction)(element, path);