From 04cf8158c49de38becb92e804a7fed26b39e6b77 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 12 Nov 2024 19:30:28 -0500 Subject: [PATCH] fonts --- CMakeLists.txt | 13 ++++++++- cloc.sh | 0 include/blt/gfx/renderer/font_renderer.h | 7 +++-- src/blt/gfx/renderer/font_renderer.cpp | 35 ++++++++++++------------ 4 files changed, 34 insertions(+), 21 deletions(-) mode change 100644 => 100755 cloc.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 02fb0fc..d3c35e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.25) include(FetchContent) -set(BLT_GRAPHICS_VERSION 1.0.8) +set(BLT_GRAPHICS_VERSION 1.0.9) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) @@ -39,16 +39,26 @@ else () find_package(OpenGL REQUIRED) endif () +#set(MSDFGEN_CORE_ONLY ON CACHE BOOL "" FORCE) +#set(MSDFGEN_BUILD_STANDALONE OFF CACHE BOOL "" FORCE) +#set(MSDFGEN_USE_VCPKG OFF CACHE BOOL "" FORCE) +#set(MSDFGEN_USE_CPP11 ON CACHE BOOL "" FORCE) + FetchContent_Declare(freetype GIT_REPOSITORY https://github.com/freetype/freetype.git GIT_TAG VER-2-13-3 FIND_PACKAGE_ARGS) +#FetchContent_Declare(msdfgen +# GIT_REPOSITORY https://github.com/Chlumsky/msdfgen.git +# GIT_TAG v1.12 +# FIND_PACKAGE_ARGS) FetchContent_Declare(imgui GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_TAG v1.91.4 FIND_PACKAGE_ARGS) FetchContent_MakeAvailable(freetype) +#FetchContent_MakeAvailable(msdfgen) FetchContent_MakeAvailable(imgui) set(IMGUI_SOURCE_FILES "${imgui_SOURCE_DIR}/imgui.cpp" "${imgui_SOURCE_DIR}/imgui_demo.cpp" @@ -64,6 +74,7 @@ add_library(BLT_WITH_GRAPHICS ${PROJECT_BUILD_FILES} ${IMGUI_SOURCE_FILES}) target_link_libraries(BLT_WITH_GRAPHICS PUBLIC BLT) target_link_libraries(BLT_WITH_GRAPHICS PUBLIC freetype) +#target_link_libraries(BLT_WITH_GRAPHICS PUBLIC msdfgen-core) target_include_directories(BLT_WITH_GRAPHICS PUBLIC include/) target_include_directories(BLT_WITH_GRAPHICS PUBLIC "${imgui_SOURCE_DIR}") diff --git a/cloc.sh b/cloc.sh old mode 100644 new mode 100755 diff --git a/include/blt/gfx/renderer/font_renderer.h b/include/blt/gfx/renderer/font_renderer.h index 3fcb6a9..d81f42a 100644 --- a/include/blt/gfx/renderer/font_renderer.h +++ b/include/blt/gfx/renderer/font_renderer.h @@ -47,7 +47,7 @@ namespace blt::gfx blt::u64 max_index_used; }; public: - explicit font_texture_atlas(blt::i32 dimensions); + explicit font_texture_atlas(blt::i32 dimensions, blt::u32 padding); added_font_results_t add_font(const font::font_file_t& file, float size); @@ -63,6 +63,7 @@ namespace blt::gfx blt::hashmap_t> glyphs; blt::u32 used_width = 0; blt::u32 used_height = 0; + blt::u32 padding; }; class font_generator_t @@ -80,7 +81,7 @@ namespace blt::gfx }; public: - explicit font_generator_t(blt::i32 dimensions, const std::vector& sizes_to_generate); + explicit font_generator_t(blt::i32 dimensions, blt::u32 padding, const std::vector& sizes_to_generate); void add_font(const font::font_file_t& file); @@ -101,7 +102,9 @@ namespace blt::gfx private: blt::i32 dimensions; + blt::u32 padding; std::vector atlases; + // doesn't make sense to store this. std::vector sizes_to_generate; }; diff --git a/src/blt/gfx/renderer/font_renderer.cpp b/src/blt/gfx/renderer/font_renderer.cpp index 2c2debe..d71bd50 100644 --- a/src/blt/gfx/renderer/font_renderer.cpp +++ b/src/blt/gfx/renderer/font_renderer.cpp @@ -18,18 +18,22 @@ #include #include #include -#include "blt/gfx/shader.h" +#include + +// TODO: signed distance fonts +//#include #include namespace blt::gfx { - font_texture_atlas::font_texture_atlas(blt::i32 dimensions): texture_gl2D(dimensions, dimensions, GL_R8) + + font_texture_atlas::font_texture_atlas(blt::i32 dimensions, blt::u32 padding): texture_gl2D(dimensions, dimensions, GL_R8), padding(padding) { bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); } font_texture_atlas::added_font_results_t font_texture_atlas::add_font(const font::font_file_t& file, float size) @@ -44,8 +48,6 @@ namespace blt::gfx blt::u64 max_index_used = start; file.font.set_pixel_sizes(0, static_cast(size)); - const blt::u32 padding = 2; - for (auto i = start; i < file.character_max_index; i++) { // returns an empty optional if there is no error code @@ -56,15 +58,12 @@ namespace blt::gfx auto width = face->glyph->bitmap.width; auto height = face->glyph->bitmap.rows; - auto texture_width = width + padding; - auto texture_height = height + padding; - - max_height = std::max(max_height, texture_height); + max_height = std::max(max_height, height); // if adding this width overflows, we need to move to the next row. - if (used_width + texture_width > static_cast(getWidth())) + if (used_width + width > static_cast(getWidth())) { - used_height += max_height; + used_height += max_height + padding; max_height = 0; used_width = 0; } @@ -79,7 +78,7 @@ namespace blt::gfx auto begin_height = used_height; auto end_width = used_width + width; auto end_height = used_height + height; - used_width += texture_width; + used_width += width + padding; // BLT_TRACE("%d %d %d %d", begin_width, begin_height, width, height); // upload the texture @@ -99,8 +98,8 @@ namespace blt::gfx return {-1ul, min_index_used, max_index_used}; } - font_generator_t::font_generator_t(blt::i32 dimensions, const std::vector& sizes_to_generate): - dimensions(dimensions), sizes_to_generate(sizes_to_generate) + font_generator_t::font_generator_t(blt::i32 dimensions, blt::u32 padding, const std::vector& sizes_to_generate): + dimensions(dimensions), padding(padding), sizes_to_generate(sizes_to_generate) {} void font_generator_t::add_font(const font::font_file_t& file) @@ -115,7 +114,7 @@ namespace blt::gfx auto& vec = atlases; if (vec.empty()) - vec.emplace_back(std::make_unique(dimensions), min_size, max_size, file.character_min_index, 0); + vec.emplace_back(std::make_unique(dimensions, padding), min_size, max_size, file.character_min_index, 0); blt::u64 start_index = file.character_min_index; while (start_index != -1ul) @@ -127,7 +126,7 @@ namespace blt::gfx vec.back().max_size = max_size; if (results.last_inserted_index != -1ul) { - vec.emplace_back(std::make_unique(dimensions), min_size, max_size, start_index, 0); + vec.emplace_back(std::make_unique(dimensions, padding), min_size, max_size, start_index, 0); min_size = size; max_size = size; } @@ -154,10 +153,10 @@ namespace blt::gfx { int size; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size); - dimensions = std::min(size, 4096); + dimensions = std::min(size, 2048); BLT_INFO("Max texture size %d, font renderer will use size %d", size, dimensions); } - generator = std::make_unique(dimensions, generated_font_sizes); + generator = std::make_unique(dimensions, 2, generated_font_sizes); } void font_renderer_t::cleanup()