Line data Source code
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/. */
6 :
7 : #ifndef MOZILLA_SVGLENGTH_H__
8 : #define MOZILLA_SVGLENGTH_H__
9 :
10 : #include "nsDebug.h"
11 : #include "nsIDOMSVGLength.h"
12 : #include "nsMathUtils.h"
13 : #include "mozilla/FloatingPoint.h"
14 :
15 : class nsSVGElement;
16 :
17 : namespace mozilla {
18 :
19 : /**
20 : * This SVGLength class is currently used for SVGLength *list* attributes only.
21 : * The class that is currently used for <length> attributes is nsSVGLength2.
22 : *
23 : * The member mUnit should always be valid, but the member mValue may be
24 : * numeric_limits<float>::quiet_NaN() under one circumstances (see the comment
25 : * in SetValueAndUnit below). Even if mValue is valid, some methods may return
26 : * numeric_limits<float>::quiet_NaN() if they involve a unit conversion that
27 : * fails - see comments below.
28 : *
29 : * The DOM wrapper class for this class is DOMSVGLength.
30 : */
31 : class SVGLength
32 : {
33 : public:
34 :
35 0 : SVGLength()
36 : #ifdef DEBUG
37 0 : : mValue(0.0f)
38 0 : , mUnit(nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN) // caught by IsValid()
39 : #endif
40 0 : {}
41 :
42 0 : SVGLength(float aValue, uint8_t aUnit)
43 0 : : mValue(aValue)
44 0 : , mUnit(aUnit)
45 : {
46 0 : NS_ASSERTION(IsValid(), "Constructed an invalid length");
47 0 : }
48 :
49 0 : SVGLength(const SVGLength &aOther)
50 0 : : mValue(aOther.mValue)
51 0 : , mUnit(aOther.mUnit)
52 0 : {}
53 :
54 0 : SVGLength& operator=(const SVGLength &rhs) {
55 0 : mValue = rhs.mValue;
56 0 : mUnit = rhs.mUnit;
57 0 : return *this;
58 : }
59 :
60 0 : bool operator==(const SVGLength &rhs) const {
61 0 : return mValue == rhs.mValue && mUnit == rhs.mUnit;
62 : }
63 :
64 : void GetValueAsString(nsAString& aValue) const;
65 :
66 : /**
67 : * This method returns true, unless there was a parse failure, in which
68 : * case it returns false (and the length is left unchanged).
69 : */
70 : bool SetValueFromString(const nsAString& aValue);
71 :
72 : /**
73 : * This will usually return a valid, finite number. There is one exception
74 : * though - see the comment in SetValueAndUnit().
75 : */
76 0 : float GetValueInCurrentUnits() const {
77 0 : return mValue;
78 : }
79 :
80 0 : uint8_t GetUnit() const {
81 0 : return mUnit;
82 : }
83 :
84 0 : void SetValueInCurrentUnits(float aValue) {
85 0 : mValue = aValue;
86 0 : NS_ASSERTION(IsValid(), "Set invalid SVGLength");
87 0 : }
88 :
89 0 : void SetValueAndUnit(float aValue, uint8_t aUnit) {
90 0 : mValue = aValue;
91 0 : mUnit = aUnit;
92 :
93 : // IsValid() should always be true, with one exception: if
94 : // SVGLengthListSMILType has to convert between unit types and the unit
95 : // conversion is undefined, it will end up passing in and setting
96 : // numeric_limits<float>::quiet_NaN(). Because of that we only check the
97 : // unit here, and allow mValue to be invalid. The painting code has to be
98 : // able to handle NaN anyway, since conversion to user units may fail in
99 : // general.
100 :
101 0 : NS_ASSERTION(IsValidUnitType(mUnit), "Set invalid SVGLength");
102 0 : }
103 :
104 : /**
105 : * If it's not possible to convert this length's value to user units, then
106 : * this method will return numeric_limits<float>::quiet_NaN().
107 : */
108 0 : float GetValueInUserUnits(const nsSVGElement *aElement, uint8_t aAxis) const {
109 0 : return mValue * GetUserUnitsPerUnit(aElement, aAxis);
110 : }
111 :
112 : /**
113 : * Get this length's value in the units specified.
114 : *
115 : * This method returns numeric_limits<float>::quiet_NaN() if it is not
116 : * possible to convert the value to the specified unit.
117 : */
118 : float GetValueInSpecifiedUnit(uint8_t aUnit,
119 : const nsSVGElement *aElement,
120 : uint8_t aAxis) const;
121 :
122 : bool IsPercentage() const {
123 : return mUnit == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE;
124 : }
125 :
126 0 : static bool IsValidUnitType(uint16_t unit) {
127 0 : return unit > nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN &&
128 0 : unit <= nsIDOMSVGLength::SVG_LENGTHTYPE_PC;
129 : }
130 :
131 : /**
132 : * Returns the number of user units per current unit.
133 : *
134 : * This method returns numeric_limits<float>::quiet_NaN() if the conversion
135 : * factor between the length's current unit and user units is undefined (see
136 : * the comments for GetUserUnitsPerInch and GetUserUnitsPerPercent).
137 : */
138 : float GetUserUnitsPerUnit(const nsSVGElement *aElement, uint8_t aAxis) const;
139 :
140 : private:
141 :
142 : #ifdef DEBUG
143 0 : bool IsValid() const {
144 0 : return IsFinite(mValue) && IsValidUnitType(mUnit);
145 : }
146 : #endif
147 :
148 : /**
149 : * The conversion factor between user units (CSS px) and CSS inches is
150 : * constant: 96 px per inch.
151 : */
152 0 : static float GetUserUnitsPerInch()
153 : {
154 0 : return 96.0;
155 : }
156 :
157 : /**
158 : * The conversion factor between user units and percentage units depends on
159 : * aElement being non-null, and on aElement having a viewport element
160 : * ancestor with only appropriate SVG elements between aElement and that
161 : * ancestor. If that's not the case, then the conversion factor is undefined.
162 : *
163 : * This function returns a non-negative value if the conversion factor is
164 : * defined, otherwise it returns numeric_limits<float>::quiet_NaN().
165 : */
166 : static float GetUserUnitsPerPercent(const nsSVGElement *aElement, uint8_t aAxis);
167 :
168 : float mValue;
169 : uint8_t mUnit;
170 : };
171 :
172 : } // namespace mozilla
173 :
174 : #endif // MOZILLA_SVGLENGTH_H__
|