#ifndef break_lines_h
#define break_lines_h
#include <wtf/ASCIICType.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/TextBreakIterator.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
static const UChar asciiLineBreakTableFirstChar = '!';
static const UChar asciiLineBreakTableLastChar = 127;
static const unsigned asciiLineBreakTableColumnCount = (asciiLineBreakTableLastChar - asciiLineBreakTableFirstChar) / 8 + 1;
WEBCORE_EXPORT extern const unsigned char asciiLineBreakTable[][asciiLineBreakTableColumnCount];
enum class NBSPBehavior {
IgnoreNBSP,
TreatNBSPAsBreak,
};
template<NBSPBehavior nbspBehavior>
static inline bool isBreakableSpace(UChar ch)
{
switch (ch) {
case ' ':
case '\n':
case '\t':
return true;
case noBreakSpace:
return nbspBehavior == NBSPBehavior::TreatNBSPAsBreak;
default:
return false;
}
}
inline bool shouldBreakAfter(UChar lastCh, UChar ch, UChar nextCh)
{
if (ch == '-' && isASCIIDigit(nextCh))
return isASCIIAlphanumeric(lastCh);
if (ch >= asciiLineBreakTableFirstChar && ch <= asciiLineBreakTableLastChar && nextCh >= asciiLineBreakTableFirstChar && nextCh <= asciiLineBreakTableLastChar) {
const unsigned char* tableRow = asciiLineBreakTable[ch - asciiLineBreakTableFirstChar];
int nextChIndex = nextCh - asciiLineBreakTableFirstChar;
return tableRow[nextChIndex / 8] & (1 << (nextChIndex % 8));
}
return false;
}
template<NBSPBehavior nbspBehavior>
inline bool needsLineBreakIterator(UChar ch)
{
if (nbspBehavior == NBSPBehavior::TreatNBSPAsBreak)
return ch > asciiLineBreakTableLastChar;
return ch > asciiLineBreakTableLastChar && ch != noBreakSpace;
}
template<typename CharacterType, NBSPBehavior nbspBehavior>
inline int nextBreakablePositionNonLoosely(LazyLineBreakIterator& lazyBreakIterator, const CharacterType* str, unsigned length, int pos)
{
int len = static_cast<int>(length);
int nextBreak = -1;
CharacterType lastLastCh = pos > 1 ? str[pos - 2] : static_cast<CharacterType>(lazyBreakIterator.secondToLastCharacter());
CharacterType lastCh = pos > 0 ? str[pos - 1] : static_cast<CharacterType>(lazyBreakIterator.lastCharacter());
unsigned priorContextLength = lazyBreakIterator.priorContextLength();
for (int i = pos; i < len; i++) {
CharacterType ch = str[i];
if (isBreakableSpace<nbspBehavior>(ch) || shouldBreakAfter(lastLastCh, lastCh, ch))
return i;
if (needsLineBreakIterator<nbspBehavior>(ch) || needsLineBreakIterator<nbspBehavior>(lastCh)) {
if (nextBreak < i) {
if (i || priorContextLength) {
TextBreakIterator* breakIterator = lazyBreakIterator.get(priorContextLength);
if (breakIterator) {
nextBreak = textBreakFollowing(breakIterator, i - 1 + priorContextLength);
if (nextBreak >= 0)
nextBreak -= priorContextLength;
}
}
}
if (i == nextBreak && !isBreakableSpace<nbspBehavior>(lastCh))
return i;
}
lastLastCh = lastCh;
lastCh = ch;
}
return len;
}
template<typename CharacterType, NBSPBehavior nbspBehavior>
static inline int nextBreakablePositionLoosely(LazyLineBreakIterator& lazyBreakIterator, const CharacterType* str, unsigned length, int pos)
{
int len = static_cast<int>(length);
int nextBreak = -1;
CharacterType lastCh = pos > 0 ? str[pos - 1] : static_cast<CharacterType>(lazyBreakIterator.lastCharacter());
unsigned priorContextLength = lazyBreakIterator.priorContextLength();
for (int i = pos; i < len; i++) {
CharacterType ch = str[i];
if (isBreakableSpace<nbspBehavior>(ch))
return i;
if (nextBreak < i) {
if (i || priorContextLength) {
TextBreakIterator* breakIterator = lazyBreakIterator.get(priorContextLength);
if (breakIterator) {
nextBreak = textBreakFollowing(breakIterator, i - 1 + priorContextLength);
if (nextBreak >= 0)
nextBreak -= priorContextLength;
}
}
}
if (i == nextBreak && !isBreakableSpace<nbspBehavior>(lastCh))
return i;
lastCh = ch;
}
return len;
}
template<typename CharacterType, NBSPBehavior nbspBehavior>
inline unsigned nextBreakablePositionKeepingAllWords(const CharacterType* string, unsigned length, unsigned startPosition)
{
for (unsigned i = startPosition; i < length; i++) {
if (isBreakableSpace<nbspBehavior>(string[i]))
return i;
}
return length;
}
inline unsigned nextBreakablePositionKeepingAllWords(LazyLineBreakIterator& lazyBreakIterator, int startPosition)
{
String string = lazyBreakIterator.string();
if (string.is8Bit())
return nextBreakablePositionKeepingAllWords<LChar, NBSPBehavior::TreatNBSPAsBreak>(string.characters8(), string.length(), startPosition);
return nextBreakablePositionKeepingAllWords<UChar, NBSPBehavior::TreatNBSPAsBreak>(string.characters16(), string.length(), startPosition);
}
inline unsigned nextBreakablePositionKeepingAllWordsIgnoringNBSP(LazyLineBreakIterator& iterator, int startPosition)
{
String string = iterator.string();
if (string.is8Bit())
return nextBreakablePositionKeepingAllWords<LChar, NBSPBehavior::IgnoreNBSP>(string.characters8(), string.length(), startPosition);
return nextBreakablePositionKeepingAllWords<UChar, NBSPBehavior::IgnoreNBSP>(string.characters16(), string.length(), startPosition);
}
inline int nextBreakablePosition(LazyLineBreakIterator& iterator, int pos)
{
String string = iterator.string();
if (string.is8Bit())
return nextBreakablePositionNonLoosely<LChar, NBSPBehavior::TreatNBSPAsBreak>(iterator, string.characters8(), string.length(), pos);
return nextBreakablePositionNonLoosely<UChar, NBSPBehavior::TreatNBSPAsBreak>(iterator, string.characters16(), string.length(), pos);
}
inline int nextBreakablePositionIgnoringNBSP(LazyLineBreakIterator& lazyBreakIterator, int pos)
{
String string = lazyBreakIterator.string();
if (string.is8Bit())
return nextBreakablePositionNonLoosely<LChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters8(), string.length(), pos);
return nextBreakablePositionNonLoosely<UChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters16(), string.length(), pos);
}
inline int nextBreakablePositionLoose(LazyLineBreakIterator& lazyBreakIterator, int pos)
{
String string = lazyBreakIterator.string();
if (string.is8Bit())
return nextBreakablePositionLoosely<LChar, NBSPBehavior::TreatNBSPAsBreak>(lazyBreakIterator, string.characters8(), string.length(), pos);
return nextBreakablePositionLoosely<UChar, NBSPBehavior::TreatNBSPAsBreak>(lazyBreakIterator, string.characters16(), string.length(), pos);
}
inline int nextBreakablePositionIgnoringNBSPLoose(LazyLineBreakIterator& lazyBreakIterator, int pos)
{
String string = lazyBreakIterator.string();
if (string.is8Bit())
return nextBreakablePositionLoosely<LChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters8(), string.length(), pos);
return nextBreakablePositionLoosely<UChar, NBSPBehavior::IgnoreNBSP>(lazyBreakIterator, string.characters16(), string.length(), pos);
}
inline bool isBreakable(LazyLineBreakIterator& lazyBreakIterator, int pos, int& nextBreakable, bool breakNBSP, bool isLooseMode, bool keepAllWords)
{
if (pos <= nextBreakable)
return pos == nextBreakable;
if (keepAllWords) {
if (breakNBSP)
nextBreakable = static_cast<int>(nextBreakablePositionKeepingAllWords(lazyBreakIterator, pos));
else
nextBreakable = static_cast<int>(nextBreakablePositionKeepingAllWordsIgnoringNBSP(lazyBreakIterator, pos));
} else if (isLooseMode) {
if (breakNBSP)
nextBreakable = nextBreakablePositionLoose(lazyBreakIterator, pos);
else
nextBreakable = nextBreakablePositionIgnoringNBSPLoose(lazyBreakIterator, pos);
} else {
if (breakNBSP)
nextBreakable = nextBreakablePosition(lazyBreakIterator, pos);
else
nextBreakable = nextBreakablePositionIgnoringNBSP(lazyBreakIterator, pos);
}
return pos == nextBreakable;
}
}
#endif // break_lines_h