gfx/2d/Rect.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/Rect.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     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 MOZILLA_GFX_RECT_H_
    1.10 +#define MOZILLA_GFX_RECT_H_
    1.11 +
    1.12 +#include "BaseRect.h"
    1.13 +#include "BaseMargin.h"
    1.14 +#include "Point.h"
    1.15 +#include "Tools.h"
    1.16 +
    1.17 +#include <cmath>
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace gfx {
    1.21 +
    1.22 +template<class units>
    1.23 +struct IntMarginTyped:
    1.24 +    public BaseMargin<int32_t, IntMarginTyped<units> >,
    1.25 +    public units {
    1.26 +    typedef BaseMargin<int32_t, IntMarginTyped<units> > Super;
    1.27 +
    1.28 +    IntMarginTyped() : Super() {}
    1.29 +    IntMarginTyped(int32_t aTop, int32_t aRight, int32_t aBottom, int32_t aLeft) :
    1.30 +        Super(aTop, aRight, aBottom, aLeft) {}
    1.31 +};
    1.32 +typedef IntMarginTyped<UnknownUnits> IntMargin;
    1.33 +
    1.34 +template<class units>
    1.35 +struct MarginTyped:
    1.36 +    public BaseMargin<Float, MarginTyped<units> >,
    1.37 +    public units {
    1.38 +    typedef BaseMargin<Float, MarginTyped<units> > Super;
    1.39 +
    1.40 +    MarginTyped() : Super() {}
    1.41 +    MarginTyped(Float aTop, Float aRight, Float aBottom, Float aLeft) :
    1.42 +        Super(aTop, aRight, aBottom, aLeft) {}
    1.43 +    explicit MarginTyped(const IntMarginTyped<units>& aMargin) :
    1.44 +        Super(float(aMargin.top), float(aMargin.right),
    1.45 +              float(aMargin.bottom), float(aMargin.left)) {}
    1.46 +};
    1.47 +typedef MarginTyped<UnknownUnits> Margin;
    1.48 +
    1.49 +template<class units>
    1.50 +IntMarginTyped<units> RoundedToInt(const MarginTyped<units>& aMargin)
    1.51 +{
    1.52 +  return IntMarginTyped<units>(int32_t(floorf(aMargin.top + 0.5f)),
    1.53 +                               int32_t(floorf(aMargin.right + 0.5f)),
    1.54 +                               int32_t(floorf(aMargin.bottom + 0.5f)),
    1.55 +                               int32_t(floorf(aMargin.left + 0.5f)));
    1.56 +}
    1.57 +
    1.58 +template<class units>
    1.59 +struct IntRectTyped :
    1.60 +    public BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> >,
    1.61 +    public units {
    1.62 +    typedef BaseRect<int32_t, IntRectTyped<units>, IntPointTyped<units>, IntSizeTyped<units>, IntMarginTyped<units> > Super;
    1.63 +
    1.64 +    IntRectTyped() : Super() {}
    1.65 +    IntRectTyped(const IntPointTyped<units>& aPos, const IntSizeTyped<units>& aSize) :
    1.66 +        Super(aPos, aSize) {}
    1.67 +    IntRectTyped(int32_t _x, int32_t _y, int32_t _width, int32_t _height) :
    1.68 +        Super(_x, _y, _width, _height) {}
    1.69 +
    1.70 +    // Rounding isn't meaningful on an integer rectangle.
    1.71 +    void Round() {}
    1.72 +    void RoundIn() {}
    1.73 +    void RoundOut() {}
    1.74 +
    1.75 +    // XXX When all of the code is ported, the following functions to convert to and from
    1.76 +    // unknown types should be removed.
    1.77 +
    1.78 +    static IntRectTyped<units> FromUnknownRect(const IntRectTyped<UnknownUnits>& rect) {
    1.79 +        return IntRectTyped<units>(rect.x, rect.y, rect.width, rect.height);
    1.80 +    }
    1.81 +
    1.82 +    IntRectTyped<UnknownUnits> ToUnknownRect() const {
    1.83 +        return IntRectTyped<UnknownUnits>(this->x, this->y, this->width, this->height);
    1.84 +    }
    1.85 +};
    1.86 +typedef IntRectTyped<UnknownUnits> IntRect;
    1.87 +
    1.88 +template<class units>
    1.89 +struct RectTyped :
    1.90 +    public BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> >,
    1.91 +    public units {
    1.92 +    typedef BaseRect<Float, RectTyped<units>, PointTyped<units>, SizeTyped<units>, MarginTyped<units> > Super;
    1.93 +
    1.94 +    RectTyped() : Super() {}
    1.95 +    RectTyped(const PointTyped<units>& aPos, const SizeTyped<units>& aSize) :
    1.96 +        Super(aPos, aSize) {}
    1.97 +    RectTyped(Float _x, Float _y, Float _width, Float _height) :
    1.98 +        Super(_x, _y, _width, _height) {}
    1.99 +    explicit RectTyped(const IntRectTyped<units>& rect) :
   1.100 +        Super(float(rect.x), float(rect.y),
   1.101 +              float(rect.width), float(rect.height)) {}
   1.102 +
   1.103 +    void NudgeToIntegers()
   1.104 +    {
   1.105 +      NudgeToInteger(&(this->x));
   1.106 +      NudgeToInteger(&(this->y));
   1.107 +      NudgeToInteger(&(this->width));
   1.108 +      NudgeToInteger(&(this->height));
   1.109 +    }
   1.110 +
   1.111 +    bool ToIntRect(IntRectTyped<units> *aOut) const
   1.112 +    {
   1.113 +      *aOut = IntRectTyped<units>(int32_t(this->X()), int32_t(this->Y()),
   1.114 +                                  int32_t(this->Width()), int32_t(this->Height()));
   1.115 +      return RectTyped<units>(Float(aOut->x), Float(aOut->y), 
   1.116 +                              Float(aOut->width), Float(aOut->height))
   1.117 +             .IsEqualEdges(*this);
   1.118 +    }
   1.119 +
   1.120 +    // XXX When all of the code is ported, the following functions to convert to and from
   1.121 +    // unknown types should be removed.
   1.122 +
   1.123 +    static RectTyped<units> FromUnknownRect(const RectTyped<UnknownUnits>& rect) {
   1.124 +        return RectTyped<units>(rect.x, rect.y, rect.width, rect.height);
   1.125 +    }
   1.126 +
   1.127 +    RectTyped<UnknownUnits> ToUnknownRect() const {
   1.128 +        return RectTyped<UnknownUnits>(this->x, this->y, this->width, this->height);
   1.129 +    }
   1.130 +
   1.131 +    // This is here only to keep IPDL-generated code happy. DO NOT USE.
   1.132 +    bool operator==(const RectTyped<units>& aRect) const
   1.133 +    {
   1.134 +      return RectTyped<units>::IsEqualEdges(aRect);
   1.135 +    }
   1.136 +};
   1.137 +typedef RectTyped<UnknownUnits> Rect;
   1.138 +
   1.139 +template<class units>
   1.140 +IntRectTyped<units> RoundedToInt(const RectTyped<units>& aRect)
   1.141 +{
   1.142 +  return IntRectTyped<units>(int32_t(floorf(aRect.x + 0.5f)),
   1.143 +                             int32_t(floorf(aRect.y + 0.5f)),
   1.144 +                             int32_t(floorf(aRect.width + 0.5f)),
   1.145 +                             int32_t(floorf(aRect.height + 0.5f)));
   1.146 +}
   1.147 +
   1.148 +template<class units>
   1.149 +IntRectTyped<units> RoundedIn(const RectTyped<units>& aRect)
   1.150 +{
   1.151 +  RectTyped<units> copy(aRect);
   1.152 +  copy.RoundIn();
   1.153 +  return IntRectTyped<units>(int32_t(copy.x),
   1.154 +                             int32_t(copy.y),
   1.155 +                             int32_t(copy.width),
   1.156 +                             int32_t(copy.height));
   1.157 +}
   1.158 +
   1.159 +template<class units>
   1.160 +IntRectTyped<units> RoundedOut(const RectTyped<units>& aRect)
   1.161 +{
   1.162 +  RectTyped<units> copy(aRect);
   1.163 +  copy.RoundOut();
   1.164 +  return IntRectTyped<units>(int32_t(copy.x),
   1.165 +                             int32_t(copy.y),
   1.166 +                             int32_t(copy.width),
   1.167 +                             int32_t(copy.height));
   1.168 +}
   1.169 +
   1.170 +}
   1.171 +}
   1.172 +
   1.173 +#endif /* MOZILLA_GFX_RECT_H_ */

mercurial