From 9e1089819d7531a704008d772fd0638e8ed9d7eb Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Tue, 18 Jun 2024 23:42:42 -0600 Subject: [PATCH] Add configurable font size --- README.md | 1 + inc/character_utils.h | 4 ++-- inc/config.h | 1 + src/character_utils.cpp | 8 ++++---- src/config.cpp | 2 ++ src/main.cpp | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 500852a..b3b6ad3 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ You can configure the position the window requests and the font that is used usi POSX=300 POSY=30 FONT=/usr/share/fonts/noto/NotoSans-Bold.ttf +FONT_SIZE=48 ``` However, when running under Wayland, the requested position of the window will not be respected, so to set the position, as well as removing window decorations, and preventing the window from stealing focus when it is opened, you will need to set window rules. diff --git a/inc/character_utils.h b/inc/character_utils.h index c4bd07e..2d29f0d 100644 --- a/inc/character_utils.h +++ b/inc/character_utils.h @@ -16,11 +16,11 @@ class Font { public: Font() = delete; - Font(const char* font); + Font(const char* font, FT_UInt size); void RenderText(GLuint VAO, GLuint VBO, GLuint shaderProgram, const char* text, float x, float y, float scale); private: - void SetupCharMap(FT_Face& face); + void SetupCharMap(FT_Face& face, FT_UInt size); public: bool LoadError; diff --git a/inc/config.h b/inc/config.h index b83027e..56d3247 100644 --- a/inc/config.h +++ b/inc/config.h @@ -8,6 +8,7 @@ struct Config uint32_t pos_x = 10; uint32_t pos_y = 25; std::string font = "/usr/share/fonts/noto/NotoSans-Regular.ttf"; + uint16_t font_size = 32; }; diff --git a/src/character_utils.cpp b/src/character_utils.cpp index 1fee1e8..43ef2d5 100644 --- a/src/character_utils.cpp +++ b/src/character_utils.cpp @@ -23,7 +23,7 @@ #include "character_utils.h" -Font::Font(const char* font) +Font::Font(const char* font, FT_UInt size) : LoadError(false), m_Characters() { FT_Library ft; @@ -41,17 +41,17 @@ Font::Font(const char* font) return; } - SetupCharMap(face); + SetupCharMap(face, size); FT_Done_Face(face); FT_Done_FreeType(ft); } -void Font::SetupCharMap(FT_Face &face) +void Font::SetupCharMap(FT_Face &face, FT_UInt size) { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction - FT_Set_Pixel_Sizes(face, 0, 48); + FT_Set_Pixel_Sizes(face, 0, size); for (unsigned char c = 0; c < 128; c++) { // load character glyph diff --git a/src/config.cpp b/src/config.cpp index 0dbc519..cf3f61b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -131,6 +131,8 @@ Config readConfig(std::filesystem::path config_path) config.pos_y = std::stoi(value); } else if (name == "FONT") { config.font = value; + } else if (name == "FONT_SIZE") { + config.font_size = std::stoi(value); } else { std::cerr << "Warning: Invalid line in config file" << std::endl; std::cerr << line_num << " | " << line << std::endl; diff --git a/src/main.cpp b/src/main.cpp index cafbfc8..2af1ef8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,7 +90,7 @@ int main() glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); - Font font(cfg.font.c_str()); + Font font(cfg.font.c_str(), cfg.font_size); if (font.LoadError) { return -1; }