Windows10 issue with system custom fonts overrides in Graphics::drawText (+workaround)

I noticed that when a custom font is installed in windows os, the call to Graphics::drawText is not using the font set with setFont but overrides it with the installed custom font from windows.

Steps to reproduce:
1/ Search on google the font “Google Font: EB Garamond”
2/ Download and Install them
3/ Graphics::drawText does not work properly anymore

Workaround (sort of) :
The workaround for this is to replace the g.drawText() by a custom method creating a textLayout, I place it is my main lookAndFeel:

    static void drawText(juce::Graphics& g,
                const juce::String& text,
                juce::Rectangle<float> bounds,
                juce::Justification just,
                juce::Colour c,
                float multiplicatorWindows = 1.2f
  {
    juce::Font font = g.getCurrentFont();
    #if BOOST_OS_WINDOWS
    bounds.translate(0, (-1.2f));
    font.setHeight(g.getCurrentFont().getHeight() * (multiplicatorWindows));
    #endif
    
    juce::AttributedString str(text);
    str.setJustification(just);
    str.setFont(font);
    str.setColour(c);
    str.setWordWrap(juce::AttributedString::none);
    
    juce::TextLayout textLayout;
    textLayout.createLayout(str, bounds.getWidth(), bounds.getHeight());
    textLayout.draw(g, bounds);
  }

it would be great if g.drawText were behaving the way it should because it is not reliable when building cross-platform apps that require to look exactly the same.
Using “textLayout” brings its own issues mostly regarding placements and it not really a wanted long-term workaround, I had to place size multiplicators and some translates on windows to obtain satisfying results.

Hope this helps to find a fix.

EDIT: This does not work 100% of the time but greatly reduces the number of machine with a font issue

bump

I’ve tried to reproduce the issue with the steps provided above, but I’m unable to do so. Could you provide more details please? It would be useful to know the affected OS version, and to see a code snippet which demonstrates the issue.

I’m running Windows 10 20H2. My steps were:

  • Downloaded the ‘EB Garamond’ font from Google Fonts, right-clicked, selected “install” and “install for all users”.
  • Modified the GuiAppExample project to use the following ‘paint’ method:
    void MainComponent::paint (juce::Graphics& g)
    {
      // (Our component is opaque, so we must completely fill the background with a solid colour)
      g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
      
      g.setFont (juce::Font { "Courier New", 64, 0 });
    
      g.setColour (juce::Colours::black);
      g.drawText ("This is some text", getLocalBounds(), juce::Justification::centred, false);
    }
    
  • Ran the app. The text was rendered using the Courier New font, as expected.

It happens only if the font you try to draw was loaded by createSystemTypefaceFor.
Bastien wasn’t clear enough

like here

which provide a sample ready to load