OLD | NEW |
---|---|
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 "core/css/CSSHelper.h" | 26 #include "core/css/CSSHelper.h" |
27 #include "core/css/CSSPrimitiveValue.h" | 27 #include "core/css/CSSPrimitiveValue.h" |
28 #include "core/dom/NodeComputedStyle.h" | 28 #include "core/dom/NodeComputedStyle.h" |
29 #include "core/frame/FrameView.h" | |
29 #include "core/layout/LayoutObject.h" | 30 #include "core/layout/LayoutObject.h" |
30 #include "core/style/ComputedStyle.h" | 31 #include "core/style/ComputedStyle.h" |
31 #include "core/svg/SVGSVGElement.h" | 32 #include "core/svg/SVGSVGElement.h" |
32 #include "platform/LengthFunctions.h" | 33 #include "platform/LengthFunctions.h" |
33 #include "platform/fonts/FontMetrics.h" | 34 #include "platform/fonts/FontMetrics.h" |
34 | 35 |
35 namespace blink { | 36 namespace blink { |
36 | 37 |
37 static inline float dimensionForLengthMode(SVGLengthMode mode, const FloatSize& viewportSize) | 38 static inline float dimensionForLengthMode(SVGLengthMode mode, const FloatSize& viewportSize) |
38 { | 39 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 return value / fontSize; | 95 return value / fontSize; |
95 } | 96 } |
96 | 97 |
97 static float convertValueFromEMSToUserUnits(const ComputedStyle* style, float va lue) | 98 static float convertValueFromEMSToUserUnits(const ComputedStyle* style, float va lue) |
98 { | 99 { |
99 if (!style) | 100 if (!style) |
100 return 0; | 101 return 0; |
101 return value * style->specifiedFontSize(); | 102 return value * style->specifiedFontSize(); |
102 } | 103 } |
103 | 104 |
105 static float viewportWidthPercent(const FloatSize& viewportSize) | |
rwlbuis
2015/12/22 16:41:49
These 4 helper functions should be inline and shou
Shanmuga Pandi
2015/12/23 09:00:59
Done.
| |
106 { | |
107 return viewportSize.width() / 100; | |
108 } | |
109 | |
110 static float viewportHeightPercent(const FloatSize& viewportSize) | |
111 { | |
112 return viewportSize.height() / 100; | |
113 } | |
rwlbuis
2015/12/22 16:41:49
You can probably unify above two by passing a dime
Shanmuga Pandi
2015/12/23 09:00:59
Done.
| |
114 | |
115 static float viewportMinPercent(const FloatSize& viewportSize) | |
116 { | |
117 return std::min(viewportSize.width(), viewportSize.height()) / 100; | |
118 } | |
119 | |
120 static float viewportMaxPercent(const FloatSize& viewportSize) | |
121 { | |
122 return std::max(viewportSize.width(), viewportSize.height()) / 100; | |
123 } | |
124 | |
104 SVGLengthContext::SVGLengthContext(const SVGElement* context) | 125 SVGLengthContext::SVGLengthContext(const SVGElement* context) |
105 : m_context(context) | 126 : m_context(context) |
106 { | 127 { |
107 } | 128 } |
108 | 129 |
109 FloatRect SVGLengthContext::resolveRectangle(const SVGElement* context, SVGUnitT ypes::SVGUnitType type, const FloatRect& viewport, const SVGLength& x, const SVG Length& y, const SVGLength& width, const SVGLength& height) | 130 FloatRect SVGLengthContext::resolveRectangle(const SVGElement* context, SVGUnitT ypes::SVGUnitType type, const FloatRect& viewport, const SVGLength& x, const SVG Length& y, const SVGLength& width, const SVGLength& height) |
110 { | 131 { |
111 ASSERT(type != SVGUnitTypes::SVG_UNIT_TYPE_UNKNOWN); | 132 ASSERT(type != SVGUnitTypes::SVG_UNIT_TYPE_UNKNOWN); |
112 if (type != SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) { | 133 if (type != SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) { |
113 const FloatSize& viewportSize = viewport.size(); | 134 const FloatSize& viewportSize = viewport.size(); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 break; | 240 break; |
220 case CSSPrimitiveValue::UnitType::Picas: | 241 case CSSPrimitiveValue::UnitType::Picas: |
221 userUnits = value * cssPixelsPerPica; | 242 userUnits = value * cssPixelsPerPica; |
222 break; | 243 break; |
223 case CSSPrimitiveValue::UnitType::Rems: | 244 case CSSPrimitiveValue::UnitType::Rems: |
224 userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); | 245 userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); |
225 break; | 246 break; |
226 case CSSPrimitiveValue::UnitType::Chs: | 247 case CSSPrimitiveValue::UnitType::Chs: |
227 userUnits = convertValueFromCHSToUserUnits(value); | 248 userUnits = convertValueFromCHSToUserUnits(value); |
228 break; | 249 break; |
250 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
251 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
252 case CSSPrimitiveValue::UnitType::ViewportMin: | |
253 case CSSPrimitiveValue::UnitType::ViewportMax: | |
254 userUnits = convertValueFromViewportUnitsToUserUnits(fromUnit, value); | |
255 break; | |
229 default: | 256 default: |
230 ASSERT_NOT_REACHED(); | 257 ASSERT_NOT_REACHED(); |
231 break; | 258 break; |
232 } | 259 } |
233 | 260 |
234 // Since we mix css <length> values with svg's length values we need to | 261 // Since we mix css <length> values with svg's length values we need to |
235 // clamp values to the narrowest range, otherwise it can result in | 262 // clamp values to the narrowest range, otherwise it can result in |
236 // rendering issues. | 263 // rendering issues. |
237 return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); | 264 return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); |
238 } | 265 } |
(...skipping 27 matching lines...) Expand all Loading... | |
266 case CSSPrimitiveValue::UnitType::Centimeters: | 293 case CSSPrimitiveValue::UnitType::Centimeters: |
267 return value / cssPixelsPerCentimeter; | 294 return value / cssPixelsPerCentimeter; |
268 case CSSPrimitiveValue::UnitType::Millimeters: | 295 case CSSPrimitiveValue::UnitType::Millimeters: |
269 return value / cssPixelsPerMillimeter; | 296 return value / cssPixelsPerMillimeter; |
270 case CSSPrimitiveValue::UnitType::Inches: | 297 case CSSPrimitiveValue::UnitType::Inches: |
271 return value / cssPixelsPerInch; | 298 return value / cssPixelsPerInch; |
272 case CSSPrimitiveValue::UnitType::Points: | 299 case CSSPrimitiveValue::UnitType::Points: |
273 return value / cssPixelsPerPoint; | 300 return value / cssPixelsPerPoint; |
274 case CSSPrimitiveValue::UnitType::Picas: | 301 case CSSPrimitiveValue::UnitType::Picas: |
275 return value / cssPixelsPerPica; | 302 return value / cssPixelsPerPica; |
303 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
304 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
305 case CSSPrimitiveValue::UnitType::ViewportMin: | |
306 case CSSPrimitiveValue::UnitType::ViewportMax: | |
307 return convertValueFromUserUnitsToViewportUnits(toUnit, value); | |
276 default: | 308 default: |
277 break; | 309 break; |
278 } | 310 } |
279 | 311 |
280 ASSERT_NOT_REACHED(); | 312 ASSERT_NOT_REACHED(); |
281 return 0; | 313 return 0; |
282 } | 314 } |
283 | 315 |
284 float SVGLengthContext::convertValueFromUserUnitsToCHS(float value) const | 316 float SVGLengthContext::convertValueFromUserUnitsToCHS(float value) const |
285 { | 317 { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 { | 354 { |
323 const ComputedStyle* style = computedStyleForLengthResolving(m_context); | 355 const ComputedStyle* style = computedStyleForLengthResolving(m_context); |
324 if (!style) | 356 if (!style) |
325 return 0; | 357 return 0; |
326 | 358 |
327 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg | 359 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg |
328 // if this causes problems in real world cases maybe it would be best to rem ove this | 360 // if this causes problems in real world cases maybe it would be best to rem ove this |
329 return value * ceilf(style->fontMetrics().xHeight() / style->effectiveZoom() ); | 361 return value * ceilf(style->fontMetrics().xHeight() / style->effectiveZoom() ); |
330 } | 362 } |
331 | 363 |
364 float SVGLengthContext::convertValueFromUserUnitsToViewportUnits(CSSPrimitiveVal ue::UnitType toUnit, float value) const | |
rwlbuis
2015/12/22 16:41:49
Do we need to take zoom into account in these 2 me
Shanmuga Pandi
2015/12/23 09:00:59
Acknowledged.
| |
365 { | |
366 if (!m_context) | |
367 return 0; | |
368 | |
369 const Document& document = m_context->document(); | |
370 FrameView* view = document.view(); | |
371 if (!view) | |
372 return 0; | |
373 | |
374 FloatSize viewportSize(view->width(), view->height()); | |
375 | |
376 switch (toUnit) { | |
377 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
378 return value / viewportWidthPercent(viewportSize); | |
379 | |
380 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
381 return value / viewportHeightPercent(viewportSize); | |
382 | |
383 case CSSPrimitiveValue::UnitType::ViewportMin: | |
384 return value / viewportMinPercent(viewportSize); | |
385 | |
386 case CSSPrimitiveValue::UnitType::ViewportMax: | |
387 return value / viewportMaxPercent(viewportSize); | |
388 default: | |
389 break; | |
390 } | |
391 | |
392 ASSERT_NOT_REACHED(); | |
393 return 0; | |
394 } | |
395 | |
396 float SVGLengthContext::convertValueFromViewportUnitsToUserUnits(CSSPrimitiveVal ue::UnitType fromUnit, float value) const | |
397 { | |
398 if (!m_context) | |
399 return 0; | |
400 | |
401 const Document& document = m_context->document(); | |
402 FrameView* view = document.view(); | |
403 if (!view) | |
404 return 0; | |
405 | |
406 FloatSize viewportSize(view->width(), view->height()); | |
407 | |
408 switch (fromUnit) { | |
409 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
410 return value * viewportWidthPercent(viewportSize); | |
411 | |
412 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
413 return value * viewportHeightPercent(viewportSize); | |
414 | |
415 case CSSPrimitiveValue::UnitType::ViewportMin: | |
416 return value * viewportMinPercent(viewportSize); | |
417 | |
418 case CSSPrimitiveValue::UnitType::ViewportMax: | |
419 return value * viewportMaxPercent(viewportSize); | |
420 default: | |
421 break; | |
422 } | |
423 | |
424 ASSERT_NOT_REACHED(); | |
425 return 0; | |
426 } | |
427 | |
332 bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const | 428 bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const |
333 { | 429 { |
334 if (!m_context) | 430 if (!m_context) |
335 return false; | 431 return false; |
336 | 432 |
337 // Root <svg> element lengths are resolved against the top level viewport. | 433 // Root <svg> element lengths are resolved against the top level viewport. |
338 if (m_context->isOutermostSVGSVGElement()) { | 434 if (m_context->isOutermostSVGSVGElement()) { |
339 viewportSize = toSVGSVGElement(m_context)->currentViewportSize(); | 435 viewportSize = toSVGSVGElement(m_context)->currentViewportSize(); |
340 return true; | 436 return true; |
341 } | 437 } |
342 | 438 |
343 // Take size from nearest viewport element. | 439 // Take size from nearest viewport element. |
344 SVGElement* viewportElement = m_context->viewportElement(); | 440 SVGElement* viewportElement = m_context->viewportElement(); |
345 if (!isSVGSVGElement(viewportElement)) | 441 if (!isSVGSVGElement(viewportElement)) |
346 return false; | 442 return false; |
347 | 443 |
348 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); | 444 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); |
349 viewportSize = svg.currentViewBoxRect().size(); | 445 viewportSize = svg.currentViewBoxRect().size(); |
350 if (viewportSize.isEmpty()) | 446 if (viewportSize.isEmpty()) |
351 viewportSize = svg.currentViewportSize(); | 447 viewportSize = svg.currentViewportSize(); |
352 | 448 |
353 return true; | 449 return true; |
354 } | 450 } |
355 | 451 |
356 } | 452 } |
OLD | NEW |