• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • khtml
  • svg
SVGTransformable.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 2007 Eric Seidel <eric@webkit.org>
5
6 This file is part of the WebKit project
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22*/
23
24#include "config.h"
25#include "Document.h"
26
27#if ENABLE(SVG)
28#include "SVGTransformable.h"
29
30#include "AffineTransform.h"
31#include "FloatConversion.h"
32//#include "RegularExpression.h"
33#include "SVGNames.h"
34#include "SVGParserUtilities.h"
35#include "SVGStyledElement.h"
36#include "SVGTransformList.h"
37
38namespace WebCore {
39
40SVGTransformable::SVGTransformable() : SVGLocatable()
41{
42}
43
44SVGTransformable::~SVGTransformable()
45{
46}
47
48AffineTransform SVGTransformable::getCTM(const SVGElement* element) const
49{
50 AffineTransform ctm = SVGLocatable::getCTM(element);
51 return animatedLocalTransform() * ctm;
52}
53
54AffineTransform SVGTransformable::getScreenCTM(const SVGElement* element) const
55{
56 AffineTransform ctm = SVGLocatable::getScreenCTM(element);
57 return animatedLocalTransform() * ctm;
58}
59
60int parseTransformParamList(const UChar*& ptr, const UChar* end, float* values, int required, int optional)
61{
62 int optionalParams = 0, requiredParams = 0;
63
64 if (!skipOptionalSpaces(ptr, end) || *ptr != '(')
65 return -1;
66
67 ptr++;
68
69 skipOptionalSpaces(ptr, end);
70
71 while (requiredParams < required) {
72 if (ptr >= end || !parseNumber(ptr, end, values[requiredParams], false))
73 return -1;
74 requiredParams++;
75 if (requiredParams < required)
76 skipOptionalSpacesOrDelimiter(ptr, end);
77 }
78 if (!skipOptionalSpaces(ptr, end))
79 return -1;
80
81 bool delimParsed = skipOptionalSpacesOrDelimiter(ptr, end);
82
83 if (ptr >= end)
84 return -1;
85
86 if (*ptr == ')') { // skip optionals
87 ptr++;
88 if (delimParsed)
89 return -1;
90 } else {
91 while (optionalParams < optional) {
92 if (ptr >= end || !parseNumber(ptr, end, values[requiredParams + optionalParams], false))
93 return -1;
94 optionalParams++;
95 if (optionalParams < optional)
96 skipOptionalSpacesOrDelimiter(ptr, end);
97 }
98
99 if (!skipOptionalSpaces(ptr, end))
100 return -1;
101
102 delimParsed = skipOptionalSpacesOrDelimiter(ptr, end);
103
104 if (ptr >= end || *ptr != ')' || delimParsed)
105 return -1;
106 ptr++;
107 }
108
109 return requiredParams + optionalParams;
110}
111
112// These should be kept in sync with enum SVGTransformType
113static const int requiredValuesForType[] = {0, 6, 1, 1, 1, 1, 1};
114static const int optionalValuesForType[] = {0, 0, 1, 1, 2, 0, 0};
115
116bool SVGTransformable::parseTransformValue(unsigned type, const UChar*& ptr, const UChar* end, SVGTransform& t)
117{
118 if (type == SVGTransform::SVG_TRANSFORM_UNKNOWN)
119 return false;
120
121 int valueCount = 0;
122 float values[] = {0, 0, 0, 0, 0, 0};
123 if ((valueCount = parseTransformParamList(ptr, end, values, requiredValuesForType[type], optionalValuesForType[type])) < 0)
124 return false;
125
126 switch (type) {
127 case SVGTransform::SVG_TRANSFORM_SKEWX:
128 t.setSkewX(values[0]);
129 break;
130 case SVGTransform::SVG_TRANSFORM_SKEWY:
131 t.setSkewY(values[0]);
132 break;
133 case SVGTransform::SVG_TRANSFORM_SCALE:
134 if (valueCount == 1) // Spec: if only one param given, assume uniform scaling
135 t.setScale(values[0], values[0]);
136 else
137 t.setScale(values[0], values[1]);
138 break;
139 case SVGTransform::SVG_TRANSFORM_TRANSLATE:
140 if (valueCount == 1) // Spec: if only one param given, assume 2nd param to be 0
141 t.setTranslate(values[0], 0);
142 else
143 t.setTranslate(values[0], values[1]);
144 break;
145 case SVGTransform::SVG_TRANSFORM_ROTATE:
146 if (valueCount == 1)
147 t.setRotate(values[0], 0, 0);
148 else
149 t.setRotate(values[0], values[1], values[2]);
150 break;
151 case SVGTransform::SVG_TRANSFORM_MATRIX:
152 t.setMatrix(AffineTransform(values[0], values[1], values[2], values[3], values[4], values[5]));
153 break;
154 }
155
156 return true;
157}
158
159static const UChar skewXDesc[] = {'s','k','e','w', 'X'};
160static const UChar skewYDesc[] = {'s','k','e','w', 'Y'};
161static const UChar scaleDesc[] = {'s','c','a','l', 'e'};
162static const UChar translateDesc[] = {'t','r','a','n', 's', 'l', 'a', 't', 'e'};
163static const UChar rotateDesc[] = {'r','o','t','a', 't', 'e'};
164static const UChar matrixDesc[] = {'m','a','t','r', 'i', 'x'};
165
166// KHTML
167/*static inline bool skipString(const UChar*& currTransform, const UChar* end, const UChar* pattern, int len)
168{
169 int i = len;
170 const UChar* curr = currTransform;
171 while (i && curr < end) {
172 if (*curr++ != *pattern++)
173 return false;
174 --i;
175 }
176 if (i)
177 return false;
178 currTransform += len;
179 return true;
180}*/
181
182static inline bool parseAndSkipType(const UChar*& currTransform, const UChar* end, unsigned short& type)
183{
184 if (currTransform >= end)
185 return false;
186
187 if (*currTransform == 's') {
188 if (skipString(currTransform, end, skewXDesc, sizeof(skewXDesc) / sizeof(UChar)))
189 type = SVGTransform::SVG_TRANSFORM_SKEWX;
190 else if (skipString(currTransform, end, skewYDesc, sizeof(skewYDesc) / sizeof(UChar)))
191 type = SVGTransform::SVG_TRANSFORM_SKEWY;
192 else if (skipString(currTransform, end, scaleDesc, sizeof(scaleDesc) / sizeof(UChar)))
193 type = SVGTransform::SVG_TRANSFORM_SCALE;
194 else
195 return false;
196 } else if (skipString(currTransform, end, translateDesc, sizeof(translateDesc) / sizeof(UChar)))
197 type = SVGTransform::SVG_TRANSFORM_TRANSLATE;
198 else if (skipString(currTransform, end, rotateDesc, sizeof(rotateDesc) / sizeof(UChar)))
199 type = SVGTransform::SVG_TRANSFORM_ROTATE;
200 else if (skipString(currTransform, end, matrixDesc, sizeof(matrixDesc) / sizeof(UChar)))
201 type = SVGTransform::SVG_TRANSFORM_MATRIX;
202 else
203 return false;
204
205 return true;
206}
207
208bool SVGTransformable::parseTransformAttribute(SVGTransformList* list, const AtomicString& transform)
209{
210 const UChar* start = transform.characters();
211 const UChar* end = start + transform.length();
212 return parseTransformAttribute(list, start, end);
213}
214
215bool SVGTransformable::parseTransformAttribute(SVGTransformList* list, const UChar*& currTransform, const UChar* end)
216{
217 bool delimParsed = false;
218 while (currTransform < end) {
219 delimParsed = false;
220 unsigned short type = SVGTransform::SVG_TRANSFORM_UNKNOWN;
221 skipOptionalSpaces(currTransform, end);
222
223 if (!parseAndSkipType(currTransform, end, type))
224 return false;
225
226 SVGTransform t;
227 if (!parseTransformValue(type, currTransform, end, t))
228 return false;
229
230 ExceptionCode ec = 0;
231 list->appendItem(t, ec);
232 skipOptionalSpaces(currTransform, end);
233 if (currTransform < end && *currTransform == ',') {
234 delimParsed = true;
235 currTransform++;
236 }
237 skipOptionalSpaces(currTransform, end);
238 }
239
240 return !delimParsed;
241}
242
243bool SVGTransformable::isKnownAttribute(const QualifiedName& attrName)
244{
245 return attrName.matches(SVGNames::transformAttr);
246}
247
248}
249
250#endif // ENABLE(SVG)
AffineTransform.h
FloatConversion.h
SVGNames.h
SVGParserUtilities.h
SVGStyledElement.h
SVGTransformList.h
SVGTransformable.h
list
QStringList list(const QString &fileClass)
end
const KShortcut & end()
WebCore
Definition: CSSHelper.h:7
WebCore::skipString
bool skipString(const QChar *&ptr, const QChar *end, const QChar *name, int length)
Definition: ParserUtilities.h:29
khtml::ExceptionCode
unsigned short ExceptionCode
Definition: ExceptionCode.h:37
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal