Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.3KB

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