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