Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkRect.cpp
1
2 /*
3  * Copyright 2006 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10 #include "SkRect.h"
11
12 void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
13     // do nothing if the params are empty
14     if (left >= right || top >= bottom) {
15         return;
16     }
17
18     // if we are empty, just assign
19     if (fLeft >= fRight || fTop >= fBottom) {
20         this->set(left, top, right, bottom);
21     } else {
22         if (left < fLeft) fLeft = left;
23         if (top < fTop) fTop = top;
24         if (right > fRight) fRight = right;
25         if (bottom > fBottom) fBottom = bottom;
26     }
27 }
28
29 void SkIRect::sort() {
30     if (fLeft > fRight) {
31         SkTSwap<int32_t>(fLeft, fRight);
32     }
33     if (fTop > fBottom) {
34         SkTSwap<int32_t>(fTop, fBottom);
35     }
36 }
37
38 /////////////////////////////////////////////////////////////////////////////
39
40 void SkRect::toQuad(SkPoint quad[4]) const {
41     SkASSERT(quad);
42
43     quad[0].set(fLeft, fTop);
44     quad[1].set(fRight, fTop);
45     quad[2].set(fRight, fBottom);
46     quad[3].set(fLeft, fBottom);
47 }
48
49 bool SkRect::setBoundsCheck(const SkPoint pts[], int count) {
50     SkASSERT((pts && count > 0) || count == 0);
51
52     bool isFinite = true;
53
54     if (count <= 0) {
55         sk_bzero(this, sizeof(SkRect));
56     } else {
57         SkScalar    l, t, r, b;
58
59         l = r = pts[0].fX;
60         t = b = pts[0].fY;
61
62         // If all of the points are finite, accum should stay 0. If we encounter
63         // a NaN or infinity, then accum should become NaN.
64         float accum = 0;
65         accum *= l; accum *= t;
66
67         for (int i = 1; i < count; i++) {
68             SkScalar x = pts[i].fX;
69             SkScalar y = pts[i].fY;
70
71             accum *= x; accum *= y;
72
73             // we use if instead of if/else, so we can generate min/max
74             // float instructions (at least on SSE)
75             if (x < l) l = x;
76             if (x > r) r = x;
77
78             if (y < t) t = y;
79             if (y > b) b = y;
80         }
81
82         SkASSERT(!accum || !SkScalarIsFinite(accum));
83         if (accum) {
84             l = t = r = b = 0;
85             isFinite = false;
86         }
87         this->set(l, t, r, b);
88     }
89
90     return isFinite;
91 }
92
93 #define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb) \
94     SkScalar L = SkMaxScalar(al, bl);                   \
95     SkScalar R = SkMinScalar(ar, br);                   \
96     SkScalar T = SkMaxScalar(at, bt);                   \
97     SkScalar B = SkMinScalar(ab, bb);                   \
98     do { if (L >= R || T >= B) return false; } while (0)
99
100 bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
101     CHECK_INTERSECT(left, top, right, bottom, fLeft, fTop, fRight, fBottom);
102     this->setLTRB(L, T, R, B);
103     return true;
104 }
105
106 bool SkRect::intersect(const SkRect& r) {
107     return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
108 }
109
110 bool SkRect::intersect(const SkRect& a, const SkRect& b) {
111     CHECK_INTERSECT(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
112     this->setLTRB(L, T, R, B);
113     return true;
114 }
115
116 void SkRect::join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
117     // do nothing if the params are empty
118     if (left >= right || top >= bottom) {
119         return;
120     }
121
122     // if we are empty, just assign
123     if (fLeft >= fRight || fTop >= fBottom) {
124         this->set(left, top, right, bottom);
125     } else {
126         fLeft   = SkMinScalar(fLeft, left);
127         fTop    = SkMinScalar(fTop, top);
128         fRight  = SkMaxScalar(fRight, right);
129         fBottom = SkMaxScalar(fBottom, bottom);
130     }
131 }
132