1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef MOZILLA_SVGLENGTH_H__
7 #define MOZILLA_SVGLENGTH_H__
10 #include "nsIDOMSVGLength.h"
11 #include "nsMathUtils.h"
12 #include "mozilla/FloatingPoint.h"
19 * This SVGLength class is currently used for SVGLength *list* attributes only.
20 * The class that is currently used for <length> attributes is nsSVGLength2.
22 * The member mUnit should always be valid, but the member mValue may be
23 * numeric_limits<float>::quiet_NaN() under one circumstances (see the comment
24 * in SetValueAndUnit below). Even if mValue is valid, some methods may return
25 * numeric_limits<float>::quiet_NaN() if they involve a unit conversion that
26 * fails - see comments below.
28 * The DOM wrapper class for this class is DOMSVGLength.
37 , mUnit(nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN
) // caught by IsValid()
41 SVGLength(float aValue
, uint8_t aUnit
)
45 NS_ASSERTION(IsValid(), "Constructed an invalid length");
48 SVGLength(const SVGLength
&aOther
)
49 : mValue(aOther
.mValue
)
53 SVGLength
& operator=(const SVGLength
&rhs
) {
59 bool operator==(const SVGLength
&rhs
) const {
60 return mValue
== rhs
.mValue
&& mUnit
== rhs
.mUnit
;
63 void GetValueAsString(nsAString
& aValue
) const;
66 * This method returns true, unless there was a parse failure, in which
67 * case it returns false (and the length is left unchanged).
69 bool SetValueFromString(const nsAString
& aValue
);
72 * This will usually return a valid, finite number. There is one exception
73 * though - see the comment in SetValueAndUnit().
75 float GetValueInCurrentUnits() const {
79 uint8_t GetUnit() const {
83 void SetValueInCurrentUnits(float aValue
) {
85 NS_ASSERTION(IsValid(), "Set invalid SVGLength");
88 void SetValueAndUnit(float aValue
, uint8_t aUnit
) {
92 // IsValid() should always be true, with one exception: if
93 // SVGLengthListSMILType has to convert between unit types and the unit
94 // conversion is undefined, it will end up passing in and setting
95 // numeric_limits<float>::quiet_NaN(). Because of that we only check the
96 // unit here, and allow mValue to be invalid. The painting code has to be
97 // able to handle NaN anyway, since conversion to user units may fail in
100 NS_ASSERTION(IsValidUnitType(mUnit
), "Set invalid SVGLength");
104 * If it's not possible to convert this length's value to user units, then
105 * this method will return numeric_limits<float>::quiet_NaN().
107 float GetValueInUserUnits(const nsSVGElement
*aElement
, uint8_t aAxis
) const {
108 return mValue
* GetUserUnitsPerUnit(aElement
, aAxis
);
112 * Get this length's value in the units specified.
114 * This method returns numeric_limits<float>::quiet_NaN() if it is not
115 * possible to convert the value to the specified unit.
117 float GetValueInSpecifiedUnit(uint8_t aUnit
,
118 const nsSVGElement
*aElement
,
119 uint8_t aAxis
) const;
121 bool IsPercentage() const {
122 return mUnit
== nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE
;
125 static bool IsValidUnitType(uint16_t unit
) {
126 return unit
> nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN
&&
127 unit
<= nsIDOMSVGLength::SVG_LENGTHTYPE_PC
;
131 * Returns the number of user units per current unit.
133 * This method returns numeric_limits<float>::quiet_NaN() if the conversion
134 * factor between the length's current unit and user units is undefined (see
135 * the comments for GetUserUnitsPerInch and GetUserUnitsPerPercent).
137 float GetUserUnitsPerUnit(const nsSVGElement
*aElement
, uint8_t aAxis
) const;
142 bool IsValid() const {
143 return IsFinite(mValue
) && IsValidUnitType(mUnit
);
148 * The conversion factor between user units (CSS px) and CSS inches is
149 * constant: 96 px per inch.
151 static float GetUserUnitsPerInch()
157 * The conversion factor between user units and percentage units depends on
158 * aElement being non-null, and on aElement having a viewport element
159 * ancestor with only appropriate SVG elements between aElement and that
160 * ancestor. If that's not the case, then the conversion factor is undefined.
162 * This function returns a non-negative value if the conversion factor is
163 * defined, otherwise it returns numeric_limits<float>::quiet_NaN().
165 static float GetUserUnitsPerPercent(const nsSVGElement
*aElement
, uint8_t aAxis
);
171 } // namespace mozilla
173 #endif // MOZILLA_SVGLENGTH_H__