1/***********************************************************
2
3Copyright 1987, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25
26Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27
28                        All Rights Reserved
29
30Permission to use, copy, modify, and distribute this software and its
31documentation for any purpose and without fee is hereby granted,
32provided that the above copyright notice appear in all copies and that
33both that copyright notice and this permission notice appear in
34supporting documentation, and that the name of Digital not be
35used in advertising or publicity pertaining to distribution of the
36software without specific, written prior permission.
37
38DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44SOFTWARE.
45
46******************************************************************/
47
48#ifndef REGIONSTRUCT_H
49#define REGIONSTRUCT_H
50
51typedef struct pixman_region16 RegionRec, *RegionPtr;
52
53#include "miscstruct.h"
54
55/* Return values from RectIn() */
56
57#define rgnOUT 0
58#define rgnIN  1
59#define rgnPART 2
60
61#define NullRegion ((RegionPtr)0)
62
63/*
64 *   clip region
65 */
66
67typedef struct pixman_region16_data RegDataRec, *RegDataPtr;
68
69extern _X_EXPORT BoxRec RegionEmptyBox;
70extern _X_EXPORT RegDataRec RegionEmptyData;
71extern _X_EXPORT RegDataRec RegionBrokenData;
72static inline Bool RegionNil(RegionPtr reg) {
73    return ((reg)->data && !(reg)->data->numRects);
74}
75
76/* not a region */
77
78static inline Bool RegionNar(RegionPtr reg) {
79    return ((reg)->data == &RegionBrokenData);
80}
81
82static inline int RegionNumRects(RegionPtr reg) {
83    return ((reg)->data ? (reg)->data->numRects : 1);
84}
85
86static inline int RegionSize(RegionPtr reg) {
87    return ((reg)->data ? (reg)->data->size : 0);
88}
89
90static inline BoxPtr RegionRects(RegionPtr reg) {
91    return ((reg)->data ? (BoxPtr)((reg)->data + 1) : &(reg)->extents);
92}
93
94static inline BoxPtr RegionBoxptr(RegionPtr reg) {
95    return ((BoxPtr)((reg)->data + 1));
96}
97
98static inline BoxPtr RegionBox(RegionPtr reg, int i) {
99    return (&RegionBoxptr(reg)[i]);
100}
101
102static inline BoxPtr RegionTop(RegionPtr reg) {
103    return RegionBox(reg, (reg)->data->numRects);
104}
105
106static inline BoxPtr RegionEnd(RegionPtr reg) {
107    return RegionBox(reg, (reg)->data->numRects - 1);
108}
109
110static inline size_t RegionSizeof(int n) {
111    if ((size_t)n < ((INT_MAX - sizeof(RegDataRec)) / sizeof(BoxRec)))
112        return (sizeof(RegDataRec) + ((n) * sizeof(BoxRec)));
113    else
114        return 0;
115}
116
117static inline void RegionInit(RegionPtr _pReg, BoxPtr _rect, int _size)
118{
119    if ((_rect) != NULL)
120    {
121        (_pReg)->extents = *(_rect);
122        (_pReg)->data = (RegDataPtr)NULL;
123    }
124    else
125    {
126        size_t rgnSize;
127        (_pReg)->extents = RegionEmptyBox;
128        if (((_size) > 1) && ((rgnSize = RegionSizeof(_size)) > 0) &&
129            (((_pReg)->data = malloc(rgnSize)) != NULL)) {
130            (_pReg)->data->size = (_size);
131            (_pReg)->data->numRects = 0;
132        }
133        else
134            (_pReg)->data = &RegionEmptyData;
135    }
136}
137
138static inline Bool RegionInitBoxes(RegionPtr pReg, BoxPtr boxes, int nBoxes)
139{
140    return pixman_region_init_rects (pReg, boxes, nBoxes);
141}
142
143static inline void RegionUninit(RegionPtr _pReg)
144{
145    if ((_pReg)->data && (_pReg)->data->size) {
146	free((_pReg)->data);
147	(_pReg)->data = NULL;
148    }
149}
150
151static inline void RegionReset(RegionPtr _pReg, BoxPtr _pBox)
152{
153    (_pReg)->extents = *(_pBox);
154    RegionUninit(_pReg);
155    (_pReg)->data = (RegDataPtr)NULL;
156}
157
158static inline Bool RegionNotEmpty(RegionPtr _pReg) {
159    return !RegionNil(_pReg);
160}
161
162static inline Bool RegionBroken(RegionPtr _pReg) {
163    return RegionNar(_pReg);
164}
165
166static inline void RegionEmpty(RegionPtr _pReg)
167{
168    RegionUninit(_pReg);
169    (_pReg)->extents.x2 = (_pReg)->extents.x1;
170    (_pReg)->extents.y2 = (_pReg)->extents.y1;
171    (_pReg)->data = &RegionEmptyData;
172}
173
174static inline BoxPtr RegionExtents(RegionPtr _pReg)
175{
176    return (&(_pReg)->extents);
177}
178
179static inline void RegionNull(RegionPtr _pReg)
180{
181    (_pReg)->extents = RegionEmptyBox;
182    (_pReg)->data = &RegionEmptyData;
183}
184
185extern _X_EXPORT void InitRegions(void);
186
187extern _X_EXPORT RegionPtr RegionCreate(
188    BoxPtr /*rect*/,
189    int /*size*/);
190
191extern _X_EXPORT void RegionDestroy(
192    RegionPtr /*pReg*/);
193
194static inline Bool
195RegionCopy(RegionPtr dst, RegionPtr src)
196{
197    return pixman_region_copy (dst, src);
198}
199
200static inline Bool
201RegionIntersect(
202    RegionPtr	newReg,     /* destination Region */
203    RegionPtr	reg1,
204    RegionPtr	reg2        /* source regions     */
205    )
206{
207    return pixman_region_intersect (newReg, reg1, reg2);
208}
209
210static inline Bool
211RegionUnion(
212    RegionPtr	newReg,          /* destination Region */
213    RegionPtr	reg1,
214    RegionPtr	reg2             /* source regions     */
215    )
216{
217    return pixman_region_union (newReg, reg1, reg2);
218}
219
220extern _X_EXPORT Bool RegionAppend(
221    RegionPtr /*dstrgn*/,
222    RegionPtr /*rgn*/);
223
224extern _X_EXPORT Bool RegionValidate(
225    RegionPtr /*badreg*/,
226    Bool * /*pOverlap*/);
227
228extern _X_EXPORT RegionPtr RegionFromRects(
229    int /*nrects*/,
230    xRectanglePtr /*prect*/,
231    int /*ctype*/);
232
233/*-
234 *-----------------------------------------------------------------------
235 * Subtract --
236 *	Subtract regS from regM and leave the result in regD.
237 *	S stands for subtrahend, M for minuend and D for difference.
238 *
239 * Results:
240 *	TRUE if successful.
241 *
242 * Side Effects:
243 *	regD is overwritten.
244 *
245 *-----------------------------------------------------------------------
246 */
247static inline Bool
248RegionSubtract(RegionPtr regD, RegionPtr regM, RegionPtr regS)
249{
250    return pixman_region_subtract (regD, regM, regS);
251}
252
253/*-
254 *-----------------------------------------------------------------------
255 * Inverse --
256 *	Take a region and a box and return a region that is everything
257 *	in the box but not in the region. The careful reader will note
258 *	that this is the same as subtracting the region from the box...
259 *
260 * Results:
261 *	TRUE.
262 *
263 * Side Effects:
264 *	newReg is overwritten.
265 *
266 *-----------------------------------------------------------------------
267 */
268
269static inline Bool
270RegionInverse(
271    RegionPtr	  newReg,       /* Destination region */
272    RegionPtr	  reg1,         /* Region to invert */
273    BoxPtr	  invRect	/* Bounding box for inversion */
274    )
275{
276    return pixman_region_inverse (newReg, reg1, invRect);
277}
278
279static inline int
280RegionContainsRect(RegionPtr region, BoxPtr prect)
281{
282    return pixman_region_contains_rectangle (region, prect);
283}
284
285/* TranslateRegion(pReg, x, y)
286   translates in place
287*/
288
289static inline void
290RegionTranslate(RegionPtr pReg, int x, int y)
291{
292    pixman_region_translate (pReg, x, y);
293}
294
295extern _X_EXPORT Bool RegionBreak(
296    RegionPtr /*pReg*/);
297
298static inline Bool
299RegionContainsPoint(
300    RegionPtr pReg,
301    int x,
302    int y,
303    BoxPtr box      /* "return" value */
304    )
305{
306    return pixman_region_contains_point (pReg, x, y, box);
307}
308
309static inline Bool
310RegionEqual(RegionPtr reg1, RegionPtr reg2)
311{
312    return pixman_region_equal (reg1, reg2);
313}
314
315extern _X_EXPORT Bool RegionRectAlloc(
316    RegionPtr /*pRgn*/,
317    int /*n*/
318);
319
320#ifdef DEBUG
321extern _X_EXPORT Bool RegionIsValid(
322    RegionPtr /*prgn*/
323);
324#endif
325
326extern _X_EXPORT void RegionPrint(
327    RegionPtr /*pReg*/);
328
329extern _X_EXPORT int RegionClipSpans(
330    RegionPtr /*prgnDst*/,
331    DDXPointPtr /*ppt*/,
332    int * /*pwidth*/,
333    int /*nspans*/,
334    DDXPointPtr /*pptNew*/,
335    int * /*pwidthNew*/,
336    int /*fSorted*/
337);
338
339#define INCLUDE_LEGACY_REGION_DEFINES
340#ifdef INCLUDE_LEGACY_REGION_DEFINES
341
342#define REGION_NIL				RegionNil
343#define REGION_NAR				RegionNar
344#define REGION_NUM_RECTS			RegionNumRects
345#define REGION_SIZE				RegionSize
346#define REGION_RECTS				RegionRects
347#define REGION_BOXPTR				RegionBoxptr
348#define REGION_BOX				RegionBox
349#define REGION_TOP				RegionTop
350#define REGION_END				RegionEnd
351#define REGION_SZOF				RegionSizeof
352#define BITMAP_TO_REGION			BitmapToRegion
353#define REGION_CREATE(pScreen, r, s)		RegionCreate(r,s)
354#define REGION_COPY(pScreen, d, r)		RegionCopy(d, r)
355#define REGION_DESTROY(pScreen, r)		RegionDestroy(r)
356#define REGION_INTERSECT(pScreen, res, r1, r2)	RegionIntersect(res, r1, r2)
357#define REGION_UNION(pScreen, res, r1, r2)	RegionUnion(res, r1, r2)
358#define REGION_SUBTRACT(pScreen, res, r1, r2)	RegionSubtract(res, r1, r2)
359#define REGION_INVERSE(pScreen, n, r, b)	RegionInverse(n, r, b)
360#define REGION_TRANSLATE(pScreen, r, x, y)	RegionTranslate(r, x, y)
361#define RECT_IN_REGION(pScreen, r, b) 		RegionContainsRect(r, b)
362#define POINT_IN_REGION(pScreen, r, x, y, b) 	RegionContainsPoint(r, x, y, b)
363#define REGION_EQUAL(pScreen, r1, r2)		RegionEqual(r1, r2)
364#define REGION_APPEND(pScreen, d, r)		RegionAppend(d, r)
365#define REGION_VALIDATE(pScreen, r, o)		RegionValidate(r, o)
366#define RECTS_TO_REGION(pScreen, n, r, c)	RegionFromRects(n, r, c)
367#define REGION_BREAK(pScreen, r)		RegionBreak(r)
368#define REGION_INIT(pScreen, r, b, s)		RegionInit(r, b, s)
369#define REGION_UNINIT(pScreen, r)		RegionUninit(r)
370#define REGION_RESET(pScreen, r, b)		RegionReset(r, b)
371#define REGION_NOTEMPTY(pScreen, r)		RegionNotEmpty(r)
372#define REGION_BROKEN(pScreen, r)		RegionBroken(r)
373#define REGION_EMPTY(pScreen, r)		RegionEmpty(r)
374#define REGION_EXTENTS(pScreen, r)		RegionExtents(r)
375#define REGION_NULL(pScreen, r)			RegionNull(r)
376
377#endif /* INCLUDE_LEGACY_REGION_DEFINES */
378#endif /* REGIONSTRUCT_H */
379