Avoid font corruption by pre-enlarging font atlas

This commit is contained in:
2025-05-03 12:03:45 -05:00
parent 4a2f148726
commit a84ec50045
3 changed files with 29 additions and 4 deletions

View File

@@ -34,6 +34,7 @@
#include "gfx/tiling.hpp"
#include "mathutil.hpp"
#include "fileio/fileio.hpp"
#include "fileio/resmgr/res_font.hpp"
#include "dialogxml/dialogs/strdlog.hpp"
#include "dialogxml/dialogs/choicedlog.hpp"
#include "dialogxml/widgets/scrollbar.hpp"
@@ -1265,6 +1266,9 @@ void handle_events() {
if(changed_display_mode) {
changed_display_mode = false;
// Force reload fonts for possible new UI scale
ResMgr::fonts.drain();
adjust_window_mode();
init_mini_map();
}

View File

@@ -1108,6 +1108,11 @@ void print_buf () {
int num_lines_visible = -line_offset;
location moveTo;
TextStyle line_style;
line_style.font = FONT_PLAIN;
line_style.colour = Colours::BLACK;
line_style.pointSize = 12;
while(num_lines_visible < LINES_IN_TEXT_WIN){
std::string message = text_buffer[message_idx].message;
int indent = message.find_first_not_of(' ');
@@ -1122,8 +1127,9 @@ void print_buf () {
}
moveTo = location(4, 1 + 12 * (LINES_IN_TEXT_WIN + line_offset - num_lines_total - text_buffer[message_idx].line_count));
sf::Text text(message, *ResMgr::fonts.get("plain"), 12 * get_ui_scale());
text.setColor(Colours::BLACK);
sf::Text text;
line_style.applyTo(text, get_ui_scale());
text.setString(message);
text.setPosition(moveTo);
draw_scale_aware_text(text_area_gworld(), text);
}

View File

@@ -29,8 +29,23 @@ sf::Font& get_font_rsrc(eFont font) {
}
void TextStyle::applyTo(sf::Text& text, double scale) const {
text.setFont(get_font_rsrc(font));
text.setCharacterSize(pointSize * scale);
// Guarantee the font texture for the needed text size won't need to be resized in the middle of rendering
sf::Font& font_obj = get_font_rsrc(font);
int size = pointSize * scale;
sf::Texture& font_texture = const_cast<sf::Texture&>(font_obj.getTexture(size));
int min_texture_size = 128 * scale;
if(font_texture.getSize().x < min_texture_size){
int texture_scale = 2;
while(128 * texture_scale < min_texture_size){
texture_scale << 1;
}
if(!font_texture.create(128 * texture_scale, 128 * texture_scale)){
throw std::string { "Failed to create large enough font texture!" };
}
}
text.setFont(font_obj);
text.setCharacterSize(size);
int style = sf::Text::Regular;
if(italic) style |= sf::Text::Italic;
if(underline) style |= sf::Text::Underlined;