@@ -279,10 +279,12 @@ namespace
279279// ---------------------------------------------------------------------------
280280// FreeType library singleton (created lazily, cleaned up by FontSystem dtor)
281281// ---------------------------------------------------------------------------
282- static FT_Library s_ft_lib = nullptr ;
282+ static FT_Library s_ft_lib = nullptr ;
283+ static std::mutex s_ft_lib_mtx;
283284
284285static FT_Library get_ft_library ()
285286{
287+ std::lock_guard<std::mutex> lk (s_ft_lib_mtx);
286288 if (!s_ft_lib) FT_Init_FreeType (&s_ft_lib);
287289 return s_ft_lib;
288290}
@@ -580,7 +582,8 @@ namespace Fonts
580582
581583struct Font ::FTData
582584{
583- FT_Face face = nullptr ;
585+ FT_Face face = nullptr ;
586+ std::mutex face_mtx;
584587};
585588
586589// ============================================================================
@@ -647,7 +650,11 @@ void Font::draw(HAL::CommandList::ptr& list,
647650 if (!m_data || !m_data->face ) return ;
648651
649652 FontAtlas& atlas = FontSystem::get_atlas ();
650- auto verts = layout_text (m_data->face , str, size, area, color, flags, atlas);
653+ std::vector<GlyphVtx> verts;
654+ {
655+ std::lock_guard<std::mutex> face_lk (m_data->face_mtx );
656+ verts = layout_text (m_data->face , str, size, area, color, flags, atlas);
657+ }
651658 draw_vertices (list, verts, &clip_rect, nullptr , flags | FW1_CLIPRECT , atlas);
652659}
653660
@@ -656,6 +663,7 @@ vec2 Font::measure(std::string str, float size, unsigned int flags)
656663 if (!m_data || !m_data->face || str.empty ()) return {0 .f , 0 .f };
657664
658665 std::wstring wstr = convert (str);
666+ std::lock_guard<std::mutex> face_lk (m_data->face_mtx );
659667 FT_Face face = m_data->face ;
660668 uint32_t px = static_cast <uint32_t >(std::ceil (size));
661669 FT_Set_Pixel_Sizes (face, 0 , px);
@@ -757,6 +765,7 @@ HAL::TextureResource* FontSystem::get_atlas_texture()
757765
758766Font::ptr FontSystem::get_font (std::string font_name)
759767{
768+ std::lock_guard<std::mutex> lk (m_fonts_mtx);
760769 return fonts[font_name];
761770}
762771
@@ -799,6 +808,7 @@ void FontGeometry::set(HAL::CommandList::ptr& /*list*/,
799808
800809 if (!font || !font->m_data || !font->m_data ->face ) return ;
801810
811+ std::lock_guard<std::mutex> face_lk (font->m_data ->face_mtx );
802812 m_impl->verts = layout_text (font->m_data ->face , str, size,
803813 area, color, flags, FontSystem::get_atlas ());
804814}
@@ -814,8 +824,12 @@ sizer FontGeometry::add(HAL::CommandList::ptr& /*list*/,
814824
815825 if (!font || !font->m_data || !font->m_data ->face ) return area;
816826
817- auto new_verts = layout_text (font->m_data ->face , str, size,
818- area, color, flags, FontSystem::get_atlas ());
827+ std::vector<GlyphVtx> new_verts;
828+ {
829+ std::lock_guard<std::mutex> face_lk (font->m_data ->face_mtx );
830+ new_verts = layout_text (font->m_data ->face , str, size,
831+ area, color, flags, FontSystem::get_atlas ());
832+ }
819833 for (auto & v : new_verts)
820834 m_impl->verts .push_back (v);
821835
0 commit comments