Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / SVGLength.h
bloba16a43d2d671ce41a1444a21b4d028c97ef5010c
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_
10 #include "nsDebug.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;
18 namespace mozilla {
20 namespace dom {
21 class SVGElement;
24 /**
25 * This SVGLength class is currently used for SVGLength *list* attributes only.
26 * The class that is currently used for <length> attributes is
27 * SVGAnimatedLength.
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.
37 class SVGLength {
38 public:
39 SVGLength()
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;
50 /**
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);
56 /**
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
62 * general.
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");
70 mValue = aValue;
73 void SetValueAndUnit(float aValue, uint8_t aUnit) {
74 mValue = aValue;
75 mUnit = aUnit;
78 /**
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);
87 /**
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,
94 uint8_t aAxis) const;
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);
126 private:
127 float mValue;
128 uint8_t mUnit;
131 } // namespace mozilla
133 #endif // DOM_SVG_SVGLENGTH_H_