1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/src/base/TextAttrs.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,469 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsTextAttrs_h_ 1.10 +#define nsTextAttrs_h_ 1.11 + 1.12 +#include "nsCOMPtr.h" 1.13 +#include "nsColor.h" 1.14 +#include "nsStyleConsts.h" 1.15 + 1.16 +class nsIFrame; 1.17 +class nsIPersistentProperties; 1.18 +class nsIContent; 1.19 +class nsDeviceContext; 1.20 + 1.21 +namespace mozilla { 1.22 +namespace a11y { 1.23 + 1.24 +class Accessible; 1.25 +class HyperTextAccessible; 1.26 + 1.27 +/** 1.28 + * Used to expose text attributes for the hyper text accessible (see 1.29 + * HyperTextAccessible class). 1.30 + * 1.31 + * @note "invalid: spelling" text attribute is implemented entirely in 1.32 + * HyperTextAccessible class. 1.33 + */ 1.34 +class TextAttrsMgr 1.35 +{ 1.36 +public: 1.37 + /** 1.38 + * Constructor. Used to expose default text attributes. 1.39 + */ 1.40 + TextAttrsMgr(HyperTextAccessible* aHyperTextAcc) : 1.41 + mOffsetAcc(nullptr), mHyperTextAcc(aHyperTextAcc), 1.42 + mOffsetAccIdx(-1), mIncludeDefAttrs(true) { } 1.43 + 1.44 + /** 1.45 + * Constructor. Used to expose text attributes at the given offset. 1.46 + * 1.47 + * @param aHyperTextAcc [in] hyper text accessible text attributes are 1.48 + * calculated for 1.49 + * @param aIncludeDefAttrs [optional] indicates whether default text 1.50 + * attributes should be included into list of exposed 1.51 + * text attributes 1.52 + * @param oOffsetAcc [optional] offset an accessible the text attributes 1.53 + * should be calculated for 1.54 + * @param oOffsetAccIdx [optional] index in parent of offset accessible 1.55 + */ 1.56 + TextAttrsMgr(HyperTextAccessible* aHyperTextAcc, 1.57 + bool aIncludeDefAttrs, 1.58 + Accessible* aOffsetAcc, 1.59 + int32_t aOffsetAccIdx) : 1.60 + mOffsetAcc(aOffsetAcc), mHyperTextAcc(aHyperTextAcc), 1.61 + mOffsetAccIdx(aOffsetAccIdx), mIncludeDefAttrs(aIncludeDefAttrs) { } 1.62 + 1.63 + /* 1.64 + * Return text attributes and hyper text offsets where these attributes are 1.65 + * applied. Offsets are calculated in the case of non default attributes. 1.66 + * 1.67 + * @note In the case of default attributes pointers on hyper text offsets 1.68 + * must be skipped. 1.69 + * 1.70 + * @param aAttributes [in, out] text attributes list 1.71 + * @param aStartHTOffset [out, optional] start hyper text offset 1.72 + * @param aEndHTOffset [out, optional] end hyper text offset 1.73 + */ 1.74 + void GetAttributes(nsIPersistentProperties* aAttributes, 1.75 + int32_t* aStartHTOffset = nullptr, 1.76 + int32_t* aEndHTOffset = nullptr); 1.77 + 1.78 +protected: 1.79 + /** 1.80 + * Calculates range (start and end offsets) of text where the text attributes 1.81 + * are stretched. New offsets may be smaller if one of text attributes changes 1.82 + * its value before or after the given offsets. 1.83 + * 1.84 + * @param aTextAttrArray [in] text attributes array 1.85 + * @param aAttrArrayLen [in] text attributes array length 1.86 + * @param aStartHTOffset [in, out] the start offset 1.87 + * @param aEndHTOffset [in, out] the end offset 1.88 + */ 1.89 + class TextAttr; 1.90 + void GetRange(TextAttr* aAttrArray[], uint32_t aAttrArrayLen, 1.91 + int32_t* aStartHTOffset, int32_t* aEndHTOffset); 1.92 + 1.93 +private: 1.94 + Accessible* mOffsetAcc; 1.95 + HyperTextAccessible* mHyperTextAcc; 1.96 + int32_t mOffsetAccIdx; 1.97 + bool mIncludeDefAttrs; 1.98 + 1.99 +protected: 1.100 + 1.101 + /** 1.102 + * Interface class of text attribute class implementations. 1.103 + */ 1.104 + class TextAttr 1.105 + { 1.106 + public: 1.107 + /** 1.108 + * Expose the text attribute to the given attribute set. 1.109 + * 1.110 + * @param aAttributes [in] the given attribute set 1.111 + * @param aIncludeDefAttrValue [in] if true then attribute is exposed even 1.112 + * if its value is the same as default one 1.113 + */ 1.114 + virtual void Expose(nsIPersistentProperties* aAttributes, 1.115 + bool aIncludeDefAttrValue) = 0; 1.116 + 1.117 + /** 1.118 + * Return true if the text attribute value on the given element equals with 1.119 + * predefined attribute value. 1.120 + */ 1.121 + virtual bool Equal(Accessible* aAccessible) = 0; 1.122 + }; 1.123 + 1.124 + 1.125 + /** 1.126 + * Base class to work with text attributes. See derived classes below. 1.127 + */ 1.128 + template<class T> 1.129 + class TTextAttr : public TextAttr 1.130 + { 1.131 + public: 1.132 + TTextAttr(bool aGetRootValue) : mGetRootValue(aGetRootValue) {} 1.133 + 1.134 + // TextAttr 1.135 + virtual void Expose(nsIPersistentProperties* aAttributes, 1.136 + bool aIncludeDefAttrValue) 1.137 + { 1.138 + if (mGetRootValue) { 1.139 + if (mIsRootDefined) 1.140 + ExposeValue(aAttributes, mRootNativeValue); 1.141 + return; 1.142 + } 1.143 + 1.144 + if (mIsDefined) { 1.145 + if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue) 1.146 + ExposeValue(aAttributes, mNativeValue); 1.147 + return; 1.148 + } 1.149 + 1.150 + if (aIncludeDefAttrValue && mIsRootDefined) 1.151 + ExposeValue(aAttributes, mRootNativeValue); 1.152 + } 1.153 + 1.154 + virtual bool Equal(Accessible* aAccessible) 1.155 + { 1.156 + T nativeValue; 1.157 + bool isDefined = GetValueFor(aAccessible, &nativeValue); 1.158 + 1.159 + if (!mIsDefined && !isDefined) 1.160 + return true; 1.161 + 1.162 + if (mIsDefined && isDefined) 1.163 + return nativeValue == mNativeValue; 1.164 + 1.165 + if (mIsDefined) 1.166 + return mNativeValue == mRootNativeValue; 1.167 + 1.168 + return nativeValue == mRootNativeValue; 1.169 + } 1.170 + 1.171 + protected: 1.172 + 1.173 + // Expose the text attribute with the given value to attribute set. 1.174 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.175 + const T& aValue) = 0; 1.176 + 1.177 + // Return native value for the given DOM element. 1.178 + virtual bool GetValueFor(Accessible* aAccessible, T* aValue) = 0; 1.179 + 1.180 + // Indicates if root value should be exposed. 1.181 + bool mGetRootValue; 1.182 + 1.183 + // Native value and flag indicating if the value is defined (initialized in 1.184 + // derived classes). Note, undefined native value means it is inherited 1.185 + // from root. 1.186 + T mNativeValue; 1.187 + bool mIsDefined; 1.188 + 1.189 + // Native root value and flag indicating if the value is defined (initialized 1.190 + // in derived classes). 1.191 + T mRootNativeValue; 1.192 + bool mIsRootDefined; 1.193 + }; 1.194 + 1.195 + 1.196 + /** 1.197 + * Class is used for the work with 'language' text attribute. 1.198 + */ 1.199 + class LangTextAttr : public TTextAttr<nsString> 1.200 + { 1.201 + public: 1.202 + LangTextAttr(HyperTextAccessible* aRoot, nsIContent* aRootElm, 1.203 + nsIContent* aElm); 1.204 + virtual ~LangTextAttr(); 1.205 + 1.206 + protected: 1.207 + 1.208 + // TextAttr 1.209 + virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue); 1.210 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.211 + const nsString& aValue); 1.212 + 1.213 + private: 1.214 + nsCOMPtr<nsIContent> mRootContent; 1.215 + }; 1.216 + 1.217 + 1.218 + /** 1.219 + * Class is used for the 'invalid' text attribute. Note, it calculated 1.220 + * the attribute from aria-invalid attribute only; invalid:spelling attribute 1.221 + * calculated from misspelled text in the editor is managed by 1.222 + * HyperTextAccessible and applied on top of the value from aria-invalid. 1.223 + */ 1.224 + class InvalidTextAttr : public TTextAttr<uint32_t> 1.225 + { 1.226 + public: 1.227 + InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm); 1.228 + virtual ~InvalidTextAttr() { }; 1.229 + 1.230 + protected: 1.231 + 1.232 + enum { 1.233 + eFalse, 1.234 + eGrammar, 1.235 + eSpelling, 1.236 + eTrue 1.237 + }; 1.238 + 1.239 + // TextAttr 1.240 + virtual bool GetValueFor(Accessible* aAccessible, uint32_t* aValue); 1.241 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.242 + const uint32_t& aValue); 1.243 + 1.244 + private: 1.245 + bool GetValue(nsIContent* aElm, uint32_t* aValue); 1.246 + nsIContent* mRootElm; 1.247 + }; 1.248 + 1.249 + 1.250 + /** 1.251 + * Class is used for the work with 'background-color' text attribute. 1.252 + */ 1.253 + class BGColorTextAttr : public TTextAttr<nscolor> 1.254 + { 1.255 + public: 1.256 + BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.257 + virtual ~BGColorTextAttr() { } 1.258 + 1.259 + protected: 1.260 + 1.261 + // TextAttr 1.262 + virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue); 1.263 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.264 + const nscolor& aValue); 1.265 + 1.266 + private: 1.267 + bool GetColor(nsIFrame* aFrame, nscolor* aColor); 1.268 + nsIFrame* mRootFrame; 1.269 + }; 1.270 + 1.271 + 1.272 + /** 1.273 + * Class is used for the work with 'color' text attribute. 1.274 + */ 1.275 + class ColorTextAttr : public TTextAttr<nscolor> 1.276 + { 1.277 + public: 1.278 + ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.279 + virtual ~ColorTextAttr() { } 1.280 + 1.281 + protected: 1.282 + 1.283 + // TTextAttr 1.284 + virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue); 1.285 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.286 + const nscolor& aValue); 1.287 + }; 1.288 + 1.289 + 1.290 + /** 1.291 + * Class is used for the work with "font-family" text attribute. 1.292 + */ 1.293 + class FontFamilyTextAttr : public TTextAttr<nsString> 1.294 + { 1.295 + public: 1.296 + FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.297 + virtual ~FontFamilyTextAttr() { } 1.298 + 1.299 + protected: 1.300 + 1.301 + // TTextAttr 1.302 + virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue); 1.303 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.304 + const nsString& aValue); 1.305 + 1.306 + private: 1.307 + 1.308 + bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily); 1.309 + }; 1.310 + 1.311 + 1.312 + /** 1.313 + * Class is used for the work with "font-size" text attribute. 1.314 + */ 1.315 + class FontSizeTextAttr : public TTextAttr<nscoord> 1.316 + { 1.317 + public: 1.318 + FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.319 + virtual ~FontSizeTextAttr() { } 1.320 + 1.321 + protected: 1.322 + 1.323 + // TTextAttr 1.324 + virtual bool GetValueFor(Accessible* aAccessible, nscoord* aValue); 1.325 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.326 + const nscoord& aValue); 1.327 + 1.328 + private: 1.329 + nsDeviceContext* mDC; 1.330 + }; 1.331 + 1.332 + 1.333 + /** 1.334 + * Class is used for the work with "font-style" text attribute. 1.335 + */ 1.336 + class FontStyleTextAttr : public TTextAttr<nscoord> 1.337 + { 1.338 + public: 1.339 + FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.340 + virtual ~FontStyleTextAttr() { } 1.341 + 1.342 + protected: 1.343 + 1.344 + // TTextAttr 1.345 + virtual bool GetValueFor(Accessible* aContent, nscoord* aValue); 1.346 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.347 + const nscoord& aValue); 1.348 + }; 1.349 + 1.350 + 1.351 + /** 1.352 + * Class is used for the work with "font-weight" text attribute. 1.353 + */ 1.354 + class FontWeightTextAttr : public TTextAttr<int32_t> 1.355 + { 1.356 + public: 1.357 + FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.358 + virtual ~FontWeightTextAttr() { } 1.359 + 1.360 + protected: 1.361 + 1.362 + // TTextAttr 1.363 + virtual bool GetValueFor(Accessible* aAccessible, int32_t* aValue); 1.364 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.365 + const int32_t& aValue); 1.366 + 1.367 + private: 1.368 + int32_t GetFontWeight(nsIFrame* aFrame); 1.369 + }; 1.370 + 1.371 + /** 1.372 + * Class is used for the work with 'auto-generated' text attribute. 1.373 + */ 1.374 + class AutoGeneratedTextAttr : public TTextAttr<bool> 1.375 + { 1.376 + public: 1.377 + AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc, 1.378 + Accessible* aAccessible); 1.379 + virtual ~AutoGeneratedTextAttr() { } 1.380 + 1.381 + protected: 1.382 + // TextAttr 1.383 + virtual bool GetValueFor(Accessible* aAccessible, bool* aValue); 1.384 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.385 + const bool& aValue); 1.386 + }; 1.387 + 1.388 + 1.389 + /** 1.390 + * TextDecorTextAttr class is used for the work with 1.391 + * "text-line-through-style", "text-line-through-color", 1.392 + * "text-underline-style" and "text-underline-color" text attributes. 1.393 + */ 1.394 + 1.395 + class TextDecorValue 1.396 + { 1.397 + public: 1.398 + TextDecorValue() { } 1.399 + TextDecorValue(nsIFrame* aFrame); 1.400 + 1.401 + nscolor Color() const { return mColor; } 1.402 + uint8_t Style() const { return mStyle; } 1.403 + 1.404 + bool IsDefined() const 1.405 + { return IsUnderline() || IsLineThrough(); } 1.406 + bool IsUnderline() const 1.407 + { return mLine & NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE; } 1.408 + bool IsLineThrough() const 1.409 + { return mLine & NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH; } 1.410 + 1.411 + bool operator ==(const TextDecorValue& aValue) 1.412 + { 1.413 + return mColor == aValue.mColor && mLine == aValue.mLine && 1.414 + mStyle == aValue.mStyle; 1.415 + } 1.416 + bool operator !=(const TextDecorValue& aValue) 1.417 + { return !(*this == aValue); } 1.418 + 1.419 + private: 1.420 + nscolor mColor; 1.421 + uint8_t mLine; 1.422 + uint8_t mStyle; 1.423 + }; 1.424 + 1.425 + class TextDecorTextAttr : public TTextAttr<TextDecorValue> 1.426 + { 1.427 + public: 1.428 + TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.429 + virtual ~TextDecorTextAttr() { } 1.430 + 1.431 + protected: 1.432 + 1.433 + // TextAttr 1.434 + virtual bool GetValueFor(Accessible* aAccessible, TextDecorValue* aValue); 1.435 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.436 + const TextDecorValue& aValue); 1.437 + }; 1.438 + 1.439 + /** 1.440 + * Class is used for the work with "text-position" text attribute. 1.441 + */ 1.442 + 1.443 + enum TextPosValue { 1.444 + eTextPosNone = 0, 1.445 + eTextPosBaseline, 1.446 + eTextPosSub, 1.447 + eTextPosSuper 1.448 + }; 1.449 + 1.450 + class TextPosTextAttr : public TTextAttr<TextPosValue> 1.451 + { 1.452 + public: 1.453 + TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); 1.454 + virtual ~TextPosTextAttr() { } 1.455 + 1.456 + protected: 1.457 + 1.458 + // TextAttr 1.459 + virtual bool GetValueFor(Accessible* aAccessible, TextPosValue* aValue); 1.460 + virtual void ExposeValue(nsIPersistentProperties* aAttributes, 1.461 + const TextPosValue& aValue); 1.462 + 1.463 + private: 1.464 + TextPosValue GetTextPosValue(nsIFrame* aFrame) const; 1.465 + }; 1.466 + 1.467 +}; // TextAttrMgr 1.468 + 1.469 +} // namespace a11y 1.470 +} // namespace mozilla 1.471 + 1.472 +#endif