From 4d583218509e479b5580e80861ac1168836d85b7 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Mon, 7 Dec 2020 10:24:30 +0200 Subject: [PATCH] [3.2] Load dynamic fonts to memory on all platforms, to avoid locked files. --- scene/resources/dynamic_font.cpp | 69 ++++---------------------------- scene/resources/dynamic_font.h | 5 +-- 2 files changed, 9 insertions(+), 65 deletions(-) diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp index 9ca0ef3fcd0b..4a3ccc86acec 100644 --- a/scene/resources/dynamic_font.cpp +++ b/scene/resources/dynamic_font.cpp @@ -112,7 +112,6 @@ DynamicFontData::~DynamicFontData() { } //////////////////// -HashMap > DynamicFontAtSize::_fontdata; Error DynamicFontAtSize::_load() { @@ -120,54 +119,22 @@ Error DynamicFontAtSize::_load() { ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType."); - // FT_OPEN_STREAM is extremely slow only on Android. - if (OS::get_singleton()->get_name() == "Android" && font->font_mem == NULL && font->font_path != String()) { - // cache font only once for each font->font_path - if (_fontdata.has(font->font_path)) { - - font->set_font_ptr(_fontdata[font->font_path].ptr(), _fontdata[font->font_path].size()); - - } else { - - FileAccess *f = FileAccess::open(font->font_path, FileAccess::READ); - if (!f) { - FT_Done_FreeType(library); - ERR_FAIL_V_MSG(ERR_CANT_OPEN, "Cannot open font file '" + font->font_path + "'."); - } - - size_t len = f->get_len(); - _fontdata[font->font_path] = Vector(); - Vector &fontdata = _fontdata[font->font_path]; - fontdata.resize(len); - f->get_buffer(fontdata.ptrw(), len); - font->set_font_ptr(fontdata.ptr(), len); - f->close(); - } - } - if (font->font_mem == NULL && font->font_path != String()) { - FileAccess *f = FileAccess::open(font->font_path, FileAccess::READ); if (!f) { FT_Done_FreeType(library); ERR_FAIL_V_MSG(ERR_CANT_OPEN, "Cannot open font file '" + font->font_path + "'."); } - memset(&stream, 0, sizeof(FT_StreamRec)); - stream.base = NULL; - stream.size = f->get_len(); - stream.pos = 0; - stream.descriptor.pointer = f; - stream.read = _ft_stream_io; - stream.close = _ft_stream_close; - - FT_Open_Args fargs; - memset(&fargs, 0, sizeof(FT_Open_Args)); - fargs.flags = FT_OPEN_STREAM; - fargs.stream = &stream; - error = FT_Open_Face(library, &fargs, 0, &face); - } else if (font->font_mem) { + size_t len = f->get_len(); + font->_fontdata = Vector(); + font->_fontdata.resize(len); + f->get_buffer(font->_fontdata.ptrw(), len); + font->set_font_ptr(font->_fontdata.ptr(), len); + f->close(); + } + if (font->font_mem) { memset(&stream, 0, sizeof(FT_StreamRec)); stream.base = (unsigned char *)font->font_mem; stream.size = font->font_mem_size; @@ -372,26 +339,6 @@ float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2 &p_pos, CharT return advance; } -unsigned long DynamicFontAtSize::_ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count) { - - FileAccess *f = (FileAccess *)stream->descriptor.pointer; - - if (f->get_position() != offset) { - f->seek(offset); - } - - if (count == 0) - return 0; - - return f->get_buffer(buffer, count); -} -void DynamicFontAtSize::_ft_stream_close(FT_Stream stream) { - - FileAccess *f = (FileAccess *)stream->descriptor.pointer; - f->close(); - memdelete(f); -} - DynamicFontAtSize::Character DynamicFontAtSize::Character::not_found() { Character ch; ch.texture_idx = -1; diff --git a/scene/resources/dynamic_font.h b/scene/resources/dynamic_font.h index 173681eff8d2..974af82e1c62 100644 --- a/scene/resources/dynamic_font.h +++ b/scene/resources/dynamic_font.h @@ -83,6 +83,7 @@ class DynamicFontData : public Resource { bool antialiased; bool force_autohinter; Hinting hinting; + Vector _fontdata; String font_path; Map size_cache; @@ -168,9 +169,6 @@ class DynamicFontAtSize : public Reference { TexturePosition _find_texture_pos_for_glyph(int p_color_size, Image::Format p_image_format, int p_width, int p_height); Character _bitmap_to_character(FT_Bitmap bitmap, int yofs, int xofs, float advance); - static unsigned long _ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count); - static void _ft_stream_close(FT_Stream stream); - HashMap char_map; _FORCE_INLINE_ void _update_char(CharType p_char); @@ -179,7 +177,6 @@ class DynamicFontAtSize : public Reference { Ref font; DynamicFontData::CacheID id; - static HashMap > _fontdata; Error _load(); public: