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