juce_DrawableText.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. DrawableText::DrawableText()
  22. : colour (Colours::black),
  23. justification (Justification::centredLeft)
  24. {
  25. setBoundingBox (Parallelogram<float> ({ 0.0f, 0.0f, 50.0f, 20.0f }));
  26. setFont (Font (15.0f), true);
  27. }
  28. DrawableText::DrawableText (const DrawableText& other)
  29. : Drawable (other),
  30. bounds (other.bounds),
  31. fontHeight (other.fontHeight),
  32. fontHScale (other.fontHScale),
  33. font (other.font),
  34. text (other.text),
  35. colour (other.colour),
  36. justification (other.justification)
  37. {
  38. refreshBounds();
  39. }
  40. DrawableText::~DrawableText()
  41. {
  42. }
  43. std::unique_ptr<Drawable> DrawableText::createCopy() const
  44. {
  45. return std::make_unique<DrawableText> (*this);
  46. }
  47. //==============================================================================
  48. void DrawableText::setText (const String& newText)
  49. {
  50. if (text != newText)
  51. {
  52. text = newText;
  53. refreshBounds();
  54. }
  55. }
  56. void DrawableText::setColour (Colour newColour)
  57. {
  58. if (colour != newColour)
  59. {
  60. colour = newColour;
  61. repaint();
  62. }
  63. }
  64. void DrawableText::setFont (const Font& newFont, bool applySizeAndScale)
  65. {
  66. if (font != newFont)
  67. {
  68. font = newFont;
  69. if (applySizeAndScale)
  70. {
  71. fontHeight = font.getHeight();
  72. fontHScale = font.getHorizontalScale();
  73. }
  74. refreshBounds();
  75. }
  76. }
  77. void DrawableText::setJustification (Justification newJustification)
  78. {
  79. justification = newJustification;
  80. repaint();
  81. }
  82. void DrawableText::setBoundingBox (Parallelogram<float> newBounds)
  83. {
  84. if (bounds != newBounds)
  85. {
  86. bounds = newBounds;
  87. refreshBounds();
  88. }
  89. }
  90. void DrawableText::setFontHeight (float newHeight)
  91. {
  92. if (fontHeight != newHeight)
  93. {
  94. fontHeight = newHeight;
  95. refreshBounds();
  96. }
  97. }
  98. void DrawableText::setFontHorizontalScale (float newScale)
  99. {
  100. if (fontHScale != newScale)
  101. {
  102. fontHScale = newScale;
  103. refreshBounds();
  104. }
  105. }
  106. void DrawableText::refreshBounds()
  107. {
  108. auto w = bounds.getWidth();
  109. auto h = bounds.getHeight();
  110. auto height = jlimit (0.01f, jmax (0.01f, h), fontHeight);
  111. auto hscale = jlimit (0.01f, jmax (0.01f, w), fontHScale);
  112. scaledFont = font;
  113. scaledFont.setHeight (height);
  114. scaledFont.setHorizontalScale (hscale);
  115. setBoundsToEnclose (getDrawableBounds());
  116. repaint();
  117. }
  118. //==============================================================================
  119. Rectangle<int> DrawableText::getTextArea (float w, float h) const
  120. {
  121. return Rectangle<float> (w, h).getSmallestIntegerContainer();
  122. }
  123. AffineTransform DrawableText::getTextTransform (float w, float h) const
  124. {
  125. return AffineTransform::fromTargetPoints (Point<float>(), bounds.topLeft,
  126. Point<float> (w, 0), bounds.topRight,
  127. Point<float> (0, h), bounds.bottomLeft);
  128. }
  129. void DrawableText::paint (Graphics& g)
  130. {
  131. transformContextToCorrectOrigin (g);
  132. auto w = bounds.getWidth();
  133. auto h = bounds.getHeight();
  134. g.addTransform (getTextTransform (w, h));
  135. g.setFont (scaledFont);
  136. g.setColour (colour);
  137. g.drawFittedText (text, getTextArea (w, h), justification, 0x100000);
  138. }
  139. Rectangle<float> DrawableText::getDrawableBounds() const
  140. {
  141. return bounds.getBoundingBox();
  142. }
  143. Path DrawableText::getOutlineAsPath() const
  144. {
  145. auto w = bounds.getWidth();
  146. auto h = bounds.getHeight();
  147. auto area = getTextArea (w, h).toFloat();
  148. GlyphArrangement arr;
  149. arr.addFittedText (scaledFont, text,
  150. area.getX(), area.getY(),
  151. area.getWidth(), area.getHeight(),
  152. justification,
  153. 0x100000);
  154. Path pathOfAllGlyphs;
  155. for (auto& glyph : arr)
  156. {
  157. Path gylphPath;
  158. glyph.createPath (gylphPath);
  159. pathOfAllGlyphs.addPath (gylphPath);
  160. }
  161. pathOfAllGlyphs.applyTransform (getTextTransform (w, h).followedBy (getTransform()));
  162. return pathOfAllGlyphs;
  163. }
  164. bool DrawableText::replaceColour (Colour originalColour, Colour replacementColour)
  165. {
  166. if (colour != originalColour)
  167. return false;
  168. setColour (replacementColour);
  169. return true;
  170. }
  171. } // namespace juce