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/SVGAnimatedLength.h"
14 #include "mozilla/dom/SVGLengthBinding.h"
16 enum nsCSSUnit
: uint32_t;
25 * This SVGLength class is currently used for SVGLength *list* attributes only.
26 * The class that is currently used for <length> attributes is
29 * The member mUnit should always be valid, but the member mValue may be
30 * numeric_limits<float>::quiet_NaN() under one circumstances (see the comment
31 * in SetValueAndUnit below). Even if mValue is valid, some methods may return
32 * numeric_limits<float>::quiet_NaN() if they involve a unit conversion that
33 * fails - see comments below.
35 * The DOM wrapper class for this class is DOMSVGLength.
40 : mValue(0.0f
), mUnit(dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN
) {}
42 SVGLength(float aValue
, uint8_t aUnit
) : mValue(aValue
), mUnit(aUnit
) {}
44 bool operator==(const SVGLength
& rhs
) const {
45 return mValue
== rhs
.mValue
&& mUnit
== rhs
.mUnit
;
48 void GetValueAsString(nsAString
& aValue
) const;
51 * This method returns true, unless there was a parse failure, in which
52 * case it returns false (and the length is left unchanged).
54 bool SetValueFromString(const nsAString
& aString
);
57 * This will usually return a valid, finite number. There is one exception
58 * though. If SVGLengthListSMILType has to convert between unit types and the
59 * unit conversion is undefined, it will end up passing in and setting
60 * numeric_limits<float>::quiet_NaN(). The painting code has to be
61 * able to handle NaN anyway, since conversion to user units may fail in
64 float GetValueInCurrentUnits() const { return mValue
; }
66 uint8_t GetUnit() const { return mUnit
; }
68 void SetValueInCurrentUnits(float aValue
) {
69 NS_ASSERTION(std::isfinite(aValue
), "Set invalid SVGLength");
73 void SetValueAndUnit(float aValue
, uint8_t aUnit
) {
79 * If it's not possible to convert this length's value to pixels, then
80 * this method will return numeric_limits<float>::quiet_NaN().
83 float GetValueInPixels(const dom::SVGElement
* aElement
, uint8_t aAxis
) const {
84 return mValue
* GetPixelsPerUnit(dom::SVGElementMetrics(aElement
), aAxis
);
88 * Get this length's value in the units specified.
90 * This method returns numeric_limits<float>::quiet_NaN() if it is not
91 * possible to convert the value to the specified unit.
93 float GetValueInSpecifiedUnit(uint8_t aUnit
, const dom::SVGElement
* aElement
,
96 bool IsPercentage() const {
97 return mUnit
== dom::SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE
;
100 float GetPixelsPerUnit(const dom::UserSpaceMetrics
& aMetrics
,
101 uint8_t aAxis
) const {
102 return GetPixelsPerUnit(aMetrics
, mUnit
, aAxis
);
105 static bool IsValidUnitType(uint16_t aUnitType
) {
106 return aUnitType
> dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN
&&
107 aUnitType
<= dom::SVGLength_Binding::SVG_LENGTHTYPE_PC
;
110 static bool IsAbsoluteUnit(uint8_t aUnit
);
112 static float GetAbsUnitsPerAbsUnit(uint8_t aUnits
, uint8_t aPerUnit
);
114 static nsCSSUnit
SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit
);
116 static void GetUnitString(nsAString
& aUnit
, uint16_t aUnitType
);
118 static uint16_t GetUnitTypeForString(const nsAString
& aUnit
);
121 * Returns the number of pixels per given unit.
123 static float GetPixelsPerUnit(const dom::UserSpaceMetrics
& aMetrics
,
124 uint8_t aUnitType
, uint8_t aAxis
);
131 } // namespace mozilla
133 #endif // DOM_SVG_SVGLENGTH_H_