📄 devrgn.c
字号:
/* * Portions Copyright (c) 1999, 2000 Greg Haerr <greg@censoft.com> * Somewhat less shamelessly ripped from the Wine distribution * * Device-independent multi-rectangle clipping routines. * * GDI region objects. Shamelessly ripped out from the X11 distribution * Thanks for the nice licence. * * Copyright 1993, 1994, 1995 Alexandre Julliard * Modifications and additions: Copyright 1998 Huw Davies *//* **********************************************************************Copyright (c) 1987, 1988 X ConsortiumPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shall not beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from the X Consortium.Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts. All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of Digital not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.************************************************************************//* * The functions in this file implement the Region abstraction, similar to one * used in the X11 sample server. A Region is simply an area, as the name * implies, and is implemented as a "y-x-banded" array of rectangles. To * explain: Each Region is made up of a certain number of rectangles sorted * by y coordinate first, and then by x coordinate. * * Furthermore, the rectangles are banded such that every rectangle with a * given upper-left y coordinate (y1) will have the same lower-right y * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it * will span the entire vertical distance of the band. This means that some * areas that could be merged into a taller rectangle will be represented as * several shorter rectangles to account for shorter rectangles to its left * or right but within its "vertical scope". * * An added constraint on the rectangles is that they must cover as much * horizontal area as possible. E.g. no two rectangles in a band are allowed * to touch. * * Whenever possible, bands will be merged together to cover a greater vertical * distance (and thus reduce the number of rectangles). Two bands can be merged * only if the bottom of one touches the top of the other and they have * rectangles in the same places (of the same width, of course). This maintains * the y-x-banding that's so nice to have... */#include <stdio.h>#include <stdlib.h>#include <string.h>#include "device.h"typedef void (*REGION_OverlapBandFunctionPtr) (MWCLIPREGION * pReg, MWRECT * r1, MWRECT * r1End, MWRECT * r2, MWRECT * r2End, MWCOORD top, MWCOORD bottom);typedef void (*REGION_NonOverlapBandFunctionPtr) (MWCLIPREGION * pReg, MWRECT * r, MWRECT * end, MWCOORD top, MWCOORD bottom);/* 1 if two RECTs overlap. * 0 if two RECTs do not overlap. */#define EXTENTCHECK(r1, r2) \ ((r1)->right > (r2)->left && \ (r1)->left < (r2)->right && \ (r1)->bottom > (r2)->top && \ (r1)->top < (r2)->bottom)/* * Check to see if there is enough memory in the present region. */#define MEMCHECK(reg, rect, firstrect){\ if ((reg)->numRects >= ((reg)->size - 1)){\ (firstrect) = GdRealloc(\ (firstrect), ((sizeof(MWRECT)) * ((reg)->size)), \ (2 * (sizeof(MWRECT)) * ((reg)->size)));\ if ((firstrect) == 0)\ return;\ (reg)->size *= 2;\ (rect) = &(firstrect)[(reg)->numRects];\ }\ }#define REGION_NOT_EMPTY(pReg) pReg->numRects#define EMPTY_REGION(pReg) { \ (pReg)->numRects = 0; \ (pReg)->extents.left = (pReg)->extents.top = 0; \ (pReg)->extents.right = (pReg)->extents.bottom = 0; \ (pReg)->type = MWREGION_NULL; \ }#define INRECT(r, x, y) \ ( ( ((r).right > x)) && \ ( ((r).left <= x)) && \ ( ((r).bottom > y)) && \ ( ((r).top <= y)) )/** * return TRUE if point is in region * * @param rgn Region. * @param x X co-ordinate of point to check. * @param y Y co-ordinate of point to check. * @return TRUE iff point is in region */MWBOOLGdPtInRegion(MWCLIPREGION *rgn, MWCOORD x, MWCOORD y){ int i; if (rgn->numRects > 0 && INRECT(rgn->extents, x, y)) for (i = 0; i < rgn->numRects; i++) if (INRECT (rgn->rects[i], x, y)) return TRUE; return FALSE;}/** * return whether rectangle is all in, partly in, or out of region * * @param rgn Region. * @param rect Rectangle to check. * @return MWRECT_PARTIN, MWRECT_ALLIN, or MWRECT_OUT. */int GdRectInRegion(MWCLIPREGION *rgn, const MWRECT *rect){ MWRECT * pCurRect; MWRECT * pRectEnd; MWCOORD rx, ry; MWBOOL partIn, partOut; /* this is (just) a useful optimization */ if (!rgn->numRects || !EXTENTCHECK(&rgn->extents, rect)) return MWRECT_OUT; partOut = FALSE; partIn = FALSE; rx = rect->left; ry = rect->top; /* * can stop when both partOut and partIn are TRUE, * or we reach rect->bottom */ for (pCurRect = rgn->rects, pRectEnd = pCurRect + rgn->numRects; pCurRect < pRectEnd; pCurRect++) { if (pCurRect->bottom <= ry) continue; /* not far enough down yet*/ if (pCurRect->top > ry) { partOut = TRUE; /* missed part of rectangle above */ if (partIn || (pCurRect->top >= rect->bottom)) break; ry = pCurRect->top; /* x guaranteed to be == rect->left */ } if (pCurRect->right <= rx) continue; /* not far enough over yet */ if (pCurRect->left > rx) { partOut = TRUE; /* missed part of rectangle to left */ if (partIn) break; } if (pCurRect->left < rect->right) { partIn = TRUE; /* definitely overlap */ if (partOut) break; } if (pCurRect->right >= rect->right) { ry = pCurRect->bottom; /* finished with this band */ if (ry >= rect->bottom) break; rx = rect->left; /* reset x out to left again */ } else { /* * Because boxes in a band are maximal width, if the first box * to overlap the rectangle doesn't completely cover it in that * band, the rectangle must be partially out, since some of it * will be uncovered in that band. partIn will have been set true * by now... */ break; } } return(partIn ? ((ry < rect->bottom) ? MWRECT_PARTIN : MWRECT_ALLIN) : MWRECT_OUT);}#if 0000/* Returns TRUE if rect is at least partly inside rgn*/MWBOOLGdRectInRegion(MWCLIPREGION *rgn, const MWRECT *rect){ MWRECT *pCurRect, *pRectEnd; MWBOOL ret = FALSE; /* this is (just) a useful optimization */ if ((rgn->numRects > 0) && EXTENTCHECK(&rgn->extents, rect)) { for (pCurRect = rgn->rects, pRectEnd = pCurRect + rgn->numRects; pCurRect < pRectEnd; pCurRect++) { if (pCurRect->bottom <= rect->top) continue; /* not far enough down yet */ if (pCurRect->top >= rect->bottom) { ret = FALSE; /* too far down */ break; } if (pCurRect->right <= rect->left) continue; /* not far enough over yet */ if (pCurRect->left >= rect->right) { continue; } ret = TRUE; break; } } return ret;}#endifstatic MWBOOLEQUALRECT(MWRECT *r1, MWRECT *r2){ return ((r1->left == r2->left) && (r1->right == r2->right) && (r1->top == r2->top) && (r1->bottom == r2->bottom));}/** * Test if two regions are identical. * * @param r1 First region. * @param r2 Second region. * @return TRUE iff the regions are exactly the same. */MWBOOLGdEqualRegion(MWCLIPREGION *r1, MWCLIPREGION *r2){ int i; if (r1->numRects != r2->numRects) return FALSE; if (r1->numRects == 0) return TRUE; if (!EQUALRECT(&r1->extents, &r2->extents)) return FALSE; for (i = 0; i < r1->numRects; i++) { if (!EQUALRECT(r1->rects + i, r2->rects + i)) return FALSE; } return TRUE;}/** * Test if a region is empty. * * @param rgn Region. * @return TRUE iff the region is empty. */MWBOOLGdEmptyRegion(MWCLIPREGION *rgn){ return rgn->numRects == 0;}/** * Create a new empty MWCLIPREGION. * * @return A new region. */MWCLIPREGION *GdAllocRegion(void) { MWCLIPREGION *rgn; if ((rgn = malloc(sizeof( MWCLIPREGION )))) { if ((rgn->rects = malloc(sizeof( MWRECT )))) { rgn->size = 1; EMPTY_REGION(rgn); return rgn; } free(rgn); } return NULL;}/** * Create a new MWCLIPREGION which is a rectangular region. * * @param left Left edge of region. * @param top Top edge of region. * @param right Right edge of region. * @param bottom Bottom edge of region. * @return A new region. */MWCLIPREGION *GdAllocRectRegion(MWCOORD left, MWCOORD top, MWCOORD right, MWCOORD bottom){ MWCLIPREGION *rgn; rgn = GdAllocRegion(); if (rgn) GdSetRectRegion(rgn, left, top, right, bottom); return rgn;}/** * Create a new MWCLIPREGION which is a rectangular region. * * @param prc Rectangle defining the region. * @return A new region. */MWCLIPREGION *GdAllocRectRegionIndirect(MWRECT *prc){ return GdAllocRectRegion(prc->left, prc->top, prc->right, prc->bottom);}/** * Redefine a region to be just a simple rectangular region. * The previous contents of the region are destroyed. * * @param rgn A region. * @param left Left edge of region. * @param top Top edge of region. * @param right Right edge of region. * @param bottom Bottom edge of region. */voidGdSetRectRegion(MWCLIPREGION *rgn, MWCOORD left, MWCOORD top, MWCOORD right, MWCOORD bottom){ if (left != right && top != bottom) { rgn->rects->left = rgn->extents.left = left; rgn->rects->top = rgn->extents.top = top; rgn->rects->right = rgn->extents.right = right; rgn->rects->bottom = rgn->extents.bottom = bottom; rgn->numRects = 1; rgn->type = MWREGION_SIMPLE; } else EMPTY_REGION(rgn);}/** * Redefine a region to be just a simple rectangular region. * The previous contents of the region are destroyed. * * @param rgn A region. * @param prc Rectangle defining the region. */voidGdSetRectRegionIndirect(MWCLIPREGION *rgn, MWRECT *prc){ GdSetRectRegion(rgn, prc->left, prc->top, prc->right, prc->bottom);}/** * Destroy a region. Similar to free(). * * @param rgn The region to destroy. */voidGdDestroyRegion(MWCLIPREGION *rgn){ if(rgn) { free(rgn->rects); free(rgn); }}/** * Translate a region by a specified offset. * * @param rgn The region to translate. * @param x The X offset. * @param y The Y offset. */voidGdOffsetRegion(MWCLIPREGION *rgn, MWCOORD x, MWCOORD y){ int nbox = rgn->numRects; MWRECT *pbox = rgn->rects; if(nbox && (x || y)) { while(nbox--) { pbox->left += x; pbox->right += x; pbox->top += y; pbox->bottom += y; pbox++; } rgn->extents.left += x; rgn->extents.right += x; rgn->extents.top += y; rgn->extents.bottom += y; }}/** * get bounding box for region, return region type * * @param rgn The region to query. * @param prc On exit, contains the bounding box for the region. * @return The region type. One of MWREGION_NULL (empty region), * MWREGION_SIMPLE (rectangular region), or MWREGION_COMPLEX (non-rectangular * region). */intGdGetRegionBox(MWCLIPREGION *rgn, MWRECT *prc){ *prc = rgn->extents; return rgn->type;}/** * Adds a rectangle to a MWCLIPREGION * * @param rect Rectangle to add to the region. * @param rgn The clip region to modify. */voidGdUnionRectWithRegion(const MWRECT *rect, MWCLIPREGION *rgn){ MWCLIPREGION region; region.rects = ®ion.extents; region.numRects = 1; region.size = 1; region.type = MWREGION_SIMPLE; region.extents = *rect; GdUnionRegion(rgn, rgn, ®ion);}/** * Subtracts a rectangle from a MWCLIPREGION * * @param rect Rectangle to remove from the region. * @param rgn The clip region to modify. */voidGdSubtractRectFromRegion(const MWRECT *rect, MWCLIPREGION *rgn){ MWCLIPREGION region; region.rects = ®ion.extents; region.numRects = 1; region.size = 1; region.type = MWREGION_SIMPLE; region.extents = *rect; GdSubtractRegion(rgn, rgn, ®ion);}/** * Modify a region so that it is identical to another region. * * @param dst Region to copy to. * @param src Region to copy from. */voidGdCopyRegion(MWCLIPREGION *dst, MWCLIPREGION *src){ if (dst != src) /* don't want to copy to itself */ { if (dst->size < src->numRects) { if (! (dst->rects = GdRealloc( dst->rects, dst->numRects * sizeof(MWRECT), src->numRects * sizeof(MWRECT)))) return; dst->size = src->numRects; } dst->numRects = src->numRects; dst->extents.left = src->extents.left; dst->extents.top = src->extents.top; dst->extents.right = src->extents.right; dst->extents.bottom = src->extents.bottom; dst->type = src->type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -