KHTML
FloatRect.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. 00003 * Copyright (C) 2005 Nokia. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 00015 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00017 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 00018 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00019 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00020 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00021 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00022 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00023 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00024 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 00027 #include "config.h" 00028 #include "FloatRect.h" 00029 00030 #include "FloatConversion.h" 00031 #include "IntRect.h" 00032 #include <algorithm> 00033 00034 using std::max; 00035 using std::min; 00036 00037 namespace WebCore { 00038 00039 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size()) 00040 { 00041 } 00042 00043 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height) 00044 { 00045 return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height)); 00046 } 00047 00048 bool FloatRect::intersects(const FloatRect& other) const 00049 { 00050 // Checking emptiness handles negative widths as well as zero. 00051 return !isEmpty() && !other.isEmpty() 00052 && x() < other.right() && other.x() < right() 00053 && y() < other.bottom() && other.y() < bottom(); 00054 } 00055 00056 bool FloatRect::contains(const FloatRect& other) const 00057 { 00058 return x() <= other.x() && right() >= other.right() 00059 && y() <= other.y() && bottom() >= other.bottom(); 00060 } 00061 00062 void FloatRect::intersect(const FloatRect& other) 00063 { 00064 float l = max(x(), other.x()); 00065 float t = max(y(), other.y()); 00066 float r = min(right(), other.right()); 00067 float b = min(bottom(), other.bottom()); 00068 00069 // Return a clean empty rectangle for non-intersecting cases. 00070 if (l >= r || t >= b) { 00071 l = 0; 00072 t = 0; 00073 r = 0; 00074 b = 0; 00075 } 00076 00077 m_location.setX(l); 00078 m_location.setY(t); 00079 m_size.setWidth(r - l); 00080 m_size.setHeight(b - t); 00081 } 00082 00083 void FloatRect::unite(const FloatRect& other) 00084 { 00085 // Handle empty special cases first. 00086 if (other.isEmpty()) 00087 return; 00088 if (isEmpty()) { 00089 *this = other; 00090 return; 00091 } 00092 00093 float l = min(x(), other.x()); 00094 float t = min(y(), other.y()); 00095 float r = max(right(), other.right()); 00096 float b = max(bottom(), other.bottom()); 00097 00098 m_location.setX(l); 00099 m_location.setY(t); 00100 m_size.setWidth(r - l); 00101 m_size.setHeight(b - t); 00102 } 00103 00104 void FloatRect::scale(float s) 00105 { 00106 m_location.setX(x() * s); 00107 m_location.setY(y() * s); 00108 m_size.setWidth(width() * s); 00109 m_size.setHeight(height() * s); 00110 } 00111 00112 IntRect enclosingIntRect(const FloatRect& rect) 00113 { 00114 int l = static_cast<int>(rect.x()); 00115 int t = static_cast<int>(rect.y()); 00116 // FIXME: These two need to be a "ceiling" operation, not rounding. 00117 // We changed them to do "+ 0.5f" to compile on Win32 where there's 00118 // no ceilf, but they should be changed back to "ceiling" at some point 00119 // and we should provide an implementation of ceilf for Win32. 00120 int r = static_cast<int>(rect.right() + 0.5f); 00121 int b = static_cast<int>(rect.bottom() + 0.5f); 00122 return IntRect(l, t, r - l, b - t); 00123 } 00124 00125 }
KDE 4.6 API Reference