Make dynamic font oversampling fully dynamic.

This commit is contained in:
Juan Linietsky 2017-12-19 21:58:06 -03:00
parent 7a10d3ab78
commit b08735f209
4 changed files with 69 additions and 1 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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();
}
}
/////////////////////////

View File

@ -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();
};