| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2010 Rob Buis <buis@kde.org> | 2 * Copyright (C) 2007, 2010 Rob Buis <buis@kde.org> |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 */ | 18 */ |
| 19 | |
| 20 #include "core/svg/SVGViewSpec.h" | 19 #include "core/svg/SVGViewSpec.h" |
| 21 | 20 |
| 22 #include "bindings/core/v8/ExceptionMessages.h" | |
| 23 #include "bindings/core/v8/ExceptionState.h" | |
| 24 #include "core/SVGNames.h" | 21 #include "core/SVGNames.h" |
| 25 #include "core/dom/ExceptionCode.h" | 22 #include "core/dom/ExceptionCode.h" |
| 26 #include "core/svg/SVGAnimatedTransformList.h" | |
| 27 #include "core/svg/SVGParserUtilities.h" | |
| 28 #include "platform/text/ParserUtilities.h" | |
| 29 | 23 |
| 30 namespace blink { | 24 namespace blink { |
| 31 | 25 |
| 32 SVGViewSpec::SVGViewSpec(SVGSVGElement* contextElement) | 26 SVGViewSpec::SVGViewSpec(SVGSVGElement* contextElement) |
| 33 // Note: addToPropertyMap is not needed, as SVGViewSpec do not correspond to
an element. | 27 // 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
. | 28 // Note: We make tear-offs' contextElement the target element of SVGViewSpec
. |
| 35 // This contextElement will be only used for keeping this alive from the tea
roff. | 29 // This contextElement will be only used for keeping this alive from the tea
roff. |
| 36 // SVGSVGElement holds a strong-ref to this SVGViewSpec, so this is kept ali
ve as: | 30 // SVGSVGElement holds a strong-ref to this SVGViewSpec, so this is kept ali
ve as: |
| 37 // AnimatedProperty tearoff -(contextElement)-> SVGSVGElement -(RefPtr)-> SV
GViewSpec. | 31 // AnimatedProperty tearoff -(contextElement)-> SVGSVGElement -(RefPtr)-> SV
GViewSpec. |
| 38 : SVGFitToViewBox(contextElement, PropertyMapPolicySkip) | 32 : SVGFitToViewBox(contextElement, PropertyMapPolicySkip) |
| 39 , m_contextElement(contextElement) | 33 , m_contextElement(contextElement) |
| 40 , m_transform(SVGAnimatedTransformList::create(contextElement, SVGNames::tra
nsformAttr, SVGTransformList::create())) | |
| 41 { | 34 { |
| 42 ASSERT(m_contextElement); | 35 ASSERT(m_contextElement); |
| 43 | 36 |
| 44 viewBox()->setReadOnly(); | 37 viewBox()->setReadOnly(); |
| 45 preserveAspectRatio()->setReadOnly(); | 38 preserveAspectRatio()->setReadOnly(); |
| 46 m_transform->setReadOnly(); | |
| 47 // Note: addToPropertyMap is not needed, as SVGViewSpec do not correspond to
an element. | 39 // Note: addToPropertyMap is not needed, as SVGViewSpec do not correspond to
an element. |
| 48 } | 40 } |
| 49 | 41 |
| 50 DEFINE_TRACE(SVGViewSpec) | 42 DEFINE_TRACE(SVGViewSpec) |
| 51 { | 43 { |
| 52 visitor->trace(m_contextElement); | 44 visitor->trace(m_contextElement); |
| 53 visitor->trace(m_transform); | |
| 54 SVGFitToViewBox::trace(visitor); | 45 SVGFitToViewBox::trace(visitor); |
| 55 } | 46 } |
| 56 | 47 |
| 57 DEFINE_TRACE_WRAPPERS(SVGViewSpec) | 48 DEFINE_TRACE_WRAPPERS(SVGViewSpec) |
| 58 { | 49 { |
| 59 visitor->traceWrappers(m_contextElement); | 50 visitor->traceWrappers(m_contextElement); |
| 60 } | 51 } |
| 61 | 52 |
| 62 bool SVGViewSpec::parseViewSpec(const String& spec) | |
| 63 { | |
| 64 if (spec.isEmpty() || !m_contextElement) | |
| 65 return false; | |
| 66 if (spec.is8Bit()) { | |
| 67 const LChar* ptr = spec.characters8(); | |
| 68 const LChar* end = ptr + spec.length(); | |
| 69 return parseViewSpecInternal(ptr, end); | |
| 70 } | |
| 71 const UChar* ptr = spec.characters16(); | |
| 72 const UChar* end = ptr + spec.length(); | |
| 73 return parseViewSpecInternal(ptr, end); | |
| 74 } | |
| 75 | |
| 76 void SVGViewSpec::reset() | 53 void SVGViewSpec::reset() |
| 77 { | 54 { |
| 78 resetZoomAndPan(); | 55 resetZoomAndPan(); |
| 79 m_transform->baseValue()->clear(); | |
| 80 updateViewBox(FloatRect()); | 56 updateViewBox(FloatRect()); |
| 81 ASSERT(preserveAspectRatio()); | 57 ASSERT(preserveAspectRatio()); |
| 82 preserveAspectRatio()->baseValue()->setAlign(SVGPreserveAspectRatio::SVG_PRE
SERVEASPECTRATIO_XMIDYMID); | 58 preserveAspectRatio()->baseValue()->setAlign(SVGPreserveAspectRatio::SVG_PRE
SERVEASPECTRATIO_XMIDYMID); |
| 83 preserveAspectRatio()->baseValue()->setMeetOrSlice(SVGPreserveAspectRatio::S
VG_MEETORSLICE_MEET); | 59 preserveAspectRatio()->baseValue()->setMeetOrSlice(SVGPreserveAspectRatio::S
VG_MEETORSLICE_MEET); |
| 84 m_viewTargetString = emptyString(); | |
| 85 } | |
| 86 | |
| 87 void SVGViewSpec::detachContextElement() | |
| 88 { | |
| 89 m_transform = nullptr; | |
| 90 clearViewBox(); | |
| 91 clearPreserveAspectRatio(); | |
| 92 m_contextElement = nullptr; | |
| 93 } | |
| 94 | |
| 95 SVGElement* SVGViewSpec::viewTarget() const | |
| 96 { | |
| 97 if (!m_contextElement) | |
| 98 return nullptr; | |
| 99 Element* element = m_contextElement->treeScope().getElementById(AtomicString
(m_viewTargetString)); | |
| 100 if (!element || !element->isSVGElement()) | |
| 101 return nullptr; | |
| 102 return toSVGElement(element); | |
| 103 } | |
| 104 | |
| 105 String SVGViewSpec::viewBoxString() const | |
| 106 { | |
| 107 if (!viewBox()) | |
| 108 return String(); | |
| 109 | |
| 110 return viewBox()->currentValue()->valueAsString(); | |
| 111 } | |
| 112 | |
| 113 String SVGViewSpec::preserveAspectRatioString() const | |
| 114 { | |
| 115 if (!preserveAspectRatio()) | |
| 116 return String(); | |
| 117 | |
| 118 return preserveAspectRatio()->baseValue()->valueAsString(); | |
| 119 } | |
| 120 | |
| 121 String SVGViewSpec::transformString() const | |
| 122 { | |
| 123 if (!m_transform) | |
| 124 return String(); | |
| 125 | |
| 126 return m_transform->baseValue()->valueAsString(); | |
| 127 } | 60 } |
| 128 | 61 |
| 129 void SVGViewSpec::setZoomAndPan(unsigned short, ExceptionState& exceptionState) | 62 void SVGViewSpec::setZoomAndPan(unsigned short, ExceptionState& exceptionState) |
| 130 { | 63 { |
| 131 // SVGViewSpec and all of its content is read-only. | 64 // SVGViewSpec and all of its content is read-only. |
| 132 exceptionState.throwDOMException(NoModificationAllowedError, ExceptionMessag
es::readOnly()); | 65 exceptionState.throwDOMException(NoModificationAllowedError, ExceptionMessag
es::readOnly()); |
| 133 } | 66 } |
| 134 | |
| 135 static const LChar svgViewSpec[] = {'s', 'v', 'g', 'V', 'i', 'e', 'w'}; | |
| 136 static const LChar viewBoxSpec[] = {'v', 'i', 'e', 'w', 'B', 'o', 'x'}; | |
| 137 static const LChar preserveAspectRatioSpec[] = {'p', 'r', 'e', 's', 'e', 'r', 'v
', 'e', 'A', 's', 'p', 'e', 'c', 't', 'R', 'a', 't', 'i', 'o'}; | |
| 138 static const LChar transformSpec[] = {'t', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm
'}; | |
| 139 static const LChar zoomAndPanSpec[] = {'z', 'o', 'o', 'm', 'A', 'n', 'd', 'P', '
a', 'n'}; | |
| 140 static const LChar viewTargetSpec[] = {'v', 'i', 'e', 'w', 'T', 'a', 'r', 'g',
'e', 't'}; | |
| 141 | |
| 142 template<typename CharType> | |
| 143 bool SVGViewSpec::parseViewSpecInternal(const CharType* ptr, const CharType* end
) | |
| 144 { | |
| 145 if (!skipString(ptr, end, svgViewSpec, WTF_ARRAY_LENGTH(svgViewSpec))) | |
| 146 return false; | |
| 147 | |
| 148 if (ptr >= end || *ptr != '(') | |
| 149 return false; | |
| 150 ptr++; | |
| 151 | |
| 152 while (ptr < end && *ptr != ')') { | |
| 153 if (*ptr == 'v') { | |
| 154 if (skipString(ptr, end, viewBoxSpec, WTF_ARRAY_LENGTH(viewBoxSpec))
) { | |
| 155 if (ptr >= end || *ptr != '(') | |
| 156 return false; | |
| 157 ptr++; | |
| 158 float x = 0.0f; | |
| 159 float y = 0.0f; | |
| 160 float width = 0.0f; | |
| 161 float height = 0.0f; | |
| 162 if (!(parseNumber(ptr, end, x) && parseNumber(ptr, end, y) && pa
rseNumber(ptr, end, width) && parseNumber(ptr, end, height, DisallowWhitespace))
) | |
| 163 return false; | |
| 164 updateViewBox(FloatRect(x, y, width, height)); | |
| 165 if (ptr >= end || *ptr != ')') | |
| 166 return false; | |
| 167 ptr++; | |
| 168 } else if (skipString(ptr, end, viewTargetSpec, WTF_ARRAY_LENGTH(vie
wTargetSpec))) { | |
| 169 if (ptr >= end || *ptr != '(') | |
| 170 return false; | |
| 171 const CharType* viewTargetStart = ++ptr; | |
| 172 while (ptr < end && *ptr != ')') | |
| 173 ptr++; | |
| 174 if (ptr >= end) | |
| 175 return false; | |
| 176 m_viewTargetString = String(viewTargetStart, ptr - viewTargetSta
rt); | |
| 177 ptr++; | |
| 178 } else | |
| 179 return false; | |
| 180 } else if (*ptr == 'z') { | |
| 181 if (!skipString(ptr, end, zoomAndPanSpec, WTF_ARRAY_LENGTH(zoomAndPa
nSpec))) | |
| 182 return false; | |
| 183 if (ptr >= end || *ptr != '(') | |
| 184 return false; | |
| 185 ptr++; | |
| 186 if (!parseZoomAndPan(ptr, end)) | |
| 187 return false; | |
| 188 if (ptr >= end || *ptr != ')') | |
| 189 return false; | |
| 190 ptr++; | |
| 191 } else if (*ptr == 'p') { | |
| 192 if (!skipString(ptr, end, preserveAspectRatioSpec, WTF_ARRAY_LENGTH(
preserveAspectRatioSpec))) | |
| 193 return false; | |
| 194 if (ptr >= end || *ptr != '(') | |
| 195 return false; | |
| 196 ptr++; | |
| 197 if (!preserveAspectRatio()->baseValue()->parse(ptr, end, false)) | |
| 198 return false; | |
| 199 if (ptr >= end || *ptr != ')') | |
| 200 return false; | |
| 201 ptr++; | |
| 202 } else if (*ptr == 't') { | |
| 203 if (!skipString(ptr, end, transformSpec, WTF_ARRAY_LENGTH(transformS
pec))) | |
| 204 return false; | |
| 205 if (ptr >= end || *ptr != '(') | |
| 206 return false; | |
| 207 ptr++; | |
| 208 m_transform->baseValue()->parse(ptr, end); | |
| 209 if (ptr >= end || *ptr != ')') | |
| 210 return false; | |
| 211 ptr++; | |
| 212 } else | |
| 213 return false; | |
| 214 | |
| 215 if (ptr < end && *ptr == ';') | |
| 216 ptr++; | |
| 217 } | |
| 218 | |
| 219 if (ptr >= end || *ptr != ')') | |
| 220 return false; | |
| 221 | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 225 } // namespace blink | 67 } // namespace blink |
| OLD | NEW |