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_TextLayout.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Pre-formatted piece of text, which may contain multiple fonts and colours.
  23. A TextLayout is created from an AttributedString, and once created can be
  24. quickly drawn into a Graphics context.
  25. @see AttributedString
  26. @tags{Graphics}
  27. */
  28. class JUCE_API TextLayout final
  29. {
  30. private:
  31. template <typename Iterator>
  32. class DereferencingIterator
  33. {
  34. public:
  35. using value_type = typename std::remove_reference<decltype(**std::declval<Iterator>())>::type;
  36. using difference_type = typename std::iterator_traits<Iterator>::difference_type;
  37. using pointer = value_type*;
  38. using reference = value_type&;
  39. using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;
  40. explicit DereferencingIterator (Iterator in) : iterator (std::move (in)) {}
  41. DereferencingIterator& operator+= (difference_type distance)
  42. {
  43. iterator += distance;
  44. return *this;
  45. }
  46. friend DereferencingIterator operator+ (DereferencingIterator i, difference_type d) { return i += d; }
  47. friend DereferencingIterator operator+ (difference_type d, DereferencingIterator i) { return i += d; }
  48. DereferencingIterator& operator-= (difference_type distance)
  49. {
  50. iterator -= distance;
  51. return *this;
  52. }
  53. friend DereferencingIterator operator- (DereferencingIterator i, difference_type d) { return i -= d; }
  54. friend difference_type operator- (DereferencingIterator a, DereferencingIterator b) { return a.iterator - b.iterator; }
  55. reference operator[] (difference_type d) const { return *iterator[d]; }
  56. friend bool operator< (DereferencingIterator a, DereferencingIterator b) { return a.iterator < b.iterator; }
  57. friend bool operator<= (DereferencingIterator a, DereferencingIterator b) { return a.iterator <= b.iterator; }
  58. friend bool operator> (DereferencingIterator a, DereferencingIterator b) { return a.iterator > b.iterator; }
  59. friend bool operator>= (DereferencingIterator a, DereferencingIterator b) { return a.iterator >= b.iterator; }
  60. friend bool operator== (DereferencingIterator a, DereferencingIterator b) { return a.iterator == b.iterator; }
  61. friend bool operator!= (DereferencingIterator a, DereferencingIterator b) { return a.iterator != b.iterator; }
  62. DereferencingIterator& operator++() { ++iterator; return *this; }
  63. DereferencingIterator& operator--() { --iterator; return *this; }
  64. DereferencingIterator operator++ (int) const { DereferencingIterator copy (*this); ++(*this); return copy; }
  65. DereferencingIterator operator-- (int) const { DereferencingIterator copy (*this); --(*this); return copy; }
  66. reference operator* () const { return **iterator; }
  67. pointer operator->() const { return *iterator; }
  68. private:
  69. Iterator iterator;
  70. };
  71. public:
  72. /** Creates an empty layout.
  73. Having created a TextLayout, you can populate it using createLayout() or
  74. createLayoutWithBalancedLineLengths().
  75. */
  76. TextLayout();
  77. TextLayout (const TextLayout&);
  78. TextLayout& operator= (const TextLayout&);
  79. TextLayout (TextLayout&&) noexcept;
  80. TextLayout& operator= (TextLayout&&) noexcept;
  81. /** Destructor. */
  82. ~TextLayout();
  83. //==============================================================================
  84. /** Creates a layout from the given attributed string.
  85. This will replace any data that is currently stored in the layout.
  86. */
  87. void createLayout (const AttributedString&, float maxWidth);
  88. /** Creates a layout from the given attributed string, given some size constraints.
  89. This will replace any data that is currently stored in the layout.
  90. */
  91. void createLayout (const AttributedString&, float maxWidth, float maxHeight);
  92. /** Creates a layout, attempting to choose a width which results in lines
  93. of a similar length.
  94. This will be slower than the normal createLayout method, but produces a
  95. tidier result.
  96. */
  97. void createLayoutWithBalancedLineLengths (const AttributedString&, float maxWidth);
  98. /** Creates a layout, attempting to choose a width which results in lines
  99. of a similar length.
  100. This will be slower than the normal createLayout method, but produces a
  101. tidier result.
  102. */
  103. void createLayoutWithBalancedLineLengths (const AttributedString&, float maxWidth, float maxHeight);
  104. /** Draws the layout within the specified area.
  105. The position of the text within the rectangle is controlled by the justification
  106. flags set in the original AttributedString that was used to create this layout.
  107. */
  108. void draw (Graphics&, Rectangle<float> area) const;
  109. //==============================================================================
  110. /** A positioned glyph. */
  111. class JUCE_API Glyph
  112. {
  113. public:
  114. Glyph (int glyphCode, Point<float> anchor, float width) noexcept;
  115. /** The code number of this glyph. */
  116. int glyphCode;
  117. /** The glyph's anchor point - this is relative to the line's origin.
  118. @see TextLayout::Line::lineOrigin
  119. */
  120. Point<float> anchor;
  121. float width;
  122. private:
  123. JUCE_LEAK_DETECTOR (Glyph)
  124. };
  125. //==============================================================================
  126. /** A sequence of glyphs with a common font and colour. */
  127. class JUCE_API Run
  128. {
  129. public:
  130. Run() = default;
  131. Run (Range<int> stringRange, int numGlyphsToPreallocate);
  132. /** Returns the X position range which contains all the glyphs in this run. */
  133. Range<float> getRunBoundsX() const noexcept;
  134. Font font; /**< The run's font. */
  135. Colour colour { 0xff000000 }; /**< The run's colour. */
  136. Array<Glyph> glyphs; /**< The glyphs in this run. */
  137. Range<int> stringRange; /**< The character range that this run represents in the
  138. original string that was used to create it. */
  139. private:
  140. JUCE_LEAK_DETECTOR (Run)
  141. };
  142. //==============================================================================
  143. /** A line containing a sequence of glyph-runs. */
  144. class JUCE_API Line
  145. {
  146. public:
  147. Line() = default;
  148. Line (Range<int> stringRange, Point<float> lineOrigin,
  149. float ascent, float descent, float leading, int numRunsToPreallocate);
  150. Line (const Line&);
  151. Line& operator= (const Line&);
  152. Line (Line&&) noexcept = default;
  153. Line& operator= (Line&&) noexcept = default;
  154. ~Line() noexcept = default;
  155. /** Returns the X position range which contains all the glyphs in this line. */
  156. Range<float> getLineBoundsX() const noexcept;
  157. /** Returns the Y position range which contains all the glyphs in this line. */
  158. Range<float> getLineBoundsY() const noexcept;
  159. /** Returns the smallest rectangle which contains all the glyphs in this line. */
  160. Rectangle<float> getLineBounds() const noexcept;
  161. void swap (Line& other) noexcept;
  162. OwnedArray<Run> runs; /**< The glyph-runs in this line. */
  163. Range<int> stringRange; /**< The character range that this line represents in the
  164. original string that was used to create it. */
  165. Point<float> lineOrigin; /**< The line's baseline origin. */
  166. float ascent = 0.0f, descent = 0.0f, leading = 0.0f;
  167. private:
  168. JUCE_LEAK_DETECTOR (Line)
  169. };
  170. //==============================================================================
  171. /** Returns the maximum width of the content. */
  172. float getWidth() const noexcept { return width; }
  173. /** Returns the maximum height of the content. */
  174. float getHeight() const noexcept { return height; }
  175. /** Returns the number of lines in the layout. */
  176. int getNumLines() const noexcept { return lines.size(); }
  177. /** Returns one of the lines. */
  178. Line& getLine (int index) const noexcept;
  179. /** Adds a line to the layout. The layout will take ownership of this line object
  180. and will delete it when it is no longer needed. */
  181. void addLine (std::unique_ptr<Line>);
  182. /** Pre-allocates space for the specified number of lines. */
  183. void ensureStorageAllocated (int numLinesNeeded);
  184. using iterator = DereferencingIterator< Line* const*>;
  185. using const_iterator = DereferencingIterator<const Line* const*>;
  186. /** Returns an iterator over the lines of content */
  187. iterator begin() { return iterator (lines.begin()); }
  188. const_iterator begin() const { return const_iterator (lines.begin()); }
  189. const_iterator cbegin() const { return const_iterator (lines.begin()); }
  190. /** Returns an iterator over the lines of content */
  191. iterator end() { return iterator (lines.end()); }
  192. const_iterator end() const { return const_iterator (lines.end()); }
  193. const_iterator cend() const { return const_iterator (lines.end()); }
  194. /** If you modify the TextLayout after creating it, call this to compute
  195. the new dimensions of the content.
  196. */
  197. void recalculateSize();
  198. private:
  199. OwnedArray<Line> lines;
  200. float width, height;
  201. Justification justification;
  202. void createStandardLayout (const AttributedString&);
  203. bool createNativeLayout (const AttributedString&);
  204. JUCE_LEAK_DETECTOR (TextLayout)
  205. };
  206. } // namespace juce