1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
/*
* (C) 1999 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights
* reserved.
* Copyright (C) 2007-2009 Torch Mobile, Inc.
* Copyright (C) 2011 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/platform/text/text_break_iterator.h"
#include <unicode/uchar.h>
#include <unicode/uvernum.h>
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/text/break_iterator_data_inline_header.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/ascii_ctype.h"
#include "third_party/blink/renderer/platform/wtf/text/character_names.h"
#include "third_party/blink/renderer/platform/wtf/text/unicode.h"
namespace blink {
// Pack 8 bits into one byte
#define B(a, b, c, d, e, f, g, h) \
((a) | ((b) << 1) | ((c) << 2) | ((d) << 3) | ((e) << 4) | ((f) << 5) | \
((g) << 6) | ((h) << 7))
#define BA_LB_COUNT U_LB_COUNT
// Line breaking table for CSS word-break: break-all. This table differs from
// asciiLineBreakTable in:
// - Indices are Line Breaking Classes defined in UAX#14 Unicode Line Breaking
// Algorithm: http://unicode.org/reports/tr14/#DescriptionOfProperties
// - 1 indicates additional break opportunities. 0 indicates to fallback to
// normal line break, not "prohibit break."
// clang-format off
static const unsigned char kBreakAllLineBreakClassTable[][BA_LB_COUNT / 8 + 1] = {
// XX AI AL B2 BA BB BK CB CL CM CR EX GL HY ID IN IS LF NS NU OP PO PR QU SA SG SP SY ZW NL WJ H2 H3 JL JT JV CP CJ HL RI EB EM ZWJ AK AP AS VF VI
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 0, 0, 0, 0, 0) }, // HL
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // RI
// Added in ICU 58.
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // EB
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // EM
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // ZWJ
#if U_ICU_VERSION_MAJOR_NUM >= 74
// Added in ICU 74. https://icu.unicode.org/download/74
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // AK
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // AP
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // AS
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // VF
{ B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0), B(0, 0, 0, 0, 0, 0, 0, 0) }, // VI
#endif // U_ICU_VERSION_MAJOR_NUM >= 74
};
// clang-format on
#undef B
static_assert(std::size(kBreakAllLineBreakClassTable) == BA_LB_COUNT,
"breakAllLineBreakClassTable should be consistent");
static inline ULineBreak LineBreakPropertyValue(UChar last_ch, UChar ch) {
if (ch == '+') // IE tailors '+' to AL-like class when break-all is enabled.
return U_LB_ALPHABETIC;
UChar32 ch32 = U16_IS_LEAD(last_ch) && U16_IS_TRAIL(ch)
? U16_GET_SUPPLEMENTARY(last_ch, ch)
: ch;
return static_cast<ULineBreak>(u_getIntPropertyValue(ch32, UCHAR_LINE_BREAK));
}
static inline bool ShouldBreakAfterBreakAll(ULineBreak last_line_break,
ULineBreak line_break) {
if (line_break >= 0 && line_break < BA_LB_COUNT && last_line_break >= 0 &&
last_line_break < BA_LB_COUNT) {
const unsigned char* table_row =
UNSAFE_TODO(kBreakAllLineBreakClassTable[last_line_break]);
return UNSAFE_TODO(table_row[line_break / 8]) & (1 << (line_break % 8));
}
return false;
}
// Computes if 'word-break:keep-all' should prevent line break.
// https://drafts.csswg.org/css-text-3/#valdef-word-break-keep-all
// The spec is not very verbose on how this should work. This logic prevents L/M
// general categories and complex line breaking since the spec says "except some
// south east aisans".
// https://github.com/w3c/csswg-drafts/issues/1619
static inline bool ShouldKeepAfterKeepAll(UChar last_ch,
UChar ch,
UChar next_ch) {
UChar pre_ch = U_MASK(u_charType(ch)) & U_GC_M_MASK ? last_ch : ch;
return U_MASK(u_charType(pre_ch)) & (U_GC_L_MASK | U_GC_N_MASK) &&
!WTF::unicode::HasLineBreakingPropertyComplexContext(pre_ch) &&
U_MASK(u_charType(next_ch)) & (U_GC_L_MASK | U_GC_N_MASK) &&
!WTF::unicode::HasLineBreakingPropertyComplexContext(next_ch);
}
enum class FastBreakResult : uint8_t { kNoBreak, kCanBreak, kUnknown };
template <typename CharacterType>
struct LazyLineBreakIterator::Context {
STACK_ALLOCATED();
public:
struct ContextChar {
STACK_ALLOCATED();
public:
ContextChar() = default;
explicit ContextChar(UChar ch) : ch(ch), is_space(IsBreakableSpace(ch)) {}
UChar ch = 0;
bool is_space = false;
};
Context(const CharacterType* str,
unsigned len,
unsigned start_offset,
unsigned index) {
DCHECK_GE(index, start_offset);
CHECK_LE(index, len);
if (index > start_offset) {
last = ContextChar(UNSAFE_TODO(str[index - 1]));
if (index > start_offset + 1) {
last_last_ch = UNSAFE_TODO(str[index - 2]);
}
}
}
bool Fetch(const CharacterType* str, unsigned len, unsigned index) {
if (index >= len) [[unlikely]] {
return false;
}
current = ContextChar(UNSAFE_TODO(str[index]));
return true;
}
void Advance(unsigned& index) {
++index;
last_last_ch = last.ch;
last = current;
}
FastBreakResult ShouldBreakFast(bool disable_soft_hyphen) const {
const UChar last_ch = last.ch;
const UChar ch = current.ch;
if (last_ch < kFastLineBreakMinChar || ch < kFastLineBreakMinChar)
[[unlikely]] {
return FastBreakResult::kNoBreak;
}
// U+002D HYPHEN-MINUS may depend on the context.
static_assert('-' >= kFastLineBreakMinChar);
if (last_ch == '-') [[unlikely]] {
if (ch <= 0x7F) {
// Up to U+007F is fast-breakable. See `LineBreakData::FillAscii()`.
if (IsASCIIDigit(ch)) {
// Don't allow line breaking between '-' and a digit if the '-' may
// mean a minus sign in the context, while allow breaking in
// 'ABCD-1234' and '1234-5678' which may be in long URLs.
return IsASCIIAlphanumeric(last_last_ch) ? FastBreakResult::kCanBreak
: FastBreakResult::kNoBreak;
}
} else {
// Defer to the Unicode algorithm to take more context into account.
return FastBreakResult::kUnknown;
}
}
// If both characters are in the fast line break table, use it for enhanced
// speed. For ASCII characters, it is also for compatibility. The table is
// generated at the build time, see the `LineBreakData` class.
if (last_ch <= kFastLineBreakMaxChar && ch <= kFastLineBreakMaxChar) {
if (!GetFastLineBreak(last_ch, ch)) {
return FastBreakResult::kNoBreak;
}
static_assert(kSoftHyphenCharacter <= kFastLineBreakMaxChar);
if (disable_soft_hyphen && last_ch == kSoftHyphenCharacter) [[unlikely]] {
return FastBreakResult::kNoBreak;
}
return FastBreakResult::kCanBreak;
}
// Otherwise defer to the Unicode algorithm.
static_assert(kNoBreakSpaceCharacter <= kFastLineBreakMaxChar,
"Include NBSP for the performance.");
return FastBreakResult::kUnknown;
}
ContextChar current;
ContextChar last;
CharacterType last_last_ch = 0;
};
template <typename CharacterType,
LineBreakType line_break_type,
BreakSpaceType break_space>
inline unsigned LazyLineBreakIterator::NextBreakablePosition(
unsigned pos,
const CharacterType* str,
unsigned len) const {
Context<CharacterType> context(str, len, start_offset_, pos);
unsigned next_break = 0;
ULineBreak last_line_break;
if constexpr (line_break_type == LineBreakType::kBreakAll) {
last_line_break =
LineBreakPropertyValue(context.last_last_ch, context.last.ch);
}
for (unsigned i = pos; context.Fetch(str, len, i); context.Advance(i)) {
switch (break_space) {
case BreakSpaceType::kAfterSpaceRun:
if (context.current.is_space) {
continue;
}
if (context.last.is_space) {
return i;
}
break;
case BreakSpaceType::kAfterEverySpace:
if (context.last.is_space ||
Character::IsOtherSpaceSeparator(context.last.ch)) {
return i;
}
if ((context.current.is_space ||
Character::IsOtherSpaceSeparator(context.current.ch)) &&
i + 1 < len) {
return i + 1;
}
break;
}
const FastBreakResult fast_break_result =
context.ShouldBreakFast(disable_soft_hyphen_);
if (fast_break_result == FastBreakResult::kCanBreak) {
return i;
}
if constexpr (line_break_type == LineBreakType::kBreakAll) {
if (!U16_IS_LEAD(context.current.ch)) {
ULineBreak line_break =
LineBreakPropertyValue(context.last.ch, context.current.ch);
if (ShouldBreakAfterBreakAll(last_line_break, line_break)) {
return i > pos && U16_IS_TRAIL(context.current.ch) ? i - 1 : i;
}
if (line_break != U_LB_COMBINING_MARK) {
last_line_break = line_break;
}
}
} else if constexpr (line_break_type == LineBreakType::kKeepAll) {
if (ShouldKeepAfterKeepAll(context.last_last_ch, context.last.ch,
context.current.ch)) {
// word-break:keep-all prevents breaks between East Asian ideographic.
continue;
}
}
if (fast_break_result == FastBreakResult::kNoBreak) {
continue;
}
if (next_break < i || !next_break) {
// Don't break if positioned at start of primary context.
if (i <= start_offset_) [[unlikely]] {
continue;
}
TextBreakIterator* break_iterator = GetIterator();
if (!break_iterator) [[unlikely]] {
continue;
}
next_break = i - 1;
for (;;) {
// Adjust the offset by |start_offset_| because |break_iterator|
// has text after |start_offset_|.
DCHECK_GE(next_break, start_offset_);
const int32_t following = break_iterator->following(
static_cast<int32_t>(next_break - start_offset_));
if (following < 0) [[unlikely]] {
DCHECK_EQ(following, icu::BreakIterator::DONE);
next_break = len;
break;
}
next_break = following + start_offset_;
if (disable_soft_hyphen_ && next_break > 0 &&
UNSAFE_TODO(str[next_break - 1]) == kSoftHyphenCharacter)
[[unlikely]] {
continue;
}
break;
}
}
if (i == next_break && !context.last.is_space) {
return i;
}
}
return len;
}
template <typename CharacterType, LineBreakType lineBreakType>
inline unsigned LazyLineBreakIterator::NextBreakablePosition(
unsigned pos,
const CharacterType* str,
unsigned len) const {
switch (break_space_) {
case BreakSpaceType::kAfterSpaceRun:
return NextBreakablePosition<CharacterType, lineBreakType,
BreakSpaceType::kAfterSpaceRun>(pos, str,
len);
case BreakSpaceType::kAfterEverySpace:
return NextBreakablePosition<CharacterType, lineBreakType,
BreakSpaceType::kAfterEverySpace>(pos, str,
len);
}
NOTREACHED();
}
template <LineBreakType lineBreakType>
inline unsigned LazyLineBreakIterator::NextBreakablePosition(
unsigned pos,
unsigned len) const {
if (string_.IsNull()) [[unlikely]] {
return 0;
}
if (string_.Is8Bit()) {
return NextBreakablePosition<LChar, lineBreakType>(
pos, UNSAFE_TODO(string_.Characters8()), len);
}
return NextBreakablePosition<UChar, lineBreakType>(
pos, UNSAFE_TODO(string_.Characters16()), len);
}
unsigned LazyLineBreakIterator::NextBreakablePositionBreakCharacter(
unsigned pos) const {
DCHECK_LE(start_offset_, string_.length());
CharacterBreakIterator& iterator = GetCharacterBreakIterator();
DCHECK_GE(pos, start_offset_);
pos -= start_offset_;
// `- 1` because the `Following()` returns the next opportunity after the
// given `offset`.
int32_t next =
iterator.Following(static_cast<int32_t>(pos > 0 ? pos - 1 : 0));
return next != kTextBreakDone ? next + start_offset_ : string_.length();
}
unsigned LazyLineBreakIterator::NextBreakablePosition(unsigned pos,
unsigned len) const {
switch (break_type_) {
case LineBreakType::kNormal:
case LineBreakType::kPhrase:
return NextBreakablePosition<LineBreakType::kNormal>(pos, len);
case LineBreakType::kBreakAll:
return NextBreakablePosition<LineBreakType::kBreakAll>(pos, len);
case LineBreakType::kKeepAll:
return NextBreakablePosition<LineBreakType::kKeepAll>(pos, len);
case LineBreakType::kBreakCharacter:
return NextBreakablePositionBreakCharacter(pos);
}
NOTREACHED();
}
unsigned LazyLineBreakIterator::NextBreakOpportunity(unsigned offset) const {
DCHECK_LE(offset, string_.length());
return NextBreakablePosition(offset, string_.length());
}
unsigned LazyLineBreakIterator::NextBreakOpportunity(unsigned offset,
unsigned len) const {
DCHECK_LE(offset, len);
DCHECK_LE(len, string_.length());
return NextBreakablePosition(offset, len);
}
unsigned LazyLineBreakIterator::PreviousBreakOpportunity(unsigned offset,
unsigned min) const {
unsigned pos = std::min(offset, string_.length());
// +2 to ensure at least one code point is included.
unsigned end = std::min(pos + 2, string_.length());
while (pos > min) {
unsigned next_break = NextBreakablePosition(pos, end);
if (next_break == pos) {
return next_break;
}
// There's no break opportunities at |pos| or after.
end = pos;
if (string_.Is8Bit())
--pos;
else
UNSAFE_TODO(U16_BACK_1(string_.Characters16(), 0, pos));
}
return min;
}
std::ostream& operator<<(std::ostream& ostream, LineBreakType line_break_type) {
switch (line_break_type) {
case LineBreakType::kNormal:
return ostream << "Normal";
case LineBreakType::kBreakAll:
return ostream << "BreakAll";
case LineBreakType::kBreakCharacter:
return ostream << "BreakCharacter";
case LineBreakType::kKeepAll:
return ostream << "KeepAll";
case LineBreakType::kPhrase:
return ostream << "Phrase";
}
NOTREACHED() << "LineBreakType::" << static_cast<int>(line_break_type);
}
std::ostream& operator<<(std::ostream& ostream, BreakSpaceType break_space) {
switch (break_space) {
case BreakSpaceType::kAfterSpaceRun:
return ostream << "kAfterSpaceRun";
case BreakSpaceType::kAfterEverySpace:
return ostream << "kAfterEverySpace";
}
NOTREACHED() << "BreakSpaceType::" << static_cast<int>(break_space);
}
} // namespace blink
|