Skip to content

Commit 24b4607

Browse files
committed
Merge pull request godotengine#97693 from bruvzg/ts_fb_silent
[TextServer] Silently skip invalid system fallback fonts.
2 parents d21a8a8 + d222daa commit 24b4607

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
@@ -1359,7 +1359,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data,
13591359
return false;
13601360
}
13611361

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

13651365
HashMap<Vector2i, FontForSizeAdvanced *>::Iterator E = p_font_data->cache.find(p_size);
@@ -1380,7 +1380,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
13801380
error = FT_Init_FreeType(&ft_library);
13811381
if (error != 0) {
13821382
memdelete(fd);
1383-
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
1383+
if (p_silent) {
1384+
return false;
1385+
} else {
1386+
ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
1387+
}
13841388
}
13851389
#ifdef MODULE_SVG_ENABLED
13861390
FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
@@ -1414,7 +1418,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
14141418
FT_Done_Face(fd->face);
14151419
fd->face = nullptr;
14161420
memdelete(fd);
1417-
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
1421+
if (p_silent) {
1422+
return false;
1423+
} else {
1424+
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
1425+
}
14181426
}
14191427
}
14201428

@@ -1849,7 +1857,11 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
18491857
}
18501858
#else
18511859
memdelete(fd);
1852-
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
1860+
if (p_silent) {
1861+
return false;
1862+
} else {
1863+
ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
1864+
}
18531865
#endif
18541866
} else {
18551867
// Init bitmap font.
@@ -1860,6 +1872,16 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
18601872
return true;
18611873
}
18621874

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

@@ -5108,6 +5130,10 @@ RID TextServerAdvanced::_find_sys_font_for_text(const RID &p_fdef, const String
51085130
SystemFontCacheRec sysf;
51095131
sysf.rid = _create_font();
51105132
_font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
5133+
if (!_font_validate(sysf.rid)) {
5134+
_free_rid(sysf.rid);
5135+
continue;
5136+
}
51115137

51125138
Dictionary var = dvar;
51135139
// 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)