summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2017-03-24 22:52:36 +0100
committerGitHub <noreply@github.com>2017-03-24 22:52:36 +0100
commit397380e9e5e07b438327b32a84a2e18f67ecf2c1 (patch)
treec98991c58202fee328e94fe0bc1dc66d0379162e
parent6674c556ae531a161b4d9c11076864db83965a18 (diff)
parent56af1a37900fb2496e41acfbc374f39979c7d806 (diff)
Merge pull request #8130 from volzhs/cache-font-master
Cache DynamicFont resource for Android
-rw-r--r--scene/resources/dynamic_font.cpp26
-rw-r--r--scene/resources/dynamic_font.h1
2 files changed, 26 insertions, 1 deletions
diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp
index 08ebb954b2..3fbfbc705a 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 {
@@ -98,6 +99,7 @@ DynamicFontData::~DynamicFontData() {
}
////////////////////
+HashMap<String, Vector<uint8_t> > DynamicFontAtSize::_fontdata;
Error DynamicFontAtSize::_load() {
@@ -106,7 +108,29 @@ 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 9502943909..4243ab2c03 100644
--- a/scene/resources/dynamic_font.h
+++ b/scene/resources/dynamic_font.h
@@ -141,6 +141,7 @@ class DynamicFontAtSize : public Reference {
Ref<DynamicFontData> font;
DynamicFontData::CacheID id;
+ static HashMap<String, Vector<uint8_t> > _fontdata;
Error _load();
protected: