1 /* 2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2005 Nokia. All rights reserved. 4 * 2008 Eric Seidel <eric (at) webkit.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef FloatSize_h 29 #define FloatSize_h 30 31 #include "IntSize.h" 32 #include <wtf/MathExtras.h> 33 #include <wtf/Platform.h> 34 35 #if PLATFORM(CG) 36 typedef struct CGSize CGSize; 37 #endif 38 39 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN)) 40 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 41 typedef struct CGSize NSSize; 42 #else 43 typedef struct _NSSize NSSize; 44 #endif 45 #endif 46 47 namespace WebCore { 48 49 class IntSize; 50 51 class FloatSize { 52 public: 53 FloatSize() : m_width(0), m_height(0) { } 54 FloatSize(float width, float height) : m_width(width), m_height(height) { } 55 FloatSize(const IntSize&); 56 57 static FloatSize narrowPrecision(double width, double height); 58 59 float width() const { return m_width; } 60 float height() const { return m_height; } 61 62 void setWidth(float width) { m_width = width; } 63 void setHeight(float height) { m_height = height; } 64 65 bool isEmpty() const { return m_width <= 0 || m_height <= 0; } 66 67 FloatSize expandedTo(const FloatSize& other) const 68 { 69 return FloatSize(m_width > other.m_width ? m_width : other.m_width, 70 m_height > other.m_height ? m_height : other.m_height); 71 } 72 73 FloatSize shrunkTo(const FloatSize& other) const 74 { 75 return FloatSize(m_width < other.m_width ? m_width : other.m_width, 76 m_height < other.m_height ? m_height : other.m_height); 77 } 78 79 #if PLATFORM(CG) 80 explicit FloatSize(const CGSize&); // don't do this implicitly since it's lossy 81 operator CGSize() const; 82 #endif 83 84 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \ 85 || (PLATFORM(CHROMIUM) && OS(DARWIN)) 86 explicit FloatSize(const NSSize &); // don't do this implicitly since it's lossy 87 operator NSSize() const; 88 #endif 89 90 private: 91 float m_width, m_height; 92 }; 93 94 inline FloatSize& operator+=(FloatSize& a, const FloatSize& b) 95 { 96 a.setWidth(a.width() + b.width()); 97 a.setHeight(a.height() + b.height()); 98 return a; 99 } 100 101 inline FloatSize& operator-=(FloatSize& a, const FloatSize& b) 102 { 103 a.setWidth(a.width() - b.width()); 104 a.setHeight(a.height() - b.height()); 105 return a; 106 } 107 108 inline FloatSize operator+(const FloatSize& a, const FloatSize& b) 109 { 110 return FloatSize(a.width() + b.width(), a.height() + b.height()); 111 } 112 113 inline FloatSize operator-(const FloatSize& a, const FloatSize& b) 114 { 115 return FloatSize(a.width() - b.width(), a.height() - b.height()); 116 } 117 118 inline FloatSize operator-(const FloatSize& size) 119 { 120 return FloatSize(-size.width(), -size.height()); 121 } 122 123 inline bool operator==(const FloatSize& a, const FloatSize& b) 124 { 125 return a.width() == b.width() && a.height() == b.height(); 126 } 127 128 inline bool operator!=(const FloatSize& a, const FloatSize& b) 129 { 130 return a.width() != b.width() || a.height() != b.height(); 131 } 132 133 inline IntSize roundedIntSize(const FloatSize& p) 134 { 135 return IntSize(static_cast<int>(roundf(p.width())), static_cast<int>(roundf(p.height()))); 136 } 137 138 } // namespace WebCore 139 140 #endif // FloatSize_h 141