From 215bede6ff494bb371fa610b6d003fe1cb7d1c7d Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Thu, 20 Jan 2022 09:30:42 +0200 Subject: [TextServer] Add function to change font, font size, and OpenType features without invalidating line break points, justification points, or recreating shaped text buffer. --- scene/gui/label.cpp | 40 +++++++++++++--------- scene/gui/label.h | 1 + scene/gui/rich_text_label.cpp | 77 ++++++++++++++++++++++++++++++++++++++++--- scene/gui/rich_text_label.h | 2 ++ scene/gui/text_edit.cpp | 73 +++++++++++++++++++++++++++++----------- scene/gui/text_edit.h | 3 +- 6 files changed, 156 insertions(+), 40 deletions(-) (limited to 'scene/gui') diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index fab420d593..852aaaab24 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -82,9 +82,11 @@ void Label::_shape() { Ref style = get_theme_stylebox(SNAME("normal"), SNAME("Label")); int width = (get_size().width - style->get_minimum_size().width); - if (dirty) { + if (dirty || font_dirty) { String lang = (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale(); - TS->shaped_text_clear(text_rid); + if (dirty) { + TS->shaped_text_clear(text_rid); + } if (text_direction == Control::TEXT_DIRECTION_INHERITED) { TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); } else { @@ -97,9 +99,17 @@ void Label::_shape() { if (visible_chars >= 0 && visible_chars_behavior == VC_CHARS_BEFORE_SHAPING) { text = text.substr(0, visible_chars); } - TS->shaped_text_add_string(text_rid, text, font->get_rids(), font_size, opentype_features, lang); + if (dirty) { + TS->shaped_text_add_string(text_rid, text, font->get_rids(), font_size, opentype_features, lang); + } else { + int spans = TS->shaped_get_span_count(text_rid); + for (int i = 0; i < spans; i++) { + TS->shaped_set_span_update_font(text_rid, i, font->get_rids(), font_size, opentype_features); + } + } TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, text)); dirty = false; + font_dirty = false; lines_dirty = true; } @@ -276,7 +286,7 @@ void Label::_notification(int p_what) { RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true); } - if (dirty || lines_dirty) { + if (dirty || font_dirty || lines_dirty) { _shape(); } @@ -521,7 +531,7 @@ void Label::_notification(int p_what) { } if (p_what == NOTIFICATION_THEME_CHANGED) { - dirty = true; + font_dirty = true; update(); } if (p_what == NOTIFICATION_RESIZED) { @@ -531,7 +541,7 @@ void Label::_notification(int p_what) { Size2 Label::get_minimum_size() const { // don't want to mutable everything - if (dirty || lines_dirty) { + if (dirty || font_dirty || lines_dirty) { const_cast