Bug 1529572 - Should resolve absolute units r=longsonr
authorviolet <violet.bugreport@gmail.com>
Sun, 23 Jun 2019 12:51:05 +0000
changeset 479905 268277536047b0724243e48eea3fa1dee11ea6db
parent 479904 0e84cf2872e7002304a700246ae709a8a3a6d9da
child 479906 37f22f5b972fb9f3bf0d6faace13a7d588b3f9f7
push id36192
push userncsoregi@mozilla.com
push dateMon, 24 Jun 2019 15:24:02 +0000
treeherdermozilla-central@51b4af33ce0d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslongsonr
bugs1529572
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1529572 - Should resolve absolute units r=longsonr Firefox only failure. Differential Revision: https://phabricator.services.mozilla.com/D35614
dom/svg/DOMSVGLength.cpp
dom/svg/SVGAnimatedLength.cpp
dom/svg/SVGAnimatedLength.h
testing/web-platform/meta/svg/types/scripted/SVGLength-px.html.ini
testing/web-platform/tests/svg/types/scripted/SVGLength-px.html
--- a/dom/svg/DOMSVGLength.cpp
+++ b/dom/svg/DOMSVGLength.cpp
@@ -208,20 +208,22 @@ float DOMSVGLength::GetValue(ErrorResult
   }
   if (HasOwner()) {
     float value = InternalItem().GetValueInUserUnits(Element(), Axis());
     if (!IsFinite(value)) {
       aRv.Throw(NS_ERROR_FAILURE);
     }
     return value;
   }
-  if (mUnit == SVGLength_Binding::SVG_LENGTHTYPE_NUMBER ||
-      mUnit == SVGLength_Binding::SVG_LENGTHTYPE_PX) {
-    return mValue;
+
+  float unitToPx;
+  if (UserSpaceMetrics::ResolveAbsoluteUnit(mUnit, unitToPx)) {
+    return mValue * unitToPx;
   }
+
   // else [SVGWG issue] Can't convert this length's value to user units
   // ReportToConsole
   aRv.Throw(NS_ERROR_FAILURE);
   return 0.0f;
 }
 
 void DOMSVGLength::SetValue(float aUserUnitValue, ErrorResult& aRv) {
   if (mIsAnimValItem) {
--- a/dom/svg/SVGAnimatedLength.cpp
+++ b/dom/svg/SVGAnimatedLength.cpp
@@ -199,32 +199,50 @@ float SVGAnimatedLength::GetPixelsPerUni
         SVGElementMetrics(static_cast<SVGElement*>(content)), aUnitType);
   }
   return GetPixelsPerUnit(NonSVGFrameUserSpaceMetrics(aFrame), aUnitType);
 }
 
 // See https://www.w3.org/TR/css-values-3/#absolute-lengths
 static const float DPI = 96.0f;
 
-float SVGAnimatedLength::GetPixelsPerUnit(const UserSpaceMetrics& aMetrics,
-                                          uint8_t aUnitType) const {
+bool UserSpaceMetrics::ResolveAbsoluteUnit(uint8_t aUnitType, float& aRes) {
   switch (aUnitType) {
     case SVGLength_Binding::SVG_LENGTHTYPE_NUMBER:
     case SVGLength_Binding::SVG_LENGTHTYPE_PX:
-      return 1;
+      aRes = 1;
+      break;
     case SVGLength_Binding::SVG_LENGTHTYPE_MM:
-      return DPI / MM_PER_INCH_FLOAT;
+      aRes = DPI / MM_PER_INCH_FLOAT;
+      break;
     case SVGLength_Binding::SVG_LENGTHTYPE_CM:
-      return 10.0f * DPI / MM_PER_INCH_FLOAT;
+      aRes = 10.0f * DPI / MM_PER_INCH_FLOAT;
+      break;
     case SVGLength_Binding::SVG_LENGTHTYPE_IN:
-      return DPI;
+      aRes = DPI;
+      break;
     case SVGLength_Binding::SVG_LENGTHTYPE_PT:
-      return DPI / POINTS_PER_INCH_FLOAT;
+      aRes = DPI / POINTS_PER_INCH_FLOAT;
+      break;
     case SVGLength_Binding::SVG_LENGTHTYPE_PC:
-      return 12.0f * DPI / POINTS_PER_INCH_FLOAT;
+      aRes = 12.0f * DPI / POINTS_PER_INCH_FLOAT;
+      break;
+    default:
+      return false;
+  }
+  return true;
+}
+
+float SVGAnimatedLength::GetPixelsPerUnit(const UserSpaceMetrics& aMetrics,
+                                          uint8_t aUnitType) const {
+  float res;
+  if (UserSpaceMetrics::ResolveAbsoluteUnit(aUnitType, res)) {
+    return res;
+  }
+  switch (aUnitType) {
     case SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE:
       return aMetrics.GetAxisLength(mCtxType) / 100.0f;
     case SVGLength_Binding::SVG_LENGTHTYPE_EMS:
       return aMetrics.GetEmLength();
     case SVGLength_Binding::SVG_LENGTHTYPE_EXS:
       return aMetrics.GetExLength();
     default:
       MOZ_ASSERT_UNREACHABLE("Unknown unit type");
--- a/dom/svg/SVGAnimatedLength.h
+++ b/dom/svg/SVGAnimatedLength.h
@@ -28,16 +28,17 @@ class SMILValue;
 namespace dom {
 class DOMSVGAnimatedLength;
 class DOMSVGLength;
 class SVGAnimationElement;
 class SVGViewportElement;
 
 class UserSpaceMetrics {
  public:
+  static bool ResolveAbsoluteUnit(uint8_t aUnitType, float& aRes);
   virtual ~UserSpaceMetrics() = default;
 
   virtual float GetEmLength() const = 0;
   virtual float GetExLength() const = 0;
   virtual float GetAxisLength(uint8_t aCtxType) const = 0;
 };
 
 class UserSpaceMetricsWithSize : public UserSpaceMetrics {
--- a/testing/web-platform/meta/svg/types/scripted/SVGLength-px.html.ini
+++ b/testing/web-platform/meta/svg/types/scripted/SVGLength-px.html.ini
@@ -2,24 +2,8 @@
   [SVGLength, converting from 'px' to other units (detached), percentage]
     expected: FAIL
 
   [SVGLength, converting from 'px' to other units (detached), ems]
     expected: FAIL
 
   [SVGLength, converting from 'px' to other units (detached), exs]
     expected: FAIL
-
-  [SVGLength, converting from 'px' to other units (detached), cm]
-    expected: FAIL
-
-  [SVGLength, converting from 'px' to other units (detached), mm]
-    expected: FAIL
-
-  [SVGLength, converting from 'px' to other units (detached), in]
-    expected: FAIL
-
-  [SVGLength, converting from 'px' to other units (detached), pt]
-    expected: FAIL
-
-  [SVGLength, converting from 'px' to other units (detached), pc]
-    expected: FAIL
-
--- a/testing/web-platform/tests/svg/types/scripted/SVGLength-px.html
+++ b/testing/web-platform/tests/svg/types/scripted/SVGLength-px.html
@@ -67,17 +67,17 @@ test(function() {
 
 test(function() {
   var length = svgElement.createSVGLength();
   length.valueAsString = "48px";
   length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM);
   var referenceValue = 48 * 2.54 / cssPixelsPerInch;
   assert_equals(length.valueAsString, referenceValue.toFixed(2) + "cm");
   assert_approx_equals(length.valueInSpecifiedUnits, referenceValue, 0.01);
-  assert_equals(length.value, 48);
+  assert_approx_equals(length.value, 48, 0.001);
   assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM);
 }, document.title + ", cm");
 
 test(function() {
   var length = svgElement.createSVGLength();
   length.valueAsString = "48px";
   length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM);
   var referenceValue = 48 * 25.4 / cssPixelsPerInch;
@@ -114,9 +114,9 @@ test(function() {
   length.valueAsString = "16px";
   length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC);
   var referenceValue = 16 / cssPixelsPerInch * 6;
   // Don't check valueAsString here, it's unreliable across browsers.
   assert_equals(length.valueInSpecifiedUnits, referenceValue);
   assert_equals(length.value, 16);
   assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC);
 }, document.title + ", pc");
-</script>
\ No newline at end of file
+</script>