• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google LLC
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 #ifndef SkUnicode_DEFINED
8 #define SkUnicode_DEFINED
9 #include "include/core/SkSpan.h"
10 #include "include/core/SkString.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/SkBitmaskEnum.h" // IWYU pragma: keep
13 #include "include/private/SkTArray.h"
14 #include "include/private/SkTo.h"
15 #include "src/utils/SkUTF.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #if !defined(SKUNICODE_IMPLEMENTATION)
24     #define SKUNICODE_IMPLEMENTATION 0
25 #endif
26 
27 #if !defined(SKUNICODE_API)
28     #if defined(SKUNICODE_DLL)
29         #if defined(_MSC_VER)
30             #if SKUNICODE_IMPLEMENTATION
31                 #define SKUNICODE_API __declspec(dllexport)
32             #else
33                 #define SKUNICODE_API __declspec(dllimport)
34             #endif
35         #else
36             #define SKUNICODE_API __attribute__((visibility("default")))
37         #endif
38     #else
39         #define SKUNICODE_API
40     #endif
41 #endif
42 
43 class SKUNICODE_API SkBidiIterator {
44 public:
45     typedef int32_t Position;
46     typedef uint8_t Level;
47     struct Region {
RegionRegion48         Region(Position start, Position end, Level level)
49             : start(start), end(end), level(level) { }
50         Position start;
51         Position end;
52         Level level;
53     };
54     enum Direction {
55         kLTR,
56         kRTL,
57     };
58     virtual ~SkBidiIterator() = default;
59     virtual Position getLength() = 0;
60     virtual Level getLevelAt(Position) = 0;
61 };
62 
63 class SKUNICODE_API SkBreakIterator {
64 public:
65     typedef int32_t Position;
66     typedef int32_t Status;
67     virtual ~SkBreakIterator() = default;
68     virtual Position first() = 0;
69     virtual Position current() = 0;
70     virtual Position next() = 0;
71     virtual Status status() = 0;
72     virtual bool isDone() = 0;
73     virtual bool setText(const char utftext8[], int utf8Units) = 0;
74     virtual bool setText(const char16_t utftext16[], int utf16Units) = 0;
75 };
76 
77 class SKUNICODE_API SkUnicode {
78     public:
79         enum CodeUnitFlags {
80             kNoCodeUnitFlag = 0x00,
81             kPartOfWhiteSpaceBreak = 0x01,
82             kGraphemeStart = 0x02,
83             kSoftLineBreakBefore = 0x04,
84             kHardLineBreakBefore = 0x08,
85             kPartOfIntraWordBreak = 0x10,
86             kControl = 0x20,
87             kTabulation = 0x40,
88             kGlyphClusterStart = 0x80,
89             kIdeographic = 0x100,
90 #ifdef OHOS_SUPPORT
91             kCombine = 0x200,
92             kPunctuation = 0x400,
93             kEllipsis = 0x800,
94 #endif
95         };
96         enum class TextDirection {
97             kLTR,
98             kRTL,
99         };
100         typedef size_t Position;
101         typedef uint8_t BidiLevel;
102         struct BidiRegion {
BidiRegionBidiRegion103             BidiRegion(Position start, Position end, BidiLevel level)
104               : start(start), end(end), level(level) { }
105             Position start;
106             Position end;
107             BidiLevel level;
108         };
109         enum class LineBreakType {
110             kSoftLineBreak = 0,
111             kHardLineBreak = 100,
112         };
113 
114         enum class BreakType {
115             kWords,
116             kGraphemes,
117             kLines
118         };
119         struct LineBreakBefore {
LineBreakBeforeLineBreakBefore120             LineBreakBefore(Position pos, LineBreakType breakType)
121               : pos(pos), breakType(breakType) { }
122             Position pos;
123             LineBreakType breakType;
124         };
125 
126         virtual ~SkUnicode() = default;
127 
128         virtual SkString toUpper(const SkString&) = 0;
129 
130         // Methods used in SkShaper and SkText
131         virtual std::unique_ptr<SkBidiIterator> makeBidiIterator
132             (const uint16_t text[], int count, SkBidiIterator::Direction) = 0;
133         virtual std::unique_ptr<SkBidiIterator> makeBidiIterator
134             (const char text[], int count, SkBidiIterator::Direction) = 0;
135         virtual std::unique_ptr<SkBreakIterator> makeBreakIterator
136             (const char locale[], BreakType breakType) = 0;
137         virtual std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType type) = 0;
138 
139         // Methods used in SkParagraph
140         static bool isTabulation(SkUnicode::CodeUnitFlags flags);
141         static bool isHardLineBreak(SkUnicode::CodeUnitFlags flags);
142         static bool isSoftLineBreak(SkUnicode::CodeUnitFlags flags);
143         static bool isGraphemeStart(SkUnicode::CodeUnitFlags flags);
144         static bool isControl(SkUnicode::CodeUnitFlags flags);
145         static bool isPartOfWhiteSpaceBreak(SkUnicode::CodeUnitFlags flags);
146         static bool isIdeographic(SkUnichar utf8);
147 #ifdef OHOS_SUPPORT
148         static bool isPunctuation(SkUnichar utf8);
149         static bool isEllipsis(SkUnichar utf8);
150 #endif
151         static bool extractBidi(const char utf8[],
152                                 int utf8Units,
153                                 TextDirection dir,
154                                 std::vector<BidiRegion>* bidiRegions);
155         virtual bool getBidiRegions(const char utf8[],
156                                     int utf8Units,
157                                     TextDirection dir,
158                                     std::vector<BidiRegion>* results) = 0;
159         virtual bool getWords(const char utf8[], int utf8Units, const char* locale,
160                               std::vector<Position>* results) = 0;
161         virtual bool computeCodeUnitFlags(
162                 char utf8[], int utf8Units, bool replaceTabs,
163                 SkTArray<SkUnicode::CodeUnitFlags, true>* results) = 0;
164         virtual bool computeCodeUnitFlags(
165                 char16_t utf16[], int utf16Units, bool replaceTabs,
166                 SkTArray<SkUnicode::CodeUnitFlags, true>* results) = 0;
167 
168         static SkString convertUtf16ToUtf8(const char16_t * utf16, int utf16Units);
169         static SkString convertUtf16ToUtf8(const std::u16string& utf16);
170         static std::u16string convertUtf8ToUtf16(const char* utf8, int utf8Units);
171         static std::u16string convertUtf8ToUtf16(const SkString& utf8);
172 
173         template <typename Appender8, typename Appender16>
extractUtfConversionMapping(SkSpan<const char> utf8,Appender8 && appender8,Appender16 && appender16)174         static bool extractUtfConversionMapping(SkSpan<const char> utf8, Appender8&& appender8, Appender16&& appender16) {
175             size_t size8 = 0;
176             size_t size16 = 0;
177             auto ptr = utf8.begin();
178             auto end = utf8.end();
179             while (ptr < end) {
180 
181                 size_t index = SkToSizeT(ptr - utf8.begin());
182                 SkUnichar u = SkUTF::NextUTF8(&ptr, end);
183 
184                 // All UTF8 code units refer to the same codepoint
185                 size_t next = SkToSizeT(ptr - utf8.begin());
186                 for (auto i = index; i < next; ++i) {
187                     //fUTF16IndexForUTF8Index.emplace_back(fUTF8IndexForUTF16Index.size());
188                     appender16(size8);
189                     ++size16;
190                 }
191                 //SkASSERT(fUTF16IndexForUTF8Index.size() == next);
192                 SkASSERT(size16 == next);
193                 if (size16 != next) {
194                     return false;
195                 }
196 
197                 // One or two UTF16 code units refer to the same codepoint
198                 uint16_t buffer[2];
199                 size_t count = SkUTF::ToUTF16(u, buffer);
200                 //fUTF8IndexForUTF16Index.emplace_back(index);
201                 appender8(index);
202                 ++size8;
203                 if (count > 1) {
204                     //fUTF8IndexForUTF16Index.emplace_back(index);
205                     appender8(index);
206                     ++size8;
207                 }
208             }
209             //fUTF16IndexForUTF8Index.emplace_back(fUTF8IndexForUTF16Index.size());
210             appender16(size8);
211             ++size16;
212             //fUTF8IndexForUTF16Index.emplace_back(fText.size());
213             appender8(utf8.size());
214             ++size8;
215 
216             return true;
217         }
218 
219         template <typename Callback>
forEachCodepoint(const char * utf8,int32_t utf8Units,Callback && callback)220         void forEachCodepoint(const char* utf8, int32_t utf8Units, Callback&& callback) {
221             const char* current = utf8;
222             const char* end = utf8 + utf8Units;
223             while (current < end) {
224                 auto before = current - utf8;
225                 SkUnichar unichar = SkUTF::NextUTF8(&current, end);
226                 if (unichar < 0) unichar = 0xFFFD;
227                 auto after = current - utf8;
228                 uint16_t buffer[2];
229                 size_t count = SkUTF::ToUTF16(unichar, buffer);
230                 callback(unichar, before, after, count);
231             }
232         }
233 
234         template <typename Callback>
forEachCodepoint(const char16_t * utf16,int32_t utf16Units,Callback && callback)235         void forEachCodepoint(const char16_t* utf16, int32_t utf16Units, Callback&& callback) {
236             const char16_t* current = utf16;
237             const char16_t* end = utf16 + utf16Units;
238             while (current < end) {
239                 auto before = current - utf16;
240                 SkUnichar unichar = SkUTF::NextUTF16((const uint16_t**)&current, (const uint16_t*)end);
241                 auto after = current - utf16;
242                 callback(unichar, before, after);
243             }
244         }
245 
246         template <typename Callback>
forEachBidiRegion(const uint16_t utf16[],int utf16Units,SkBidiIterator::Direction dir,Callback && callback)247         void forEachBidiRegion(const uint16_t utf16[], int utf16Units, SkBidiIterator::Direction dir, Callback&& callback) {
248             auto iter = makeBidiIterator(utf16, utf16Units, dir);
249             const uint16_t* start16 = utf16;
250             const uint16_t* end16 = utf16 + utf16Units;
251             SkBidiIterator::Level currentLevel = 0;
252 
253             SkBidiIterator::Position pos16 = 0;
254             while (pos16 <= iter->getLength()) {
255                 auto level = iter->getLevelAt(pos16);
256                 if (pos16 == 0) {
257                     currentLevel = level;
258                 } else if (level != currentLevel) {
259                     callback(pos16, start16 - utf16, currentLevel);
260                     currentLevel = level;
261                 }
262                 if (start16 == end16) {
263                     break;
264                 }
265                 SkUnichar u = SkUTF::NextUTF16(&start16, end16);
266                 pos16 += SkUTF::ToUTF16(u);
267             }
268         }
269 
270         template <typename Callback>
forEachBreak(const char16_t utf16[],int utf16Units,SkUnicode::BreakType type,Callback && callback)271         void forEachBreak(const char16_t utf16[], int utf16Units, SkUnicode::BreakType type, Callback&& callback) {
272             auto iter = makeBreakIterator(type);
273             iter->setText(utf16, utf16Units);
274             auto pos = iter->first();
275             do {
276                 callback(pos, iter->status());
277                 pos = iter->next();
278             } while (!iter->isDone());
279         }
280 
281         virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0;
282 
283         virtual std::unique_ptr<SkUnicode> copy() = 0;
284 
285         static std::unique_ptr<SkUnicode> Make();
286 
287         static std::unique_ptr<SkUnicode> MakeIcuBasedUnicode();
288 
289         static std::unique_ptr<SkUnicode> MakeClientBasedUnicode(
290                 SkSpan<char> text,
291                 std::vector<SkUnicode::Position> words,
292                 std::vector<SkUnicode::Position> graphemeBreaks,
293                 std::vector<SkUnicode::LineBreakBefore> lineBreaks);
294 };
295 
296 namespace sknonstd {
297     template <> struct is_bitmask_enum<SkUnicode::CodeUnitFlags> : std::true_type {};
298 }  // namespace sknonstd
299 #endif // SkUnicode_DEFINED
300