From 77a3bdde03de9c1ba34ea47277608cc147b8741a Mon Sep 17 00:00:00 2001 From: omigamedev Date: Thu, 19 Sep 2019 11:51:49 +0200 Subject: [PATCH] add extended ascii --- src/font.cpp | 28 +++++++++++++++++----------- src/font.h | 2 +- src/node_text.cpp | 1 + src/node_text_input.cpp | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index f6ab406..1f00d62 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -102,13 +102,13 @@ std::vector TextMesh::tokenize(const std::string& s, const Font float y = 0; for (char c : p) { - if (c >= 32 && c < 127) // visible ascii character + if ((uint8_t)c >= 32 && (uint8_t)c < 256) // visible ascii character { stbtt_aligned_quad q; - stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c - f.start_char, &x, &y, &q, true); + stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, (uint8_t)c - f.start_char, &x, &y, &q, true); } } - ret.emplace_back(p, x * f.scale); + ret.emplace_back(p, x); } return ret; } @@ -139,7 +139,7 @@ void TextMesh::update(kFont id, const std::string& text) auto& f = FontManager::get(id); float spacing = f.bounds.w - f.bounds.y; float avg_width = f.bounds.z - f.bounds.x; - cur_box = glm::vec4(0, f.bounds.y, 5, avg_width); + cur_box = glm::vec4(0, f.bounds.y, 5, spacing); glm::vec2 bbmin(FLT_MAX); glm::vec2 bbmax(-FLT_MAX); @@ -160,9 +160,10 @@ void TextMesh::update(kFont id, const std::string& text) tokenize(text, f) : std::vector{ Token(text, 0.f) }; - for (auto p : parts) + for (size_t i = 0; i < parts.size(); i++) { - if (max_width > 0 && (x + p.w) > max_width * f.scale) + const auto& p = parts[i]; + if (i > 0 && max_width > 0 && (x + p.w) > max_width * f.scale) { x = 0; y += spacing * f.scale; @@ -173,16 +174,18 @@ void TextMesh::update(kFont id, const std::string& text) { x = 0; y += spacing * f.scale; - cur_box = glm::vec4(x, y / f.scale + f.bounds.y, 5, avg_width); + cur_box = glm::vec4(x, y / f.scale + f.bounds.y, 5, spacing); continue; } - if (max_width > 0 && x > max_width * f.scale /*font scale factor*/) + stbtt_aligned_quad q; + float wrap_test_x = x; + stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, (uint8_t)c - f.start_char, &wrap_test_x, &y, &q, true); + if (max_width > 0 && q.x1 > max_width * f.scale) { x = 0; y += spacing * f.scale; } - stbtt_aligned_quad q; - stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c - f.start_char, &x, &y, &q, true); + stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, (uint8_t)c - f.start_char, &x, &y, &q, true); auto n = (int)v.size(); v.emplace_back(q.x0 / f.scale, q.y1 / f.scale, q.s0, q.t1); v.emplace_back(q.x0 / f.scale, q.y0 / f.scale, q.s0, q.t0); @@ -196,7 +199,10 @@ void TextMesh::update(kFont id, const std::string& text) idx.push_back(n + 3); bbmin = glm::min(bbmin, xy(f.bounds)); bbmax = glm::max(bbmax, { q.x1 / f.scale, y + f.bounds.w }); - cur_box = glm::vec4(q.x1 / f.scale + 2, y / f.scale + f.bounds.y, 5, avg_width); + if (q.x1 / f.scale + 5 > max_width * f.scale) + cur_box = glm::vec4(0, (y + spacing * f.scale) / f.scale + f.bounds.y, 5, spacing); + else + cur_box = glm::vec4(q.x1 / f.scale + 2, y / f.scale + f.bounds.y, 5, spacing); } } for (auto& vi : v) diff --git a/src/font.h b/src/font.h index 6e70d6e..fd723b2 100644 --- a/src/font.h +++ b/src/font.h @@ -14,7 +14,7 @@ class Font public: int w = 512; int h = 512; - const int num_chars = 96; + const int num_chars = 256-32; const int start_char = 32; // {mix, max} glm::vec4 bounds{ 0 }; diff --git a/src/node_text.cpp b/src/node_text.cpp index a4ef35b..42312d1 100644 --- a/src/node_text.cpp +++ b/src/node_text.cpp @@ -117,4 +117,5 @@ void NodeText::draw() void NodeText::handle_resize(glm::vec2 old_size, glm::vec2 new_size, float zoom) { m_text_mesh.update(font_id, m_text); + SetSize(m_text_mesh.bb); } diff --git a/src/node_text_input.cpp b/src/node_text_input.cpp index fe3ccbc..d3d576f 100644 --- a/src/node_text_input.cpp +++ b/src/node_text_input.cpp @@ -110,7 +110,7 @@ kEventResult NodeTextInput::handle_event(Event* e) on_return(this); } } - else if (ke->m_char >= 32 && ke->m_char < 127) // visible ascii character + else if ((uint8_t)ke->m_char >= 32 && (uint8_t)ke->m_char < 256) // visible ascii character { m_text += (char)ke->m_char; set_text(m_text);