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/. */
7 #ifndef DOM_SVG_SVGLENGTH_H_
8 #define DOM_SVG_SVGLENGTH_H_
11 #include "nsMathUtils.h"
12 #include "mozilla/FloatingPoint.h"
13 #include "mozilla/dom/SVGLengthBinding.h"
22 * This SVGLength class is currently used for SVGLength *list* attributes only.
23 * The class that is currently used for <length> attributes is
26 * The member mUnit should always be valid, but the member mValue may be
27 * numeric_limits<float>::quiet_NaN() under one circumstances (see the comment
28 * in SetValueAndUnit below). Even if mValue is valid, some methods may return
29 * numeric_limits<float>::quiet_NaN() if they involve a unit conversion that
30 * fails - see comments below.
32 * The DOM wrapper class for this class is DOMSVGLength.
38 mUnit(dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN
) // caught by
42 SVGLength(float aValue
, uint8_t aUnit
) : mValue(aValue
), mUnit(aUnit
) {
43 NS_ASSERTION(IsValid(), "Constructed an invalid length");
46 bool operator==(const SVGLength
& rhs
) const {
47 return mValue
== rhs
.mValue
&& mUnit
== rhs
.mUnit
;
50 void GetValueAsString(nsAString
& aValue
) const;
53 * This method returns true, unless there was a parse failure, in which
54 * case it returns false (and the length is left unchanged).
56 bool SetValueFromString(const nsAString
& aString
);
59 * This will usually return a valid, finite number. There is one exception
60 * though - see the comment in SetValueAndUnit().
62 float GetValueInCurrentUnits() const { return mValue
; }
64 uint8_t GetUnit() const { return mUnit
; }
66 void SetValueInCurrentUnits(float aValue
) {
68 NS_ASSERTION(IsValid(), "Set invalid SVGLength");
71 void SetValueAndUnit(float aValue
, uint8_t aUnit
) {
75 // IsValid() should always be true, with one exception: if
76 // SVGLengthListSMILType has to convert between unit types and the unit
77 // conversion is undefined, it will end up passing in and setting
78 // numeric_limits<float>::quiet_NaN(). Because of that we only check the
79 // unit here, and allow mValue to be invalid. The painting code has to be
80 // able to handle NaN anyway, since conversion to user units may fail in
83 NS_ASSERTION(IsValidUnitType(mUnit
), "Set invalid SVGLength");
87 * If it's not possible to convert this length's value to user units, then
88 * this method will return numeric_limits<float>::quiet_NaN().
90 float GetValueInUserUnits(const dom::SVGElement
* aElement
,
91 uint8_t aAxis
) const {
92 return mValue
* GetUserUnitsPerUnit(aElement
, aAxis
);
96 * Get this length's value in the units specified.
98 * This method returns numeric_limits<float>::quiet_NaN() if it is not
99 * possible to convert the value to the specified unit.
101 float GetValueInSpecifiedUnit(uint8_t aUnit
, const dom::SVGElement
* aElement
,
102 uint8_t aAxis
) const;
104 bool IsPercentage() const {
105 return mUnit
== dom::SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE
;
108 static bool IsValidUnitType(uint16_t unit
) {
109 return unit
> dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN
&&
110 unit
<= dom::SVGLength_Binding::SVG_LENGTHTYPE_PC
;
114 * Returns the number of user units per current unit.
116 * This method returns numeric_limits<float>::quiet_NaN() if the conversion
117 * factor between the length's current unit and user units is undefined (see
118 * the comments for GetUserUnitsPerInch and GetUserUnitsPerPercent).
120 float GetUserUnitsPerUnit(const dom::SVGElement
* aElement
,
121 uint8_t aAxis
) const;
125 bool IsValid() const { return IsFinite(mValue
) && IsValidUnitType(mUnit
); }
129 * The conversion factor between user units (CSS px) and CSS inches is
130 * constant: 96 px per inch.
132 static float GetUserUnitsPerInch() { return 96.0; }
135 * The conversion factor between user units and percentage units depends on
136 * aElement being non-null, and on aElement having a viewport element
137 * ancestor with only appropriate SVG elements between aElement and that
138 * ancestor. If that's not the case, then the conversion factor is undefined.
140 * This function returns a non-negative value if the conversion factor is
141 * defined, otherwise it returns numeric_limits<float>::quiet_NaN().
143 static float GetUserUnitsPerPercent(const dom::SVGElement
* aElement
,
150 } // namespace mozilla
152 #endif // DOM_SVG_SVGLENGTH_H_