summaryrefslogtreecommitdiffstats
path: root/accessible/base/TextAttrs.h
blob: 031e11427395f1c831eeb8795f27fb17ebeaa637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsTextAttrs_h_
#define nsTextAttrs_h_

#include "mozilla/FontPropertyTypes.h"
#include "nsCOMPtr.h"
#include "nsColor.h"
#include "nsString.h"
#include "nsStyleConsts.h"

class nsIFrame;
class nsIContent;
class nsDeviceContext;

namespace mozilla {
namespace a11y {

class AccAttributes;
class LocalAccessible;
class HyperTextAccessible;

/**
 * Used to expose text attributes for the hyper text accessible (see
 * HyperTextAccessible class).
 *
 * @note "invalid: spelling" text attribute is implemented entirely in
 *       HyperTextAccessible class.
 */
class TextAttrsMgr {
 public:
  /**
   * Constructor. Used to expose default text attributes.
   */
  explicit TextAttrsMgr(HyperTextAccessible* aHyperTextAcc)
      : mOffsetAcc(nullptr),
        mHyperTextAcc(aHyperTextAcc),
        mOffsetAccIdx(-1),
        mIncludeDefAttrs(true) {}

  /**
   * Constructor. Used to expose text attributes at the given offset.
   *
   * @param aHyperTextAcc    [in] hyper text accessible text attributes are
   *                          calculated for
   * @param aIncludeDefAttrs [optional] indicates whether default text
   *                          attributes should be included into list of exposed
   *                          text attributes
   * @param oOffsetAcc       [optional] offset an accessible the text attributes
   *                          should be calculated for
   * @param oOffsetAccIdx    [optional] index in parent of offset accessible
   */
  TextAttrsMgr(HyperTextAccessible* aHyperTextAcc, bool aIncludeDefAttrs,
               LocalAccessible* aOffsetAcc, int32_t aOffsetAccIdx)
      : mOffsetAcc(aOffsetAcc),
        mHyperTextAcc(aHyperTextAcc),
        mOffsetAccIdx(aOffsetAccIdx),
        mIncludeDefAttrs(aIncludeDefAttrs) {}

  /*
   * Return text attributes and hyper text offsets where these attributes are
   * applied. Offsets are calculated in the case of non default attributes.
   *
   * @note In the case of default attributes pointers on hyper text offsets
   *       must be skipped.
   *
   * @param aAttributes    [in, out] text attributes list
   * @param aStartHTOffset [out, optional] start hyper text offset
   * @param aEndHTOffset   [out, optional] end hyper text offset
   */
  void GetAttributes(AccAttributes* aAttributes,
                     uint32_t* aStartHTOffset = nullptr,
                     uint32_t* aEndHTOffset = nullptr);

 protected:
  /**
   * Calculates range (start and end offsets) of text where the text attributes
   * are stretched. New offsets may be smaller if one of text attributes changes
   * its value before or after the given offsets.
   *
   * @param aTextAttrArray  [in] text attributes array
   * @param aAttrArrayLen   [in] text attributes array length
   * @param aStartHTOffset  [in, out] the start offset
   * @param aEndHTOffset    [in, out] the end offset
   */
  class TextAttr;
  void GetRange(TextAttr* aAttrArray[], uint32_t aAttrArrayLen,
                uint32_t* aStartOffset, uint32_t* aEndOffset);

 private:
  LocalAccessible* mOffsetAcc;
  HyperTextAccessible* mHyperTextAcc;
  int32_t mOffsetAccIdx;
  bool mIncludeDefAttrs;

 protected:
  /**
   * Interface class of text attribute class implementations.
   */
  class TextAttr {
   public:
    /**
     * Expose the text attribute to the given attribute set.
     *
     * @param aAttributes           [in] the given attribute set
     * @param aIncludeDefAttrValue  [in] if true then attribute is exposed even
     *                               if its value is the same as default one
     */
    virtual void Expose(AccAttributes* aAttributes,
                        bool aIncludeDefAttrValue) = 0;

    /**
     * Return true if the text attribute value on the given element equals with
     * predefined attribute value.
     */
    virtual bool Equal(LocalAccessible* aAccessible) = 0;
  };

  /**
   * Base class to work with text attributes. See derived classes below.
   */
  template <class T>
  class TTextAttr : public TextAttr {
   public:
    explicit TTextAttr(bool aGetRootValue) : mGetRootValue(aGetRootValue) {}

    // TextAttr
    virtual void Expose(AccAttributes* aAttributes,
                        bool aIncludeDefAttrValue) override {
      if (mGetRootValue) {
        if (mIsRootDefined) ExposeValue(aAttributes, mRootNativeValue);
        return;
      }

      if (mIsDefined) {
        if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue) {
          ExposeValue(aAttributes, mNativeValue);
        }
        return;
      }

      if (aIncludeDefAttrValue && mIsRootDefined) {
        ExposeValue(aAttributes, mRootNativeValue);
      }
    }

    virtual bool Equal(LocalAccessible* aAccessible) override {
      T nativeValue;
      bool isDefined = GetValueFor(aAccessible, &nativeValue);

      if (!mIsDefined && !isDefined) return true;

      if (mIsDefined && isDefined) return nativeValue == mNativeValue;

      if (mIsDefined) return mNativeValue == mRootNativeValue;

      return nativeValue == mRootNativeValue;
    }

   protected:
    // Expose the text attribute with the given value to attribute set.
    virtual void ExposeValue(AccAttributes* aAttributes, const T& aValue) = 0;

    // Return native value for the given DOM element.
    virtual bool GetValueFor(LocalAccessible* aAccessible, T* aValue) = 0;

    // Indicates if root value should be exposed.
    bool mGetRootValue;

    // Native value and flag indicating if the value is defined (initialized in
    // derived classes). Note, undefined native value means it is inherited
    // from root.
    MOZ_INIT_OUTSIDE_CTOR T mNativeValue;
    MOZ_INIT_OUTSIDE_CTOR bool mIsDefined;

    // Native root value and flag indicating if the value is defined
    // (initialized in derived classes).
    MOZ_INIT_OUTSIDE_CTOR T mRootNativeValue;
    MOZ_INIT_OUTSIDE_CTOR bool mIsRootDefined;
  };

  /**
   * Class is used for the work with 'language' text attribute.
   */
  class LangTextAttr : public TTextAttr<nsString> {
   public:
    LangTextAttr(HyperTextAccessible* aRoot, nsIContent* aRootElm,
                 nsIContent* aElm);
    virtual ~LangTextAttr();

   protected:
    // TextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             nsString* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const nsString& aValue) override;

   private:
    nsCOMPtr<nsIContent> mRootContent;
  };

  /**
   * Class is used for the 'invalid' text attribute. Note, it calculated
   * the attribute from aria-invalid attribute only; invalid:spelling attribute
   * calculated from misspelled text in the editor is managed by
   * HyperTextAccessible and applied on top of the value from aria-invalid.
   */
  class InvalidTextAttr : public TTextAttr<uint32_t> {
   public:
    InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm);
    virtual ~InvalidTextAttr(){};

   protected:
    enum { eFalse, eGrammar, eSpelling, eTrue };

    // TextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             uint32_t* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const uint32_t& aValue) override;

   private:
    bool GetValue(nsIContent* aElm, uint32_t* aValue);
    nsIContent* mRootElm;
  };

  /**
   * Class is used for the work with 'background-color' text attribute.
   */
  class BGColorTextAttr : public TTextAttr<nscolor> {
   public:
    BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~BGColorTextAttr() {}

   protected:
    // TextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             nscolor* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const nscolor& aValue) override;

   private:
    bool GetColor(nsIFrame* aFrame, nscolor* aColor);
    nsIFrame* mRootFrame;
  };

  /**
   * Class is used for the work with 'color' text attribute.
   */
  class ColorTextAttr : public TTextAttr<nscolor> {
   public:
    ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~ColorTextAttr() {}

   protected:
    // TTextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             nscolor* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const nscolor& aValue) override;
  };

  /**
   * Class is used for the work with "font-family" text attribute.
   */
  class FontFamilyTextAttr : public TTextAttr<nsString> {
   public:
    FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~FontFamilyTextAttr() {}

   protected:
    // TTextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             nsString* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const nsString& aValue) override;

   private:
    bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily);
  };

  /**
   * Class is used for the work with "font-size" text attribute.
   */
  class FontSizeTextAttr : public TTextAttr<nscoord> {
   public:
    FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~FontSizeTextAttr() {}

   protected:
    // TTextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             nscoord* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const nscoord& aValue) override;

   private:
    nsDeviceContext* mDC;
  };

  /**
   * Class is used for the work with "font-style" text attribute.
   */
  class FontStyleTextAttr : public TTextAttr<mozilla::FontSlantStyle> {
   public:
    FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~FontStyleTextAttr() {}

   protected:
    // TTextAttr
    virtual bool GetValueFor(LocalAccessible* aContent,
                             mozilla::FontSlantStyle* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const mozilla::FontSlantStyle& aValue) override;
  };

  /**
   * Class is used for the work with "font-weight" text attribute.
   */
  class FontWeightTextAttr : public TTextAttr<mozilla::FontWeight> {
   public:
    FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~FontWeightTextAttr() {}

   protected:
    // TTextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             mozilla::FontWeight* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const mozilla::FontWeight& aValue) override;

   private:
    mozilla::FontWeight GetFontWeight(nsIFrame* aFrame);
  };

  /**
   * Class is used for the work with 'auto-generated' text attribute.
   */
  class AutoGeneratedTextAttr : public TTextAttr<bool> {
   public:
    AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc,
                          LocalAccessible* aAccessible);
    virtual ~AutoGeneratedTextAttr() {}

   protected:
    // TextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             bool* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const bool& aValue) override;
  };

  /**
   * TextDecorTextAttr class is used for the work with
   * "text-line-through-style", "text-line-through-color",
   * "text-underline-style" and "text-underline-color" text attributes.
   */

  class TextDecorValue {
   public:
    TextDecorValue()
        : mColor{0},
          mLine{StyleTextDecorationLine::NONE},
          mStyle{StyleTextDecorationStyle::None} {}
    explicit TextDecorValue(nsIFrame* aFrame);

    nscolor Color() const { return mColor; }
    mozilla::StyleTextDecorationStyle Style() const { return mStyle; }

    bool IsDefined() const { return IsUnderline() || IsLineThrough(); }
    bool IsUnderline() const {
      return bool(mLine & mozilla::StyleTextDecorationLine::UNDERLINE);
    }
    bool IsLineThrough() const {
      return bool(mLine & mozilla::StyleTextDecorationLine::LINE_THROUGH);
    }

    bool operator==(const TextDecorValue& aValue) const {
      return mColor == aValue.mColor && mLine == aValue.mLine &&
             mStyle == aValue.mStyle;
    }
    bool operator!=(const TextDecorValue& aValue) const {
      return !(*this == aValue);
    }

   private:
    nscolor mColor;
    mozilla::StyleTextDecorationLine mLine;
    mozilla::StyleTextDecorationStyle mStyle;
  };

  class TextDecorTextAttr : public TTextAttr<TextDecorValue> {
   public:
    TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
    virtual ~TextDecorTextAttr() {}

   protected:
    // TextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             TextDecorValue* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const TextDecorValue& aValue) override;
  };

  /**
   * Class is used for the work with "text-position" text attribute.
   */

  enum TextPosValue { eTextPosBaseline, eTextPosSub, eTextPosSuper };

  class TextPosTextAttr : public TTextAttr<Maybe<TextPosValue>> {
   public:
    TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame,
                    nsIContent* aRootElm, nsIContent* aElm);
    virtual ~TextPosTextAttr() {}

   protected:
    // TextAttr
    virtual bool GetValueFor(LocalAccessible* aAccessible,
                             Maybe<TextPosValue>* aValue) override;
    virtual void ExposeValue(AccAttributes* aAttributes,
                             const Maybe<TextPosValue>& aValue) override;

   private:
    Maybe<TextPosValue> GetAriaTextPosValue(nsIContent* aElm) const;
    Maybe<TextPosValue> GetAriaTextPosValue(nsIContent* aElm,
                                            nsIFrame*& ariaFrame) const;
    Maybe<TextPosValue> GetLayoutTextPosValue(nsIFrame* aFrame) const;
    nsIContent* mRootElm;
  };

};  // TextAttrMgr

}  // namespace a11y
}  // namespace mozilla

#endif