gfx/skia/trunk/include/core/SkRect.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkRect.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,792 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkRect_DEFINED
    1.14 +#define SkRect_DEFINED
    1.15 +
    1.16 +#include "SkPoint.h"
    1.17 +#include "SkSize.h"
    1.18 +
    1.19 +/** \struct SkIRect
    1.20 +
    1.21 +    SkIRect holds four 32 bit integer coordinates for a rectangle
    1.22 +*/
    1.23 +struct SK_API SkIRect {
    1.24 +    int32_t fLeft, fTop, fRight, fBottom;
    1.25 +
    1.26 +    static SkIRect SK_WARN_UNUSED_RESULT MakeEmpty() {
    1.27 +        SkIRect r;
    1.28 +        r.setEmpty();
    1.29 +        return r;
    1.30 +    }
    1.31 +
    1.32 +    static SkIRect SK_WARN_UNUSED_RESULT MakeLargest() {
    1.33 +        SkIRect r;
    1.34 +        r.setLargest();
    1.35 +        return r;
    1.36 +    }
    1.37 +
    1.38 +    static SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h) {
    1.39 +        SkIRect r;
    1.40 +        r.set(0, 0, w, h);
    1.41 +        return r;
    1.42 +    }
    1.43 +
    1.44 +    static SkIRect SK_WARN_UNUSED_RESULT MakeSize(const SkISize& size) {
    1.45 +        SkIRect r;
    1.46 +        r.set(0, 0, size.width(), size.height());
    1.47 +        return r;
    1.48 +    }
    1.49 +
    1.50 +    static SkIRect SK_WARN_UNUSED_RESULT MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b) {
    1.51 +        SkIRect rect;
    1.52 +        rect.set(l, t, r, b);
    1.53 +        return rect;
    1.54 +    }
    1.55 +
    1.56 +    static SkIRect SK_WARN_UNUSED_RESULT MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h) {
    1.57 +        SkIRect r;
    1.58 +        r.set(x, y, x + w, y + h);
    1.59 +        return r;
    1.60 +    }
    1.61 +
    1.62 +    int left() const { return fLeft; }
    1.63 +    int top() const { return fTop; }
    1.64 +    int right() const { return fRight; }
    1.65 +    int bottom() const { return fBottom; }
    1.66 +
    1.67 +    /** return the left edge of the rect */
    1.68 +    int x() const { return fLeft; }
    1.69 +    /** return the top edge of the rect */
    1.70 +    int y() const { return fTop; }
    1.71 +    /**
    1.72 +     *  Returns the rectangle's width. This does not check for a valid rect
    1.73 +     *  (i.e. left <= right) so the result may be negative.
    1.74 +     */
    1.75 +    int width() const { return fRight - fLeft; }
    1.76 +
    1.77 +    /**
    1.78 +     *  Returns the rectangle's height. This does not check for a valid rect
    1.79 +     *  (i.e. top <= bottom) so the result may be negative.
    1.80 +     */
    1.81 +    int height() const { return fBottom - fTop; }
    1.82 +
    1.83 +    /**
    1.84 +     *  Since the center of an integer rect may fall on a factional value, this
    1.85 +     *  method is defined to return (right + left) >> 1.
    1.86 +     *
    1.87 +     *  This is a specific "truncation" of the average, which is different than
    1.88 +     *  (right + left) / 2 when the sum is negative.
    1.89 +     */
    1.90 +    int centerX() const { return (fRight + fLeft) >> 1; }
    1.91 +
    1.92 +    /**
    1.93 +     *  Since the center of an integer rect may fall on a factional value, this
    1.94 +     *  method is defined to return (bottom + top) >> 1
    1.95 +     *
    1.96 +     *  This is a specific "truncation" of the average, which is different than
    1.97 +     *  (bottom + top) / 2 when the sum is negative.
    1.98 +     */
    1.99 +    int centerY() const { return (fBottom + fTop) >> 1; }
   1.100 +
   1.101 +    /**
   1.102 +     *  Return true if the rectangle's width or height are <= 0
   1.103 +     */
   1.104 +    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
   1.105 +
   1.106 +    bool isLargest() const { return SK_MinS32 == fLeft &&
   1.107 +                                    SK_MinS32 == fTop &&
   1.108 +                                    SK_MaxS32 == fRight &&
   1.109 +                                    SK_MaxS32 == fBottom; }
   1.110 +
   1.111 +    friend bool operator==(const SkIRect& a, const SkIRect& b) {
   1.112 +        return !memcmp(&a, &b, sizeof(a));
   1.113 +    }
   1.114 +
   1.115 +    friend bool operator!=(const SkIRect& a, const SkIRect& b) {
   1.116 +        return !(a == b);
   1.117 +    }
   1.118 +
   1.119 +    bool is16Bit() const {
   1.120 +        return  SkIsS16(fLeft) && SkIsS16(fTop) &&
   1.121 +                SkIsS16(fRight) && SkIsS16(fBottom);
   1.122 +    }
   1.123 +
   1.124 +    /** Set the rectangle to (0,0,0,0)
   1.125 +    */
   1.126 +    void setEmpty() { memset(this, 0, sizeof(*this)); }
   1.127 +
   1.128 +    void set(int32_t left, int32_t top, int32_t right, int32_t bottom) {
   1.129 +        fLeft   = left;
   1.130 +        fTop    = top;
   1.131 +        fRight  = right;
   1.132 +        fBottom = bottom;
   1.133 +    }
   1.134 +    // alias for set(l, t, r, b)
   1.135 +    void setLTRB(int32_t left, int32_t top, int32_t right, int32_t bottom) {
   1.136 +        this->set(left, top, right, bottom);
   1.137 +    }
   1.138 +
   1.139 +    void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) {
   1.140 +        fLeft = x;
   1.141 +        fTop = y;
   1.142 +        fRight = x + width;
   1.143 +        fBottom = y + height;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     *  Make the largest representable rectangle
   1.148 +     */
   1.149 +    void setLargest() {
   1.150 +        fLeft = fTop = SK_MinS32;
   1.151 +        fRight = fBottom = SK_MaxS32;
   1.152 +    }
   1.153 +
   1.154 +    /**
   1.155 +     *  Make the largest representable rectangle, but inverted (e.g. fLeft will
   1.156 +     *  be max 32bit and right will be min 32bit).
   1.157 +     */
   1.158 +    void setLargestInverted() {
   1.159 +        fLeft = fTop = SK_MaxS32;
   1.160 +        fRight = fBottom = SK_MinS32;
   1.161 +    }
   1.162 +
   1.163 +    /** Offset set the rectangle by adding dx to its left and right,
   1.164 +        and adding dy to its top and bottom.
   1.165 +    */
   1.166 +    void offset(int32_t dx, int32_t dy) {
   1.167 +        fLeft   += dx;
   1.168 +        fTop    += dy;
   1.169 +        fRight  += dx;
   1.170 +        fBottom += dy;
   1.171 +    }
   1.172 +
   1.173 +    void offset(const SkIPoint& delta) {
   1.174 +        this->offset(delta.fX, delta.fY);
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     *  Offset this rect such its new x() and y() will equal newX and newY.
   1.179 +     */
   1.180 +    void offsetTo(int32_t newX, int32_t newY) {
   1.181 +        fRight += newX - fLeft;
   1.182 +        fBottom += newY - fTop;
   1.183 +        fLeft = newX;
   1.184 +        fTop = newY;
   1.185 +    }
   1.186 +
   1.187 +    /** Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards,
   1.188 +        making the rectangle narrower. If dx is negative, then the sides are moved outwards,
   1.189 +        making the rectangle wider. The same holds true for dy and the top and bottom.
   1.190 +    */
   1.191 +    void inset(int32_t dx, int32_t dy) {
   1.192 +        fLeft   += dx;
   1.193 +        fTop    += dy;
   1.194 +        fRight  -= dx;
   1.195 +        fBottom -= dy;
   1.196 +    }
   1.197 +
   1.198 +   /** Outset the rectangle by (dx,dy). If dx is positive, then the sides are
   1.199 +       moved outwards, making the rectangle wider. If dx is negative, then the
   1.200 +       sides are moved inwards, making the rectangle narrower. The same holds
   1.201 +       true for dy and the top and bottom.
   1.202 +    */
   1.203 +    void outset(int32_t dx, int32_t dy)  { this->inset(-dx, -dy); }
   1.204 +
   1.205 +    bool quickReject(int l, int t, int r, int b) const {
   1.206 +        return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b;
   1.207 +    }
   1.208 +
   1.209 +    /** Returns true if (x,y) is inside the rectangle and the rectangle is not
   1.210 +        empty. The left and top are considered to be inside, while the right
   1.211 +        and bottom are not. Thus for the rectangle (0, 0, 5, 10), the
   1.212 +        points (0,0) and (0,9) are inside, while (-1,0) and (5,9) are not.
   1.213 +    */
   1.214 +    bool contains(int32_t x, int32_t y) const {
   1.215 +        return  (unsigned)(x - fLeft) < (unsigned)(fRight - fLeft) &&
   1.216 +                (unsigned)(y - fTop) < (unsigned)(fBottom - fTop);
   1.217 +    }
   1.218 +
   1.219 +    /** Returns true if the 4 specified sides of a rectangle are inside or equal to this rectangle.
   1.220 +        If either rectangle is empty, contains() returns false.
   1.221 +    */
   1.222 +    bool contains(int32_t left, int32_t top, int32_t right, int32_t bottom) const {
   1.223 +        return  left < right && top < bottom && !this->isEmpty() && // check for empties
   1.224 +                fLeft <= left && fTop <= top &&
   1.225 +                fRight >= right && fBottom >= bottom;
   1.226 +    }
   1.227 +
   1.228 +    /** Returns true if the specified rectangle r is inside or equal to this rectangle.
   1.229 +    */
   1.230 +    bool contains(const SkIRect& r) const {
   1.231 +        return  !r.isEmpty() && !this->isEmpty() &&     // check for empties
   1.232 +                fLeft <= r.fLeft && fTop <= r.fTop &&
   1.233 +                fRight >= r.fRight && fBottom >= r.fBottom;
   1.234 +    }
   1.235 +
   1.236 +    /** Return true if this rectangle contains the specified rectangle.
   1.237 +        For speed, this method does not check if either this or the specified
   1.238 +        rectangles are empty, and if either is, its return value is undefined.
   1.239 +        In the debugging build however, we assert that both this and the
   1.240 +        specified rectangles are non-empty.
   1.241 +    */
   1.242 +    bool containsNoEmptyCheck(int32_t left, int32_t top,
   1.243 +                              int32_t right, int32_t bottom) const {
   1.244 +        SkASSERT(fLeft < fRight && fTop < fBottom);
   1.245 +        SkASSERT(left < right && top < bottom);
   1.246 +
   1.247 +        return fLeft <= left && fTop <= top &&
   1.248 +               fRight >= right && fBottom >= bottom;
   1.249 +    }
   1.250 +
   1.251 +    bool containsNoEmptyCheck(const SkIRect& r) const {
   1.252 +        return containsNoEmptyCheck(r.fLeft, r.fTop, r.fRight, r.fBottom);
   1.253 +    }
   1.254 +
   1.255 +    /** If r intersects this rectangle, return true and set this rectangle to that
   1.256 +        intersection, otherwise return false and do not change this rectangle.
   1.257 +        If either rectangle is empty, do nothing and return false.
   1.258 +    */
   1.259 +    bool intersect(const SkIRect& r) {
   1.260 +        SkASSERT(&r);
   1.261 +        return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
   1.262 +    }
   1.263 +
   1.264 +    /** If rectangles a and b intersect, return true and set this rectangle to
   1.265 +        that intersection, otherwise return false and do not change this
   1.266 +        rectangle. If either rectangle is empty, do nothing and return false.
   1.267 +    */
   1.268 +    bool intersect(const SkIRect& a, const SkIRect& b) {
   1.269 +        SkASSERT(&a && &b);
   1.270 +
   1.271 +        if (!a.isEmpty() && !b.isEmpty() &&
   1.272 +                a.fLeft < b.fRight && b.fLeft < a.fRight &&
   1.273 +                a.fTop < b.fBottom && b.fTop < a.fBottom) {
   1.274 +            fLeft   = SkMax32(a.fLeft,   b.fLeft);
   1.275 +            fTop    = SkMax32(a.fTop,    b.fTop);
   1.276 +            fRight  = SkMin32(a.fRight,  b.fRight);
   1.277 +            fBottom = SkMin32(a.fBottom, b.fBottom);
   1.278 +            return true;
   1.279 +        }
   1.280 +        return false;
   1.281 +    }
   1.282 +
   1.283 +    /** If rectangles a and b intersect, return true and set this rectangle to
   1.284 +        that intersection, otherwise return false and do not change this
   1.285 +        rectangle. For speed, no check to see if a or b are empty is performed.
   1.286 +        If either is, then the return result is undefined. In the debug build,
   1.287 +        we assert that both rectangles are non-empty.
   1.288 +    */
   1.289 +    bool intersectNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
   1.290 +        SkASSERT(&a && &b);
   1.291 +        SkASSERT(!a.isEmpty() && !b.isEmpty());
   1.292 +
   1.293 +        if (a.fLeft < b.fRight && b.fLeft < a.fRight &&
   1.294 +                a.fTop < b.fBottom && b.fTop < a.fBottom) {
   1.295 +            fLeft   = SkMax32(a.fLeft,   b.fLeft);
   1.296 +            fTop    = SkMax32(a.fTop,    b.fTop);
   1.297 +            fRight  = SkMin32(a.fRight,  b.fRight);
   1.298 +            fBottom = SkMin32(a.fBottom, b.fBottom);
   1.299 +            return true;
   1.300 +        }
   1.301 +        return false;
   1.302 +    }
   1.303 +
   1.304 +    /** If the rectangle specified by left,top,right,bottom intersects this rectangle,
   1.305 +        return true and set this rectangle to that intersection,
   1.306 +        otherwise return false and do not change this rectangle.
   1.307 +        If either rectangle is empty, do nothing and return false.
   1.308 +    */
   1.309 +    bool intersect(int32_t left, int32_t top, int32_t right, int32_t bottom) {
   1.310 +        if (left < right && top < bottom && !this->isEmpty() &&
   1.311 +                fLeft < right && left < fRight && fTop < bottom && top < fBottom) {
   1.312 +            if (fLeft < left) fLeft = left;
   1.313 +            if (fTop < top) fTop = top;
   1.314 +            if (fRight > right) fRight = right;
   1.315 +            if (fBottom > bottom) fBottom = bottom;
   1.316 +            return true;
   1.317 +        }
   1.318 +        return false;
   1.319 +    }
   1.320 +
   1.321 +    /** Returns true if a and b are not empty, and they intersect
   1.322 +     */
   1.323 +    static bool Intersects(const SkIRect& a, const SkIRect& b) {
   1.324 +        return  !a.isEmpty() && !b.isEmpty() &&              // check for empties
   1.325 +        a.fLeft < b.fRight && b.fLeft < a.fRight &&
   1.326 +        a.fTop < b.fBottom && b.fTop < a.fBottom;
   1.327 +    }
   1.328 +
   1.329 +    /**
   1.330 +     *  Returns true if a and b intersect. debug-asserts that neither are empty.
   1.331 +     */
   1.332 +    static bool IntersectsNoEmptyCheck(const SkIRect& a, const SkIRect& b) {
   1.333 +        SkASSERT(!a.isEmpty());
   1.334 +        SkASSERT(!b.isEmpty());
   1.335 +        return  a.fLeft < b.fRight && b.fLeft < a.fRight &&
   1.336 +                a.fTop < b.fBottom && b.fTop < a.fBottom;
   1.337 +    }
   1.338 +
   1.339 +    /** Update this rectangle to enclose itself and the specified rectangle.
   1.340 +        If this rectangle is empty, just set it to the specified rectangle. If the specified
   1.341 +        rectangle is empty, do nothing.
   1.342 +    */
   1.343 +    void join(int32_t left, int32_t top, int32_t right, int32_t bottom);
   1.344 +
   1.345 +    /** Update this rectangle to enclose itself and the specified rectangle.
   1.346 +        If this rectangle is empty, just set it to the specified rectangle. If the specified
   1.347 +        rectangle is empty, do nothing.
   1.348 +    */
   1.349 +    void join(const SkIRect& r) {
   1.350 +        this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
   1.351 +    }
   1.352 +
   1.353 +    /** Swap top/bottom or left/right if there are flipped.
   1.354 +        This can be called if the edges are computed separately,
   1.355 +        and may have crossed over each other.
   1.356 +        When this returns, left <= right && top <= bottom
   1.357 +    */
   1.358 +    void sort();
   1.359 +
   1.360 +    static const SkIRect& SK_WARN_UNUSED_RESULT EmptyIRect() {
   1.361 +        static const SkIRect gEmpty = { 0, 0, 0, 0 };
   1.362 +        return gEmpty;
   1.363 +    }
   1.364 +};
   1.365 +
   1.366 +/** \struct SkRect
   1.367 +*/
   1.368 +struct SK_API SkRect {
   1.369 +    SkScalar    fLeft, fTop, fRight, fBottom;
   1.370 +
   1.371 +    static SkRect SK_WARN_UNUSED_RESULT MakeEmpty() {
   1.372 +        SkRect r;
   1.373 +        r.setEmpty();
   1.374 +        return r;
   1.375 +    }
   1.376 +
   1.377 +    static SkRect SK_WARN_UNUSED_RESULT MakeLargest() {
   1.378 +        SkRect r;
   1.379 +        r.setLargest();
   1.380 +        return r;
   1.381 +    }
   1.382 +
   1.383 +    static SkRect SK_WARN_UNUSED_RESULT MakeWH(SkScalar w, SkScalar h) {
   1.384 +        SkRect r;
   1.385 +        r.set(0, 0, w, h);
   1.386 +        return r;
   1.387 +    }
   1.388 +
   1.389 +    static SkRect SK_WARN_UNUSED_RESULT MakeSize(const SkSize& size) {
   1.390 +        SkRect r;
   1.391 +        r.set(0, 0, size.width(), size.height());
   1.392 +        return r;
   1.393 +    }
   1.394 +
   1.395 +    static SkRect SK_WARN_UNUSED_RESULT MakeLTRB(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
   1.396 +        SkRect rect;
   1.397 +        rect.set(l, t, r, b);
   1.398 +        return rect;
   1.399 +    }
   1.400 +
   1.401 +    static SkRect SK_WARN_UNUSED_RESULT MakeXYWH(SkScalar x, SkScalar y, SkScalar w, SkScalar h) {
   1.402 +        SkRect r;
   1.403 +        r.set(x, y, x + w, y + h);
   1.404 +        return r;
   1.405 +    }
   1.406 +
   1.407 +    SK_ATTR_DEPRECATED("use Make()")
   1.408 +    static SkRect SK_WARN_UNUSED_RESULT MakeFromIRect(const SkIRect& irect) {
   1.409 +        SkRect r;
   1.410 +        r.set(SkIntToScalar(irect.fLeft),
   1.411 +              SkIntToScalar(irect.fTop),
   1.412 +              SkIntToScalar(irect.fRight),
   1.413 +              SkIntToScalar(irect.fBottom));
   1.414 +        return r;
   1.415 +    }
   1.416 +
   1.417 +    static SkRect SK_WARN_UNUSED_RESULT Make(const SkIRect& irect) {
   1.418 +        SkRect r;
   1.419 +        r.set(SkIntToScalar(irect.fLeft),
   1.420 +              SkIntToScalar(irect.fTop),
   1.421 +              SkIntToScalar(irect.fRight),
   1.422 +              SkIntToScalar(irect.fBottom));
   1.423 +        return r;
   1.424 +    }
   1.425 +
   1.426 +    /**
   1.427 +     *  Return true if the rectangle's width or height are <= 0
   1.428 +     */
   1.429 +    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
   1.430 +
   1.431 +    bool isLargest() const { return SK_ScalarMin == fLeft &&
   1.432 +                                    SK_ScalarMin == fTop &&
   1.433 +                                    SK_ScalarMax == fRight &&
   1.434 +                                    SK_ScalarMax == fBottom; }
   1.435 +
   1.436 +    /**
   1.437 +     *  Returns true iff all values in the rect are finite. If any are
   1.438 +     *  infinite or NaN (or SK_FixedNaN when SkScalar is fixed) then this
   1.439 +     *  returns false.
   1.440 +     */
   1.441 +    bool isFinite() const {
   1.442 +        float accum = 0;
   1.443 +        accum *= fLeft;
   1.444 +        accum *= fTop;
   1.445 +        accum *= fRight;
   1.446 +        accum *= fBottom;
   1.447 +
   1.448 +        // accum is either NaN or it is finite (zero).
   1.449 +        SkASSERT(0 == accum || !(accum == accum));
   1.450 +
   1.451 +        // value==value will be true iff value is not NaN
   1.452 +        // TODO: is it faster to say !accum or accum==accum?
   1.453 +        return accum == accum;
   1.454 +    }
   1.455 +
   1.456 +    SkScalar    x() const { return fLeft; }
   1.457 +    SkScalar    y() const { return fTop; }
   1.458 +    SkScalar    left() const { return fLeft; }
   1.459 +    SkScalar    top() const { return fTop; }
   1.460 +    SkScalar    right() const { return fRight; }
   1.461 +    SkScalar    bottom() const { return fBottom; }
   1.462 +    SkScalar    width() const { return fRight - fLeft; }
   1.463 +    SkScalar    height() const { return fBottom - fTop; }
   1.464 +    SkScalar    centerX() const { return SkScalarHalf(fLeft + fRight); }
   1.465 +    SkScalar    centerY() const { return SkScalarHalf(fTop + fBottom); }
   1.466 +
   1.467 +    friend bool operator==(const SkRect& a, const SkRect& b) {
   1.468 +        return SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4);
   1.469 +    }
   1.470 +
   1.471 +    friend bool operator!=(const SkRect& a, const SkRect& b) {
   1.472 +        return !SkScalarsEqual((SkScalar*)&a, (SkScalar*)&b, 4);
   1.473 +    }
   1.474 +
   1.475 +    /** return the 4 points that enclose the rectangle (top-left, top-right, bottom-right,
   1.476 +        bottom-left). TODO: Consider adding param to control whether quad is CW or CCW.
   1.477 +     */
   1.478 +    void toQuad(SkPoint quad[4]) const;
   1.479 +
   1.480 +    /** Set this rectangle to the empty rectangle (0,0,0,0)
   1.481 +    */
   1.482 +    void setEmpty() { memset(this, 0, sizeof(*this)); }
   1.483 +
   1.484 +    void set(const SkIRect& src) {
   1.485 +        fLeft   = SkIntToScalar(src.fLeft);
   1.486 +        fTop    = SkIntToScalar(src.fTop);
   1.487 +        fRight  = SkIntToScalar(src.fRight);
   1.488 +        fBottom = SkIntToScalar(src.fBottom);
   1.489 +    }
   1.490 +
   1.491 +    void set(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
   1.492 +        fLeft   = left;
   1.493 +        fTop    = top;
   1.494 +        fRight  = right;
   1.495 +        fBottom = bottom;
   1.496 +    }
   1.497 +    // alias for set(l, t, r, b)
   1.498 +    void setLTRB(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
   1.499 +        this->set(left, top, right, bottom);
   1.500 +    }
   1.501 +
   1.502 +    /** Initialize the rect with the 4 specified integers. The routine handles
   1.503 +        converting them to scalars (by calling SkIntToScalar)
   1.504 +     */
   1.505 +    void iset(int left, int top, int right, int bottom) {
   1.506 +        fLeft   = SkIntToScalar(left);
   1.507 +        fTop    = SkIntToScalar(top);
   1.508 +        fRight  = SkIntToScalar(right);
   1.509 +        fBottom = SkIntToScalar(bottom);
   1.510 +    }
   1.511 +
   1.512 +    /**
   1.513 +     *  Set this rectangle to be left/top at 0,0, and have the specified width
   1.514 +     *  and height (automatically converted to SkScalar).
   1.515 +     */
   1.516 +    void isetWH(int width, int height) {
   1.517 +        fLeft = fTop = 0;
   1.518 +        fRight = SkIntToScalar(width);
   1.519 +        fBottom = SkIntToScalar(height);
   1.520 +    }
   1.521 +
   1.522 +    /** Set this rectangle to be the bounds of the array of points.
   1.523 +        If the array is empty (count == 0), then set this rectangle
   1.524 +        to the empty rectangle (0,0,0,0)
   1.525 +    */
   1.526 +    void set(const SkPoint pts[], int count) {
   1.527 +        // set() had been checking for non-finite values, so keep that behavior
   1.528 +        // for now. Now that we have setBoundsCheck(), we may decide to make
   1.529 +        // set() be simpler/faster, and not check for those.
   1.530 +        (void)this->setBoundsCheck(pts, count);
   1.531 +    }
   1.532 +
   1.533 +    // alias for set(pts, count)
   1.534 +    void setBounds(const SkPoint pts[], int count) {
   1.535 +        (void)this->setBoundsCheck(pts, count);
   1.536 +    }
   1.537 +
   1.538 +    /**
   1.539 +     *  Compute the bounds of the array of points, and set this rect to that
   1.540 +     *  bounds and return true... unless a non-finite value is encountered,
   1.541 +     *  in which case this rect is set to empty and false is returned.
   1.542 +     */
   1.543 +    bool setBoundsCheck(const SkPoint pts[], int count);
   1.544 +
   1.545 +    void set(const SkPoint& p0, const SkPoint& p1) {
   1.546 +        fLeft =   SkMinScalar(p0.fX, p1.fX);
   1.547 +        fRight =  SkMaxScalar(p0.fX, p1.fX);
   1.548 +        fTop =    SkMinScalar(p0.fY, p1.fY);
   1.549 +        fBottom = SkMaxScalar(p0.fY, p1.fY);
   1.550 +    }
   1.551 +
   1.552 +    void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) {
   1.553 +        fLeft = x;
   1.554 +        fTop = y;
   1.555 +        fRight = x + width;
   1.556 +        fBottom = y + height;
   1.557 +    }
   1.558 +
   1.559 +    void setWH(SkScalar width, SkScalar height) {
   1.560 +        fLeft = 0;
   1.561 +        fTop = 0;
   1.562 +        fRight = width;
   1.563 +        fBottom = height;
   1.564 +    }
   1.565 +
   1.566 +    /**
   1.567 +     *  Make the largest representable rectangle
   1.568 +     */
   1.569 +    void setLargest() {
   1.570 +        fLeft = fTop = SK_ScalarMin;
   1.571 +        fRight = fBottom = SK_ScalarMax;
   1.572 +    }
   1.573 +
   1.574 +    /**
   1.575 +     *  Make the largest representable rectangle, but inverted (e.g. fLeft will
   1.576 +     *  be max and right will be min).
   1.577 +     */
   1.578 +    void setLargestInverted() {
   1.579 +        fLeft = fTop = SK_ScalarMax;
   1.580 +        fRight = fBottom = SK_ScalarMin;
   1.581 +    }
   1.582 +
   1.583 +    /** Offset set the rectangle by adding dx to its left and right,
   1.584 +        and adding dy to its top and bottom.
   1.585 +    */
   1.586 +    void offset(SkScalar dx, SkScalar dy) {
   1.587 +        fLeft   += dx;
   1.588 +        fTop    += dy;
   1.589 +        fRight  += dx;
   1.590 +        fBottom += dy;
   1.591 +    }
   1.592 +
   1.593 +    void offset(const SkPoint& delta) {
   1.594 +        this->offset(delta.fX, delta.fY);
   1.595 +    }
   1.596 +
   1.597 +    /**
   1.598 +     *  Offset this rect such its new x() and y() will equal newX and newY.
   1.599 +     */
   1.600 +    void offsetTo(SkScalar newX, SkScalar newY) {
   1.601 +        fRight += newX - fLeft;
   1.602 +        fBottom += newY - fTop;
   1.603 +        fLeft = newX;
   1.604 +        fTop = newY;
   1.605 +    }
   1.606 +
   1.607 +    /** Inset the rectangle by (dx,dy). If dx is positive, then the sides are
   1.608 +        moved inwards, making the rectangle narrower. If dx is negative, then
   1.609 +        the sides are moved outwards, making the rectangle wider. The same holds
   1.610 +         true for dy and the top and bottom.
   1.611 +    */
   1.612 +    void inset(SkScalar dx, SkScalar dy)  {
   1.613 +        fLeft   += dx;
   1.614 +        fTop    += dy;
   1.615 +        fRight  -= dx;
   1.616 +        fBottom -= dy;
   1.617 +    }
   1.618 +
   1.619 +   /** Outset the rectangle by (dx,dy). If dx is positive, then the sides are
   1.620 +       moved outwards, making the rectangle wider. If dx is negative, then the
   1.621 +       sides are moved inwards, making the rectangle narrower. The same holds
   1.622 +       true for dy and the top and bottom.
   1.623 +    */
   1.624 +    void outset(SkScalar dx, SkScalar dy)  { this->inset(-dx, -dy); }
   1.625 +
   1.626 +    /** If this rectangle intersects r, return true and set this rectangle to that
   1.627 +        intersection, otherwise return false and do not change this rectangle.
   1.628 +        If either rectangle is empty, do nothing and return false.
   1.629 +    */
   1.630 +    bool intersect(const SkRect& r);
   1.631 +    bool intersect2(const SkRect& r);
   1.632 +
   1.633 +    /** If this rectangle intersects the rectangle specified by left, top, right, bottom,
   1.634 +        return true and set this rectangle to that intersection, otherwise return false
   1.635 +        and do not change this rectangle.
   1.636 +        If either rectangle is empty, do nothing and return false.
   1.637 +    */
   1.638 +    bool intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
   1.639 +
   1.640 +    /**
   1.641 +     *  Return true if this rectangle is not empty, and the specified sides of
   1.642 +     *  a rectangle are not empty, and they intersect.
   1.643 +     */
   1.644 +    bool intersects(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) const {
   1.645 +        return // first check that both are not empty
   1.646 +               left < right && top < bottom &&
   1.647 +               fLeft < fRight && fTop < fBottom &&
   1.648 +               // now check for intersection
   1.649 +               fLeft < right && left < fRight &&
   1.650 +               fTop < bottom && top < fBottom;
   1.651 +    }
   1.652 +
   1.653 +    /** If rectangles a and b intersect, return true and set this rectangle to
   1.654 +     *  that intersection, otherwise return false and do not change this
   1.655 +     *  rectangle. If either rectangle is empty, do nothing and return false.
   1.656 +     */
   1.657 +    bool intersect(const SkRect& a, const SkRect& b);
   1.658 +
   1.659 +    /**
   1.660 +     *  Return true if rectangles a and b are not empty and intersect.
   1.661 +     */
   1.662 +    static bool Intersects(const SkRect& a, const SkRect& b) {
   1.663 +        return  !a.isEmpty() && !b.isEmpty() &&
   1.664 +                a.fLeft < b.fRight && b.fLeft < a.fRight &&
   1.665 +                a.fTop < b.fBottom && b.fTop < a.fBottom;
   1.666 +    }
   1.667 +
   1.668 +    /**
   1.669 +     *  Update this rectangle to enclose itself and the specified rectangle.
   1.670 +     *  If this rectangle is empty, just set it to the specified rectangle.
   1.671 +     *  If the specified rectangle is empty, do nothing.
   1.672 +     */
   1.673 +    void join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom);
   1.674 +
   1.675 +    /** Update this rectangle to enclose itself and the specified rectangle.
   1.676 +        If this rectangle is empty, just set it to the specified rectangle. If the specified
   1.677 +        rectangle is empty, do nothing.
   1.678 +    */
   1.679 +    void join(const SkRect& r) {
   1.680 +        this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
   1.681 +    }
   1.682 +    // alias for join()
   1.683 +    void growToInclude(const SkRect& r) { this->join(r); }
   1.684 +
   1.685 +    /**
   1.686 +     *  Grow the rect to include the specified (x,y). After this call, the
   1.687 +     *  following will be true: fLeft <= x <= fRight && fTop <= y <= fBottom.
   1.688 +     *
   1.689 +     *  This is close, but not quite the same contract as contains(), since
   1.690 +     *  contains() treats the left and top different from the right and bottom.
   1.691 +     *  contains(x,y) -> fLeft <= x < fRight && fTop <= y < fBottom. Also note
   1.692 +     *  that contains(x,y) always returns false if the rect is empty.
   1.693 +     */
   1.694 +    void growToInclude(SkScalar x, SkScalar y) {
   1.695 +        fLeft  = SkMinScalar(x, fLeft);
   1.696 +        fRight = SkMaxScalar(x, fRight);
   1.697 +        fTop    = SkMinScalar(y, fTop);
   1.698 +        fBottom = SkMaxScalar(y, fBottom);
   1.699 +    }
   1.700 +
   1.701 +    /** Bulk version of growToInclude */
   1.702 +    void growToInclude(const SkPoint pts[], int count) {
   1.703 +        this->growToInclude(pts, sizeof(SkPoint), count);
   1.704 +    }
   1.705 +
   1.706 +    /** Bulk version of growToInclude with stride. */
   1.707 +    void growToInclude(const SkPoint pts[], size_t stride, int count) {
   1.708 +        SkASSERT(count >= 0);
   1.709 +        SkASSERT(stride >= sizeof(SkPoint));
   1.710 +        const SkPoint* end = (const SkPoint*)((intptr_t)pts + count * stride);
   1.711 +        for (; pts < end; pts = (const SkPoint*)((intptr_t)pts + stride)) {
   1.712 +            this->growToInclude(pts->fX, pts->fY);
   1.713 +        }
   1.714 +    }
   1.715 +
   1.716 +    /**
   1.717 +     *  Return true if this rectangle contains r, and if both rectangles are
   1.718 +     *  not empty.
   1.719 +     */
   1.720 +    bool contains(const SkRect& r) const {
   1.721 +        // todo: can we eliminate the this->isEmpty check?
   1.722 +        return  !r.isEmpty() && !this->isEmpty() &&
   1.723 +                fLeft <= r.fLeft && fTop <= r.fTop &&
   1.724 +                fRight >= r.fRight && fBottom >= r.fBottom;
   1.725 +    }
   1.726 +
   1.727 +    /**
   1.728 +     *  Set the dst rectangle by rounding this rectangle's coordinates to their
   1.729 +     *  nearest integer values using SkScalarRoundToInt.
   1.730 +     */
   1.731 +    void round(SkIRect* dst) const {
   1.732 +        SkASSERT(dst);
   1.733 +        dst->set(SkScalarRoundToInt(fLeft), SkScalarRoundToInt(fTop),
   1.734 +                 SkScalarRoundToInt(fRight), SkScalarRoundToInt(fBottom));
   1.735 +    }
   1.736 +
   1.737 +    /**
   1.738 +     *  Set the dst rectangle by rounding "out" this rectangle, choosing the
   1.739 +     *  SkScalarFloor of top and left, and the SkScalarCeil of right and bottom.
   1.740 +     */
   1.741 +    void roundOut(SkIRect* dst) const {
   1.742 +        SkASSERT(dst);
   1.743 +        dst->set(SkScalarFloorToInt(fLeft), SkScalarFloorToInt(fTop),
   1.744 +                 SkScalarCeilToInt(fRight), SkScalarCeilToInt(fBottom));
   1.745 +    }
   1.746 +
   1.747 +    /**
   1.748 +     *  Expand this rectangle by rounding its coordinates "out", choosing the
   1.749 +     *  floor of top and left, and the ceil of right and bottom. If this rect
   1.750 +     *  is already on integer coordinates, then it will be unchanged.
   1.751 +     */
   1.752 +    void roundOut() {
   1.753 +        this->set(SkScalarFloorToScalar(fLeft),
   1.754 +                  SkScalarFloorToScalar(fTop),
   1.755 +                  SkScalarCeilToScalar(fRight),
   1.756 +                  SkScalarCeilToScalar(fBottom));
   1.757 +    }
   1.758 +
   1.759 +    /**
   1.760 +     *  Set the dst rectangle by rounding "in" this rectangle, choosing the
   1.761 +     *  ceil of top and left, and the floor of right and bottom. This does *not*
   1.762 +     *  call sort(), so it is possible that the resulting rect is inverted...
   1.763 +     *  e.g. left >= right or top >= bottom. Call isEmpty() to detect that.
   1.764 +     */
   1.765 +    void roundIn(SkIRect* dst) const {
   1.766 +        SkASSERT(dst);
   1.767 +        dst->set(SkScalarCeilToInt(fLeft), SkScalarCeilToInt(fTop),
   1.768 +                 SkScalarFloorToInt(fRight), SkScalarFloorToInt(fBottom));
   1.769 +    }
   1.770 +
   1.771 +    /**
   1.772 +     *  Return a new SkIRect which is contains the rounded coordinates of this
   1.773 +     *  rect using SkScalarRoundToInt.
   1.774 +     */
   1.775 +    SkIRect round() const {
   1.776 +        SkIRect ir;
   1.777 +        this->round(&ir);
   1.778 +        return ir;
   1.779 +    }
   1.780 +
   1.781 +    /**
   1.782 +     *  Swap top/bottom or left/right if there are flipped (i.e. if width()
   1.783 +     *  or height() would have returned a negative value.) This should be called
   1.784 +     *  if the edges are computed separately, and may have crossed over each
   1.785 +     *  other. When this returns, left <= right && top <= bottom
   1.786 +     */
   1.787 +    void sort();
   1.788 +
   1.789 +    /**
   1.790 +     *  cast-safe way to treat the rect as an array of (4) SkScalars.
   1.791 +     */
   1.792 +    const SkScalar* asScalars() const { return &fLeft; }
   1.793 +};
   1.794 +
   1.795 +#endif

mercurial