LibLine: Port text segmentation to the ICU text segmenter

This commit is contained in:
Timothy Flynn 2024-06-19 13:14:54 -04:00 committed by Andreas Kling
commit 3974996e95
Notes: sideshowbarker 2024-07-17 08:59:18 +09:00
2 changed files with 5 additions and 9 deletions

View file

@ -7,4 +7,4 @@ set(SOURCES
) )
serenity_lib(LibLine line) serenity_lib(LibLine line)
target_link_libraries(LibLine PRIVATE LibCore LibUnicode) target_link_libraries(LibLine PRIVATE LibCore LibLocale)

View file

@ -21,7 +21,7 @@
#include <LibCore/Event.h> #include <LibCore/Event.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibCore/Notifier.h> #include <LibCore/Notifier.h>
#include <LibUnicode/Segmentation.h> #include <LibLocale/Segmenter.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
@ -1916,8 +1916,10 @@ StringMetrics Editor::actual_rendered_string_metrics(Utf32View const& view, RedB
auto mask_it = masks.begin(); auto mask_it = masks.begin();
auto segmenter = Locale::Segmenter::create(Locale::SegmenterGranularity::Grapheme);
Vector<size_t> grapheme_breaks; Vector<size_t> grapheme_breaks;
Unicode::for_each_grapheme_segmentation_boundary(view, [&](size_t offset) -> IterationDecision {
segmenter->for_each_boundary(view, [&](size_t offset) -> IterationDecision {
if (offset >= view.length()) if (offset >= view.length())
return IterationDecision::Break; return IterationDecision::Break;
@ -1925,12 +1927,6 @@ StringMetrics Editor::actual_rendered_string_metrics(Utf32View const& view, RedB
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
// In case Unicode data isn't available, default to using code points as grapheme boundaries.
if (grapheme_breaks.is_empty()) {
for (size_t i = 0; i < view.length(); ++i)
grapheme_breaks.append(i);
}
for (size_t break_index = 0; break_index < grapheme_breaks.size(); ++break_index) { for (size_t break_index = 0; break_index < grapheme_breaks.size(); ++break_index) {
auto i = grapheme_breaks[break_index]; auto i = grapheme_breaks[break_index];
auto c = view[i]; auto c = view[i];