The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

234 lines
7.0KB

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