accessible/src/base/TextAttrs.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial