Last active
June 7, 2023 12:59
-
-
Save HalCanary/0e13bb472695a6312c0aaec54bee382c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SkFontTypes.h" | |
#include <cstddef> | |
class SkFont; | |
class SkTextBlob; | |
template<typename T> class sk_sp; | |
sk_sp<SkTextBlob> SkShapeText(const void* text, | |
size_t byteLength, | |
const SkFont& font, | |
SkTextEncoding encoding = kUTF8_SkTextEncoding); | |
////////////////////////////////////////////////////////////////// | |
#include "SkUTF.h" | |
#include "SkShaper.h" | |
template <typename T, SkUnichar (*FN)(const T**, const T*)> | |
static SkString convert_to_utf8(const void* src, size_t srcBytes) { | |
const T* ptr = (const T*)src; | |
const T* stop = (const T*)((const char*)src + srcBytes); | |
size_t count = 0; | |
while (ptr < stop) { | |
count += SkUTF::ToUTF8(FN(&ptr, stop), nullptr); | |
} | |
SkString result(count); | |
ptr = (const T*)src; | |
char* dst = result.writable_str(); | |
while (ptr < stop) { | |
dst += SkUTF::ToUTF8(FN(&ptr, stop), dst); | |
} | |
return result; | |
} | |
sk_sp<SkTextBlob> SkShapeText(const void* text, | |
size_t byteLength, | |
const SkFont& font, | |
SkTextEncoding encoding) { | |
SkString tmp; | |
switch (encoding) { | |
case kUTF8_SkTextEncoding: | |
break; | |
case SkTextEncoding::kUTF16: | |
tmp = convert_to_utf8<uint16_t, SkUTF::NextUTF16>(text, byteLength); | |
text = tmp.c_str(); byteLength = tmp.size(); | |
break; | |
case SkTextEncoding::kUTF32: | |
tmp = convert_to_utf8<int32_t, SkUTF::NextUTF32>(text, byteLength); | |
text = tmp.c_str(); byteLength = tmp.size(); | |
break; | |
default: | |
return nullptr; | |
} | |
SkTextBlobBuilderRunHandler handler; | |
SkShaper shaper(font.refTypeface()); | |
shaper.shape(&handler, font, (const char*)text, byteLength, | |
true, {0, 0}, FLT_MAX); | |
return handler.makeBlob(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment