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/. */
7 #ifndef mozilla_ShapeUtils_h
8 #define mozilla_ShapeUtils_h
12 #include "nsStyleConsts.h"
24 // ShapeUtils is a namespace class containing utility functions related to
25 // processing basic shapes in the CSS Shapes Module.
26 // https://drafts.csswg.org/css-shapes/#basic-shape-functions
28 struct ShapeUtils final
{
29 // Compute the length of a keyword <shape-radius>, i.e. closest-side or
30 // farthest-side, for a circle or an ellipse on a single dimension. The
31 // caller needs to call for both dimensions and combine the result.
32 // https://drafts.csswg.org/css-shapes/#typedef-shape-radius.
33 // @return The length of the radius in app units.
34 static nscoord
ComputeShapeRadius(const StyleShapeRadius
& aType
,
35 const nscoord aCenter
,
36 const nscoord aPosMin
,
37 const nscoord aPosMax
);
39 // Compute the position based on |aRefBox|.
40 // @param aRefBox The reference box for the position.
41 // @return The point inside |aRefBox|.
42 static nsPoint
ComputePosition(const StylePosition
&, const nsRect
&);
44 // Compute the center of a circle or an ellipse.
45 // @param aRefBox The reference box of the basic shape.
46 // @return The point of the center.
47 static nsPoint
ComputeCircleOrEllipseCenter(const StyleBasicShape
&,
48 const nsRect
& aRefBox
);
50 // Compute the radius for a circle.
51 // @param aCenter the center of the circle.
52 // @param aRefBox the reference box of the circle.
53 // @return The length of the radius in app units.
54 static nscoord
ComputeCircleRadius(const StyleBasicShape
&,
55 const nsPoint
& aCenter
,
56 const nsRect
& aRefBox
);
58 // Compute the radii for an ellipse.
59 // @param aCenter the center of the ellipse.
60 // @param aRefBox the reference box of the ellipse.
61 // @return The radii of the ellipse in app units. The width and height
62 // represent the x-axis and y-axis radii of the ellipse.
63 static nsSize
ComputeEllipseRadii(const StyleBasicShape
&,
64 const nsPoint
& aCenter
,
65 const nsRect
& aRefBox
);
67 // Compute the rect for an inset. If the inset amount is larger than
68 // aRefBox itself, this will return a rect the same shape as the inverse
69 // rect that would be created by insetting aRefBox by the inset amount.
70 // This process is *not* what is called for by the current spec at
71 // https://drafts.csswg.org/css-shapes-1/#supported-basic-shapes.
72 // The spec currently treats empty shapes, including overly-inset rects, as
73 // defining 'empty float areas' that don't affect layout. However, it is
74 // practically useful to treat empty shapes as having edges for purposes of
75 // affecting layout, and there is growing momentum for the approach we
77 // @param aRefBox the reference box of the inset.
78 // @return The inset rect in app units.
79 static nsRect
ComputeInsetRect(const StyleBasicShape
&, const nsRect
& aRefBox
);
80 static nsRect
ComputeInsetRect(const StyleRect
<LengthPercentage
>& aStyleRect
,
81 const nsRect
& aRefBox
);
83 // Compute the radii for an inset.
84 // @param aRefBox the reference box of the inset.
85 // @param aInsetRect the inset rect in app units.
86 // @param aRadii the returned radii in app units.
87 // @return true if any of the radii is nonzero; false otherwise.
88 static bool ComputeInsetRadii(const StyleBasicShape
&, const nsRect
& aRefBox
,
89 const nsRect
& aInsetRect
, nscoord aRadii
[8]);
91 // Compute the vertices for a polygon.
92 // @param aRefBox the reference box of the polygon.
93 // @return The vertices in app units; the coordinate space is the same
95 static nsTArray
<nsPoint
> ComputePolygonVertices(const StyleBasicShape
&,
96 const nsRect
& aRefBox
);
98 // Compute a gfx::path from a circle.
99 // @param aRefBox the reference box of the circle.
100 // @param aCenter the center point of the circle.
101 // @return The gfx::Path of this circle.
102 static already_AddRefed
<gfx::Path
> BuildCirclePath(const StyleBasicShape
&,
103 const nsRect
& aRefBox
,
104 const nsPoint
& aCenter
,
105 nscoord aAppUnitsPerPixel
,
108 // Compute a gfx::path from an ellipse.
109 // @param aRefBox the reference box of the ellipse.
110 // @param aCenter the center point of the ellipse.
111 // @return The gfx::Path of this ellipse.
112 static already_AddRefed
<gfx::Path
> BuildEllipsePath(const StyleBasicShape
&,
113 const nsRect
& aRefBox
,
114 const nsPoint
& aCenter
,
115 nscoord aAppUnitsPerPixel
,
118 // Compute a gfx::path from a polygon.
119 // @param aRefBox the reference box of the polygon.
120 // @return The gfx::Path of this polygon.
121 static already_AddRefed
<gfx::Path
> BuildPolygonPath(const StyleBasicShape
&,
122 const nsRect
& aRefBox
,
123 nscoord aAppUnitsPerPixel
,
126 // Compute a gfx::path from a StyleBasicShape which is an inset.
127 // @param aRefBox the reference box of the inset.
128 // @return The gfx::Path of this inset.
129 static already_AddRefed
<gfx::Path
> BuildInsetPath(const StyleBasicShape
&,
130 const nsRect
& aRefBox
,
131 nscoord aAppUnitsPerPixel
,
134 // Compute a gfx::path from an inset rect and the round radii.
135 // @param aRadii the radii of the inset. It should be an array with length 8.
136 // If it's nullptr, we don't have the valid radii.
137 // @param aRefBox the reference box of the inset.
138 // @return The gfx::Path of this inset.
140 // Note: we use nsTArray<nscoord> for radii because we use this type in layers
142 static already_AddRefed
<gfx::Path
> BuildInsetPath(const nsRect
& aInsetRect
,
143 const nscoord aRadii
[8],
144 const nsRect
& aRefBox
,
145 nscoord aAppUnitsPerPixel
,
149 } // namespace mozilla
151 #endif // mozilla_ShapeUtils_h