1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxRect.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef GFX_RECT_H 1.10 +#define GFX_RECT_H 1.11 + 1.12 +#include "gfxTypes.h" 1.13 +#include "gfxPoint.h" 1.14 +#include "nsDebug.h" 1.15 +#include "nsRect.h" 1.16 +#include "mozilla/gfx/BaseMargin.h" 1.17 +#include "mozilla/gfx/BaseRect.h" 1.18 +#include "mozilla/Assertions.h" 1.19 + 1.20 +struct gfxMargin : public mozilla::gfx::BaseMargin<gfxFloat, gfxMargin> { 1.21 + typedef mozilla::gfx::BaseMargin<gfxFloat, gfxMargin> Super; 1.22 + 1.23 + // Constructors 1.24 + gfxMargin() : Super() {} 1.25 + gfxMargin(const gfxMargin& aMargin) : Super(aMargin) {} 1.26 + gfxMargin(gfxFloat aTop, gfxFloat aRight, gfxFloat aBottom, gfxFloat aLeft) 1.27 + : Super(aTop, aRight, aBottom, aLeft) {} 1.28 +}; 1.29 + 1.30 +namespace mozilla { 1.31 + namespace css { 1.32 + enum Corner { 1.33 + // this order is important! 1.34 + eCornerTopLeft = 0, 1.35 + eCornerTopRight = 1, 1.36 + eCornerBottomRight = 2, 1.37 + eCornerBottomLeft = 3, 1.38 + eNumCorners = 4 1.39 + }; 1.40 + } 1.41 +} 1.42 +#define NS_CORNER_TOP_LEFT mozilla::css::eCornerTopLeft 1.43 +#define NS_CORNER_TOP_RIGHT mozilla::css::eCornerTopRight 1.44 +#define NS_CORNER_BOTTOM_RIGHT mozilla::css::eCornerBottomRight 1.45 +#define NS_CORNER_BOTTOM_LEFT mozilla::css::eCornerBottomLeft 1.46 +#define NS_NUM_CORNERS mozilla::css::eNumCorners 1.47 + 1.48 +#define NS_FOR_CSS_CORNERS(var_) \ 1.49 + for (mozilla::css::Corner var_ = NS_CORNER_TOP_LEFT; \ 1.50 + var_ <= NS_CORNER_BOTTOM_LEFT; \ 1.51 + var_++) 1.52 + 1.53 +static inline mozilla::css::Corner operator++(mozilla::css::Corner& corner, int) { 1.54 + NS_PRECONDITION(corner >= NS_CORNER_TOP_LEFT && 1.55 + corner < NS_NUM_CORNERS, "Out of range corner"); 1.56 + corner = mozilla::css::Corner(corner + 1); 1.57 + return corner; 1.58 +} 1.59 + 1.60 +struct gfxRect : 1.61 + public mozilla::gfx::BaseRect<gfxFloat, gfxRect, gfxPoint, gfxSize, gfxMargin> { 1.62 + typedef mozilla::gfx::BaseRect<gfxFloat, gfxRect, gfxPoint, gfxSize, gfxMargin> Super; 1.63 + 1.64 + gfxRect() : Super() {} 1.65 + gfxRect(const gfxPoint& aPos, const gfxSize& aSize) : 1.66 + Super(aPos, aSize) {} 1.67 + gfxRect(gfxFloat aX, gfxFloat aY, gfxFloat aWidth, gfxFloat aHeight) : 1.68 + Super(aX, aY, aWidth, aHeight) {} 1.69 + gfxRect(const nsIntRect& aRect) : 1.70 + Super(aRect.x, aRect.y, aRect.width, aRect.height) {} 1.71 + 1.72 + /** 1.73 + * Return true if all components of this rect are within 1.74 + * aEpsilon of integer coordinates, defined as 1.75 + * |round(coord) - coord| <= |aEpsilon| 1.76 + * for x,y,width,height. 1.77 + */ 1.78 + bool WithinEpsilonOfIntegerPixels(gfxFloat aEpsilon) const; 1.79 + 1.80 + gfxPoint AtCorner(mozilla::css::Corner corner) const { 1.81 + switch (corner) { 1.82 + case NS_CORNER_TOP_LEFT: return TopLeft(); 1.83 + case NS_CORNER_TOP_RIGHT: return TopRight(); 1.84 + case NS_CORNER_BOTTOM_RIGHT: return BottomRight(); 1.85 + case NS_CORNER_BOTTOM_LEFT: return BottomLeft(); 1.86 + default: 1.87 + NS_ERROR("Invalid corner!"); 1.88 + break; 1.89 + } 1.90 + return gfxPoint(0.0, 0.0); 1.91 + } 1.92 + 1.93 + gfxPoint CCWCorner(mozilla::css::Side side) const { 1.94 + switch (side) { 1.95 + case NS_SIDE_TOP: return TopLeft(); 1.96 + case NS_SIDE_RIGHT: return TopRight(); 1.97 + case NS_SIDE_BOTTOM: return BottomRight(); 1.98 + case NS_SIDE_LEFT: return BottomLeft(); 1.99 + } 1.100 + MOZ_CRASH("Incomplete switch"); 1.101 + } 1.102 + 1.103 + gfxPoint CWCorner(mozilla::css::Side side) const { 1.104 + switch (side) { 1.105 + case NS_SIDE_TOP: return TopRight(); 1.106 + case NS_SIDE_RIGHT: return BottomRight(); 1.107 + case NS_SIDE_BOTTOM: return BottomLeft(); 1.108 + case NS_SIDE_LEFT: return TopLeft(); 1.109 + } 1.110 + MOZ_CRASH("Incomplete switch"); 1.111 + } 1.112 + 1.113 + /* Conditions this border to Cairo's max coordinate space. 1.114 + * The caller can check IsEmpty() after Condition() -- if it's TRUE, 1.115 + * the caller can possibly avoid doing any extra rendering. 1.116 + */ 1.117 + void Condition(); 1.118 + 1.119 + void Scale(gfxFloat k) { 1.120 + NS_ASSERTION(k >= 0.0, "Invalid (negative) scale factor"); 1.121 + x *= k; 1.122 + y *= k; 1.123 + width *= k; 1.124 + height *= k; 1.125 + } 1.126 + 1.127 + void Scale(gfxFloat sx, gfxFloat sy) { 1.128 + NS_ASSERTION(sx >= 0.0, "Invalid (negative) scale factor"); 1.129 + NS_ASSERTION(sy >= 0.0, "Invalid (negative) scale factor"); 1.130 + x *= sx; 1.131 + y *= sy; 1.132 + width *= sx; 1.133 + height *= sy; 1.134 + } 1.135 + 1.136 + void ScaleInverse(gfxFloat k) { 1.137 + NS_ASSERTION(k > 0.0, "Invalid (negative) scale factor"); 1.138 + x /= k; 1.139 + y /= k; 1.140 + width /= k; 1.141 + height /= k; 1.142 + } 1.143 +}; 1.144 + 1.145 +struct gfxCornerSizes { 1.146 + gfxSize sizes[NS_NUM_CORNERS]; 1.147 + 1.148 + gfxCornerSizes () { } 1.149 + 1.150 + gfxCornerSizes (gfxFloat v) { 1.151 + for (int i = 0; i < NS_NUM_CORNERS; i++) 1.152 + sizes[i].SizeTo(v, v); 1.153 + } 1.154 + 1.155 + gfxCornerSizes (gfxFloat tl, gfxFloat tr, gfxFloat br, gfxFloat bl) { 1.156 + sizes[NS_CORNER_TOP_LEFT].SizeTo(tl, tl); 1.157 + sizes[NS_CORNER_TOP_RIGHT].SizeTo(tr, tr); 1.158 + sizes[NS_CORNER_BOTTOM_RIGHT].SizeTo(br, br); 1.159 + sizes[NS_CORNER_BOTTOM_LEFT].SizeTo(bl, bl); 1.160 + } 1.161 + 1.162 + gfxCornerSizes (const gfxSize& tl, const gfxSize& tr, const gfxSize& br, const gfxSize& bl) { 1.163 + sizes[NS_CORNER_TOP_LEFT] = tl; 1.164 + sizes[NS_CORNER_TOP_RIGHT] = tr; 1.165 + sizes[NS_CORNER_BOTTOM_RIGHT] = br; 1.166 + sizes[NS_CORNER_BOTTOM_LEFT] = bl; 1.167 + } 1.168 + 1.169 + const gfxSize& operator[] (mozilla::css::Corner index) const { 1.170 + return sizes[index]; 1.171 + } 1.172 + 1.173 + gfxSize& operator[] (mozilla::css::Corner index) { 1.174 + return sizes[index]; 1.175 + } 1.176 + 1.177 + void Scale(gfxFloat aXScale, gfxFloat aYScale) 1.178 + { 1.179 + for (int i = 0; i < NS_NUM_CORNERS; i++) 1.180 + sizes[i].Scale(aXScale, aYScale); 1.181 + } 1.182 + 1.183 + const gfxSize TopLeft() const { return sizes[NS_CORNER_TOP_LEFT]; } 1.184 + gfxSize& TopLeft() { return sizes[NS_CORNER_TOP_LEFT]; } 1.185 + 1.186 + const gfxSize TopRight() const { return sizes[NS_CORNER_TOP_RIGHT]; } 1.187 + gfxSize& TopRight() { return sizes[NS_CORNER_TOP_RIGHT]; } 1.188 + 1.189 + const gfxSize BottomLeft() const { return sizes[NS_CORNER_BOTTOM_LEFT]; } 1.190 + gfxSize& BottomLeft() { return sizes[NS_CORNER_BOTTOM_LEFT]; } 1.191 + 1.192 + const gfxSize BottomRight() const { return sizes[NS_CORNER_BOTTOM_RIGHT]; } 1.193 + gfxSize& BottomRight() { return sizes[NS_CORNER_BOTTOM_RIGHT]; } 1.194 +}; 1.195 +#endif /* GFX_RECT_H */