diff --git a/modules/juce_gui_basics/drawables/juce_DrawableText.cpp b/modules/juce_gui_basics/drawables/juce_DrawableText.cpp index 16ccb03387..a976389cc3 100644 --- a/modules/juce_gui_basics/drawables/juce_DrawableText.cpp +++ b/modules/juce_gui_basics/drawables/juce_DrawableText.cpp @@ -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 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; diff --git a/modules/juce_gui_basics/drawables/juce_DrawableText.h b/modules/juce_gui_basics/drawables/juce_DrawableText.h index 1af5a7bcd5..71d2027102 100644 --- a/modules/juce_gui_basics/drawables/juce_DrawableText.h +++ b/modules/juce_gui_basics/drawables/juce_DrawableText.h @@ -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;