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.

270 lines
11KB

  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 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. Glyph (const Glyph&) noexcept;
  116. Glyph& operator= (const Glyph&) noexcept;
  117. ~Glyph() noexcept;
  118. /** The code number of this glyph. */
  119. int glyphCode;
  120. /** The glyph's anchor point - this is relative to the line's origin.
  121. @see TextLayout::Line::lineOrigin
  122. */
  123. Point<float> anchor;
  124. float width;
  125. private:
  126. JUCE_LEAK_DETECTOR (Glyph)
  127. };
  128. //==============================================================================
  129. /** A sequence of glyphs with a common font and colour. */
  130. class JUCE_API Run
  131. {
  132. public:
  133. Run() noexcept;
  134. Run (const Run&);
  135. Run (Range<int> stringRange, int numGlyphsToPreallocate);
  136. ~Run() noexcept;
  137. /** Returns the X position range which contains all the glyphs in this run. */
  138. Range<float> getRunBoundsX() const noexcept;
  139. Font font; /**< The run's font. */
  140. Colour colour; /**< The run's colour. */
  141. Array<Glyph> glyphs; /**< The glyphs in this run. */
  142. Range<int> stringRange; /**< The character range that this run represents in the
  143. original string that was used to create it. */
  144. private:
  145. Run& operator= (const Run&);
  146. JUCE_LEAK_DETECTOR (Run)
  147. };
  148. //==============================================================================
  149. /** A line containing a sequence of glyph-runs. */
  150. class JUCE_API Line
  151. {
  152. public:
  153. Line() noexcept;
  154. Line (const Line&);
  155. Line (Range<int> stringRange, Point<float> lineOrigin,
  156. float ascent, float descent, float leading, int numRunsToPreallocate);
  157. ~Line() noexcept;
  158. /** Returns the X position range which contains all the glyphs in this line. */
  159. Range<float> getLineBoundsX() const noexcept;
  160. /** Returns the Y position range which contains all the glyphs in this line. */
  161. Range<float> getLineBoundsY() const noexcept;
  162. /** Returns the smallest rectangle which contains all the glyphs in this line. */
  163. Rectangle<float> getLineBounds() const noexcept;
  164. OwnedArray<Run> runs; /**< The glyph-runs in this line. */
  165. Range<int> stringRange; /**< The character range that this line represents in the
  166. original string that was used to create it. */
  167. Point<float> lineOrigin; /**< The line's baseline origin. */
  168. float ascent, descent, leading;
  169. private:
  170. Line& operator= (const Line&);
  171. JUCE_LEAK_DETECTOR (Line)
  172. };
  173. //==============================================================================
  174. /** Returns the maximum width of the content. */
  175. float getWidth() const noexcept { return width; }
  176. /** Returns the maximum height of the content. */
  177. float getHeight() const noexcept { return height; }
  178. /** Returns the number of lines in the layout. */
  179. int getNumLines() const noexcept { return lines.size(); }
  180. /** Returns one of the lines. */
  181. Line& getLine (int index) const noexcept;
  182. /** Adds a line to the layout. The layout will take ownership of this line object
  183. and will delete it when it is no longer needed. */
  184. void addLine (std::unique_ptr<Line>);
  185. /** Pre-allocates space for the specified number of lines. */
  186. void ensureStorageAllocated (int numLinesNeeded);
  187. using iterator = DereferencingIterator< Line* const*>;
  188. using const_iterator = DereferencingIterator<const Line* const*>;
  189. /** Returns an iterator over the lines of content */
  190. iterator begin() { return iterator (lines.begin()); }
  191. const_iterator begin() const { return const_iterator (lines.begin()); }
  192. const_iterator cbegin() const { return const_iterator (lines.begin()); }
  193. /** Returns an iterator over the lines of content */
  194. iterator end() { return iterator (lines.end()); }
  195. const_iterator end() const { return const_iterator (lines.end()); }
  196. const_iterator cend() const { return const_iterator (lines.end()); }
  197. /** If you modify the TextLayout after creating it, call this to compute
  198. the new dimensions of the content.
  199. */
  200. void recalculateSize();
  201. private:
  202. OwnedArray<Line> lines;
  203. float width, height;
  204. Justification justification;
  205. void createStandardLayout (const AttributedString&);
  206. bool createNativeLayout (const AttributedString&);
  207. JUCE_LEAK_DETECTOR (TextLayout)
  208. };
  209. } // namespace juce