1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

DrawableText: Add option to avoid trimming the start text

This commit is contained in:
attila 2025-08-21 20:45:30 +02:00 committed by Attila Szarvas
parent 50b51f512a
commit b03a23503a
2 changed files with 40 additions and 6 deletions

View file

@ -75,6 +75,12 @@ void DrawableText::setText (const String& newText)
}
}
void DrawableText::setPreserveWhitespace (bool shouldPreserveWhitespace)
{
if (std::exchange (preserveWhitespace, shouldPreserveWhitespace) != shouldPreserveWhitespace)
refreshBounds();
}
void DrawableText::setColour (Colour newColour)
{
if (colour != newColour)
@ -173,7 +179,11 @@ void DrawableText::paint (Graphics& g)
g.setFont (scaledFont);
g.setColour (colour);
g.drawFittedText (text, getTextArea (w, h), justification, 0x100000);
if (preserveWhitespace)
g.drawText (text, getTextArea (w, h), justification, false);
else
g.drawFittedText (text, getTextArea (w, h), justification, 0x100000);
}
Rectangle<float> DrawableText::getDrawableBounds() const
@ -188,11 +198,27 @@ Path DrawableText::getOutlineAsPath() const
auto area = getTextArea (w, h).toFloat();
GlyphArrangement arr;
arr.addFittedText (scaledFont, text,
area.getX(), area.getY(),
area.getWidth(), area.getHeight(),
justification,
0x100000);
if (preserveWhitespace)
{
arr.addJustifiedText (scaledFont,
text,
area.getX(),
area.getY() + scaledFont.getAscent(),
area.getWidth(),
justification);
}
else
{
arr.addFittedText (scaledFont,
text,
area.getX(),
area.getY(),
area.getWidth(),
area.getHeight(),
justification,
0x100000);
}
Path pathOfAllGlyphs;

View file

@ -58,6 +58,13 @@ public:
/** Sets the text to display.*/
void setText (const String& newText);
/** Sets whether whitespace should always be preserved when drawing. The default value is false.
Historically, DrawableText would sometimes trim the provided text during drawing based on
its contents. With this value set to true, trimming will never occur.
*/
void setPreserveWhitespace (bool shouldPreserveWhitespace);
/** Returns the currently displayed text */
const String& getText() const noexcept { return text;}
@ -116,6 +123,7 @@ private:
float fontHeight, fontHScale;
Font font { withDefaultMetrics (FontOptions{}) }, scaledFont { withDefaultMetrics (FontOptions{}) };
String text;
bool preserveWhitespace = false;
Colour colour;
Justification justification;