1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_ShapeUtils_h
8 #define mozilla_ShapeUtils_h
9 
10 #include "nsCoord.h"
11 #include "nsSize.h"
12 #include "nsStyleConsts.h"
13 
14 struct nsPoint;
15 struct nsRect;
16 
17 namespace mozilla {
18 class StyleBasicShape;
19 
20 // ShapeUtils is a namespace class containing utility functions related to
21 // processing basic shapes in the CSS Shapes Module.
22 // https://drafts.csswg.org/css-shapes/#basic-shape-functions
23 //
24 struct ShapeUtils final {
25   // Compute the length of a keyword <shape-radius>, i.e. closest-side or
26   // farthest-side, for a circle or an ellipse on a single dimension. The
27   // caller needs to call for both dimensions and combine the result.
28   // https://drafts.csswg.org/css-shapes/#typedef-shape-radius.
29   // @return The length of the radius in app units.
30   static nscoord ComputeShapeRadius(const StyleShapeRadius aType,
31                                     const nscoord aCenter,
32                                     const nscoord aPosMin,
33                                     const nscoord aPosMax);
34 
35   // Compute the center of a circle or an ellipse.
36   // @param aRefBox The reference box of the basic shape.
37   // @return The point of the center.
38   static nsPoint ComputeCircleOrEllipseCenter(
39       const UniquePtr<StyleBasicShape>& aBasicShape, const nsRect& aRefBox);
40 
41   // Compute the radius for a circle.
42   // @param aCenter the center of the circle.
43   // @param aRefBox the reference box of the circle.
44   // @return The length of the radius in app units.
45   static nscoord ComputeCircleRadius(
46       const UniquePtr<StyleBasicShape>& aBasicShape, const nsPoint& aCenter,
47       const nsRect& aRefBox);
48 
49   // Compute the radii for an ellipse.
50   // @param aCenter the center of the ellipse.
51   // @param aRefBox the reference box of the ellipse.
52   // @return The radii of the ellipse in app units. The width and height
53   // represent the x-axis and y-axis radii of the ellipse.
54   static nsSize ComputeEllipseRadii(
55       const UniquePtr<StyleBasicShape>& aBasicShape, const nsPoint& aCenter,
56       const nsRect& aRefBox);
57 
58   // Compute the rect for an inset.
59   // @param aRefBox the reference box of the inset.
60   // @return The inset rect in app units.
61   static nsRect ComputeInsetRect(const UniquePtr<StyleBasicShape>& aBasicShape,
62                                  const nsRect& aRefBox);
63 
64   // Compute the radii for an inset.
65   // @param aRefBox the reference box of the inset.
66   // @param aInsetRect the inset rect computed by ComputeInsetRect().
67   // @param aRadii the returned radii in app units.
68   // @return true if any of the radii is nonzero; false otherwise.
69   static bool ComputeInsetRadii(const UniquePtr<StyleBasicShape>& aBasicShape,
70                                 const nsRect& aInsetRect, const nsRect& aRefBox,
71                                 nscoord aRadii[8]);
72 
73   // Compute the vertices for a polygon.
74   // @param aRefBox the reference box of the polygon.
75   // @return The vertices in app units; the coordinate space is the same
76   //         as aRefBox.
77   static nsTArray<nsPoint> ComputePolygonVertices(
78       const UniquePtr<StyleBasicShape>& aBasicShape, const nsRect& aRefBox);
79 };
80 
81 }  // namespace mozilla
82 
83 #endif  // mozilla_ShapeUtils_h
84