michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_SCALEFACTOR_H_ michael@0: #define MOZILLA_GFX_SCALEFACTOR_H_ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include "gfxPoint.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: /* michael@0: * This class represents a scaling factor between two different pixel unit michael@0: * systems. This is effectively a type-safe float, intended to be used in michael@0: * combination with the known-type instances of gfx::Point, gfx::Rect, etc. michael@0: * michael@0: * Note that some parts of the code that pre-date this class used separate michael@0: * scaling factors for the x and y axes. However, at runtime these values michael@0: * were always expected to be the same, so this class uses only one scale michael@0: * factor for both axes. The two constructors that take two-axis scaling michael@0: * factors check to ensure that this assertion holds. michael@0: */ michael@0: template michael@0: struct ScaleFactor { michael@0: float scale; michael@0: michael@0: MOZ_CONSTEXPR ScaleFactor() : scale(1.0) {} michael@0: MOZ_CONSTEXPR ScaleFactor(const ScaleFactor& aCopy) : scale(aCopy.scale) {} michael@0: explicit MOZ_CONSTEXPR ScaleFactor(float aScale) : scale(aScale) {} michael@0: michael@0: explicit ScaleFactor(float aX, float aY) : scale(aX) { michael@0: MOZ_ASSERT(fabs(aX - aY) < 1e-6); michael@0: } michael@0: michael@0: ScaleFactor Inverse() { michael@0: return ScaleFactor(1 / scale); michael@0: } michael@0: michael@0: bool operator==(const ScaleFactor& aOther) const { michael@0: return scale == aOther.scale; michael@0: } michael@0: michael@0: bool operator!=(const ScaleFactor& aOther) const { michael@0: return !(*this == aOther); michael@0: } michael@0: michael@0: bool operator<(const ScaleFactor& aOther) const { michael@0: return scale < aOther.scale; michael@0: } michael@0: michael@0: bool operator<=(const ScaleFactor& aOther) const { michael@0: return scale <= aOther.scale; michael@0: } michael@0: michael@0: bool operator>(const ScaleFactor& aOther) const { michael@0: return scale > aOther.scale; michael@0: } michael@0: michael@0: bool operator>=(const ScaleFactor& aOther) const { michael@0: return scale >= aOther.scale; michael@0: } michael@0: michael@0: template michael@0: ScaleFactor operator/(const ScaleFactor& aOther) const { michael@0: return ScaleFactor(scale / aOther.scale); michael@0: } michael@0: michael@0: template michael@0: ScaleFactor operator/(const ScaleFactor& aOther) const { michael@0: return ScaleFactor(scale / aOther.scale); michael@0: } michael@0: michael@0: template michael@0: ScaleFactor operator*(const ScaleFactor& aOther) const { michael@0: return ScaleFactor(scale * aOther.scale); michael@0: } michael@0: michael@0: template michael@0: ScaleFactor operator*(const ScaleFactor& aOther) const { michael@0: return ScaleFactor(scale * aOther.scale); michael@0: } michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */