未验证 提交 28bc7b12 编写于 作者: J Jason Simmons 提交者: GitHub

libtxt: improvements to GetWordBoundary (#4446)

* fix an off-by-one when the offset itself is a word boundary
* lazily create the word break iterator
上级 df188190
......@@ -991,17 +991,20 @@ Paragraph::Range<size_t> Paragraph::GetWordBoundary(size_t offset) const {
if (text_.size() == 0)
return Range<size_t>(0, 0);
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<icu::BreakIterator> break_iter(
icu::BreakIterator::createWordInstance(icu::Locale(), status));
if (!U_SUCCESS(status))
return Range<size_t>(0, 0);
break_iter->setText(icu::UnicodeString(false, text_.data(), text_.size()));
if (!word_breaker_) {
UErrorCode status = U_ZERO_ERROR;
word_breaker_.reset(
icu::BreakIterator::createWordInstance(icu::Locale(), status));
if (!U_SUCCESS(status))
return Range<size_t>(0, 0);
}
word_breaker_->setText(icu::UnicodeString(false, text_.data(), text_.size()));
int32_t prev_boundary = break_iter->preceding(offset);
int32_t prev_boundary = word_breaker_->preceding(offset + 1);
int32_t next_boundary = word_breaker_->next();
if (prev_boundary == icu::BreakIterator::DONE)
prev_boundary = offset;
int32_t next_boundary = break_iter->next();
if (next_boundary == icu::BreakIterator::DONE)
next_boundary = offset;
return Range<size_t>(prev_boundary, next_boundary);
......
......@@ -193,6 +193,7 @@ class Paragraph {
std::shared_ptr<FontCollection> font_collection_;
minikin::LineBreaker breaker_;
mutable std::unique_ptr<icu::BreakIterator> word_breaker_;
struct LineRange {
LineRange(size_t s, size_t e, bool h) : start(s), end(e), hard_break(h) {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册