add extended ascii

This commit is contained in:
2019-09-19 11:51:49 +02:00
parent 4d788bb174
commit 77a3bdde03
4 changed files with 20 additions and 13 deletions

View File

@@ -102,13 +102,13 @@ std::vector<TextMesh::Token> TextMesh::tokenize(const std::string& s, const Font
float y = 0; float y = 0;
for (char c : p) 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_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; return ret;
} }
@@ -139,7 +139,7 @@ void TextMesh::update(kFont id, const std::string& text)
auto& f = FontManager::get(id); auto& f = FontManager::get(id);
float spacing = f.bounds.w - f.bounds.y; float spacing = f.bounds.w - f.bounds.y;
float avg_width = f.bounds.z - f.bounds.x; 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 bbmin(FLT_MAX);
glm::vec2 bbmax(-FLT_MAX); glm::vec2 bbmax(-FLT_MAX);
@@ -160,9 +160,10 @@ void TextMesh::update(kFont id, const std::string& text)
tokenize(text, f) : tokenize(text, f) :
std::vector<Token>{ Token(text, 0.f) }; std::vector<Token>{ 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; x = 0;
y += spacing * f.scale; y += spacing * f.scale;
@@ -173,16 +174,18 @@ void TextMesh::update(kFont id, const std::string& text)
{ {
x = 0; x = 0;
y += spacing * f.scale; 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; 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; x = 0;
y += spacing * f.scale; y += spacing * f.scale;
} }
stbtt_aligned_quad q; stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, (uint8_t)c - f.start_char, &x, &y, &q, true);
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c - f.start_char, &x, &y, &q, true);
auto n = (int)v.size(); 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.y1 / f.scale, q.s0, q.t1);
v.emplace_back(q.x0 / f.scale, q.y0 / f.scale, q.s0, q.t0); 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); idx.push_back(n + 3);
bbmin = glm::min(bbmin, xy(f.bounds)); bbmin = glm::min(bbmin, xy(f.bounds));
bbmax = glm::max(bbmax, { q.x1 / f.scale, y + f.bounds.w }); 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) for (auto& vi : v)

View File

@@ -14,7 +14,7 @@ class Font
public: public:
int w = 512; int w = 512;
int h = 512; int h = 512;
const int num_chars = 96; const int num_chars = 256-32;
const int start_char = 32; const int start_char = 32;
// {mix, max} // {mix, max}
glm::vec4 bounds{ 0 }; glm::vec4 bounds{ 0 };

View File

@@ -117,4 +117,5 @@ void NodeText::draw()
void NodeText::handle_resize(glm::vec2 old_size, glm::vec2 new_size, float zoom) void NodeText::handle_resize(glm::vec2 old_size, glm::vec2 new_size, float zoom)
{ {
m_text_mesh.update(font_id, m_text); m_text_mesh.update(font_id, m_text);
SetSize(m_text_mesh.bb);
} }

View File

@@ -110,7 +110,7 @@ kEventResult NodeTextInput::handle_event(Event* e)
on_return(this); 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; m_text += (char)ke->m_char;
set_text(m_text); set_text(m_text);