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.

205 lines
5.0KB

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