Problems with drawFittedText


#1

Hello Jules,

I believe there's a bug in Graphics::drawFittedText(). The problem is that depending on the current frame's width, even if there is enough space to break the phrase into 3 or more lines, drawFittedText() incorrectly chooses to use just 2 lines and to insert ellipsis, thus cutting out some text. I discovered this indirectly, trying to draw a Label.

You can verify this by substituting the following code inside LiveConstantDemo.cpp in the Juce demo app, starting at line #34. Launching the demo app and activating "Begin Demo" from the "Live Constants" page, you'll see the test phrase correctly layed out using 3 lines. If you start increasing the "blockWidth" parameter (3rd panel), you'll begin to see the incorrect behaviour from value 97 onwards.

For completeness, I'm using OSX Mavericks, Juce version 3.06.

    void paint (Graphics& g)
    {
        g.fillAll (JUCE_LIVE_CONSTANT (Colour (0xffe5e7a7)));

        g.setColour (JUCE_LIVE_CONSTANT (Colours::red.withAlpha (0.2f)));
        int blockWidth = JUCE_LIVE_CONSTANT (74);
        int blockHeight = JUCE_LIVE_CONSTANT (48);
        g.fillRect ((getWidth() - blockWidth) / 2, (getHeight() - blockHeight) / 2, blockWidth, blockHeight);

        Colour fontColour = JUCE_LIVE_CONSTANT (Colour (0xff000a55));
        float fontSize = JUCE_LIVE_CONSTANT (15);

        const Font font (fontSize, Font::bold);

        g.setColour (fontColour);
        g.setFont (font);

        Rectangle<int> textArea;

        textArea.setBounds(
            (getWidth() - blockWidth) / 2,
            (getHeight() - blockHeight) / 2,
            blockWidth,
            blockHeight);

        // this emulates the way LookAndFeel_V2::drawLabel() works...
        g.drawFittedText(
            getDemoText()
            ,textArea
            ,Justification::topLeft
            ,jmax (1, (int) (textArea.getHeight() / fontSize))
            ,1.0f);
    }

    static String getDemoText()
    {
        return JUCE_LIVE_CONSTANT ("Some Files Import and Export");
    }

Thanks for your attention!


#2

Hello Jules,
is there any chance to solve the problem?


#3

Yes - I'll have a look when I get chance!


#4

I've had a look at this now, and added a bit of a tweak to the heuristic it uses. Should be better, but let me know if you find any other edge-cases where it goes wrong.


#5

Thank you very much Jules! For what I can see, it works much better now.