2007-10-18 Eric Seidel <eric@webkit.org>
[WebKit-https.git] / WebCore / ksvg2 / svg / SVGViewSpec.cpp
1 /*
2     Copyright (C) 2007 Rob Buis <buis@kde.org>
3
4     This file is part of the KDE project
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Library General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Library General Public License for more details.
15
16     You should have received a copy of the GNU Library General Public License
17     along with this library; see the file COPYING.LIB.  If not, write to
18     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19     Boston, MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23 #if ENABLE(SVG)
24 #include "SVGViewSpec.h"
25
26 #include "PlatformString.h"
27 #include "SVGParserUtilities.h"
28 #include "SVGPreserveAspectRatio.h"
29 #include "SVGSVGElement.h"
30 #include "SVGTransformList.h"
31 #include "SVGTransformable.h"
32
33 namespace WebCore {
34
35 SVGViewSpec::SVGViewSpec(const SVGSVGElement* contextElement)
36     : SVGFitToViewBox()
37     , SVGZoomAndPan()
38     , m_transform(new SVGTransformList())
39     , m_contextElement(contextElement)
40 {
41 }
42
43 SVGViewSpec::~SVGViewSpec()
44 {
45 }
46
47 void SVGViewSpec::setTransform(const String& transform)
48 {
49     SVGTransformable::parseTransformAttribute(m_transform.get(), transform);
50 }
51
52 void SVGViewSpec::setViewBoxString(const String& viewBox)
53 {
54     float x, y, w, h;
55     const UChar* c = viewBox.characters();
56     const UChar* end = c + viewBox.length();
57     if (!parseViewBox(c, end, x, y, w, h, false))
58         return;
59     setViewBoxBaseValue(FloatRect(x, y, w, h));
60 }
61
62 void SVGViewSpec::setPreserveAspectRatioString(const String& preserve)
63 {
64     const UChar* c = preserve.characters();
65     const UChar* end = c + preserve.length();
66     preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end);
67 }
68
69 void SVGViewSpec::setViewTargetString(const String& viewTargetString)
70 {
71     m_viewTargetString = viewTargetString;
72 }
73
74 SVGElement* SVGViewSpec::viewTarget() const
75 {
76     return static_cast<SVGElement*>(m_contextElement->ownerDocument()->getElementById(m_viewTargetString));
77 }
78
79 const SVGElement* SVGViewSpec::contextElement() const
80 {
81     return m_contextElement;
82 }
83
84 static const UChar svgViewSpec[] = {'s','v','g','V', 'i', 'e', 'w'};
85 static const UChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'};
86 static const UChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'};
87 static const UChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm'};
88 static const UChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', 'a', 'n'};
89 static const UChar viewTargetSpec[] =  {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g', 'e', 't'};
90
91 bool SVGViewSpec::parseViewSpec(const String& viewSpec)
92 {
93     const UChar* currViewSpec = viewSpec.characters();
94     const UChar* end = currViewSpec + viewSpec.length();
95
96     if (currViewSpec >= end)
97         return false;
98
99     if (!skipString(currViewSpec, end, svgViewSpec, sizeof(svgViewSpec) / sizeof(UChar)))
100         return false;
101
102     if (currViewSpec >= end || *currViewSpec != '(' )
103         return false;
104     currViewSpec++;
105
106     while (currViewSpec < end && *currViewSpec != ')') {
107         if (*currViewSpec == 'v') {
108             if (skipString(currViewSpec, end, viewBoxSpec, sizeof(viewBoxSpec) / sizeof(UChar))) {
109                 if (currViewSpec >= end || *currViewSpec != '(')
110                     return false;
111                 currViewSpec++;
112                 float x, y, w, h;
113                 if (!parseViewBox(currViewSpec, end, x, y, w, h, false))
114                     return false;
115                 setViewBoxBaseValue(FloatRect(x, y, w, h));
116                 if (currViewSpec >= end || *currViewSpec != ')')
117                     return false;
118                 currViewSpec++;
119             } else if (skipString(currViewSpec, end, viewTargetSpec, sizeof(viewTargetSpec) / sizeof(UChar))) {
120                 if (currViewSpec >= end || *currViewSpec != '(')
121                     return false;
122                 const UChar* viewTargetStart = ++currViewSpec;
123                 while (currViewSpec < end && *currViewSpec != ')')
124                     currViewSpec++;
125                 if (currViewSpec >= end)
126                     return false;
127                 setViewTargetString(String(viewTargetStart, currViewSpec - viewTargetStart));
128                 currViewSpec++;
129             } else
130                 return false;
131         } else if (*currViewSpec == 'z') {
132             if (!skipString(currViewSpec, end, zoomAndPanSpec, sizeof(zoomAndPanSpec) / sizeof(UChar)))
133                 return false;
134             if (currViewSpec >= end || *currViewSpec != '(')
135                 return false;
136             currViewSpec++;
137             if (!parseZoomAndPan(currViewSpec, end))
138                 return false;
139             if (currViewSpec >= end || *currViewSpec != ')')
140                 return false;
141             currViewSpec++;
142         } else if (*currViewSpec == 'p') {
143             if (!skipString(currViewSpec, end, preserveAspectRatioSpec, sizeof(preserveAspectRatioSpec) / sizeof(UChar)))
144                 return false;
145             if (currViewSpec >= end || *currViewSpec != '(')
146                 return false;
147             currViewSpec++;
148             if (!preserveAspectRatioBaseValue()->parsePreserveAspectRatio(currViewSpec, end, false))
149                 return false;
150             if (currViewSpec >= end || *currViewSpec != ')')
151                 return false;
152             currViewSpec++;
153         } else if (*currViewSpec == 't') {
154             if (!skipString(currViewSpec, end, transformSpec, sizeof(transformSpec) / sizeof(UChar)))
155                 return false;
156             if (currViewSpec >= end || *currViewSpec != '(')
157                 return false;
158             currViewSpec++;
159             SVGTransformable::parseTransformAttribute(m_transform.get(), currViewSpec, end);
160             if (currViewSpec >= end || *currViewSpec != ')')
161                 return false;
162             currViewSpec++;
163         } else
164             return false;
165
166         if (currViewSpec < end && *currViewSpec == ';')
167             currViewSpec++;
168     }
169     
170     if (currViewSpec >= end || *currViewSpec != ')')
171         return false;
172
173     return true;
174 }
175
176 }
177
178 #endif // ENABLE(SVG)
179