Bug 1326406 Part 2 - Extract the computation of ellipse radii as ComputeEllipseRadii(). draft
authorTing-Yu Lin <tlin@mozilla.com>
Mon, 23 Jan 2017 17:17:05 +0800
changeset 466053 0e14d70df907ad85c6b2cc5b657ebba095487bfa
parent 466052 93c64020ea87a8ac939ef6c521836160a305e3e8
child 466054 88d7ca10f1d303193a352cd385a61858dc584854
push id42771
push userbmo:tlin@mozilla.com
push dateWed, 25 Jan 2017 05:31:10 +0000
bugs1326406
milestone54.0a1
Bug 1326406 Part 2 - Extract the computation of ellipse radii as ComputeEllipseRadii(). MozReview-Commit-ID: LPdvg66adHQ
layout/base/ShapeUtils.cpp
layout/base/ShapeUtils.h
layout/svg/nsCSSClipPathInstance.cpp
--- a/layout/base/ShapeUtils.cpp
+++ b/layout/base/ShapeUtils.cpp
@@ -72,9 +72,37 @@ ShapeUtils::ComputeCircleRadius(StyleBas
       SVGContentUtils::ComputeNormalizedHypotenuse(aRefBox.width,
                                                    aRefBox.height);
     r = nsRuleNode::ComputeCoordPercentCalc(coords[0],
                                             NSToCoordRound(referenceLength));
   }
   return r;
 }
 
+nsSize
+ShapeUtils::ComputeEllipseRadii(StyleBasicShape* const aBasicShape,
+                                const nsPoint& aCenter,
+                                const nsRect& aRefBox)
+{
+  const nsTArray<nsStyleCoord>& coords = aBasicShape->Coordinates();
+  MOZ_ASSERT(coords.Length() == 2, "wrong number of arguments");
+  nsSize radii;
+
+  if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
+    const StyleShapeRadius radiusX = coords[0].GetEnumValue<StyleShapeRadius>();
+    radii.width = ComputeShapeRadius(radiusX, aCenter.x, aRefBox.x,
+                                     aRefBox.XMost());
+  } else {
+    radii.width = nsRuleNode::ComputeCoordPercentCalc(coords[0], aRefBox.width);
+  }
+
+  if (coords[1].GetUnit() == eStyleUnit_Enumerated) {
+    const StyleShapeRadius radiusY = coords[1].GetEnumValue<StyleShapeRadius>();
+    radii.height = ComputeShapeRadius(radiusY, aCenter.y, aRefBox.y,
+                                      aRefBox.YMost());
+  } else {
+    radii.height = nsRuleNode::ComputeCoordPercentCalc(coords[1], aRefBox.height);
+  }
+
+  return radii;
+}
+
 } // namespace mozilla
--- a/layout/base/ShapeUtils.h
+++ b/layout/base/ShapeUtils.h
@@ -43,13 +43,22 @@ struct ShapeUtils final
 
   // Compute the radius for a circle.
   // @param aCenter the center of the circle.
   // @param aRefBox the reference box of the circle.
   // @return The length of the radius in app units.
   static nscoord ComputeCircleRadius(
     mozilla::StyleBasicShape* const aBasicShape,
     const nsPoint& aCenter, const nsRect& aRefBox);
+
+  // Compute the radii for an ellipse.
+  // @param aCenter the center of the ellipse.
+  // @param aRefBox the reference box of the ellipse.
+  // @return The radii of the ellipse in app units. The width and height
+  // represent the x-axis and y-axis radii of the ellipse.
+  static nsSize ComputeEllipseRadii(
+    mozilla::StyleBasicShape* const aBasicShape,
+    const nsPoint& aCenter, const nsRect& aRefBox);
 };
 
 } // namespace mozilla
 
 #endif // mozilla_ShapeUtils_h
--- a/layout/svg/nsCSSClipPathInstance.cpp
+++ b/layout/svg/nsCSSClipPathInstance.cpp
@@ -130,42 +130,22 @@ nsCSSClipPathInstance::CreateClipPathEll
                                              const nsRect& aRefBox)
 {
   StyleBasicShape* basicShape = mClipPathStyle.GetBasicShape();
 
   RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
 
   nsPoint center =
     ShapeUtils::ComputeCircleOrEllipseCenter(basicShape, aRefBox);
-
-  const nsTArray<nsStyleCoord>& coords = basicShape->Coordinates();
-  MOZ_ASSERT(coords.Length() == 2, "wrong number of arguments");
-  nscoord rx = 0, ry = 0;
-  if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
-    rx = ShapeUtils::ComputeShapeRadius(coords[0].GetEnumValue<StyleShapeRadius>(),
-                                        center.x,
-                                        aRefBox.x,
-                                        aRefBox.x + aRefBox.width);
-  } else {
-    rx = nsRuleNode::ComputeCoordPercentCalc(coords[0], aRefBox.width);
-  }
-  if (coords[1].GetUnit() == eStyleUnit_Enumerated) {
-    ry = ShapeUtils::ComputeShapeRadius(coords[1].GetEnumValue<StyleShapeRadius>(),
-                                        center.y,
-                                        aRefBox.y,
-                                        aRefBox.y + aRefBox.height);
-  } else {
-    ry = nsRuleNode::ComputeCoordPercentCalc(coords[1], aRefBox.height);
-  }
-
+  nsSize radii = ShapeUtils::ComputeEllipseRadii(basicShape, center, aRefBox);
   nscoord appUnitsPerDevPixel =
     mTargetFrame->PresContext()->AppUnitsPerDevPixel();
   EllipseToBezier(builder.get(),
                   Point(center.x, center.y) / appUnitsPerDevPixel,
-                  Size(rx, ry) / appUnitsPerDevPixel);
+                  Size(radii.width, radii.height) / appUnitsPerDevPixel);
   builder->Close();
   return builder->Finish();
 }
 
 already_AddRefed<Path>
 nsCSSClipPathInstance::CreateClipPathPolygon(DrawTarget* aDrawTarget,
                                              const nsRect& aRefBox)
 {