Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / svg / SVGViewSpec.cpp
blobe6ded33b5e8f66320a203e6fd01ddd92ce5a2488
1 /*
2 Copyright (C) 2007 Rob Buis <buis@kde.org>
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.
20 #include "config.h"
21 #if ENABLE(SVG)
22 #include "SVGViewSpec.h"
24 #include "Document.h"
25 #include "PlatformString.h"
26 #include "SVGParserUtilities.h"
27 #include "SVGPreserveAspectRatio.h"
28 #include "SVGSVGElement.h"
29 #include "SVGTransformList.h"
30 #include "SVGTransformable.h"
32 namespace WebCore {
34 SVGViewSpec::SVGViewSpec(const SVGSVGElement* contextElement)
35 : SVGFitToViewBox()
36 , SVGZoomAndPan()
37 , m_contextElement(contextElement)
38 , m_viewBox(this, SVGNames::viewBoxAttr)
39 , m_preserveAspectRatio(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create())
40 , m_transform(SVGTransformList::create(SVGNames::transformAttr))
44 SVGViewSpec::~SVGViewSpec()
48 void SVGViewSpec::setTransform(const String& transform)
50 SVGTransformable::parseTransformAttribute(m_transform.get(), transform);
53 void SVGViewSpec::setViewBoxString(const String& viewBox)
55 float x, y, w, h;
56 const UChar* c = viewBox.characters();
57 const UChar* end = c + viewBox.length();
58 if (!parseViewBox(m_contextElement->document(), c, end, x, y, w, h, false))
59 return;
60 setViewBoxBaseValue(FloatRect(x, y, w, h));
63 void SVGViewSpec::setPreserveAspectRatioString(const String& preserve)
65 const UChar* c = preserve.characters();
66 const UChar* end = c + preserve.length();
67 preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end);
70 void SVGViewSpec::setViewTargetString(const String& viewTargetString)
72 m_viewTargetString = viewTargetString;
75 SVGElement* SVGViewSpec::viewTarget() const
77 return static_cast<SVGElement*>(m_contextElement->document()->getElementById(m_viewTargetString));
80 static const UChar svgViewSpec[] = {'s', 'v', 'g', 'V', 'i', 'e', 'w'};
81 static const UChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'};
82 static const UChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'};
83 static const UChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm'};
84 static const UChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', 'a', 'n'};
85 static const UChar viewTargetSpec[] = {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g', 'e', 't'};
87 bool SVGViewSpec::parseViewSpec(const String& viewSpec)
89 const UChar* currViewSpec = viewSpec.characters();
90 const UChar* end = currViewSpec + viewSpec.length();
92 if (currViewSpec >= end)
93 return false;
95 if (!skipString(currViewSpec, end, svgViewSpec, sizeof(svgViewSpec) / sizeof(UChar)))
96 return false;
98 if (currViewSpec >= end || *currViewSpec != '(' )
99 return false;
100 currViewSpec++;
102 while (currViewSpec < end && *currViewSpec != ')') {
103 if (*currViewSpec == 'v') {
104 if (skipString(currViewSpec, end, viewBoxSpec, sizeof(viewBoxSpec) / sizeof(UChar))) {
105 if (currViewSpec >= end || *currViewSpec != '(')
106 return false;
107 currViewSpec++;
108 float x, y, w, h;
109 if (!parseViewBox(m_contextElement->document(), currViewSpec, end, x, y, w, h, false))
110 return false;
111 setViewBoxBaseValue(FloatRect(x, y, w, h));
112 if (currViewSpec >= end || *currViewSpec != ')')
113 return false;
114 currViewSpec++;
115 } else if (skipString(currViewSpec, end, viewTargetSpec, sizeof(viewTargetSpec) / sizeof(UChar))) {
116 if (currViewSpec >= end || *currViewSpec != '(')
117 return false;
118 const UChar* viewTargetStart = ++currViewSpec;
119 while (currViewSpec < end && *currViewSpec != ')')
120 currViewSpec++;
121 if (currViewSpec >= end)
122 return false;
123 setViewTargetString(String(viewTargetStart, currViewSpec - viewTargetStart));
124 currViewSpec++;
125 } else
126 return false;
127 } else if (*currViewSpec == 'z') {
128 if (!skipString(currViewSpec, end, zoomAndPanSpec, sizeof(zoomAndPanSpec) / sizeof(UChar)))
129 return false;
130 if (currViewSpec >= end || *currViewSpec != '(')
131 return false;
132 currViewSpec++;
133 if (!parseZoomAndPan(currViewSpec, end))
134 return false;
135 if (currViewSpec >= end || *currViewSpec != ')')
136 return false;
137 currViewSpec++;
138 } else if (*currViewSpec == 'p') {
139 if (!skipString(currViewSpec, end, preserveAspectRatioSpec, sizeof(preserveAspectRatioSpec) / sizeof(UChar)))
140 return false;
141 if (currViewSpec >= end || *currViewSpec != '(')
142 return false;
143 currViewSpec++;
144 if (!preserveAspectRatioBaseValue()->parsePreserveAspectRatio(currViewSpec, end, false))
145 return false;
146 if (currViewSpec >= end || *currViewSpec != ')')
147 return false;
148 currViewSpec++;
149 } else if (*currViewSpec == 't') {
150 if (!skipString(currViewSpec, end, transformSpec, sizeof(transformSpec) / sizeof(UChar)))
151 return false;
152 if (currViewSpec >= end || *currViewSpec != '(')
153 return false;
154 currViewSpec++;
155 SVGTransformable::parseTransformAttribute(m_transform.get(), currViewSpec, end);
156 if (currViewSpec >= end || *currViewSpec != ')')
157 return false;
158 currViewSpec++;
159 } else
160 return false;
162 if (currViewSpec < end && *currViewSpec == ';')
163 currViewSpec++;
166 if (currViewSpec >= end || *currViewSpec != ')')
167 return false;
169 return true;
174 #endif // ENABLE(SVG)