From 3898f2c648b1a731dead8337aad8912d2b8b80d7 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 2 Nov 2021 21:52:09 +0800 Subject: *misc --- Runtime/GUI/TextMesh.cpp | 2 + Runtime/GUI/TextMesh.h | 2 - Runtime/GUI/UIQuad.cpp | 63 +++++++ Runtime/GUI/UIQuad.h | 21 +++ Runtime/GUI/utf8.cpp | 415 ++++++++++++++++++++++++++++++++++++++++++++ Runtime/GUI/utf8_decode.cpp | 415 -------------------------------------------- 6 files changed, 501 insertions(+), 417 deletions(-) create mode 100644 Runtime/GUI/UIQuad.cpp create mode 100644 Runtime/GUI/UIQuad.h create mode 100644 Runtime/GUI/utf8.cpp delete mode 100644 Runtime/GUI/utf8_decode.cpp (limited to 'Runtime/GUI') diff --git a/Runtime/GUI/TextMesh.cpp b/Runtime/GUI/TextMesh.cpp index de9a195..2f66170 100644 --- a/Runtime/GUI/TextMesh.cpp +++ b/Runtime/GUI/TextMesh.cpp @@ -50,6 +50,8 @@ InitializeStaticVariables([]() { s_SizePerText = sizeof(TextMeshVBOLayout) * 4; }); +// 一段文字里面的网格可能会来自不同的atlas,在生成TextMesh时做好合批 + TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment) { m_Font = font; diff --git a/Runtime/GUI/TextMesh.h b/Runtime/GUI/TextMesh.h index c091a78..c5f05a2 100644 --- a/Runtime/GUI/TextMesh.h +++ b/Runtime/GUI/TextMesh.h @@ -34,8 +34,6 @@ namespace TextHelper TextMeshHash GetTextMeshHash(); } -// 一段文字里面的网格可能会来自不同的atlas,在生成TextMesh时做好合批 - class TextMesh { public: diff --git a/Runtime/GUI/UIQuad.cpp b/Runtime/GUI/UIQuad.cpp new file mode 100644 index 0000000..089d0e1 --- /dev/null +++ b/Runtime/GUI/UIQuad.cpp @@ -0,0 +1,63 @@ +#include "../Math/Vector2.h" +#include "../Graphics/GfxDevice.h" +#include "UIQuad.h" + +struct UIQuadLayout +{ + Internal::Vector2 position; + Internal::Vector2 uv; +}; + +static CustomVertexLayout layout; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UIQuadLayout)); + VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector2), 2, VertexAttrFormat_Float, sizeof(UIQuadLayout)); + + layout.attributes.push_back(POSITION); + layout.attributes.push_back(UV); +}); + +void UIQuad::Draw() +{ + const int nVerts = 4; + const int nIndices = 6; + + float pos[] = { + m_Left, m_Bottom, // left-bottom + m_Right, m_Bottom, // right-bottom + m_Right, m_Top, // right-top + m_Left, m_Top, // top-left + }; + + float uv[] = { + 0, 0, + 1, 0, + 1, 1, + 0, 1, + }; + + int indices[] = { + 0, 1, 3, // right-top + 1, 2, 3, // left-bottom + }; + + uint8* vb; + uint16* ib; + + g_SharedVBO.GetChunk(sizeof(UIQuadLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); + + UIQuadLayout* dst = (UIQuadLayout*)vb; + + for (int i = 0; i < nVerts; ++i) + { + dst[i].position.Set(pos[2 * i], pos[2 * i + 1]); + dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]); + } + + for (int i = 0; i < nIndices; ++i) + ib[i] = indices[i]; + + g_SharedVBO.ReleaseChunk(4, 6); + g_SharedVBO.DrawChunk(layout); +} \ No newline at end of file diff --git a/Runtime/GUI/UIQuad.h b/Runtime/GUI/UIQuad.h new file mode 100644 index 0000000..bcd95a0 --- /dev/null +++ b/Runtime/GUI/UIQuad.h @@ -0,0 +1,21 @@ +#pragma once +#include "../Rendering/DynamicMesh.h" +#include "../Utilities/StaticInitiator.h" + +class UIQuad : public DynamicMesh +{ +public : + UIQuad(float l, float r, float t, float b) + { + m_Left = l; + m_Right = r; + m_Top = t; + m_Bottom = b; + } + + void Draw() override; + +private: + float m_Left, m_Right, m_Top, m_Bottom; + +}; diff --git a/Runtime/GUI/utf8.cpp b/Runtime/GUI/utf8.cpp new file mode 100644 index 0000000..8a3a086 --- /dev/null +++ b/Runtime/GUI/utf8.cpp @@ -0,0 +1,415 @@ +/* + * Description: UTF-8 瀛楃涓茬殑瑙g爜鍜岀紪鐮佸嚱鏁 + * unicode 瀛楃澶勭悊鍑芥暟 + * History: yang@haipo.me, 2013/05/29, create + * + * This code is in the public domain. + * You may use this code any way you wish, private, educational, + * or commercial. It's free. + */ + +# include +# include + +# include "utf8.h" + +namespace utf8 +{ + + ucs4_t getu8c(char **src, int *illegal) + { + static char umap[256] = { 0 }; + static int umap_init_flag = 0; + + if (umap_init_flag == 0) + { + int i; + + for (i = 0; i < 0x100; ++i) + { + if (i < 0x80) + { + umap[i] = 1; + } + else if (i >= 0xc0 && i < 0xe0) + { + umap[i] = 2; + } + else if (i >= 0xe0 && i < 0xf0) + { + umap[i] = 3; + } + else if (i >= 0xf0 && i < 0xf8) + { + umap[i] = 4; + } + else if (i >= 0xf8 && i < 0xfc) + { + umap[i] = 5; + } + else if (i >= 0xfc && i < 0xfe) + { + umap[i] = 6; + } + else + { + umap[i] = 0; + } + } + + umap_init_flag = 1; + } + + uint8_t *s = (uint8_t *)(*src); + int r_illegal = 0; + + while (umap[*s] == 0) + { + ++s; + ++r_illegal; + } + + uint8_t *t; + int byte_num; + uint32_t uc; + int i; + + repeat_label: + t = s; + byte_num = umap[*s]; + uc = *s++ & (0xff >> byte_num); + + for (i = 1; i < byte_num; ++i) + { + if (umap[*s]) + { + r_illegal += s - t; + goto repeat_label; + } + else + { + uc = (uc << 6) + (*s & 0x3f); + s += 1; + } + } + + *src = (char *)s; + if (illegal) + { + *illegal = r_illegal; + } + + return uc; + } + + size_t u8decode(char const *str, ucs4_t *des, size_t n, int *illegal) + { + if (n == 0) + return 0; + + char *s = (char *)str; + size_t i = 0; + ucs4_t uc = 0; + int r_illegal_all = 0, r_illegal; + + while ((uc = getu8c(&s, &r_illegal))) + { + if (i < (n - 1)) + { + des[i++] = uc; + r_illegal_all += r_illegal; + } + else + { + break; + } + } + + des[i] = 0; + if (illegal) + { + *illegal = r_illegal_all + r_illegal; + } + + return i; + } + +# define IF_CAN_HOLD(left, n) do { \ + size_t m = (size_t)(n); \ + if ((size_t)(left) < (m + 1)) return -2; \ + (left) -= m; \ +} while (0) + + int putu8c(ucs4_t uc, char **des, size_t *left) + { + if (uc < 0) + return -1; + + if (uc < (0x1 << 7)) + { + IF_CAN_HOLD(*left, 1); + + **des = (char)uc; + *des += 1; + **des = 0; + + return 1; + } + + int byte_num; + + if (uc < (0x1 << 11)) + { + byte_num = 2; + } + else if (uc < (0x1 << 16)) + { + byte_num = 3; + } + else if (uc < (0x1 << 21)) + { + byte_num = 4; + } + else if (uc < (0x1 << 26)) + { + byte_num = 5; + } + else + { + byte_num = 6; + } + + IF_CAN_HOLD(*left, byte_num); + + int i; + for (i = byte_num - 1; i > 0; --i) + { + *(uint8_t *)(*des + i) = (uc & 0x3f) | 0x80; + uc >>= 6; + } + + *(uint8_t *)(*des) = uc | (0xff << (8 - byte_num)); + + *des += byte_num; + **des = 0; + + return byte_num; + } + + size_t u8encode(ucs4_t *us, char *des, size_t n, int *illegal) + { + if (n == 0) + return 0; + + char *s = des; + size_t left = n; + size_t len = 0; + int r_illegal = 0; + + *s = 0; + while (*us) + { + int ret = putu8c(*us, &s, &left); + if (ret > 0) + { + len += ret; + } + else if (ret == -1) + { + r_illegal += 1; + } + else + { + break; + } + + ++us; + } + + if (illegal) + { + *illegal = r_illegal; + } + + return len; + } + + /* 鍏ㄨ瀛楃 */ + int isufullwidth(ucs4_t uc) + { + if (uc == 0x3000) + return 1; + + if (uc >= 0xff01 && uc <= 0xff5e) + return 1; + + return 0; + } + + /* 鍏ㄨ瀛楁瘝 */ + int isufullwidthalpha(ucs4_t uc) + { + if (uc >= 0xff21 && uc <= 0xff3a) + return 1; + + if (uc >= 0xff41 && uc <= 0xff5a) + return 2; + + return 0; + } + + /* 鍏ㄨ鏁板瓧 */ + int isufullwidthdigit(ucs4_t uc) + { + if (uc >= 0xff10 && uc <= 0xff19) + return 1; + + return 0; + } + + /* 鍏ㄨ杞崐瑙 */ + ucs4_t ufull2half(ucs4_t uc) + { + if (uc == 0x3000) + return ' '; + + if (uc >= 0xff01 && uc <= 0xff5e) + return uc - 0xfee0; + + return uc; + } + + /* 鍗婅杞叏瑙 */ + ucs4_t uhalf2full(ucs4_t uc) + { + if (uc == ' ') + return 0x3000; + + if (uc >= 0x21 && uc <= 0x7e) + return uc + 0xfee0; + + return uc; + } + + /* 涓棩闊╄秺缁熶竴琛ㄦ剰鏂囧瓧 */ + int isuchiness(ucs4_t uc) + { + /* 鏈鍒濇湡缁熶竴姹夊瓧 */ + if (uc >= 0x4e00 && uc <= 0x9fcc) + return 1; + + /* 鎵╁睍 A 鍖 */ + if (uc >= 0x3400 && uc <= 0x4db5) + return 2; + + /* 鎵╁睍 B 鍖 */ + if (uc >= 0x20000 && uc <= 0x2a6d6) + return 3; + + /* 鎵╁睍 C 鍖 */ + if (uc >= 0x2a700 && uc <= 0x2b734) + return 4; + + /* 鎵╁睍 D 鍖 */ + if (uc >= 0x2b740 && uc <= 0x2b81f) + return 5; + + /* 鎵╁睍 E 鍖 */ + if (uc >= 0x2b820 && uc <= 0x2f7ff) + return 6; + + /* 鍙版咕鍏煎姹夊瓧 */ + if (uc >= 0x2f800 && uc <= 0x2fa1d) + return 7; + + /* 鍖楁湞椴滃吋瀹规眽瀛 */ + if (uc >= 0xfa70 && uc <= 0xfad9) + return 8; + + /* 鍏煎姹夊瓧 */ + if (uc >= 0xf900 && uc <= 0xfa2d) + return 9; + + /* 鍏煎姹夊瓧 */ + if (uc >= 0xfa30 && uc <= 0xfa6d) + return 10; + + return 0; + } + + /* 涓枃鏍囩偣 */ + int isuzhpunct(ucs4_t uc) + { + if (uc >= 0x3001 && uc <= 0x3002) + return 1; + + if (uc >= 0x3008 && uc <= 0x300f) + return 1; + + if (uc >= 0xff01 && uc <= 0xff0f) + return 1; + + if (uc >= 0xff1a && uc <= 0xff20) + return 1; + + if (uc >= 0xff3b && uc <= 0xff40) + return 1; + + if (uc >= 0xff5b && uc <= 0xff5e) + return 1; + + if (uc >= 0x2012 && uc <= 0x201f) + return 1; + + if (uc >= 0xfe41 && uc <= 0xfe44) + return 1; + + if (uc >= 0xfe49 && uc <= 0xfe4f) + return 1; + + if (uc >= 0x3010 && uc <= 0x3017) + return 1; + + return 0; + } + + /* 鏃ユ枃骞冲亣鍚 */ + int isuhiragana(ucs4_t uc) + { + if (uc >= 0x3040 && uc <= 0x309f) + return 1; + + return 0; + } + + /* 鏃ユ枃鐗囧亣鍚 */ + int isukatakana(ucs4_t uc) + { + if (uc >= 0x30a0 && uc <= 0x30ff) + return 1; + + if (uc >= 0x31f0 && uc <= 0x31ff) + return 2; + + return 0; + } + + /* 闊╂枃 */ + int isukorean(ucs4_t uc) + { + /* 闊╂枃鎷奸煶 */ + if (uc >= 0xac00 && uc <= 0xd7af) + return 1; + + /* 闊╂枃瀛楁瘝 */ + if (uc >= 0x1100 && uc <= 0x11ff) + return 2; + + /* 闊╂枃鍏煎瀛楁瘝 */ + if (uc >= 0x3130 && uc <= 0x318f) + return 3; + + return 0; + } + +} diff --git a/Runtime/GUI/utf8_decode.cpp b/Runtime/GUI/utf8_decode.cpp deleted file mode 100644 index 8a3a086..0000000 --- a/Runtime/GUI/utf8_decode.cpp +++ /dev/null @@ -1,415 +0,0 @@ -/* - * Description: UTF-8 瀛楃涓茬殑瑙g爜鍜岀紪鐮佸嚱鏁 - * unicode 瀛楃澶勭悊鍑芥暟 - * History: yang@haipo.me, 2013/05/29, create - * - * This code is in the public domain. - * You may use this code any way you wish, private, educational, - * or commercial. It's free. - */ - -# include -# include - -# include "utf8.h" - -namespace utf8 -{ - - ucs4_t getu8c(char **src, int *illegal) - { - static char umap[256] = { 0 }; - static int umap_init_flag = 0; - - if (umap_init_flag == 0) - { - int i; - - for (i = 0; i < 0x100; ++i) - { - if (i < 0x80) - { - umap[i] = 1; - } - else if (i >= 0xc0 && i < 0xe0) - { - umap[i] = 2; - } - else if (i >= 0xe0 && i < 0xf0) - { - umap[i] = 3; - } - else if (i >= 0xf0 && i < 0xf8) - { - umap[i] = 4; - } - else if (i >= 0xf8 && i < 0xfc) - { - umap[i] = 5; - } - else if (i >= 0xfc && i < 0xfe) - { - umap[i] = 6; - } - else - { - umap[i] = 0; - } - } - - umap_init_flag = 1; - } - - uint8_t *s = (uint8_t *)(*src); - int r_illegal = 0; - - while (umap[*s] == 0) - { - ++s; - ++r_illegal; - } - - uint8_t *t; - int byte_num; - uint32_t uc; - int i; - - repeat_label: - t = s; - byte_num = umap[*s]; - uc = *s++ & (0xff >> byte_num); - - for (i = 1; i < byte_num; ++i) - { - if (umap[*s]) - { - r_illegal += s - t; - goto repeat_label; - } - else - { - uc = (uc << 6) + (*s & 0x3f); - s += 1; - } - } - - *src = (char *)s; - if (illegal) - { - *illegal = r_illegal; - } - - return uc; - } - - size_t u8decode(char const *str, ucs4_t *des, size_t n, int *illegal) - { - if (n == 0) - return 0; - - char *s = (char *)str; - size_t i = 0; - ucs4_t uc = 0; - int r_illegal_all = 0, r_illegal; - - while ((uc = getu8c(&s, &r_illegal))) - { - if (i < (n - 1)) - { - des[i++] = uc; - r_illegal_all += r_illegal; - } - else - { - break; - } - } - - des[i] = 0; - if (illegal) - { - *illegal = r_illegal_all + r_illegal; - } - - return i; - } - -# define IF_CAN_HOLD(left, n) do { \ - size_t m = (size_t)(n); \ - if ((size_t)(left) < (m + 1)) return -2; \ - (left) -= m; \ -} while (0) - - int putu8c(ucs4_t uc, char **des, size_t *left) - { - if (uc < 0) - return -1; - - if (uc < (0x1 << 7)) - { - IF_CAN_HOLD(*left, 1); - - **des = (char)uc; - *des += 1; - **des = 0; - - return 1; - } - - int byte_num; - - if (uc < (0x1 << 11)) - { - byte_num = 2; - } - else if (uc < (0x1 << 16)) - { - byte_num = 3; - } - else if (uc < (0x1 << 21)) - { - byte_num = 4; - } - else if (uc < (0x1 << 26)) - { - byte_num = 5; - } - else - { - byte_num = 6; - } - - IF_CAN_HOLD(*left, byte_num); - - int i; - for (i = byte_num - 1; i > 0; --i) - { - *(uint8_t *)(*des + i) = (uc & 0x3f) | 0x80; - uc >>= 6; - } - - *(uint8_t *)(*des) = uc | (0xff << (8 - byte_num)); - - *des += byte_num; - **des = 0; - - return byte_num; - } - - size_t u8encode(ucs4_t *us, char *des, size_t n, int *illegal) - { - if (n == 0) - return 0; - - char *s = des; - size_t left = n; - size_t len = 0; - int r_illegal = 0; - - *s = 0; - while (*us) - { - int ret = putu8c(*us, &s, &left); - if (ret > 0) - { - len += ret; - } - else if (ret == -1) - { - r_illegal += 1; - } - else - { - break; - } - - ++us; - } - - if (illegal) - { - *illegal = r_illegal; - } - - return len; - } - - /* 鍏ㄨ瀛楃 */ - int isufullwidth(ucs4_t uc) - { - if (uc == 0x3000) - return 1; - - if (uc >= 0xff01 && uc <= 0xff5e) - return 1; - - return 0; - } - - /* 鍏ㄨ瀛楁瘝 */ - int isufullwidthalpha(ucs4_t uc) - { - if (uc >= 0xff21 && uc <= 0xff3a) - return 1; - - if (uc >= 0xff41 && uc <= 0xff5a) - return 2; - - return 0; - } - - /* 鍏ㄨ鏁板瓧 */ - int isufullwidthdigit(ucs4_t uc) - { - if (uc >= 0xff10 && uc <= 0xff19) - return 1; - - return 0; - } - - /* 鍏ㄨ杞崐瑙 */ - ucs4_t ufull2half(ucs4_t uc) - { - if (uc == 0x3000) - return ' '; - - if (uc >= 0xff01 && uc <= 0xff5e) - return uc - 0xfee0; - - return uc; - } - - /* 鍗婅杞叏瑙 */ - ucs4_t uhalf2full(ucs4_t uc) - { - if (uc == ' ') - return 0x3000; - - if (uc >= 0x21 && uc <= 0x7e) - return uc + 0xfee0; - - return uc; - } - - /* 涓棩闊╄秺缁熶竴琛ㄦ剰鏂囧瓧 */ - int isuchiness(ucs4_t uc) - { - /* 鏈鍒濇湡缁熶竴姹夊瓧 */ - if (uc >= 0x4e00 && uc <= 0x9fcc) - return 1; - - /* 鎵╁睍 A 鍖 */ - if (uc >= 0x3400 && uc <= 0x4db5) - return 2; - - /* 鎵╁睍 B 鍖 */ - if (uc >= 0x20000 && uc <= 0x2a6d6) - return 3; - - /* 鎵╁睍 C 鍖 */ - if (uc >= 0x2a700 && uc <= 0x2b734) - return 4; - - /* 鎵╁睍 D 鍖 */ - if (uc >= 0x2b740 && uc <= 0x2b81f) - return 5; - - /* 鎵╁睍 E 鍖 */ - if (uc >= 0x2b820 && uc <= 0x2f7ff) - return 6; - - /* 鍙版咕鍏煎姹夊瓧 */ - if (uc >= 0x2f800 && uc <= 0x2fa1d) - return 7; - - /* 鍖楁湞椴滃吋瀹规眽瀛 */ - if (uc >= 0xfa70 && uc <= 0xfad9) - return 8; - - /* 鍏煎姹夊瓧 */ - if (uc >= 0xf900 && uc <= 0xfa2d) - return 9; - - /* 鍏煎姹夊瓧 */ - if (uc >= 0xfa30 && uc <= 0xfa6d) - return 10; - - return 0; - } - - /* 涓枃鏍囩偣 */ - int isuzhpunct(ucs4_t uc) - { - if (uc >= 0x3001 && uc <= 0x3002) - return 1; - - if (uc >= 0x3008 && uc <= 0x300f) - return 1; - - if (uc >= 0xff01 && uc <= 0xff0f) - return 1; - - if (uc >= 0xff1a && uc <= 0xff20) - return 1; - - if (uc >= 0xff3b && uc <= 0xff40) - return 1; - - if (uc >= 0xff5b && uc <= 0xff5e) - return 1; - - if (uc >= 0x2012 && uc <= 0x201f) - return 1; - - if (uc >= 0xfe41 && uc <= 0xfe44) - return 1; - - if (uc >= 0xfe49 && uc <= 0xfe4f) - return 1; - - if (uc >= 0x3010 && uc <= 0x3017) - return 1; - - return 0; - } - - /* 鏃ユ枃骞冲亣鍚 */ - int isuhiragana(ucs4_t uc) - { - if (uc >= 0x3040 && uc <= 0x309f) - return 1; - - return 0; - } - - /* 鏃ユ枃鐗囧亣鍚 */ - int isukatakana(ucs4_t uc) - { - if (uc >= 0x30a0 && uc <= 0x30ff) - return 1; - - if (uc >= 0x31f0 && uc <= 0x31ff) - return 2; - - return 0; - } - - /* 闊╂枃 */ - int isukorean(ucs4_t uc) - { - /* 闊╂枃鎷奸煶 */ - if (uc >= 0xac00 && uc <= 0xd7af) - return 1; - - /* 闊╂枃瀛楁瘝 */ - if (uc >= 0x1100 && uc <= 0x11ff) - return 2; - - /* 闊╂枃鍏煎瀛楁瘝 */ - if (uc >= 0x3130 && uc <= 0x318f) - return 3; - - return 0; - } - -} -- cgit v1.1-26-g67d0