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/. */
7 #ifndef NSLINEBREAKER_H_
8 #define NSLINEBREAKER_H_
10 #include "mozilla/intl/LineBreaker.h"
11 #include "mozilla/intl/Segmenter.h"
19 * A receiver of line break data.
21 class nsILineBreakSink
{
24 * Sets the break data for a substring of the associated text chunk.
25 * One or more of these calls will be performed; the union of all substrings
26 * will cover the entire text chunk. Substrings may overlap (i.e., we may
27 * set the break-before state of a character more than once).
28 * @param aBreakBefore the break-before states for the characters in the
29 * substring. These are enum values from gfxTextRun::CompressedGlyph:
30 * FLAG_BREAK_TYPE_NONE - no linebreak is allowed here
31 * FLAG_BREAK_TYPE_NORMAL - a normal (whitespace) linebreak
32 * FLAG_BREAK_TYPE_HYPHEN - a hyphenation point
34 virtual void SetBreaks(uint32_t aStart
, uint32_t aLength
,
35 uint8_t* aBreakBefore
) = 0;
38 * Indicates which characters should be capitalized. Only called if
39 * BREAK_NEED_CAPITALIZATION was requested.
41 virtual void SetCapitalization(uint32_t aStart
, uint32_t aLength
,
42 bool* aCapitalize
) = 0;
46 * A line-breaking state machine. You feed text into it via AppendText calls
47 * and it computes the possible line breaks. Because break decisions can
48 * require a lot of context, the breaks for a piece of text are sometimes not
49 * known until later text has been seen (or all text ends). So breaks are
50 * returned via a call to SetBreaks on the nsILineBreakSink object passed
51 * with each text chunk, which might happen during the corresponding AppendText
52 * call, or might happen during a later AppendText call or even a Reset()
55 * The linebreak results MUST NOT depend on how the text is broken up
56 * into AppendText calls.
58 * The current strategy is that we break the overall text into
59 * whitespace-delimited "words". Then those words are passed to the LineBreaker
60 * service for deeper analysis if they contain a "complex" character as
63 * This class also handles detection of which characters should be capitalized
64 * for text-transform:capitalize. This is a good place to handle that because
65 * we have all the context we need.
72 static inline bool IsSpace(char16_t u
) {
73 return mozilla::intl::NS_IsSpace(u
);
76 static inline bool IsComplexASCIIChar(char16_t u
) {
77 return !((0x0030 <= u
&& u
<= 0x0039) || (0x0041 <= u
&& u
<= 0x005A) ||
78 (0x0061 <= u
&& u
<= 0x007A) || (0x000a == u
));
81 static inline bool IsComplexChar(char16_t u
) {
82 return IsComplexASCIIChar(u
) ||
83 mozilla::intl::NS_NeedsPlatformNativeHandling(u
) ||
84 (0x1100 <= u
&& u
<= 0x11ff) || // Hangul Jamo
85 (0x2000 <= u
&& u
<= 0x21ff) || // Punctuations and Symbols
86 (0x2e80 <= u
&& u
<= 0xd7ff) || // several CJK blocks
87 (0xf900 <= u
&& u
<= 0xfaff) || // CJK Compatibility Idographs
88 (0xff00 <= u
&& u
<= 0xffef); // Halfwidth and Fullwidth Forms
91 // Break opportunities exist at the end of each run of breakable whitespace
92 // (see IsSpace above). Break opportunities can also exist between pairs of
93 // non-whitespace characters, as determined by mozilla::intl::LineBreaker.
94 // We pass a whitespace-
95 // delimited word to LineBreaker if it contains at least one character
96 // matching IsComplexChar.
97 // We provide flags to control on a per-chunk basis where breaks are allowed.
98 // At any character boundary, exactly one text chunk governs whether a
99 // break is allowed at that boundary.
101 // We operate on text after whitespace processing has been applied, so
102 // other characters (e.g. tabs and newlines) may have been converted to
106 * Flags passed with each chunk of text.
110 * Do not introduce a break opportunity at the start of this chunk of text.
112 BREAK_SUPPRESS_INITIAL
= 0x01,
114 * Do not introduce a break opportunity in the interior of this chunk of
115 * text. Also, whitespace in this chunk is treated as non-breakable.
117 BREAK_SUPPRESS_INSIDE
= 0x02,
119 * The sink currently is already set up to have no breaks in it;
120 * if no breaks are possible, nsLineBreaker does not need to call
121 * SetBreaks on it. This is useful when handling large quantities of
122 * preformatted text; the textruns will never have any breaks set on them,
123 * and there is no need to ever actually scan the text for breaks, except
124 * at the end of textruns in case context is needed for following breakable
127 BREAK_SKIP_SETTING_NO_BREAKS
= 0x04,
129 * We need to be notified of characters that should be capitalized
130 * (as in text-transform:capitalize) in this chunk of text.
132 BREAK_NEED_CAPITALIZATION
= 0x08,
134 * Auto-hyphenation is enabled, so we need to get a hyphenator
135 * (if available) and use it to find breakpoints.
137 BREAK_USE_AUTO_HYPHENATION
= 0x10
141 * Append "invisible whitespace". This acts like whitespace, but there is
142 * no actual text associated with it. Only the BREAK_SUPPRESS_INSIDE flag
145 nsresult
AppendInvisibleWhitespace(uint32_t aFlags
);
148 * Feed Unicode text into the linebreaker for analysis. aLength must be
150 * @param aSink can be null if the breaks are not actually needed (we may
151 * still be setting up state for later breaks)
153 nsresult
AppendText(nsAtom
* aHyphenationLanguage
, const char16_t
* aText
,
154 uint32_t aLength
, uint32_t aFlags
,
155 nsILineBreakSink
* aSink
);
157 * Feed 8-bit text into the linebreaker for analysis. aLength must be nonzero.
158 * @param aSink can be null if the breaks are not actually needed (we may
159 * still be setting up state for later breaks)
161 nsresult
AppendText(nsAtom
* aHyphenationLanguage
, const uint8_t* aText
,
162 uint32_t aLength
, uint32_t aFlags
,
163 nsILineBreakSink
* aSink
);
165 * Reset all state. This means the current run has ended; any outstanding
166 * calls through nsILineBreakSink are made, and all outstanding references to
167 * nsILineBreakSink objects are dropped.
168 * After this call, this linebreaker can be reused.
169 * This must be called at least once between any call to AppendText() and
170 * destroying the object.
171 * @param aTrailingBreak this is set to true when there is a break opportunity
172 * at the end of the text. This will normally only be declared true when there
173 * is breakable whitespace at the end.
175 nsresult
Reset(bool* aTrailingBreak
);
178 * Set word-break mode for line breaker. This is set by word-break property.
180 void SetWordBreak(mozilla::intl::WordBreakRule aMode
) {
181 // If current word is non-empty and mode is changing, flush the breaker.
182 if (aMode
!= mWordBreak
&& !mCurrentWord
.IsEmpty()) {
183 nsresult rv
= FlushCurrentWord();
185 NS_WARNING("FlushCurrentWord failed, line-breaks may be wrong");
187 // If previous mode was break-all, we should allow a break here.
188 // XXX (jfkthame) css-text spec seems unclear on this, raised question in
189 // https://github.com/w3c/csswg-drafts/issues/3897
190 if (mWordBreak
== mozilla::intl::WordBreakRule::BreakAll
) {
198 * Set line-break rule strictness mode for line breaker. This is set by the
199 * line-break property.
201 void SetStrictness(mozilla::intl::LineBreakRule aMode
) {
202 if (aMode
!= mLineBreak
&& !mCurrentWord
.IsEmpty()) {
203 nsresult rv
= FlushCurrentWord();
205 NS_WARNING("FlushCurrentWord failed, line-breaks may be wrong");
207 // If previous mode was anywhere, we should allow a break here.
208 if (mLineBreak
== mozilla::intl::LineBreakRule::Anywhere
) {
216 * Return whether the line-breaker has a buffered "current word" that may
217 * be extended with additional word-forming characters.
219 bool InWord() const { return !mCurrentWord
.IsEmpty(); }
222 * Set the word-continuation state, which will suppress capitalization of
223 * the next letter that might otherwise apply.
225 void SetWordContinuation(bool aContinuation
) {
226 mWordContinuation
= aContinuation
;
230 // This is a list of text sources that make up the "current word" (i.e.,
231 // run of text which does not contain any whitespace). All the mLengths
232 // are are nonzero, these cannot overlap.
234 TextItem(nsILineBreakSink
* aSink
, uint32_t aSinkOffset
, uint32_t aLength
,
237 mSinkOffset(aSinkOffset
),
241 nsILineBreakSink
* mSink
;
242 uint32_t mSinkOffset
;
247 // State for the nonwhitespace "word" that started in previous text and hasn't
250 // When the current word ends, this computes the linebreak opportunities
251 // *inside* the word (excluding either end) and sets them through the
252 // appropriate sink(s). Then we clear the current word state.
253 nsresult
FlushCurrentWord();
255 void UpdateCurrentWordLanguage(nsAtom
* aHyphenationLanguage
);
257 void FindHyphenationPoints(nsHyphenator
* aHyphenator
,
258 const char16_t
* aTextStart
,
259 const char16_t
* aTextLimit
, uint8_t* aBreakState
);
261 AutoTArray
<char16_t
, 100> mCurrentWord
;
262 // All the items that contribute to mCurrentWord
263 AutoTArray
<TextItem
, 2> mTextItems
;
264 nsAtom
* mCurrentWordLanguage
;
265 bool mCurrentWordContainsMixedLang
;
266 bool mCurrentWordContainsComplexChar
;
267 bool mScriptIsChineseOrJapanese
;
269 // True if the previous character was breakable whitespace
270 bool mAfterBreakableSpace
;
271 // True if a break must be allowed at the current position because
272 // a run of breakable whitespace ends here
274 // Break rules for letters from the "word-break" property.
275 mozilla::intl::WordBreakRule mWordBreak
;
276 // Line breaking strictness from the "line-break" property.
277 mozilla::intl::LineBreakRule mLineBreak
;
278 // Should the text be treated as continuing a word-in-progress (for purposes
279 // of initial capitalization)? Normally this is set to false whenever we
280 // start using a linebreaker, but it may be set to true if the line-breaker
281 // has been explicitly flushed mid-word.
282 bool mWordContinuation
;
285 #endif /*NSLINEBREAKER_H_*/