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

Unified Diff: third_party/WebKit/Source/core/svg/SVGLengthContext.cpp

Issue 1544543003: Adding support of viewport units to SVG (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/core/svg/SVGLengthContext.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/svg/SVGLengthContext.cpp
diff --git a/third_party/WebKit/Source/core/svg/SVGLengthContext.cpp b/third_party/WebKit/Source/core/svg/SVGLengthContext.cpp
index f553158421f727b1c1c0d0772fdae44da33a1052..22b768839f3dbc38612b45138d2df0a4bdb956c8 100644
--- a/third_party/WebKit/Source/core/svg/SVGLengthContext.cpp
+++ b/third_party/WebKit/Source/core/svg/SVGLengthContext.cpp
@@ -26,6 +26,7 @@
#include "core/css/CSSHelper.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/dom/NodeComputedStyle.h"
+#include "core/frame/FrameView.h"
#include "core/layout/LayoutObject.h"
#include "core/style/ComputedStyle.h"
#include "core/svg/SVGSVGElement.h"
@@ -101,6 +102,26 @@ static float convertValueFromEMSToUserUnits(const ComputedStyle* style, float va
return value * style->specifiedFontSize();
}
+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.
+{
+ return viewportSize.width() / 100;
+}
+
+static float viewportHeightPercent(const FloatSize& viewportSize)
+{
+ return viewportSize.height() / 100;
+}
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.
+
+static float viewportMinPercent(const FloatSize& viewportSize)
+{
+ return std::min(viewportSize.width(), viewportSize.height()) / 100;
+}
+
+static float viewportMaxPercent(const FloatSize& viewportSize)
+{
+ return std::max(viewportSize.width(), viewportSize.height()) / 100;
+}
+
SVGLengthContext::SVGLengthContext(const SVGElement* context)
: m_context(context)
{
@@ -226,6 +247,12 @@ float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode,
case CSSPrimitiveValue::UnitType::Chs:
userUnits = convertValueFromCHSToUserUnits(value);
break;
+ case CSSPrimitiveValue::UnitType::ViewportWidth:
+ case CSSPrimitiveValue::UnitType::ViewportHeight:
+ case CSSPrimitiveValue::UnitType::ViewportMin:
+ case CSSPrimitiveValue::UnitType::ViewportMax:
+ userUnits = convertValueFromViewportUnitsToUserUnits(fromUnit, value);
+ break;
default:
ASSERT_NOT_REACHED();
break;
@@ -273,6 +300,11 @@ float SVGLengthContext::convertValueFromUserUnits(float value, SVGLengthMode mod
return value / cssPixelsPerPoint;
case CSSPrimitiveValue::UnitType::Picas:
return value / cssPixelsPerPica;
+ case CSSPrimitiveValue::UnitType::ViewportWidth:
+ case CSSPrimitiveValue::UnitType::ViewportHeight:
+ case CSSPrimitiveValue::UnitType::ViewportMin:
+ case CSSPrimitiveValue::UnitType::ViewportMax:
+ return convertValueFromUserUnitsToViewportUnits(toUnit, value);
default:
break;
}
@@ -329,6 +361,70 @@ float SVGLengthContext::convertValueFromEXSToUserUnits(float value) const
return value * ceilf(style->fontMetrics().xHeight() / style->effectiveZoom());
}
+float SVGLengthContext::convertValueFromUserUnitsToViewportUnits(CSSPrimitiveValue::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.
+{
+ if (!m_context)
+ return 0;
+
+ const Document& document = m_context->document();
+ FrameView* view = document.view();
+ if (!view)
+ return 0;
+
+ FloatSize viewportSize(view->width(), view->height());
+
+ switch (toUnit) {
+ case CSSPrimitiveValue::UnitType::ViewportWidth:
+ return value / viewportWidthPercent(viewportSize);
+
+ case CSSPrimitiveValue::UnitType::ViewportHeight:
+ return value / viewportHeightPercent(viewportSize);
+
+ case CSSPrimitiveValue::UnitType::ViewportMin:
+ return value / viewportMinPercent(viewportSize);
+
+ case CSSPrimitiveValue::UnitType::ViewportMax:
+ return value / viewportMaxPercent(viewportSize);
+ default:
+ break;
+ }
+
+ ASSERT_NOT_REACHED();
+ return 0;
+}
+
+float SVGLengthContext::convertValueFromViewportUnitsToUserUnits(CSSPrimitiveValue::UnitType fromUnit, float value) const
+{
+ if (!m_context)
+ return 0;
+
+ const Document& document = m_context->document();
+ FrameView* view = document.view();
+ if (!view)
+ return 0;
+
+ FloatSize viewportSize(view->width(), view->height());
+
+ switch (fromUnit) {
+ case CSSPrimitiveValue::UnitType::ViewportWidth:
+ return value * viewportWidthPercent(viewportSize);
+
+ case CSSPrimitiveValue::UnitType::ViewportHeight:
+ return value * viewportHeightPercent(viewportSize);
+
+ case CSSPrimitiveValue::UnitType::ViewportMin:
+ return value * viewportMinPercent(viewportSize);
+
+ case CSSPrimitiveValue::UnitType::ViewportMax:
+ return value * viewportMaxPercent(viewportSize);
+ default:
+ break;
+ }
+
+ ASSERT_NOT_REACHED();
+ return 0;
+}
+
bool SVGLengthContext::determineViewport(FloatSize& viewportSize) const
{
if (!m_context)
« no previous file with comments | « third_party/WebKit/Source/core/svg/SVGLengthContext.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698