Skip to content

Commit d222daa

Browse files
committed
[TextServer] Silently skip invalid system fallback fonts.
1 parent e3213aa commit d222daa

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

modules/text_server_adv/text_server_adv.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data,
13571357
return false;
13581358
}
13591359

1360-
_FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size) const {
1360+
_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 {
13611361
ERR_FAIL_COND_V(p_size.x <= 0, false);
13621362

13631363
HashMap<Vector2i, FontForSizeAdvanced *>::Iterator E = p_font_data->cache.find(p_size);
@@ -1378,7 +1378,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
13781378
error = FT_Init_FreeType(&ft_library);
13791379
if (error != 0) {
13801380
memdelete(fd);
1381-
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
1381+
if (p_silent) {
1382+
return false;
1383+
} else {
1384+
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
1385+
}
13821386
}
13831387
#ifdef MODULE_SVG_ENABLED
13841388
FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
@@ -1412,7 +1416,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
14121416
FT_Done_Face(fd->face);
14131417
fd->face = nullptr;
14141418
memdelete(fd);
1415-
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
1419+
if (p_silent) {
1420+
return false;
1421+
} else {
1422+
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
1423+
}
14161424
}
14171425
}
14181426

@@ -1847,7 +1855,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
18471855
}
18481856
#else
18491857
memdelete(fd);
1850-
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
1858+
if (p_silent) {
1859+
return false;
1860+
} else {
1861+
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
1862+
}
18511863
#endif
18521864
} else {
18531865
// Init bitmap font.
@@ -1858,6 +1870,16 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
18581870
return true;
18591871
}
18601872

1873+
_FORCE_INLINE_ bool TextServerAdvanced::_font_validate(const RID &p_font_rid) const {
1874+
FontAdvanced *fd = _get_font_data(p_font_rid);
1875+
ERR_FAIL_NULL_V(fd, false);
1876+
1877+
MutexLock lock(fd->mutex);
1878+
Vector2i size = _get_size(fd, 16);
1879+
FontForSizeAdvanced *ffsd = nullptr;
1880+
return _ensure_cache_for_size(fd, size, ffsd, true);
1881+
}
1882+
18611883
_FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) {
18621884
MutexLock ftlock(ft_mutex);
18631885

@@ -5106,6 +5128,10 @@ RID TextServerAdvanced::_find_sys_font_for_text(const RID &p_fdef, const String
51065128
SystemFontCacheRec sysf;
51075129
sysf.rid = _create_font();
51085130
_font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
5131+
if (!_font_validate(sysf.rid)) {
5132+
_free_rid(sysf.rid);
5133+
continue;
5134+
}
51095135

51105136
Dictionary var = dvar;
51115137
// Select matching style from collection.

modules/text_server_adv/text_server_adv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ class TextServerAdvanced : public TextServerExtension {
365365
_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;
366366
#endif
367367
_FORCE_INLINE_ bool _ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph) const;
368-
_FORCE_INLINE_ bool _ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size) const;
368+
_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;
369+
_FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
369370
_FORCE_INLINE_ void _font_clear_cache(FontAdvanced *p_font_data);
370371
static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
371372

modules/text_server_fb/text_server_fb.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data,
791791
return false;
792792
}
793793

794-
_FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size) const {
794+
_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 {
795795
ERR_FAIL_COND_V(p_size.x <= 0, false);
796796

797797
HashMap<Vector2i, FontForSizeFallback *>::Iterator E = p_font_data->cache.find(p_size);
@@ -813,7 +813,11 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
813813
error = FT_Init_FreeType(&ft_library);
814814
if (error != 0) {
815815
memdelete(fd);
816-
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
816+
if (p_silent) {
817+
return false;
818+
} else {
819+
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
820+
}
817821
}
818822
#ifdef MODULE_SVG_ENABLED
819823
FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
@@ -847,7 +851,11 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
847851
FT_Done_Face(fd->face);
848852
fd->face = nullptr;
849853
memdelete(fd);
850-
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
854+
if (p_silent) {
855+
return false;
856+
} else {
857+
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
858+
}
851859
}
852860
}
853861

@@ -980,7 +988,11 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
980988
}
981989
#else
982990
memdelete(fd);
983-
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
991+
if (p_silent) {
992+
return false;
993+
} else {
994+
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
995+
}
984996
#endif
985997
}
986998

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

1004+
_FORCE_INLINE_ bool TextServerFallback::_font_validate(const RID &p_font_rid) const {
1005+
FontFallback *fd = _get_font_data(p_font_rid);
1006+
ERR_FAIL_NULL_V(fd, false);
1007+
1008+
MutexLock lock(fd->mutex);
1009+
Vector2i size = _get_size(fd, 16);
1010+
FontForSizeFallback *ffsd = nullptr;
1011+
return _ensure_cache_for_size(fd, size, ffsd, true);
1012+
}
1013+
9921014
_FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
9931015
MutexLock ftlock(ft_mutex);
9941016

@@ -3920,6 +3942,10 @@ RID TextServerFallback::_find_sys_font_for_text(const RID &p_fdef, const String
39203942
SystemFontCacheRec sysf;
39213943
sysf.rid = _create_font();
39223944
_font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
3945+
if (!_font_validate(sysf.rid)) {
3946+
_free_rid(sysf.rid);
3947+
continue;
3948+
}
39233949

39243950
Dictionary var = dvar;
39253951
// Select matching style from collection.

modules/text_server_fb/text_server_fb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ class TextServerFallback : public TextServerExtension {
314314
_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;
315315
#endif
316316
_FORCE_INLINE_ bool _ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph) const;
317-
_FORCE_INLINE_ bool _ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size) const;
317+
_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;
318+
_FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
318319
_FORCE_INLINE_ void _font_clear_cache(FontFallback *p_font_data);
319320
static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
320321

0 commit comments

Comments
 (0)