Back to index

lightning-sunbird  0.9+nobinonly
nsRegionPh.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsRegionPh.h"
00039 
00040 /* Turn debug off to limit all the output to PhGfxLog */
00041 #undef DEBUG
00042 #undef FORCE_PR_LOG
00043 
00044 #include "nsPhGfxLog.h"
00045 
00046 
00047 #define tulx t->rect.ul.x
00048 #define tuly t->rect.ul.y
00049 #define tlrx t->rect.lr.x
00050 #define tlry t->rect.lr.y
00051 
00052 #define culx c->rect.ul.x
00053 #define culy c->rect.ul.y
00054 #define clrx c->rect.lr.x
00055 #define clry c->rect.lr.y
00056 
00057 NS_IMPL_ISUPPORTS1(nsRegionPh, nsIRegion)
00058 
00059 
00060 void nsRegionPh :: Intersect( PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight ) 
00061 {
00062        if( aWidth > 0 && aHeight > 0 ) {
00063               /* Create a temporary tile to  assign to mRegion */
00064               PhTile_t tile;
00065               tile.rect.ul.x = aX;
00066               tile.rect.ul.y = aY;
00067               tile.rect.lr.x = (aX+aWidth-1);
00068               tile.rect.lr.y = (aY+aHeight-1);
00069               tile.next = NULL;
00070     
00071               PhTile_t *original = mRegion;
00072               mRegion = PhIntersectTilings( mRegion, &tile, NULL );
00073               PhFreeTiles( original );
00074               if ( mRegion == NULL )
00075                      SetTo(0, 0, 1, 1);
00076        }
00077        else 
00078               SetRegionEmpty();
00079 }
00080 
00081 
00082 PRBool nsRegionPh :: IsEqual( const nsIRegion &aRegion ) {
00083   PRBool result = PR_TRUE;
00084   PhTile_t *tiles;
00085   aRegion.GetNativeRegion((void*&)tiles);
00086 
00087   /* If both are equal/NULL then it is equal */
00088   if( mRegion == tiles ) return PR_TRUE;
00089   else if( mRegion == NULL || tiles == NULL ) return PR_FALSE;
00090     
00091   PhSortTiles( mRegion );
00092   PhSortTiles( tiles );
00093 
00094   PhTile_t *t = mRegion, *c = tiles;
00095   while( t ) {
00096     if( tulx != culx || tuly != culy || tlrx != clrx || tlry != clry ) {
00097       result = PR_FALSE;
00098       break;  
00099                      }
00100     t = t->next;  
00101     c = c->next;
00102        }
00103   return result;
00104        }
00105 
00106 
00107 void nsRegionPh :: GetBoundingBox( PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight ) {
00108 
00109        /* 99.99% there is only one tile - so simplify things, while allow the general case to work as well */
00110        if( mRegion && !mRegion->next ) {
00111               *aX = mRegion->rect.ul.x;
00112               *aY = mRegion->rect.ul.y;
00113               *aWidth = mRegion->rect.lr.x - mRegion->rect.ul.x + 1;
00114               *aHeight = mRegion->rect.lr.y - mRegion->rect.ul.y + 1;
00115               return;
00116               }
00117 
00118   int bX=-32767, bY=-32767;
00119 
00120   *aX = 32767; //0
00121   *aY = 32767; //0
00122 
00123        PhTile_t *t = mRegion;
00124 
00125        while( t ) {
00126               *aX = PR_MIN( *aX, tulx );
00127               *aY = PR_MIN( *aY, tuly );
00128               bX = PR_MAX( bX, tlrx );
00129               bY = PR_MAX( bY, tlry );     
00130               t = t->next;   
00131               }
00132 
00133        *aWidth =  bX - *aX + 1;
00134        *aHeight = bY - *aY + 1;
00135        }
00136 
00137 
00138 PRBool nsRegionPh :: ContainsRect( PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight ) {
00139   if( !mRegion ) return PR_FALSE;
00140 
00141        PhCoalesceTiles( PhMergeTiles( PhSortTiles( mRegion )));  
00142 
00143        /* Create a temporary tile to  assign to mRegion */
00144        PhTile_t *tile = PhGetTile();
00145        tile->rect.ul.x = aX;
00146        tile->rect.ul.y = aY;
00147        tile->rect.lr.x = (aX+aWidth-1);
00148        tile->rect.lr.y = (aY+aHeight-1);
00149        tile->next = NULL;
00150 
00151        PhTile_t *test;
00152        test = PhIntersectTilings( tile, mRegion, NULL );
00153 
00154        PhFreeTiles( tile );
00155 
00156        if( test ) {
00157               PhFreeTiles( test );
00158               return PR_TRUE;
00159               }
00160        else return PR_FALSE;
00161        }
00162 
00163 
00164 NS_IMETHODIMP nsRegionPh :: GetRects( nsRegionRectSet **aRects ) {
00165 
00166        /* 99.99% there is only one tile - so simplify things, while allow the general case to work as well */
00167        if( mRegion && !mRegion->next ) {
00168               if( *aRects == nsnull ) *aRects = ( nsRegionRectSet * ) PR_Malloc( sizeof( nsRegionRectSet ) );
00169               nsRegionRect *rect = (*aRects)->mRects;
00170               (*aRects)->mRectsLen = (*aRects)->mNumRects = 1;
00171               rect->x = mRegion->rect.ul.x;
00172               rect->y = mRegion->rect.ul.y;
00173               rect->width = mRegion->rect.lr.x - mRegion->rect.ul.x + 1;
00174               rect->height = mRegion->rect.lr.y - mRegion->rect.ul.y + 1;
00175               (*aRects)->mArea = rect->width * rect->height;
00176               return NS_OK;
00177               }
00178 
00179        /* the general case - old code */
00180   nsRegionRectSet   *rects;
00181   int               nbox = 0;
00182   nsRegionRect      *rect;
00183   PhTile_t            *t = mRegion;
00184 
00185   while( t ) { nbox++; t = t->next; } /* Count the Tiles */
00186 
00187   rects = *aRects;
00188 
00189   if ((nsnull == rects) || (rects->mRectsLen < (PRUint32) nbox)) {
00190     void *buf = PR_Realloc(rects, sizeof(nsRegionRectSet) + (sizeof(nsRegionRect) * (nbox - 1)));//was -1
00191     if (nsnull == buf) {
00192       if (nsnull != rects) rects->mNumRects = 0;
00193       return NS_OK;
00194        }
00195                 
00196     rects = (nsRegionRectSet *) buf;
00197     rects->mRectsLen = nbox;
00198        }
00199 
00200   rects->mNumRects = nbox;
00201   rects->mArea = 0;
00202   rect = &rects->mRects[0];
00203   t = mRegion;                  /* Reset tile indexer */
00204 
00205   while( nbox-- ) {
00206     rect->x = tulx;
00207     rect->width = (tlrx - tulx+1);
00208               rect->y = tuly;
00209     rect->height = (tlry - tuly+1);                                                                               
00210     rects->mArea += rect->width * rect->height;
00211     rect++;
00212     t = t->next;
00213        }
00214  
00215   *aRects = rects;
00216   return NS_OK;
00217        }