Bug 1311244 Part 3 - Extract the computation of center as ComputeCircleOrEllipseCenter(). draft
authorTing-Yu Lin <tlin@mozilla.com>
Fri, 06 Jan 2017 16:35:53 +0800
changeset 459615 8d32baeb5b482ab073a78ecc70ecb52c1ec8577c
parent 459614 79a63631fd6f1919cb61ab8eaa9c52b7c8eb0c5b
child 459616 7589ebedab47e9a5fe6d5992433cd6f491e93104
push id41270
push userbmo:tlin@mozilla.com
push dateThu, 12 Jan 2017 09:51:31 +0000
bugs1311244
milestone53.0a1
Bug 1311244 Part 3 - Extract the computation of center as ComputeCircleOrEllipseCenter(). MozReview-Commit-ID: A6OTJ9PD43c
layout/base/ShapeUtils.cpp
layout/base/ShapeUtils.h
layout/svg/nsCSSClipPathInstance.cpp
--- a/layout/base/ShapeUtils.cpp
+++ b/layout/base/ShapeUtils.cpp
@@ -3,16 +3,19 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/ShapeUtils.h"
 
 #include <cstdlib>
 
+#include "nsCSSRendering.h"
+#include "nsStyleStruct.h"
+
 namespace mozilla {
 
 nscoord
 ShapeUtils::ComputeShapeRadius(const StyleShapeRadius aType,
                                const nscoord aCenter,
                                const nscoord aPosMin,
                                const nscoord aPosMax)
 {
@@ -25,9 +28,21 @@ ShapeUtils::ComputeShapeRadius(const Sty
       break;
     case StyleShapeRadius::ClosestSide:
       length = dist1 > dist2 ? dist2 : dist1;
       break;
   }
   return length;
 }
 
+nsPoint
+ShapeUtils::ComputeCircleOrEllipseCenter(StyleBasicShape* const aBasicShape,
+                                         const nsRect& aRefBox)
+{
+  nsPoint topLeft, anchor;
+  nsSize size(aRefBox.Size());
+  nsImageRenderer::ComputeObjectAnchorPoint(aBasicShape->GetPosition(),
+                                            size, size,
+                                            &topLeft, &anchor);
+  return nsPoint(anchor.x + aRefBox.x, anchor.y + aRefBox.y);
+}
+
 } // namespace mozilla
--- a/layout/base/ShapeUtils.h
+++ b/layout/base/ShapeUtils.h
@@ -5,17 +5,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_ShapeUtils_h
 #define mozilla_ShapeUtils_h
 
 #include "nsCoord.h"
 #include "nsStyleConsts.h"
 
+struct nsPoint;
+struct nsRect;
+
 namespace mozilla {
+class StyleBasicShape;
 
 // ShapeUtils is a namespace class containing utility functions related to
 // processing basic shapes in the CSS Shapes Module.
 // https://drafts.csswg.org/css-shapes/#basic-shape-functions
 //
 struct ShapeUtils final
 {
   // Compute the length of a keyword <shape-radius>, i.e. closest-side or
@@ -23,13 +27,21 @@ struct ShapeUtils final
   // caller needs to call for both dimensions and combine the result.
   // https://drafts.csswg.org/css-shapes/#typedef-shape-radius.
   //
   // @return The length of the radius in app units.
   static nscoord ComputeShapeRadius(const StyleShapeRadius aType,
                                     const nscoord aCenter,
                                     const nscoord aPosMin,
                                     const nscoord aPosMax);
+
+  // Compute the center of a circle or an ellipse.
+  //
+  // @param aRefBox The reference box of the basic shape.
+  // @return The point of the center.
+  static nsPoint ComputeCircleOrEllipseCenter(
+    StyleBasicShape* const aBasicShape,
+    const nsRect& aRefBox);
 };
 
 } // namespace mozilla
 
 #endif // mozilla_ShapeUtils_h
--- a/layout/svg/nsCSSClipPathInstance.cpp
+++ b/layout/svg/nsCSSClipPathInstance.cpp
@@ -108,22 +108,18 @@ nsCSSClipPathInstance::CreateClipPath(Dr
 already_AddRefed<Path>
 nsCSSClipPathInstance::CreateClipPathCircle(DrawTarget* aDrawTarget,
                                             const nsRect& aRefBox)
 {
   StyleBasicShape* basicShape = mClipPathStyle.GetBasicShape();
 
   RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
 
-  nsPoint topLeft, anchor;
-  nsSize size = nsSize(aRefBox.width, aRefBox.height);
-  nsImageRenderer::ComputeObjectAnchorPoint(basicShape->GetPosition(),
-                                            size, size,
-                                            &topLeft, &anchor);
-  nsPoint center(anchor.x + aRefBox.x, anchor.y + aRefBox.y);
+  nsPoint center =
+    ShapeUtils::ComputeCircleOrEllipseCenter(basicShape, aRefBox);
 
   const nsTArray<nsStyleCoord>& coords = basicShape->Coordinates();
   MOZ_ASSERT(coords.Length() == 1, "wrong number of arguments");
   nscoord r = 0;
   if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
     const auto styleShapeRadius = coords[0].GetEnumValue<StyleShapeRadius>();
     nscoord horizontal =
       ShapeUtils::ComputeShapeRadius(styleShapeRadius, center.x, aRefBox.x,
@@ -158,22 +154,18 @@ nsCSSClipPathInstance::CreateClipPathCir
 already_AddRefed<Path>
 nsCSSClipPathInstance::CreateClipPathEllipse(DrawTarget* aDrawTarget,
                                              const nsRect& aRefBox)
 {
   StyleBasicShape* basicShape = mClipPathStyle.GetBasicShape();
 
   RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
 
-  nsPoint topLeft, anchor;
-  nsSize size = nsSize(aRefBox.width, aRefBox.height);
-  nsImageRenderer::ComputeObjectAnchorPoint(basicShape->GetPosition(),
-                                            size, size,
-                                            &topLeft, &anchor);
-  nsPoint center(anchor.x + aRefBox.x, anchor.y + aRefBox.y);
+  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,