LCOV - code coverage report
Current view: directory - objdir/dist/include - nsRect.h (source / functions) Found Hit Coverage
Test: app.info Lines: 116 16 13.8 %
Date: 2012-04-21 Functions: 24 6 25.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       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                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      26                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : 
      39                 : #ifndef NSRECT_H
      40                 : #define NSRECT_H
      41                 : 
      42                 : #include <stdio.h>
      43                 : #include "nsCoord.h"
      44                 : #include "nsPoint.h"
      45                 : #include "nsSize.h"
      46                 : #include "nsMargin.h"
      47                 : #include "gfxCore.h"
      48                 : #include "nsTraceRefcnt.h"
      49                 : #include "mozilla/gfx/BaseRect.h"
      50                 : 
      51                 : struct nsIntRect;
      52                 : 
      53                 : struct NS_GFX nsRect :
      54                 :   public mozilla::gfx::BaseRect<nscoord, nsRect, nsPoint, nsSize, nsMargin> {
      55                 :   typedef mozilla::gfx::BaseRect<nscoord, nsRect, nsPoint, nsSize, nsMargin> Super;
      56                 : 
      57                 :   static void VERIFY_COORD(nscoord aValue) { ::VERIFY_COORD(aValue); }
      58                 : 
      59                 :   // Constructors
      60               0 :   nsRect() : Super()
      61                 :   {
      62               0 :     MOZ_COUNT_CTOR(nsRect);
      63               0 :   }
      64               0 :   nsRect(const nsRect& aRect) : Super(aRect)
      65                 :   {
      66               0 :     MOZ_COUNT_CTOR(nsRect);
      67               0 :   }
      68               0 :   nsRect(const nsPoint& aOrigin, const nsSize &aSize) : Super(aOrigin, aSize)
      69                 :   {
      70               0 :     MOZ_COUNT_CTOR(nsRect);
      71               0 :   }
      72               2 :   nsRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) :
      73               2 :       Super(aX, aY, aWidth, aHeight)
      74                 :   {
      75               2 :     MOZ_COUNT_CTOR(nsRect);
      76               2 :   }
      77                 : 
      78                 : #ifdef NS_BUILD_REFCNT_LOGGING
      79            1370 :   ~nsRect() {
      80            1370 :     MOZ_COUNT_DTOR(nsRect);
      81            1370 :   }
      82                 : #endif
      83                 : 
      84                 :   // A version of Inflate that caps the values to the nscoord range.
      85                 :   // x & y is capped at the minimum value nscoord_MIN and
      86                 :   // width & height is capped at the maximum value nscoord_MAX.
      87               0 :   void SaturatingInflate(const nsMargin& aMargin)
      88                 :   {
      89                 : #ifdef NS_COORD_IS_FLOAT
      90                 :     Inflate(aMargin);
      91                 : #else
      92               0 :     PRInt64 nx = PRInt64(x) - aMargin.left;
      93               0 :     if (nx < nscoord_MIN) {
      94               0 :       NS_WARNING("Underflowed nscoord_MIN in conversion to nscoord x");
      95               0 :       nx = nscoord_MIN;
      96                 :     }
      97               0 :     x = nscoord(nx);
      98                 : 
      99               0 :     PRInt64 ny = PRInt64(y) - aMargin.top;
     100               0 :     if (ny < nscoord_MIN) {
     101               0 :       NS_WARNING("Underflowed nscoord_MIN in conversion to nscoord y");
     102               0 :       ny = nscoord_MIN;
     103                 :     }
     104               0 :     y = nscoord(ny);
     105                 : 
     106               0 :     PRInt64 w = PRInt64(width) + PRInt64(aMargin.left) + aMargin.right;
     107               0 :     if (w > nscoord_MAX) {
     108               0 :       NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord width");
     109               0 :       w = nscoord_MAX;
     110                 :     }
     111               0 :     width = nscoord(w);
     112                 : 
     113               0 :     PRInt64 h = PRInt64(height) + PRInt64(aMargin.top) + aMargin.bottom;
     114               0 :     if (h > nscoord_MAX) {
     115               0 :       NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord height");
     116               0 :       h = nscoord_MAX;
     117                 :     }
     118               0 :     height = nscoord(h);
     119                 : #endif
     120               0 :   }
     121                 : 
     122                 :   // We have saturating versions of all the Union methods. These avoid
     123                 :   // overflowing nscoord values in the 'width' and 'height' fields by
     124                 :   // clamping the width and height values to nscoord_MAX if necessary.
     125                 : 
     126                 :   nsRect SaturatingUnion(const nsRect& aRect) const
     127                 :   {
     128                 :     if (IsEmpty()) {
     129                 :       return aRect;
     130                 :     } else if (aRect.IsEmpty()) {
     131                 :       return *static_cast<const nsRect*>(this);
     132                 :     } else {
     133                 :       return SaturatingUnionEdges(aRect);
     134                 :     }
     135                 :   }
     136                 : 
     137               0 :   nsRect SaturatingUnionEdges(const nsRect& aRect) const
     138                 :   {
     139                 : #ifdef NS_COORD_IS_FLOAT
     140                 :     return UnionEdges(aRect);
     141                 : #else
     142               0 :     nsRect result;
     143               0 :     result.x = NS_MIN(aRect.x, x);
     144               0 :     result.y = NS_MIN(aRect.y, y);
     145               0 :     PRInt64 w = NS_MAX(PRInt64(aRect.x) + aRect.width, PRInt64(x) + width) - result.x;
     146               0 :     PRInt64 h = NS_MAX(PRInt64(aRect.y) + aRect.height, PRInt64(y) + height) - result.y;
     147               0 :     if (w > nscoord_MAX) {
     148               0 :       NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord width");
     149               0 :       w = nscoord_MAX;
     150                 :     }
     151               0 :     if (h > nscoord_MAX) {
     152               0 :       NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord height");
     153               0 :       h = nscoord_MAX;
     154                 :     }
     155               0 :     result.width = nscoord(w);
     156               0 :     result.height = nscoord(h);
     157                 :     return result;
     158                 : #endif
     159                 :   }
     160                 : 
     161                 :   void SaturatingUnionRect(const nsRect& aRect1, const nsRect& aRect2)
     162                 :   {
     163                 :     *this = aRect1.SaturatingUnion(aRect2);
     164                 :   }
     165               0 :   void SaturatingUnionRectEdges(const nsRect& aRect1, const nsRect& aRect2)
     166                 :   {
     167               0 :     *this = aRect1.SaturatingUnionEdges(aRect2);
     168               0 :   }
     169                 : 
     170                 :   // Converts this rect from aFromAPP, an appunits per pixel ratio, to aToAPP.
     171                 :   // In the RoundOut version we make the rect the smallest rect containing the
     172                 :   // unrounded result. In the RoundIn version we make the rect the largest rect
     173                 :   // contained in the unrounded result.
     174                 :   // Note: this can turn an empty rectangle into a non-empty rectangle
     175                 :   inline nsRect ConvertAppUnitsRoundOut(PRInt32 aFromAPP, PRInt32 aToAPP) const;
     176                 :   inline nsRect ConvertAppUnitsRoundIn(PRInt32 aFromAPP, PRInt32 aToAPP) const;
     177                 : 
     178                 :   inline nsIntRect ScaleToNearestPixels(float aXScale, float aYScale,
     179                 :                                         nscoord aAppUnitsPerPixel) const;
     180                 :   inline nsIntRect ToNearestPixels(nscoord aAppUnitsPerPixel) const;
     181                 :   // Note: this can turn an empty rectangle into a non-empty rectangle
     182                 :   inline nsIntRect ScaleToOutsidePixels(float aXScale, float aYScale,
     183                 :                                         nscoord aAppUnitsPerPixel) const;
     184                 :   // Note: this can turn an empty rectangle into a non-empty rectangle
     185                 :   inline nsIntRect ToOutsidePixels(nscoord aAppUnitsPerPixel) const;
     186                 :   inline nsIntRect ScaleToInsidePixels(float aXScale, float aYScale,
     187                 :                                        nscoord aAppUnitsPerPixel) const;
     188                 :   inline nsIntRect ToInsidePixels(nscoord aAppUnitsPerPixel) const;
     189                 : };
     190                 : 
     191                 : struct NS_GFX nsIntRect :
     192               0 :   public mozilla::gfx::BaseRect<PRInt32, nsIntRect, nsIntPoint, nsIntSize, nsIntMargin> {
     193                 :   typedef mozilla::gfx::BaseRect<PRInt32, nsIntRect, nsIntPoint, nsIntSize, nsIntMargin> Super;
     194                 : 
     195                 :   // Constructors
     196             269 :   nsIntRect() : Super()
     197                 :   {
     198             269 :   }
     199              49 :   nsIntRect(const nsIntRect& aRect) : Super(aRect)
     200                 :   {
     201              49 :   }
     202              60 :   nsIntRect(const nsIntPoint& aOrigin, const nsIntSize &aSize) : Super(aOrigin, aSize)
     203                 :   {
     204              60 :   }
     205            1636 :   nsIntRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) :
     206            1636 :       Super(aX, aY, aWidth, aHeight)
     207                 :   {
     208            1636 :   }
     209                 : 
     210                 :   inline nsRect ToAppUnits(nscoord aAppUnitsPerPixel) const;
     211                 : 
     212                 :   // Returns a special nsIntRect that's used in some places to signify
     213                 :   // "all available space".
     214               0 :   static const nsIntRect& GetMaxSizedIntRect() { return kMaxSizedIntRect; }
     215                 : 
     216                 :   // This is here only to keep IPDL-generated code happy. DO NOT USE.
     217               0 :   bool operator==(const nsIntRect& aRect) const
     218                 :   {
     219               0 :     return IsEqualEdges(aRect);
     220                 :   }
     221                 : 
     222                 : protected:
     223                 :   static const nsIntRect kMaxSizedIntRect;
     224                 : };
     225                 : 
     226                 : /*
     227                 :  * App Unit/Pixel conversions
     228                 :  */
     229                 : 
     230                 : inline nsRect
     231               0 : nsRect::ConvertAppUnitsRoundOut(PRInt32 aFromAPP, PRInt32 aToAPP) const
     232                 : {
     233               0 :   if (aFromAPP == aToAPP) {
     234               0 :     return *this;
     235                 :   }
     236                 : 
     237               0 :   nsRect rect;
     238               0 :   nscoord right = NSToCoordCeil(NSCoordScale(XMost(), aFromAPP, aToAPP));
     239               0 :   nscoord bottom = NSToCoordCeil(NSCoordScale(YMost(), aFromAPP, aToAPP));
     240               0 :   rect.x = NSToCoordFloor(NSCoordScale(x, aFromAPP, aToAPP));
     241               0 :   rect.y = NSToCoordFloor(NSCoordScale(y, aFromAPP, aToAPP));
     242               0 :   rect.width = (right - rect.x);
     243               0 :   rect.height = (bottom - rect.y);
     244                 : 
     245               0 :   return rect;
     246                 : }
     247                 : 
     248                 : inline nsRect
     249               0 : nsRect::ConvertAppUnitsRoundIn(PRInt32 aFromAPP, PRInt32 aToAPP) const
     250                 : {
     251               0 :   if (aFromAPP == aToAPP) {
     252               0 :     return *this;
     253                 :   }
     254                 : 
     255               0 :   nsRect rect;
     256               0 :   nscoord right = NSToCoordFloor(NSCoordScale(XMost(), aFromAPP, aToAPP));
     257               0 :   nscoord bottom = NSToCoordFloor(NSCoordScale(YMost(), aFromAPP, aToAPP));
     258               0 :   rect.x = NSToCoordCeil(NSCoordScale(x, aFromAPP, aToAPP));
     259               0 :   rect.y = NSToCoordCeil(NSCoordScale(y, aFromAPP, aToAPP));
     260               0 :   rect.width = (right - rect.x);
     261               0 :   rect.height = (bottom - rect.y);
     262                 : 
     263               0 :   return rect;
     264                 : }
     265                 : 
     266                 : // scale the rect but round to preserve centers
     267                 : inline nsIntRect
     268               0 : nsRect::ScaleToNearestPixels(float aXScale, float aYScale,
     269                 :                              nscoord aAppUnitsPerPixel) const
     270                 : {
     271               0 :   nsIntRect rect;
     272               0 :   rect.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale);
     273               0 :   rect.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale);
     274                 :   rect.width  = NSToIntRoundUp(NSAppUnitsToDoublePixels(XMost(),
     275               0 :                                aAppUnitsPerPixel) * aXScale) - rect.x;
     276                 :   rect.height = NSToIntRoundUp(NSAppUnitsToDoublePixels(YMost(),
     277               0 :                                aAppUnitsPerPixel) * aYScale) - rect.y;
     278                 :   return rect;
     279                 : }
     280                 : 
     281                 : // scale the rect but round to smallest containing rect
     282                 : inline nsIntRect
     283               0 : nsRect::ScaleToOutsidePixels(float aXScale, float aYScale,
     284                 :                              nscoord aAppUnitsPerPixel) const
     285                 : {
     286               0 :   nsIntRect rect;
     287               0 :   rect.x = NSToIntFloor(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)) * aXScale);
     288               0 :   rect.y = NSToIntFloor(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)) * aYScale);
     289                 :   rect.width  = NSToIntCeil(NSAppUnitsToFloatPixels(XMost(),
     290               0 :                             float(aAppUnitsPerPixel)) * aXScale) - rect.x;
     291                 :   rect.height = NSToIntCeil(NSAppUnitsToFloatPixels(YMost(),
     292               0 :                             float(aAppUnitsPerPixel)) * aYScale) - rect.y;
     293                 :   return rect;
     294                 : }
     295                 : 
     296                 : // scale the rect but round to largest contained rect
     297                 : inline nsIntRect
     298               0 : nsRect::ScaleToInsidePixels(float aXScale, float aYScale,
     299                 :                             nscoord aAppUnitsPerPixel) const
     300                 : {
     301               0 :   nsIntRect rect;
     302               0 :   rect.x = NSToIntCeil(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)) * aXScale);
     303               0 :   rect.y = NSToIntCeil(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)) * aYScale);
     304                 :   rect.width  = NSToIntFloor(NSAppUnitsToFloatPixels(XMost(),
     305               0 :                              float(aAppUnitsPerPixel)) * aXScale) - rect.x;
     306                 :   rect.height = NSToIntFloor(NSAppUnitsToFloatPixels(YMost(),
     307               0 :                              float(aAppUnitsPerPixel)) * aYScale) - rect.y;
     308                 :   return rect;
     309                 : }
     310                 : 
     311                 : inline nsIntRect
     312               0 : nsRect::ToNearestPixels(nscoord aAppUnitsPerPixel) const
     313                 : {
     314               0 :   return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
     315                 : }
     316                 : 
     317                 : inline nsIntRect
     318               0 : nsRect::ToOutsidePixels(nscoord aAppUnitsPerPixel) const
     319                 : {
     320               0 :   return ScaleToOutsidePixels(1.0f, 1.0f, aAppUnitsPerPixel);
     321                 : }
     322                 : 
     323                 : inline nsIntRect
     324               0 : nsRect::ToInsidePixels(nscoord aAppUnitsPerPixel) const
     325                 : {
     326               0 :   return ScaleToInsidePixels(1.0f, 1.0f, aAppUnitsPerPixel);
     327                 : }
     328                 : 
     329                 : // app units are integer multiples of pixels, so no rounding needed
     330                 : inline nsRect
     331               0 : nsIntRect::ToAppUnits(nscoord aAppUnitsPerPixel) const
     332                 : {
     333                 :   return nsRect(NSIntPixelsToAppUnits(x, aAppUnitsPerPixel),
     334                 :                 NSIntPixelsToAppUnits(y, aAppUnitsPerPixel),
     335                 :                 NSIntPixelsToAppUnits(width, aAppUnitsPerPixel),
     336               0 :                 NSIntPixelsToAppUnits(height, aAppUnitsPerPixel));
     337                 : }
     338                 : 
     339                 : #ifdef DEBUG
     340                 : // Diagnostics
     341                 : extern NS_GFX FILE* operator<<(FILE* out, const nsRect& rect);
     342                 : #endif // DEBUG
     343                 : 
     344                 : #endif /* NSRECT_H */

Generated by: LCOV version 1.7