tor-browser

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

TextAttrs.h (11109B)


      1 /* -*- Mode: C++; tab-width: 2; 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 #ifndef nsTextAttrs_h_
      7 #define nsTextAttrs_h_
      8 
      9 #include "mozilla/FontPropertyTypes.h"
     10 #include "nsCOMPtr.h"
     11 #include "nsColor.h"
     12 #include "nsString.h"
     13 #include "nsStyleConsts.h"
     14 
     15 class nsIFrame;
     16 class nsIContent;
     17 class nsDeviceContext;
     18 
     19 namespace mozilla {
     20 namespace a11y {
     21 
     22 class AccAttributes;
     23 class LocalAccessible;
     24 class HyperTextAccessible;
     25 
     26 /**
     27 * Used to expose text attributes for the hyper text accessible (see
     28 * HyperTextAccessible class).
     29 *
     30 * @note "invalid: spelling" text attribute is implemented entirely in
     31 *       HyperTextAccessible class.
     32 */
     33 class TextAttrsMgr {
     34 public:
     35  /**
     36   * Constructor. Used to expose default text attributes.
     37   */
     38  explicit TextAttrsMgr(HyperTextAccessible* aHyperTextAcc)
     39      : mOffsetAcc(nullptr),
     40        mHyperTextAcc(aHyperTextAcc),
     41        mIncludeDefAttrs(true) {}
     42 
     43  /**
     44   * Constructor. Used to expose text attributes at the given child.
     45   *
     46   * @param aHyperTextAcc    [in] hyper text accessible text attributes are
     47   *                          calculated for
     48   * @param aIncludeDefAttrs [optional] indicates whether default text
     49   *                          attributes should be included into list of exposed
     50   *                          text attributes
     51   * @param aOffsetAcc       [optional] The child accessible the text attributes
     52   *                          should be calculated for
     53   */
     54  TextAttrsMgr(HyperTextAccessible* aHyperTextAcc, bool aIncludeDefAttrs,
     55               LocalAccessible* aOffsetAcc)
     56      : mOffsetAcc(aOffsetAcc),
     57        mHyperTextAcc(aHyperTextAcc),
     58        mIncludeDefAttrs(aIncludeDefAttrs) {}
     59 
     60  /*
     61   * Return text attributes.
     62   *
     63   * @param aAttributes    [in, out] text attributes list
     64   */
     65  void GetAttributes(AccAttributes* aAttributes);
     66 
     67 private:
     68  LocalAccessible* mOffsetAcc;
     69  HyperTextAccessible* mHyperTextAcc;
     70  bool mIncludeDefAttrs;
     71 
     72 protected:
     73  /**
     74   * Interface class of text attribute class implementations.
     75   */
     76  class TextAttr {
     77   public:
     78    /**
     79     * Expose the text attribute to the given attribute set.
     80     *
     81     * @param aAttributes           [in] the given attribute set
     82     * @param aIncludeDefAttrValue  [in] if true then attribute is exposed even
     83     *                               if its value is the same as default one
     84     */
     85    virtual void Expose(AccAttributes* aAttributes,
     86                        bool aIncludeDefAttrValue) = 0;
     87  };
     88 
     89  /**
     90   * Base class to work with text attributes. See derived classes below.
     91   */
     92  template <class T>
     93  class TTextAttr : public TextAttr {
     94   public:
     95    explicit TTextAttr(bool aGetRootValue) : mGetRootValue(aGetRootValue) {}
     96 
     97    // TextAttr
     98    virtual void Expose(AccAttributes* aAttributes,
     99                        bool aIncludeDefAttrValue) override {
    100      if (mGetRootValue) {
    101        if (mIsRootDefined) ExposeValue(aAttributes, mRootNativeValue);
    102        return;
    103      }
    104 
    105      if (mIsDefined) {
    106        if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue) {
    107          ExposeValue(aAttributes, mNativeValue);
    108        }
    109        return;
    110      }
    111 
    112      if (aIncludeDefAttrValue && mIsRootDefined) {
    113        ExposeValue(aAttributes, mRootNativeValue);
    114      }
    115    }
    116 
    117   protected:
    118    // Expose the text attribute with the given value to attribute set.
    119    virtual void ExposeValue(AccAttributes* aAttributes, const T& aValue) = 0;
    120 
    121    // Indicates if root value should be exposed.
    122    bool mGetRootValue;
    123 
    124    // Native value and flag indicating if the value is defined (initialized in
    125    // derived classes). Note, undefined native value means it is inherited
    126    // from root.
    127    MOZ_INIT_OUTSIDE_CTOR T mNativeValue;
    128    MOZ_INIT_OUTSIDE_CTOR bool mIsDefined;
    129 
    130    // Native root value and flag indicating if the value is defined
    131    // (initialized in derived classes).
    132    MOZ_INIT_OUTSIDE_CTOR T mRootNativeValue;
    133    MOZ_INIT_OUTSIDE_CTOR bool mIsRootDefined;
    134  };
    135 
    136  /**
    137   * Class is used for the work with 'language' text attribute.
    138   */
    139  class LangTextAttr : public TTextAttr<nsString> {
    140   public:
    141    LangTextAttr(HyperTextAccessible* aRoot, nsIContent* aRootElm,
    142                 nsIContent* aElm);
    143    virtual ~LangTextAttr();
    144 
    145   protected:
    146    virtual void ExposeValue(AccAttributes* aAttributes,
    147                             const nsString& aValue) override;
    148 
    149   private:
    150    nsCOMPtr<nsIContent> mRootContent;
    151  };
    152 
    153  /**
    154   * Class is used for the 'invalid' text attribute. Note, it calculated
    155   * the attribute from aria-invalid attribute only; invalid:spelling attribute
    156   * calculated from misspelled text in the editor is managed by
    157   * HyperTextAccessible and applied on top of the value from aria-invalid.
    158   */
    159  class InvalidTextAttr : public TTextAttr<uint32_t> {
    160   public:
    161    InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm);
    162    virtual ~InvalidTextAttr() {};
    163 
    164   protected:
    165    enum { eFalse, eGrammar, eSpelling, eTrue };
    166 
    167    // TextAttr
    168    virtual void ExposeValue(AccAttributes* aAttributes,
    169                             const uint32_t& aValue) override;
    170 
    171   private:
    172    bool GetValue(nsIContent* aElm, uint32_t* aValue);
    173    nsIContent* mRootElm;
    174  };
    175 
    176  /**
    177   * Class is used for the work with 'background-color' text attribute.
    178   */
    179  class BGColorTextAttr : public TTextAttr<nscolor> {
    180   public:
    181    BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    182    virtual ~BGColorTextAttr() {}
    183 
    184   protected:
    185    // TextAttr
    186    virtual void ExposeValue(AccAttributes* aAttributes,
    187                             const nscolor& aValue) override;
    188 
    189   private:
    190    bool GetColor(nsIFrame* aFrame, nscolor* aColor);
    191    nsIFrame* mRootFrame;
    192  };
    193 
    194  /**
    195   * Class is used for the work with 'color' text attribute.
    196   */
    197  class ColorTextAttr : public TTextAttr<nscolor> {
    198   public:
    199    ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    200    virtual ~ColorTextAttr() {}
    201 
    202   protected:
    203    // TTextAttr
    204    virtual void ExposeValue(AccAttributes* aAttributes,
    205                             const nscolor& aValue) override;
    206  };
    207 
    208  /**
    209   * Class is used for the work with "font-family" text attribute.
    210   */
    211  class FontFamilyTextAttr : public TTextAttr<nsString> {
    212   public:
    213    FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    214    virtual ~FontFamilyTextAttr() {}
    215 
    216   protected:
    217    // TTextAttr
    218    virtual void ExposeValue(AccAttributes* aAttributes,
    219                             const nsString& aValue) override;
    220 
    221   private:
    222    bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily);
    223  };
    224 
    225  /**
    226   * Class is used for the work with "font-size" text attribute.
    227   */
    228  class FontSizeTextAttr : public TTextAttr<nscoord> {
    229   public:
    230    FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    231    virtual ~FontSizeTextAttr() {}
    232 
    233   protected:
    234    // TTextAttr
    235    virtual void ExposeValue(AccAttributes* aAttributes,
    236                             const nscoord& aValue) override;
    237 
    238   private:
    239    nsDeviceContext* mDC;
    240  };
    241 
    242  /**
    243   * Class is used for the work with "font-style" text attribute.
    244   */
    245  class FontStyleTextAttr : public TTextAttr<mozilla::FontSlantStyle> {
    246   public:
    247    FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    248    virtual ~FontStyleTextAttr() {}
    249 
    250   protected:
    251    // TTextAttr
    252    virtual void ExposeValue(AccAttributes* aAttributes,
    253                             const mozilla::FontSlantStyle& aValue) override;
    254  };
    255 
    256  /**
    257   * Class is used for the work with "font-weight" text attribute.
    258   */
    259  class FontWeightTextAttr : public TTextAttr<mozilla::FontWeight> {
    260   public:
    261    FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    262    virtual ~FontWeightTextAttr() {}
    263 
    264   protected:
    265    // TTextAttr
    266    virtual void ExposeValue(AccAttributes* aAttributes,
    267                             const mozilla::FontWeight& aValue) override;
    268 
    269   private:
    270    mozilla::FontWeight GetFontWeight(nsIFrame* aFrame);
    271  };
    272 
    273  /**
    274   * Class is used for the work with 'auto-generated' text attribute.
    275   */
    276  class AutoGeneratedTextAttr : public TTextAttr<bool> {
    277   public:
    278    AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc,
    279                          LocalAccessible* aAccessible);
    280    virtual ~AutoGeneratedTextAttr() {}
    281 
    282   protected:
    283    // TextAttr
    284    virtual void ExposeValue(AccAttributes* aAttributes,
    285                             const bool& aValue) override;
    286  };
    287 
    288  /**
    289   * TextDecorTextAttr class is used for the work with
    290   * "text-line-through-style", "text-line-through-color",
    291   * "text-underline-style" and "text-underline-color" text attributes.
    292   */
    293 
    294  class TextDecorValue {
    295   public:
    296    TextDecorValue()
    297        : mColor{0},
    298          mLine{StyleTextDecorationLine::NONE},
    299          mStyle{StyleTextDecorationStyle::None} {}
    300    explicit TextDecorValue(nsIFrame* aFrame);
    301 
    302    nscolor Color() const { return mColor; }
    303    mozilla::StyleTextDecorationStyle Style() const { return mStyle; }
    304 
    305    bool IsDefined() const { return IsUnderline() || IsLineThrough(); }
    306    bool IsUnderline() const {
    307      return bool(mLine & mozilla::StyleTextDecorationLine::UNDERLINE);
    308    }
    309    bool IsLineThrough() const {
    310      return bool(mLine & mozilla::StyleTextDecorationLine::LINE_THROUGH);
    311    }
    312 
    313    bool operator==(const TextDecorValue& aValue) const {
    314      return mColor == aValue.mColor && mLine == aValue.mLine &&
    315             mStyle == aValue.mStyle;
    316    }
    317    bool operator!=(const TextDecorValue& aValue) const {
    318      return !(*this == aValue);
    319    }
    320 
    321   private:
    322    nscolor mColor;
    323    mozilla::StyleTextDecorationLine mLine;
    324    mozilla::StyleTextDecorationStyle mStyle;
    325  };
    326 
    327  class TextDecorTextAttr : public TTextAttr<TextDecorValue> {
    328   public:
    329    TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    330    virtual ~TextDecorTextAttr() {}
    331 
    332   protected:
    333    // TextAttr
    334    virtual void ExposeValue(AccAttributes* aAttributes,
    335                             const TextDecorValue& aValue) override;
    336  };
    337 
    338  /**
    339   * Class is used for the work with "text-position" text attribute.
    340   */
    341 
    342  enum TextPosValue { eTextPosBaseline, eTextPosSub, eTextPosSuper };
    343 
    344  class TextPosTextAttr : public TTextAttr<Maybe<TextPosValue>> {
    345   public:
    346    TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame,
    347                    nsIContent* aRootElm, nsIContent* aElm);
    348    virtual ~TextPosTextAttr() {}
    349 
    350   protected:
    351    // TextAttr
    352    virtual void ExposeValue(AccAttributes* aAttributes,
    353                             const Maybe<TextPosValue>& aValue) override;
    354 
    355   private:
    356    Maybe<TextPosValue> GetAriaTextPosValue(nsIContent* aElm) const;
    357    Maybe<TextPosValue> GetAriaTextPosValue(nsIContent* aElm,
    358                                            nsIFrame*& ariaFrame) const;
    359    Maybe<TextPosValue> GetLayoutTextPosValue(nsIFrame* aFrame) const;
    360    nsIContent* mRootElm;
    361  };
    362 
    363 };  // TextAttrMgr
    364 
    365 }  // namespace a11y
    366 }  // namespace mozilla
    367 
    368 #endif