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 #endif 93 }; 94 enum class TextDirection { 95 kLTR, 96 kRTL, 97 }; 98 typedef size_t Position; 99 typedef uint8_t BidiLevel; 100 struct BidiRegion { BidiRegionBidiRegion101 BidiRegion(Position start, Position end, BidiLevel level) 102 : start(start), end(end), level(level) { } 103 Position start; 104 Position end; 105 BidiLevel level; 106 }; 107 enum class LineBreakType { 108 kSoftLineBreak = 0, 109 kHardLineBreak = 100, 110 }; 111 112 enum class BreakType { 113 kWords, 114 kGraphemes, 115 kLines 116 }; 117 struct LineBreakBefore { LineBreakBeforeLineBreakBefore118 LineBreakBefore(Position pos, LineBreakType breakType) 119 : pos(pos), breakType(breakType) { } 120 Position pos; 121 LineBreakType breakType; 122 }; 123 124 virtual ~SkUnicode() = default; 125 126 virtual SkString toUpper(const SkString&) = 0; 127 128 // Methods used in SkShaper and SkText 129 virtual std::unique_ptr<SkBidiIterator> makeBidiIterator 130 (const uint16_t text[], int count, SkBidiIterator::Direction) = 0; 131 virtual std::unique_ptr<SkBidiIterator> makeBidiIterator 132 (const char text[], int count, SkBidiIterator::Direction) = 0; 133 virtual std::unique_ptr<SkBreakIterator> makeBreakIterator 134 (const char locale[], BreakType breakType) = 0; 135 virtual std::unique_ptr<SkBreakIterator> makeBreakIterator(BreakType type) = 0; 136 137 // Methods used in SkParagraph 138 static bool isTabulation(SkUnicode::CodeUnitFlags flags); 139 static bool isHardLineBreak(SkUnicode::CodeUnitFlags flags); 140 static bool isSoftLineBreak(SkUnicode::CodeUnitFlags flags); 141 static bool isGraphemeStart(SkUnicode::CodeUnitFlags flags); 142 static bool isControl(SkUnicode::CodeUnitFlags flags); 143 static bool isPartOfWhiteSpaceBreak(SkUnicode::CodeUnitFlags flags); 144 static bool isIdeographic(SkUnichar utf8); 145 static bool extractBidi(const char utf8[], 146 int utf8Units, 147 TextDirection dir, 148 std::vector<BidiRegion>* bidiRegions); 149 virtual bool getBidiRegions(const char utf8[], 150 int utf8Units, 151 TextDirection dir, 152 std::vector<BidiRegion>* results) = 0; 153 virtual bool getWords(const char utf8[], int utf8Units, const char* locale, 154 std::vector<Position>* results) = 0; 155 virtual bool computeCodeUnitFlags( 156 char utf8[], int utf8Units, bool replaceTabs, 157 SkTArray<SkUnicode::CodeUnitFlags, true>* results) = 0; 158 virtual bool computeCodeUnitFlags( 159 char16_t utf16[], int utf16Units, bool replaceTabs, 160 SkTArray<SkUnicode::CodeUnitFlags, true>* results) = 0; 161 162 static SkString convertUtf16ToUtf8(const char16_t * utf16, int utf16Units); 163 static SkString convertUtf16ToUtf8(const std::u16string& utf16); 164 static std::u16string convertUtf8ToUtf16(const char* utf8, int utf8Units); 165 static std::u16string convertUtf8ToUtf16(const SkString& utf8); 166 167 template <typename Appender8, typename Appender16> extractUtfConversionMapping(SkSpan<const char> utf8,Appender8 && appender8,Appender16 && appender16)168 static bool extractUtfConversionMapping(SkSpan<const char> utf8, Appender8&& appender8, Appender16&& appender16) { 169 size_t size8 = 0; 170 size_t size16 = 0; 171 auto ptr = utf8.begin(); 172 auto end = utf8.end(); 173 while (ptr < end) { 174 175 size_t index = SkToSizeT(ptr - utf8.begin()); 176 SkUnichar u = SkUTF::NextUTF8(&ptr, end); 177 178 // All UTF8 code units refer to the same codepoint 179 size_t next = SkToSizeT(ptr - utf8.begin()); 180 for (auto i = index; i < next; ++i) { 181 //fUTF16IndexForUTF8Index.emplace_back(fUTF8IndexForUTF16Index.size()); 182 appender16(size8); 183 ++size16; 184 } 185 //SkASSERT(fUTF16IndexForUTF8Index.size() == next); 186 SkASSERT(size16 == next); 187 if (size16 != next) { 188 return false; 189 } 190 191 // One or two UTF16 code units refer to the same codepoint 192 uint16_t buffer[2]; 193 size_t count = SkUTF::ToUTF16(u, buffer); 194 //fUTF8IndexForUTF16Index.emplace_back(index); 195 appender8(index); 196 ++size8; 197 if (count > 1) { 198 //fUTF8IndexForUTF16Index.emplace_back(index); 199 appender8(index); 200 ++size8; 201 } 202 } 203 //fUTF16IndexForUTF8Index.emplace_back(fUTF8IndexForUTF16Index.size()); 204 appender16(size8); 205 ++size16; 206 //fUTF8IndexForUTF16Index.emplace_back(fText.size()); 207 appender8(utf8.size()); 208 ++size8; 209 210 return true; 211 } 212 213 template <typename Callback> forEachCodepoint(const char * utf8,int32_t utf8Units,Callback && callback)214 void forEachCodepoint(const char* utf8, int32_t utf8Units, Callback&& callback) { 215 const char* current = utf8; 216 const char* end = utf8 + utf8Units; 217 while (current < end) { 218 auto before = current - utf8; 219 SkUnichar unichar = SkUTF::NextUTF8(¤t, end); 220 if (unichar < 0) unichar = 0xFFFD; 221 auto after = current - utf8; 222 uint16_t buffer[2]; 223 size_t count = SkUTF::ToUTF16(unichar, buffer); 224 callback(unichar, before, after, count); 225 } 226 } 227 228 template <typename Callback> forEachCodepoint(const char16_t * utf16,int32_t utf16Units,Callback && callback)229 void forEachCodepoint(const char16_t* utf16, int32_t utf16Units, Callback&& callback) { 230 const char16_t* current = utf16; 231 const char16_t* end = utf16 + utf16Units; 232 while (current < end) { 233 auto before = current - utf16; 234 SkUnichar unichar = SkUTF::NextUTF16((const uint16_t**)¤t, (const uint16_t*)end); 235 auto after = current - utf16; 236 callback(unichar, before, after); 237 } 238 } 239 240 template <typename Callback> forEachBidiRegion(const uint16_t utf16[],int utf16Units,SkBidiIterator::Direction dir,Callback && callback)241 void forEachBidiRegion(const uint16_t utf16[], int utf16Units, SkBidiIterator::Direction dir, Callback&& callback) { 242 auto iter = makeBidiIterator(utf16, utf16Units, dir); 243 const uint16_t* start16 = utf16; 244 const uint16_t* end16 = utf16 + utf16Units; 245 SkBidiIterator::Level currentLevel = 0; 246 247 SkBidiIterator::Position pos16 = 0; 248 while (pos16 <= iter->getLength()) { 249 auto level = iter->getLevelAt(pos16); 250 if (pos16 == 0) { 251 currentLevel = level; 252 } else if (level != currentLevel) { 253 callback(pos16, start16 - utf16, currentLevel); 254 currentLevel = level; 255 } 256 if (start16 == end16) { 257 break; 258 } 259 SkUnichar u = SkUTF::NextUTF16(&start16, end16); 260 pos16 += SkUTF::ToUTF16(u); 261 } 262 } 263 264 template <typename Callback> forEachBreak(const char16_t utf16[],int utf16Units,SkUnicode::BreakType type,Callback && callback)265 void forEachBreak(const char16_t utf16[], int utf16Units, SkUnicode::BreakType type, Callback&& callback) { 266 auto iter = makeBreakIterator(type); 267 iter->setText(utf16, utf16Units); 268 auto pos = iter->first(); 269 do { 270 callback(pos, iter->status()); 271 pos = iter->next(); 272 } while (!iter->isDone()); 273 } 274 275 virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0; 276 277 virtual std::unique_ptr<SkUnicode> copy() = 0; 278 279 static std::unique_ptr<SkUnicode> Make(); 280 281 static std::unique_ptr<SkUnicode> MakeIcuBasedUnicode(); 282 283 static std::unique_ptr<SkUnicode> MakeClientBasedUnicode( 284 SkSpan<char> text, 285 std::vector<SkUnicode::Position> words, 286 std::vector<SkUnicode::Position> graphemeBreaks, 287 std::vector<SkUnicode::LineBreakBefore> lineBreaks); 288 }; 289 290 namespace sknonstd { 291 template <> struct is_bitmask_enum<SkUnicode::CodeUnitFlags> : std::true_type {}; 292 } // namespace sknonstd 293 #endif // SkUnicode_DEFINED 294