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.

178 lines
6.3KB

  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&& other) 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& text, float maxWidth);
  47. /** Creates a layout, attempting to choose a width which results in lines
  48. of a similar length.
  49. This will be slower than the normal createLayout method, but produces a
  50. tidier result.
  51. */
  52. void createLayoutWithBalancedLineLengths (const AttributedString& text, float maxWidth);
  53. /** Draws the layout within the specified area.
  54. The position of the text within the rectangle is controlled by the justification
  55. flags set in the original AttributedString that was used to create this layout.
  56. */
  57. void draw (Graphics& g, const Rectangle<float>& area) const;
  58. //==============================================================================
  59. /** A positioned glyph. */
  60. class JUCE_API Glyph
  61. {
  62. public:
  63. Glyph (int glyphCode, Point<float> anchor, float width) noexcept;
  64. Glyph (const Glyph&) noexcept;
  65. Glyph& operator= (const Glyph&) noexcept;
  66. ~Glyph() noexcept;
  67. /** The code number of this glyph. */
  68. int glyphCode;
  69. /** The glyph's anchor point - this is relative to the line's origin.
  70. @see TextLayout::Line::lineOrigin
  71. */
  72. Point<float> anchor;
  73. float width;
  74. private:
  75. JUCE_LEAK_DETECTOR (Glyph)
  76. };
  77. //==============================================================================
  78. /** A sequence of glyphs with a common font and colour. */
  79. class JUCE_API Run
  80. {
  81. public:
  82. Run() noexcept;
  83. Run (const Run&);
  84. Run (Range<int> stringRange, int numGlyphsToPreallocate);
  85. ~Run() noexcept;
  86. Font font; /**< The run's font. */
  87. Colour colour; /**< The run's colour. */
  88. Array<Glyph> glyphs; /**< The glyphs in this run. */
  89. Range<int> stringRange; /**< The character range that this run represents in the
  90. original string that was used to create it. */
  91. private:
  92. Run& operator= (const Run&);
  93. JUCE_LEAK_DETECTOR (Run)
  94. };
  95. //==============================================================================
  96. /** A line containing a sequence of glyph-runs. */
  97. class JUCE_API Line
  98. {
  99. public:
  100. Line() noexcept;
  101. Line (const Line&);
  102. Line (Range<int> stringRange, Point<float> lineOrigin,
  103. float ascent, float descent, float leading, int numRunsToPreallocate);
  104. ~Line() noexcept;
  105. /** Returns the X position range which contains all the glyphs in this line. */
  106. Range<float> getLineBoundsX() const noexcept;
  107. OwnedArray<Run> runs; /**< The glyph-runs in this line. */
  108. Range<int> stringRange; /**< The character range that this line represents in the
  109. original string that was used to create it. */
  110. Point<float> lineOrigin; /**< The line's baseline origin. */
  111. float ascent, descent, leading;
  112. private:
  113. Line& operator= (const Line&);
  114. JUCE_LEAK_DETECTOR (Line)
  115. };
  116. //==============================================================================
  117. /** Returns the maximum width of the content. */
  118. float getWidth() const noexcept { return width; }
  119. /** Returns the maximum height of the content. */
  120. float getHeight() const noexcept;
  121. /** Returns the number of lines in the layout. */
  122. int getNumLines() const noexcept { return lines.size(); }
  123. /** Returns one of the lines. */
  124. Line& getLine (int index) const;
  125. /** Adds a line to the layout. The layout will take ownership of this line object
  126. and will delete it when it is no longer needed. */
  127. void addLine (Line* line);
  128. /** Pre-allocates space for the specified number of lines. */
  129. void ensureStorageAllocated (int numLinesNeeded);
  130. private:
  131. OwnedArray<Line> lines;
  132. float width;
  133. Justification justification;
  134. void createStandardLayout (const AttributedString&);
  135. bool createNativeLayout (const AttributedString&);
  136. void recalculateWidth (const AttributedString&);
  137. JUCE_LEAK_DETECTOR (TextLayout)
  138. };
  139. #endif // JUCE_TEXTLAYOUT_H_INCLUDED