tor-browser

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

FontPropertyTypes.h (4783B)


      1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /* font specific types shared by both thebes and layout */
      7 
      8 #ifndef GFX_FONT_PROPERTY_TYPES_H
      9 #define GFX_FONT_PROPERTY_TYPES_H
     10 
     11 #include <algorithm>
     12 #include <cstdint>
     13 #include <cmath>
     14 #include <utility>
     15 
     16 #include <ctype.h>
     17 #include <string.h>
     18 
     19 #include "mozilla/Assertions.h"
     20 #include "mozilla/ServoStyleConsts.h"
     21 #include "nsString.h"
     22 
     23 /*
     24 * This file is separate from gfxFont.h so that layout can include it
     25 * without bringing in gfxFont.h and everything it includes.
     26 */
     27 
     28 namespace mozilla {
     29 
     30 using FontSlantStyle = StyleFontStyle;
     31 using FontWeight = StyleFontWeight;
     32 using FontStretch = StyleFontStretch;
     33 
     34 /**
     35 * Convenience type to hold a <min, max> pair representing a range of values.
     36 *
     37 * The min and max are both inclusive, so when min == max the range represents
     38 * a single value (not an empty range).
     39 */
     40 template <class T, class Derived>
     41 class FontPropertyRange {
     42  // This implementation assumes the underlying property type is a 16-bit value
     43  // (see FromScalar and AsScalar below).
     44  static_assert(sizeof(T) == 2, "FontPropertyValue should be a 16-bit type!");
     45 
     46 public:
     47  /**
     48   * Construct a range from given minimum and maximum values (inclusive).
     49   */
     50  FontPropertyRange(T aMin, T aMax) : mValues(aMin, aMax) {
     51    MOZ_ASSERT(aMin <= aMax);
     52  }
     53 
     54  /**
     55   * Construct a range representing a single value (min==max).
     56   */
     57  explicit FontPropertyRange(T aValue) : mValues(aValue, aValue) {}
     58 
     59  explicit FontPropertyRange(const FontPropertyRange& aOther) = default;
     60  FontPropertyRange& operator=(const FontPropertyRange& aOther) = default;
     61 
     62  T Min() const { return mValues.first; }
     63  T Max() const { return mValues.second; }
     64 
     65  /**
     66   * Clamp the given value to this range.
     67   */
     68  T Clamp(T aValue) const { return std::clamp(aValue, Min(), Max()); }
     69 
     70  /**
     71   * Return whether the range consists of a single unique value.
     72   */
     73  bool IsSingle() const { return Min() == Max(); }
     74 
     75  bool operator==(const FontPropertyRange& aOther) const {
     76    return mValues == aOther.mValues;
     77  }
     78  bool operator!=(const FontPropertyRange& aOther) const {
     79    return mValues != aOther.mValues;
     80  }
     81 
     82  /**
     83   * Conversion of the property range to/from a single 32-bit scalar value,
     84   * suitable for IPC serialization, hashing, caching.
     85   *
     86   * No assumptions should be made about the numeric value of the scalar.
     87   *
     88   * This depends on the underlying property type being a 16-bit value!
     89   */
     90  using ScalarType = uint32_t;
     91 
     92  ScalarType AsScalar() const {
     93    return (mValues.first.UnsignedRaw() << 16) | mValues.second.UnsignedRaw();
     94  }
     95 
     96  static Derived FromScalar(ScalarType aScalar) {
     97    static_assert(std::is_base_of_v<FontPropertyRange, Derived>);
     98    return Derived(T::FromRaw(aScalar >> 16), T::FromRaw(aScalar & 0xffff));
     99  }
    100 
    101 protected:
    102  std::pair<T, T> mValues;
    103 };
    104 
    105 class WeightRange : public FontPropertyRange<FontWeight, WeightRange> {
    106 public:
    107  WeightRange(FontWeight aMin, FontWeight aMax)
    108      : FontPropertyRange(aMin, aMax) {}
    109 
    110  explicit WeightRange(FontWeight aWeight) : FontPropertyRange(aWeight) {}
    111 
    112  WeightRange(const WeightRange& aOther) = default;
    113 
    114  void ToString(nsACString& aOutString, const char* aDelim = "..") const {
    115    aOutString.AppendFloat(Min().ToFloat());
    116    if (!IsSingle()) {
    117      aOutString.Append(aDelim);
    118      aOutString.AppendFloat(Max().ToFloat());
    119    }
    120  }
    121 };
    122 
    123 class StretchRange : public FontPropertyRange<FontStretch, StretchRange> {
    124 public:
    125  StretchRange(FontStretch aMin, FontStretch aMax)
    126      : FontPropertyRange(aMin, aMax) {}
    127 
    128  explicit StretchRange(FontStretch aStretch) : FontPropertyRange(aStretch) {}
    129 
    130  StretchRange(const StretchRange& aOther) = default;
    131 
    132  void ToString(nsACString& aOutString, const char* aDelim = "..") const {
    133    aOutString.AppendFloat(Min().ToFloat());
    134    if (!IsSingle()) {
    135      aOutString.Append(aDelim);
    136      aOutString.AppendFloat(Max().ToFloat());
    137    }
    138  }
    139 };
    140 
    141 class SlantStyleRange
    142    : public FontPropertyRange<FontSlantStyle, SlantStyleRange> {
    143 public:
    144  SlantStyleRange(FontSlantStyle aMin, FontSlantStyle aMax)
    145      : FontPropertyRange(aMin, aMax) {}
    146 
    147  explicit SlantStyleRange(FontSlantStyle aStyle) : FontPropertyRange(aStyle) {}
    148 
    149  SlantStyleRange(const SlantStyleRange& aOther) = default;
    150 
    151  void ToString(nsACString& aOutString, const char* aDelim = "..") const {
    152    Min().ToString(aOutString);
    153    if (!IsSingle()) {
    154      aOutString.Append(aDelim);
    155      Max().ToString(aOutString);
    156    }
    157  }
    158 };
    159 
    160 }  // namespace mozilla
    161 
    162 #endif  // GFX_FONT_PROPERTY_TYPES_H