~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/gfx/src/photon/nsRegionPh.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
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 mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is 
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsRegionPh.h"
 
40
 
 
41
/* Turn debug off to limit all the output to PhGfxLog */
 
42
#undef DEBUG
 
43
#undef FORCE_PR_LOG
 
44
 
 
45
#include "nsPhGfxLog.h"
 
46
 
 
47
 
 
48
#define tulx t->rect.ul.x
 
49
#define tuly t->rect.ul.y
 
50
#define tlrx t->rect.lr.x
 
51
#define tlry t->rect.lr.y
 
52
 
 
53
#define culx c->rect.ul.x
 
54
#define culy c->rect.ul.y
 
55
#define clrx c->rect.lr.x
 
56
#define clry c->rect.lr.y
 
57
 
 
58
NS_IMPL_ISUPPORTS1(nsRegionPh, nsIRegion)
 
59
 
 
60
 
 
61
void nsRegionPh :: Intersect( PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight ) 
 
62
{
 
63
        if( aWidth > 0 && aHeight > 0 ) {
 
64
                /* Create a temporary tile to  assign to mRegion */
 
65
                PhTile_t tile;
 
66
                tile.rect.ul.x = aX;
 
67
                tile.rect.ul.y = aY;
 
68
                tile.rect.lr.x = (aX+aWidth-1);
 
69
                tile.rect.lr.y = (aY+aHeight-1);
 
70
                tile.next = NULL;
 
71
    
 
72
                PhTile_t *original = mRegion;
 
73
                mRegion = PhIntersectTilings( mRegion, &tile, NULL );
 
74
                PhFreeTiles( original );
 
75
                if ( mRegion == NULL )
 
76
                        SetTo(0, 0, 1, 1);
 
77
        }
 
78
        else 
 
79
                SetRegionEmpty();
 
80
}
 
81
 
 
82
 
 
83
PRBool nsRegionPh :: IsEqual( const nsIRegion &aRegion ) {
 
84
  PRBool result = PR_TRUE;
 
85
  PhTile_t *tiles;
 
86
  aRegion.GetNativeRegion((void*&)tiles);
 
87
 
 
88
  /* If both are equal/NULL then it is equal */
 
89
  if( mRegion == tiles ) return PR_TRUE;
 
90
  else if( mRegion == NULL || tiles == NULL ) return PR_FALSE;
 
91
    
 
92
  PhSortTiles( mRegion );
 
93
  PhSortTiles( tiles );
 
94
 
 
95
  PhTile_t *t = mRegion, *c = tiles;
 
96
  while( t ) {
 
97
    if( tulx != culx || tuly != culy || tlrx != clrx || tlry != clry ) {
 
98
      result = PR_FALSE;
 
99
      break;    
 
100
                        }
 
101
    t = t->next;  
 
102
    c = c->next;
 
103
        }
 
104
  return result;
 
105
        }
 
106
 
 
107
 
 
108
void nsRegionPh :: GetBoundingBox( PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight ) {
 
109
 
 
110
        /* 99.99% there is only one tile - so simplify things, while allow the general case to work as well */
 
111
        if( mRegion && !mRegion->next ) {
 
112
                *aX = mRegion->rect.ul.x;
 
113
                *aY = mRegion->rect.ul.y;
 
114
                *aWidth = mRegion->rect.lr.x - mRegion->rect.ul.x + 1;
 
115
                *aHeight = mRegion->rect.lr.y - mRegion->rect.ul.y + 1;
 
116
                return;
 
117
                }
 
118
 
 
119
  int bX=-32767, bY=-32767;
 
120
 
 
121
  *aX = 32767; //0
 
122
  *aY = 32767; //0
 
123
 
 
124
        PhTile_t *t = mRegion;
 
125
 
 
126
        while( t ) {
 
127
                *aX = PR_MIN( *aX, tulx );
 
128
                *aY = PR_MIN( *aY, tuly );
 
129
                bX = PR_MAX( bX, tlrx );
 
130
                bY = PR_MAX( bY, tlry );         
 
131
                t = t->next;   
 
132
                }
 
133
 
 
134
        *aWidth =  bX - *aX + 1;
 
135
        *aHeight = bY - *aY + 1;
 
136
        }
 
137
 
 
138
 
 
139
PRBool nsRegionPh :: ContainsRect( PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight ) {
 
140
  if( !mRegion ) return PR_FALSE;
 
141
 
 
142
        PhCoalesceTiles( PhMergeTiles( PhSortTiles( mRegion )));  
 
143
 
 
144
        /* Create a temporary tile to  assign to mRegion */
 
145
        PhTile_t *tile = PhGetTile();
 
146
        tile->rect.ul.x = aX;
 
147
        tile->rect.ul.y = aY;
 
148
        tile->rect.lr.x = (aX+aWidth-1);
 
149
        tile->rect.lr.y = (aY+aHeight-1);
 
150
        tile->next = NULL;
 
151
 
 
152
        PhTile_t *test;
 
153
        test = PhIntersectTilings( tile, mRegion, NULL );
 
154
 
 
155
        PhFreeTiles( tile );
 
156
 
 
157
        if( test ) {
 
158
                PhFreeTiles( test );
 
159
                return PR_TRUE;
 
160
                }
 
161
        else return PR_FALSE;
 
162
        }
 
163
 
 
164
 
 
165
NS_IMETHODIMP nsRegionPh :: GetRects( nsRegionRectSet **aRects ) {
 
166
 
 
167
        /* 99.99% there is only one tile - so simplify things, while allow the general case to work as well */
 
168
        if( mRegion && !mRegion->next ) {
 
169
                if( *aRects == nsnull ) *aRects = ( nsRegionRectSet * ) PR_Malloc( sizeof( nsRegionRectSet ) );
 
170
                nsRegionRect *rect = (*aRects)->mRects;
 
171
                (*aRects)->mRectsLen = (*aRects)->mNumRects = 1;
 
172
                rect->x = mRegion->rect.ul.x;
 
173
                rect->y = mRegion->rect.ul.y;
 
174
                rect->width = mRegion->rect.lr.x - mRegion->rect.ul.x + 1;
 
175
                rect->height = mRegion->rect.lr.y - mRegion->rect.ul.y + 1;
 
176
                (*aRects)->mArea = rect->width * rect->height;
 
177
                return NS_OK;
 
178
                }
 
179
 
 
180
        /* the general case - old code */
 
181
  nsRegionRectSet   *rects;
 
182
  int               nbox = 0;
 
183
  nsRegionRect      *rect;
 
184
  PhTile_t              *t = mRegion;
 
185
 
 
186
  while( t ) { nbox++; t = t->next; } /* Count the Tiles */
 
187
 
 
188
  rects = *aRects;
 
189
 
 
190
  if ((nsnull == rects) || (rects->mRectsLen < (PRUint32) nbox)) {
 
191
    void *buf = PR_Realloc(rects, sizeof(nsRegionRectSet) + (sizeof(nsRegionRect) * (nbox - 1)));//was -1
 
192
    if (nsnull == buf) {
 
193
      if (nsnull != rects) rects->mNumRects = 0;
 
194
      return NS_OK;
 
195
        }
 
196
                  
 
197
    rects = (nsRegionRectSet *) buf;
 
198
    rects->mRectsLen = nbox;
 
199
        }
 
200
 
 
201
  rects->mNumRects = nbox;
 
202
  rects->mArea = 0;
 
203
  rect = &rects->mRects[0];
 
204
  t = mRegion;                  /* Reset tile indexer */
 
205
 
 
206
  while( nbox-- ) {
 
207
    rect->x = tulx;
 
208
    rect->width = (tlrx - tulx+1);
 
209
                rect->y = tuly;
 
210
    rect->height = (tlry - tuly+1);                                                                                       
 
211
    rects->mArea += rect->width * rect->height;
 
212
    rect++;
 
213
    t = t->next;
 
214
        }
 
215
 
 
216
  *aRects = rects;
 
217
  return NS_OK;
 
218
        }