Justification::centred won't work correctly with extra kerning


When using Font::setExtraKerningFactor() (or withExtraKerningFactor()), centring doesn’t work correctly any more. I think the string width is calculated wrongly as the extra kerning after the last glyph is taken into account.

To reproduce this issue, create a new GUI application with a basic window and replace the paint routine with the following code. Besides the two text lines, rectangles are drawn two show the different border sizes on the left and right.

void MainContentComponent::paint (Graphics& g)
    const int w = getWidth();

    g.fillAll (Colour (0xff001F36));

    // text; added an H at the end to have a clear right border
    const String text("Hello World!H");

    // used fonts
    Font font(Font(20.f));
    Font kerning(font.withExtraKerningFactor(1.f));

    // left border distances with width = 600
    const int borderNormal = 245;
    const int borderKerning = 115;

    // draw texts
    g.drawText(text, 0, 10, w, 50, Justification::centred, true);

    g.drawText(text, 0, 60, w, 50, Justification::centred, true);

    // draw rectangles to show borders
    g.drawRect(0, 10, borderNormal, 50);
    g.drawRect(w-borderNormal, 10, borderNormal, 50);

    g.drawRect(0, 60, borderKerning, 50);
    g.drawRect(w - borderKerning, 60, borderKerning, 50);

I tested this with a rather current JUCE4 on Windows but I’d expect it to be system independent.

Edit: added an image to show the issue:


Can't argue that you're right, but can't promise to fix it soon as it's pretty obscure.. Will add it to our backlog!