Chromium Code Reviews| 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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 break; | 220 break; |
| 220 case CSSPrimitiveValue::UnitType::Picas: | 221 case CSSPrimitiveValue::UnitType::Picas: |
| 221 userUnits = value * cssPixelsPerPica; | 222 userUnits = value * cssPixelsPerPica; |
| 222 break; | 223 break; |
| 223 case CSSPrimitiveValue::UnitType::Rems: | 224 case CSSPrimitiveValue::UnitType::Rems: |
| 224 userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); | 225 userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); |
| 225 break; | 226 break; |
| 226 case CSSPrimitiveValue::UnitType::Chs: | 227 case CSSPrimitiveValue::UnitType::Chs: |
| 227 userUnits = convertValueFromCHSToUserUnits(value); | 228 userUnits = convertValueFromCHSToUserUnits(value); |
| 228 break; | 229 break; |
| 230 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
| 231 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
| 232 case CSSPrimitiveValue::UnitType::ViewportMin: | |
| 233 case CSSPrimitiveValue::UnitType::ViewportMax: | |
| 234 userUnits = convertValueFromViewportUnitsToUserUnits(fromUnit, value); | |
| 235 break; | |
| 229 default: | 236 default: |
| 230 ASSERT_NOT_REACHED(); | 237 ASSERT_NOT_REACHED(); |
| 231 break; | 238 break; |
| 232 } | 239 } |
| 233 | 240 |
| 234 // Since we mix css <length> values with svg's length values we need to | 241 // 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 | 242 // clamp values to the narrowest range, otherwise it can result in |
| 236 // rendering issues. | 243 // rendering issues. |
| 237 return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); | 244 return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); |
| 238 } | 245 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 266 case CSSPrimitiveValue::UnitType::Centimeters: | 273 case CSSPrimitiveValue::UnitType::Centimeters: |
| 267 return value / cssPixelsPerCentimeter; | 274 return value / cssPixelsPerCentimeter; |
| 268 case CSSPrimitiveValue::UnitType::Millimeters: | 275 case CSSPrimitiveValue::UnitType::Millimeters: |
| 269 return value / cssPixelsPerMillimeter; | 276 return value / cssPixelsPerMillimeter; |
| 270 case CSSPrimitiveValue::UnitType::Inches: | 277 case CSSPrimitiveValue::UnitType::Inches: |
| 271 return value / cssPixelsPerInch; | 278 return value / cssPixelsPerInch; |
| 272 case CSSPrimitiveValue::UnitType::Points: | 279 case CSSPrimitiveValue::UnitType::Points: |
| 273 return value / cssPixelsPerPoint; | 280 return value / cssPixelsPerPoint; |
| 274 case CSSPrimitiveValue::UnitType::Picas: | 281 case CSSPrimitiveValue::UnitType::Picas: |
| 275 return value / cssPixelsPerPica; | 282 return value / cssPixelsPerPica; |
| 283 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
| 284 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
| 285 case CSSPrimitiveValue::UnitType::ViewportMin: | |
| 286 case CSSPrimitiveValue::UnitType::ViewportMax: | |
| 287 return convertValueFromUserUnitsToViewportUnits(toUnit, value); | |
| 276 default: | 288 default: |
| 277 break; | 289 break; |
| 278 } | 290 } |
| 279 | 291 |
| 280 ASSERT_NOT_REACHED(); | 292 ASSERT_NOT_REACHED(); |
| 281 return 0; | 293 return 0; |
| 282 } | 294 } |
| 283 | 295 |
| 284 float SVGLengthContext::convertValueFromUserUnitsToCHS(float value) const | 296 float SVGLengthContext::convertValueFromUserUnitsToCHS(float value) const |
| 285 { | 297 { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 { | 334 { |
| 323 const ComputedStyle* style = computedStyleForLengthResolving(m_context); | 335 const ComputedStyle* style = computedStyleForLengthResolving(m_context); |
| 324 if (!style) | 336 if (!style) |
| 325 return 0; | 337 return 0; |
| 326 | 338 |
| 327 // Use of ceil allows a pixel match to the W3Cs expected output of coords-un its-03-b.svg | 339 // 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 | 340 // 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() ); | 341 return value * ceilf(style->fontMetrics().xHeight() / style->effectiveZoom() ); |
| 330 } | 342 } |
| 331 | 343 |
| 344 static inline float viewportLengthPercent(const float widthOrHeight) | |
| 345 { | |
| 346 return widthOrHeight / 100; | |
| 347 } | |
| 348 | |
| 349 static inline float viewportMinPercent(const FloatSize& viewportSize) | |
| 350 { | |
| 351 return std::min(viewportSize.width(), viewportSize.height()) / 100; | |
| 352 } | |
| 353 | |
| 354 static inline float viewportMaxPercent(const FloatSize& viewportSize) | |
| 355 { | |
| 356 return std::max(viewportSize.width(), viewportSize.height()) / 100; | |
| 357 } | |
| 358 | |
| 359 | |
| 360 float SVGLengthContext::convertValueFromUserUnitsToViewportUnits(CSSPrimitiveVal ue::UnitType toUnit, float value) const | |
| 361 { | |
| 362 if (!m_context) | |
| 363 return 0; | |
| 364 | |
| 365 const ComputedStyle* style = computedStyleForLengthResolving(m_context); | |
| 366 if (!style) | |
| 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) { | |
|
fs
2016/01/04 19:40:05
I think this would be slightly if it was using sam
Shanmuga Pandi
2016/01/05 10:04:49
Done.
| |
| 377 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
| 378 return value / viewportLengthPercent(viewportSize.width()) / style->effe ctiveZoom(); | |
| 379 | |
| 380 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
| 381 return value / viewportLengthPercent(viewportSize.height()) / style->eff ectiveZoom(); | |
| 382 | |
| 383 case CSSPrimitiveValue::UnitType::ViewportMin: | |
| 384 return value / viewportMinPercent(viewportSize) / style->effectiveZoom() ; | |
| 385 | |
| 386 case CSSPrimitiveValue::UnitType::ViewportMax: | |
| 387 return value / viewportMaxPercent(viewportSize) / style->effectiveZoom() ; | |
| 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 ComputedStyle* style = computedStyleForLengthResolving(m_context); | |
| 402 if (!style) | |
| 403 return 0; | |
| 404 | |
| 405 const Document& document = m_context->document(); | |
| 406 FrameView* view = document.view(); | |
| 407 if (!view) | |
| 408 return 0; | |
| 409 | |
| 410 FloatSize viewportSize(view->width(), view->height()); | |
| 411 | |
| 412 switch (fromUnit) { | |
| 413 case CSSPrimitiveValue::UnitType::ViewportWidth: | |
| 414 return value * viewportLengthPercent(viewportSize.width()) / style->effe ctiveZoom(); | |
| 415 | |
| 416 case CSSPrimitiveValue::UnitType::ViewportHeight: | |
| 417 return value * viewportLengthPercent(viewportSize.height()) / style->eff ectiveZoom(); | |
| 418 | |
| 419 case CSSPrimitiveValue::UnitType::ViewportMin: | |
| 420 return value * viewportMinPercent(viewportSize) / style->effectiveZoom() ; | |
| 421 | |
| 422 case CSSPrimitiveValue::UnitType::ViewportMax: | |
| 423 return value * viewportMaxPercent(viewportSize) / style->effectiveZoom() ; | |
| 424 default: | |
| 425 break; | |
| 426 } | |
| 427 | |
| 428 ASSERT_NOT_REACHED(); | |
| 429 return 0; | |
| 430 } | |
| 431 | |
| 332 bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const | 432 bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const |
| 333 { | 433 { |
| 334 if (!m_context) | 434 if (!m_context) |
| 335 return false; | 435 return false; |
| 336 | 436 |
| 337 // Root <svg> element lengths are resolved against the top level viewport. | 437 // Root <svg> element lengths are resolved against the top level viewport. |
| 338 if (m_context->isOutermostSVGSVGElement()) { | 438 if (m_context->isOutermostSVGSVGElement()) { |
| 339 viewportSize = toSVGSVGElement(m_context)->currentViewportSize(); | 439 viewportSize = toSVGSVGElement(m_context)->currentViewportSize(); |
| 340 return true; | 440 return true; |
| 341 } | 441 } |
| 342 | 442 |
| 343 // Take size from nearest viewport element. | 443 // Take size from nearest viewport element. |
| 344 SVGElement* viewportElement = m_context->viewportElement(); | 444 SVGElement* viewportElement = m_context->viewportElement(); |
| 345 if (!isSVGSVGElement(viewportElement)) | 445 if (!isSVGSVGElement(viewportElement)) |
| 346 return false; | 446 return false; |
| 347 | 447 |
| 348 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); | 448 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); |
| 349 viewportSize = svg.currentViewBoxRect().size(); | 449 viewportSize = svg.currentViewBoxRect().size(); |
| 350 if (viewportSize.isEmpty()) | 450 if (viewportSize.isEmpty()) |
| 351 viewportSize = svg.currentViewportSize(); | 451 viewportSize = svg.currentViewportSize(); |
| 352 | 452 |
| 353 return true; | 453 return true; |
| 354 } | 454 } |
| 355 | 455 |
| 356 } | 456 } |
| OLD | NEW |