ScaleFactor.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef MOZILLA_GFX_SCALEFACTOR_H_
  6. #define MOZILLA_GFX_SCALEFACTOR_H_
  7. #include "mozilla/Attributes.h"
  8. #include "gfxPoint.h"
  9. namespace mozilla {
  10. namespace gfx {
  11. /*
  12. * This class represents a scaling factor between two different pixel unit
  13. * systems. This is effectively a type-safe float, intended to be used in
  14. * combination with the known-type instances of gfx::Point, gfx::Rect, etc.
  15. *
  16. * This class is meant to be used in cases where a single scale applies to
  17. * both the x and y axes. For cases where two diferent scales apply, use
  18. * ScaleFactors2D.
  19. */
  20. template<class src, class dst>
  21. struct ScaleFactor {
  22. float scale;
  23. constexpr ScaleFactor() : scale(1.0) {}
  24. constexpr ScaleFactor(const ScaleFactor<src, dst>& aCopy) : scale(aCopy.scale) {}
  25. explicit constexpr ScaleFactor(float aScale) : scale(aScale) {}
  26. ScaleFactor<dst, src> Inverse() {
  27. return ScaleFactor<dst, src>(1 / scale);
  28. }
  29. bool operator==(const ScaleFactor<src, dst>& aOther) const {
  30. return scale == aOther.scale;
  31. }
  32. bool operator!=(const ScaleFactor<src, dst>& aOther) const {
  33. return !(*this == aOther);
  34. }
  35. bool operator<(const ScaleFactor<src, dst>& aOther) const {
  36. return scale < aOther.scale;
  37. }
  38. bool operator<=(const ScaleFactor<src, dst>& aOther) const {
  39. return scale <= aOther.scale;
  40. }
  41. bool operator>(const ScaleFactor<src, dst>& aOther) const {
  42. return scale > aOther.scale;
  43. }
  44. bool operator>=(const ScaleFactor<src, dst>& aOther) const {
  45. return scale >= aOther.scale;
  46. }
  47. template<class other>
  48. ScaleFactor<other, dst> operator/(const ScaleFactor<src, other>& aOther) const {
  49. return ScaleFactor<other, dst>(scale / aOther.scale);
  50. }
  51. template<class other>
  52. ScaleFactor<src, other> operator/(const ScaleFactor<other, dst>& aOther) const {
  53. return ScaleFactor<src, other>(scale / aOther.scale);
  54. }
  55. template<class other>
  56. ScaleFactor<src, other> operator*(const ScaleFactor<dst, other>& aOther) const {
  57. return ScaleFactor<src, other>(scale * aOther.scale);
  58. }
  59. template<class other>
  60. ScaleFactor<other, dst> operator*(const ScaleFactor<other, src>& aOther) const {
  61. return ScaleFactor<other, dst>(scale * aOther.scale);
  62. }
  63. };
  64. } // namespace gfx
  65. } // namespace mozilla
  66. #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */