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.h 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A text string with a set of colour/font settings that are associated with sub-ranges
  23. of the text.
  24. An attributed string lets you create a string with varied fonts, colours, word-wrapping,
  25. layout, etc., and draw it using AttributedString::draw().
  26. @see TextLayout
  27. @tags{Graphics}
  28. */
  29. class JUCE_API AttributedString
  30. {
  31. public:
  32. /** Creates an empty attributed string. */
  33. AttributedString() = default;
  34. /** Creates an attributed string with the given text. */
  35. explicit AttributedString (const String& newString) { setText (newString); }
  36. AttributedString (const AttributedString&) = default;
  37. AttributedString& operator= (const AttributedString&) = default;
  38. AttributedString (AttributedString&&) noexcept = default;
  39. AttributedString& operator= (AttributedString&&) noexcept = default;
  40. //==============================================================================
  41. /** Returns the complete text of this attributed string. */
  42. const String& getText() const noexcept { return text; }
  43. /** Replaces all the text.
  44. This will change the text, but won't affect any of the colour or font attributes
  45. that have been added.
  46. */
  47. void setText (const String& newText);
  48. /** Appends some text (with a default font and colour). */
  49. void append (const String& textToAppend);
  50. /** Appends some text, with a specified font, and the default colour (black). */
  51. void append (const String& textToAppend, const Font& font);
  52. /** Appends some text, with a specified colour, and the default font. */
  53. void append (const String& textToAppend, Colour colour);
  54. /** Appends some text, with a specified font and colour. */
  55. void append (const String& textToAppend, const Font& font, Colour colour);
  56. /** Appends another AttributedString to this one.
  57. Note that this will only append the text, fonts, and colours - it won't copy any
  58. other properties such as justification, line-spacing, etc from the other object.
  59. */
  60. void append (const AttributedString& other);
  61. /** Resets the string, clearing all text and attributes.
  62. Note that this won't affect global settings like the justification type,
  63. word-wrap mode, etc.
  64. */
  65. void clear();
  66. //==============================================================================
  67. /** Draws this string within the given area.
  68. The layout of the string within the rectangle is controlled by the justification
  69. value passed to setJustification().
  70. */
  71. void draw (Graphics& g, const Rectangle<float>& area) const;
  72. //==============================================================================
  73. /** Returns the justification that should be used for laying-out the text.
  74. This may include both vertical and horizontal flags.
  75. */
  76. Justification getJustification() const noexcept { return justification; }
  77. /** Sets the justification that should be used for laying-out the text.
  78. This may include both vertical and horizontal flags.
  79. */
  80. void setJustification (Justification newJustification) noexcept;
  81. //==============================================================================
  82. /** Types of word-wrap behaviour.
  83. @see getWordWrap, setWordWrap
  84. */
  85. enum WordWrap
  86. {
  87. none, /**< No word-wrapping: lines extend indefinitely. */
  88. byWord, /**< Lines are wrapped on a word boundary. */
  89. byChar, /**< Lines are wrapped on a character boundary. */
  90. };
  91. /** Returns the word-wrapping behaviour. */
  92. WordWrap getWordWrap() const noexcept { return wordWrap; }
  93. /** Sets the word-wrapping behaviour. */
  94. void setWordWrap (WordWrap newWordWrap) noexcept;
  95. //==============================================================================
  96. /** Types of reading direction that can be used.
  97. @see getReadingDirection, setReadingDirection
  98. */
  99. enum ReadingDirection
  100. {
  101. natural,
  102. leftToRight,
  103. rightToLeft,
  104. };
  105. /** Returns the reading direction for the text. */
  106. ReadingDirection getReadingDirection() const noexcept { return readingDirection; }
  107. /** Sets the reading direction that should be used for the text. */
  108. void setReadingDirection (ReadingDirection newReadingDirection) noexcept;
  109. //==============================================================================
  110. /** Returns the extra line-spacing distance. */
  111. float getLineSpacing() const noexcept { return lineSpacing; }
  112. /** Sets an extra line-spacing distance. */
  113. void setLineSpacing (float newLineSpacing) noexcept;
  114. //==============================================================================
  115. /** An attribute that has been applied to a range of characters in an AttributedString. */
  116. class JUCE_API Attribute
  117. {
  118. public:
  119. Attribute() = default;
  120. Attribute (const Attribute&) = default;
  121. Attribute& operator= (const Attribute&) = default;
  122. Attribute (Attribute&&) noexcept = default;
  123. Attribute& operator= (Attribute&&) noexcept = default;
  124. /** Creates an attribute that specifies the font and colour for a range of characters. */
  125. Attribute (Range<int> range, const Font& font, Colour colour) noexcept;
  126. /** The range of characters to which this attribute will be applied. */
  127. Range<int> range;
  128. /** The font for this range of characters. */
  129. Font font;
  130. /** The colour for this range of characters. */
  131. Colour colour { 0xff000000 };
  132. private:
  133. JUCE_LEAK_DETECTOR (Attribute)
  134. };
  135. /** Returns the number of attributes that have been added to this string. */
  136. int getNumAttributes() const noexcept { return attributes.size(); }
  137. /** Returns one of the string's attributes.
  138. The index provided must be less than getNumAttributes(), and >= 0.
  139. */
  140. const Attribute& getAttribute (int index) const noexcept { return attributes.getReference (index); }
  141. //==============================================================================
  142. /** Adds a colour attribute for the specified range. */
  143. void setColour (Range<int> range, Colour colour);
  144. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  145. void setColour (Colour colour);
  146. /** Adds a font attribute for the specified range. */
  147. void setFont (Range<int> range, const Font& font);
  148. /** Removes all existing font attributes, and applies this font to the whole string. */
  149. void setFont (const Font& font);
  150. private:
  151. String text;
  152. float lineSpacing = 0.0f;
  153. Justification justification = Justification::left;
  154. WordWrap wordWrap = AttributedString::byWord;
  155. ReadingDirection readingDirection = AttributedString::natural;
  156. Array<Attribute> attributes;
  157. JUCE_LEAK_DETECTOR (AttributedString)
  158. };
  159. } // namespace juce