accessible/src/base/TextAttrs.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 #ifndef nsTextAttrs_h_
     7 #define nsTextAttrs_h_
     9 #include "nsCOMPtr.h"
    10 #include "nsColor.h"
    11 #include "nsStyleConsts.h"
    13 class nsIFrame;
    14 class nsIPersistentProperties;
    15 class nsIContent;
    16 class nsDeviceContext;
    18 namespace mozilla {
    19 namespace a11y {
    21 class Accessible;
    22 class HyperTextAccessible;
    24 /**
    25  * Used to expose text attributes for the hyper text accessible (see
    26  * HyperTextAccessible class).
    27  *
    28  * @note "invalid: spelling" text attribute is implemented entirely in
    29  *       HyperTextAccessible class.
    30  */
    31 class TextAttrsMgr
    32 {
    33 public:
    34   /**
    35    * Constructor. Used to expose default text attributes.
    36    */
    37   TextAttrsMgr(HyperTextAccessible* aHyperTextAcc) :
    38     mOffsetAcc(nullptr),  mHyperTextAcc(aHyperTextAcc),
    39     mOffsetAccIdx(-1), mIncludeDefAttrs(true) { }
    41   /**
    42    * Constructor. Used to expose text attributes at the given offset.
    43    *
    44    * @param aHyperTextAcc    [in] hyper text accessible text attributes are
    45    *                          calculated for
    46    * @param aIncludeDefAttrs [optional] indicates whether default text
    47    *                          attributes should be included into list of exposed
    48    *                          text attributes
    49    * @param oOffsetAcc       [optional] offset an accessible the text attributes
    50    *                          should be calculated for
    51    * @param oOffsetAccIdx    [optional] index in parent of offset accessible
    52    */
    53   TextAttrsMgr(HyperTextAccessible* aHyperTextAcc,
    54                bool aIncludeDefAttrs,
    55                Accessible* aOffsetAcc,
    56                int32_t aOffsetAccIdx) :
    57     mOffsetAcc(aOffsetAcc), mHyperTextAcc(aHyperTextAcc),
    58     mOffsetAccIdx(aOffsetAccIdx), mIncludeDefAttrs(aIncludeDefAttrs) { }
    60   /*
    61    * Return text attributes and hyper text offsets where these attributes are
    62    * applied. Offsets are calculated in the case of non default attributes.
    63    *
    64    * @note In the case of default attributes pointers on hyper text offsets
    65    *       must be skipped.
    66    *
    67    * @param aAttributes    [in, out] text attributes list
    68    * @param aStartHTOffset [out, optional] start hyper text offset
    69    * @param aEndHTOffset   [out, optional] end hyper text offset
    70    */
    71   void GetAttributes(nsIPersistentProperties* aAttributes,
    72                      int32_t* aStartHTOffset = nullptr,
    73                      int32_t* aEndHTOffset = nullptr);
    75 protected:
    76   /**
    77    * Calculates range (start and end offsets) of text where the text attributes
    78    * are stretched. New offsets may be smaller if one of text attributes changes
    79    * its value before or after the given offsets.
    80    *
    81    * @param aTextAttrArray  [in] text attributes array
    82    * @param aAttrArrayLen   [in] text attributes array length
    83    * @param aStartHTOffset  [in, out] the start offset
    84    * @param aEndHTOffset    [in, out] the end offset
    85    */
    86   class TextAttr;
    87   void GetRange(TextAttr* aAttrArray[], uint32_t aAttrArrayLen,
    88                 int32_t* aStartHTOffset, int32_t* aEndHTOffset);
    90 private:
    91   Accessible* mOffsetAcc;
    92   HyperTextAccessible* mHyperTextAcc;
    93   int32_t mOffsetAccIdx;
    94   bool mIncludeDefAttrs;
    96 protected:
    98   /**
    99    * Interface class of text attribute class implementations.
   100    */
   101   class TextAttr
   102   {
   103   public:
   104     /**
   105      * Expose the text attribute to the given attribute set.
   106      *
   107      * @param aAttributes           [in] the given attribute set
   108      * @param aIncludeDefAttrValue  [in] if true then attribute is exposed even
   109      *                               if its value is the same as default one
   110      */
   111     virtual void Expose(nsIPersistentProperties* aAttributes,
   112                         bool aIncludeDefAttrValue) = 0;
   114     /**
   115      * Return true if the text attribute value on the given element equals with
   116      * predefined attribute value.
   117      */
   118     virtual bool Equal(Accessible* aAccessible) = 0;
   119   };
   122   /**
   123    * Base class to work with text attributes. See derived classes below.
   124    */
   125   template<class T>
   126   class TTextAttr : public TextAttr
   127   {
   128   public:
   129     TTextAttr(bool aGetRootValue) : mGetRootValue(aGetRootValue) {}
   131     // TextAttr
   132     virtual void Expose(nsIPersistentProperties* aAttributes,
   133                         bool aIncludeDefAttrValue)
   134     {
   135       if (mGetRootValue) {
   136         if (mIsRootDefined)
   137           ExposeValue(aAttributes, mRootNativeValue);
   138         return;
   139       }
   141       if (mIsDefined) {
   142         if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue)
   143           ExposeValue(aAttributes, mNativeValue);
   144         return;
   145       }
   147       if (aIncludeDefAttrValue && mIsRootDefined)
   148         ExposeValue(aAttributes, mRootNativeValue);
   149     }
   151     virtual bool Equal(Accessible* aAccessible)
   152     {
   153       T nativeValue;
   154       bool isDefined = GetValueFor(aAccessible, &nativeValue);
   156       if (!mIsDefined && !isDefined)
   157         return true;
   159       if (mIsDefined && isDefined)
   160         return nativeValue == mNativeValue;
   162       if (mIsDefined)
   163         return mNativeValue == mRootNativeValue;
   165       return nativeValue == mRootNativeValue;
   166     }
   168   protected:
   170     // Expose the text attribute with the given value to attribute set.
   171     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   172                              const T& aValue) = 0;
   174     // Return native value for the given DOM element.
   175     virtual bool GetValueFor(Accessible* aAccessible, T* aValue) = 0;
   177     // Indicates if root value should be exposed.
   178     bool mGetRootValue;
   180     // Native value and flag indicating if the value is defined (initialized in
   181     // derived classes). Note, undefined native value means it is inherited
   182     // from root.
   183     T mNativeValue;
   184     bool mIsDefined;
   186     // Native root value and flag indicating if the value is defined  (initialized
   187     // in derived classes).
   188     T mRootNativeValue;
   189     bool mIsRootDefined;
   190   };
   193   /**
   194    * Class is used for the work with 'language' text attribute.
   195    */
   196   class LangTextAttr : public TTextAttr<nsString>
   197   {
   198   public:
   199     LangTextAttr(HyperTextAccessible* aRoot, nsIContent* aRootElm,
   200                  nsIContent* aElm);
   201     virtual ~LangTextAttr();
   203   protected:
   205     // TextAttr
   206     virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue);
   207     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   208                              const nsString& aValue);
   210   private:
   211     nsCOMPtr<nsIContent> mRootContent;
   212   };
   215   /**
   216    * Class is used for the 'invalid' text attribute. Note, it calculated
   217    * the attribute from aria-invalid attribute only; invalid:spelling attribute
   218    * calculated from misspelled text in the editor is managed by
   219    * HyperTextAccessible and applied on top of the value from aria-invalid.
   220    */
   221   class InvalidTextAttr : public TTextAttr<uint32_t>
   222   {
   223   public:
   224     InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm);
   225     virtual ~InvalidTextAttr() { };
   227   protected:
   229     enum {
   230       eFalse,
   231       eGrammar,
   232       eSpelling,
   233       eTrue
   234     };
   236     // TextAttr
   237     virtual bool GetValueFor(Accessible* aAccessible, uint32_t* aValue);
   238     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   239                              const uint32_t& aValue);
   241   private:
   242     bool GetValue(nsIContent* aElm, uint32_t* aValue);
   243     nsIContent* mRootElm;
   244   };
   247   /**
   248    * Class is used for the work with 'background-color' text attribute.
   249    */
   250   class BGColorTextAttr : public TTextAttr<nscolor>
   251   {
   252   public:
   253     BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   254     virtual ~BGColorTextAttr() { }
   256   protected:
   258     // TextAttr
   259     virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue);
   260     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   261                              const nscolor& aValue);
   263   private:
   264     bool GetColor(nsIFrame* aFrame, nscolor* aColor);
   265     nsIFrame* mRootFrame;
   266   };
   269   /**
   270    * Class is used for the work with 'color' text attribute.
   271    */
   272   class ColorTextAttr : public TTextAttr<nscolor>
   273   {
   274   public:
   275     ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   276     virtual ~ColorTextAttr() { }
   278   protected:
   280     // TTextAttr
   281     virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue);
   282     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   283                              const nscolor& aValue);
   284   };
   287   /**
   288    * Class is used for the work with "font-family" text attribute.
   289    */
   290   class FontFamilyTextAttr : public TTextAttr<nsString>
   291   {
   292   public:
   293     FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   294     virtual ~FontFamilyTextAttr() { }
   296   protected:
   298     // TTextAttr
   299     virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue);
   300     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   301                              const nsString& aValue);
   303   private:
   305     bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily);
   306   };
   309   /**
   310    * Class is used for the work with "font-size" text attribute.
   311    */
   312   class FontSizeTextAttr : public TTextAttr<nscoord>
   313   {
   314   public:
   315     FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   316     virtual ~FontSizeTextAttr() { }
   318   protected: 
   320     // TTextAttr
   321     virtual bool GetValueFor(Accessible* aAccessible, nscoord* aValue);
   322     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   323                              const nscoord& aValue);
   325   private:
   326     nsDeviceContext* mDC;
   327   };
   330   /**
   331    * Class is used for the work with "font-style" text attribute.
   332    */
   333   class FontStyleTextAttr : public TTextAttr<nscoord>
   334   {
   335   public:
   336     FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   337     virtual ~FontStyleTextAttr() { }
   339   protected:
   341     // TTextAttr
   342     virtual bool GetValueFor(Accessible* aContent, nscoord* aValue);
   343     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   344                              const nscoord& aValue);
   345   };
   348   /**
   349    * Class is used for the work with "font-weight" text attribute.
   350    */
   351   class FontWeightTextAttr : public TTextAttr<int32_t>
   352   {
   353   public:
   354     FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   355     virtual ~FontWeightTextAttr() { }
   357   protected:
   359     // TTextAttr
   360     virtual bool GetValueFor(Accessible* aAccessible, int32_t* aValue);
   361     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   362                              const int32_t& aValue);
   364   private:
   365     int32_t GetFontWeight(nsIFrame* aFrame);
   366   };
   368   /**
   369    * Class is used for the work with 'auto-generated' text attribute.
   370    */
   371   class AutoGeneratedTextAttr : public TTextAttr<bool>
   372   {
   373   public:
   374     AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc,
   375                           Accessible* aAccessible);
   376     virtual ~AutoGeneratedTextAttr() { }
   378   protected:
   379     // TextAttr
   380     virtual bool GetValueFor(Accessible* aAccessible, bool* aValue);
   381     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   382                              const bool& aValue);
   383   };
   386   /**
   387    * TextDecorTextAttr class is used for the work with
   388    * "text-line-through-style", "text-line-through-color",
   389    * "text-underline-style" and "text-underline-color" text attributes.
   390    */
   392   class TextDecorValue
   393   {
   394   public:
   395     TextDecorValue() { }
   396     TextDecorValue(nsIFrame* aFrame);
   398     nscolor Color() const { return mColor; }
   399     uint8_t Style() const { return mStyle; }
   401     bool IsDefined() const
   402       { return IsUnderline() || IsLineThrough(); }
   403     bool IsUnderline() const
   404       { return mLine & NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE; }
   405     bool IsLineThrough() const
   406       { return mLine & NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH; }
   408     bool operator ==(const TextDecorValue& aValue)
   409     {
   410       return mColor == aValue.mColor && mLine == aValue.mLine &&
   411         mStyle == aValue.mStyle;
   412     }
   413     bool operator !=(const TextDecorValue& aValue)
   414       { return !(*this == aValue); }
   416   private:
   417     nscolor mColor;
   418     uint8_t mLine;
   419     uint8_t mStyle;
   420   };
   422   class TextDecorTextAttr : public TTextAttr<TextDecorValue>
   423   {
   424   public:
   425     TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   426     virtual ~TextDecorTextAttr() { }
   428   protected:
   430     // TextAttr
   431     virtual bool GetValueFor(Accessible* aAccessible, TextDecorValue* aValue);
   432     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   433                              const TextDecorValue& aValue);
   434   };
   436   /**
   437    * Class is used for the work with "text-position" text attribute.
   438    */
   440   enum TextPosValue {
   441     eTextPosNone = 0,
   442     eTextPosBaseline,
   443     eTextPosSub,
   444     eTextPosSuper
   445   };
   447   class TextPosTextAttr : public TTextAttr<TextPosValue>
   448   {
   449   public:
   450     TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
   451     virtual ~TextPosTextAttr() { }
   453   protected:
   455     // TextAttr
   456     virtual bool GetValueFor(Accessible* aAccessible, TextPosValue* aValue);
   457     virtual void ExposeValue(nsIPersistentProperties* aAttributes,
   458                              const TextPosValue& aValue);
   460   private:
   461     TextPosValue GetTextPosValue(nsIFrame* aFrame) const;
   462   };
   464 }; // TextAttrMgr
   466 } // namespace a11y
   467 } // namespace mozilla
   469 #endif

mercurial