JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_DrawableText.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30 : colour (Colours::black),
31 justification (Justification::centredLeft)
32{
33 setBoundingBox (Parallelogram<float> ({ 0.0f, 0.0f, 50.0f, 20.0f }));
34 setFont (Font (15.0f), true);
35}
36
38 : Drawable (other),
39 bounds (other.bounds),
40 fontHeight (other.fontHeight),
41 fontHScale (other.fontHScale),
42 font (other.font),
43 text (other.text),
44 colour (other.colour),
45 justification (other.justification)
46{
47 refreshBounds();
48}
49
53
55{
56 return std::make_unique<DrawableText> (*this);
57}
58
59//==============================================================================
61{
62 if (text != newText)
63 {
64 text = newText;
65 refreshBounds();
66 }
67}
68
70{
71 if (colour != newColour)
72 {
73 colour = newColour;
74 repaint();
75 }
76}
77
79{
80 if (font != newFont)
81 {
82 font = newFont;
83
85 {
86 fontHeight = font.getHeight();
87 fontHScale = font.getHorizontalScale();
88 }
89
90 refreshBounds();
91 }
92}
93
99
101{
102 if (bounds != newBounds)
103 {
104 bounds = newBounds;
105 refreshBounds();
106 }
107}
108
109void DrawableText::setFontHeight (float newHeight)
110{
111 if (! approximatelyEqual (fontHeight, newHeight))
112 {
113 fontHeight = newHeight;
114 refreshBounds();
115 }
116}
117
118void DrawableText::setFontHorizontalScale (float newScale)
119{
120 if (! approximatelyEqual (fontHScale, newScale))
121 {
122 fontHScale = newScale;
123 refreshBounds();
124 }
125}
126
127void DrawableText::refreshBounds()
128{
129 auto w = bounds.getWidth();
130 auto h = bounds.getHeight();
131
132 auto height = jlimit (0.01f, jmax (0.01f, h), fontHeight);
133 auto hscale = jlimit (0.01f, jmax (0.01f, w), fontHScale);
134
135 scaledFont = font;
136 scaledFont.setHeight (height);
137 scaledFont.setHorizontalScale (hscale);
138
139 setBoundsToEnclose (getDrawableBounds());
140 repaint();
141}
142
143//==============================================================================
144Rectangle<int> DrawableText::getTextArea (float w, float h) const
145{
146 return Rectangle<float> (w, h).getSmallestIntegerContainer();
147}
148
149AffineTransform DrawableText::getTextTransform (float w, float h) const
150{
151 return AffineTransform::fromTargetPoints (Point<float>(), bounds.topLeft,
152 Point<float> (w, 0), bounds.topRight,
153 Point<float> (0, h), bounds.bottomLeft);
154}
155
157{
158 transformContextToCorrectOrigin (g);
159
160 auto w = bounds.getWidth();
161 auto h = bounds.getHeight();
162
163 g.addTransform (getTextTransform (w, h));
164 g.setFont (scaledFont);
165 g.setColour (colour);
166
167 g.drawFittedText (text, getTextArea (w, h), justification, 0x100000);
168}
169
174
176{
177 auto w = bounds.getWidth();
178 auto h = bounds.getHeight();
179 auto area = getTextArea (w, h).toFloat();
180
182 arr.addFittedText (scaledFont, text,
183 area.getX(), area.getY(),
184 area.getWidth(), area.getHeight(),
185 justification,
186 0x100000);
187
189
190 for (auto& glyph : arr)
191 {
193 glyph.createPath (gylphPath);
195 }
196
197 pathOfAllGlyphs.applyTransform (getTextTransform (w, h).followedBy (drawableTransform));
198
199 return pathOfAllGlyphs;
200}
201
203{
204 if (colour != originalColour)
205 return false;
206
208 return true;
209}
210
211//==============================================================================
213{
215 {
216 public:
217 DrawableTextAccessibilityHandler (DrawableText& drawableTextToWrap)
218 : AccessibilityHandler (drawableTextToWrap, AccessibilityRole::staticText),
219 drawableText (drawableTextToWrap)
220 {
221 }
222
223 String getTitle() const override { return drawableText.getText(); }
224
225 private:
226 DrawableText& drawableText;
227 };
228
229 return std::make_unique<DrawableTextAccessibilityHandler> (*this);
230}
231
232} // namespace juce
Base class for accessible Components.
static AffineTransform fromTargetPoints(float x00, float y00, float x10, float y10, float x01, float y01) noexcept
Returns the transform that will map three known points onto three coordinates that are supplied.
AffineTransform followedBy(const AffineTransform &other) const noexcept
Returns the result of concatenating another transformation after this one.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
void repaint()
Marks the whole component as needing to be redrawn.
A drawable object which renders a line of text.
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Override this method to return a custom AccessibilityHandler for this component.
Rectangle< float > getDrawableBounds() const override
Returns the area that this drawable covers.
void setJustification(Justification newJustification)
Changes the justification of the text within the bounding box.
std::unique_ptr< Drawable > createCopy() const override
Creates a deep copy of this Drawable object.
void setText(const String &newText)
Sets the text to display.
bool replaceColour(Colour originalColour, Colour replacementColour) override
Recursively replaces a colour that might be used for filling or stroking.
Path getOutlineAsPath() const override
Creates a path that describes the outline of this drawable.
void setColour(Colour newColour)
Sets the colour of the text.
~DrawableText() override
Destructor.
void setFont(const Font &newFont, bool applySizeAndScale)
Sets the font to use.
void paint(Graphics &) override
Components can override this method to draw their content.
DrawableText()
Creates a DrawableText object.
void setBoundingBox(Parallelogram< float > newBounds)
Sets the bounding box that contains the text.
The base class for objects which can draw themselves, e.g.
Represents a particular font, including its size, style, etc.
Definition juce_Font.h:42
float getHeight() const noexcept
Returns the total height of this font, in pixels.
void setHorizontalScale(float scaleFactor)
Changes the font's horizontal scale factor.
void setHeight(float newHeight)
Changes the font's height.
float getHorizontalScale() const noexcept
Returns the font's horizontal scale.
A set of glyphs, each with a position.
void addFittedText(const Font &font, const String &text, float x, float y, float width, float height, Justification layout, int maximumLinesToUse, float minimumHorizontalScale=0.0f)
Tries to fit some text within a given space.
A graphics context, used for drawing a component or image.
void drawFittedText(const String &text, int x, int y, int width, int height, Justification justificationFlags, int maximumNumberOfLines, float minimumHorizontalScale=0.0f) const
Tries to draw a text string inside a given space.
void setFont(const Font &newFont)
Changes the font to use for subsequent text-drawing functions.
void addTransform(const AffineTransform &transform)
Adds a transformation which will be performed on all the graphics operations that the context subsequ...
void setColour(Colour newColour)
Changes the current drawing colour.
Represents a type of justification to be used when positioning graphical items.
Represents a parallelogram that is defined by 3 points.
ValueType getWidth() const noexcept
Returns the width of the parallelogram (i.e.
Rectangle< ValueType > getBoundingBox() const noexcept
Returns the smallest rectangle that encloses this parallelogram.
ValueType getHeight() const noexcept
Returns the height of the parallelogram (i.e.
A path is a sequence of lines and curves that may either form a closed shape or be open-ended.
Definition juce_Path.h:65
void addPath(const Path &pathToAppend)
Adds another path to this one.
Manages a rectangle and allows geometric operations to be performed on it.
Rectangle< float > toFloat() const noexcept
Casts this rectangle to a Rectangle<float>.
The JUCE String class!
Definition juce_String.h:53
JUCE Namespace.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88