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.

223 lines
6.7KB

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