accessible/src/base/TextAttrs.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:98f932253725
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 "nsCOMPtr.h"
10 #include "nsColor.h"
11 #include "nsStyleConsts.h"
12
13 class nsIFrame;
14 class nsIPersistentProperties;
15 class nsIContent;
16 class nsDeviceContext;
17
18 namespace mozilla {
19 namespace a11y {
20
21 class Accessible;
22 class HyperTextAccessible;
23
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) { }
40
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) { }
59
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);
74
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);
89
90 private:
91 Accessible* mOffsetAcc;
92 HyperTextAccessible* mHyperTextAcc;
93 int32_t mOffsetAccIdx;
94 bool mIncludeDefAttrs;
95
96 protected:
97
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;
113
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 };
120
121
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) {}
130
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 }
140
141 if (mIsDefined) {
142 if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue)
143 ExposeValue(aAttributes, mNativeValue);
144 return;
145 }
146
147 if (aIncludeDefAttrValue && mIsRootDefined)
148 ExposeValue(aAttributes, mRootNativeValue);
149 }
150
151 virtual bool Equal(Accessible* aAccessible)
152 {
153 T nativeValue;
154 bool isDefined = GetValueFor(aAccessible, &nativeValue);
155
156 if (!mIsDefined && !isDefined)
157 return true;
158
159 if (mIsDefined && isDefined)
160 return nativeValue == mNativeValue;
161
162 if (mIsDefined)
163 return mNativeValue == mRootNativeValue;
164
165 return nativeValue == mRootNativeValue;
166 }
167
168 protected:
169
170 // Expose the text attribute with the given value to attribute set.
171 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
172 const T& aValue) = 0;
173
174 // Return native value for the given DOM element.
175 virtual bool GetValueFor(Accessible* aAccessible, T* aValue) = 0;
176
177 // Indicates if root value should be exposed.
178 bool mGetRootValue;
179
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;
185
186 // Native root value and flag indicating if the value is defined (initialized
187 // in derived classes).
188 T mRootNativeValue;
189 bool mIsRootDefined;
190 };
191
192
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();
202
203 protected:
204
205 // TextAttr
206 virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue);
207 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
208 const nsString& aValue);
209
210 private:
211 nsCOMPtr<nsIContent> mRootContent;
212 };
213
214
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() { };
226
227 protected:
228
229 enum {
230 eFalse,
231 eGrammar,
232 eSpelling,
233 eTrue
234 };
235
236 // TextAttr
237 virtual bool GetValueFor(Accessible* aAccessible, uint32_t* aValue);
238 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
239 const uint32_t& aValue);
240
241 private:
242 bool GetValue(nsIContent* aElm, uint32_t* aValue);
243 nsIContent* mRootElm;
244 };
245
246
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() { }
255
256 protected:
257
258 // TextAttr
259 virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue);
260 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
261 const nscolor& aValue);
262
263 private:
264 bool GetColor(nsIFrame* aFrame, nscolor* aColor);
265 nsIFrame* mRootFrame;
266 };
267
268
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() { }
277
278 protected:
279
280 // TTextAttr
281 virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue);
282 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
283 const nscolor& aValue);
284 };
285
286
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() { }
295
296 protected:
297
298 // TTextAttr
299 virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue);
300 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
301 const nsString& aValue);
302
303 private:
304
305 bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily);
306 };
307
308
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() { }
317
318 protected:
319
320 // TTextAttr
321 virtual bool GetValueFor(Accessible* aAccessible, nscoord* aValue);
322 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
323 const nscoord& aValue);
324
325 private:
326 nsDeviceContext* mDC;
327 };
328
329
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() { }
338
339 protected:
340
341 // TTextAttr
342 virtual bool GetValueFor(Accessible* aContent, nscoord* aValue);
343 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
344 const nscoord& aValue);
345 };
346
347
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() { }
356
357 protected:
358
359 // TTextAttr
360 virtual bool GetValueFor(Accessible* aAccessible, int32_t* aValue);
361 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
362 const int32_t& aValue);
363
364 private:
365 int32_t GetFontWeight(nsIFrame* aFrame);
366 };
367
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() { }
377
378 protected:
379 // TextAttr
380 virtual bool GetValueFor(Accessible* aAccessible, bool* aValue);
381 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
382 const bool& aValue);
383 };
384
385
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 */
391
392 class TextDecorValue
393 {
394 public:
395 TextDecorValue() { }
396 TextDecorValue(nsIFrame* aFrame);
397
398 nscolor Color() const { return mColor; }
399 uint8_t Style() const { return mStyle; }
400
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; }
407
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); }
415
416 private:
417 nscolor mColor;
418 uint8_t mLine;
419 uint8_t mStyle;
420 };
421
422 class TextDecorTextAttr : public TTextAttr<TextDecorValue>
423 {
424 public:
425 TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
426 virtual ~TextDecorTextAttr() { }
427
428 protected:
429
430 // TextAttr
431 virtual bool GetValueFor(Accessible* aAccessible, TextDecorValue* aValue);
432 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
433 const TextDecorValue& aValue);
434 };
435
436 /**
437 * Class is used for the work with "text-position" text attribute.
438 */
439
440 enum TextPosValue {
441 eTextPosNone = 0,
442 eTextPosBaseline,
443 eTextPosSub,
444 eTextPosSuper
445 };
446
447 class TextPosTextAttr : public TTextAttr<TextPosValue>
448 {
449 public:
450 TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
451 virtual ~TextPosTextAttr() { }
452
453 protected:
454
455 // TextAttr
456 virtual bool GetValueFor(Accessible* aAccessible, TextPosValue* aValue);
457 virtual void ExposeValue(nsIPersistentProperties* aAttributes,
458 const TextPosValue& aValue);
459
460 private:
461 TextPosValue GetTextPosValue(nsIFrame* aFrame) const;
462 };
463
464 }; // TextAttrMgr
465
466 } // namespace a11y
467 } // namespace mozilla
468
469 #endif

mercurial