Files
panopainter/src/font.h
2019-09-19 11:51:49 +02:00

66 lines
1.5 KiB
C++

#pragma once
#include "texture.h"
#include "util.h"
#include <stb/stb_truetype.h>
enum class kFont : uint16_t
{
Arial_11 = const_hash("arial-11"),
Arial_30 = const_hash("arial-30"),
};
class Font
{
public:
int w = 512;
int h = 512;
const int num_chars = 256-32;
const int start_char = 32;
// {mix, max}
glm::vec4 bounds{ 0 };
int size = 0;
stbtt_fontinfo font;
Texture2D font_tex;
std::string path;
std::vector<stbtt_bakedchar> chars;
float scale = 1.f;
bool load(const std::string& ttf, int sz, float scale);
void change_scale(float scale);
void calc_bounds();
};
class FontManager
{
public:
static std::map<kFont, Font> m_fonts;
static Sampler m_sampler;
static void init();
static bool load(kFont id, const char* ttf, int sz, float scale);
static const Font& get(kFont id);
static void invalidate() { m_fonts.clear(); }
static void change_scale(float scale);
};
class TextMesh
{
struct Token
{
std::string s;
float w;
Token(const std::string& str, float width) : s(str), w(width) {}
};
std::vector<Token> tokenize(const std::string& s, const Font& f) const noexcept;
public:
GLuint font_array = 0;
int font_array_count = 0;
int max_width = 0;
GLuint font_buffers[2] = {0, 0};
kFont font_id;
glm::vec2 bb = { 0, 0 };
glm::vec4 cur_box;
bool create();
void update(kFont id, const std::string& text);
void draw();
};