ShapeUtils.h (7320B)
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 #include "nsTArray.h" 14 15 struct nsPoint; 16 struct nsRect; 17 18 namespace mozilla { 19 namespace gfx { 20 class Path; 21 class PathBuilder; 22 } // namespace gfx 23 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 27 // 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); 38 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&); 43 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); 49 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); 57 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); 66 67 // Compute the rect for an inset()/xywh()/rect(). 68 // If the inset amount is larger than aRefBox itself, this will return a rect 69 // the same shape as the inverse rect that would be created by insetting 70 // aRefBox by the inset amount. This process is *not* what is called for by 71 // the current spec at 72 // https://drafts.csswg.org/css-shapes-1/#supported-basic-shapes. 73 // 74 // The spec currently treats empty shapes, including overly-inset rects, as 75 // defining 'empty float areas' that don't affect layout. However, it is 76 // practically useful to treat empty shapes as having edges for purposes of 77 // affecting layout, and there is growing momentum for the approach we 78 // are taking here. 79 // @param aRefBox the reference box of the inset/xywh/rect. 80 // @return The inset/xywh/rect rect in app units. 81 static nsRect ComputeInsetRect(const StyleRect<LengthPercentage>& aStyleRect, 82 const nsRect& aRefBox); 83 84 // Compute the radii for a rectanglar shape, i.e. inset()/xywh()/rect(). 85 // @param aRefBox the reference box of the rect. 86 // @param aRect the rect we computed from Compute{Inset}Rect(), in app units. 87 // @param aRadii the returned radii in app units. 88 // @return true if any of the radii is nonzero; false otherwise. 89 static bool ComputeRectRadii(const StyleBorderRadius&, const nsRect& aRefBox, 90 const nsRect& aRect, nsRectCornerRadii&); 91 92 // Compute the vertices for a polygon. 93 // @param aRefBox the reference box of the polygon. 94 // @return The vertices in app units; the coordinate space is the same 95 // as aRefBox. 96 static nsTArray<nsPoint> ComputePolygonVertices(const StyleBasicShape&, 97 const nsRect& aRefBox); 98 99 // Compute a gfx::path from a circle. 100 // @param aRefBox the reference box of the circle. 101 // @param aCenter the center point of the circle. 102 // @return The gfx::Path of this circle. 103 static already_AddRefed<gfx::Path> BuildCirclePath(const StyleBasicShape&, 104 const nsRect& aRefBox, 105 const nsPoint& aCenter, 106 nscoord aAppUnitsPerPixel, 107 gfx::PathBuilder*); 108 109 // Compute a gfx::path from an ellipse. 110 // @param aRefBox the reference box of the ellipse. 111 // @param aCenter the center point of the ellipse. 112 // @return The gfx::Path of this ellipse. 113 static already_AddRefed<gfx::Path> BuildEllipsePath(const StyleBasicShape&, 114 const nsRect& aRefBox, 115 const nsPoint& aCenter, 116 nscoord aAppUnitsPerPixel, 117 gfx::PathBuilder*); 118 119 // Compute a gfx::path from a polygon. 120 // @param aRefBox the reference box of the polygon. 121 // @return The gfx::Path of this polygon. 122 static already_AddRefed<gfx::Path> BuildPolygonPath(const StyleBasicShape&, 123 const nsRect& aRefBox, 124 nscoord aAppUnitsPerPixel, 125 gfx::PathBuilder*); 126 127 // Compute a gfx::path from a StyleBasicShape which is an inset. 128 // @param aRefBox the reference box of the inset. 129 // @return The gfx::Path of this inset. 130 static already_AddRefed<gfx::Path> BuildInsetPath(const StyleBasicShape&, 131 const nsRect& aRefBox, 132 nscoord aAppUnitsPerPixel, 133 gfx::PathBuilder*); 134 135 // Compute a gfx::path from a rectanglar shape (i.e. inset()/xywh()/rect()) 136 // and the round radii. 137 // @param aRect the rect we computed from Compute{Inset}Rect(). 138 // @param aRadii the radii of the rect or null. 139 // @param aRefBox the reference box of the rect. 140 // @return The gfx::Path of this rect. 141 static already_AddRefed<gfx::Path> BuildRectPath(const nsRect& aRect, 142 const nsRectCornerRadii*, 143 const nsRect& aRefBox, 144 nscoord aAppUnitsPerPixel, 145 gfx::PathBuilder*); 146 }; 147 148 } // namespace mozilla 149 150 #endif // mozilla_ShapeUtils_h