Fonts and typefaces

Hi,

I don’t know why, but I always find working with Fonts in juce to be difficutl and hit and miss.

For instance, I’m calling Typeface::scanFolderForFonts() to scan for some fonts, and then if I call Font::findAllTypefaceNames() the new typefaces show up. All good.

I then try and use one of the fonts by:

auto the_font( Font( typeface_name, style, 14 ) );

and it fails as it can’t find the font.

Any ideas?

Ideally, I don’t want to have to load the fonts from disk and would prefer to have them in resources, but can’t find any equivalent to Typeface::scanFolderForFonts() that works on in memory fonts. e.g. what is the standard practice for registering for example 6 fonts at startup and then using them with

auto the_font( Font( typeface_name, style, 14 ) );

thx

Having had to debug fonts a few times myself, here’s some tips:

  • Dump the font names and be sure you’ve got the name right - ensure the typeface_name you pass to Font matches one of these exactly.
StringArray typefaces = Font::findAllTypefaceNames();
for (const auto& typeface : typefaces)
    DBG(typeface);
  • Same with styles:
StringArray styles = Font::findAllTypefaceStyles(typeface_name);
for (const auto& style : styles)
    DBG(style);
  • Try constructing the font with a basic style or no style to isolate the issue:
Font the_font(typeface_name, 14.0f); // No style specified
DBG("Font loaded: " + the_font.getTypefaceName());
  • If you really need to be sure, embed the font as a BinaryResource in your project, and load it that way:
  •  void registerFont(const void* fontData, size_t dataSize, const String& typefaceName)
        {
            // Create a typeface from the font data in memory
            auto typeface = Typeface::createSystemTypefaceFor(fontData, dataSize);
    
            if (typeface != nullptr)
            {
                // Optionally, you can store the typeface in a global Typeface::Ptr for reuse
                // typefaceCache.add(typefaceName, typeface);
    
                // Create a Font object to register the typeface with JUCE's font system
                Font font(typeface);
                font.setTypefaceName(typefaceName);
    
                // Log for debugging
                DBG("Registered font: " + typefaceName);
            }
            else
            {
                DBG("Failed to load font: " + typefaceName);
            }
        }
    
  • 
    
  • void registerAllBinaryFonts()
    {
        // Example: List of font resources (manually defined or generated)
        struct FontResource { const void* data; size_t size; String name; };
        Array<FontResource> fonts = {
            { BinaryData::Font1_ttf, BinaryData::Font1_ttfSize, "Font1" },
            { BinaryData::Font2_ttf, BinaryData::Font2_ttfSize, "Font2" },
            // Add more fonts
        };
    
        for (const auto& font : fonts)
            registerFont(font.data, font.size, font.name);
    }
    
2 Likes

thx for the speedy reply - will go through this.

Welcome, let us know how you proceed. I have to debug font issues every few months - in the end I just resorted to setting the policy “all fonts needed in the design are included in the bundle and we register and manage them ourselves” ..

1 Like

Not really had any joy. In order “register” a font it seems like we need to call getTypefacePtr() in order to place it into the TypefaceCache.

if ( auto typeface = Typeface::createSystemTypefaceFor( p_data, size ) ) {
   Font font( typeface->getName(), typeface->getStyle(), 12.0 );
   font.getTypefacePtr();

   DBG( "Registered: " << font.getTypefaceName() << "-" <<   font.getTypefaceStyle() );

```
If there is a later use of
Font font( name, style, 12.0 );

then it does fetch the correct typeface from the cache supposedly but it doesn’t work consistently.

So far, the only way I’ve found to do this is to call Typeface::createSystemTypefaceFor( p_data, size ) in the draw code and set the font directly from that. Not sure how efficient this is though. Just making the lookup static so it’s only done once in the function.

Don’t know why Font stuff in JUCE always seems to much effort to just load a font by name and use it.

1 Like