diff options
-rw-r--r-- | scene/main/scene_tree.cpp | 1 | ||||
-rw-r--r-- | scene/register_scene_types.cpp | 4 | ||||
-rw-r--r-- | scene/resources/dynamic_font.cpp | 54 | ||||
-rw-r--r-- | scene/resources/dynamic_font.h | 11 |
4 files changed, 69 insertions, 1 deletions
diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index 467332b4e3..400c7f5b6e 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -498,6 +498,7 @@ bool SceneTree::idle(float p_time) { if (use_font_oversampling) { DynamicFontAtSize::font_oversampling = OS::get_singleton()->get_window_size().width / root->get_visible_rect().size.width; + DynamicFont::update_oversampling(); } last_screen_size = win_size; diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 246283edcc..39e6698725 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -542,6 +542,8 @@ void register_scene_types() { ClassDB::register_class<DynamicFontData>(); ClassDB::register_class<DynamicFont>(); + DynamicFont::initialize_dynamic_fonts(); + ClassDB::register_virtual_class<StyleBox>(); ClassDB::register_class<StyleBoxEmpty>(); ClassDB::register_class<StyleBoxTexture>(); @@ -621,6 +623,8 @@ void unregister_scene_types() { memdelete(resource_loader_stream_texture); memdelete(resource_loader_theme); + DynamicFont::finish_dynamic_fonts(); + if (resource_saver_text) { memdelete(resource_saver_text); } diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp index e6bff8f2ac..66b1e49d13 100644 --- a/scene/resources/dynamic_font.cpp +++ b/scene/resources/dynamic_font.cpp @@ -620,6 +620,21 @@ void DynamicFontAtSize::_update_char(CharType p_char) { char_map[p_char] = chr; } +bool DynamicFontAtSize::update_oversampling() { + if (oversampling == font_oversampling) + return false; + if (!valid) + return false; + + FT_Done_FreeType(library); + textures.clear(); + char_map.clear(); + oversampling = font_oversampling; + _load(); + + return true; +} + DynamicFontAtSize::DynamicFontAtSize() { valid = false; @@ -919,15 +934,52 @@ void DynamicFont::_bind_methods() { BIND_ENUM_CONSTANT(SPACING_SPACE); } -DynamicFont::DynamicFont() { +Mutex *DynamicFont::dynamic_font_mutex = NULL; + +SelfList<DynamicFont>::List DynamicFont::dynamic_fonts; + +DynamicFont::DynamicFont() : + font_list(this) { spacing_top = 0; spacing_bottom = 0; spacing_char = 0; spacing_space = 0; + if (dynamic_font_mutex) + dynamic_font_mutex->lock(); + dynamic_fonts.add(&font_list); + if (dynamic_font_mutex) + dynamic_font_mutex->unlock(); } DynamicFont::~DynamicFont() { + + if (dynamic_font_mutex) + dynamic_font_mutex->lock(); + dynamic_fonts.remove(&font_list); + if (dynamic_font_mutex) + dynamic_font_mutex->unlock(); +} + +void DynamicFont::initialize_dynamic_fonts() { + dynamic_font_mutex = Mutex::create(); +} + +void DynamicFont::finish_dynamic_fonts() { + memdelete(dynamic_font_mutex); + dynamic_font_mutex = NULL; +} + +void DynamicFont::update_oversampling() { + + SelfList<DynamicFont> *E = dynamic_fonts.first(); + while (E) { + + if (E->self()->data_at_size.is_valid() && E->self()->data_at_size->update_oversampling()) { + E->self()->emit_changed(); + } + E = E->next(); + } } ///////////////////////// diff --git a/scene/resources/dynamic_font.h b/scene/resources/dynamic_font.h index 536b8925d0..b2452a6a0a 100644 --- a/scene/resources/dynamic_font.h +++ b/scene/resources/dynamic_font.h @@ -32,6 +32,7 @@ #ifdef FREETYPE_ENABLED #include "io/resource_loader.h" +#include "os/mutex.h" #include "os/thread_safe.h" #include "scene/resources/font.h" @@ -160,6 +161,7 @@ public: float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks) const; void set_texture_flags(uint32_t p_flags); + bool update_oversampling(); DynamicFontAtSize(); ~DynamicFontAtSize(); @@ -235,6 +237,15 @@ public: virtual float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1)) const; + SelfList<DynamicFont> font_list; + + static Mutex *dynamic_font_mutex; + static SelfList<DynamicFont>::List dynamic_fonts; + + static void initialize_dynamic_fonts(); + static void finish_dynamic_fonts(); + static void update_oversampling(); + DynamicFont(); ~DynamicFont(); }; |