@@ -105,6 +105,15 @@ class TypefaceCache final : private DeletedAtShutdown
105105
106106 const ScopedWriteLock slw (lock);
107107
108+ auto newFace = CachedFace { key,
109+ ++counter,
110+ juce_getTypefaceForFont != nullptr
111+ ? juce_getTypefaceForFont (font)
112+ : Font::getDefaultTypefaceForFont (font) };
113+
114+ if (newFace.typeface == nullptr )
115+ return nullptr ;
116+
108117 const auto replaceIter = std::min_element (faces.begin (),
109118 faces.end (),
110119 [] (const auto & a, const auto & b)
@@ -114,13 +123,8 @@ class TypefaceCache final : private DeletedAtShutdown
114123
115124 jassert (replaceIter != faces.end ());
116125 auto & face = *replaceIter;
117- face = CachedFace { key,
118- ++counter,
119- juce_getTypefaceForFont != nullptr
120- ? juce_getTypefaceForFont (font)
121- : Font::getDefaultTypefaceForFont (font) };
122126
123- jassert ( face. typeface != nullptr ); // the look and feel must return a typeface!
127+ face = std::move (newFace);
124128
125129 if (defaultFace == nullptr && key == Key{})
126130 defaultFace = face.typeface ;
@@ -208,10 +212,7 @@ class Font::SharedFontInternal : public ReferenceCountedObject
208212 const ScopedLock lock (mutex);
209213
210214 if (typeface == nullptr )
211- {
212215 typeface = options.getTypeface () != nullptr ? options.getTypeface () : TypefaceCache::getInstance ()->findTypefaceFor (f);
213- jassert (typeface != nullptr );
214- }
215216
216217 return typeface;
217218 }
@@ -765,8 +766,13 @@ int Font::getStringWidth (const String& text) const
765766
766767float Font::getStringWidthFloat (const String& text) const
767768{
768- const auto w = getTypefacePtr ()->getStringWidth (getMetricsKind (), text, getHeight (), getHorizontalScale ());
769- return w + (getHeight () * getHorizontalScale () * getExtraKerningFactor () * (float ) text.length ());
769+ if (auto typeface = getTypefacePtr ())
770+ {
771+ const auto w = typeface->getStringWidth (getMetricsKind (), text, getHeight (), getHorizontalScale ());
772+ return w + (getHeight () * getHorizontalScale () * getExtraKerningFactor () * (float ) text.length ());
773+ }
774+
775+ return 0 ;
770776}
771777
772778void Font::findFonts (Array<Font>& destArray)
@@ -834,9 +840,19 @@ Font Font::findSuitableFontForText (const String& text, const String& language)
834840 return copy;
835841 }
836842
837- if (auto current = getTypefacePtr ())
843+ const auto fallbackTypefacePtr = std::invoke ([&]
844+ {
845+ if (auto current = getTypefacePtr ())
846+ return current;
847+
848+ auto copy = *this ;
849+ copy.setTypefaceName (Font::getDefaultSansSerifFontName ());
850+ return copy.getTypefacePtr ();
851+ });
852+
853+ if (fallbackTypefacePtr != nullptr )
838854 {
839- if (auto suggested = current ->createSystemFallback (text, language))
855+ if (auto suggested = fallbackTypefacePtr ->createSystemFallback (text, language))
840856 {
841857 auto copy = *this ;
842858
@@ -1022,4 +1038,4 @@ static FontTests fontTests;
10221038
10231039#endif
10241040
1025- } // namespace juce
1041+ } // namespace juce
0 commit comments