KHtml

SVGViewSpec.cpp
1 /*
2  Copyright (C) 2007 Rob Buis <[email protected]>
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., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "wtf/Platform.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 
36 SVGViewSpec::SVGViewSpec(const SVGSVGElement *contextElement)
37  : SVGFitToViewBox()
38  , SVGZoomAndPan()
39  , m_transform(SVGTransformList::create(SVGNames::transformAttr))
40  , m_contextElement(contextElement)
41 {
42 }
43 
44 SVGViewSpec::~SVGViewSpec()
45 {
46 }
47 
48 void SVGViewSpec::setTransform(const String &transform)
49 {
50  SVGTransformable::parseTransformAttribute(m_transform.get(), transform);
51 }
52 
53 void SVGViewSpec::setViewBoxString(const String &viewBox)
54 {
55  float x, y, w, h;
56  const UChar *c = viewBox.characters();
57  const UChar *end = c + viewBox.length();
58  if (!parseViewBox(c, end, x, y, w, h, false)) {
59  return;
60  }
61  setViewBoxBaseValue(FloatRect(x, y, w, h));
62 }
63 
64 void SVGViewSpec::setPreserveAspectRatioString(const String &preserve)
65 {
66  const UChar *c = preserve.characters();
67  const UChar *end = c + preserve.length();
68  preserveAspectRatioBaseValue()->parsePreserveAspectRatio(c, end);
69 }
70 
71 void SVGViewSpec::setViewTargetString(const String &viewTargetString)
72 {
73  m_viewTargetString = viewTargetString;
74 }
75 
76 SVGElement *SVGViewSpec::viewTarget() const
77 {
78  return static_cast<SVGElement *>(m_contextElement->ownerDocument()->getElementById(m_viewTargetString));
79 }
80 
81 const SVGElement *SVGViewSpec::contextElement() const
82 {
83  return m_contextElement;
84 }
85 
86 static const UChar svgViewSpec[] = {'s', 'v', 'g', 'V', 'i', 'e', 'w'};
87 static const UChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'};
88 static const UChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'};
89 static const UChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm'};
90 static const UChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', 'a', 'n'};
91 static const UChar viewTargetSpec[] = {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g', 'e', 't'};
92 
93 bool SVGViewSpec::parseViewSpec(const String &viewSpec)
94 {
95  const UChar *currViewSpec = viewSpec.characters();
96  const UChar *end = currViewSpec + viewSpec.length();
97 
98  if (currViewSpec >= end) {
99  return false;
100  }
101 
102  if (!skipString(currViewSpec, end, svgViewSpec, sizeof(svgViewSpec) / sizeof(UChar))) {
103  return false;
104  }
105 
106  if (currViewSpec >= end || *currViewSpec != '(') {
107  return false;
108  }
109  currViewSpec++;
110 
111  while (currViewSpec < end && *currViewSpec != ')') {
112  if (*currViewSpec == 'v') {
113  if (skipString(currViewSpec, end, viewBoxSpec, sizeof(viewBoxSpec) / sizeof(UChar))) {
114  if (currViewSpec >= end || *currViewSpec != '(') {
115  return false;
116  }
117  currViewSpec++;
118  float x, y, w, h;
119  if (!parseViewBox(currViewSpec, end, x, y, w, h, false)) {
120  return false;
121  }
122  setViewBoxBaseValue(FloatRect(x, y, w, h));
123  if (currViewSpec >= end || *currViewSpec != ')') {
124  return false;
125  }
126  currViewSpec++;
127  } else if (skipString(currViewSpec, end, viewTargetSpec, sizeof(viewTargetSpec) / sizeof(UChar))) {
128  if (currViewSpec >= end || *currViewSpec != '(') {
129  return false;
130  }
131  const UChar *viewTargetStart = ++currViewSpec;
132  while (currViewSpec < end && *currViewSpec != ')') {
133  currViewSpec++;
134  }
135  if (currViewSpec >= end) {
136  return false;
137  }
138  setViewTargetString(String(viewTargetStart, currViewSpec - viewTargetStart));
139  currViewSpec++;
140  } else {
141  return false;
142  }
143  } else if (*currViewSpec == 'z') {
144  if (!skipString(currViewSpec, end, zoomAndPanSpec, sizeof(zoomAndPanSpec) / sizeof(UChar))) {
145  return false;
146  }
147  if (currViewSpec >= end || *currViewSpec != '(') {
148  return false;
149  }
150  currViewSpec++;
151  if (!parseZoomAndPan(currViewSpec, end)) {
152  return false;
153  }
154  if (currViewSpec >= end || *currViewSpec != ')') {
155  return false;
156  }
157  currViewSpec++;
158  } else if (*currViewSpec == 'p') {
159  if (!skipString(currViewSpec, end, preserveAspectRatioSpec, sizeof(preserveAspectRatioSpec) / sizeof(UChar))) {
160  return false;
161  }
162  if (currViewSpec >= end || *currViewSpec != '(') {
163  return false;
164  }
165  currViewSpec++;
166  if (!preserveAspectRatioBaseValue()->parsePreserveAspectRatio(currViewSpec, end, false)) {
167  return false;
168  }
169  if (currViewSpec >= end || *currViewSpec != ')') {
170  return false;
171  }
172  currViewSpec++;
173  } else if (*currViewSpec == 't') {
174  if (!skipString(currViewSpec, end, transformSpec, sizeof(transformSpec) / sizeof(UChar))) {
175  return false;
176  }
177  if (currViewSpec >= end || *currViewSpec != '(') {
178  return false;
179  }
180  currViewSpec++;
181  SVGTransformable::parseTransformAttribute(m_transform.get(), currViewSpec, end);
182  if (currViewSpec >= end || *currViewSpec != ')') {
183  return false;
184  }
185  currViewSpec++;
186  } else {
187  return false;
188  }
189 
190  if (currViewSpec < end && *currViewSpec == ';') {
191  currViewSpec++;
192  }
193  }
194 
195  if (currViewSpec >= end || *currViewSpec != ')') {
196  return false;
197  }
198 
199  return true;
200 }
201 
202 }
203 
204 #endif // ENABLE(SVG)
QAction * create(StandardAction id, const QObject *recvr, Func slot, QObject *parent)
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:48:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.