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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. Invariants:
  27. - Every character in the string is a member of exactly one attribute.
  28. - Attributes are sorted such that the range-end of attribute 'i' is equal to the
  29. range-begin of attribute 'i + 1'.
  30. @see TextLayout
  31. @tags{Graphics}
  32. */
  33. class JUCE_API AttributedString
  34. {
  35. public:
  36. /** Creates an empty attributed string. */
  37. AttributedString() = default;
  38. /** Creates an attributed string with the given text. */
  39. explicit AttributedString (const String& newString) { setText (newString); }
  40. AttributedString (const AttributedString&) = default;
  41. AttributedString& operator= (const AttributedString&) = default;
  42. AttributedString (AttributedString&&) noexcept = default;
  43. AttributedString& operator= (AttributedString&&) noexcept = default;
  44. //==============================================================================
  45. /** Returns the complete text of this attributed string. */
  46. const String& getText() const noexcept { return text; }
  47. /** Replaces all the text.
  48. This will change the text, but won't affect any of the colour or font attributes
  49. that have been added.
  50. */
  51. void setText (const String& newText);
  52. /** Appends some text (with a default font and colour). */
  53. void append (const String& textToAppend);
  54. /** Appends some text, with a specified font, and the default colour (black). */
  55. void append (const String& textToAppend, const Font& font);
  56. /** Appends some text, with a specified colour, and the default font. */
  57. void append (const String& textToAppend, Colour colour);
  58. /** Appends some text, with a specified font and colour. */
  59. void append (const String& textToAppend, const Font& font, Colour colour);
  60. /** Appends another AttributedString to this one.
  61. Note that this will only append the text, fonts, and colours - it won't copy any
  62. other properties such as justification, line-spacing, etc from the other object.
  63. */
  64. void append (const AttributedString& other);
  65. /** Resets the string, clearing all text and attributes.
  66. Note that this won't affect global settings like the justification type,
  67. word-wrap mode, etc.
  68. */
  69. void clear();
  70. //==============================================================================
  71. /** Draws this string within the given area.
  72. The layout of the string within the rectangle is controlled by the justification
  73. value passed to setJustification().
  74. */
  75. void draw (Graphics& g, const Rectangle<float>& area) const;
  76. //==============================================================================
  77. /** Returns the justification that should be used for laying-out the text.
  78. This may include both vertical and horizontal flags.
  79. */
  80. Justification getJustification() const noexcept { return justification; }
  81. /** Sets the justification that should be used for laying-out the text.
  82. This may include both vertical and horizontal flags.
  83. */
  84. void setJustification (Justification newJustification) noexcept;
  85. //==============================================================================
  86. /** Types of word-wrap behaviour.
  87. @see getWordWrap, setWordWrap
  88. */
  89. enum WordWrap
  90. {
  91. none, /**< No word-wrapping: lines extend indefinitely. */
  92. byWord, /**< Lines are wrapped on a word boundary. */
  93. byChar, /**< Lines are wrapped on a character boundary. */
  94. };
  95. /** Returns the word-wrapping behaviour. */
  96. WordWrap getWordWrap() const noexcept { return wordWrap; }
  97. /** Sets the word-wrapping behaviour. */
  98. void setWordWrap (WordWrap newWordWrap) noexcept;
  99. //==============================================================================
  100. /** Types of reading direction that can be used.
  101. @see getReadingDirection, setReadingDirection
  102. */
  103. enum ReadingDirection
  104. {
  105. natural,
  106. leftToRight,
  107. rightToLeft,
  108. };
  109. /** Returns the reading direction for the text. */
  110. ReadingDirection getReadingDirection() const noexcept { return readingDirection; }
  111. /** Sets the reading direction that should be used for the text. */
  112. void setReadingDirection (ReadingDirection newReadingDirection) noexcept;
  113. //==============================================================================
  114. /** Returns the extra line-spacing distance. */
  115. float getLineSpacing() const noexcept { return lineSpacing; }
  116. /** Sets an extra line-spacing distance. */
  117. void setLineSpacing (float newLineSpacing) noexcept;
  118. //==============================================================================
  119. /** An attribute that has been applied to a range of characters in an AttributedString. */
  120. class JUCE_API Attribute
  121. {
  122. public:
  123. Attribute() = default;
  124. Attribute (const Attribute&) = default;
  125. Attribute& operator= (const Attribute&) = default;
  126. Attribute (Attribute&&) noexcept = default;
  127. Attribute& operator= (Attribute&&) noexcept = default;
  128. /** Creates an attribute that specifies the font and colour for a range of characters. */
  129. Attribute (Range<int> range, const Font& font, Colour colour) noexcept;
  130. /** The range of characters to which this attribute will be applied. */
  131. Range<int> range;
  132. /** The font for this range of characters. */
  133. Font font;
  134. /** The colour for this range of characters. */
  135. Colour colour { 0xff000000 };
  136. private:
  137. JUCE_LEAK_DETECTOR (Attribute)
  138. };
  139. /** Returns the number of attributes that have been added to this string. */
  140. int getNumAttributes() const noexcept { return attributes.size(); }
  141. /** Returns one of the string's attributes.
  142. The index provided must be less than getNumAttributes(), and >= 0.
  143. */
  144. const Attribute& getAttribute (int index) const noexcept { return attributes.getReference (index); }
  145. //==============================================================================
  146. /** Adds a colour attribute for the specified range. */
  147. void setColour (Range<int> range, Colour colour);
  148. /** Removes all existing colour attributes, and applies this colour to the whole string. */
  149. void setColour (Colour colour);
  150. /** Adds a font attribute for the specified range. */
  151. void setFont (Range<int> range, const Font& font);
  152. /** Removes all existing font attributes, and applies this font to the whole string. */
  153. void setFont (const Font& font);
  154. private:
  155. String text;
  156. float lineSpacing = 0.0f;
  157. Justification justification = Justification::left;
  158. WordWrap wordWrap = AttributedString::byWord;
  159. ReadingDirection readingDirection = AttributedString::natural;
  160. Array<Attribute> attributes;
  161. JUCE_LEAK_DETECTOR (AttributedString)
  162. };
  163. } // namespace juce