1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef GFX_RECT_H
7 #define GFX_RECT_H
8 
9 #include "gfxTypes.h"
10 #include "gfxPoint.h"
11 #include "nsDebug.h"
12 #include "nsRect.h"
13 #include "mozilla/gfx/BaseMargin.h"
14 #include "mozilla/gfx/BaseRect.h"
15 #include "mozilla/gfx/MatrixFwd.h"
16 #include "mozilla/Assertions.h"
17 
18 struct gfxQuad;
19 
20 struct gfxMargin : public mozilla::gfx::BaseMargin<gfxFloat, gfxMargin> {
21   typedef mozilla::gfx::BaseMargin<gfxFloat, gfxMargin> Super;
22 
23   // Constructors
gfxMargingfxMargin24   gfxMargin() : Super() {}
gfxMargingfxMargin25   gfxMargin(const gfxMargin& aMargin) : Super(aMargin) {}
gfxMargingfxMargin26   gfxMargin(gfxFloat aTop, gfxFloat aRight, gfxFloat aBottom, gfxFloat aLeft)
27     : Super(aTop, aRight, aBottom, aLeft) {}
28 };
29 
30 namespace mozilla {
31     namespace css {
32         enum Corner {
33             // this order is important!
34             eCornerTopLeft = 0,
35             eCornerTopRight = 1,
36             eCornerBottomRight = 2,
37             eCornerBottomLeft = 3,
38             eNumCorners = 4
39         };
40     } // namespace css
41 } // namespace mozilla
42 #define NS_CORNER_TOP_LEFT mozilla::css::eCornerTopLeft
43 #define NS_CORNER_TOP_RIGHT mozilla::css::eCornerTopRight
44 #define NS_CORNER_BOTTOM_RIGHT mozilla::css::eCornerBottomRight
45 #define NS_CORNER_BOTTOM_LEFT mozilla::css::eCornerBottomLeft
46 #define NS_NUM_CORNERS mozilla::css::eNumCorners
47 
48 #define NS_FOR_CSS_CORNERS(var_)                         \
49     for (mozilla::css::Corner var_ = NS_CORNER_TOP_LEFT; \
50          var_ <= NS_CORNER_BOTTOM_LEFT;                  \
51          var_++)
52 
53 static inline mozilla::css::Corner operator++(mozilla::css::Corner& corner, int) {
54     NS_PRECONDITION(corner >= NS_CORNER_TOP_LEFT &&
55                     corner < NS_NUM_CORNERS, "Out of range corner");
56     corner = mozilla::css::Corner(corner + 1);
57     return corner;
58 }
59 
60 struct gfxRect :
61     public mozilla::gfx::BaseRect<gfxFloat, gfxRect, gfxPoint, gfxSize, gfxMargin> {
62     typedef mozilla::gfx::BaseRect<gfxFloat, gfxRect, gfxPoint, gfxSize, gfxMargin> Super;
63 
gfxRectgfxRect64     gfxRect() : Super() {}
gfxRectgfxRect65     gfxRect(const gfxPoint& aPos, const gfxSize& aSize) :
66         Super(aPos, aSize) {}
gfxRectgfxRect67     gfxRect(gfxFloat aX, gfxFloat aY, gfxFloat aWidth, gfxFloat aHeight) :
68         Super(aX, aY, aWidth, aHeight) {}
69 
70     /**
71      * Return true if all components of this rect are within
72      * aEpsilon of integer coordinates, defined as
73      *   |round(coord) - coord| <= |aEpsilon|
74      * for x,y,width,height.
75      */
76     bool WithinEpsilonOfIntegerPixels(gfxFloat aEpsilon) const;
77 
AtCornergfxRect78     gfxPoint AtCorner(mozilla::css::Corner corner) const {
79         switch (corner) {
80             case NS_CORNER_TOP_LEFT: return TopLeft();
81             case NS_CORNER_TOP_RIGHT: return TopRight();
82             case NS_CORNER_BOTTOM_RIGHT: return BottomRight();
83             case NS_CORNER_BOTTOM_LEFT: return BottomLeft();
84             default:
85                 NS_ERROR("Invalid corner!");
86                 break;
87         }
88         return gfxPoint(0.0, 0.0);
89     }
90 
CCWCornergfxRect91     gfxPoint CCWCorner(mozilla::Side side) const {
92         switch (side) {
93             case NS_SIDE_TOP: return TopLeft();
94             case NS_SIDE_RIGHT: return TopRight();
95             case NS_SIDE_BOTTOM: return BottomRight();
96             case NS_SIDE_LEFT: return BottomLeft();
97         }
98         MOZ_CRASH("Incomplete switch");
99     }
100 
CWCornergfxRect101     gfxPoint CWCorner(mozilla::Side side) const {
102         switch (side) {
103             case NS_SIDE_TOP: return TopRight();
104             case NS_SIDE_RIGHT: return BottomRight();
105             case NS_SIDE_BOTTOM: return BottomLeft();
106             case NS_SIDE_LEFT: return TopLeft();
107         }
108         MOZ_CRASH("Incomplete switch");
109     }
110 
111     /* Conditions this border to Cairo's max coordinate space.
112      * The caller can check IsEmpty() after Condition() -- if it's TRUE,
113      * the caller can possibly avoid doing any extra rendering.
114      */
115     void Condition();
116 
ScalegfxRect117     void Scale(gfxFloat k) {
118         NS_ASSERTION(k >= 0.0, "Invalid (negative) scale factor");
119         x *= k;
120         y *= k;
121         width *= k;
122         height *= k;
123     }
124 
ScalegfxRect125     void Scale(gfxFloat sx, gfxFloat sy) {
126         NS_ASSERTION(sx >= 0.0, "Invalid (negative) scale factor");
127         NS_ASSERTION(sy >= 0.0, "Invalid (negative) scale factor");
128         x *= sx;
129         y *= sy;
130         width *= sx;
131         height *= sy;
132     }
133 
ScaleInversegfxRect134     void ScaleInverse(gfxFloat k) {
135         NS_ASSERTION(k > 0.0, "Invalid (negative) scale factor");
136         x /= k;
137         y /= k;
138         width /= k;
139         height /= k;
140     }
141 
142     /*
143      * Transform this rectangle with aMatrix, resulting in a gfxQuad.
144      */
145     gfxQuad TransformToQuad(const mozilla::gfx::Matrix4x4 &aMatrix) const;
146 };
147 
148 #endif /* GFX_RECT_H */
149