|
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 NSLINEBREAKER_H_ |
|
7 #define NSLINEBREAKER_H_ |
|
8 |
|
9 #include "nsString.h" |
|
10 #include "nsTArray.h" |
|
11 #include "nsILineBreaker.h" |
|
12 |
|
13 class nsIAtom; |
|
14 class nsHyphenator; |
|
15 |
|
16 /** |
|
17 * A receiver of line break data. |
|
18 */ |
|
19 class nsILineBreakSink { |
|
20 public: |
|
21 /** |
|
22 * Sets the break data for a substring of the associated text chunk. |
|
23 * One or more of these calls will be performed; the union of all substrings |
|
24 * will cover the entire text chunk. Substrings may overlap (i.e., we may |
|
25 * set the break-before state of a character more than once). |
|
26 * @param aBreakBefore the break-before states for the characters in the substring. |
|
27 * These are enum values from gfxTextRun::CompressedGlyph: |
|
28 * FLAG_BREAK_TYPE_NONE - no linebreak is allowed here |
|
29 * FLAG_BREAK_TYPE_NORMAL - a normal (whitespace) linebreak |
|
30 * FLAG_BREAK_TYPE_HYPHEN - a hyphenation point |
|
31 */ |
|
32 virtual void SetBreaks(uint32_t aStart, uint32_t aLength, uint8_t* aBreakBefore) = 0; |
|
33 |
|
34 /** |
|
35 * Indicates which characters should be capitalized. Only called if |
|
36 * BREAK_NEED_CAPITALIZATION was requested. |
|
37 */ |
|
38 virtual void SetCapitalization(uint32_t aStart, uint32_t aLength, bool* aCapitalize) = 0; |
|
39 }; |
|
40 |
|
41 /** |
|
42 * A line-breaking state machine. You feed text into it via AppendText calls |
|
43 * and it computes the possible line breaks. Because break decisions can |
|
44 * require a lot of context, the breaks for a piece of text are sometimes not |
|
45 * known until later text has been seen (or all text ends). So breaks are |
|
46 * returned via a call to SetBreaks on the nsILineBreakSink object passed |
|
47 * with each text chunk, which might happen during the corresponding AppendText |
|
48 * call, or might happen during a later AppendText call or even a Reset() |
|
49 * call. |
|
50 * |
|
51 * The linebreak results MUST NOT depend on how the text is broken up |
|
52 * into AppendText calls. |
|
53 * |
|
54 * The current strategy is that we break the overall text into |
|
55 * whitespace-delimited "words". Then those words are passed to the nsILineBreaker |
|
56 * service for deeper analysis if they contain a "complex" character as described |
|
57 * below. |
|
58 * |
|
59 * This class also handles detection of which characters should be capitalized |
|
60 * for text-transform:capitalize. This is a good place to handle that because |
|
61 * we have all the context we need. |
|
62 */ |
|
63 class nsLineBreaker { |
|
64 public: |
|
65 nsLineBreaker(); |
|
66 ~nsLineBreaker(); |
|
67 |
|
68 static inline bool IsSpace(char16_t u) { return NS_IsSpace(u); } |
|
69 |
|
70 static inline bool IsComplexASCIIChar(char16_t u) |
|
71 { |
|
72 return !((0x0030 <= u && u <= 0x0039) || |
|
73 (0x0041 <= u && u <= 0x005A) || |
|
74 (0x0061 <= u && u <= 0x007A) || |
|
75 (0x000a == u)); |
|
76 } |
|
77 |
|
78 static inline bool IsComplexChar(char16_t u) |
|
79 { |
|
80 return IsComplexASCIIChar(u) || |
|
81 NS_NeedsPlatformNativeHandling(u) || |
|
82 (0x1100 <= u && u <= 0x11ff) || // Hangul Jamo |
|
83 (0x2000 <= u && u <= 0x21ff) || // Punctuations and Symbols |
|
84 (0x2e80 <= u && u <= 0xd7ff) || // several CJK blocks |
|
85 (0xf900 <= u && u <= 0xfaff) || // CJK Compatibility Idographs |
|
86 (0xff00 <= u && u <= 0xffef); // Halfwidth and Fullwidth Forms |
|
87 } |
|
88 |
|
89 // Break opportunities exist at the end of each run of breakable whitespace |
|
90 // (see IsSpace above). Break opportunities can also exist between pairs of |
|
91 // non-whitespace characters, as determined by nsILineBreaker. We pass a whitespace- |
|
92 // delimited word to nsILineBreaker if it contains at least one character |
|
93 // matching IsComplexChar. |
|
94 // We provide flags to control on a per-chunk basis where breaks are allowed. |
|
95 // At any character boundary, exactly one text chunk governs whether a |
|
96 // break is allowed at that boundary. |
|
97 // |
|
98 // We operate on text after whitespace processing has been applied, so |
|
99 // other characters (e.g. tabs and newlines) may have been converted to |
|
100 // spaces. |
|
101 |
|
102 /** |
|
103 * Flags passed with each chunk of text. |
|
104 */ |
|
105 enum { |
|
106 /* |
|
107 * Do not introduce a break opportunity at the start of this chunk of text. |
|
108 */ |
|
109 BREAK_SUPPRESS_INITIAL = 0x01, |
|
110 /** |
|
111 * Do not introduce a break opportunity in the interior of this chunk of text. |
|
112 * Also, whitespace in this chunk is treated as non-breakable. |
|
113 */ |
|
114 BREAK_SUPPRESS_INSIDE = 0x02, |
|
115 /** |
|
116 * The sink currently is already set up to have no breaks in it; |
|
117 * if no breaks are possible, nsLineBreaker does not need to call |
|
118 * SetBreaks on it. This is useful when handling large quantities of |
|
119 * preformatted text; the textruns will never have any breaks set on them, |
|
120 * and there is no need to ever actually scan the text for breaks, except |
|
121 * at the end of textruns in case context is needed for following breakable |
|
122 * text. |
|
123 */ |
|
124 BREAK_SKIP_SETTING_NO_BREAKS = 0x04, |
|
125 /** |
|
126 * We need to be notified of characters that should be capitalized |
|
127 * (as in text-transform:capitalize) in this chunk of text. |
|
128 */ |
|
129 BREAK_NEED_CAPITALIZATION = 0x08, |
|
130 /** |
|
131 * Auto-hyphenation is enabled, so we need to get a hyphenator |
|
132 * (if available) and use it to find breakpoints. |
|
133 */ |
|
134 BREAK_USE_AUTO_HYPHENATION = 0x10 |
|
135 }; |
|
136 |
|
137 /** |
|
138 * Append "invisible whitespace". This acts like whitespace, but there is |
|
139 * no actual text associated with it. Only the BREAK_SUPPRESS_INSIDE flag |
|
140 * is relevant here. |
|
141 */ |
|
142 nsresult AppendInvisibleWhitespace(uint32_t aFlags); |
|
143 |
|
144 /** |
|
145 * Feed Unicode text into the linebreaker for analysis. aLength must be |
|
146 * nonzero. |
|
147 * @param aSink can be null if the breaks are not actually needed (we may |
|
148 * still be setting up state for later breaks) |
|
149 */ |
|
150 nsresult AppendText(nsIAtom* aHyphenationLanguage, const char16_t* aText, uint32_t aLength, |
|
151 uint32_t aFlags, nsILineBreakSink* aSink); |
|
152 /** |
|
153 * Feed 8-bit text into the linebreaker for analysis. aLength must be nonzero. |
|
154 * @param aSink can be null if the breaks are not actually needed (we may |
|
155 * still be setting up state for later breaks) |
|
156 */ |
|
157 nsresult AppendText(nsIAtom* aHyphenationLanguage, const uint8_t* aText, uint32_t aLength, |
|
158 uint32_t aFlags, nsILineBreakSink* aSink); |
|
159 /** |
|
160 * Reset all state. This means the current run has ended; any outstanding |
|
161 * calls through nsILineBreakSink are made, and all outstanding references to |
|
162 * nsILineBreakSink objects are dropped. |
|
163 * After this call, this linebreaker can be reused. |
|
164 * This must be called at least once between any call to AppendText() and |
|
165 * destroying the object. |
|
166 * @param aTrailingBreak this is set to true when there is a break opportunity |
|
167 * at the end of the text. This will normally only be declared true when there |
|
168 * is breakable whitespace at the end. |
|
169 */ |
|
170 nsresult Reset(bool* aTrailingBreak); |
|
171 |
|
172 /* |
|
173 * Set word-break mode for linebreaker. This is set by word-break property. |
|
174 * @param aMode is nsILineBreaker::kWordBreak_* value. |
|
175 */ |
|
176 void SetWordBreak(uint8_t aMode) { mWordBreak = aMode; } |
|
177 |
|
178 private: |
|
179 // This is a list of text sources that make up the "current word" (i.e., |
|
180 // run of text which does not contain any whitespace). All the mLengths |
|
181 // are are nonzero, these cannot overlap. |
|
182 struct TextItem { |
|
183 TextItem(nsILineBreakSink* aSink, uint32_t aSinkOffset, uint32_t aLength, |
|
184 uint32_t aFlags) |
|
185 : mSink(aSink), mSinkOffset(aSinkOffset), mLength(aLength), mFlags(aFlags) {} |
|
186 |
|
187 nsILineBreakSink* mSink; |
|
188 uint32_t mSinkOffset; |
|
189 uint32_t mLength; |
|
190 uint32_t mFlags; |
|
191 }; |
|
192 |
|
193 // State for the nonwhitespace "word" that started in previous text and hasn't |
|
194 // finished yet. |
|
195 |
|
196 // When the current word ends, this computes the linebreak opportunities |
|
197 // *inside* the word (excluding either end) and sets them through the |
|
198 // appropriate sink(s). Then we clear the current word state. |
|
199 nsresult FlushCurrentWord(); |
|
200 |
|
201 void UpdateCurrentWordLanguage(nsIAtom *aHyphenationLanguage); |
|
202 |
|
203 void FindHyphenationPoints(nsHyphenator *aHyphenator, |
|
204 const char16_t *aTextStart, |
|
205 const char16_t *aTextLimit, |
|
206 uint8_t *aBreakState); |
|
207 |
|
208 nsAutoTArray<char16_t,100> mCurrentWord; |
|
209 // All the items that contribute to mCurrentWord |
|
210 nsAutoTArray<TextItem,2> mTextItems; |
|
211 nsIAtom* mCurrentWordLanguage; |
|
212 bool mCurrentWordContainsMixedLang; |
|
213 bool mCurrentWordContainsComplexChar; |
|
214 |
|
215 // True if the previous character was breakable whitespace |
|
216 bool mAfterBreakableSpace; |
|
217 // True if a break must be allowed at the current position because |
|
218 // a run of breakable whitespace ends here |
|
219 bool mBreakHere; |
|
220 // line break mode by "word-break" style |
|
221 uint8_t mWordBreak; |
|
222 }; |
|
223 |
|
224 #endif /*NSLINEBREAKER_H_*/ |