xref: /aosp_15_r20/external/skia/modules/skplaintexteditor/src/word_boundaries.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 
4 #include "include/core/SkTypes.h"
5 #include "modules/skplaintexteditor/src/word_boundaries.h"
6 #include "modules/skunicode/include/SkUnicode.h"
7 #include <memory>
8 
9 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
10 #include "modules/skunicode/include/SkUnicode_icu.h"
11 #endif
12 
13 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
14 #include "modules/skunicode/include/SkUnicode_libgrapheme.h"
15 #endif
16 
17 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
18 #include "modules/skunicode/include/SkUnicode_icu4x.h"
19 #endif
20 
21 namespace {
get_unicode()22 sk_sp<SkUnicode> get_unicode() {
23 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
24     if (auto unicode = SkUnicodes::ICU::Make()) {
25         return unicode;
26     }
27 #endif  // defined(SK_UNICODE_ICU_IMPLEMENTATION)
28 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
29     if (auto unicode = SkUnicodes::Libgrapheme::Make()) {
30         return unicode;
31     }
32 #endif
33 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
34     if (auto unicode = SkUnicodes::ICU4X::Make()) {
35         return unicode;
36     }
37 #endif
38     SkDEBUGFAIL("Cannot make SkUnicode");
39     return nullptr;
40 }
41 }
42 
GetUtf8WordBoundaries(const char * begin,size_t byteCount,const char * locale)43 std::vector<bool> GetUtf8WordBoundaries(const char* begin, size_t byteCount, const char* locale) {
44     auto unicode = get_unicode();
45     if (nullptr == unicode) {
46         return {};
47     }
48     std::vector<SkUnicode::Position> positions;
49     if (!unicode->getWords(begin, byteCount, locale, &positions) || byteCount == 0) {
50         return {};
51     }
52     std::vector<bool> result;
53     result.resize(byteCount);
54     for (auto& pos : positions) {
55         result[pos] = true;
56     }
57     return result;
58 }
59