2010-07-16 14:42:12 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
#ifndef MOZILLA_SVGLENGTH_H__
|
|
|
|
#define MOZILLA_SVGLENGTH_H__
|
|
|
|
|
2012-01-26 01:57:21 -08:00
|
|
|
#include "nsDebug.h"
|
2010-07-16 14:42:12 -07:00
|
|
|
#include "nsIDOMSVGLength.h"
|
2011-08-01 10:43:23 -07:00
|
|
|
#include "nsMathUtils.h"
|
2014-10-01 02:26:00 -07:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
class nsSVGElement;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This SVGLength class is currently used for SVGLength *list* attributes only.
|
|
|
|
* The class that is currently used for <length> attributes is nsSVGLength2.
|
|
|
|
*
|
|
|
|
* The member mUnit should always be valid, but the member mValue may be
|
|
|
|
* numeric_limits<float>::quiet_NaN() under one circumstances (see the comment
|
|
|
|
* in SetValueAndUnit below). Even if mValue is valid, some methods may return
|
|
|
|
* numeric_limits<float>::quiet_NaN() if they involve a unit conversion that
|
|
|
|
* fails - see comments below.
|
|
|
|
*
|
|
|
|
* The DOM wrapper class for this class is DOMSVGLength.
|
|
|
|
*/
|
|
|
|
class SVGLength
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SVGLength()
|
|
|
|
#ifdef DEBUG
|
|
|
|
: mValue(0.0f)
|
|
|
|
, mUnit(nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN) // caught by IsValid()
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
SVGLength(float aValue, uint8_t aUnit)
|
2010-07-16 14:42:12 -07:00
|
|
|
: mValue(aValue)
|
|
|
|
, mUnit(aUnit)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsValid(), "Constructed an invalid length");
|
|
|
|
}
|
|
|
|
|
|
|
|
SVGLength(const SVGLength &aOther)
|
|
|
|
: mValue(aOther.mValue)
|
|
|
|
, mUnit(aOther.mUnit)
|
|
|
|
{}
|
|
|
|
|
|
|
|
SVGLength& operator=(const SVGLength &rhs) {
|
|
|
|
mValue = rhs.mValue;
|
|
|
|
mUnit = rhs.mUnit;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool operator==(const SVGLength &rhs) const {
|
2010-07-16 14:42:12 -07:00
|
|
|
return mValue == rhs.mValue && mUnit == rhs.mUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetValueAsString(nsAString& aValue) const;
|
|
|
|
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* This method returns true, unless there was a parse failure, in which
|
|
|
|
* case it returns false (and the length is left unchanged).
|
2010-07-16 14:42:12 -07:00
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool SetValueFromString(const nsAString& aValue);
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This will usually return a valid, finite number. There is one exception
|
|
|
|
* though - see the comment in SetValueAndUnit().
|
|
|
|
*/
|
|
|
|
float GetValueInCurrentUnits() const {
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t GetUnit() const {
|
2010-07-16 14:42:12 -07:00
|
|
|
return mUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValueInCurrentUnits(float aValue) {
|
|
|
|
mValue = aValue;
|
|
|
|
NS_ASSERTION(IsValid(), "Set invalid SVGLength");
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetValueAndUnit(float aValue, uint8_t aUnit) {
|
2010-07-16 14:42:12 -07:00
|
|
|
mValue = aValue;
|
|
|
|
mUnit = aUnit;
|
|
|
|
|
|
|
|
// IsValid() should always be true, with one exception: if
|
|
|
|
// SVGLengthListSMILType has to convert between unit types and the unit
|
|
|
|
// conversion is undefined, it will end up passing in and setting
|
|
|
|
// numeric_limits<float>::quiet_NaN(). Because of that we only check the
|
|
|
|
// unit here, and allow mValue to be invalid. The painting code has to be
|
|
|
|
// able to handle NaN anyway, since conversion to user units may fail in
|
|
|
|
// general.
|
|
|
|
|
|
|
|
NS_ASSERTION(IsValidUnitType(mUnit), "Set invalid SVGLength");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If it's not possible to convert this length's value to user units, then
|
|
|
|
* this method will return numeric_limits<float>::quiet_NaN().
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
float GetValueInUserUnits(const nsSVGElement *aElement, uint8_t aAxis) const {
|
2010-07-16 14:42:12 -07:00
|
|
|
return mValue * GetUserUnitsPerUnit(aElement, aAxis);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this length's value in the units specified.
|
|
|
|
*
|
|
|
|
* This method returns numeric_limits<float>::quiet_NaN() if it is not
|
|
|
|
* possible to convert the value to the specified unit.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
float GetValueInSpecifiedUnit(uint8_t aUnit,
|
2010-07-16 14:42:12 -07:00
|
|
|
const nsSVGElement *aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aAxis) const;
|
2010-07-16 14:42:12 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPercentage() const {
|
2010-07-16 14:42:12 -07:00
|
|
|
return mUnit == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool IsValidUnitType(uint16_t unit) {
|
2010-07-16 14:42:12 -07:00
|
|
|
return unit > nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN &&
|
|
|
|
unit <= nsIDOMSVGLength::SVG_LENGTHTYPE_PC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of user units per current unit.
|
|
|
|
*
|
|
|
|
* This method returns numeric_limits<float>::quiet_NaN() if the conversion
|
|
|
|
* factor between the length's current unit and user units is undefined (see
|
|
|
|
* the comments for GetUserUnitsPerInch and GetUserUnitsPerPercent).
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
float GetUserUnitsPerUnit(const nsSVGElement *aElement, uint8_t aAxis) const;
|
2010-07-16 14:42:12 -07:00
|
|
|
|
2013-12-24 14:09:22 -08:00
|
|
|
private:
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool IsValid() const {
|
2014-10-01 02:26:00 -07:00
|
|
|
return IsFinite(mValue) && IsValidUnitType(mUnit);
|
2013-12-24 14:09:22 -08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-16 14:42:12 -07:00
|
|
|
/**
|
2010-08-13 02:58:04 -07:00
|
|
|
* The conversion factor between user units (CSS px) and CSS inches is
|
|
|
|
* constant: 96 px per inch.
|
2010-07-16 14:42:12 -07:00
|
|
|
*/
|
2010-08-13 02:58:04 -07:00
|
|
|
static float GetUserUnitsPerInch()
|
|
|
|
{
|
|
|
|
return 96.0;
|
|
|
|
}
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The conversion factor between user units and percentage units depends on
|
|
|
|
* aElement being non-null, and on aElement having a viewport element
|
|
|
|
* ancestor with only appropriate SVG elements between aElement and that
|
|
|
|
* ancestor. If that's not the case, then the conversion factor is undefined.
|
|
|
|
*
|
|
|
|
* This function returns a non-negative value if the conversion factor is
|
|
|
|
* defined, otherwise it returns numeric_limits<float>::quiet_NaN().
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static float GetUserUnitsPerPercent(const nsSVGElement *aElement, uint8_t aAxis);
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
float mValue;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t mUnit;
|
2010-07-16 14:42:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // MOZILLA_SVGLENGTH_H__
|