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