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

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: Fixed the initial types on properties 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 void SVGLength::setUnitType(CSSPrimitiveValue::UnitType type)
187 { 101 {
188 ASSERT(type != LengthTypeUnknown && type <= LengthTypeCHS); 102 ASSERT(type != CSSPrimitiveValue::UnitType::Unknown
189 m_unitType = type; 103 && (type <= CSSPrimitiveValue::UnitType::UserUnits
104 || type == CSSPrimitiveValue::UnitType::Chs
105 || type == CSSPrimitiveValue::UnitType::Rems));
106 m_value = CSSPrimitiveValue::create(m_value->getFloatValue(), type);
190 } 107 }
191 108
192 float SVGLength::valueAsPercentage() const 109 float SVGLength::valueAsPercentage() const
193 { 110 {
194 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed. 111 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed.
195 if (m_unitType == LengthTypePercentage) { 112 if (m_value->typeWithCalcResolved() == CSSPrimitiveValue::UnitType::Percenta ge) {
Timothy Loh 2015/11/04 00:33:44 isPercentage()
Stephen Chennney 2015/11/04 18:04:47 Done.
196 // Note: This division is a source of floating point inaccuracy. 113 // Note: This division is a source of floating point inaccuracy.
197 return m_valueInSpecifiedUnits / 100; 114 return m_value->getFloatValue() / 100;
198 } 115 }
199 116
200 return m_valueInSpecifiedUnits; 117 return m_value->getFloatValue();
201 } 118 }
202 119
203 float SVGLength::valueAsPercentage100() const 120 float SVGLength::valueAsPercentage100() const
204 { 121 {
205 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed. 122 // LengthTypePercentage is represented with 100% = 100.0. Good for accuracy but could eventually be changed.
206 if (m_unitType == LengthTypePercentage) 123 if (m_value->typeWithCalcResolved() == CSSPrimitiveValue::UnitType::Percenta ge)
Timothy Loh 2015/11/04 00:33:44 isPercentage()
Stephen Chennney 2015/11/04 18:04:47 Done.
207 return m_valueInSpecifiedUnits; 124 return m_value->getFloatValue();
208 125
209 return m_valueInSpecifiedUnits * 100; 126 return m_value->getFloatValue() * 100;
210 } 127 }
211 128
212 float SVGLength::scaleByPercentage(float input) const 129 float SVGLength::scaleByPercentage(float input) const
213 { 130 {
214 float result = input * m_valueInSpecifiedUnits; 131 float result = input * m_value->getFloatValue();
215 if (m_unitType == LengthTypePercentage) { 132 if (m_value->typeWithCalcResolved() == CSSPrimitiveValue::UnitType::Percenta ge) {
Timothy Loh 2015/11/04 00:33:44 isPercentage()
Stephen Chennney 2015/11/04 18:04:47 Done.
216 // Delaying division by 100 as long as possible since it introduces floa ting point errors. 133 // Delaying division by 100 as long as possible since it introduces floa ting point errors.
217 result = result / 100; 134 result = result / 100;
218 } 135 }
219 return result; 136 return result;
220 } 137 }
221 138
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) 139 void SVGLength::setValueAsString(const String& string, ExceptionState& exception State)
240 { 140 {
241 if (string.isEmpty()) { 141 if (string.isEmpty()) {
242 m_unitType = LengthTypeNumber; 142 m_value = cssValuePool().createValue(0, CSSPrimitiveValue::UnitType::Use rUnits);
243 m_valueInSpecifiedUnits = 0;
244 return; 143 return;
245 } 144 }
246 145
247 float convertedNumber = 0; 146 CSSParserContext svgParserContext(SVGAttributeMode, 0);
248 SVGLengthType type = LengthTypeUnknown; 147 RefPtrWillBeRawPtr<CSSValue> parsed = CSSParser::parseSingleValue(CSSPropert yX, string, svgParserContext);
249 148 if (!parsed || !parsed->isPrimitiveValue()) {
Stephen Chennney 2015/11/04 18:04:47 Does this catch and prevent the calc() case?
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."); 149 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
256 return; 150 return;
257 } 151 }
258 152
259 m_unitType = type; 153 ASSERT(parsed->isPrimitiveValue());
Timothy Loh 2015/11/04 00:33:44 This is.. super redundant, since we checked it a f
Stephen Chennney 2015/11/04 18:04:47 Done.
260 m_valueInSpecifiedUnits = convertedNumber; 154 m_value = toCSSPrimitiveValue(parsed.get());
261 } 155 }
262 156
263 String SVGLength::valueAsString() const 157 String SVGLength::valueAsString() const
264 { 158 {
265 return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(unitType ()); 159 return m_value->customCSSText();
266 } 160 }
267 161
268 void SVGLength::newValueSpecifiedUnits(SVGLengthType type, float value) 162 void SVGLength::newValueSpecifiedUnits(CSSPrimitiveValue::UnitType type, float v alue)
269 { 163 {
270 setUnitType(type); 164 m_value = CSSPrimitiveValue::create(value, type);
271 m_valueInSpecifiedUnits = value;
272 } 165 }
273 166
274 void SVGLength::convertToSpecifiedUnits(SVGLengthType type, const SVGLengthConte xt& context) 167 void SVGLength::convertToSpecifiedUnits(CSSPrimitiveValue::UnitType type, const SVGLengthContext& context)
275 { 168 {
276 ASSERT(type != LengthTypeUnknown && type <= LengthTypeCHS); 169 ASSERT(type != CSSPrimitiveValue::UnitType::Unknown
170 && (type <= CSSPrimitiveValue::UnitType::Picas
171 || type == CSSPrimitiveValue::UnitType::Chs
172 || type == CSSPrimitiveValue::UnitType::Rems));
277 173
278 float valueInUserUnits = value(context); 174 float valueInUserUnits = value(context);
279 m_unitType = type; 175 m_value = CSSPrimitiveValue::create(
280 setValue(valueInUserUnits, context); 176 context.convertValueFromUserUnits(valueInUserUnits, unitMode(), type),
177 type);
281 } 178 }
282 179
283 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedNam e& attrName) 180 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedNam e& attrName)
284 { 181 {
285 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMa p; 182 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMa p;
286 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ()); 183 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());
287 184
288 if (s_lengthModeMap.isEmpty()) { 185 if (s_lengthModeMap.isEmpty()) {
289 s_lengthModeMap.set(SVGNames::xAttr, SVGLengthMode::Width); 186 s_lengthModeMap.set(SVGNames::xAttr, SVGLengthMode::Width);
290 s_lengthModeMap.set(SVGNames::yAttr, SVGLengthMode::Height); 187 s_lengthModeMap.set(SVGNames::yAttr, SVGLengthMode::Height);
(...skipping 25 matching lines...) Expand all
316 213
317 return SVGLengthMode::Other; 214 return SVGLengthMode::Other;
318 } 215 }
319 216
320 void SVGLength::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* c ontextElement) 217 void SVGLength::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* c ontextElement)
321 { 218 {
322 SVGLengthContext lengthContext(contextElement); 219 SVGLengthContext lengthContext(contextElement);
323 setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext), le ngthContext); 220 setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext), le ngthContext);
324 } 221 }
325 222
326 void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement, fl oat percentage, unsigned repeatCount, PassRefPtrWillBeRawPtr<SVGPropertyBase> fr omValue, PassRefPtrWillBeRawPtr<SVGPropertyBase> toValue, PassRefPtrWillBeRawPtr <SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement) 223 void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement,
224 float percentage,
225 unsigned repeatCount,
226 PassRefPtrWillBeRawPtr<SVGPropertyBase> fromValue,
227 PassRefPtrWillBeRawPtr<SVGPropertyBase> toValue,
228 PassRefPtrWillBeRawPtr<SVGPropertyBase> toAtEndOfDurationValue,
229 SVGElement* contextElement)
327 { 230 {
328 RefPtrWillBeRawPtr<SVGLength> fromLength = toSVGLength(fromValue); 231 RefPtrWillBeRawPtr<SVGLength> fromLength = toSVGLength(fromValue);
329 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue); 232 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue);
330 RefPtrWillBeRawPtr<SVGLength> toAtEndOfDurationLength = toSVGLength(toAtEndO fDurationValue); 233 RefPtrWillBeRawPtr<SVGLength> toAtEndOfDurationLength = toSVGLength(toAtEndO fDurationValue);
331 234
332 SVGLengthContext lengthContext(contextElement); 235 SVGLengthContext lengthContext(contextElement);
333 float animatedNumber = value(lengthContext); 236 float animatedNumber = value(lengthContext);
334 animationElement->animateAdditiveNumber(percentage, repeatCount, fromLength- >value(lengthContext), toLength->value(lengthContext), toAtEndOfDurationLength-> value(lengthContext), animatedNumber); 237 animationElement->animateAdditiveNumber(percentage, repeatCount, fromLength- >value(lengthContext),
238 toLength->value(lengthContext), toAtEndOfDurationLength->value(lengthCon text), animatedNumber);
335 239
336 ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(animationElement-> attributeName())); 240 ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(animationElement-> attributeName()));
337 m_unitType = percentage < 0.5 ? fromLength->unitType() : toLength->unitType( ); 241
338 setValue(animatedNumber, lengthContext); 242 CSSPrimitiveValue::UnitType newUnit = percentage < 0.5 ? fromLength->typeWit hCalcResolved() : toLength->typeWithCalcResolved();
243 animatedNumber = lengthContext.convertValueFromUserUnits(animatedNumber, uni tMode(), newUnit);
244 m_value = CSSPrimitiveValue::create(animatedNumber, newUnit);
339 } 245 }
340 246
341 float SVGLength::calculateDistance(PassRefPtrWillBeRawPtr<SVGPropertyBase> toVal ue, SVGElement* contextElement) 247 float SVGLength::calculateDistance(PassRefPtrWillBeRawPtr<SVGPropertyBase> toVal ue, SVGElement* contextElement)
342 { 248 {
343 SVGLengthContext lengthContext(contextElement); 249 SVGLengthContext lengthContext(contextElement);
344 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue); 250 RefPtrWillBeRawPtr<SVGLength> toLength = toSVGLength(toValue);
345 251
346 return fabsf(toLength->value(lengthContext) - value(lengthContext)); 252 return fabsf(toLength->value(lengthContext) - value(lengthContext));
347 } 253 }
348 254
349 } 255 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698