| 1 |
/* |
| 2 |
* Copyright 2022 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 |
#include "include/private/base/SkDebug.h" |
| 8 |
#include "modules/skunicode/include/SkUnicode.h" |
| 9 |
#include "src/base/SkBitmaskEnum.h" |
| 10 |
|
| 11 |
using namespace skia_private; |
| 12 |
|
| 13 |
SkString SkUnicode::convertUtf16ToUtf8(const char16_t* utf16Char, int utf16Units) { |
| 14 |
const uint16_t* utf16 = reinterpret_cast<const uint16_t*>(utf16Char); |
| 15 |
int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units); |
| 16 |
if (utf8Units < 0) { |
| 17 |
SkDEBUGF("Convert error: Invalid utf16 input"); |
| 18 |
return SkString(); |
| 19 |
} |
| 20 |
SkString s(utf8Units); |
| 21 |
SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(s.data(), utf8Units, utf16, utf16Units); |
| 22 |
SkASSERT(dstLen == utf8Units); |
| 23 |
return s; |
| 24 |
} |
| 25 |
|
| 26 |
SkString SkUnicode::convertUtf16ToUtf8(const std::u16string& utf16) { |
| 27 |
return convertUtf16ToUtf8(utf16.c_str(), utf16.size()); |
| 28 |
} |
| 29 |
|
| 30 |
std::u16string SkUnicode::convertUtf8ToUtf16(const char* utf8, int utf8Units) { |
| 31 |
int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units); |
| 32 |
if (utf16Units < 0) { |
| 33 |
SkDEBUGF("Convert error: Invalid utf8 input"); |
| 34 |
return std::u16string(); |
| 35 |
} |
| 36 |
std::u16string utf16Char(utf16Units, '\0'); |
| 37 |
uint16_t* utf16 = reinterpret_cast<uint16_t*>(utf16Char.data()); |
| 38 |
SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16, utf16Units, utf8, utf8Units); |
| 39 |
SkASSERT(dstLen == utf16Units); |
| 40 |
return utf16Char; |
| 41 |
} |
| 42 |
|
| 43 |
std::u16string SkUnicode::convertUtf8ToUtf16(const SkString& utf8) { |
| 44 |
return convertUtf8ToUtf16(utf8.c_str(), utf8.size()); |
| 45 |
} |
| 46 |
|
| 47 |
bool SkUnicode::hasTabulationFlag(SkUnicode::CodeUnitFlags flags) { |
| 48 |
return (flags & SkUnicode::kTabulation) == SkUnicode::kTabulation; |
| 49 |
} |
| 50 |
|
| 51 |
bool SkUnicode::hasHardLineBreakFlag(SkUnicode::CodeUnitFlags flags) { |
| 52 |
return (flags & SkUnicode::kHardLineBreakBefore) == SkUnicode::kHardLineBreakBefore; |
| 53 |
} |
| 54 |
|
| 55 |
bool SkUnicode::hasSoftLineBreakFlag(SkUnicode::CodeUnitFlags flags) { |
| 56 |
return (flags & SkUnicode::kSoftLineBreakBefore) == SkUnicode::kSoftLineBreakBefore; |
| 57 |
} |
| 58 |
|
| 59 |
bool SkUnicode::hasGraphemeStartFlag(SkUnicode::CodeUnitFlags flags) { |
| 60 |
return (flags & SkUnicode::kGraphemeStart) == SkUnicode::kGraphemeStart; |
| 61 |
} |
| 62 |
|
| 63 |
bool SkUnicode::hasControlFlag(SkUnicode::CodeUnitFlags flags) { |
| 64 |
return (flags & SkUnicode::kControl) == SkUnicode::kControl; |
| 65 |
} |
| 66 |
|
| 67 |
bool SkUnicode::hasPartOfWhiteSpaceBreakFlag(SkUnicode::CodeUnitFlags flags) { |
| 68 |
return (flags & SkUnicode::kPartOfWhiteSpaceBreak) == SkUnicode::kPartOfWhiteSpaceBreak; |
| 69 |
} |