1 // Copyright 2010-2018, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // Platform-independent structures for points, sizes, and rectangles 31 #ifndef MOZC_BASE_COORDINATES_H_ 32 #define MOZC_BASE_COORDINATES_H_ 33 34 namespace mozc { 35 struct Point { 36 int x; 37 int y; xPoint38 explicit Point(int newx = 0, int newy = 0) : x(newx), y(newy) {} PointPoint39 Point(const Point &point) : x(point.x), y(point.y) {} 40 }; 41 42 struct Size { 43 int width; 44 int height; 45 explicit Size(int newwidth = 0, int newheight = 0) widthSize46 : width(newwidth), height(newheight) {} SizeSize47 Size(const Size &size) : width(size.width), height(size.height) {} 48 }; 49 50 struct Rect { 51 Point origin; 52 Size size; RectRect53 Rect(int newx, int newy, int newwidth, int newheight) 54 : origin(newx, newy), size(newwidth, newheight) {} RectRect55 Rect(const Point &o, const Size &s) : origin(o), size(s) {} RectRect56 Rect(const Rect &rect) : origin(rect.origin), size(rect.size) {} RectRect57 Rect() {} 58 59 // Accessors WidthRect60 inline int Width() const { return size.width; } HeightRect61 inline int Height() const { return size.height; } LeftRect62 inline int Left() const { return origin.x; } TopRect63 inline int Top() const { return origin.y; } RightRect64 inline int Right() const { return origin.x + size.width; } BottomRect65 inline int Bottom() const { return origin.y + size.height; } 66 67 // Other methods DeflateRectRect68 inline void DeflateRect(int l, int t, int r, int b) { 69 origin.x += l; 70 origin.y += t; 71 size.width -= l + r; 72 size.height -= t + b; 73 } DeflateRectRect74 inline void DeflateRect(int x, int y) { 75 DeflateRect(x, y, x, y); 76 } DeflateRectRect77 inline void DeflateRect(const Size &s) { DeflateRect(s.width, s.height); } 78 // Returns true if the right side is less than or equal to the coordinate 79 // of the left side, or the coordinate of the bottom side is less than or 80 // equal to the coordinate of the top side. 81 // This behaviour is compatible to the IsRectEmpty API 82 // http://msdn.microsoft.com/en-us/library/dd145017.aspx IsRectEmptyRect83 inline bool IsRectEmpty() const { 84 return size.width <= 0 || size.height <= 0; 85 } PtrInRectRect86 inline bool PtrInRect(const Point &p) const { 87 return p.x >= origin.x && p.x <= origin.x + size.width && 88 p.y >= origin.y && p.y <= origin.y + size.height; 89 } 90 }; 91 } // namespace mozc 92 #endif // MOZC_BASE_COORDINATES_H_ 93