1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-20 01:14:20 +00:00
JUCE/modules/juce_gui_basics/drawables/juce_DrawableText.cpp
2022-04-04 12:36:32 +01:00

225 lines
5.7 KiB
C++

/*
==============================================================================
This file is part of the JUCE 7 technical preview.
Copyright (c) 2022 - Raw Material Software Limited
You may use this code under the terms of the GPL v3
(see www.gnu.org/licenses).
For the technical preview this file cannot be licensed commercially.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
DrawableText::DrawableText()
: colour (Colours::black),
justification (Justification::centredLeft)
{
setBoundingBox (Parallelogram<float> ({ 0.0f, 0.0f, 50.0f, 20.0f }));
setFont (Font (15.0f), true);
}
DrawableText::DrawableText (const DrawableText& other)
: Drawable (other),
bounds (other.bounds),
fontHeight (other.fontHeight),
fontHScale (other.fontHScale),
font (other.font),
text (other.text),
colour (other.colour),
justification (other.justification)
{
refreshBounds();
}
DrawableText::~DrawableText()
{
}
std::unique_ptr<Drawable> DrawableText::createCopy() const
{
return std::make_unique<DrawableText> (*this);
}
//==============================================================================
void DrawableText::setText (const String& newText)
{
if (text != newText)
{
text = newText;
refreshBounds();
}
}
void DrawableText::setColour (Colour newColour)
{
if (colour != newColour)
{
colour = newColour;
repaint();
}
}
void DrawableText::setFont (const Font& newFont, bool applySizeAndScale)
{
if (font != newFont)
{
font = newFont;
if (applySizeAndScale)
{
fontHeight = font.getHeight();
fontHScale = font.getHorizontalScale();
}
refreshBounds();
}
}
void DrawableText::setJustification (Justification newJustification)
{
justification = newJustification;
repaint();
}
void DrawableText::setBoundingBox (Parallelogram<float> newBounds)
{
if (bounds != newBounds)
{
bounds = newBounds;
refreshBounds();
}
}
void DrawableText::setFontHeight (float newHeight)
{
if (fontHeight != newHeight)
{
fontHeight = newHeight;
refreshBounds();
}
}
void DrawableText::setFontHorizontalScale (float newScale)
{
if (fontHScale != newScale)
{
fontHScale = newScale;
refreshBounds();
}
}
void DrawableText::refreshBounds()
{
auto w = bounds.getWidth();
auto h = bounds.getHeight();
auto height = jlimit (0.01f, jmax (0.01f, h), fontHeight);
auto hscale = jlimit (0.01f, jmax (0.01f, w), fontHScale);
scaledFont = font;
scaledFont.setHeight (height);
scaledFont.setHorizontalScale (hscale);
setBoundsToEnclose (getDrawableBounds());
repaint();
}
//==============================================================================
Rectangle<int> DrawableText::getTextArea (float w, float h) const
{
return Rectangle<float> (w, h).getSmallestIntegerContainer();
}
AffineTransform DrawableText::getTextTransform (float w, float h) const
{
return AffineTransform::fromTargetPoints (Point<float>(), bounds.topLeft,
Point<float> (w, 0), bounds.topRight,
Point<float> (0, h), bounds.bottomLeft);
}
void DrawableText::paint (Graphics& g)
{
transformContextToCorrectOrigin (g);
auto w = bounds.getWidth();
auto h = bounds.getHeight();
g.addTransform (getTextTransform (w, h));
g.setFont (scaledFont);
g.setColour (colour);
g.drawFittedText (text, getTextArea (w, h), justification, 0x100000);
}
Rectangle<float> DrawableText::getDrawableBounds() const
{
return bounds.getBoundingBox();
}
Path DrawableText::getOutlineAsPath() const
{
auto w = bounds.getWidth();
auto h = bounds.getHeight();
auto area = getTextArea (w, h).toFloat();
GlyphArrangement arr;
arr.addFittedText (scaledFont, text,
area.getX(), area.getY(),
area.getWidth(), area.getHeight(),
justification,
0x100000);
Path pathOfAllGlyphs;
for (auto& glyph : arr)
{
Path gylphPath;
glyph.createPath (gylphPath);
pathOfAllGlyphs.addPath (gylphPath);
}
pathOfAllGlyphs.applyTransform (getTextTransform (w, h).followedBy (getTransform()));
return pathOfAllGlyphs;
}
bool DrawableText::replaceColour (Colour originalColour, Colour replacementColour)
{
if (colour != originalColour)
return false;
setColour (replacementColour);
return true;
}
//==============================================================================
std::unique_ptr<AccessibilityHandler> DrawableText::createAccessibilityHandler()
{
class DrawableTextAccessibilityHandler : public AccessibilityHandler
{
public:
DrawableTextAccessibilityHandler (DrawableText& drawableTextToWrap)
: AccessibilityHandler (drawableTextToWrap, AccessibilityRole::staticText),
drawableText (drawableTextToWrap)
{
}
String getTitle() const override { return drawableText.getText(); }
private:
DrawableText& drawableText;
};
return std::make_unique<DrawableTextAccessibilityHandler> (*this);
}
} // namespace juce