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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A Pre-formatted piece of text, which may contain multiple fonts and colours.
  24. A TextLayout is created from an AttributedString, and once created can be
  25. quickly drawn into a Graphics context.
  26. @see AttributedString
  27. */
  28. class JUCE_API TextLayout
  29. {
  30. public:
  31. /** Creates an empty layout.
  32. Having created a TextLayout, you can populate it using createLayout() or
  33. createLayoutWithBalancedLineLengths().
  34. */
  35. TextLayout();
  36. TextLayout (const TextLayout&);
  37. TextLayout& operator= (const TextLayout&);
  38. TextLayout (TextLayout&&) noexcept;
  39. TextLayout& operator= (TextLayout&&) noexcept;
  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&, 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();
  151. JUCE_LEAK_DETECTOR (TextLayout)
  152. };
  153. } // namespace juce