Debian Codegraph @

modules/skottie/src/text/Unicode.cpp

raw
1 /*
2 * Copyright 2025 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
8 #include <memory>
9 #include <utility>
10
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkAssert.h"
13 #include "modules/skottie/include/TextShaper.h"
14 #include "modules/skunicode/include/SkUnicode.h"
15
16 namespace {
17
18 // Performs iterator "intersection", i.e. it only returns break positions matching both iterators.
19 class IntersectingBreakIterator final : public SkBreakIterator {
20 public:
21 IntersectingBreakIterator(std::unique_ptr<SkBreakIterator> a,
22 std::unique_ptr<SkBreakIterator> b)
23 : fA(std::move(a))
24 , fB(std::move(b))
25 {
26 SkASSERT(fA);
27 SkASSERT(fB);
28 }
29
30 Position first() override {
31 fCurrent = fA->first();
32 SkAssertResult(fB->first() == fCurrent);
33 fDone = false;
34 return fCurrent;
35 }
36
37 Position current() override {
38 return fCurrent;
39 }
40
41 bool isDone() override {
42 SkASSERT((fA->isDone() || fB->isDone()) == fDone);
43 return fDone;
44 }
45
46 Position next() override {
47 SkASSERT(fDone || (fA->current() == fB->current()));
48 Position pos_a = fCurrent,
49 pos_b = fCurrent;
50
51 while (!fDone) {
52 if (pos_a < pos_b) {
53 pos_a = fA->next();
54 fDone = fA->isDone();
55 } else {
56 pos_b = fB->next();
57 fDone = fB->isDone();
58 }
59
60 if (pos_a == pos_b) {
61 break;
62 }
63 }
64
65 // At this point the positions are either valid and equal (advanced successfully),
66 // or one of them is negative (for the done iterator);
67 SkASSERT(pos_a == pos_b || fDone);
68 SkASSERT((pos_a < 0 || pos_b < 0) == fDone);
69
70 return fCurrent = std::min(pos_a, pos_b);
71 }
72
73 bool setText(const char utftext8[], int utf8Units) override {
74 fDone = false;
75 fCurrent = 0;
76 return fA->setText(utftext8, utf8Units) &&
77 fB->setText(utftext8, utf8Units);
78 }
79
80 bool setText(const char16_t utftext16[], int utf16Units) override {
81 fDone = false;
82 fCurrent = 0;
83 return fA->setText(utftext16, utf16Units) &&
84 fB->setText(utftext16, utf16Units);
85 }
86
87 Status status() override {
88 SkUNREACHABLE;
89 }
90
91 private:
92 const std::unique_ptr<SkBreakIterator> fA, fB;
93 Position fCurrent = 0;
94 bool fDone = false;
95 };
96
97 // An SkUnicode wrapper which offers stricter line break semantics (it never breaks words).
98 class StrictLinebreakUnicode final : public SkUnicode {
99 public:
100 explicit StrictLinebreakUnicode(sk_sp<SkUnicode> uc) : fUnicode(std::move(uc))
101 {
102 SkASSERT(fUnicode);
103 }
104
105 std::unique_ptr<SkBreakIterator> makeBreakIterator(const char locale[],
106 BreakType breakType) override {
107 std::unique_ptr<SkBreakIterator> brk = fUnicode->makeBreakIterator(locale, breakType);
108 if (!brk || breakType != BreakType::kLines) {
109 return brk;
110 }
111
112 // When requested a line break iterator, return a composite iterator which prevents
113 // breaking mid-word.
114 std::unique_ptr<SkBreakIterator> wbrk =
115 fUnicode->makeBreakIterator(locale, BreakType::kWords);
116
117 return wbrk
118 ? std::make_unique<IntersectingBreakIterator>(std::move(brk), std::move(wbrk))
119 : std::move(brk);
120 }
121
122 // Proxy everything else.
123 SkString toUpper(const SkString& str) override { return fUnicode->toUpper(str); }
124 SkString toUpper(const SkString& str, const char* locale) override {
125 return fUnicode->toUpper(str, locale);
126 }
127 bool isControl(SkUnichar utf8) override { return fUnicode->isControl(utf8); }
128 bool isWhitespace(SkUnichar utf8) override { return fUnicode->isWhitespace(utf8); }
129 bool isSpace(SkUnichar utf8) override { return fUnicode->isSpace(utf8); }
130 bool isTabulation(SkUnichar utf8) override { return fUnicode->isTabulation(utf8); }
131 bool isHardBreak(SkUnichar utf8) override { return fUnicode->isHardBreak(utf8); }
132 bool isEmoji(SkUnichar utf8) override { return fUnicode->isEmoji(utf8); }
133 bool isEmojiComponent(SkUnichar utf8) override { return fUnicode->isEmojiComponent(utf8); }
134 bool isEmojiModifierBase(SkUnichar utf8) override {
135 return fUnicode->isEmojiModifierBase(utf8);
136 }
137 bool isEmojiModifier(SkUnichar utf8) override { return fUnicode->isEmojiModifier(utf8); }
138 bool isRegionalIndicator(SkUnichar utf8) override {
139 return fUnicode->isRegionalIndicator(utf8);
140 }
141 bool isIdeographic(SkUnichar utf8) override { return fUnicode->isIdeographic(utf8); }
142 std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[],
143 int count,
144 SkBidiIterator::Direction dir) override {
145 return fUnicode->makeBidiIterator(text, count, dir);
146 }
147 std::unique_ptr<SkBidiIterator> makeBidiIterator(const char text[],
148 int count,
149 SkBidiIterator::Direction dir) override {
150 return fUnicode->makeBidiIterator(text, count, dir);
151 }
152 std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType breakType) override {
153 return fUnicode->makeBreakIterator(breakType);
154 }
155 bool getBidiRegions(const char utf8[],
156 int utf8Units,
157 TextDirection dir,
158 std::vector<BidiRegion>* results) override {
159 return fUnicode->getBidiRegions(utf8, utf8Units, dir, results);
160 }
161 bool getWords(const char utf8[], int utf8Units, const char* locale,
162 std::vector<Position>* results) override {
163 return fUnicode->getWords(utf8, utf8Units, locale, results);
164 }
165 bool getUtf8Words(const char utf8[],
166 int utf8Units,
167 const char* locale,
168 std::vector<Position>* results) override {
169 return fUnicode->getUtf8Words(utf8, utf8Units, locale, results);
170 }
171 bool getSentences(const char utf8[],
172 int utf8Units,
173 const char* locale,
174 std::vector<Position>* results) override {
175 return fUnicode->getSentences(utf8, utf8Units, locale, results);
176 }
177 bool computeCodeUnitFlags(char utf8[], int utf8Units, bool replaceTabs,
178 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) override {
179 return fUnicode->computeCodeUnitFlags(utf8, utf8Units, replaceTabs, results);
180 }
181 bool computeCodeUnitFlags(char16_t utf16[], int utf16Units, bool replaceTabs,
182 skia_private::TArray<SkUnicode::CodeUnitFlags, true>* results) override {
183 return fUnicode->computeCodeUnitFlags(utf16, utf16Units, replaceTabs, results);
184 }
185
186 void reorderVisual(const BidiLevel runLevels[],
187 int levelsCount,
188 int32_t logicalFromVisual[]) override {
189 return fUnicode->reorderVisual(runLevels, levelsCount, logicalFromVisual);
190 }
191
192 private:
193 const sk_sp<SkUnicode> fUnicode;
194 };
195
196 } // namespace
197
198 namespace skottie {
199
200 sk_sp<SkUnicode> SK_API MakeStrictLinebreakUnicode(sk_sp<SkUnicode> uc) {
201 return uc
202 ? sk_make_sp<StrictLinebreakUnicode>(std::move(uc))
203 : nullptr;
204 }
205
206 std::unique_ptr<SkBreakIterator> MakeIntersectingBreakIteratorForTesting(
207 std::unique_ptr<SkBreakIterator> a, std::unique_ptr<SkBreakIterator> b) {
208 return std::make_unique<IntersectingBreakIterator>(std::move(a), std::move(b));
209 }
210
211 } // namespace skottie