change font scale based on ui scale, fix brush outline color

This commit is contained in:
2019-08-08 23:50:45 +02:00
parent 34464c167e
commit 67dd9a2456
6 changed files with 59 additions and 25 deletions

View File

@@ -9,16 +9,18 @@
std::map<kFont, Font> FontManager::m_fonts;
Sampler FontManager::m_sampler;
bool Font::load(const char* ttf, int font_size)
bool Font::load(const std::string& ttf, int font_size, float font_scale)
{
Asset file;
LOG("Font::load %s", ttf);
if (file.open(ttf) && file.read_all())
if (file.open(ttf.c_str()) && file.read_all())
{
path = ttf;
scale = font_scale;
LOG("Font::load loaded");
auto bitmap = std::make_unique<uint8_t[]>(w*h);
chars.resize(num_chars);
stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size*2, bitmap.get(), w, h, start_char, num_chars, chars.data());
stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size*scale, bitmap.get(), w, h, start_char, num_chars, chars.data());
calc_bounds();
font_tex.create(w, h, GL_R8, GL_RED, bitmap.get());
file.close();
@@ -28,6 +30,11 @@ bool Font::load(const char* ttf, int font_size)
return false;
}
void Font::change_scale(float scale)
{
load(path, size, scale);
}
void Font::calc_bounds()
{
glm::vec2 bbmin(FLT_MAX);
@@ -37,8 +44,8 @@ void Font::calc_bounds()
stbtt_aligned_quad q;
float x = 0, y = 0;
stbtt_GetBakedQuad(chars.data(), w, h, i, &x, &y, &q, true);
bbmin = glm::min(bbmin, { q.x0 / 2.f, q.y0 / 2.f });
bbmax = glm::max(bbmax, { q.x1 / 2.f, q.y1 / 2.f });
bbmin = glm::min(bbmin, { q.x0 / scale, q.y0 / scale });
bbmax = glm::max(bbmax, { q.x1 / scale, q.y1 / scale });
}
bounds = { glm::vec4(bbmin, bbmax) };
}
@@ -48,9 +55,9 @@ void FontManager::init()
m_sampler.create();
}
bool FontManager::load(kFont id, const char* ttf, int sz)
bool FontManager::load(kFont id, const char* ttf, int sz, float scale)
{
return m_fonts[id].load(ttf, sz);
return m_fonts[id].load(ttf, sz, scale);
}
const Font& FontManager::get(kFont id)
@@ -58,6 +65,12 @@ const Font& FontManager::get(kFont id)
return m_fonts[id];
}
void FontManager::change_scale(float scale)
{
for (auto& f : m_fonts)
f.second.change_scale(scale);
}
bool TextMesh::create()
{
App::I->render_task([this]
@@ -96,30 +109,30 @@ void TextMesh::update(kFont id, const char* text)
if (text[i] == '\n')
{
x = 0;
y += f.size * 2;
y += f.size * f.scale;
continue;
}
if (max_width > 0 && x > max_width * 2 /*font scale factor*/)
if (max_width > 0 && x > max_width * f.scale /*font scale factor*/)
{
x = 0;
y += f.size * 2;
y += f.size * f.scale;
}
int c = text[i] - f.start_char;
stbtt_aligned_quad q;
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c, &x, &y, &q, true);
auto n = (int)v.size();
v.emplace_back(q.x0/2.f, q.y1/2.f, q.s0, q.t1);
v.emplace_back(q.x0/2.f, q.y0/2.f, q.s0, q.t0);
v.emplace_back(q.x1/2.f, q.y0/2.f, q.s1, q.t0);
v.emplace_back(q.x1/2.f, q.y1/2.f, q.s1, 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.x1/f.scale, q.y0/f.scale, q.s1, q.t0);
v.emplace_back(q.x1/f.scale, q.y1/f.scale, q.s1, q.t1);
idx.push_back(n+0);
idx.push_back(n+1);
idx.push_back(n+2);
idx.push_back(n+0);
idx.push_back(n+2);
idx.push_back(n+3);
bbmin = glm::min(bbmin, { q.x0/2.f, q.y0/2.f });
bbmax = glm::max(bbmax, { q.x1/2.f, q.y1/2.f });
bbmin = glm::min(bbmin, { q.x0/f.scale, q.y0/f.scale });
bbmax = glm::max(bbmax, { q.x1/f.scale, q.y1/f.scale });
}
for (auto& vi : v)
vi -= glm::vec4(bbmin, 0, 0);