Debian Codegraph @

modules/skunicode/src/SkUnicode_icu4x.cpp

raw
1 /*
2 * Copyright 2023 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "include/core/SkSpan.h"
8 #include "include/core/SkString.h"
9 #include "include/core/SkTypes.h"
10 #include "include/private/base/SkAssert.h"
11 #include "include/private/base/SkTArray.h"
12 #include "include/private/base/SkTo.h"
13 #include "modules/skunicode/include/SkUnicode.h"
14 #include "modules/skunicode/include/SkUnicode_icu4x.h"
15 #include "modules/skunicode/src/SkUnicode_hardcoded.h"
16 #include "src/base/SkBitmaskEnum.h"
17 #include "src/base/SkUTF.h"
18
19 #include <ICU4XBidi.hpp>
20 #include <ICU4XCaseMapper.hpp>
21 #include <ICU4XCodePointMapData8.hpp>
22 #include <ICU4XCodePointSetData.hpp>
23 #include <ICU4XDataProvider.hpp>
24 #include <ICU4XGraphemeClusterSegmenter.hpp>
25 #include <ICU4XLineSegmenter.hpp>
26 #include <ICU4XWordSegmenter.hpp>
27
28 #include <algorithm>
29 #include <cstdint>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34
35 class SkUnicode_icu4x : public SkUnicode {
36 public:
37 SkUnicode_icu4x() {
38 fLocale = ICU4XLocale::create_from_string("tr").ok().value();
39 fDataProvider = ICU4XDataProvider::create_compiled();
40 fCaseMapper = ICU4XCaseMapper::create(fDataProvider).ok().value();
41 const auto general = ICU4XCodePointMapData8::load_general_category(fDataProvider).ok().value();
42 fControls = general.get_set_for_value(/*Control*/15);
43 fWhitespaces = general.get_set_for_value(/*SpaceSeparator*/12);
44 fSpaces = general.get_set_for_value(/*SpaceSeparator*/12);
45 // TODO: u_isSpace
46 fBlanks = ICU4XCodePointSetData::load_blank(fDataProvider).ok().value();
47 fEmoji = ICU4XCodePointSetData::load_emoji(fDataProvider).ok().value();
48 fEmojiComponent = ICU4XCodePointSetData::load_emoji_component(fDataProvider).ok().value();
49 fEmojiModifier = ICU4XCodePointSetData::load_emoji_modifier(fDataProvider).ok().value();
50 fEmojiModifierBase = ICU4XCodePointSetData::load_emoji_modifier_base(fDataProvider).ok().value();
51 fEmoji = ICU4XCodePointSetData::load_emoji(fDataProvider).ok().value();
52 fRegionalIndicator = ICU4XCodePointSetData::load_regional_indicator(fDataProvider).ok().value();
53 fIdeographic = ICU4XCodePointSetData::load_ideographic(fDataProvider).ok().value();
54 fLineBreaks = ICU4XCodePointMapData8::load_line_break(fDataProvider).ok().value();
55 }
56
57 ~SkUnicode_icu4x() override = default;
58
59 void reset();
60
61 // SkUnicode properties
62 bool isControl(SkUnichar utf8) override { return fControls.contains(utf8); }
63 bool isWhitespace(SkUnichar utf8) override { return fWhitespaces.contains(utf8); }
64 bool isSpace(SkUnichar utf8) override { return fBlanks.contains(utf8); }
65 bool isHardBreak(SkUnichar utf8) override {
66 auto value = fLineBreaks.get(utf8);
67 return (value == /*MandatoryBreak*/6) ||
68 (value == /*CarriageReturn*/10) ||
69 (value == /*LineFeed*/17) ||
70 (value == /*NextLine*/29);
71 }
72 bool isEmoji(SkUnichar utf8) override { return fEmoji.contains(utf8); }
73 bool isEmojiComponent(SkUnichar utf8) override { return fEmojiComponent.contains(utf8); }
74 bool isEmojiModifierBase(SkUnichar utf8) override { return fEmojiModifierBase.contains(utf8); }
75 bool isEmojiModifier(SkUnichar utf8) override { return fEmojiModifier.contains(utf8); }
76 bool isRegionalIndicator(SkUnichar utf8) override { return fRegionalIndicator.contains(utf8); }
77 bool isIdeographic(SkUnichar utf8) override { return fIdeographic.contains(utf8); }
78
79 // TODO: is there a check for tabulation
80 bool isTabulation(SkUnichar utf8) override {
81 return utf8 == '\t';
82 }
83
84 // For SkShaper
85 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
86 SkBidiIterator::Direction dir) override;
87 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
88 int count,
89 SkBidiIterator::Direction dir) override;
90 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
91 BreakType breakType) override;
92 std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType breakType) override;
93 // For SkParagraph
94 bool getBidiRegions(const char utf8[],
95 int utf8Units,
96 TextDirection dir,
97 std::vector<BidiRegion>* results) override {
98
99 const auto bidi = ICU4XBidi::create(fDataProvider).ok().value();
100 std::string_view string_view(utf8, utf8Units);
101 auto info = bidi.for_text(string_view, dir == TextDirection::kLTR ? 0 : 1);
102 auto currentLevel = info.level_at(0);
103 size_t start = 0;
104
105 for (size_t i = 1; i < info.size(); i++) {
106 const auto level = info.level_at(i);
107 if (level != currentLevel) {
108 (*results).emplace_back(start, i, currentLevel);
109 currentLevel = level;
110 start = i;
111 }
112 }
113 (*results).emplace_back(start, info.size(), currentLevel);
114 return true;
115 }
116
117 bool getBidiRegions(const uint16_t utf16[],
118 int utf16Units,
119 TextDirection dir,
120 std::vector<BidiRegion>* results) {
121 auto utf16Char = reinterpret_cast<const char16_t*>(utf16);
122 auto utf8 = SkUnicode::convertUtf16ToUtf8(utf16Char, utf16Units);
123 return this->getBidiRegions(utf8.data(), utf8.size(), dir, results);
124 }
125
126 bool computeCodeUnitFlags(char utf8[],
127 int utf8Units,
128 bool replaceTabs,
129 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) override {
130 results->clear();
131 results->push_back_n(utf8Units + 1, CodeUnitFlags::kNoCodeUnitFlag);
132 this->markLineBreaks(utf8, utf8Units, /*hardLineBreaks=*/false, results);
133 this->markHardLineBreaksHack(utf8, utf8Units, results);
134 this->markGraphemes(utf8, utf8Units, results);
135 this->markCharacters(utf8, utf8Units, replaceTabs, results);
136 return true;
137 }
138
139 bool computeCodeUnitFlags(char16_t utf16[], int utf16Units, bool replaceTabs,
140 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) override {
141 SkASSERT(false);
142 return true;
143 }
144
145 bool getWords(const char utf8[],
146 int utf8Units,
147 const char* locale,
148 std::vector<Position>* results) override {
149 auto utf16 = SkUnicode::convertUtf8ToUtf16(utf8, utf8Units);
150 const diplomat::span<const uint16_t> span((uint16_t*)utf16.data(), utf16.size());
151 const auto segmenter = ICU4XWordSegmenter::create_dictionary(fDataProvider).ok().value();
152 auto iterator = segmenter.segment_utf16(span);
153 while (true) {
154 int32_t breakpoint = iterator.next();
155 if (breakpoint == -1) {
156 break;
157 }
158 results->emplace_back(breakpoint);
159 }
160 return true;
161 }
162
163 SkString toUpper(const SkString& str) override {
164 return toUpper(str, "und");
165 }
166
167 SkString toUpper(const SkString& str, const char* localeStr) override {
168 auto locale = ICU4XLocale::create_from_string(localeStr).ok().value();
169 std::string std_string(str.data(), str.size());
170 // TODO: upper case
171 auto result = fCaseMapper.uppercase(std_string, locale).ok().value();
172 return SkString(result.data(), result.size());
173 }
174
175 void reorderVisual(const BidiLevel runLevels[],
176 int levelsCount,
177 int32_t logicalFromVisual[]) override {
178
179 if (levelsCount < 1) {
180 // To avoid an assert in unicode
181 return;
182 }
183 SkASSERT(runLevels != nullptr);
184 const auto bidi = ICU4XBidi::create(fDataProvider).ok().value();
185 const diplomat::span<const uint8_t> levels(&runLevels[0], levelsCount);
186 auto map = bidi.reorder_visual(levels);
187 SkASSERT(SkToSizeT(levelsCount) == map.len());
188 std::vector<int32_t> results;
189 for (size_t i = 0; i < map.len(); i++) {
190 auto level = map.get(i);
191 logicalFromVisual[i] = SkToS32(level);
192 }
193 }
194
195 private:
196 friend class SkBreakIterator_icu4x;
197 friend class SkBidiIterator_icu4x;
198
199 bool markHardLineBreaksHack(char utf8[],
200 int utf8Units,
201 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) {
202 const char* end = utf8 + utf8Units;
203 const char* ch = utf8;
204 while (ch < end) {
205 auto unichar = SkUTF::NextUTF8(&ch, end);
206 if (this->isHardBreak(unichar)) {
207 (*results)[ch - utf8] |= CodeUnitFlags::kHardLineBreakBefore;
208 }
209 }
210 return true;
211 }
212
213 SkUnichar getChar32(const char* pointer, const char* end) {
214 if (pointer < end) {
215 return SkUTF::NextUTF8(&pointer, end);
216 }
217 return -1;
218 }
219
220 bool markLineBreaks(char utf8[],
221 int utf8Units,
222 bool hardLineBreaks,
223 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) {
224 if (utf8Units == 0) {
225 return true;
226 }
227 // TODO: Remove hard line break hack and detect it here
228 SkASSERT(!hardLineBreaks);
229 const auto lineBreakingOptions = hardLineBreaks
230 ? ICU4XLineBreakOptionsV1{ICU4XLineBreakStrictness::Strict, ICU4XLineBreakWordOption::Normal, false}
231 : ICU4XLineBreakOptionsV1{ICU4XLineBreakStrictness::Loose, ICU4XLineBreakWordOption::Normal, false};
232 const auto segmenter = ICU4XLineSegmenter::create_auto_with_options_v1(fDataProvider, lineBreakingOptions).ok().value();
233 std::string_view string_view(utf8, utf8Units);
234 auto iterator = segmenter.segment_utf8(string_view);
235
236 while (true) {
237 int32_t lineBreak = iterator.next();
238 if (lineBreak == -1) {
239 break;
240 }
241 if (hardLineBreaks) {
242 (*results)[lineBreak] |= CodeUnitFlags::kHardLineBreakBefore;
243 } else {
244 (*results)[lineBreak] |= CodeUnitFlags::kSoftLineBreakBefore;
245 }
246 }
247 if (!hardLineBreaks) {
248 (*results)[0] |= CodeUnitFlags::kSoftLineBreakBefore;
249 (*results)[utf8Units] |= CodeUnitFlags::kSoftLineBreakBefore;
250 }
251 return true;
252 }
253
254 bool markGraphemes(const char utf8[],
255 int utf8Units,
256 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) {
257 const auto segmenter = ICU4XGraphemeClusterSegmenter::create(fDataProvider).ok().value();
258 std::string_view string_view(utf8, utf8Units);
259 auto iterator = segmenter.segment_utf8(string_view);
260 while (true) {
261 int32_t graphemeStart = iterator.next();
262 if (graphemeStart == -1) {
263 break;
264 }
265 (*results)[graphemeStart] |= CodeUnitFlags::kGraphemeStart;
266 }
267 return true;
268 }
269
270 bool markCharacters(char utf8[],
271 int utf8Units,
272 bool replaceTabs,
273 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) {
274 const char* current = utf8;
275 const char* end = utf8 + utf8Units;
276 while (current < end) {
277 auto before = current - utf8;
278 SkUnichar unichar = SkUTF::NextUTF8(&current, end);
279 if (unichar < 0) unichar = 0xFFFD;
280 auto after = current - utf8;
281 if (replaceTabs && SkUnicode_icu4x::isTabulation(unichar)) {
282 results->at(before) |= SkUnicode::kTabulation;
283 if (replaceTabs) {
284 unichar = ' ';
285 utf8[before] = ' ';
286 }
287 }
288 for (auto i = before; i < after; ++i) {
289 bool isHardBreak = this->isHardBreak(unichar);
290 bool isSpace = this->isSpace(unichar) || isHardBreak;
291 bool isWhitespace = this->isWhitespace(unichar) || isHardBreak;
292 if (isSpace) {
293 results->at(i) |= SkUnicode::kPartOfIntraWordBreak;
294 }
295 if (isWhitespace) {
296 results->at(i) |= SkUnicode::kPartOfWhiteSpaceBreak;
297 }
298 if (this->isControl(unichar)) {
299 results->at(i) |= SkUnicode::kControl;
300 }
301 }
302 }
303 return true;
304 }
305
306 bool getUtf8Words(const char utf8[],
307 int utf8Units,
308 const char* locale,
309 std::vector<Position>* results) override {
310 SkDEBUGF("Method 'getUtf8Words' is not implemented\n");
311 return false;
312 }
313
314 bool getSentences(const char utf8[],
315 int utf8Units,
316 const char* locale,
317 std::vector<SkUnicode::Position>* results) override {
318 SkDEBUGF("Method 'getSentences' is not implemented\n");
319 return false;
320 }
321
322 std::shared_ptr<std::vector<SkUnicode::BidiRegion>> fRegions;
323 ICU4XLocale fLocale;
324 ICU4XDataProvider fDataProvider;
325 ICU4XCaseMapper fCaseMapper;
326 ICU4XCodePointSetData fWhitespaces;
327 ICU4XCodePointSetData fSpaces;
328 ICU4XCodePointSetData fBlanks;
329 ICU4XCodePointSetData fEmoji;
330 ICU4XCodePointSetData fEmojiComponent;
331 ICU4XCodePointSetData fEmojiModifier;
332 ICU4XCodePointSetData fEmojiModifierBase;
333 ICU4XCodePointSetData fRegionalIndicator;
334 ICU4XCodePointSetData fIdeographic;
335 ICU4XCodePointSetData fControls;
336 ICU4XCodePointMapData8 fLineBreaks;
337 };
338
339 class SkBreakIterator_icu4x: public SkBreakIterator {
340 public:
341 SkBreakIterator_icu4x() { }
342 Position first() override { SkASSERT(false); return -1; }
343 Position current() override { SkASSERT(false); return -1; }
344 Position next() override { SkASSERT(false); return -1; }
345 Status status() override { SkASSERT(false); return -1; }
346 bool isDone() override { SkASSERT(false); return false; }
347 bool setText(const char utftext8[], int utf8Units) override { SkASSERT(false); return false; }
348 bool setText(const char16_t utftext16[], int utf16Units) override { SkASSERT(false); return false; }
349 };
350
351 class SkBidiIterator_icu4x : public SkBidiIterator {
352 std::shared_ptr<std::vector<SkUnicode::BidiRegion>> fRegions;
353 public:
354 explicit SkBidiIterator_icu4x(std::shared_ptr<std::vector<SkUnicode::BidiRegion>> regions)
355 : fRegions(regions) { }
356 Position getLength() override { return fRegions->size(); }
357 Level getLevelAt(Position pos) override {
358 auto found = std::lower_bound(
359 fRegions->begin(),
360 fRegions->end(),
361 SkUnicode::BidiRegion(pos, pos, 0),
362 [](const SkUnicode::BidiRegion& a, const SkUnicode::BidiRegion& b) {
363 return a.start <= b.start && a.end <= b.end;
364 });
365 return found->level;
366 }
367 };
368
369 std::unique_ptr<SkBidiIterator> SkUnicode_icu4x::makeBidiIterator(const uint16_t text[], int count,
370 SkBidiIterator::Direction dir) {
371 if (fRegions) {
372 fRegions->clear();
373 } else {
374 fRegions = std::make_shared<std::vector<SkUnicode::BidiRegion>>();
375 }
376
377 if (this->getBidiRegions(text, count, dir == SkBidiIterator::Direction::kLTR ? TextDirection::kLTR : TextDirection::kRTL, fRegions.get())) {
378 return std::make_unique<SkBidiIterator_icu4x>(fRegions);
379 } else {
380 return nullptr;
381 }
382 }
383
384 std::unique_ptr<SkBidiIterator> SkUnicode_icu4x::makeBidiIterator(const char text[],
385 int count,
386 SkBidiIterator::Direction dir) {
387 if (fRegions) {
388 fRegions->clear();
389 } else {
390 fRegions = std::make_shared<std::vector<SkUnicode::BidiRegion>>();
391 }
392 if (this->getBidiRegions(text, count, dir == SkBidiIterator::Direction::kLTR ? TextDirection::kLTR : TextDirection::kRTL, fRegions.get())) {
393 return std::make_unique<SkBidiIterator_icu4x>(fRegions);
394 } else {
395 return nullptr;
396 }
397 }
398
399 std::unique_ptr<SkBreakIterator> SkUnicode_icu4x::makeBreakIterator(const char locale[],
400 BreakType breakType) {
401 SkASSERT(false); return nullptr;
402 }
403
404 std::unique_ptr<SkBreakIterator> SkUnicode_icu4x::makeBreakIterator(BreakType breakType) {
405 SkASSERT(false); return nullptr;
406 }
407
408 namespace SkUnicodes::ICU4X {
409 sk_sp<SkUnicode> Make() {
410 return sk_make_sp<SkUnicode_icu4x>();
411 }
412 }