2 * Copyright (C) 2007, 2010 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.
21 #include "core/svg/SVGViewSpec.h"
23 #include "bindings/core/v8/ExceptionMessages.h"
24 #include "bindings/core/v8/ExceptionState.h"
25 #include "core/SVGNames.h"
26 #include "core/dom/ExceptionCode.h"
27 #include "core/svg/SVGAnimatedTransformList.h"
28 #include "core/svg/SVGParserUtilities.h"
32 SVGViewSpec::SVGViewSpec(SVGSVGElement* contextElement)
33 // Note: addToPropertyMap is not needed, as SVGViewSpec do not correspond to an element.
34 // Note: We make tear-offs' contextElement the target element of SVGViewSpec.
35 // This contextElement will be only used for keeping this alive from the tearoff.
36 // SVGSVGElement holds a strong-ref to this SVGViewSpec, so this is kept alive as:
37 // AnimatedProperty tearoff -(contextElement)-> SVGSVGElement -(RefPtr)-> SVGViewSpec.
38 : SVGFitToViewBox(contextElement, PropertyMapPolicySkip)
39 , m_contextElement(contextElement)
40 , m_transform(SVGAnimatedTransformList::create(contextElement, SVGNames::transformAttr, SVGTransformList::create()))
42 ASSERT(m_contextElement);
44 viewBox()->setReadOnly();
45 preserveAspectRatio()->setReadOnly();
46 m_transform->setReadOnly();
47 // Note: addToPropertyMap is not needed, as SVGViewSpec do not correspond to an element.
50 bool SVGViewSpec::parseViewSpec(const String& spec)
52 if (spec.isEmpty() || !m_contextElement)
55 const LChar* ptr = spec.characters8();
56 const LChar* end = ptr + spec.length();
57 return parseViewSpecInternal(ptr, end);
59 const UChar* ptr = spec.characters16();
60 const UChar* end = ptr + spec.length();
61 return parseViewSpecInternal(ptr, end);
64 void SVGViewSpec::reset()
67 m_transform->baseValue()->clear();
68 updateViewBox(FloatRect());
69 ASSERT(preserveAspectRatio());
70 preserveAspectRatio()->baseValue()->setAlign(SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMID);
71 preserveAspectRatio()->baseValue()->setMeetOrSlice(SVGPreserveAspectRatio::SVG_MEETORSLICE_MEET);
72 m_viewTargetString = emptyString();
75 void SVGViewSpec::detachContextElement()
77 m_transform = nullptr;
79 clearPreserveAspectRatio();
80 m_contextElement = nullptr;
83 SVGElement* SVGViewSpec::viewTarget() const
85 if (!m_contextElement)
87 Element* element = m_contextElement->treeScope().getElementById(AtomicString(m_viewTargetString));
88 if (!element || !element->isSVGElement())
90 return toSVGElement(element);
93 String SVGViewSpec::viewBoxString() const
98 return viewBox()->currentValue()->valueAsString();
101 String SVGViewSpec::preserveAspectRatioString() const
103 if (!preserveAspectRatio())
106 return preserveAspectRatio()->baseValue()->valueAsString();
109 String SVGViewSpec::transformString() const
114 return m_transform->baseValue()->valueAsString();
117 void SVGViewSpec::setZoomAndPan(unsigned short, ExceptionState& exceptionState)
119 // SVGViewSpec and all of its content is read-only.
120 exceptionState.throwDOMException(NoModificationAllowedError, ExceptionMessages::readOnly());
123 static const LChar svgViewSpec[] = {'s', 'v', 'g', 'V', 'i', 'e', 'w'};
124 static const LChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'};
125 static const LChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'};
126 static const LChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm'};
127 static const LChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', 'a', 'n'};
128 static const LChar viewTargetSpec[] = {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g', 'e', 't'};
130 template<typename CharType>
131 bool SVGViewSpec::parseViewSpecInternal(const CharType* ptr, const CharType* end)
133 if (!skipString(ptr, end, svgViewSpec, WTF_ARRAY_LENGTH(svgViewSpec)))
136 if (ptr >= end || *ptr != '(')
140 while (ptr < end && *ptr != ')') {
142 if (skipString(ptr, end, viewBoxSpec, WTF_ARRAY_LENGTH(viewBoxSpec))) {
143 if (ptr >= end || *ptr != '(')
150 if (!(parseNumber(ptr, end, x) && parseNumber(ptr, end, y) && parseNumber(ptr, end, width) && parseNumber(ptr, end, height, DisallowWhitespace)))
152 updateViewBox(FloatRect(x, y, width, height));
153 if (ptr >= end || *ptr != ')')
156 } else if (skipString(ptr, end, viewTargetSpec, WTF_ARRAY_LENGTH(viewTargetSpec))) {
157 if (ptr >= end || *ptr != '(')
159 const CharType* viewTargetStart = ++ptr;
160 while (ptr < end && *ptr != ')')
164 m_viewTargetString = String(viewTargetStart, ptr - viewTargetStart);
168 } else if (*ptr == 'z') {
169 if (!skipString(ptr, end, zoomAndPanSpec, WTF_ARRAY_LENGTH(zoomAndPanSpec)))
171 if (ptr >= end || *ptr != '(')
174 if (!parseZoomAndPan(ptr, end))
176 if (ptr >= end || *ptr != ')')
179 } else if (*ptr == 'p') {
180 if (!skipString(ptr, end, preserveAspectRatioSpec, WTF_ARRAY_LENGTH(preserveAspectRatioSpec)))
182 if (ptr >= end || *ptr != '(')
185 if (!preserveAspectRatio()->baseValue()->parse(ptr, end, false))
187 if (ptr >= end || *ptr != ')')
190 } else if (*ptr == 't') {
191 if (!skipString(ptr, end, transformSpec, WTF_ARRAY_LENGTH(transformSpec)))
193 if (ptr >= end || *ptr != '(')
196 m_transform->baseValue()->parse(ptr, end);
197 if (ptr >= end || *ptr != ')')
203 if (ptr < end && *ptr == ';')
207 if (ptr >= end || *ptr != ')')
213 void SVGViewSpec::trace(Visitor* visitor)
215 visitor->trace(m_contextElement);