Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: third_party/WebKit/Source/core/svg/SVGLength.cpp

Issue 1421533006: Make SVGLength wrap a CSSPrimitiveValue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added expectation Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org> 2 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org> 3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2007 Apple Inc. All rights reserved. 4 * Copyright (C) 2007 Apple Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 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 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, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 */ 20 */
21 21
22 #include "config.h" 22 #include "config.h"
23 23
24 #include "core/svg/SVGLength.h" 24 #include "core/svg/SVGLength.h"
25 25
26 #include "bindings/core/v8/ExceptionState.h" 26 #include "bindings/core/v8/ExceptionState.h"
27 #include "core/SVGNames.h" 27 #include "core/SVGNames.h"
28 #include "core/css/CSSPrimitiveValue.h" 28 #include "core/css/CSSPrimitiveValue.h"
29 #include "core/css/CSSValue.h"
30 #include "core/css/CSSValuePool.h"
31 #include "core/css/parser/CSSParser.h"
29 #include "core/dom/ExceptionCode.h" 32 #include "core/dom/ExceptionCode.h"
30 #include "core/svg/SVGAnimationElement.h" 33 #include "core/svg/SVGAnimationElement.h"
31 #include "core/svg/SVGParserUtilities.h" 34 #include "core/svg/SVGParserUtilities.h"
32 #include "platform/animation/AnimationUtilities.h" 35 #include "platform/animation/AnimationUtilities.h"
33 #include "wtf/MathExtras.h" 36 #include "wtf/MathExtras.h"
34 #include "wtf/text/WTFString.h" 37 #include "wtf/text/WTFString.h"
35 38
36 namespace blink { 39 namespace blink {
37 40
38 namespace {
39
40 inline const char* lengthTypeToString(SVGLengthType type)
41 {
42 switch (type) {
43 case LengthTypeUnknown:
44 case LengthTypeNumber:
45 return "";
46 case LengthTypePercentage:
47 return "%";
48 case LengthTypeEMS:
49 return "em";
50 case LengthTypeEXS:
51 return "ex";
52 case LengthTypePX:
53 return "px";
54 case LengthTypeCM:
55 return "cm";
56 case LengthTypeMM:
57 return "mm";
58 case LengthTypeIN:
59 return "in";
60 case LengthTypePT:
61 return "pt";
62 case LengthTypePC:
63 return "pc";
64 case LengthTypeREMS:
65 return "rem";
66 case LengthTypeCHS:
67 return "ch";
68 }
69
70 ASSERT_NOT_REACHED();
71 return "";
72 }
73
74 template<typename CharType>
75 SVGLengthType stringToLengthType(const CharType*& ptr, const CharType* end)
76 {
77 if (ptr == end)
78 return LengthTypeNumber;
79
80 SVGLengthType type = LengthTypeUnknown;
81 const CharType firstChar = *ptr++;
82
83 if (firstChar == '%') {
84 type = LengthTypePercentage;
85 } else if (isHTMLSpace<CharType>(firstChar)) {
86 type = LengthTypeNumber;
87 } else if (ptr < end) {
88 const CharType secondChar = *ptr++;
89
90 if (firstChar == 'p') {
91 if (secondChar == 'x')
92 type = LengthTypePX;
93 if (secondChar == 't')
94 type = LengthTypePT;
95 if (secondChar == 'c')
96 type = LengthTypePC;
97 } else if (firstChar == 'e') {
98 if (secondChar == 'm')
99 type = LengthTypeEMS;
100 if (secondChar == 'x')
101 type = LengthTypeEXS;
102 } else if (firstChar == 'r') {
103 if (secondChar == 'e' && ptr < end) {
104 const CharType thirdChar = *ptr++;
105 if (thirdChar == 'm')
106 type = LengthTypeREMS;
107 }
108 } else if (firstChar == 'c') {
109 if (secondChar == 'h')
110 type = LengthTypeCHS;
111 if (secondChar == 'm')
112 type = LengthTypeCM;
113 } else if (firstChar == 'm' && secondChar == 'm') {
114 type = LengthTypeMM;
115 } else if (firstChar == 'i' && secondChar == 'n') {
116 type = LengthTypeIN;
117 } else if (isHTMLSpace<CharType>(firstChar) && isHTMLSpace<CharType>(sec ondChar)) {
118 type = LengthTypeNumber;
119 }
120 }
121
122 if (!skipOptionalSVGSpaces(ptr, end))
123 return type;
124
125 return LengthTypeUnknown;
126 }
127
128 } // namespace
129
130 SVGLength::SVGLength(SVGLengthMode mode) 41 SVGLength::SVGLength(SVGLengthMode mode)
131 : SVGPropertyBase(classType()) 42 : SVGPropertyBase(classType())
132 , m_valueInSpecifiedUnits(0) 43 , m_value(cssValuePool().createValue(0, CSSPrimitiveValue::UnitType::UserUni ts))
133 , m_unitMode(static_cast<unsigned>(mode)) 44 , m_unitMode(static_cast<unsigned>(mode))
134 , m_unitType(LengthTypeNumber)
135 { 45 {
136 ASSERT(unitMode() == mode); 46 ASSERT(unitMode() == mode);
137 } 47 }
138 48
139 SVGLength::SVGLength(const SVGLength& o) 49 SVGLength::SVGLength(const SVGLength& o)
140 : SVGPropertyBase(classType()) 50 : SVGPropertyBase(classType())
141 , m_valueInSpecifiedUnits(o.m_valueInSpecifiedUnits) 51 , m_value(o.m_value)
142 , m_unitMode(o.m_unitMode) 52 , m_unitMode(o.m_unitMode)
143 , m_unitType(o.m_unitType)
144 { 53 {
145 } 54 }
146 55
56 DEFINE_TRACE(SVGLength)
57 {
58 visitor->trace(m_value);
59 SVGPropertyBase::trace(visitor);
60 }
61
147 PassRefPtrWillBeRawPtr<SVGLength> SVGLength::clone() const 62 PassRefPtrWillBeRawPtr<SVGLength> SVGLength::clone() const
148 { 63 {
149 return adoptRefWillBeNoop(new SVGLength(*this)); 64 return adoptRefWillBeNoop(new SVGLength(*this));
150 } 65 }
151 66
152 PassRefPtrWillBeRawPtr<SVGPropertyBase> SVGLength::cloneForAnimation(const Strin g& value) const 67 PassRefPtrWillBeRawPtr<SVGPropertyBase> SVGLength::cloneForAnimation(const Strin g& value) const
153 { 68 {
154 RefPtrWillBeRawPtr<SVGLength> length = create(); 69 RefPtrWillBeRawPtr<SVGLength> length = create();
155 70
156 length->m_unitMode = m_unitMode; 71 length->m_unitMode = m_unitMode;
157 length->m_unitType = m_unitType;
158 72
159 TrackExceptionState exceptionState; 73 TrackExceptionState exceptionState;
160 length->setValueAsString(value, exceptionState); 74 length->setValueAsString(value, exceptionState);
161 if (exceptionState.hadException()) { 75 if (exceptionState.hadException()) {
162 length->m_unitType = LengthTypeNumber; 76 length->m_value = CSSPrimitiveValue::create(0, CSSPrimitiveValue::UnitTy pe::UserUnits);
163 length->m_valueInSpecifiedUnits = 0;
164 } 77 }
165 78
166 return length.release(); 79 return length.release();
167 } 80 }
168 81
169 bool SVGLength::operator==(const SVGLength& other) const 82 bool SVGLength::operator==(const SVGLength& other) const
170 { 83 {
171 return m_unitMode == other.m_unitMode 84 return m_unitMode == other.m_unitMode && m_value == other.m_value;
172 && m_unitType == other.m_unitType
173 && m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
174 } 85 }
175 86
176 float SVGLength::value(const SVGLengthContext& context) const 87 float SVGLength::value(const SVGLengthContext& context) const
177 { 88 {
178 return context.convertValueToUserUnits(m_valueInSpecifiedUnits, unitMode(), unitType()); 89 return context.convertValueToUserUnits(
90 m_value->getFloatValue(), unitMode(), m_value->typeWithCalcResolved());
179 } 91 }
180 92
181 void SVGLength::setValue(float value, const SVGLengthContext& context) 93 void SVGLength::setValue(float value, const SVGLengthContext& context)
182 { 94 {
183 m_valueInSpecifiedUnits = context.convertValueFromUserUnits(value, unitMode( ), unitType()); 95 m_value = CSSPrimitiveValue::create(
96 context.convertValueFromUserUnits(value, unitMode(), m_value->typeWithCa lcResolved()),
97 m_value->typeWithCalcResolved());
184 } 98 }
185 99
186 void SVGLength::setUnitType(SVGLengthType type) 100 bool isSupportedCSSUnitType(CSSPrimitiveValue::UnitType type)
187 { 101 {
188 ASSERT(type != LengthTypeUnknown && type <= LengthTypeCHS); 102 return type != CSSPrimitiveValue::UnitType::Unknown
189 m_unitType = type; 103 && (type <= CSSPrimitiveValue::UnitType::UserUnits
104 || type == CSSPrimitiveValue::UnitType::Chs
105 || type == CSSPrimitiveValue::UnitType::Rems);
106 }
107
108 void SVGLength::setUnitType(CSSPrimitiveValue::UnitType type)
109 {
110 ASSERT(isSupportedCSSUnitType(type));
111 m_value = CSSPrimitiveValue::create(m_value->getFloatValue(), type);
190 } 112 }
191 113
192 float SVGLength::valueAsPercentage() const 114 float SVGLength::valueAsPercentage() const
193 { 115 {
194 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed. 116 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed.
195 if (m_unitType == LengthTypePercentage) { 117 if (m_value->isPercentage()) {
196 // Note: This division is a source of floating point inaccuracy. 118 // Note: This division is a source of floating point inaccuracy.
197 return m_valueInSpecifiedUnits / 100; 119 return m_value->getFloatValue() / 100;
198 } 120 }
199 121
200 return m_valueInSpecifiedUnits; 122 return m_value->getFloatValue();
201 } 123 }
202 124
203 float SVGLength::valueAsPercentage100() const 125 float SVGLength::valueAsPercentage100() const
204 { 126 {
205 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed. 127 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed.
206 if (m_unitType == LengthTypePercentage) 128 if (m_value->isPercentage())
207 return m_valueInSpecifiedUnits; 129 return m_value->getFloatValue();
208 130
209 return m_valueInSpecifiedUnits * 100; 131 return m_value->getFloatValue() * 100;
210 } 132 }
211 133
212 float SVGLength::scaleByPercentage(float input) const 134 float SVGLength::scaleByPercentage(float input) const
213 { 135 {
214 float result = input * m_valueInSpecifiedUnits; 136 float result = input * m_value->getFloatValue();
215 if (m_unitType == LengthTypePercentage) { 137 if (m_value->isPercentage()) {
216 // Delaying division by 100 as long as possible since it introduces floa ting point errors. 138 // Delaying division by 100 as long as possible since it introduces floa ting point errors.
217 result = result / 100; 139 result = result / 100;
218 } 140 }
219 return result; 141 return result;
220 } 142 }
221 143
222 template<typename CharType>
223 static bool parseValueInternal(const String& string, float& convertedNumber, SVG LengthType& type)
224 {
225 const CharType* ptr = string.getCharacters<CharType>();
226 const CharType* end = ptr + string.length();
227
228 if (!parseNumber(ptr, end, convertedNumber, AllowLeadingWhitespace))
229 return false;
230
231 type = stringToLengthType(ptr, end);
232 ASSERT(ptr <= end);
233 if (type == LengthTypeUnknown)
234 return false;
235
236 return true;
237 }
238
239 void SVGLength::setValueAsString(const String& string, ExceptionState& exception State) 144 void SVGLength::setValueAsString(const String& string, ExceptionState& exception State)
240 { 145 {
241 if (string.isEmpty()) { 146 if (string.isEmpty()) {
242 m_unitType = LengthTypeNumber; 147 m_value = cssValuePool().createValue(0, CSSPrimitiveValue::UnitType::Use rUnits);
243 m_valueInSpecifiedUnits = 0;
244 return; 148 return;
245 } 149 }
246 150
247 float convertedNumber = 0; 151 CSSParserContext svgParserContext(SVGAttributeMode, 0);
248 SVGLengthType type = LengthTypeUnknown; 152 RefPtrWillBeRawPtr<CSSValue> parsed = CSSParser::parseSingleValue(CSSPropert yX, string, svgParserContext);
249 153 if (!parsed || !parsed->isPrimitiveValue()) {
250 bool success = string.is8Bit() ?
251 parseValueInternal<LChar>(string, convertedNumber, type) :
252 parseValueInternal<UChar>(string, convertedNumber, type);
253
254 if (!success) {
255 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid."); 154 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
256 return; 155 return;
257 } 156 }
258 157
259 m_unitType = type; 158 CSSPrimitiveValue* newValue = toCSSPrimitiveValue(parsed.get());
260 m_valueInSpecifiedUnits = convertedNumber; 159 // TODO(fs): Enable calc for SVG lengths
160 if (newValue->isCalculated() || !isSupportedCSSUnitType(newValue->typeWithCa lcResolved())) {
161 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
162 return;
163 }
164
165 m_value = newValue;
261 } 166 }
262 167
263 String SVGLength::valueAsString() const 168 String SVGLength::valueAsString() const
264 { 169 {
265 return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(unitType ()); 170 return m_value->customCSSText();
266 } 171 }
267 172
268 void SVGLength::newValueSpecifiedUnits(SVGLengthType type, float value) 173 void SVGLength::newValueSpecifiedUnits(CSSPrimitiveValue::UnitType type, float v alue)
269 { 174 {
270 setUnitType(type); 175 m_value = CSSPrimitiveValue::create(value, type);
271 m_valueInSpecifiedUnits = value;
272 } 176 }
273 177
274 void SVGLength::convertToSpecifiedUnits(SVGLengthType type, const SVGLengthConte xt& context) 178 void SVGLength::convertToSpecifiedUnits(CSSPrimitiveValue::UnitType type, const SVGLengthContext& context)
275 { 179 {
276 ASSERT(type != LengthTypeUnknown && type <= LengthTypeCHS); 180 ASSERT(isSupportedCSSUnitType(type));
277 181
278 float valueInUserUnits = value(context); 182 float valueInUserUnits = value(context);
279 m_unitType = type; 183 m_value = CSSPrimitiveValue::create(
280 setValue(valueInUserUnits, context); 184 context.convertValueFromUserUnits(valueInUserUnits, unitMode(), type),
185 type);
281 } 186 }
282 187
283 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedNam e& attrName) 188 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedNam e& attrName)
284 { 189 {
285 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMa p; 190 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMa p;
286 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ()); 191 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());
287 192
288 if (s_lengthModeMap.isEmpty()) { 193 if (s_lengthModeMap.isEmpty()) {
289 s_lengthModeMap.set(SVGNames::xAttr, SVGLengthMode::Width); 194 s_lengthModeMap.set(SVGNames::xAttr, SVGLengthMode::Width);
290 s_lengthModeMap.set(SVGNames::yAttr, SVGLengthMode::Height); 195 s_lengthModeMap.set(SVGNames::yAttr, SVGLengthMode::Height);
(...skipping 25 matching lines...) Expand all
316 221
317 return SVGLengthMode::Other; 222 return SVGLengthMode::Other;
318 } 223 }
319 224
320 void SVGLength::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* c ontextElement) 225 void SVGLength::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* c ontextElement)
321 { 226 {
322 SVGLengthContext lengthContext(contextElement); 227 SVGLengthContext lengthContext(contextElement);
323 setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext), le ngthContext); 228 setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext), le ngthContext);
324 } 229 }
325 230
326 void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement, fl oat percentage, unsigned repeatCount, PassRefPtrWillBeRawPtr<SVGPropertyBase> fr omValue, PassRefPtrWillBeRawPtr<SVGPropertyBase> toValue, PassRefPtrWillBeRawPtr <SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement) 231 void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement,
232 float percentage,
233 unsigned repeatCount,
234 PassRefPtrWillBeRawPtr<SVGPropertyBase> fromValue,
235 PassRefPtrWillBeRawPtr<SVGPropertyBase> toValue,
236 PassRefPtrWillBeRawPtr<SVGPropertyBase> toAtEndOfDurationValue,
237 SVGElement* contextElement)
327 { 238 {
328 RefPtrWillBeRawPtr<SVGLength> fromLength = toSVGLength(fromValue); 239 RefPtrWillBeRawPtr<SVGLength> fromLength = toSVGLength(fromValue);
329 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue); 240 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue);
330 RefPtrWillBeRawPtr<SVGLength> toAtEndOfDurationLength = toSVGLength(toAtEndO fDurationValue); 241 RefPtrWillBeRawPtr<SVGLength> toAtEndOfDurationLength = toSVGLength(toAtEndO fDurationValue);
331 242
332 SVGLengthContext lengthContext(contextElement); 243 SVGLengthContext lengthContext(contextElement);
333 float animatedNumber = value(lengthContext); 244 float animatedNumber = value(lengthContext);
334 animationElement->animateAdditiveNumber(percentage, repeatCount, fromLength- >value(lengthContext), toLength->value(lengthContext), toAtEndOfDurationLength-> value(lengthContext), animatedNumber); 245 animationElement->animateAdditiveNumber(percentage, repeatCount, fromLength- >value(lengthContext),
246 toLength->value(lengthContext), toAtEndOfDurationLength->value(lengthCon text), animatedNumber);
335 247
336 ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(animationElement-> attributeName())); 248 ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(animationElement-> attributeName()));
337 m_unitType = percentage < 0.5 ? fromLength->unitType() : toLength->unitType( ); 249
338 setValue(animatedNumber, lengthContext); 250 CSSPrimitiveValue::UnitType newUnit = percentage < 0.5 ? fromLength->typeWit hCalcResolved() : toLength->typeWithCalcResolved();
251 animatedNumber = lengthContext.convertValueFromUserUnits(animatedNumber, uni tMode(), newUnit);
252 m_value = CSSPrimitiveValue::create(animatedNumber, newUnit);
339 } 253 }
340 254
341 float SVGLength::calculateDistance(PassRefPtrWillBeRawPtr<SVGPropertyBase> toVal ue, SVGElement* contextElement) 255 float SVGLength::calculateDistance(PassRefPtrWillBeRawPtr<SVGPropertyBase> toVal ue, SVGElement* contextElement)
342 { 256 {
343 SVGLengthContext lengthContext(contextElement); 257 SVGLengthContext lengthContext(contextElement);
344 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue); 258 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue);
345 259
346 return fabsf(toLength->value(lengthContext) - value(lengthContext)); 260 return fabsf(toLength->value(lengthContext) - value(lengthContext));
347 } 261 }
348 262
349 } 263 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/svg/SVGLength.h ('k') | third_party/WebKit/Source/core/svg/SVGLengthContext.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698