From dfe7af7991a13defe1ab38e9a6909bd7ff744ef3 Mon Sep 17 00:00:00 2001 From: lizzie Date: Tue, 21 Jul 2026 10:17:08 +0000 Subject: [PATCH 1/3] insert_or_find_glyph() instead of sep. get_glyph/make_glyph --- src/gamestate/system_state.cpp | 4 ++-- src/graphics/stateless_render.cpp | 6 +++--- src/launcher/launcher_main.hpp | 2 +- src/map/map_state.cpp | 4 ++-- src/text/fonts.cpp | 26 +++++++++++++------------- src/text/fonts.hpp | 3 +-- src/text/text.cpp | 12 ++++-------- 7 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index fb1951f696..c6657c1c47 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -761,7 +761,7 @@ void state::render() { // called to render the frame may (and should) delay retu if(map_state.province_labels_require_lines) { //auto glyphid = FT_Get_Char_Index(font_collection.mfont.face, 0x2026); //bool ellipsis_valid = true; - //font_collection.mfont.make_glyph(uint16_t(glyphid)); + //font_collection.mfont.insert_or_find_glyph(uint16_t(glyphid)); //auto& gso = font_collection.mfont.glyphs[uint16_t(glyphid)]; //auto width_of_ellipsis = float(gso.ft_width); @@ -771,7 +771,7 @@ void state::render() { // called to render the frame may (and should) delay retu text::stored_glyphs ellipsis_glyphs; font_collection.mfont.remake_map_cache(*this, ellipsis_glyphs, ellipsis); auto dot_g = FT_Get_Char_Index(font_collection.mfont.face, '.'); - font_collection.mfont.make_glyph(uint16_t(dot_g)); + font_collection.mfont.insert_or_find_glyph(uint16_t(dot_g)); auto& gso2 = font_collection.mfont.glyphs[uint16_t(dot_g)]; auto width_of_ellipsis = font_collection.mfont.text_extent(*this, ellipsis_glyphs, 0, ellipsis_glyphs.glyph_info.size()); */ diff --git a/src/graphics/stateless_render.cpp b/src/graphics/stateless_render.cpp index 721d84b8df..15888481db 100644 --- a/src/graphics/stateless_render.cpp +++ b/src/graphics/stateless_render.cpp @@ -84,11 +84,11 @@ void text_render( pixel_x_off = trunc_pixel_x_off + 1.0f; } - font_instance.make_glyph(uint16_t(glyphid), subpixel); - auto& gso = font_instance.get_glyph(uint16_t(glyphid), subpixel); + font_instance.insert_or_find_glyph(uint16_t(glyphid), subpixel); + auto gso = font_instance.insert_or_find_glyph(uint16_t(glyphid), subpixel)->get(); float x_advance = float(glyph_info[i].x_advance) / text::fixed_to_fp; - if(gso.width != 0) { + if (gso.width != 0) { float x_offset = pixel_x_off + float(gso.bitmap_left); float y_offset = float(-gso.bitmap_top) - float(glyph_info[i].y_offset) / text::fixed_to_fp; diff --git a/src/launcher/launcher_main.hpp b/src/launcher/launcher_main.hpp index d7ef4ba457..7a98e9ccff 100644 --- a/src/launcher/launcher_main.hpp +++ b/src/launcher/launcher_main.hpp @@ -1249,7 +1249,7 @@ void internal_text_render(std::string_view str, color_modification enabled, floa std::vector glyphs; for(unsigned int i = 0; i < glyph_count; i++) { - font_instance.make_glyph((uint16_t)glyph_info[i].codepoint, 0); + font_instance.insert_or_find_glyph((uint16_t)glyph_info[i].codepoint, 0); glyphs.emplace_back(glyph_info[i], glyph_pos[i]); } diff --git a/src/map/map_state.cpp b/src/map/map_state.cpp index a494580177..d3a31699fc 100644 --- a/src/map/map_state.cpp +++ b/src/map/map_state.cpp @@ -1358,7 +1358,7 @@ void load_map_text_glyphs(sys::state& state) { unsigned int glyph_count = static_cast(item.text.glyph_info.size()); for(unsigned int i = 0; i < glyph_count; i++) { hb_codepoint_t glyphid = item.text.glyph_info[i].codepoint; - state.font_collection.mfont.make_glyph(glyphid); + state.font_collection.mfont.insert_or_find_glyph(glyphid); } } } @@ -1368,7 +1368,7 @@ void load_map_province_text_glyphs(sys::state& state) { unsigned int glyph_count = static_cast(item.text.glyph_info.size()); for(unsigned int i = 0; i < glyph_count; i++) { hb_codepoint_t glyphid = item.text.glyph_info[i].codepoint; - state.font_collection.mfont.make_glyph(glyphid); + state.font_collection.mfont.insert_or_find_glyph(glyphid); } } } diff --git a/src/text/fonts.cpp b/src/text/fonts.cpp index 09116f609c..3240066dcb 100644 --- a/src/text/fonts.cpp +++ b/src/text/fonts.cpp @@ -539,7 +539,7 @@ void font_at_size::create(FT_Library lib, FT_Byte* file_data, size_t file_size, void font_manager::load_font(font& fnt, char const* file_data, uint32_t fz) { fnt.file_data = std::unique_ptr(new FT_Byte[fz]); fnt.file_size = fz; - memcpy(fnt.file_data.get(), file_data, fz); + std::memcpy(fnt.file_data.get(), file_data, fz); } float font_at_size::line_height(sys::state& state) const { @@ -561,12 +561,9 @@ bool font::can_display(char32_t ch_in) const { return FT_Get_Char_Index(sized_fonts.begin()->second.font_face, ch_in) != 0; } -glyph_sub_offset& font_at_size:: get_glyph(uint16_t glyph_in, int32_t subpixel) { - return glyph_positions[(uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)]; -} -void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) { - if(glyph_positions.find((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)) != glyph_positions.end()) - return; +std::optional> font_at_size::insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel) { + if(auto const it = glyph_positions.find((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)); it != glyph_positions.end()) + return it->second; // load all glyph metrics if(glyph_in) { @@ -587,7 +584,7 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) { auto err = FT_Get_Glyph(font_face->glyph, &g_result); if(err != 0) { glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); - return; + return std::nullopt; } FT_Bitmap const& bitmap = ((FT_BitmapGlyphRec*)g_result)->bitmap; @@ -596,7 +593,7 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) { if(bitmap.rows > 1024 || bitmap.width > 1024) { // too large to render FT_Done_Glyph(g_result); glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); - return; + return std::nullopt; } if(bitmap.width + internal_tx_line_xpos >= 1024) { // new line internal_tx_line_xpos = 0; @@ -648,8 +645,11 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) { delete[] temp; } FT_Done_Glyph(g_result); - glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); + + auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); + return it.first->second; } + return std::nullopt; } stored_glyphs::stored_glyphs(sys::state& state, int32_t size, font_selection type, std::span s, uint32_t details_offset, layout_details* d, uint16_t font_handle) { @@ -947,7 +947,7 @@ void font_at_size::remake_cache(sys::state& state, font_selection type, stored_g for(unsigned int j = 0; j < gcount; j++) { // Preload glyphs total_x_advance += glyph_pos[j].x_advance / (text::fixed_to_fp * state.user_settings.ui_scale); - //make_glyph(uint16_t(glyph_info[j].codepoint)); + //insert_or_find_glyph(uint16_t(glyph_info[j].codepoint)); txt.glyph_info.emplace_back(glyph_info[j], glyph_pos[j]); } } @@ -1007,7 +1007,7 @@ void font_at_size::remake_bidiless_cache(sys::state& state, font_selection type, hb_glyph_position_t* glyph_pos = hb_buffer_get_glyph_positions(hb_buf, &gcount); for(unsigned int j = 0; j < gcount; j++) { // Preload glyphs - //make_glyph(uint16_t(glyph_info[j].codepoint)); + //insert_or_find_glyph(uint16_t(glyph_info[j].codepoint)); txt.glyph_info.emplace_back(glyph_info[j], glyph_pos[j]); } @@ -1150,7 +1150,7 @@ float font_at_size::stateless_text_extent(float ui_scale, char const* codepoints hb_glyph_position_t* glyph_pos = hb_buffer_get_glyph_positions(hb_buf, &glyph_count); float x = 0.0f; for(unsigned int i = 0; i < glyph_count; i++) { - make_glyph((uint16_t)glyph_info[i].codepoint, 0); + insert_or_find_glyph((uint16_t)glyph_info[i].codepoint, 0); hb_codepoint_t glyphid = glyph_info[i].codepoint; auto& gso = glyph_positions[glyphid << 2]; float x_advance = float(glyph_pos[i].x_advance) / text::fixed_to_fp; diff --git a/src/text/fonts.hpp b/src/text/fonts.hpp index bd64364d41..1828bfb086 100644 --- a/src/text/fonts.hpp +++ b/src/text/fonts.hpp @@ -209,8 +209,7 @@ class font_at_size { std::vector textures; - void make_glyph(uint16_t glyph_in, int32_t subpixel); - glyph_sub_offset& get_glyph(uint16_t glyph_in, int32_t subpixel); + std::optional> insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel); void reset(); void create(FT_Library lib, FT_Byte* file_data, size_t file_size, int32_t real_size); void remake_cache(sys::state& state, font_selection type, stored_glyphs& txt, std::span source, uint32_t details_offset = 0, layout_details* d = nullptr, uint16_t font_handle = 0); diff --git a/src/text/text.cpp b/src/text/text.cpp index 9a15b6d669..cd9d231c94 100644 --- a/src/text/text.cpp +++ b/src/text/text.cpp @@ -1484,15 +1484,13 @@ void add_to_layout_box(sys::state& state, layout_base& dest, layout_box& box, st auto glyphid = FT_Get_Char_Index(font_inst.font_face, 0x2026); bool ellipsis_valid = true; - font_inst.make_glyph(uint16_t(glyphid), 0); - auto& gso = font_inst.get_glyph(uint16_t(glyphid), 0); + auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0)->get(); auto width_of_ellipsis = float(gso.bitmap_left + gso.width) / state.user_settings.ui_scale; if(width_of_ellipsis <= 0 || glyphid == 0) { ellipsis_valid = false; auto dot_g = FT_Get_Char_Index(font_inst.font_face, '.'); - font_inst.make_glyph(uint16_t(dot_g), 0); - auto& gso2 = font_inst.get_glyph(uint16_t(dot_g), 0); + auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0)->get(); width_of_ellipsis = float(gso2.bitmap_left + gso2.width) * 3.0f / state.user_settings.ui_scale; } if(state.user_settings.use_classic_fonts) { @@ -1612,15 +1610,13 @@ void add_to_layout_box(sys::state& state, layout_base& dest, layout_box& box, st auto glyphid = FT_Get_Char_Index(font_inst.font_face, 0x2026); bool ellipsis_valid = true; - font_inst.make_glyph(uint16_t(glyphid), 0); - auto& gso = font_inst.get_glyph(uint16_t(glyphid), 0); + auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0)->get(); auto width_of_ellipsis = float(gso.bitmap_left + gso.width) / state.user_settings.ui_scale; if(width_of_ellipsis <= 0 || glyphid == 0) { ellipsis_valid = false; auto dot_g = FT_Get_Char_Index(font_inst.font_face, '.'); - font_inst.make_glyph(uint16_t(dot_g), 0); - auto& gso2 = font_inst.get_glyph(uint16_t(dot_g), 0); + auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0)->get(); width_of_ellipsis = float(gso2.bitmap_left + gso2.width) * 3.0f / state.user_settings.ui_scale; } if(state.user_settings.use_classic_fonts) { From 3dc6b932373f667f0740259c74a7869ab00bc00f Mon Sep 17 00:00:00 2001 From: lizzie Date: Tue, 21 Jul 2026 10:27:23 +0000 Subject: [PATCH 2/3] fixed --- src/gamestate/system_state.cpp | 4 ++-- src/graphics/stateless_render.cpp | 3 +-- src/map/map_state.cpp | 4 ++-- src/text/fonts.cpp | 10 +++++----- src/text/fonts.hpp | 2 +- src/text/text.cpp | 8 ++++---- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index c6657c1c47..fb1951f696 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -761,7 +761,7 @@ void state::render() { // called to render the frame may (and should) delay retu if(map_state.province_labels_require_lines) { //auto glyphid = FT_Get_Char_Index(font_collection.mfont.face, 0x2026); //bool ellipsis_valid = true; - //font_collection.mfont.insert_or_find_glyph(uint16_t(glyphid)); + //font_collection.mfont.make_glyph(uint16_t(glyphid)); //auto& gso = font_collection.mfont.glyphs[uint16_t(glyphid)]; //auto width_of_ellipsis = float(gso.ft_width); @@ -771,7 +771,7 @@ void state::render() { // called to render the frame may (and should) delay retu text::stored_glyphs ellipsis_glyphs; font_collection.mfont.remake_map_cache(*this, ellipsis_glyphs, ellipsis); auto dot_g = FT_Get_Char_Index(font_collection.mfont.face, '.'); - font_collection.mfont.insert_or_find_glyph(uint16_t(dot_g)); + font_collection.mfont.make_glyph(uint16_t(dot_g)); auto& gso2 = font_collection.mfont.glyphs[uint16_t(dot_g)]; auto width_of_ellipsis = font_collection.mfont.text_extent(*this, ellipsis_glyphs, 0, ellipsis_glyphs.glyph_info.size()); */ diff --git a/src/graphics/stateless_render.cpp b/src/graphics/stateless_render.cpp index 15888481db..e452f736a1 100644 --- a/src/graphics/stateless_render.cpp +++ b/src/graphics/stateless_render.cpp @@ -84,8 +84,7 @@ void text_render( pixel_x_off = trunc_pixel_x_off + 1.0f; } - font_instance.insert_or_find_glyph(uint16_t(glyphid), subpixel); - auto gso = font_instance.insert_or_find_glyph(uint16_t(glyphid), subpixel)->get(); + auto gso = font_instance.insert_or_find_glyph(uint16_t(glyphid), subpixel).get(); float x_advance = float(glyph_info[i].x_advance) / text::fixed_to_fp; if (gso.width != 0) { diff --git a/src/map/map_state.cpp b/src/map/map_state.cpp index d3a31699fc..a494580177 100644 --- a/src/map/map_state.cpp +++ b/src/map/map_state.cpp @@ -1358,7 +1358,7 @@ void load_map_text_glyphs(sys::state& state) { unsigned int glyph_count = static_cast(item.text.glyph_info.size()); for(unsigned int i = 0; i < glyph_count; i++) { hb_codepoint_t glyphid = item.text.glyph_info[i].codepoint; - state.font_collection.mfont.insert_or_find_glyph(glyphid); + state.font_collection.mfont.make_glyph(glyphid); } } } @@ -1368,7 +1368,7 @@ void load_map_province_text_glyphs(sys::state& state) { unsigned int glyph_count = static_cast(item.text.glyph_info.size()); for(unsigned int i = 0; i < glyph_count; i++) { hb_codepoint_t glyphid = item.text.glyph_info[i].codepoint; - state.font_collection.mfont.insert_or_find_glyph(glyphid); + state.font_collection.mfont.make_glyph(glyphid); } } } diff --git a/src/text/fonts.cpp b/src/text/fonts.cpp index 3240066dcb..cede983573 100644 --- a/src/text/fonts.cpp +++ b/src/text/fonts.cpp @@ -561,7 +561,7 @@ bool font::can_display(char32_t ch_in) const { return FT_Get_Char_Index(sized_fonts.begin()->second.font_face, ch_in) != 0; } -std::optional> font_at_size::insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel) { +std::reference_wrapper font_at_size::insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel) { if(auto const it = glyph_positions.find((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)); it != glyph_positions.end()) return it->second; @@ -583,8 +583,8 @@ std::optional> font_at_size::insert_or_ FT_Glyph g_result; auto err = FT_Get_Glyph(font_face->glyph, &g_result); if(err != 0) { - glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); - return std::nullopt; + auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); + return it.first->second; } FT_Bitmap const& bitmap = ((FT_BitmapGlyphRec*)g_result)->bitmap; @@ -592,8 +592,8 @@ std::optional> font_at_size::insert_or_ assert(bitmap.rows <= 1024 && bitmap.width <= 1024); if(bitmap.rows > 1024 || bitmap.width > 1024) { // too large to render FT_Done_Glyph(g_result); - glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); - return std::nullopt; + auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); + return it.first->second; } if(bitmap.width + internal_tx_line_xpos >= 1024) { // new line internal_tx_line_xpos = 0; diff --git a/src/text/fonts.hpp b/src/text/fonts.hpp index 1828bfb086..3cb11df904 100644 --- a/src/text/fonts.hpp +++ b/src/text/fonts.hpp @@ -209,7 +209,7 @@ class font_at_size { std::vector textures; - std::optional> insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel); + std::reference_wrapper insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel); void reset(); void create(FT_Library lib, FT_Byte* file_data, size_t file_size, int32_t real_size); void remake_cache(sys::state& state, font_selection type, stored_glyphs& txt, std::span source, uint32_t details_offset = 0, layout_details* d = nullptr, uint16_t font_handle = 0); diff --git a/src/text/text.cpp b/src/text/text.cpp index cd9d231c94..5e6e9c7bb4 100644 --- a/src/text/text.cpp +++ b/src/text/text.cpp @@ -1484,13 +1484,13 @@ void add_to_layout_box(sys::state& state, layout_base& dest, layout_box& box, st auto glyphid = FT_Get_Char_Index(font_inst.font_face, 0x2026); bool ellipsis_valid = true; - auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0)->get(); + auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0).get(); auto width_of_ellipsis = float(gso.bitmap_left + gso.width) / state.user_settings.ui_scale; if(width_of_ellipsis <= 0 || glyphid == 0) { ellipsis_valid = false; auto dot_g = FT_Get_Char_Index(font_inst.font_face, '.'); - auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0)->get(); + auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0).get(); width_of_ellipsis = float(gso2.bitmap_left + gso2.width) * 3.0f / state.user_settings.ui_scale; } if(state.user_settings.use_classic_fonts) { @@ -1610,13 +1610,13 @@ void add_to_layout_box(sys::state& state, layout_base& dest, layout_box& box, st auto glyphid = FT_Get_Char_Index(font_inst.font_face, 0x2026); bool ellipsis_valid = true; - auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0)->get(); + auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0).get(); auto width_of_ellipsis = float(gso.bitmap_left + gso.width) / state.user_settings.ui_scale; if(width_of_ellipsis <= 0 || glyphid == 0) { ellipsis_valid = false; auto dot_g = FT_Get_Char_Index(font_inst.font_face, '.'); - auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0)->get(); + auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0).get(); width_of_ellipsis = float(gso2.bitmap_left + gso2.width) * 3.0f / state.user_settings.ui_scale; } if(state.user_settings.use_classic_fonts) { From d929dd69d68269a18f1c95f50dcf97073ba4e176 Mon Sep 17 00:00:00 2001 From: lizzie Date: Tue, 21 Jul 2026 10:29:44 +0000 Subject: [PATCH 3/3] fixed --- src/text/fonts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/text/fonts.cpp b/src/text/fonts.cpp index cede983573..e551561914 100644 --- a/src/text/fonts.cpp +++ b/src/text/fonts.cpp @@ -649,7 +649,7 @@ std::reference_wrapper font_at_size::insert_or_find_glyph(uint auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso); return it.first->second; } - return std::nullopt; + std::abort(); //<-- should NOT call here ffs } stored_glyphs::stored_glyphs(sys::state& state, int32_t size, font_selection type, std::span s, uint32_t details_offset, layout_details* d, uint16_t font_handle) {