1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/ScaleFactor.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; 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_SCALEFACTOR_H_ 1.10 +#define MOZILLA_GFX_SCALEFACTOR_H_ 1.11 + 1.12 +#include "mozilla/Attributes.h" 1.13 + 1.14 +#include "gfxPoint.h" 1.15 + 1.16 +namespace mozilla { 1.17 +namespace gfx { 1.18 + 1.19 +/* 1.20 + * This class represents a scaling factor between two different pixel unit 1.21 + * systems. This is effectively a type-safe float, intended to be used in 1.22 + * combination with the known-type instances of gfx::Point, gfx::Rect, etc. 1.23 + * 1.24 + * Note that some parts of the code that pre-date this class used separate 1.25 + * scaling factors for the x and y axes. However, at runtime these values 1.26 + * were always expected to be the same, so this class uses only one scale 1.27 + * factor for both axes. The two constructors that take two-axis scaling 1.28 + * factors check to ensure that this assertion holds. 1.29 + */ 1.30 +template<class src, class dst> 1.31 +struct ScaleFactor { 1.32 + float scale; 1.33 + 1.34 + MOZ_CONSTEXPR ScaleFactor() : scale(1.0) {} 1.35 + MOZ_CONSTEXPR ScaleFactor(const ScaleFactor<src, dst>& aCopy) : scale(aCopy.scale) {} 1.36 + explicit MOZ_CONSTEXPR ScaleFactor(float aScale) : scale(aScale) {} 1.37 + 1.38 + explicit ScaleFactor(float aX, float aY) : scale(aX) { 1.39 + MOZ_ASSERT(fabs(aX - aY) < 1e-6); 1.40 + } 1.41 + 1.42 + ScaleFactor<dst, src> Inverse() { 1.43 + return ScaleFactor<dst, src>(1 / scale); 1.44 + } 1.45 + 1.46 + bool operator==(const ScaleFactor<src, dst>& aOther) const { 1.47 + return scale == aOther.scale; 1.48 + } 1.49 + 1.50 + bool operator!=(const ScaleFactor<src, dst>& aOther) const { 1.51 + return !(*this == aOther); 1.52 + } 1.53 + 1.54 + bool operator<(const ScaleFactor<src, dst>& aOther) const { 1.55 + return scale < aOther.scale; 1.56 + } 1.57 + 1.58 + bool operator<=(const ScaleFactor<src, dst>& aOther) const { 1.59 + return scale <= aOther.scale; 1.60 + } 1.61 + 1.62 + bool operator>(const ScaleFactor<src, dst>& aOther) const { 1.63 + return scale > aOther.scale; 1.64 + } 1.65 + 1.66 + bool operator>=(const ScaleFactor<src, dst>& aOther) const { 1.67 + return scale >= aOther.scale; 1.68 + } 1.69 + 1.70 + template<class other> 1.71 + ScaleFactor<other, dst> operator/(const ScaleFactor<src, other>& aOther) const { 1.72 + return ScaleFactor<other, dst>(scale / aOther.scale); 1.73 + } 1.74 + 1.75 + template<class other> 1.76 + ScaleFactor<src, other> operator/(const ScaleFactor<other, dst>& aOther) const { 1.77 + return ScaleFactor<src, other>(scale / aOther.scale); 1.78 + } 1.79 + 1.80 + template<class other> 1.81 + ScaleFactor<src, other> operator*(const ScaleFactor<dst, other>& aOther) const { 1.82 + return ScaleFactor<src, other>(scale * aOther.scale); 1.83 + } 1.84 + 1.85 + template<class other> 1.86 + ScaleFactor<other, dst> operator*(const ScaleFactor<other, src>& aOther) const { 1.87 + return ScaleFactor<other, dst>(scale * aOther.scale); 1.88 + } 1.89 +}; 1.90 + 1.91 +} 1.92 +} 1.93 + 1.94 +#endif /* MOZILLA_GFX_SCALEFACTOR_H_ */