change font scale based on ui scale, fix brush outline color
This commit is contained in:
@@ -130,8 +130,8 @@ void App::initAssets()
|
|||||||
LOG("initializing assets");
|
LOG("initializing assets");
|
||||||
FontManager::init();
|
FontManager::init();
|
||||||
LOG("initializing assets loading fonts");
|
LOG("initializing assets loading fonts");
|
||||||
FontManager::load(kFont::Arial_11, "data/fonts/Roboto-Regular.ttf", 17);
|
FontManager::load(kFont::Arial_11, "data/fonts/Roboto-Regular.ttf", 17, display_density * zoom);
|
||||||
FontManager::load(kFont::Arial_30, "data/fonts/Roboto-Regular.ttf", 30);
|
FontManager::load(kFont::Arial_30, "data/fonts/Roboto-Regular.ttf", 30, display_density * zoom);
|
||||||
|
|
||||||
LOG("initializing assets create sampler");
|
LOG("initializing assets create sampler");
|
||||||
sampler.create(GL_NEAREST);
|
sampler.create(GL_NEAREST);
|
||||||
|
|||||||
@@ -258,6 +258,7 @@ public:
|
|||||||
void ui_restore();
|
void ui_restore();
|
||||||
void set_ui_rtl(bool rtl);
|
void set_ui_rtl(bool rtl);
|
||||||
bool get_ui_rtl() const;
|
bool get_ui_rtl() const;
|
||||||
|
void set_ui_scale(float scale);
|
||||||
|
|
||||||
void cmd_convert(std::string pano_path, std::string out_path);
|
void cmd_convert(std::string pano_path, std::string out_path);
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "node_panel_floating.h"
|
#include "node_panel_floating.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "serializer.h"
|
#include "serializer.h"
|
||||||
|
#include "font.h"
|
||||||
|
|
||||||
void App::title_update()
|
void App::title_update()
|
||||||
{
|
{
|
||||||
@@ -935,10 +936,7 @@ void App::init_menu_tools()
|
|||||||
|
|
||||||
ui_scale->on_select = [ui_scale](Node* target, int index)
|
ui_scale->on_select = [ui_scale](Node* target, int index)
|
||||||
{
|
{
|
||||||
App::I->zoom = ui_scale->get_float(index);
|
App::I->set_ui_scale(ui_scale->get_float(index));
|
||||||
Settings::set("ui-scale", Serializer::Float(App::I->zoom));
|
|
||||||
Settings::save();
|
|
||||||
App::I->title_update();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1420,6 +1418,15 @@ void App::initLayout()
|
|||||||
LOG("initializing layout completed");
|
LOG("initializing layout completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void App::set_ui_scale(float scale)
|
||||||
|
{
|
||||||
|
zoom = scale;
|
||||||
|
FontManager::change_scale(zoom * display_density);
|
||||||
|
Settings::set("ui-scale", Serializer::Float(zoom));
|
||||||
|
Settings::save();
|
||||||
|
App::I->title_update();
|
||||||
|
}
|
||||||
|
|
||||||
void App::set_ui_rtl(bool rtl)
|
void App::set_ui_rtl(bool rtl)
|
||||||
{
|
{
|
||||||
ui_rtl = rtl;
|
ui_rtl = rtl;
|
||||||
|
|||||||
@@ -281,7 +281,16 @@ void CanvasModePen::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
glm::u8vec4 pixel;
|
glm::u8vec4 pixel;
|
||||||
glReadPixels(pos.x / App::I->zoom, (App::I->height - pos.y - 1) / App::I->zoom, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
|
GLint fb_width = App::I->width;
|
||||||
|
GLint fb_height = App::I->height;
|
||||||
|
if (node->m_density != 1.f)
|
||||||
|
{
|
||||||
|
fb_width = node->m_rtt.getWidth();
|
||||||
|
fb_height = node->m_rtt.getHeight();
|
||||||
|
}
|
||||||
|
glReadPixels((pos.x / App::I->width) * fb_width,
|
||||||
|
((App::I->height - pos.y - 1) / App::I->height) * fb_height,
|
||||||
|
1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
|
||||||
bool outline = glm::min(tip_scale.x, tip_scale.y) < 20 || m_resizing ? false : m_draw_outline;
|
bool outline = glm::min(tip_scale.x, tip_scale.y) < 20 || m_resizing ? false : m_draw_outline;
|
||||||
ShaderManager::u_int(kShaderUniform::DrawOutline, outline);
|
ShaderManager::u_int(kShaderUniform::DrawOutline, outline);
|
||||||
ShaderManager::u_vec4(kShaderUniform::Col, outline ? glm::vec4(1.f - glm::vec3(pixel) / 255.f, 1.f) : tip_color);
|
ShaderManager::u_vec4(kShaderUniform::Col, outline ? glm::vec4(1.f - glm::vec3(pixel) / 255.f, 1.f) : tip_color);
|
||||||
|
|||||||
45
src/font.cpp
45
src/font.cpp
@@ -9,16 +9,18 @@
|
|||||||
std::map<kFont, Font> FontManager::m_fonts;
|
std::map<kFont, Font> FontManager::m_fonts;
|
||||||
Sampler FontManager::m_sampler;
|
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;
|
Asset file;
|
||||||
LOG("Font::load %s", ttf);
|
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");
|
LOG("Font::load loaded");
|
||||||
auto bitmap = std::make_unique<uint8_t[]>(w*h);
|
auto bitmap = std::make_unique<uint8_t[]>(w*h);
|
||||||
chars.resize(num_chars);
|
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();
|
calc_bounds();
|
||||||
font_tex.create(w, h, GL_R8, GL_RED, bitmap.get());
|
font_tex.create(w, h, GL_R8, GL_RED, bitmap.get());
|
||||||
file.close();
|
file.close();
|
||||||
@@ -28,6 +30,11 @@ bool Font::load(const char* ttf, int font_size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Font::change_scale(float scale)
|
||||||
|
{
|
||||||
|
load(path, size, scale);
|
||||||
|
}
|
||||||
|
|
||||||
void Font::calc_bounds()
|
void Font::calc_bounds()
|
||||||
{
|
{
|
||||||
glm::vec2 bbmin(FLT_MAX);
|
glm::vec2 bbmin(FLT_MAX);
|
||||||
@@ -37,8 +44,8 @@ void Font::calc_bounds()
|
|||||||
stbtt_aligned_quad q;
|
stbtt_aligned_quad q;
|
||||||
float x = 0, y = 0;
|
float x = 0, y = 0;
|
||||||
stbtt_GetBakedQuad(chars.data(), w, h, i, &x, &y, &q, true);
|
stbtt_GetBakedQuad(chars.data(), w, h, i, &x, &y, &q, true);
|
||||||
bbmin = glm::min(bbmin, { q.x0 / 2.f, q.y0 / 2.f });
|
bbmin = glm::min(bbmin, { q.x0 / scale, q.y0 / scale });
|
||||||
bbmax = glm::max(bbmax, { q.x1 / 2.f, q.y1 / 2.f });
|
bbmax = glm::max(bbmax, { q.x1 / scale, q.y1 / scale });
|
||||||
}
|
}
|
||||||
bounds = { glm::vec4(bbmin, bbmax) };
|
bounds = { glm::vec4(bbmin, bbmax) };
|
||||||
}
|
}
|
||||||
@@ -48,9 +55,9 @@ void FontManager::init()
|
|||||||
m_sampler.create();
|
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)
|
const Font& FontManager::get(kFont id)
|
||||||
@@ -58,6 +65,12 @@ const Font& FontManager::get(kFont id)
|
|||||||
return m_fonts[id];
|
return m_fonts[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FontManager::change_scale(float scale)
|
||||||
|
{
|
||||||
|
for (auto& f : m_fonts)
|
||||||
|
f.second.change_scale(scale);
|
||||||
|
}
|
||||||
|
|
||||||
bool TextMesh::create()
|
bool TextMesh::create()
|
||||||
{
|
{
|
||||||
App::I->render_task([this]
|
App::I->render_task([this]
|
||||||
@@ -96,30 +109,30 @@ void TextMesh::update(kFont id, const char* text)
|
|||||||
if (text[i] == '\n')
|
if (text[i] == '\n')
|
||||||
{
|
{
|
||||||
x = 0;
|
x = 0;
|
||||||
y += f.size * 2;
|
y += f.size * f.scale;
|
||||||
continue;
|
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;
|
x = 0;
|
||||||
y += f.size * 2;
|
y += f.size * f.scale;
|
||||||
}
|
}
|
||||||
int c = text[i] - f.start_char;
|
int c = text[i] - f.start_char;
|
||||||
stbtt_aligned_quad q;
|
stbtt_aligned_quad q;
|
||||||
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c, &x, &y, &q, true);
|
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c, &x, &y, &q, true);
|
||||||
auto n = (int)v.size();
|
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/f.scale, q.y1/f.scale, q.s0, q.t1);
|
||||||
v.emplace_back(q.x0/2.f, q.y0/2.f, q.s0, q.t0);
|
v.emplace_back(q.x0/f.scale, q.y0/f.scale, q.s0, q.t0);
|
||||||
v.emplace_back(q.x1/2.f, q.y0/2.f, q.s1, q.t0);
|
v.emplace_back(q.x1/f.scale, q.y0/f.scale, q.s1, q.t0);
|
||||||
v.emplace_back(q.x1/2.f, q.y1/2.f, q.s1, q.t1);
|
v.emplace_back(q.x1/f.scale, q.y1/f.scale, q.s1, q.t1);
|
||||||
idx.push_back(n+0);
|
idx.push_back(n+0);
|
||||||
idx.push_back(n+1);
|
idx.push_back(n+1);
|
||||||
idx.push_back(n+2);
|
idx.push_back(n+2);
|
||||||
idx.push_back(n+0);
|
idx.push_back(n+0);
|
||||||
idx.push_back(n+2);
|
idx.push_back(n+2);
|
||||||
idx.push_back(n+3);
|
idx.push_back(n+3);
|
||||||
bbmin = glm::min(bbmin, { q.x0/2.f, q.y0/2.f });
|
bbmin = glm::min(bbmin, { q.x0/f.scale, q.y0/f.scale });
|
||||||
bbmax = glm::max(bbmax, { q.x1/2.f, q.y1/2.f });
|
bbmax = glm::max(bbmax, { q.x1/f.scale, q.y1/f.scale });
|
||||||
}
|
}
|
||||||
for (auto& vi : v)
|
for (auto& vi : v)
|
||||||
vi -= glm::vec4(bbmin, 0, 0);
|
vi -= glm::vec4(bbmin, 0, 0);
|
||||||
|
|||||||
@@ -21,9 +21,12 @@ public:
|
|||||||
int size = 0;
|
int size = 0;
|
||||||
stbtt_fontinfo font;
|
stbtt_fontinfo font;
|
||||||
Texture2D font_tex;
|
Texture2D font_tex;
|
||||||
|
std::string path;
|
||||||
std::vector<stbtt_bakedchar> chars;
|
std::vector<stbtt_bakedchar> chars;
|
||||||
|
float scale = 1.f;
|
||||||
|
|
||||||
bool load(const char* ttf, int sz);
|
bool load(const std::string& ttf, int sz, float scale);
|
||||||
|
void change_scale(float scale);
|
||||||
void calc_bounds();
|
void calc_bounds();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -33,9 +36,10 @@ public:
|
|||||||
static std::map<kFont, Font> m_fonts;
|
static std::map<kFont, Font> m_fonts;
|
||||||
static Sampler m_sampler;
|
static Sampler m_sampler;
|
||||||
static void init();
|
static void init();
|
||||||
static bool load(kFont id, const char* ttf, int sz);
|
static bool load(kFont id, const char* ttf, int sz, float scale);
|
||||||
static const Font& get(kFont id);
|
static const Font& get(kFont id);
|
||||||
static void invalidate() { m_fonts.clear(); }
|
static void invalidate() { m_fonts.clear(); }
|
||||||
|
static void change_scale(float scale);
|
||||||
};
|
};
|
||||||
|
|
||||||
class TextMesh
|
class TextMesh
|
||||||
|
|||||||
Reference in New Issue
Block a user