2004-04-18 01:52:36 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2004-01-10 19:18:36 +03:00
|
|
|
*
|
2004-04-18 01:52:36 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2004-01-10 19:18:36 +03:00
|
|
|
*
|
2003-01-21 23:38:19 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
2004-01-10 19:18:36 +03:00
|
|
|
*
|
2004-04-18 01:52:36 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Dainis Jonitis, <Dainis_Jonitis@swh-t.lv>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2003-01-21 23:38:19 +03:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
2004-04-18 01:52:36 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2003-01-21 23:38:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef nsRegion_h__
|
|
|
|
#define nsRegion_h__
|
|
|
|
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
// Implementation of region.
|
2003-01-21 23:38:19 +03:00
|
|
|
// Region is represented as circular double-linked list of nsRegion::RgnRect structures.
|
|
|
|
// Rectangles in this list do not overlap and are sorted by (y, x) coordinates.
|
|
|
|
|
|
|
|
#include "nsRect.h"
|
2004-11-03 05:37:21 +03:00
|
|
|
#include "nsPoint.h"
|
2003-01-21 23:38:19 +03:00
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
class NS_GFX nsRegion
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
friend class nsRegionRectIterator;
|
|
|
|
friend class RgnRectMemoryAllocator;
|
2003-01-21 23:38:19 +03:00
|
|
|
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
// Special version of nsRect structure for speed optimizations in nsRegion code.
|
|
|
|
// Most important functions could be made inline and be sure that passed rectangles
|
|
|
|
// will always be non-empty.
|
|
|
|
//
|
|
|
|
// Do not add any new member variables to this structure!
|
|
|
|
// Otherwise it will break casts from nsRect to nsRectFast, which expect data parts to be identical.
|
|
|
|
struct nsRectFast : public nsRect
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRectFast () {} // No need to call parent constructor to set default values
|
|
|
|
nsRectFast (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) : nsRect (aX, aY, aWidth, aHeight) {}
|
|
|
|
nsRectFast (const nsRect& aRect) : nsRect (aRect) {}
|
|
|
|
|
|
|
|
// Override nsRect methods to make them inline. Do not check for emptiness.
|
|
|
|
inline PRBool Contains (const nsRect& aRect) const;
|
|
|
|
inline PRBool Intersects (const nsRect& aRect) const;
|
|
|
|
inline PRBool IntersectRect (const nsRect& aRect1, const nsRect& aRect2);
|
|
|
|
inline void UnionRect (const nsRect& aRect1, const nsRect& aRect2);
|
|
|
|
};
|
2003-01-21 23:38:19 +03:00
|
|
|
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
struct RgnRect : public nsRectFast
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
|
|
|
RgnRect* prev;
|
|
|
|
RgnRect* next;
|
|
|
|
|
|
|
|
RgnRect () {} // No need to call parent constructor to set default values
|
|
|
|
RgnRect (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) : nsRectFast (aX, aY, aWidth, aHeight) {}
|
|
|
|
RgnRect (const nsRectFast& aRect) : nsRectFast (aRect) {}
|
|
|
|
|
2007-12-25 07:37:44 +03:00
|
|
|
void* operator new (size_t) CPP_THROW_NEW;
|
|
|
|
void operator delete (void* aRect, size_t);
|
2004-01-10 19:18:36 +03:00
|
|
|
|
|
|
|
RgnRect& operator = (const RgnRect& aRect) // Do not overwrite prev/next pointers
|
|
|
|
{
|
2004-01-23 20:27:59 +03:00
|
|
|
x = aRect.x;
|
|
|
|
y = aRect.y;
|
|
|
|
width = aRect.width;
|
|
|
|
height = aRect.height;
|
2004-01-10 19:18:36 +03:00
|
|
|
return *this;
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion () { Init (); }
|
|
|
|
nsRegion (const nsRect& aRect) { Init (); Copy (aRect); }
|
|
|
|
nsRegion (const nsRegion& aRegion) { Init (); Copy (aRegion); }
|
2003-01-21 23:38:19 +03:00
|
|
|
~nsRegion () { SetToElements (0); }
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& operator = (const nsRect& aRect) { Copy (aRect); return *this; }
|
|
|
|
nsRegion& operator = (const nsRegion& aRegion) { Copy (aRegion); return *this; }
|
2003-01-21 23:38:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
nsRegion& And (const nsRegion& aRgn1, const nsRegion& aRgn2);
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& And (const nsRegion& aRegion, const nsRect& aRect);
|
|
|
|
nsRegion& And (const nsRect& aRect, const nsRegion& aRegion)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
|
|
|
return And (aRegion, aRect);
|
|
|
|
}
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& And (const nsRect& aRect1, const nsRect& aRect2)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRect TmpRect;
|
|
|
|
|
|
|
|
TmpRect.IntersectRect (aRect1, aRect2);
|
2003-01-21 23:38:19 +03:00
|
|
|
return Copy (TmpRect);
|
|
|
|
}
|
2004-01-10 19:18:36 +03:00
|
|
|
|
2003-01-21 23:38:19 +03:00
|
|
|
nsRegion& Or (const nsRegion& aRgn1, const nsRegion& aRgn2);
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Or (const nsRegion& aRegion, const nsRect& aRect);
|
|
|
|
nsRegion& Or (const nsRect& aRect, const nsRegion& aRegion)
|
|
|
|
{
|
2003-01-21 23:38:19 +03:00
|
|
|
return Or (aRegion, aRect);
|
|
|
|
}
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Or (const nsRect& aRect1, const nsRect& aRect2)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
Copy (aRect1);
|
|
|
|
return Or (*this, aRect2);
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRegion& Xor (const nsRegion& aRgn1, const nsRegion& aRgn2);
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Xor (const nsRegion& aRegion, const nsRect& aRect);
|
|
|
|
nsRegion& Xor (const nsRect& aRect, const nsRegion& aRegion)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
|
|
|
return Xor (aRegion, aRect);
|
|
|
|
}
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Xor (const nsRect& aRect1, const nsRect& aRect2)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
Copy (aRect1);
|
|
|
|
return Xor (*this, aRect2);
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRegion& Sub (const nsRegion& aRgn1, const nsRegion& aRgn2);
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Sub (const nsRegion& aRegion, const nsRect& aRect);
|
|
|
|
nsRegion& Sub (const nsRect& aRect, const nsRegion& aRegion)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
return Sub (nsRegion (aRect), aRegion);
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Sub (const nsRect& aRect1, const nsRect& aRect2)
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
2004-01-10 19:18:36 +03:00
|
|
|
Copy (aRect1);
|
|
|
|
return Sub (*this, aRect2);
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
|
|
|
|
2005-11-24 04:05:18 +03:00
|
|
|
PRBool Contains (const nsRect& aRect) const;
|
|
|
|
PRBool Intersects (const nsRect& aRect) const;
|
2003-01-21 23:38:19 +03:00
|
|
|
|
2004-11-03 05:37:21 +03:00
|
|
|
void MoveBy (PRInt32 aXOffset, PRInt32 aYOffset)
|
|
|
|
{
|
|
|
|
MoveBy (nsPoint (aXOffset, aYOffset));
|
|
|
|
}
|
|
|
|
void MoveBy (nsPoint aPt);
|
2004-01-10 19:18:36 +03:00
|
|
|
void SetEmpty ()
|
|
|
|
{
|
2003-01-21 23:38:19 +03:00
|
|
|
SetToElements (0);
|
|
|
|
mBoundRect.SetRect (0, 0, 0, 0);
|
|
|
|
}
|
2004-01-10 19:18:36 +03:00
|
|
|
|
2003-01-21 23:38:19 +03:00
|
|
|
PRBool IsEmpty () const { return mRectCount == 0; }
|
|
|
|
PRBool IsComplex () const { return mRectCount > 1; }
|
|
|
|
PRBool IsEqual (const nsRegion& aRegion) const;
|
|
|
|
PRUint32 GetNumRects () const { return mRectCount; }
|
2004-01-10 19:18:36 +03:00
|
|
|
const nsRect& GetBounds () const { return mBoundRect; }
|
2003-01-21 23:38:19 +03:00
|
|
|
|
2004-11-03 05:37:21 +03:00
|
|
|
/**
|
|
|
|
* Make sure the region has at most aMaxRects by adding area to it
|
|
|
|
* if necessary. The simplified region will be a superset of the
|
|
|
|
* original region. The simplified region's bounding box will be
|
|
|
|
* the same as for the current region.
|
|
|
|
*/
|
|
|
|
void SimplifyOutward (PRUint32 aMaxRects);
|
|
|
|
/**
|
|
|
|
* Make sure the region has at most aMaxRects by removing area from
|
|
|
|
* it if necessary. The simplified region will be a subset of the
|
|
|
|
* original region.
|
|
|
|
*/
|
|
|
|
void SimplifyInward (PRUint32 aMaxRects);
|
2005-11-24 04:05:18 +03:00
|
|
|
/**
|
|
|
|
* Efficiently try to remove a rectangle from this region. The actual
|
|
|
|
* area removed could be some sub-area contained by the rectangle
|
|
|
|
* (even possibly nothing at all).
|
|
|
|
*
|
|
|
|
* We remove all rectangles that are contained by aRect.
|
|
|
|
*/
|
|
|
|
void SimpleSubtract (const nsRect& aRect);
|
|
|
|
/**
|
|
|
|
* Efficiently try to remove a region from this region. The actual
|
|
|
|
* area removed could be some sub-area contained by aRegion
|
|
|
|
* (even possibly nothing at all).
|
|
|
|
*
|
|
|
|
* We remove all rectangles of this region that are contained by
|
|
|
|
* a rectangle of aRegion.
|
|
|
|
*/
|
|
|
|
void SimpleSubtract (const nsRegion& aRegion);
|
2003-01-21 23:38:19 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint32 mRectCount;
|
|
|
|
RgnRect* mCurRect;
|
|
|
|
RgnRect mRectListHead;
|
|
|
|
nsRectFast mBoundRect;
|
|
|
|
|
2003-03-25 05:58:10 +03:00
|
|
|
void Init ();
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegion& Copy (const nsRegion& aRegion);
|
|
|
|
nsRegion& Copy (const nsRect& aRect);
|
|
|
|
void InsertBefore (RgnRect* aNewRect, RgnRect* aRelativeRect);
|
|
|
|
void InsertAfter (RgnRect* aNewRect, RgnRect* aRelativeRect);
|
2003-01-21 23:38:19 +03:00
|
|
|
void SetToElements (PRUint32 aCount);
|
|
|
|
RgnRect* Remove (RgnRect* aRect);
|
|
|
|
void InsertInPlace (RgnRect* aRect, PRBool aOptimizeOnFly = PR_FALSE);
|
2004-01-10 19:18:36 +03:00
|
|
|
inline void SaveLinkChain ();
|
|
|
|
inline void RestoreLinkChain ();
|
2003-01-21 23:38:19 +03:00
|
|
|
void Optimize ();
|
|
|
|
void SubRegion (const nsRegion& aRegion, nsRegion& aResult) const;
|
|
|
|
void SubRect (const nsRectFast& aRect, nsRegion& aResult, nsRegion& aCompleted) const;
|
|
|
|
void SubRect (const nsRectFast& aRect, nsRegion& aResult) const
|
|
|
|
{ SubRect (aRect, aResult, aResult); }
|
|
|
|
void Merge (const nsRegion& aRgn1, const nsRegion& aRgn2);
|
|
|
|
void MoveInto (nsRegion& aDestRegion, const RgnRect* aStartRect);
|
|
|
|
void MoveInto (nsRegion& aDestRegion)
|
|
|
|
{ MoveInto (aDestRegion, mRectListHead.next); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Allow read-only access to region rectangles by iterating the list
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
class NS_GFX nsRegionRectIterator
|
2003-01-21 23:38:19 +03:00
|
|
|
{
|
|
|
|
const nsRegion* mRegion;
|
|
|
|
const nsRegion::RgnRect* mCurPtr;
|
|
|
|
|
|
|
|
public:
|
2004-01-10 19:18:36 +03:00
|
|
|
nsRegionRectIterator (const nsRegion& aRegion)
|
|
|
|
{
|
|
|
|
mRegion = &aRegion;
|
|
|
|
mCurPtr = &aRegion.mRectListHead;
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
const nsRect* Next ()
|
|
|
|
{
|
|
|
|
mCurPtr = mCurPtr->next;
|
2003-01-21 23:38:19 +03:00
|
|
|
return (mCurPtr != &mRegion->mRectListHead) ? mCurPtr : nsnull;
|
|
|
|
}
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
const nsRect* Prev ()
|
|
|
|
{
|
|
|
|
mCurPtr = mCurPtr->prev;
|
2003-01-21 23:38:19 +03:00
|
|
|
return (mCurPtr != &mRegion->mRectListHead) ? mCurPtr : nsnull;
|
|
|
|
}
|
|
|
|
|
2004-01-10 19:18:36 +03:00
|
|
|
void Reset ()
|
|
|
|
{
|
|
|
|
mCurPtr = &mRegion->mRectListHead;
|
2003-01-21 23:38:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|