diff --git a/src/graphics/stateless_render.cpp b/src/graphics/stateless_render.cpp index 721d84b8df..e452f736a1 100644 --- a/src/graphics/stateless_render.cpp +++ b/src/graphics/stateless_render.cpp @@ -84,11 +84,10 @@ 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); + 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/text/fonts.cpp b/src/text/fonts.cpp index 09116f609c..e551561914 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::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; // load all glyph metrics if(glyph_in) { @@ -586,8 +583,8 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) { 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; + 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; @@ -595,8 +592,8 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) { 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; + 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; @@ -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; } + 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) { @@ -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..3cb11df904 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::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 9a15b6d669..5e6e9c7bb4 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) {