Bug 1311244 Part 2 - Create ShapeUtils, and move EnumerationToLength into it. draft
authorTing-Yu Lin <tlin@mozilla.com>
Fri, 06 Jan 2017 16:35:29 +0800
changeset 459614 79a63631fd6f1919cb61ab8eaa9c52b7c8eb0c5b
parent 459613 830376fb7287af2379f9a7f1b760fb1dbfeee484
child 459615 8d32baeb5b482ab073a78ecc70ecb52c1ec8577c
push id41270
push userbmo:tlin@mozilla.com
push dateThu, 12 Jan 2017 09:51:31 +0000
bugs1311244
milestone53.0a1
Bug 1311244 Part 2 - Create ShapeUtils, and move EnumerationToLength into it. More functions in nsCSSClipPathInstance will be refactored and moved into ShapeUtils in subsequent patches. MozReview-Commit-ID: LmJUevY8YGr
layout/base/ShapeUtils.cpp
layout/base/ShapeUtils.h
layout/base/moz.build
layout/svg/nsCSSClipPathInstance.cpp
new file mode 100644
--- /dev/null
+++ b/layout/base/ShapeUtils.cpp
@@ -0,0 +1,33 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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>
+
+namespace mozilla {
+
+nscoord
+ShapeUtils::ComputeShapeRadius(const StyleShapeRadius aType,
+                               const nscoord aCenter,
+                               const nscoord aPosMin,
+                               const nscoord aPosMax)
+{
+  nscoord dist1 = std::abs(aPosMin - aCenter);
+  nscoord dist2 = std::abs(aPosMax - aCenter);
+  nscoord length = 0;
+  switch (aType) {
+    case StyleShapeRadius::FarthestSide:
+      length = dist1 > dist2 ? dist1 : dist2;
+      break;
+    case StyleShapeRadius::ClosestSide:
+      length = dist1 > dist2 ? dist2 : dist1;
+      break;
+  }
+  return length;
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/layout/base/ShapeUtils.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#ifndef mozilla_ShapeUtils_h
+#define mozilla_ShapeUtils_h
+
+#include "nsCoord.h"
+#include "nsStyleConsts.h"
+
+namespace mozilla {
+
+// 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
+  // farthest-side, for a circle or an ellipse on a single dimension. The
+  // 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);
+};
+
+} // namespace mozilla
+
+#endif // mozilla_ShapeUtils_h
--- a/layout/base/moz.build
+++ b/layout/base/moz.build
@@ -75,16 +75,17 @@ EXPORTS.mozilla += [
     'PresShell.h',
     'RestyleLogging.h',
     'RestyleManager.h',
     'RestyleManagerBase.h',
     'RestyleManagerHandle.h',
     'RestyleManagerHandleInlines.h',
     'ServoRestyleManager.h',
     'ServoRestyleManagerInlines.h',
+    'ShapeUtils.h',
     'StaticPresData.h',
 ]
 
 UNIFIED_SOURCES += [
     'AccessibleCaret.cpp',
     'AccessibleCaretEventHub.cpp',
     'AccessibleCaretManager.cpp',
     'FramePropertyTable.cpp',
@@ -109,16 +110,17 @@ UNIFIED_SOURCES += [
     'nsStyleSheetService.cpp',
     'PositionedEventTargeting.cpp',
     'PresShell.cpp',
     'RestyleManager.cpp',
     'RestyleManagerBase.cpp',
     'RestyleTracker.cpp',
     'ScrollbarStyles.cpp',
     'ServoRestyleManager.cpp',
+    'ShapeUtils.cpp',
     'StackArena.cpp',
     'StaticPresData.cpp',
     'TouchManager.cpp',
     'ZoomConstraintsClient.cpp',
 ]
 
 if CONFIG['ENABLE_INTL_API']:
     EXPORTS += [
--- a/layout/svg/nsCSSClipPathInstance.cpp
+++ b/layout/svg/nsCSSClipPathInstance.cpp
@@ -6,16 +6,17 @@
 // Main header first:
 #include "nsCSSClipPathInstance.h"
 
 #include "gfx2DGlue.h"
 #include "gfxPlatform.h"
 #include "mozilla/dom/SVGSVGElement.h"
 #include "mozilla/gfx/2D.h"
 #include "mozilla/gfx/PathHelpers.h"
+#include "mozilla/ShapeUtils.h"
 #include "nsCSSRendering.h"
 #include "nsIFrame.h"
 #include "nsRenderingContext.h"
 #include "nsRuleNode.h"
 #include "nsSVGElement.h"
 #include "nsSVGUtils.h"
 #include "nsSVGViewBox.h"
 
@@ -99,32 +100,16 @@ nsCSSClipPathInstance::CreateClipPath(Dr
     default:
       MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Unexpected shape type");
   }
   // Return an empty Path:
   RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
   return builder->Finish();
 }
 
-static void
-EnumerationToLength(nscoord& aCoord, StyleShapeRadius aType,
-                    nscoord aCenter, nscoord aPosMin, nscoord aPosMax)
-{
-  nscoord dist1 = abs(aPosMin - aCenter);
-  nscoord dist2 = abs(aPosMax - aCenter);
-  switch (aType) {
-    case StyleShapeRadius::FarthestSide:
-      aCoord = dist1 > dist2 ? dist1 : dist2;
-      break;
-    case StyleShapeRadius::ClosestSide:
-      aCoord = dist1 > dist2 ? dist2 : dist1;
-      break;
-  }
-}
-
 already_AddRefed<Path>
 nsCSSClipPathInstance::CreateClipPathCircle(DrawTarget* aDrawTarget,
                                             const nsRect& aRefBox)
 {
   StyleBasicShape* basicShape = mClipPathStyle.GetBasicShape();
 
   RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
 
@@ -135,21 +120,22 @@ nsCSSClipPathInstance::CreateClipPathCir
                                             &topLeft, &anchor);
   nsPoint center(anchor.x + aRefBox.x, anchor.y + aRefBox.y);
 
   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, vertical;
-    EnumerationToLength(horizontal, styleShapeRadius,
-                        center.x, aRefBox.x, aRefBox.x + aRefBox.width);
-    EnumerationToLength(vertical, styleShapeRadius,
-                        center.y, aRefBox.y, aRefBox.y + aRefBox.height);
+    nscoord horizontal =
+      ShapeUtils::ComputeShapeRadius(styleShapeRadius, center.x, aRefBox.x,
+                                     aRefBox.x + aRefBox.width);
+    nscoord vertical =
+      ShapeUtils::ComputeShapeRadius(styleShapeRadius, center.y, aRefBox.y,
+                                     aRefBox.y + aRefBox.height);
     if (styleShapeRadius == StyleShapeRadius::FarthestSide) {
       r = horizontal > vertical ? horizontal : vertical;
     } else {
       r = horizontal < vertical ? horizontal : vertical;
     }
   } else {
     // We resolve percent <shape-radius> value for circle() as defined here:
     // https://drafts.csswg.org/css-shapes/#funcdef-circle
@@ -183,24 +169,28 @@ nsCSSClipPathInstance::CreateClipPathEll
                                             size, size,
                                             &topLeft, &anchor);
   nsPoint center(anchor.x + aRefBox.x, anchor.y + aRefBox.y);
 
   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) {
-    EnumerationToLength(rx, coords[0].GetEnumValue<StyleShapeRadius>(),
-                        center.x, aRefBox.x, aRefBox.x + aRefBox.width);
+    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) {
-    EnumerationToLength(ry, coords[1].GetEnumValue<StyleShapeRadius>(),
-                        center.y, aRefBox.y, aRefBox.y + aRefBox.height);
+    ry = ShapeUtils::ComputeShapeRadius(coords[1].GetEnumValue<StyleShapeRadius>(),
+                                        center.y,
+                                        aRefBox.y,
+                                        aRefBox.y + aRefBox.height);
   } else {
     ry = nsRuleNode::ComputeCoordPercentCalc(coords[1], aRefBox.height);
   }
 
   nscoord appUnitsPerDevPixel =
     mTargetFrame->PresContext()->AppUnitsPerDevPixel();
   EllipseToBezier(builder.get(),
                   Point(center.x, center.y) / appUnitsPerDevPixel,