Back to index

lightning-sunbird  0.9+nobinonly
nsRegion.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Dainis Jonitis, <Dainis_Jonitis@swh-t.lv>.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 
00038 #ifndef nsRegion_h__
00039 #define nsRegion_h__
00040 
00041 
00042 // Implementation of region.
00043 // Region is represented as circular double-linked list of nsRegion::RgnRect structures.
00044 // Rectangles in this list do not overlap and are sorted by (y, x) coordinates.
00045 
00046 #include "nsRect.h"
00047 #include "nsPoint.h"
00048 
00049 class NS_GFX nsRegion
00050 {
00051   friend class nsRegionRectIterator;
00052   friend class RgnRectMemoryAllocator;
00053 
00054 
00055 // Special version of nsRect structure for speed optimizations in nsRegion code.
00056 // Most important functions could be made inline and be sure that passed rectangles
00057 // will always be non-empty.
00058 // 
00059 // Do not add any new member variables to this structure! 
00060 // Otherwise it will break casts from nsRect to nsRectFast, which expect data parts to be identical.
00061   struct nsRectFast : public nsRect
00062   {
00063     nsRectFast () {}      // No need to call parent constructor to set default values
00064     nsRectFast (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) : nsRect (aX, aY, aWidth, aHeight) {}
00065     nsRectFast (const nsRect& aRect) : nsRect (aRect) {}
00066 
00067     // Override nsRect methods to make them inline. Do not check for emptiness.
00068     inline PRBool Contains (const nsRect& aRect) const;
00069     inline PRBool Intersects (const nsRect& aRect) const;
00070     inline PRBool IntersectRect (const nsRect& aRect1, const nsRect& aRect2);
00071     inline void UnionRect (const nsRect& aRect1, const nsRect& aRect2);
00072   };
00073 
00074 
00075   struct RgnRect : public nsRectFast
00076   {
00077     RgnRect* prev;
00078     RgnRect* next;
00079 
00080     RgnRect () {}                           // No need to call parent constructor to set default values
00081     RgnRect (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) : nsRectFast (aX, aY, aWidth, aHeight) {}
00082     RgnRect (const nsRectFast& aRect) : nsRectFast (aRect) {}
00083 
00084     inline void* operator new (size_t) CPP_THROW_NEW;
00085     inline void  operator delete (void* aRect, size_t);
00086 
00087     RgnRect& operator = (const RgnRect& aRect)      // Do not overwrite prev/next pointers
00088     {
00089       x = aRect.x;
00090       y = aRect.y;
00091       width = aRect.width;
00092       height = aRect.height;
00093       return *this;
00094     }
00095   };
00096 
00097 
00098 public:
00099   nsRegion () { Init (); }
00100   nsRegion (const nsRect& aRect) { Init (); Copy (aRect); }
00101   nsRegion (const nsRegion& aRegion) { Init (); Copy (aRegion); }
00102  ~nsRegion () { SetToElements (0); }
00103   nsRegion& operator = (const nsRect& aRect) { Copy (aRect); return *this; }
00104   nsRegion& operator = (const nsRegion& aRegion) { Copy (aRegion); return *this; }
00105 
00106 
00107   nsRegion& And  (const nsRegion& aRgn1,   const nsRegion& aRgn2);
00108   nsRegion& And  (const nsRegion& aRegion, const nsRect& aRect);
00109   nsRegion& And  (const nsRect& aRect, const nsRegion& aRegion)
00110   {
00111     return  And  (aRegion, aRect);
00112   }
00113   nsRegion& And  (const nsRect& aRect1, const nsRect& aRect2)
00114   {
00115     nsRect TmpRect;
00116 
00117     TmpRect.IntersectRect (aRect1, aRect2);
00118     return Copy (TmpRect);
00119   }
00120 
00121   nsRegion& Or   (const nsRegion& aRgn1,   const nsRegion& aRgn2);
00122   nsRegion& Or   (const nsRegion& aRegion, const nsRect& aRect);
00123   nsRegion& Or   (const nsRect& aRect, const nsRegion& aRegion)
00124   {
00125     return  Or   (aRegion, aRect);
00126   }
00127   nsRegion& Or   (const nsRect& aRect1, const nsRect& aRect2)
00128   {
00129     Copy (aRect1);
00130     return Or (*this, aRect2);
00131   }
00132 
00133   nsRegion& Xor  (const nsRegion& aRgn1,   const nsRegion& aRgn2);
00134   nsRegion& Xor  (const nsRegion& aRegion, const nsRect& aRect);
00135   nsRegion& Xor  (const nsRect& aRect, const nsRegion& aRegion)
00136   {
00137     return  Xor  (aRegion, aRect);
00138   }
00139   nsRegion& Xor  (const nsRect& aRect1, const nsRect& aRect2)
00140   {
00141     Copy (aRect1);
00142     return Xor (*this, aRect2);
00143   }
00144 
00145   nsRegion& Sub  (const nsRegion& aRgn1,   const nsRegion& aRgn2);
00146   nsRegion& Sub  (const nsRegion& aRegion, const nsRect& aRect);
00147   nsRegion& Sub  (const nsRect& aRect, const nsRegion& aRegion)
00148   {
00149     return Sub (nsRegion (aRect), aRegion);
00150   }
00151   nsRegion& Sub  (const nsRect& aRect1, const nsRect& aRect2)
00152   {
00153     Copy (aRect1);
00154     return Sub (*this, aRect2);
00155   }
00156 
00157 
00158   void MoveBy (PRInt32 aXOffset, PRInt32 aYOffset)
00159   {
00160     MoveBy (nsPoint (aXOffset, aYOffset));
00161   }
00162   void MoveBy (nsPoint aPt);
00163   void SetEmpty ()
00164   {
00165     SetToElements (0);
00166     mBoundRect.SetRect (0, 0, 0, 0);
00167   }
00168 
00169   PRBool IsEmpty () const { return mRectCount == 0; }
00170   PRBool IsComplex () const { return mRectCount > 1; }
00171   PRBool IsEqual (const nsRegion& aRegion) const;
00172   PRUint32 GetNumRects () const { return mRectCount; }
00173   const nsRect& GetBounds () const { return mBoundRect; }
00174 
00181   void SimplifyOutward (PRUint32 aMaxRects);
00187   void SimplifyInward (PRUint32 aMaxRects);
00188 
00189 private:
00190   PRUint32    mRectCount;
00191   RgnRect*    mCurRect;
00192   RgnRect     mRectListHead;
00193   nsRectFast  mBoundRect;
00194 
00195   void Init ();
00196   nsRegion& Copy (const nsRegion& aRegion);
00197   nsRegion& Copy (const nsRect& aRect);
00198   void InsertBefore (RgnRect* aNewRect, RgnRect* aRelativeRect);
00199   void InsertAfter (RgnRect* aNewRect, RgnRect* aRelativeRect);
00200   void SetToElements (PRUint32 aCount);
00201   RgnRect* Remove (RgnRect* aRect);
00202   void InsertInPlace (RgnRect* aRect, PRBool aOptimizeOnFly = PR_FALSE);
00203   inline void SaveLinkChain ();
00204   inline void RestoreLinkChain ();
00205   void Optimize ();
00206   void SubRegion (const nsRegion& aRegion, nsRegion& aResult) const;
00207   void SubRect (const nsRectFast& aRect, nsRegion& aResult, nsRegion& aCompleted) const;
00208   void SubRect (const nsRectFast& aRect, nsRegion& aResult) const
00209   {    SubRect (aRect, aResult, aResult);  }
00210   void Merge (const nsRegion& aRgn1, const nsRegion& aRgn2);
00211   void MoveInto (nsRegion& aDestRegion, const RgnRect* aStartRect);
00212   void MoveInto (nsRegion& aDestRegion)
00213   {    MoveInto (aDestRegion, mRectListHead.next);  }
00214 };
00215 
00216 
00217 
00218 // Allow read-only access to region rectangles by iterating the list
00219 
00220 class NS_GFX nsRegionRectIterator
00221 {
00222   const nsRegion*  mRegion;
00223   const nsRegion::RgnRect* mCurPtr;
00224 
00225 public:
00226   nsRegionRectIterator (const nsRegion& aRegion)
00227   {
00228     mRegion = &aRegion;
00229     mCurPtr = &aRegion.mRectListHead;
00230   }
00231 
00232   const nsRect* Next ()
00233   {
00234     mCurPtr = mCurPtr->next;
00235     return (mCurPtr != &mRegion->mRectListHead) ? mCurPtr : nsnull;
00236   }
00237 
00238   const nsRect* Prev ()
00239   {
00240     mCurPtr = mCurPtr->prev;
00241     return (mCurPtr != &mRegion->mRectListHead) ? mCurPtr : nsnull;
00242   }
00243 
00244   void Reset ()
00245   {
00246     mCurPtr = &mRegion->mRectListHead;
00247   }
00248 };
00249 
00250 
00251 #endif