| 1 | // Copyright 2019 Google LLC. |
| 2 | |
| 3 | #include "include/core/SkString.h" |
| 4 | #include "include/core/SkTypes.h" |
| 5 | #include "include/private/SkTo.h" |
| 6 | #include "modules/skparagraph/src/ParagraphUtil.h" |
| 7 | #include "src/utils/SkUTF.h" |
| 8 | |
| 9 | #include <unicode/umachine.h> |
| 10 | #include <unicode/uchar.h> |
| 11 | #include <unicode/ustring.h> |
| 12 | #include <unicode/utypes.h> |
| 13 | #include <string> |
| 14 | |
| 15 | namespace skia { |
| 16 | namespace textlayout { |
| 17 | |
| 18 | SkString SkStringFromU16String(const std::u16string& utf16text) { |
| 19 | SkString dst; |
| 20 | UErrorCode status = U_ZERO_ERROR; |
| 21 | int32_t dstSize; |
| 22 | // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR |
| 23 | u_strToUTF8(nullptr, 0, &dstSize, (UChar*)utf16text.data(), SkToS32(utf16text.size()), &status); |
| 24 | dst.resize(dstSize); |
| 25 | status = U_ZERO_ERROR; |
| 26 | u_strToUTF8(dst.writable_str(), dst.size(), nullptr, |
| 27 | (UChar*)utf16text.data(), SkToS32(utf16text.size()), &status); |
| 28 | if (U_FAILURE(status)) { |
| 29 | SkDEBUGF("Invalid UTF-16 input: %s" , u_errorName(status)); |
| 30 | return dst; |
| 31 | } |
| 32 | return dst; |
| 33 | } |
| 34 | |
| 35 | SkUnichar nextUtf8Unit(const char** ptr, const char* end) { |
| 36 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 37 | return val < 0 ? 0xFFFD : val; |
| 38 | } |
| 39 | |
| 40 | bool isControl(SkUnichar utf8) { |
| 41 | return u_iscntrl(utf8); |
| 42 | } |
| 43 | |
| 44 | bool isWhitespace(SkUnichar utf8) { |
| 45 | return u_isWhitespace(utf8); |
| 46 | } |
| 47 | |
| 48 | } // namespace textlayout |
| 49 | } // namespace skia |
| 50 | |