[GTK] Menu list button doesn't use the text color from the theme
[WebKit-https.git] / Source / WebCore / rendering / break_lines.h
1 /*
2  * Copyright (C) 2005, 2007, 2010, 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2011 Google Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef break_lines_h
23 #define break_lines_h
24
25 #include "TextBreakIterator.h"
26 #include <wtf/ASCIICType.h>
27 #include <wtf/StdLibExtras.h>
28 #include <wtf/unicode/CharacterNames.h>
29
30 namespace WebCore {
31
32 static const UChar asciiLineBreakTableFirstChar = '!';
33 static const UChar asciiLineBreakTableLastChar = 127;
34 static const unsigned asciiLineBreakTableColumnCount = (asciiLineBreakTableLastChar - asciiLineBreakTableFirstChar) / 8 + 1;
35
36 WEBCORE_EXPORT extern const unsigned char asciiLineBreakTable[][asciiLineBreakTableColumnCount];
37
38 enum class NBSPBehavior {
39     IgnoreNBSP,
40     TreatNBSPAsBreak,
41 };
42
43 template<NBSPBehavior nbspBehavior>
44 static inline bool isBreakableSpace(UChar ch)
45 {
46     switch (ch) {
47     case ' ':
48     case '\n':
49     case '\t':
50         return true;
51     case noBreakSpace:
52         return nbspBehavior == NBSPBehavior::TreatNBSPAsBreak;
53     default:
54         return false;
55     }
56 }
57
58 inline bool shouldBreakAfter(UChar lastCh, UChar ch, UChar nextCh)
59 {
60     // Don't allow line breaking between '-' and a digit if the '-' may mean a minus sign in the context,
61     // while allow breaking in 'ABCD-1234' and '1234-5678' which may be in long URLs.
62     if (ch == '-' && isASCIIDigit(nextCh))
63         return isASCIIAlphanumeric(lastCh);
64
65     // If both ch and nextCh are ASCII characters, use a lookup table for enhanced speed and for compatibility
66     // with other browsers (see comments for asciiLineBreakTable for details).
67     if (ch >= asciiLineBreakTableFirstChar && ch <= asciiLineBreakTableLastChar && nextCh >= asciiLineBreakTableFirstChar && nextCh <= asciiLineBreakTableLastChar) {
68         const unsigned char* tableRow = asciiLineBreakTable[ch - asciiLineBreakTableFirstChar];
69         int nextChIndex = nextCh - asciiLineBreakTableFirstChar;
70         return tableRow[nextChIndex / 8] & (1 << (nextChIndex % 8));
71     }
72     // Otherwise defer to the Unicode algorithm by returning false.
73     return false;
74 }
75
76 template<NBSPBehavior nbspBehavior>
77 inline bool needsLineBreakIterator(UChar ch)
78 {
79     if (nbspBehavior == NBSPBehavior::TreatNBSPAsBreak)
80         return ch > asciiLineBreakTableLastChar;
81     return ch > asciiLineBreakTableLastChar && ch != noBreakSpace;
82 }
83
84 // When in non-loose mode, we can use the ASCII shortcut table.
85 template<typename CharacterType, NBSPBehavior nbspBehavior>
86 inline int nextBreakablePositionNonLoosely(LazyLineBreakIterator& lazyBreakIterator, const CharacterType* str, unsigned length, int pos)
87 {
88     int len = static_cast<int>(length);
89     int nextBreak = -1;
90
91     CharacterType lastLastCh = pos > 1 ? str[pos - 2] : static_cast<CharacterType>(lazyBreakIterator.secondToLastCharacter());
92     CharacterType lastCh = pos > 0 ? str[pos - 1] : static_cast<CharacterType>(lazyBreakIterator.lastCharacter());
93     unsigned priorContextLength = lazyBreakIterator.priorContextLength();
94     for (int i = pos; i < len; i++) {
95         CharacterType ch = str[i];
96
97         // Non-loose mode, so use ASCII shortcut (shouldBreakAfter) if not breakable space.
98         if (isBreakableSpace<nbspBehavior>(ch) || shouldBreakAfter(lastLastCh, lastCh, ch))
99             return i;
100
101         // Non-loose mode, so conditionally use break iterator.
102         if (needsLineBreakIterator<nbspBehavior>(ch) || needsLineBreakIterator<nbspBehavior>(lastCh)) {
103             if (nextBreak < i) {
104                 // Don't break if positioned at start of primary context and there is no prior context.
105                 if (i || priorContextLength) {
106                     TextBreakIterator* breakIterator = lazyBreakIterator.get(priorContextLength);
107                     if (breakIterator) {
108                         nextBreak = textBreakFollowing(breakIterator, i - 1 + priorContextLength);
109                         if (nextBreak >= 0)
110                             nextBreak -= priorContextLength;
111                     }
112                 }
113             }
114             if (i == nextBreak && !isBreakableSpace<nbspBehavior>(lastCh))
115                 return i;
116         }
117
118         lastLastCh = lastCh;
119         lastCh = ch;
120     }
121
122     return len;
123 }
124
125 // When in loose mode, we can't use the ASCII shortcut table since loose mode allows "$100" to break after '$' in content marked as CJK.
126 // N.B. It should be possible to combine the following with the non-loose version above by adding a LooseBehavior template parameter;
127 // however, when doing this, a 10% performance regression appeared on chromium-win (https://bugs.webkit.org/show_bug.cgi?id=89235#c112).
128 template<typename CharacterType, NBSPBehavior nbspBehavior>
129 static inline int nextBreakablePositionLoosely(LazyLineBreakIterator& lazyBreakIterator, const CharacterType* str, unsigned length, int pos)
130 {
131     int len = static_cast<int>(length);
132     int nextBreak = -1;
133
134     CharacterType lastCh = pos > 0 ? str[pos - 1] : static_cast<CharacterType>(lazyBreakIterator.lastCharacter());
135     unsigned priorContextLength = lazyBreakIterator.priorContextLength();
136     for (int i = pos; i < len; i++) {
137         CharacterType ch = str[i];
138
139         // Always loose mode, so don't use ASCII shortcut (shouldBreakAfter).
140         if (isBreakableSpace<nbspBehavior>(ch))
141             return i;
142
143         // Always use line break iterator in loose mode.
144         if (nextBreak < i) {
145             // Don't break if positioned at start of primary context and there is no prior context.
146             if (i || priorContextLength) {
147                 TextBreakIterator* breakIterator = lazyBreakIterator.get(priorContextLength);
148                 if (breakIterator) {
149                     nextBreak = textBreakFollowing(breakIterator, i - 1 + priorContextLength);
150                     if (nextBreak >= 0)
151                         nextBreak -= priorContextLength;
152                 }
153             }
154         }
155         if (i == nextBreak && !isBreakableSpace<nbspBehavior>(lastCh))
156             return i;
157
158         lastCh = ch;
159     }
160
161     return len;
162 }
163
164 template<typename CharacterType, NBSPBehavior nbspBehavior>
165 inline unsigned nextBreakablePositionKeepingAllWords(const CharacterType* string, unsigned length, unsigned startPosition)
166 {
167     for (unsigned i = startPosition; i < length; i++) {
168         if (isBreakableSpace<nbspBehavior>(string[i]))
169             return i;
170     }
171     return length;
172 }
173
174 inline unsigned nextBreakablePositionKeepingAllWords(LazyLineBreakIterator& lazyBreakIterator, int startPosition)
175 {
176     String string = lazyBreakIterator.string();
177     if (string.is8Bit())
178         return nextBreakablePositionKeepingAllWords<LChar, NBSPBehavior::TreatNBSPAsBreak>(string.characters8(), string.length(), startPosition);
179     return nextBreakablePositionKeepingAllWords<UChar, NBSPBehavior::TreatNBSPAsBreak>(string.characters16(), string.length(), startPosition);
180 }
181
182 inline unsigned nextBreakablePositionKeepingAllWordsIgnoringNBSP(LazyLineBreakIterator& iterator, int startPosition)
183 {
184     String string = iterator.string();
185     if (string.is8Bit())
186         return nextBreakablePositionKeepingAllWords<LChar, NBSPBehavior::IgnoreNBSP>(string.characters8(), string.length(), startPosition);
187     return nextBreakablePositionKeepingAllWords<UChar, NBSPBehavior::IgnoreNBSP>(string.characters16(), string.length(), startPosition);
188 }
189
190 inline int nextBreakablePosition(LazyLineBreakIterator& iterator, int pos)
191 {
192     String string = iterator.string();
193     if (string.is8Bit())
194         return nextBreakablePositionNonLoosely<LChar, NBSPBehavior::TreatNBSPAsBreak>(iterator, string.characters8(), string.length(), pos);
195     return nextBreakablePositionNonLoosely<UChar, NBSPBehavior::TreatNBSPAsBreak>(iterator, string.characters16(), string.length(), pos);
196 }
197
198 inline int nextBreakablePositionIgnoringNBSP(LazyLineBreakIterator& lazyBreakIterator, int pos)
199 {
200     String string = lazyBreakIterator.string();
201     if (string.is8Bit())
202         return nextBreakablePositionNonLoosely<LChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters8(), string.length(), pos);
203     return nextBreakablePositionNonLoosely<UChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters16(), string.length(), pos);
204 }
205
206 inline int nextBreakablePositionLoose(LazyLineBreakIterator& lazyBreakIterator, int pos)
207 {
208     String string = lazyBreakIterator.string();
209     if (string.is8Bit())
210         return nextBreakablePositionLoosely<LChar, NBSPBehavior::TreatNBSPAsBreak>(lazyBreakIterator, string.characters8(), string.length(), pos);
211     return nextBreakablePositionLoosely<UChar, NBSPBehavior::TreatNBSPAsBreak>(lazyBreakIterator, string.characters16(), string.length(), pos);
212 }
213
214 inline int nextBreakablePositionIgnoringNBSPLoose(LazyLineBreakIterator& lazyBreakIterator, int pos)
215 {
216     String string = lazyBreakIterator.string();
217     if (string.is8Bit())
218         return nextBreakablePositionLoosely<LChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters8(), string.length(), pos);
219     return nextBreakablePositionLoosely<UChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters16(), string.length(), pos);
220 }
221
222 inline bool isBreakable(LazyLineBreakIterator& lazyBreakIterator, int pos, int& nextBreakable, bool breakNBSP, bool isLooseMode, bool keepAllWords)
223 {
224     if (pos <= nextBreakable)
225         return pos == nextBreakable;
226
227     if (keepAllWords) {
228         if (breakNBSP)
229             nextBreakable = static_cast<int>(nextBreakablePositionKeepingAllWords(lazyBreakIterator, pos));
230         else
231             nextBreakable = static_cast<int>(nextBreakablePositionKeepingAllWordsIgnoringNBSP(lazyBreakIterator, pos));
232     } else if (isLooseMode) {
233         if (breakNBSP)
234             nextBreakable = nextBreakablePositionLoose(lazyBreakIterator, pos);
235         else
236             nextBreakable = nextBreakablePositionIgnoringNBSPLoose(lazyBreakIterator, pos);
237     } else {
238         if (breakNBSP)
239             nextBreakable = nextBreakablePosition(lazyBreakIterator, pos);
240         else
241             nextBreakable = nextBreakablePositionIgnoringNBSP(lazyBreakIterator, pos);
242     }
243     return pos == nextBreakable;
244 }
245
246 } // namespace WebCore
247
248 #endif // break_lines_h