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.

218 lines
6.6KB

  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 (const Range<int>& range_, const Colour& colour_)
  19. : range (range_), colour (new Colour (colour_))
  20. {
  21. }
  22. AttributedString::Attribute::Attribute (const 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() {}
  33. //==============================================================================
  34. AttributedString::AttributedString()
  35. : lineSpacing (0.0f),
  36. justification (Justification::left),
  37. wordWrap (AttributedString::byWord),
  38. readingDirection (AttributedString::natural)
  39. {
  40. }
  41. AttributedString::AttributedString (const String& newString)
  42. : text (newString),
  43. lineSpacing (0.0f),
  44. justification (Justification::left),
  45. wordWrap (AttributedString::byWord),
  46. readingDirection (AttributedString::natural)
  47. {
  48. }
  49. AttributedString::AttributedString (const AttributedString& other)
  50. : text (other.text),
  51. lineSpacing (other.lineSpacing),
  52. justification (other.justification),
  53. wordWrap (other.wordWrap),
  54. readingDirection (other.readingDirection)
  55. {
  56. attributes.addCopiesOf (other.attributes);
  57. }
  58. AttributedString& AttributedString::operator= (const AttributedString& other)
  59. {
  60. if (this != &other)
  61. {
  62. text = other.text;
  63. lineSpacing = other.lineSpacing;
  64. justification = other.justification;
  65. wordWrap = other.wordWrap;
  66. readingDirection = other.readingDirection;
  67. attributes.clear();
  68. attributes.addCopiesOf (other.attributes);
  69. }
  70. return *this;
  71. }
  72. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  73. AttributedString::AttributedString (AttributedString&& other) noexcept
  74. : text (static_cast <String&&> (other.text)),
  75. lineSpacing (other.lineSpacing),
  76. justification (other.justification),
  77. wordWrap (other.wordWrap),
  78. readingDirection (other.readingDirection),
  79. attributes (static_cast <OwnedArray<Attribute>&&> (other.attributes))
  80. {
  81. }
  82. AttributedString& AttributedString::operator= (AttributedString&& other) noexcept
  83. {
  84. text = static_cast <String&&> (other.text);
  85. lineSpacing = other.lineSpacing;
  86. justification = other.justification;
  87. wordWrap = other.wordWrap;
  88. readingDirection = other.readingDirection;
  89. attributes = static_cast <OwnedArray<Attribute>&&> (other.attributes);
  90. return *this;
  91. }
  92. #endif
  93. AttributedString::~AttributedString() {}
  94. void AttributedString::setText (const String& other)
  95. {
  96. text = other;
  97. }
  98. void AttributedString::append (const String& textToAppend)
  99. {
  100. text += textToAppend;
  101. }
  102. void AttributedString::append (const String& textToAppend, const Font& font)
  103. {
  104. const int oldLength = text.length();
  105. const int newLength = textToAppend.length();
  106. text += textToAppend;
  107. setFont (Range<int> (oldLength, oldLength + newLength), font);
  108. }
  109. void AttributedString::append (const String& textToAppend, const Colour& colour)
  110. {
  111. const int oldLength = text.length();
  112. const int newLength = textToAppend.length();
  113. text += textToAppend;
  114. setColour (Range<int> (oldLength, oldLength + newLength), colour);
  115. }
  116. void AttributedString::append (const String& textToAppend, const Font& font, const Colour& colour)
  117. {
  118. const int oldLength = text.length();
  119. const int newLength = textToAppend.length();
  120. text += textToAppend;
  121. setFont (Range<int> (oldLength, oldLength + newLength), font);
  122. setColour (Range<int> (oldLength, oldLength + newLength), colour);
  123. }
  124. void AttributedString::clear()
  125. {
  126. text = String::empty;
  127. attributes.clear();
  128. }
  129. void AttributedString::setJustification (const Justification& newJustification) noexcept
  130. {
  131. justification = newJustification;
  132. }
  133. void AttributedString::setWordWrap (WordWrap newWordWrap) noexcept
  134. {
  135. wordWrap = newWordWrap;
  136. }
  137. void AttributedString::setReadingDirection (ReadingDirection newReadingDirection) noexcept
  138. {
  139. readingDirection = newReadingDirection;
  140. }
  141. void AttributedString::setLineSpacing (const float newLineSpacing) noexcept
  142. {
  143. lineSpacing = newLineSpacing;
  144. }
  145. void AttributedString::setColour (const Range<int>& range, const Colour& colour)
  146. {
  147. attributes.add (new Attribute (range, colour));
  148. }
  149. void AttributedString::setColour (const Colour& colour)
  150. {
  151. for (int i = attributes.size(); --i >= 0;)
  152. if (attributes.getUnchecked(i)->getColour() != nullptr)
  153. attributes.remove (i);
  154. setColour (Range<int> (0, text.length()), colour);
  155. }
  156. void AttributedString::setFont (const Range<int>& range, const Font& font)
  157. {
  158. attributes.add (new Attribute (range, font));
  159. }
  160. void AttributedString::setFont (const Font& font)
  161. {
  162. for (int i = attributes.size(); --i >= 0;)
  163. if (attributes.getUnchecked(i)->getFont() != nullptr)
  164. attributes.remove (i);
  165. setFont (Range<int> (0, text.length()), font);
  166. }
  167. void AttributedString::draw (Graphics& g, const Rectangle<float>& area) const
  168. {
  169. if (text.isNotEmpty() && g.clipRegionIntersects (area.getSmallestIntegerContainer()))
  170. {
  171. if (! g.getInternalContext()->drawTextLayout (*this, area))
  172. {
  173. TextLayout layout;
  174. layout.createLayout (*this, area.getWidth());
  175. layout.draw (g, area);
  176. }
  177. }
  178. }