Skip to content

Commit

Permalink
[TextServer] Silently skip invalid system fallback fonts.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed Oct 1, 2024
1 parent e3213aa commit d222daa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
34 changes: 30 additions & 4 deletions modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data,
return false;
}

_FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size) const {
_FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size, bool p_silent) const {
ERR_FAIL_COND_V(p_size.x <= 0, false);

HashMap<Vector2i, FontForSizeAdvanced *>::Iterator E = p_font_data->cache.find(p_size);
Expand All @@ -1378,7 +1378,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
error = FT_Init_FreeType(&ft_library);
if (error != 0) {
memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
}
}
#ifdef MODULE_SVG_ENABLED
FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
Expand Down Expand Up @@ -1412,7 +1416,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
FT_Done_Face(fd->face);
fd->face = nullptr;
memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
}
}
}

Expand Down Expand Up @@ -1847,7 +1855,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
}
#else
memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
}
#endif
} else {
// Init bitmap font.
Expand All @@ -1858,6 +1870,16 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
return true;
}

_FORCE_INLINE_ bool TextServerAdvanced::_font_validate(const RID &p_font_rid) const {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL_V(fd, false);

MutexLock lock(fd->mutex);
Vector2i size = _get_size(fd, 16);
FontForSizeAdvanced *ffsd = nullptr;
return _ensure_cache_for_size(fd, size, ffsd, true);
}

_FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) {
MutexLock ftlock(ft_mutex);

Expand Down Expand Up @@ -5106,6 +5128,10 @@ RID TextServerAdvanced::_find_sys_font_for_text(const RID &p_fdef, const String
SystemFontCacheRec sysf;
sysf.rid = _create_font();
_font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
if (!_font_validate(sysf.rid)) {
_free_rid(sysf.rid);
continue;
}

Dictionary var = dvar;
// Select matching style from collection.
Expand Down
3 changes: 2 additions & 1 deletion modules/text_server_adv/text_server_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ class TextServerAdvanced : public TextServerExtension {
_FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const;
#endif
_FORCE_INLINE_ bool _ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph) const;
_FORCE_INLINE_ bool _ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size) const;
_FORCE_INLINE_ bool _ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size, bool p_silent = false) const;
_FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
_FORCE_INLINE_ void _font_clear_cache(FontAdvanced *p_font_data);
static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);

Expand Down
34 changes: 30 additions & 4 deletions modules/text_server_fb/text_server_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data,
return false;
}

_FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size) const {
_FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size, bool p_silent) const {
ERR_FAIL_COND_V(p_size.x <= 0, false);

HashMap<Vector2i, FontForSizeFallback *>::Iterator E = p_font_data->cache.find(p_size);
Expand All @@ -813,7 +813,11 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
error = FT_Init_FreeType(&ft_library);
if (error != 0) {
memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
}
}
#ifdef MODULE_SVG_ENABLED
FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
Expand Down Expand Up @@ -847,7 +851,11 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
FT_Done_Face(fd->face);
fd->face = nullptr;
memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
}
}
}

Expand Down Expand Up @@ -980,7 +988,11 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
}
#else
memdelete(fd);
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
}
#endif
}

Expand All @@ -989,6 +1001,16 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
return true;
}

_FORCE_INLINE_ bool TextServerFallback::_font_validate(const RID &p_font_rid) const {
FontFallback *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL_V(fd, false);

MutexLock lock(fd->mutex);
Vector2i size = _get_size(fd, 16);
FontForSizeFallback *ffsd = nullptr;
return _ensure_cache_for_size(fd, size, ffsd, true);
}

_FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
MutexLock ftlock(ft_mutex);

Expand Down Expand Up @@ -3920,6 +3942,10 @@ RID TextServerFallback::_find_sys_font_for_text(const RID &p_fdef, const String
SystemFontCacheRec sysf;
sysf.rid = _create_font();
_font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
if (!_font_validate(sysf.rid)) {
_free_rid(sysf.rid);
continue;
}

Dictionary var = dvar;
// Select matching style from collection.
Expand Down
3 changes: 2 additions & 1 deletion modules/text_server_fb/text_server_fb.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ class TextServerFallback : public TextServerExtension {
_FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeFallback *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const;
#endif
_FORCE_INLINE_ bool _ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph) const;
_FORCE_INLINE_ bool _ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size) const;
_FORCE_INLINE_ bool _ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size, bool p_silent = false) const;
_FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
_FORCE_INLINE_ void _font_clear_cache(FontFallback *p_font_data);
static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);

Expand Down

0 comments on commit d222daa

Please sign in to comment.