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.

197 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_TEXTLAYOUT_H_INCLUDED
  18. #define JUCE_TEXTLAYOUT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A Pre-formatted piece of text, which may contain multiple fonts and colours.
  22. A TextLayout is created from an AttributedString, and once created can be
  23. quickly drawn into a Graphics context.
  24. @see AttributedString
  25. */
  26. class JUCE_API TextLayout
  27. {
  28. public:
  29. /** Creates an empty layout.
  30. Having created a TextLayout, you can populate it using createLayout() or
  31. createLayoutWithBalancedLineLengths().
  32. */
  33. TextLayout();
  34. TextLayout (const TextLayout&);
  35. TextLayout& operator= (const TextLayout&);
  36. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  37. TextLayout (TextLayout&&) noexcept;
  38. TextLayout& operator= (TextLayout&&) noexcept;
  39. #endif
  40. /** Destructor. */
  41. ~TextLayout();
  42. //==============================================================================
  43. /** Creates a layout from the given attributed string.
  44. This will replace any data that is currently stored in the layout.
  45. */
  46. void createLayout (const AttributedString&, float maxWidth);
  47. /** Creates a layout from the given attributed string, given some size constraints.
  48. This will replace any data that is currently stored in the layout.
  49. */
  50. void createLayout (const AttributedString&, float maxWidth, float maxHeight);
  51. /** Creates a layout, attempting to choose a width which results in lines
  52. of a similar length.
  53. This will be slower than the normal createLayout method, but produces a
  54. tidier result.
  55. */
  56. void createLayoutWithBalancedLineLengths (const AttributedString&, float maxWidth);
  57. /** Creates a layout, attempting to choose a width which results in lines
  58. of a similar length.
  59. This will be slower than the normal createLayout method, but produces a
  60. tidier result.
  61. */
  62. void createLayoutWithBalancedLineLengths (const AttributedString&, float maxWidth, float maxHeight);
  63. /** Draws the layout within the specified area.
  64. The position of the text within the rectangle is controlled by the justification
  65. flags set in the original AttributedString that was used to create this layout.
  66. */
  67. void draw (Graphics&, const Rectangle<float>& area) const;
  68. //==============================================================================
  69. /** A positioned glyph. */
  70. class JUCE_API Glyph
  71. {
  72. public:
  73. Glyph (int glyphCode, Point<float> anchor, float width) noexcept;
  74. Glyph (const Glyph&) noexcept;
  75. Glyph& operator= (const Glyph&) noexcept;
  76. ~Glyph() noexcept;
  77. /** The code number of this glyph. */
  78. int glyphCode;
  79. /** The glyph's anchor point - this is relative to the line's origin.
  80. @see TextLayout::Line::lineOrigin
  81. */
  82. Point<float> anchor;
  83. float width;
  84. private:
  85. JUCE_LEAK_DETECTOR (Glyph)
  86. };
  87. //==============================================================================
  88. /** A sequence of glyphs with a common font and colour. */
  89. class JUCE_API Run
  90. {
  91. public:
  92. Run() noexcept;
  93. Run (const Run&);
  94. Run (Range<int> stringRange, int numGlyphsToPreallocate);
  95. ~Run() noexcept;
  96. Font font; /**< The run's font. */
  97. Colour colour; /**< The run's colour. */
  98. Array<Glyph> glyphs; /**< The glyphs in this run. */
  99. Range<int> stringRange; /**< The character range that this run represents in the
  100. original string that was used to create it. */
  101. private:
  102. Run& operator= (const Run&);
  103. JUCE_LEAK_DETECTOR (Run)
  104. };
  105. //==============================================================================
  106. /** A line containing a sequence of glyph-runs. */
  107. class JUCE_API Line
  108. {
  109. public:
  110. Line() noexcept;
  111. Line (const Line&);
  112. Line (Range<int> stringRange, Point<float> lineOrigin,
  113. float ascent, float descent, float leading, int numRunsToPreallocate);
  114. ~Line() noexcept;
  115. /** Returns the X position range which contains all the glyphs in this line. */
  116. Range<float> getLineBoundsX() const noexcept;
  117. /** Returns the Y position range which contains all the glyphs in this line. */
  118. Range<float> getLineBoundsY() const noexcept;
  119. /** Returns the smallest rectangle which contains all the glyphs in this line. */
  120. Rectangle<float> getLineBounds() const noexcept;
  121. OwnedArray<Run> runs; /**< The glyph-runs in this line. */
  122. Range<int> stringRange; /**< The character range that this line represents in the
  123. original string that was used to create it. */
  124. Point<float> lineOrigin; /**< The line's baseline origin. */
  125. float ascent, descent, leading;
  126. private:
  127. Line& operator= (const Line&);
  128. JUCE_LEAK_DETECTOR (Line)
  129. };
  130. //==============================================================================
  131. /** Returns the maximum width of the content. */
  132. float getWidth() const noexcept { return width; }
  133. /** Returns the maximum height of the content. */
  134. float getHeight() const noexcept { return height; }
  135. /** Returns the number of lines in the layout. */
  136. int getNumLines() const noexcept { return lines.size(); }
  137. /** Returns one of the lines. */
  138. Line& getLine (int index) const;
  139. /** Adds a line to the layout. The layout will take ownership of this line object
  140. and will delete it when it is no longer needed. */
  141. void addLine (Line*);
  142. /** Pre-allocates space for the specified number of lines. */
  143. void ensureStorageAllocated (int numLinesNeeded);
  144. private:
  145. OwnedArray<Line> lines;
  146. float width, height;
  147. Justification justification;
  148. void createStandardLayout (const AttributedString&);
  149. bool createNativeLayout (const AttributedString&);
  150. void recalculateSize (const AttributedString&);
  151. JUCE_LEAK_DETECTOR (TextLayout)
  152. };
  153. #endif // JUCE_TEXTLAYOUT_H_INCLUDED