diff options
| author | volzhs | 2017-02-20 21:52:19 +0900 |
|---|---|---|
| committer | volzhs | 2017-02-22 22:54:19 +0900 |
| commit | 71a5b0885bc3401c030e509e7abe7408db58cc46 (patch) | |
| tree | 1ae646a0c42a65ae8931aa3d3da8bb0e738d72e6 | |
| parent | d5c2a6b76b9e7a444661d4102e63edd89f2256f8 (diff) | |
| download | godot-71a5b0885bc3401c030e509e7abe7408db58cc46.tar.gz godot-71a5b0885bc3401c030e509e7abe7408db58cc46.tar.zst godot-71a5b0885bc3401c030e509e7abe7408db58cc46.zip | |
Cache DynamicFont resource for Android
| -rw-r--r-- | scene/resources/dynamic_font.cpp | 27 | ||||
| -rw-r--r-- | scene/resources/dynamic_font.h | 2 |
2 files changed, 27 insertions, 2 deletions
diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp index 3361c1e41..749046189 100644 --- a/scene/resources/dynamic_font.cpp +++ b/scene/resources/dynamic_font.cpp @@ -29,6 +29,7 @@ #ifdef FREETYPE_ENABLED #include "dynamic_font.h" #include "os/file_access.h" +#include "os/os.h" bool DynamicFontData::CacheID::operator< (CacheID right) const{ @@ -106,6 +107,7 @@ DynamicFontData::~DynamicFontData() //////////////////// +HashMap< String, Vector<uint8_t> > DynamicFontAtSize::_fontdata; Error DynamicFontAtSize::_load() { @@ -115,7 +117,30 @@ Error DynamicFontAtSize::_load() { ERR_EXPLAIN(TTR("Error initializing FreeType.")); ERR_FAIL_COND_V( error !=0, ERR_CANT_CREATE ); - if (font->font_path!=String()) { + // 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); + ERR_FAIL_COND_V(!f,ERR_CANT_OPEN); + + size_t len=f->get_len(); + _fontdata[font->font_path]=Vector<uint8_t>(); + Vector<uint8_t>& fontdata=_fontdata[font->font_path]; + fontdata.resize(len); + f->get_buffer(fontdata.ptr(), 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); ERR_FAIL_COND_V(!f,ERR_CANT_OPEN); diff --git a/scene/resources/dynamic_font.h b/scene/resources/dynamic_font.h index 131c3d3e8..9d31a092f 100644 --- a/scene/resources/dynamic_font.h +++ b/scene/resources/dynamic_font.h @@ -140,7 +140,7 @@ friend class DynamicFontData; DynamicFontData::CacheID id; - + static HashMap< String, Vector<uint8_t> > _fontdata; Error _load(); protected: |
