tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGLength.h (5011B)


      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 DOM_SVG_SVGLENGTH_H_
      8 #define DOM_SVG_SVGLENGTH_H_
      9 
     10 #include "mozilla/dom/SVGAnimatedLength.h"
     11 #include "mozilla/dom/SVGLengthBinding.h"
     12 #include "nsDebug.h"
     13 
     14 enum nsCSSUnit : uint32_t;
     15 
     16 namespace mozilla {
     17 
     18 namespace dom {
     19 class SVGElement;
     20 }
     21 
     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.
     26 *
     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.
     32 *
     33 * The DOM wrapper class for this class is DOMSVGLength.
     34 */
     35 class SVGLength {
     36 public:
     37  SVGLength()
     38      : mValue(0.0f), mUnit(dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN) {}
     39 
     40  SVGLength(float aValue, uint8_t aUnit) : mValue(aValue), mUnit(aUnit) {}
     41 
     42  bool operator==(const SVGLength& rhs) const {
     43    return mValue == rhs.mValue && mUnit == rhs.mUnit;
     44  }
     45 
     46  void GetValueAsString(nsAString& aValue) const;
     47 
     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).
     51   */
     52  bool SetValueFromString(const nsAString& aString);
     53 
     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.
     61   */
     62  float GetValueInCurrentUnits() const { return mValue; }
     63 
     64  uint8_t GetUnit() const { return mUnit; }
     65 
     66  void SetValueInCurrentUnits(float aValue) {
     67    NS_ASSERTION(std::isfinite(aValue), "Set invalid SVGLength");
     68    mValue = aValue;
     69  }
     70 
     71  void SetValueAndUnit(float aValue, uint8_t aUnit) {
     72    mValue = aValue;
     73    mUnit = aUnit;
     74  }
     75 
     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().
     79   */
     80  float GetValueInPixels(const dom::SVGElement* aElement, uint8_t aAxis) const {
     81    return mValue * GetPixelsPerUnit(dom::SVGElementMetrics(aElement), aAxis);
     82  }
     83 
     84  float GetValueInPixelsWithZoom(const dom::SVGElement* aElement,
     85                                 uint8_t aAxis) const {
     86    return mValue *
     87           GetPixelsPerUnitWithZoom(dom::SVGElementMetrics(aElement), aAxis);
     88  }
     89 
     90  /**
     91   * Get this length's value in the units specified.
     92   *
     93   * This method returns numeric_limits<float>::quiet_NaN() if it is not
     94   * possible to convert the value to the specified unit.
     95   */
     96  float GetValueInSpecifiedUnit(uint8_t aUnit, const dom::SVGElement* aElement,
     97                                uint8_t aAxis) const;
     98 
     99  bool IsPercentage() const { return IsPercentageUnit(mUnit); }
    100 
    101  float GetPixelsPerUnitWithZoom(const dom::UserSpaceMetrics& aMetrics,
    102                                 uint8_t aAxis) const {
    103    return GetPixelsPerUnit(aMetrics, mUnit, aAxis, true);
    104  }
    105 
    106  float GetPixelsPerUnit(const dom::UserSpaceMetrics& aMetrics,
    107                         uint8_t aAxis) const {
    108    return GetPixelsPerUnit(aMetrics, mUnit, aAxis, false);
    109  }
    110 
    111  static bool IsValidUnitType(uint16_t aUnitType) {
    112    return aUnitType > dom::SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN &&
    113           aUnitType <= dom::SVGLength_Binding::SVG_LENGTHTYPE_PC;
    114  }
    115 
    116  static bool IsPercentageUnit(uint8_t aUnit) {
    117    return aUnit == dom::SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE;
    118  }
    119 
    120  static bool IsAbsoluteUnit(uint8_t aUnit);
    121 
    122  static bool IsFontRelativeUnit(uint8_t aUnit);
    123 
    124  static float GetAbsUnitsPerAbsUnit(uint8_t aUnits, uint8_t aPerUnit);
    125 
    126  static nsCSSUnit SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit);
    127 
    128  static void GetUnitString(nsAString& aUnit, uint16_t aUnitType);
    129 
    130  static uint16_t GetUnitTypeForString(const nsAString& aUnit);
    131 
    132  /**
    133   * Returns the number of pixels per given unit.
    134   */
    135  static float GetPixelsPerUnit(const dom::UserSpaceMetrics& aMetrics,
    136                                uint8_t aUnitType, uint8_t aAxis,
    137                                bool aApplyZoom);
    138 
    139  static float GetPixelsPerCSSUnit(const dom::UserSpaceMetrics& aMetrics,
    140                                   nsCSSUnit aCSSUnit, uint8_t aAxis,
    141                                   bool aApplyZoom);
    142 
    143 private:
    144  float mValue;
    145  uint8_t mUnit;
    146 };
    147 
    148 }  // namespace mozilla
    149 
    150 #endif  // DOM_SVG_SVGLENGTH_H_