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.

199 lines
7.8KB

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