Skip to content

Commit

Permalink
v142
Browse files Browse the repository at this point in the history
  • Loading branch information
inanevin committed May 14, 2023
1 parent 2b9d741 commit 649e8e5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 79 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ add_library(Lina::VG ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_MAJOR=1)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_MINOR=4)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_PATCH=1)
target_compile_definitions(${PROJECT_NAME} PUBLIC LINAVG_VERSION_PATCH=2)

#--------------------------------------------------------------------
# Subdirectories & linking
Expand Down
12 changes: 6 additions & 6 deletions Examples/include/Backends/GL/GLBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ namespace LinaVG::Backend
ShaderData m_sdfTextShaderData;
ShaderData m_simpleTextShaderData;
float m_proj[4][4] = {0};
char* m_defaultVtxShader = nullptr;
char* m_defaultFragShader = nullptr;
char* m_roundedGradientFragShader = nullptr;
char* m_texturedFragShader = nullptr;
char* m_sdfTextFragShader = nullptr;
char* m_simpleTextFragShader = nullptr;
const char* m_defaultVtxShader = nullptr;
const char* m_defaultFragShader = nullptr;
const char* m_roundedGradientFragShader = nullptr;
const char* m_texturedFragShader = nullptr;
const char* m_sdfTextFragShader = nullptr;
const char* m_simpleTextFragShader = nullptr;
bool m_skipDraw = false;
};

Expand Down
2 changes: 1 addition & 1 deletion include/Core/Drawer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ namespace LinaVG
/// <summary>
/// For processing UTf8 texts.
/// </summary>
uint32_t GetNextUnicodeChar(const char* text, uint32_t& byteCount);
std::vector<int32_t> GetUtf8Codepoints(const char* str);
#endif

}; // namespace Internal
Expand Down
8 changes: 4 additions & 4 deletions include/Core/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ namespace LinaVG
/// <returns></returns>
static Vec2 GetPolygonCentroidFast(Vec2* points, int size);

static bool Math::IsEqual(const Vec2& v1, const Vec2& v2);
static bool Math::IsEqualMarg(const Vec2& v1, const Vec2& v2, float err = 0.001f);
static bool Math::IsEqualMarg(float f1, float f2, float err = 0.001f);
static bool Math::IsEqual(const Vec4& v1, const Vec4& v2);
static bool IsEqual(const Vec2& v1, const Vec2& v2);
static bool IsEqualMarg(const Vec2& v1, const Vec2& v2, float err = 0.001f);
static bool IsEqualMarg(float f1, float f2, float err = 0.001f);
static bool IsEqual(const Vec4& v1, const Vec4& v2);
static float Abs(float f);
static float Clamp(float f, float min, float max);
static int Clamp(int i, int min, int max);
Expand Down
110 changes: 43 additions & 67 deletions src/Core/Drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3176,50 +3176,6 @@ namespace LinaVG
return offset;
}

uint32_t GetNextUnicodeChar(const char* p, uint32_t& byteCount)
{
uint32_t codepoint = 0;
if ((*p & 0b10000000) == 0b00000000)
{
// 1-byte character (ASCII)
auto ch = static_cast<unsigned char>(*p);
byteCount = 1;
codepoint = static_cast<uint32_t>(ch);
}
else if ((*p & 0b11100000) == 0b11000000)
{
// 2-byte character
codepoint = ((*p & 0b00011111) << 6) |
(*(p + 1) & 0b00111111);
byteCount = 2;
}
else if ((*p & 0b11110000) == 0b11100000)
{
// 3-byte character
codepoint = ((*p & 0b00001111) << 12) |
((*(p + 1) & 0b00111111) << 6) |
(*(p + 2) & 0b00111111);
byteCount = 3;
}
else if ((*p & 0b11111000) == 0b11110000)
{
// 4-byte character
codepoint = ((*p & 0b00000111) << 18) |
((*(p + 1) & 0b00111111) << 12) |
((*(p + 2) & 0b00111111) << 6) |
(*(p + 3) & 0b00111111);
byteCount = 4;
}
else
{
// Invalid UTF-8 sequence
if (Config.errorCallback)
Config.errorCallback("LinaVG -> Invalid UTF-8 sequence!");
}

return codepoint;
}

void DrawText(DrawBuffer* buf, LinaVGFont* font, const char* text, const Vec2& position, const Vec2& offset, const Vec4Grad& color, float spacing, bool isGradient, float scale)
{
const uint8_t* c;
Expand Down Expand Up @@ -3314,19 +3270,12 @@ namespace LinaVG

if (font->m_supportsUnicode)
{
const uint8_t* c = (const uint8_t*)text;
for (const char* p = text; *p; p++)
{
uint32_t byteCount = 0;
uint32_t character = GetNextUnicodeChar(p, byteCount);

if (byteCount == 0)
return;
auto codepoints = GetUtf8Codepoints(text);

auto ch = font->m_characterGlyphs[character];
drawChar(ch, character);

p += byteCount - 1;
for (auto cp : codepoints)
{
auto ch = font->m_characterGlyphs[cp];
drawChar(ch, cp);
}
}
else
Expand All @@ -3340,6 +3289,39 @@ namespace LinaVG
}
}

std::vector<int32_t> GetUtf8Codepoints(const char* str)
{
std::vector<int32_t> codepoints;
const char* p = str;
while (*p)
{
int32_t codepoint = 0;
unsigned char c = *p;
if (c < 0x80)
{ // 1-byte sequence
codepoint = c;
p += 1;
}
else if (c < 0xE0)
{ // 2-byte sequence
codepoint = ((p[0] & 0x1F) << 6) | (p[1] & 0x3F);
p += 2;
}
else if (c < 0xF0)
{ // 3-byte sequence
codepoint = ((p[0] & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F);
p += 3;
}
else
{ // 4-byte sequence
codepoint = ((p[0] & 0x07) << 18) | ((p[1] & 0x3F) << 12) | ((p[2] & 0x3F) << 6) | (p[3] & 0x3F);
p += 4;
}
codepoints.push_back(codepoint);
}
return codepoints;
}

Vec2 CalcTextSize(const char* text, LinaVGFont* font, float scale, float spacing, float sdfThickness)
{
float maxCharacterHeight = 0.0f;
Expand All @@ -3355,18 +3337,12 @@ namespace LinaVG

if (font->m_supportsUnicode)
{
for (const char* p = text; *p; p++)
{
uint32_t byteCount = 0;
uint32_t character = GetNextUnicodeChar(p, byteCount);
auto codepoints = GetUtf8Codepoints(text);

if (byteCount == 0)
return Vec2(0, 0);

auto ch = font->m_characterGlyphs[character];
calcSizeChar(ch, character);

p += byteCount - 1;
for (auto cp : codepoints)
{
auto ch = font->m_characterGlyphs[cp];
calcSizeChar(ch, cp);
}
}
else
Expand Down

0 comments on commit 649e8e5

Please sign in to comment.