drawFittedText justification - solved

drawFittedTextProblem.zip (4.3 KB) Hi!

I am trying to draw text at the very top of a rectangle.
To do that I am using drawFittedText with Justification::centredTop.
Unfortunately, the text is a bit off. I would expect the text to appear at the very top. But actually it is a few pixels down.

Here is a zoomed in version of my problem:

As you can see, the text starts a few pixels below the top of the blue rectangle.
Maybe I am picky, but I would like it to start at the very top of the blue rectangle :slight_smile:

Here is my code:


    //define area:
    juce::Rectangle<int> textArea(30,30, 100, 100);
    
    //draw a blue background rect into textArea:
    g.setColour(juce::Colours::blue);
    g.fillRect(textArea);
    
    //draw text into same textArea: 
    g.setFont (juce::Font (16.0f));
    g.setColour (juce::Colours::white);
    g.drawFittedText ("Text", textArea, juce::Justification::centredTop, 1, 1);

I am testing on JUCE 6.0.7 under OSX 11.2.2 (big sur) on an Intel Mac.

I have attached the zipped test project, which can be used to reproduce the problem (see drawFittedTextProblem.zip).

JUCE doesn’t consider the characters themselves but what the fonts report as the ascent/descents which are the peaks & bottoms of the characters. This can very between windows and Mac and sometimes be a pain to deal with.

I would try some other fonts and see if you can find one which positions more to your liking.

It’s easy to pick a font for a project and tune it to a platform only to realize it renders differently on Mac vs windows

Thanks for your answer.
I think I get it. The extra space at top is for things such as “Umlauts” (Ä, Ü. etc).
I guess the font needs to leave extra space for those dots above the letter.
Hmmm…

What I am doing now is to compensate for the offset. By drawing the text a few pixels above where I actually want it to be. (Because my text has no umlauts).