tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ScaleFactor.h (2875B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef MOZILLA_GFX_SCALEFACTOR_H_
      8 #define MOZILLA_GFX_SCALEFACTOR_H_
      9 
     10 #include <ostream>
     11 
     12 #include "gfxPoint.h"
     13 
     14 namespace mozilla {
     15 namespace gfx {
     16 
     17 /*
     18 * This class represents a scaling factor between two different pixel unit
     19 * systems. This is effectively a type-safe float, intended to be used in
     20 * combination with the known-type instances of gfx::Point, gfx::Rect, etc.
     21 *
     22 * This class is meant to be used in cases where a single scale applies to
     23 * both the x and y axes. For cases where two diferent scales apply, use
     24 * ScaleFactors2D.
     25 */
     26 template <class Src, class Dst>
     27 struct ScaleFactor {
     28  float scale;
     29 
     30  constexpr ScaleFactor() : scale(1.0) {}
     31  constexpr ScaleFactor(const ScaleFactor<Src, Dst>& aCopy)
     32      : scale(aCopy.scale) {}
     33  explicit constexpr ScaleFactor(float aScale) : scale(aScale) {}
     34 
     35  ScaleFactor<Dst, Src> Inverse() { return ScaleFactor<Dst, Src>(1 / scale); }
     36 
     37  ScaleFactor<Src, Dst>& operator=(const ScaleFactor<Src, Dst>&) = default;
     38 
     39  bool operator==(const ScaleFactor<Src, Dst>& aOther) const {
     40    return scale == aOther.scale;
     41  }
     42 
     43  bool operator!=(const ScaleFactor<Src, Dst>& aOther) const {
     44    return !(*this == aOther);
     45  }
     46 
     47  bool operator<(const ScaleFactor<Src, Dst>& aOther) const {
     48    return scale < aOther.scale;
     49  }
     50 
     51  bool operator<=(const ScaleFactor<Src, Dst>& aOther) const {
     52    return scale <= aOther.scale;
     53  }
     54 
     55  bool operator>(const ScaleFactor<Src, Dst>& aOther) const {
     56    return scale > aOther.scale;
     57  }
     58 
     59  bool operator>=(const ScaleFactor<Src, Dst>& aOther) const {
     60    return scale >= aOther.scale;
     61  }
     62 
     63  template <class Other>
     64  ScaleFactor<Other, Dst> operator/(
     65      const ScaleFactor<Src, Other>& aOther) const {
     66    return ScaleFactor<Other, Dst>(scale / aOther.scale);
     67  }
     68 
     69  template <class Other>
     70  ScaleFactor<Src, Other> operator/(
     71      const ScaleFactor<Other, Dst>& aOther) const {
     72    return ScaleFactor<Src, Other>(scale / aOther.scale);
     73  }
     74 
     75  template <class Other>
     76  ScaleFactor<Src, Other> operator*(
     77      const ScaleFactor<Dst, Other>& aOther) const {
     78    return ScaleFactor<Src, Other>(scale * aOther.scale);
     79  }
     80 
     81  template <class Other>
     82  ScaleFactor<Other, Dst> operator*(
     83      const ScaleFactor<Other, Src>& aOther) const {
     84    return ScaleFactor<Other, Dst>(scale * aOther.scale);
     85  }
     86 
     87  friend std::ostream& operator<<(std::ostream& aStream,
     88                                  const ScaleFactor<Src, Dst>& aSF) {
     89    return aStream << aSF.scale;
     90  }
     91 };
     92 
     93 }  // namespace gfx
     94 }  // namespace mozilla
     95 
     96 #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */