2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
30 #include "juce_DrawableText.h"
31 #include "juce_DrawableComposite.h"
34 //==============================================================================
35 DrawableText::DrawableText()
36 : colour (Colours::black
),
37 justification (Justification::centredLeft
)
39 setBoundingBox (RelativeParallelogram (RelativePoint (0.0f
, 0.0f
),
40 RelativePoint (50.0f
, 0.0f
),
41 RelativePoint (0.0f
, 20.0f
)));
42 setFont (Font (15.0f
), true);
45 DrawableText::DrawableText (const DrawableText
& other
)
46 : bounds (other
.bounds
),
47 fontSizeControlPoint (other
.fontSizeControlPoint
),
50 colour (other
.colour
),
51 justification (other
.justification
)
55 DrawableText::~DrawableText()
59 //==============================================================================
60 void DrawableText::setText (const String
& newText
)
69 void DrawableText::setColour (const Colour
& newColour
)
71 if (colour
!= newColour
)
78 void DrawableText::setFont (const Font
& newFont
, bool applySizeAndScale
)
84 if (applySizeAndScale
)
86 setFontSizeControlPoint (RelativePoint (RelativeParallelogram::getPointForInternalCoord (resolvedPoints
,
87 Point
<float> (font
.getHorizontalScale() * font
.getHeight(), font
.getHeight()))));
94 void DrawableText::setJustification (const Justification
& newJustification
)
96 justification
= newJustification
;
100 void DrawableText::setBoundingBox (const RelativeParallelogram
& newBounds
)
102 if (bounds
!= newBounds
)
109 void DrawableText::setFontSizeControlPoint (const RelativePoint
& newPoint
)
111 if (fontSizeControlPoint
!= newPoint
)
113 fontSizeControlPoint
= newPoint
;
118 void DrawableText::refreshBounds()
120 if (bounds
.isDynamic() || fontSizeControlPoint
.isDynamic())
122 Drawable::Positioner
<DrawableText
>* const p
= new Drawable::Positioner
<DrawableText
> (*this);
129 recalculateCoordinates (0);
133 bool DrawableText::registerCoordinates (RelativeCoordinatePositionerBase
& pos
)
135 bool ok
= pos
.addPoint (bounds
.topLeft
);
136 ok
= pos
.addPoint (bounds
.topRight
) && ok
;
137 ok
= pos
.addPoint (bounds
.bottomLeft
) && ok
;
138 return pos
.addPoint (fontSizeControlPoint
) && ok
;
141 void DrawableText::recalculateCoordinates (Expression::Scope
* scope
)
143 bounds
.resolveThreePoints (resolvedPoints
, scope
);
145 const float w
= Line
<float> (resolvedPoints
[0], resolvedPoints
[1]).getLength();
146 const float h
= Line
<float> (resolvedPoints
[0], resolvedPoints
[2]).getLength();
148 const Point
<float> fontCoords (RelativeParallelogram::getInternalCoordForPoint (resolvedPoints
, fontSizeControlPoint
.resolve (scope
)));
149 const float fontHeight
= jlimit (0.01f
, jmax (0.01f
, h
), fontCoords
.getY());
150 const float fontWidth
= jlimit (0.01f
, jmax (0.01f
, w
), fontCoords
.getX());
153 scaledFont
.setHeight (fontHeight
);
154 scaledFont
.setHorizontalScale (fontWidth
/ fontHeight
);
156 setBoundsToEnclose (getDrawableBounds());
160 const AffineTransform
DrawableText::getArrangementAndTransform (GlyphArrangement
& glyphs
) const
162 const float w
= Line
<float> (resolvedPoints
[0], resolvedPoints
[1]).getLength();
163 const float h
= Line
<float> (resolvedPoints
[0], resolvedPoints
[2]).getLength();
165 glyphs
.addFittedText (scaledFont
, text
, 0, 0, w
, h
, justification
, 0x100000);
167 return AffineTransform::fromTargetPoints (0, 0, resolvedPoints
[0].getX(), resolvedPoints
[0].getY(),
168 w
, 0, resolvedPoints
[1].getX(), resolvedPoints
[1].getY(),
169 0, h
, resolvedPoints
[2].getX(), resolvedPoints
[2].getY());
172 //==============================================================================
173 void DrawableText::paint (Graphics
& g
)
175 transformContextToCorrectOrigin (g
);
177 g
.setColour (colour
);
180 const AffineTransform
transform (getArrangementAndTransform (ga
));
181 ga
.draw (g
, transform
);
184 Rectangle
<float> DrawableText::getDrawableBounds() const
186 return RelativeParallelogram::getBoundingBox (resolvedPoints
);
189 Drawable
* DrawableText::createCopy() const
191 return new DrawableText (*this);
194 //==============================================================================
195 const Identifier
DrawableText::valueTreeType ("Text");
197 const Identifier
DrawableText::ValueTreeWrapper::text ("text");
198 const Identifier
DrawableText::ValueTreeWrapper::colour ("colour");
199 const Identifier
DrawableText::ValueTreeWrapper::font ("font");
200 const Identifier
DrawableText::ValueTreeWrapper::justification ("justification");
201 const Identifier
DrawableText::ValueTreeWrapper::topLeft ("topLeft");
202 const Identifier
DrawableText::ValueTreeWrapper::topRight ("topRight");
203 const Identifier
DrawableText::ValueTreeWrapper::bottomLeft ("bottomLeft");
204 const Identifier
DrawableText::ValueTreeWrapper::fontSizeAnchor ("fontSizeAnchor");
206 //==============================================================================
207 DrawableText::ValueTreeWrapper::ValueTreeWrapper (const ValueTree
& state_
)
208 : ValueTreeWrapperBase (state_
)
210 jassert (state
.hasType (valueTreeType
));
213 String
DrawableText::ValueTreeWrapper::getText() const
215 return state
[text
].toString();
218 void DrawableText::ValueTreeWrapper::setText (const String
& newText
, UndoManager
* undoManager
)
220 state
.setProperty (text
, newText
, undoManager
);
223 Value
DrawableText::ValueTreeWrapper::getTextValue (UndoManager
* undoManager
)
225 return state
.getPropertyAsValue (text
, undoManager
);
228 Colour
DrawableText::ValueTreeWrapper::getColour() const
230 return Colour::fromString (state
[colour
].toString());
233 void DrawableText::ValueTreeWrapper::setColour (const Colour
& newColour
, UndoManager
* undoManager
)
235 state
.setProperty (colour
, newColour
.toString(), undoManager
);
238 Justification
DrawableText::ValueTreeWrapper::getJustification() const
240 return Justification ((int) state
[justification
]);
243 void DrawableText::ValueTreeWrapper::setJustification (const Justification
& newJustification
, UndoManager
* undoManager
)
245 state
.setProperty (justification
, newJustification
.getFlags(), undoManager
);
248 Font
DrawableText::ValueTreeWrapper::getFont() const
250 return Font::fromString (state
[font
]);
253 void DrawableText::ValueTreeWrapper::setFont (const Font
& newFont
, UndoManager
* undoManager
)
255 state
.setProperty (font
, newFont
.toString(), undoManager
);
258 Value
DrawableText::ValueTreeWrapper::getFontValue (UndoManager
* undoManager
)
260 return state
.getPropertyAsValue (font
, undoManager
);
263 RelativeParallelogram
DrawableText::ValueTreeWrapper::getBoundingBox() const
265 return RelativeParallelogram (state
[topLeft
].toString(), state
[topRight
].toString(), state
[bottomLeft
].toString());
268 void DrawableText::ValueTreeWrapper::setBoundingBox (const RelativeParallelogram
& newBounds
, UndoManager
* undoManager
)
270 state
.setProperty (topLeft
, newBounds
.topLeft
.toString(), undoManager
);
271 state
.setProperty (topRight
, newBounds
.topRight
.toString(), undoManager
);
272 state
.setProperty (bottomLeft
, newBounds
.bottomLeft
.toString(), undoManager
);
275 RelativePoint
DrawableText::ValueTreeWrapper::getFontSizeControlPoint() const
277 return state
[fontSizeAnchor
].toString();
280 void DrawableText::ValueTreeWrapper::setFontSizeControlPoint (const RelativePoint
& p
, UndoManager
* undoManager
)
282 state
.setProperty (fontSizeAnchor
, p
.toString(), undoManager
);
285 //==============================================================================
286 void DrawableText::refreshFromValueTree (const ValueTree
& tree
, ComponentBuilder
&)
288 ValueTreeWrapper
v (tree
);
289 setComponentID (v
.getID());
291 const RelativeParallelogram
newBounds (v
.getBoundingBox());
292 const RelativePoint
newFontPoint (v
.getFontSizeControlPoint());
293 const Colour
newColour (v
.getColour());
294 const Justification
newJustification (v
.getJustification());
295 const String
newText (v
.getText());
296 const Font
newFont (v
.getFont());
298 if (text
!= newText
|| font
!= newFont
|| justification
!= newJustification
299 || colour
!= newColour
|| bounds
!= newBounds
|| newFontPoint
!= fontSizeControlPoint
)
301 setBoundingBox (newBounds
);
302 setFontSizeControlPoint (newFontPoint
);
303 setColour (newColour
);
304 setFont (newFont
, false);
305 setJustification (newJustification
);
310 ValueTree
DrawableText::createValueTree (ComponentBuilder::ImageProvider
*) const
312 ValueTree
tree (valueTreeType
);
313 ValueTreeWrapper
v (tree
);
315 v
.setID (getComponentID());
316 v
.setText (text
, nullptr);
317 v
.setFont (font
, nullptr);
318 v
.setJustification (justification
, nullptr);
319 v
.setColour (colour
, nullptr);
320 v
.setBoundingBox (bounds
, nullptr);
321 v
.setFontSizeControlPoint (fontSizeControlPoint
, nullptr);