drawFittedText() not constrained to bounds?


#1

I’m painting a multiline string with g.drawFittedText(message, b, Justification::topLeft, 1); and getting the following result :

image

The red background is being filled with g.fillRect(b).

I would have expected that I would only see 1 line given the 4th parameter to drawFittedText (I set that like that as a test), and also that the text would be constrained to the bounds.

Using JUCE 5.4.1 on master. I also tried without setting a custom font in case something screwy was happening there.

Is this a bug or have I made a dumb mistake/faulty assumption?


#2

To confirm it’s not me doing something dumb, I started a new AudioApplication from the Projucer and simply added this to the paint method :

    auto b = Rectangle<int>(50, 50, 200, 100);
    g.setColour(Colours::darkred);
    g.fillRect(b);
    String message = "This is a message.\nIt can be extremely long if required.\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    g.setColour(Colours::white);
    g.drawFittedText(message, b, Justification::topLeft, 1);

which gives me this result :

Of course I still may have done something dumb, but I hope not :wink:


#3

Those functions don’t actually clip anything, the rectangle just tells it where you want the text. If you want it to be clipped to that rect, just call g.reduceClipRegion before you draw.


#4

Super, thanks. Faulty assumption it was then. :slight_smile:
Although the documentation does state this :

If the text just won’t fit into the space, it’ll cram as much as possible in there, and put some ellipsis at the end to show that it’s been truncated.

which is why I thought something was broken.

edit : it seems having \n in the text changes the behaviour.
image


#5

Hmm, that is a bit misleading. I think maybe it can only do the ellipsis if you give it a single line to lay out.


#6

Yes, appears so. As noted in the edit to previous reply.