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