1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Oracle Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Oracle Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2005
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Stuart Parmenter <pavlov@pavlov.net>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef GFX_RECT_H
39 : #define GFX_RECT_H
40 :
41 : #include "nsAlgorithm.h"
42 : #include "gfxTypes.h"
43 : #include "gfxPoint.h"
44 : #include "gfxCore.h"
45 : #include "nsDebug.h"
46 : #include "mozilla/gfx/BaseMargin.h"
47 : #include "mozilla/gfx/BaseRect.h"
48 : #include "mozilla/Assertions.h"
49 : #include "nsRect.h"
50 :
51 : struct gfxMargin : public mozilla::gfx::BaseMargin<gfxFloat, gfxMargin> {
52 : typedef mozilla::gfx::BaseMargin<gfxFloat, gfxMargin> Super;
53 :
54 : // Constructors
55 : gfxMargin() : Super() {}
56 : gfxMargin(const gfxMargin& aMargin) : Super(aMargin) {}
57 0 : gfxMargin(gfxFloat aLeft, gfxFloat aTop, gfxFloat aRight, gfxFloat aBottom)
58 0 : : Super(aLeft, aTop, aRight, aBottom) {}
59 : };
60 :
61 : namespace mozilla {
62 : namespace css {
63 : enum Corner {
64 : // this order is important!
65 : eCornerTopLeft = 0,
66 : eCornerTopRight = 1,
67 : eCornerBottomRight = 2,
68 : eCornerBottomLeft = 3,
69 : eNumCorners = 4
70 : };
71 : }
72 : }
73 : #define NS_CORNER_TOP_LEFT mozilla::css::eCornerTopLeft
74 : #define NS_CORNER_TOP_RIGHT mozilla::css::eCornerTopRight
75 : #define NS_CORNER_BOTTOM_RIGHT mozilla::css::eCornerBottomRight
76 : #define NS_CORNER_BOTTOM_LEFT mozilla::css::eCornerBottomLeft
77 : #define NS_NUM_CORNERS mozilla::css::eNumCorners
78 :
79 : #define NS_FOR_CSS_CORNERS(var_) \
80 : for (mozilla::css::Corner var_ = NS_CORNER_TOP_LEFT; \
81 : var_ <= NS_CORNER_BOTTOM_LEFT; \
82 : var_++)
83 :
84 0 : static inline mozilla::css::Corner operator++(mozilla::css::Corner& corner, int) {
85 0 : NS_PRECONDITION(corner >= NS_CORNER_TOP_LEFT &&
86 : corner < NS_NUM_CORNERS, "Out of range corner");
87 0 : corner = mozilla::css::Corner(corner + 1);
88 0 : return corner;
89 : }
90 :
91 : struct THEBES_API gfxRect :
92 : public mozilla::gfx::BaseRect<gfxFloat, gfxRect, gfxPoint, gfxSize, gfxMargin> {
93 : typedef mozilla::gfx::BaseRect<gfxFloat, gfxRect, gfxPoint, gfxSize, gfxMargin> Super;
94 :
95 0 : gfxRect() : Super() {}
96 0 : gfxRect(const gfxPoint& aPos, const gfxSize& aSize) :
97 0 : Super(aPos, aSize) {}
98 21 : gfxRect(gfxFloat aX, gfxFloat aY, gfxFloat aWidth, gfxFloat aHeight) :
99 21 : Super(aX, aY, aWidth, aHeight) {}
100 0 : gfxRect(const nsIntRect& aRect) :
101 0 : Super(aRect.x, aRect.y, aRect.width, aRect.height) {}
102 :
103 : /**
104 : * Return true if all components of this rect are within
105 : * aEpsilon of integer coordinates, defined as
106 : * |round(coord) - coord| <= |aEpsilon|
107 : * for x,y,width,height.
108 : */
109 : bool WithinEpsilonOfIntegerPixels(gfxFloat aEpsilon) const;
110 :
111 0 : gfxPoint AtCorner(mozilla::css::Corner corner) const {
112 0 : switch (corner) {
113 0 : case NS_CORNER_TOP_LEFT: return TopLeft();
114 0 : case NS_CORNER_TOP_RIGHT: return TopRight();
115 0 : case NS_CORNER_BOTTOM_RIGHT: return BottomRight();
116 0 : case NS_CORNER_BOTTOM_LEFT: return BottomLeft();
117 : default:
118 0 : NS_ERROR("Invalid corner!");
119 : break;
120 : }
121 0 : return gfxPoint(0.0, 0.0);
122 : }
123 :
124 0 : gfxPoint CCWCorner(mozilla::css::Side side) const {
125 0 : switch (side) {
126 0 : case NS_SIDE_TOP: return TopLeft();
127 0 : case NS_SIDE_RIGHT: return TopRight();
128 0 : case NS_SIDE_BOTTOM: return BottomRight();
129 0 : case NS_SIDE_LEFT: return BottomLeft();
130 : }
131 : MOZ_NOT_REACHED("Incomplet switch");
132 : }
133 :
134 0 : gfxPoint CWCorner(mozilla::css::Side side) const {
135 0 : switch (side) {
136 0 : case NS_SIDE_TOP: return TopRight();
137 0 : case NS_SIDE_RIGHT: return BottomRight();
138 0 : case NS_SIDE_BOTTOM: return BottomLeft();
139 0 : case NS_SIDE_LEFT: return TopLeft();
140 : }
141 : MOZ_NOT_REACHED("Incomplet switch");
142 : }
143 :
144 : /* Conditions this border to Cairo's max coordinate space.
145 : * The caller can check IsEmpty() after Condition() -- if it's TRUE,
146 : * the caller can possibly avoid doing any extra rendering.
147 : */
148 : void Condition();
149 :
150 0 : void Scale(gfxFloat k) {
151 0 : NS_ASSERTION(k >= 0.0, "Invalid (negative) scale factor");
152 0 : x *= k;
153 0 : y *= k;
154 0 : width *= k;
155 0 : height *= k;
156 0 : }
157 :
158 0 : void Scale(gfxFloat sx, gfxFloat sy) {
159 0 : NS_ASSERTION(sx >= 0.0, "Invalid (negative) scale factor");
160 0 : NS_ASSERTION(sy >= 0.0, "Invalid (negative) scale factor");
161 0 : x *= sx;
162 0 : y *= sy;
163 0 : width *= sx;
164 0 : height *= sy;
165 0 : }
166 :
167 0 : void ScaleInverse(gfxFloat k) {
168 0 : NS_ASSERTION(k > 0.0, "Invalid (negative) scale factor");
169 0 : x /= k;
170 0 : y /= k;
171 0 : width /= k;
172 0 : height /= k;
173 0 : }
174 : };
175 :
176 : struct THEBES_API gfxCornerSizes {
177 : gfxSize sizes[NS_NUM_CORNERS];
178 :
179 0 : gfxCornerSizes () { }
180 :
181 0 : gfxCornerSizes (gfxFloat v) {
182 0 : for (int i = 0; i < NS_NUM_CORNERS; i++)
183 0 : sizes[i].SizeTo(v, v);
184 0 : }
185 :
186 : gfxCornerSizes (gfxFloat tl, gfxFloat tr, gfxFloat br, gfxFloat bl) {
187 : sizes[NS_CORNER_TOP_LEFT].SizeTo(tl, tl);
188 : sizes[NS_CORNER_TOP_RIGHT].SizeTo(tr, tr);
189 : sizes[NS_CORNER_BOTTOM_RIGHT].SizeTo(br, br);
190 : sizes[NS_CORNER_BOTTOM_LEFT].SizeTo(bl, bl);
191 : }
192 :
193 0 : gfxCornerSizes (const gfxSize& tl, const gfxSize& tr, const gfxSize& br, const gfxSize& bl) {
194 0 : sizes[NS_CORNER_TOP_LEFT] = tl;
195 0 : sizes[NS_CORNER_TOP_RIGHT] = tr;
196 0 : sizes[NS_CORNER_BOTTOM_RIGHT] = br;
197 0 : sizes[NS_CORNER_BOTTOM_LEFT] = bl;
198 0 : }
199 :
200 0 : const gfxSize& operator[] (mozilla::css::Corner index) const {
201 0 : return sizes[index];
202 : }
203 :
204 0 : gfxSize& operator[] (mozilla::css::Corner index) {
205 0 : return sizes[index];
206 : }
207 :
208 : const gfxSize TopLeft() const { return sizes[NS_CORNER_TOP_LEFT]; }
209 : gfxSize& TopLeft() { return sizes[NS_CORNER_TOP_LEFT]; }
210 :
211 : const gfxSize TopRight() const { return sizes[NS_CORNER_TOP_RIGHT]; }
212 : gfxSize& TopRight() { return sizes[NS_CORNER_TOP_RIGHT]; }
213 :
214 : const gfxSize BottomLeft() const { return sizes[NS_CORNER_BOTTOM_LEFT]; }
215 : gfxSize& BottomLeft() { return sizes[NS_CORNER_BOTTOM_LEFT]; }
216 :
217 : const gfxSize BottomRight() const { return sizes[NS_CORNER_BOTTOM_RIGHT]; }
218 : gfxSize& BottomRight() { return sizes[NS_CORNER_BOTTOM_RIGHT]; }
219 : };
220 : #endif /* GFX_RECT_H */
|