Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.2] Load dynamic fonts to memory on all platforms, to avoid locked files. #44160

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 8 additions & 61 deletions scene/resources/dynamic_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,62 +112,29 @@ DynamicFontData::~DynamicFontData() {
}

////////////////////
HashMap<String, Vector<uint8_t> > DynamicFontAtSize::_fontdata;

Error DynamicFontAtSize::_load() {

int error = FT_Init_FreeType(&library);

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<uint8_t>();
Vector<uint8_t> &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<uint8_t>();
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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions scene/resources/dynamic_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class DynamicFontData : public Resource {
bool antialiased;
bool force_autohinter;
Hinting hinting;
Vector<uint8_t> _fontdata;

String font_path;
Map<CacheID, DynamicFontAtSize *> size_cache;
Expand Down Expand Up @@ -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<CharType, Character> char_map;

_FORCE_INLINE_ void _update_char(CharType p_char);
Expand All @@ -179,7 +177,6 @@ class DynamicFontAtSize : public Reference {
Ref<DynamicFontData> font;
DynamicFontData::CacheID id;

static HashMap<String, Vector<uint8_t> > _fontdata;
Error _load();

public:
Expand Down