From ec44545e82a8df247a11954782fdab9b51303013 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 12 Dec 2017 17:44:34 -0800 Subject: [PATCH] libtxt: improvements to GetWordBoundary (flutter/engine#4446) * fix an off-by-one when the offset itself is a word boundary * lazily create the word break iterator --- .../third_party/txt/src/txt/paragraph.cc | 19 +++++++++++-------- .../third_party/txt/src/txt/paragraph.h | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc index 02a51c4cf8..ec554f0ff1 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc @@ -991,17 +991,20 @@ Paragraph::Range Paragraph::GetWordBoundary(size_t offset) const { if (text_.size() == 0) return Range(0, 0); - UErrorCode status = U_ZERO_ERROR; - std::unique_ptr break_iter( - icu::BreakIterator::createWordInstance(icu::Locale(), status)); - if (!U_SUCCESS(status)) - return Range(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(0, 0); + } - int32_t prev_boundary = break_iter->preceding(offset); + word_breaker_->setText(icu::UnicodeString(false, text_.data(), text_.size())); + + 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(prev_boundary, next_boundary); diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.h b/engine/src/flutter/third_party/txt/src/txt/paragraph.h index 7f18f3df7a..43b5ae9fe2 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.h +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.h @@ -193,6 +193,7 @@ class Paragraph { std::shared_ptr font_collection_; minikin::LineBreaker breaker_; + mutable std::unique_ptr word_breaker_; struct LineRange { LineRange(size_t s, size_t e, bool h) : start(s), end(e), hard_break(h) {}