Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / SVGLength.h
blobd8465c5d33d8195d5c65f0ab2fb847ca8e07d309
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 "mozilla/dom/SVGAnimatedLength.h"
12 #include "mozilla/dom/SVGLengthBinding.h"
14 enum nsCSSUnit : uint32_t;
16 namespace mozilla {
18 namespace dom {
19 class SVGElement;
22 /**
23 * This SVGLength class is currently used for SVGLength *list* attributes only.
24 * The class that is currently used for <length> attributes is
25 * SVGAnimatedLength.
27 * The member mUnit should always be valid, but the member mValue may be
28 * numeric_limits<float>::quiet_NaN() under one circumstances (see the comment
29 * in SetValueAndUnit below). Even if mValue is valid, some methods may return
30 * numeric_limits<float>::quiet_NaN() if they involve a unit conversion that
31 * fails - see comments below.
33 * The DOM wrapper class for this class is DOMSVGLength.
35 class SVGLength {
36 public:
37 SVGLength()
38 : mValue(0.0f), mUnit(dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN) {}
40 SVGLength(float aValue, uint8_t aUnit) : mValue(aValue), mUnit(aUnit) {}
42 bool operator==(const SVGLength& rhs) const {
43 return mValue == rhs.mValue && mUnit == rhs.mUnit;
46 void GetValueAsString(nsAString& aValue) const;
48 /**
49 * This method returns true, unless there was a parse failure, in which
50 * case it returns false (and the length is left unchanged).
52 bool SetValueFromString(const nsAString& aString);
54 /**
55 * This will usually return a valid, finite number. There is one exception
56 * though. If SVGLengthListSMILType has to convert between unit types and the
57 * unit conversion is undefined, it will end up passing in and setting
58 * numeric_limits<float>::quiet_NaN(). The painting code has to be
59 * able to handle NaN anyway, since conversion to user units may fail in
60 * general.
62 float GetValueInCurrentUnits() const { return mValue; }
64 uint8_t GetUnit() const { return mUnit; }
66 void SetValueInCurrentUnits(float aValue) {
67 NS_ASSERTION(std::isfinite(aValue), "Set invalid SVGLength");
68 mValue = aValue;
71 void SetValueAndUnit(float aValue, uint8_t aUnit) {
72 mValue = aValue;
73 mUnit = aUnit;
76 /**
77 * If it's not possible to convert this length's value to pixels, then
78 * this method will return numeric_limits<float>::quiet_NaN().
80 float GetValueInPixels(const dom::SVGElement* aElement, uint8_t aAxis) const {
81 return mValue * GetPixelsPerUnit(dom::SVGElementMetrics(aElement), aAxis);
84 float GetValueInPixelsWithZoom(const dom::SVGElement* aElement,
85 uint8_t aAxis) const {
86 return mValue *
87 GetPixelsPerUnitWithZoom(dom::SVGElementMetrics(aElement), aAxis);
90 /**
91 * Get this length's value in the units specified.
93 * This method returns numeric_limits<float>::quiet_NaN() if it is not
94 * possible to convert the value to the specified unit.
96 float GetValueInSpecifiedUnit(uint8_t aUnit, const dom::SVGElement* aElement,
97 uint8_t aAxis) const;
99 bool IsPercentage() const { return IsPercentageUnit(mUnit); }
101 float GetPixelsPerUnitWithZoom(const dom::UserSpaceMetrics& aMetrics,
102 uint8_t aAxis) const {
103 return GetPixelsPerUnit(aMetrics, mUnit, aAxis, true);
106 float GetPixelsPerUnit(const dom::UserSpaceMetrics& aMetrics,
107 uint8_t aAxis) const {
108 return GetPixelsPerUnit(aMetrics, mUnit, aAxis, false);
111 static bool IsValidUnitType(uint16_t aUnitType) {
112 return aUnitType > dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN &&
113 aUnitType <= dom::SVGLength_Binding::SVG_LENGTHTYPE_PC;
116 static bool IsPercentageUnit(uint8_t aUnit) {
117 return aUnit == dom::SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE;
120 static bool IsAbsoluteUnit(uint8_t aUnit);
122 static bool IsFontRelativeUnit(uint8_t aUnit);
124 static float GetAbsUnitsPerAbsUnit(uint8_t aUnits, uint8_t aPerUnit);
126 static nsCSSUnit SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit);
128 static void GetUnitString(nsAString& aUnit, uint16_t aUnitType);
130 static uint16_t GetUnitTypeForString(const nsAString& aUnit);
133 * Returns the number of pixels per given unit.
135 static float GetPixelsPerUnit(const dom::UserSpaceMetrics& aMetrics,
136 uint8_t aUnitType, uint8_t aAxis,
137 bool aApplyZoom);
139 private:
140 float mValue;
141 uint8_t mUnit;
144 } // namespace mozilla
146 #endif // DOM_SVG_SVGLENGTH_H_