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.

juce_AttributedString.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. AttributedString::Attribute::Attribute (Range<int> range_, Colour colour_)
  18. : range (range_), colour (new Colour (colour_))
  19. {
  20. }
  21. AttributedString::Attribute::Attribute (Range<int> range_, const Font& font_)
  22. : range (range_), font (new Font (font_))
  23. {
  24. }
  25. AttributedString::Attribute::Attribute (const Attribute& other)
  26. : range (other.range),
  27. font (other.font.createCopy()),
  28. colour (other.colour.createCopy())
  29. {
  30. }
  31. AttributedString::Attribute::Attribute (const Attribute& other, const int offset)
  32. : range (other.range + offset),
  33. font (other.font.createCopy()),
  34. colour (other.colour.createCopy())
  35. {
  36. }
  37. AttributedString::Attribute::~Attribute() {}
  38. //==============================================================================
  39. AttributedString::AttributedString()
  40. : lineSpacing (0.0f),
  41. justification (Justification::left),
  42. wordWrap (AttributedString::byWord),
  43. readingDirection (AttributedString::natural)
  44. {
  45. }
  46. AttributedString::AttributedString (const String& newString)
  47. : text (newString),
  48. lineSpacing (0.0f),
  49. justification (Justification::left),
  50. wordWrap (AttributedString::byWord),
  51. readingDirection (AttributedString::natural)
  52. {
  53. }
  54. AttributedString::AttributedString (const AttributedString& other)
  55. : text (other.text),
  56. lineSpacing (other.lineSpacing),
  57. justification (other.justification),
  58. wordWrap (other.wordWrap),
  59. readingDirection (other.readingDirection)
  60. {
  61. attributes.addCopiesOf (other.attributes);
  62. }
  63. AttributedString& AttributedString::operator= (const AttributedString& other)
  64. {
  65. if (this != &other)
  66. {
  67. text = other.text;
  68. lineSpacing = other.lineSpacing;
  69. justification = other.justification;
  70. wordWrap = other.wordWrap;
  71. readingDirection = other.readingDirection;
  72. attributes.clear();
  73. attributes.addCopiesOf (other.attributes);
  74. }
  75. return *this;
  76. }
  77. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  78. AttributedString::AttributedString (AttributedString&& other) noexcept
  79. : text (static_cast <String&&> (other.text)),
  80. lineSpacing (other.lineSpacing),
  81. justification (other.justification),
  82. wordWrap (other.wordWrap),
  83. readingDirection (other.readingDirection),
  84. attributes (static_cast <OwnedArray<Attribute>&&> (other.attributes))
  85. {
  86. }
  87. AttributedString& AttributedString::operator= (AttributedString&& other) noexcept
  88. {
  89. text = static_cast <String&&> (other.text);
  90. lineSpacing = other.lineSpacing;
  91. justification = other.justification;
  92. wordWrap = other.wordWrap;
  93. readingDirection = other.readingDirection;
  94. attributes = static_cast <OwnedArray<Attribute>&&> (other.attributes);
  95. return *this;
  96. }
  97. #endif
  98. AttributedString::~AttributedString() {}
  99. void AttributedString::setText (const String& other)
  100. {
  101. text = other;
  102. }
  103. void AttributedString::append (const String& textToAppend)
  104. {
  105. text += textToAppend;
  106. }
  107. void AttributedString::append (const String& textToAppend, const Font& font)
  108. {
  109. const int oldLength = text.length();
  110. const int newLength = textToAppend.length();
  111. text += textToAppend;
  112. setFont (Range<int> (oldLength, oldLength + newLength), font);
  113. }
  114. void AttributedString::append (const String& textToAppend, Colour colour)
  115. {
  116. const int oldLength = text.length();
  117. const int newLength = textToAppend.length();
  118. text += textToAppend;
  119. setColour (Range<int> (oldLength, oldLength + newLength), colour);
  120. }
  121. void AttributedString::append (const String& textToAppend, const Font& font, Colour colour)
  122. {
  123. const int oldLength = text.length();
  124. const int newLength = textToAppend.length();
  125. text += textToAppend;
  126. setFont (Range<int> (oldLength, oldLength + newLength), font);
  127. setColour (Range<int> (oldLength, oldLength + newLength), colour);
  128. }
  129. void AttributedString::append (const AttributedString& other)
  130. {
  131. const int originalLength = text.length();
  132. text += other.text;
  133. for (int i = 0; i < other.attributes.size(); ++i)
  134. attributes.add (new Attribute (*other.attributes.getUnchecked(i), originalLength));
  135. }
  136. void AttributedString::clear()
  137. {
  138. text = String::empty;
  139. attributes.clear();
  140. }
  141. void AttributedString::setJustification (Justification newJustification) noexcept
  142. {
  143. justification = newJustification;
  144. }
  145. void AttributedString::setWordWrap (WordWrap newWordWrap) noexcept
  146. {
  147. wordWrap = newWordWrap;
  148. }
  149. void AttributedString::setReadingDirection (ReadingDirection newReadingDirection) noexcept
  150. {
  151. readingDirection = newReadingDirection;
  152. }
  153. void AttributedString::setLineSpacing (const float newLineSpacing) noexcept
  154. {
  155. lineSpacing = newLineSpacing;
  156. }
  157. void AttributedString::setColour (Range<int> range, Colour colour)
  158. {
  159. attributes.add (new Attribute (range, colour));
  160. }
  161. void AttributedString::setColour (Colour colour)
  162. {
  163. for (int i = attributes.size(); --i >= 0;)
  164. if (attributes.getUnchecked(i)->getColour() != nullptr)
  165. attributes.remove (i);
  166. setColour (Range<int> (0, text.length()), colour);
  167. }
  168. void AttributedString::setFont (Range<int> range, const Font& font)
  169. {
  170. attributes.add (new Attribute (range, font));
  171. }
  172. void AttributedString::setFont (const Font& font)
  173. {
  174. for (int i = attributes.size(); --i >= 0;)
  175. if (attributes.getUnchecked(i)->getFont() != nullptr)
  176. attributes.remove (i);
  177. setFont (Range<int> (0, text.length()), font);
  178. }
  179. void AttributedString::draw (Graphics& g, const Rectangle<float>& area) const
  180. {
  181. if (text.isNotEmpty() && g.clipRegionIntersects (area.getSmallestIntegerContainer()))
  182. {
  183. if (! g.getInternalContext().drawTextLayout (*this, area))
  184. {
  185. TextLayout layout;
  186. layout.createLayout (*this, area.getWidth());
  187. layout.draw (g, area);
  188. }
  189. }
  190. }