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

Side by Side Diff: Source/core/svg/SVGLengthContext.cpp

Issue 901193002: De-ExceptionState-ify SVGLengthContext (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase. Created 5 years, 10 months 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 | Annotate | Revision Log
« no previous file with comments | « Source/core/svg/SVGLengthContext.h ('k') | Source/core/svg/SVGLengthList.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 * Copyright (C) Research In Motion Limited 2011. All rights reserved. 5 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details. 15 * Library General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Library General Public License 17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to 18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA. 20 * Boston, MA 02110-1301, USA.
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/svg/SVGLengthContext.h" 24 #include "core/svg/SVGLengthContext.h"
25 25
26 #include "bindings/core/v8/ExceptionMessages.h"
27 #include "bindings/core/v8/ExceptionState.h"
28 #include "core/SVGNames.h"
29 #include "core/css/CSSHelper.h" 26 #include "core/css/CSSHelper.h"
30 #include "core/dom/ExceptionCode.h" 27 #include "core/layout/LayoutObject.h"
31 #include "core/rendering/RenderView.h" 28 #include "core/rendering/style/RenderStyle.h"
32 #include "core/rendering/svg/RenderSVGRoot.h"
33 #include "core/rendering/svg/RenderSVGViewportContainer.h"
34 #include "core/svg/SVGSVGElement.h" 29 #include "core/svg/SVGSVGElement.h"
35 #include "platform/fonts/FontMetrics.h" 30 #include "platform/fonts/FontMetrics.h"
36 31
37 namespace blink { 32 namespace blink {
38 33
39 SVGLengthContext::SVGLengthContext(const SVGElement* context) 34 SVGLengthContext::SVGLengthContext(const SVGElement* context)
40 : m_context(context) 35 : m_context(context)
41 { 36 {
42 } 37 }
43 38
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 ASSERT(type != SVGUnitTypes::SVG_UNIT_TYPE_UNKNOWN); 79 ASSERT(type != SVGUnitTypes::SVG_UNIT_TYPE_UNKNOWN);
85 if (type == SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) { 80 if (type == SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) {
86 SVGLengthContext lengthContext(context); 81 SVGLengthContext lengthContext(context);
87 return x->value(lengthContext); 82 return x->value(lengthContext);
88 } 83 }
89 84
90 // FIXME: valueAsPercentage() won't be correct for eg. cm units. They need t o be resolved in user space and then be considered in objectBoundingBox space. 85 // FIXME: valueAsPercentage() won't be correct for eg. cm units. They need t o be resolved in user space and then be considered in objectBoundingBox space.
91 return x->valueAsPercentage(); 86 return x->valueAsPercentage();
92 } 87 }
93 88
94 float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode, SVGLengthType fromUnit, ExceptionState& exceptionState) const 89 float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode, SVGLengthType fromUnit) const
95 { 90 {
96 switch (fromUnit) { 91 switch (fromUnit) {
97 case LengthTypeUnknown: 92 case LengthTypeUnknown:
98 exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::a rgumentNullOrIncorrectType(3, "SVGLengthType"));
99 return 0; 93 return 0;
100 case LengthTypeNumber: 94 case LengthTypeNumber:
101 return value; 95 return value;
102 case LengthTypePX: 96 case LengthTypePX:
103 return value; 97 return value;
104 case LengthTypePercentage: 98 case LengthTypePercentage: {
105 return convertValueFromPercentageToUserUnits(value, mode, exceptionState ) / 100; 99 FloatSize viewportSize;
100 if (!determineViewport(viewportSize))
101 return 0;
102 return convertValueFromPercentageToUserUnits(value, mode, viewportSize) / 100;
103 }
106 case LengthTypeEMS: 104 case LengthTypeEMS:
107 return convertValueFromEMSToUserUnits(value, exceptionState); 105 return convertValueFromEMSToUserUnits(value);
108 case LengthTypeEXS: 106 case LengthTypeEXS:
109 return convertValueFromEXSToUserUnits(value, exceptionState); 107 return convertValueFromEXSToUserUnits(value);
110 case LengthTypeCM: 108 case LengthTypeCM:
111 return value * cssPixelsPerCentimeter; 109 return value * cssPixelsPerCentimeter;
112 case LengthTypeMM: 110 case LengthTypeMM:
113 return value * cssPixelsPerMillimeter; 111 return value * cssPixelsPerMillimeter;
114 case LengthTypeIN: 112 case LengthTypeIN:
115 return value * cssPixelsPerInch; 113 return value * cssPixelsPerInch;
116 case LengthTypePT: 114 case LengthTypePT:
117 return value * cssPixelsPerPoint; 115 return value * cssPixelsPerPoint;
118 case LengthTypePC: 116 case LengthTypePC:
119 return value * cssPixelsPerPica; 117 return value * cssPixelsPerPica;
120 } 118 }
121 119
122 ASSERT_NOT_REACHED(); 120 ASSERT_NOT_REACHED();
123 return 0; 121 return 0;
124 } 122 }
125 123
126 float SVGLengthContext::convertValueFromUserUnits(float value, SVGLengthMode mod e, SVGLengthType toUnit, ExceptionState& exceptionState) const 124 float SVGLengthContext::convertValueFromUserUnits(float value, SVGLengthMode mod e, SVGLengthType toUnit) const
127 { 125 {
128 switch (toUnit) { 126 switch (toUnit) {
129 case LengthTypeUnknown: 127 case LengthTypeUnknown:
130 exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::a rgumentNullOrIncorrectType(3, "SVGLengthType"));
131 return 0; 128 return 0;
132 case LengthTypeNumber: 129 case LengthTypeNumber:
133 return value; 130 return value;
134 case LengthTypePercentage: 131 case LengthTypePercentage: {
135 return convertValueFromUserUnitsToPercentage(value * 100, mode, exceptio nState); 132 FloatSize viewportSize;
133 if (!determineViewport(viewportSize))
134 return 0;
135 return convertValueFromUserUnitsToPercentage(value * 100, mode, viewport Size);
136 }
136 case LengthTypeEMS: 137 case LengthTypeEMS:
137 return convertValueFromUserUnitsToEMS(value, exceptionState); 138 return convertValueFromUserUnitsToEMS(value);
138 case LengthTypeEXS: 139 case LengthTypeEXS:
139 return convertValueFromUserUnitsToEXS(value, exceptionState); 140 return convertValueFromUserUnitsToEXS(value);
140 case LengthTypePX: 141 case LengthTypePX:
141 return value; 142 return value;
142 case LengthTypeCM: 143 case LengthTypeCM:
143 return value / cssPixelsPerCentimeter; 144 return value / cssPixelsPerCentimeter;
144 case LengthTypeMM: 145 case LengthTypeMM:
145 return value / cssPixelsPerMillimeter; 146 return value / cssPixelsPerMillimeter;
146 case LengthTypeIN: 147 case LengthTypeIN:
147 return value / cssPixelsPerInch; 148 return value / cssPixelsPerInch;
148 case LengthTypePT: 149 case LengthTypePT:
149 return value / cssPixelsPerPoint; 150 return value / cssPixelsPerPoint;
(...skipping 12 matching lines...) Expand all
162 return viewportSize.width(); 163 return viewportSize.width();
163 case LengthModeHeight: 164 case LengthModeHeight:
164 return viewportSize.height(); 165 return viewportSize.height();
165 case LengthModeOther: 166 case LengthModeOther:
166 return sqrtf(viewportSize.diagonalLengthSquared() / 2); 167 return sqrtf(viewportSize.diagonalLengthSquared() / 2);
167 } 168 }
168 ASSERT_NOT_REACHED(); 169 ASSERT_NOT_REACHED();
169 return 0; 170 return 0;
170 } 171 }
171 172
172 float SVGLengthContext::convertValueFromUserUnitsToPercentage(float value, SVGLe ngthMode mode, ExceptionState& exceptionState) const 173 float SVGLengthContext::convertValueFromUserUnitsToPercentage(float value, SVGLe ngthMode mode, const FloatSize& viewportSize)
173 { 174 {
174 FloatSize viewportSize;
175 if (!determineViewport(viewportSize)) {
176 exceptionState.throwDOMException(NotSupportedError, "The viewport could not be determined.");
177 return 0;
178 }
179
180 return value / dimensionForLengthMode(mode, viewportSize) * 100; 175 return value / dimensionForLengthMode(mode, viewportSize) * 100;
181 } 176 }
182 177
183 float SVGLengthContext::convertValueFromPercentageToUserUnits(float value, SVGLe ngthMode mode, ExceptionState& exceptionState) const
184 {
185 FloatSize viewportSize;
186 if (!determineViewport(viewportSize)) {
187 exceptionState.throwDOMException(NotSupportedError, "The viewport could not be determined.");
188 return 0;
189 }
190 return convertValueFromPercentageToUserUnits(value, mode, viewportSize);
191 }
192
193 float SVGLengthContext::convertValueFromPercentageToUserUnits(float value, SVGLe ngthMode mode, const FloatSize& viewportSize) 178 float SVGLengthContext::convertValueFromPercentageToUserUnits(float value, SVGLe ngthMode mode, const FloatSize& viewportSize)
194 { 179 {
195 return value * dimensionForLengthMode(mode, viewportSize); 180 return value * dimensionForLengthMode(mode, viewportSize);
196 } 181 }
197 182
198 float SVGLengthContext::convertValueFromPercentageToUserUnits(const SVGLength& v alue, const FloatSize& viewportSize) 183 float SVGLengthContext::convertValueFromPercentageToUserUnits(const SVGLength& v alue, const FloatSize& viewportSize)
199 { 184 {
200 return value.scaleByPercentage(dimensionForLengthMode(value.unitMode(), view portSize)); 185 return value.scaleByPercentage(dimensionForLengthMode(value.unitMode(), view portSize));
201 } 186 }
202 187
203 static inline RenderStyle* renderStyleForLengthResolving(const SVGElement* conte xt) 188 static inline RenderStyle* renderStyleForLengthResolving(const SVGElement* conte xt)
204 { 189 {
205 if (!context) 190 if (!context)
206 return 0; 191 return 0;
207 192
208 const ContainerNode* currentContext = context; 193 const ContainerNode* currentContext = context;
209 do { 194 do {
210 if (currentContext->renderer()) 195 if (currentContext->renderer())
211 return currentContext->renderer()->style(); 196 return currentContext->renderer()->style();
212 currentContext = currentContext->parentNode(); 197 currentContext = currentContext->parentNode();
213 } while (currentContext); 198 } while (currentContext);
214 199
215 // There must be at least a RenderSVGRoot renderer, carrying a style. 200 // There must be at least a RenderSVGRoot renderer, carrying a style.
216 ASSERT_NOT_REACHED(); 201 ASSERT_NOT_REACHED();
217 return 0; 202 return 0;
218 } 203 }
219 204
220 float SVGLengthContext::convertValueFromUserUnitsToEMS(float value, ExceptionSta te& exceptionState) const 205 float SVGLengthContext::convertValueFromUserUnitsToEMS(float value) const
221 { 206 {
222 RenderStyle* style = renderStyleForLengthResolving(m_context); 207 RenderStyle* style = renderStyleForLengthResolving(m_context);
223 if (!style) { 208 if (!style)
224 exceptionState.throwDOMException(NotSupportedError, "No context could be found.");
225 return 0; 209 return 0;
226 }
227 210
228 float fontSize = style->specifiedFontSize(); 211 float fontSize = style->specifiedFontSize();
229 if (!fontSize) { 212 if (!fontSize)
230 exceptionState.throwDOMException(NotSupportedError, "No font-size could be determined.");
231 return 0; 213 return 0;
232 }
233 214
234 return value / fontSize; 215 return value / fontSize;
235 } 216 }
236 217
237 float SVGLengthContext::convertValueFromEMSToUserUnits(float value, ExceptionSta te& exceptionState) const 218 float SVGLengthContext::convertValueFromEMSToUserUnits(float value) const
238 { 219 {
239 RenderStyle* style = renderStyleForLengthResolving(m_context); 220 RenderStyle* style = renderStyleForLengthResolving(m_context);
240 if (!style) { 221 if (!style)
241 exceptionState.throwDOMException(NotSupportedError, "No context could be found.");
242 return 0; 222 return 0;
243 }
244
245 return value * style->specifiedFontSize(); 223 return value * style->specifiedFontSize();
246 } 224 }
247 225
248 float SVGLengthContext::convertValueFromUserUnitsToEXS(float value, ExceptionSta te& exceptionState) const 226 float SVGLengthContext::convertValueFromUserUnitsToEXS(float value) const
249 { 227 {
250 RenderStyle* style = renderStyleForLengthResolving(m_context); 228 RenderStyle* style = renderStyleForLengthResolving(m_context);
251 if (!style) { 229 if (!style)
252 exceptionState.throwDOMException(NotSupportedError, "No context could be found.");
253 return 0; 230 return 0;
254 }
255 231
256 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg 232 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg
257 // if this causes problems in real world cases maybe it would be best to rem ove this 233 // if this causes problems in real world cases maybe it would be best to rem ove this
258 float xHeight = ceilf(style->fontMetrics().xHeight()); 234 float xHeight = ceilf(style->fontMetrics().xHeight());
259 if (!xHeight) { 235 if (!xHeight)
260 exceptionState.throwDOMException(NotSupportedError, "No x-height could b e determined.");
261 return 0; 236 return 0;
262 }
263 237
264 return value / xHeight; 238 return value / xHeight;
265 } 239 }
266 240
267 float SVGLengthContext::convertValueFromEXSToUserUnits(float value, ExceptionSta te& exceptionState) const 241 float SVGLengthContext::convertValueFromEXSToUserUnits(float value) const
268 { 242 {
269 RenderStyle* style = renderStyleForLengthResolving(m_context); 243 RenderStyle* style = renderStyleForLengthResolving(m_context);
270 if (!style) { 244 if (!style)
271 exceptionState.throwDOMException(NotSupportedError, "No context could be found.");
272 return 0; 245 return 0;
273 }
274 246
275 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg 247 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg
276 // if this causes problems in real world cases maybe it would be best to rem ove this 248 // if this causes problems in real world cases maybe it would be best to rem ove this
277 return value * ceilf(style->fontMetrics().xHeight()); 249 return value * ceilf(style->fontMetrics().xHeight());
278 } 250 }
279 251
280 bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const 252 bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const
281 { 253 {
282 if (!m_context) 254 if (!m_context)
283 return false; 255 return false;
(...skipping 11 matching lines...) Expand all
295 267
296 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); 268 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement);
297 viewportSize = svg.currentViewBoxRect().size(); 269 viewportSize = svg.currentViewBoxRect().size();
298 if (viewportSize.isEmpty()) 270 if (viewportSize.isEmpty())
299 viewportSize = svg.currentViewportSize(); 271 viewportSize = svg.currentViewportSize();
300 272
301 return true; 273 return true;
302 } 274 }
303 275
304 } 276 }
OLDNEW
« no previous file with comments | « Source/core/svg/SVGLengthContext.h ('k') | Source/core/svg/SVGLengthList.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698