| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 namespace blink { | 36 namespace blink { |
| 37 | 37 |
| 38 IntRect::IntRect(const FloatRect& r) | 38 IntRect::IntRect(const FloatRect& r) |
| 39 : m_location(clampTo<int>(r.x()), clampTo<int>(r.y())), | 39 : m_location(clampTo<int>(r.x()), clampTo<int>(r.y())), |
| 40 m_size(clampTo<int>(r.width()), clampTo<int>(r.height())) {} | 40 m_size(clampTo<int>(r.width()), clampTo<int>(r.height())) {} |
| 41 | 41 |
| 42 IntRect::IntRect(const LayoutRect& r) | 42 IntRect::IntRect(const LayoutRect& r) |
| 43 : m_location(r.x().toInt(), r.y().toInt()), | 43 : m_location(r.x().toInt(), r.y().toInt()), |
| 44 m_size(r.width().toInt(), r.height().toInt()) {} | 44 m_size(r.width().toInt(), r.height().toInt()) {} |
| 45 | 45 |
| 46 // Use long instead of int to avoid undefined behavior of storing -2147483648 |
| 47 static inline int GetClampedDiff(const long& a, const long& b) { |
| 48 return a - b <= std::numeric_limits<int>::max() |
| 49 ? a - b |
| 50 : std::numeric_limits<int>::max(); |
| 51 } |
| 52 |
| 46 bool IntRect::intersects(const IntRect& other) const { | 53 bool IntRect::intersects(const IntRect& other) const { |
| 47 // Checking emptiness handles negative widths as well as zero. | 54 // Checking emptiness handles negative widths as well as zero. |
| 48 return !isEmpty() && !other.isEmpty() && x() < other.maxX() && | 55 return !isEmpty() && !other.isEmpty() && x() < other.maxX() && |
| 49 other.x() < maxX() && y() < other.maxY() && other.y() < maxY(); | 56 other.x() < maxX() && y() < other.maxY() && other.y() < maxY(); |
| 50 } | 57 } |
| 51 | 58 |
| 52 bool IntRect::contains(const IntRect& other) const { | 59 bool IntRect::contains(const IntRect& other) const { |
| 53 return x() <= other.x() && maxX() >= other.maxX() && y() <= other.y() && | 60 return x() <= other.x() && maxX() >= other.maxX() && y() <= other.y() && |
| 54 maxY() >= other.maxY(); | 61 maxY() >= other.maxY(); |
| 55 } | 62 } |
| 56 | 63 |
| 57 void IntRect::intersect(const IntRect& other) { | 64 void IntRect::intersect(const IntRect& other) { |
| 58 int left = std::max(x(), other.x()); | 65 int left = std::max(x(), other.x()); |
| 59 int top = std::max(y(), other.y()); | 66 int top = std::max(y(), other.y()); |
| 60 int right = std::min(maxX(), other.maxX()); | 67 int right = std::min(maxX(), other.maxX()); |
| 61 int bottom = std::min(maxY(), other.maxY()); | 68 int bottom = std::min(maxY(), other.maxY()); |
| 62 | 69 |
| 63 // Return a clean empty rectangle for non-intersecting cases. | 70 // Return a clean empty rectangle for non-intersecting cases. |
| 64 if (left >= right || top >= bottom) { | 71 if (left >= right || top >= bottom) { |
| 65 left = 0; | 72 left = 0; |
| 66 top = 0; | 73 top = 0; |
| 67 right = 0; | 74 right = 0; |
| 68 bottom = 0; | 75 bottom = 0; |
| 69 } | 76 } |
| 70 | 77 |
| 71 m_location.setX(left); | 78 m_location.setX(left); |
| 72 m_location.setY(top); | 79 m_location.setY(top); |
| 73 m_size.setWidth(right - left); | 80 m_size.setWidth(GetClampedDiff(right, left)); |
| 74 m_size.setHeight(bottom - top); | 81 m_size.setHeight(GetClampedDiff(bottom, top)); |
| 75 } | 82 } |
| 76 | 83 |
| 77 void IntRect::unite(const IntRect& other) { | 84 void IntRect::unite(const IntRect& other) { |
| 78 // Handle empty special cases first. | 85 // Handle empty special cases first. |
| 79 if (other.isEmpty()) | 86 if (other.isEmpty()) |
| 80 return; | 87 return; |
| 81 if (isEmpty()) { | 88 if (isEmpty()) { |
| 82 *this = other; | 89 *this = other; |
| 83 return; | 90 return; |
| 84 } | 91 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 99 } | 106 } |
| 100 | 107 |
| 101 void IntRect::uniteEvenIfEmpty(const IntRect& other) { | 108 void IntRect::uniteEvenIfEmpty(const IntRect& other) { |
| 102 int left = std::min(x(), other.x()); | 109 int left = std::min(x(), other.x()); |
| 103 int top = std::min(y(), other.y()); | 110 int top = std::min(y(), other.y()); |
| 104 int right = std::max(maxX(), other.maxX()); | 111 int right = std::max(maxX(), other.maxX()); |
| 105 int bottom = std::max(maxY(), other.maxY()); | 112 int bottom = std::max(maxY(), other.maxY()); |
| 106 | 113 |
| 107 m_location.setX(left); | 114 m_location.setX(left); |
| 108 m_location.setY(top); | 115 m_location.setY(top); |
| 109 m_size.setWidth(right - left); | 116 m_size.setWidth(GetClampedDiff(right, left)); |
| 110 m_size.setHeight(bottom - top); | 117 m_size.setHeight(GetClampedDiff(bottom, top)); |
| 111 } | 118 } |
| 112 | 119 |
| 113 void IntRect::scale(float s) { | 120 void IntRect::scale(float s) { |
| 114 m_location.setX((int)(x() * s)); | 121 m_location.setX((int)(x() * s)); |
| 115 m_location.setY((int)(y() * s)); | 122 m_location.setY((int)(y() * s)); |
| 116 m_size.setWidth((int)(width() * s)); | 123 m_size.setWidth((int)(width() * s)); |
| 117 m_size.setHeight((int)(height() * s)); | 124 m_size.setHeight((int)(height() * s)); |
| 118 } | 125 } |
| 119 | 126 |
| 120 static inline int distanceToInterval(int pos, int start, int end) { | 127 static inline int distanceToInterval(int pos, int start, int end) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 175 |
| 169 return result; | 176 return result; |
| 170 } | 177 } |
| 171 | 178 |
| 172 String IntRect::toString() const { | 179 String IntRect::toString() const { |
| 173 return String::format("%s %s", location().toString().ascii().data(), | 180 return String::format("%s %s", location().toString().ascii().data(), |
| 174 size().toString().ascii().data()); | 181 size().toString().ascii().data()); |
| 175 } | 182 } |
| 176 | 183 |
| 177 } // namespace blink | 184 } // namespace blink |
| OLD | NEW |