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.

322 lines
14KB

  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 glyph from a particular font, with a particular size, style,
  23. typeface and position.
  24. You should rarely need to use this class directly - for most purposes, the
  25. GlyphArrangement class will do what you need for text layout.
  26. @see GlyphArrangement, Font
  27. @tags{Graphics}
  28. */
  29. class JUCE_API PositionedGlyph final
  30. {
  31. public:
  32. //==============================================================================
  33. PositionedGlyph() noexcept;
  34. PositionedGlyph (const Font& font, juce_wchar character, int glyphNumber,
  35. float anchorX, float baselineY, float width, bool isWhitespace);
  36. /** Returns the character the glyph represents. */
  37. juce_wchar getCharacter() const noexcept { return character; }
  38. /** Checks whether the glyph is actually empty. */
  39. bool isWhitespace() const noexcept { return whitespace; }
  40. /** Returns the position of the glyph's left-hand edge. */
  41. float getLeft() const noexcept { return x; }
  42. /** Returns the position of the glyph's right-hand edge. */
  43. float getRight() const noexcept { return x + w; }
  44. /** Returns the y position of the glyph's baseline. */
  45. float getBaselineY() const noexcept { return y; }
  46. /** Returns the y position of the top of the glyph. */
  47. float getTop() const { return y - font.getAscent(); }
  48. /** Returns the y position of the bottom of the glyph. */
  49. float getBottom() const { return y + font.getDescent(); }
  50. /** Returns the bounds of the glyph. */
  51. Rectangle<float> getBounds() const { return { x, getTop(), w, font.getHeight() }; }
  52. //==============================================================================
  53. /** Shifts the glyph's position by a relative amount. */
  54. void moveBy (float deltaX, float deltaY);
  55. //==============================================================================
  56. /** Draws the glyph into a graphics context.
  57. (Note that this may change the context's currently selected font).
  58. */
  59. void draw (Graphics& g) const;
  60. /** Draws the glyph into a graphics context, with an extra transform applied to it.
  61. (Note that this may change the context's currently selected font).
  62. */
  63. void draw (Graphics& g, AffineTransform transform) const;
  64. /** Returns the path for this glyph.
  65. @param path the glyph's outline will be appended to this path
  66. */
  67. void createPath (Path& path) const;
  68. /** Checks to see if a point lies within this glyph. */
  69. bool hitTest (float x, float y) const;
  70. private:
  71. //==============================================================================
  72. friend class GlyphArrangement;
  73. Font font;
  74. juce_wchar character;
  75. int glyph;
  76. float x, y, w;
  77. bool whitespace;
  78. JUCE_LEAK_DETECTOR (PositionedGlyph)
  79. };
  80. //==============================================================================
  81. /**
  82. A set of glyphs, each with a position.
  83. You can create a GlyphArrangement, text to it and then draw it onto a
  84. graphics context. It's used internally by the text methods in the
  85. Graphics class, but can be used directly if more control is needed.
  86. @see Font, PositionedGlyph
  87. @tags{Graphics}
  88. */
  89. class JUCE_API GlyphArrangement final
  90. {
  91. public:
  92. //==============================================================================
  93. /** Creates an empty arrangement. */
  94. GlyphArrangement();
  95. GlyphArrangement (const GlyphArrangement&) = default;
  96. GlyphArrangement& operator= (const GlyphArrangement&) = default;
  97. GlyphArrangement (GlyphArrangement&&) = default;
  98. GlyphArrangement& operator= (GlyphArrangement&&) = default;
  99. /** Destructor. */
  100. ~GlyphArrangement() = default;
  101. //==============================================================================
  102. /** Returns the total number of glyphs in the arrangement. */
  103. int getNumGlyphs() const noexcept { return glyphs.size(); }
  104. /** Returns one of the glyphs from the arrangement.
  105. @param index the glyph's index, from 0 to (getNumGlyphs() - 1). Be
  106. careful not to pass an out-of-range index here, as it
  107. doesn't do any bounds-checking.
  108. */
  109. PositionedGlyph& getGlyph (int index) noexcept;
  110. const PositionedGlyph* begin() const { return glyphs.begin(); }
  111. const PositionedGlyph* end() const { return glyphs.end(); }
  112. //==============================================================================
  113. /** Clears all text from the arrangement and resets it. */
  114. void clear();
  115. /** Appends a line of text to the arrangement.
  116. This will add the text as a single line, where x is the left-hand edge of the
  117. first character, and y is the position for the text's baseline.
  118. If the text contains new-lines or carriage-returns, this will ignore them - use
  119. addJustifiedText() to add multi-line arrangements.
  120. */
  121. void addLineOfText (const Font& font,
  122. const String& text,
  123. float x, float y);
  124. /** Adds a line of text, truncating it if it's wider than a specified size.
  125. This is the same as addLineOfText(), but if the line's width exceeds the value
  126. specified in maxWidthPixels, it will be truncated using either ellipsis (i.e. dots: "..."),
  127. if useEllipsis is true, or if this is false, it will just drop any subsequent characters.
  128. */
  129. void addCurtailedLineOfText (const Font& font,
  130. const String& text,
  131. float x, float y,
  132. float maxWidthPixels,
  133. bool useEllipsis);
  134. /** Adds some multi-line text, breaking lines at word-boundaries if they are too wide.
  135. This will add text to the arrangement, breaking it into new lines either where there
  136. is a new-line or carriage-return character in the text, or where a line's width
  137. exceeds the value set in maxLineWidth.
  138. Each line that is added will be laid out using the flags set in horizontalLayout, so
  139. the lines can be left- or right-justified, or centred horizontally in the space
  140. between x and (x + maxLineWidth).
  141. The y coordinate is the position of the baseline of the first line of text - subsequent
  142. lines will be placed below it, separated by a distance of font.getHeight() + leading.
  143. */
  144. void addJustifiedText (const Font& font,
  145. const String& text,
  146. float x, float y,
  147. float maxLineWidth,
  148. Justification horizontalLayout,
  149. float leading = 0.0f);
  150. /** Tries to fit some text within a given space.
  151. This does its best to make the given text readable within the specified rectangle,
  152. so it's useful for labelling things.
  153. If the text is too big, it'll be squashed horizontally or broken over multiple lines
  154. if the maximumLinesToUse value allows this. If the text just won't fit into the space,
  155. it'll cram as much as possible in there, and put some ellipsis at the end to show that
  156. it's been truncated.
  157. A Justification parameter lets you specify how the text is laid out within the rectangle,
  158. both horizontally and vertically.
  159. The minimumHorizontalScale parameter specifies how much the text can be squashed horizontally
  160. to try to squeeze it into the space. If you don't want any horizontal scaling to occur, you
  161. can set this value to 1.0f. Pass 0 if you want it to use the default value.
  162. @see Graphics::drawFittedText
  163. */
  164. void addFittedText (const Font& font,
  165. const String& text,
  166. float x, float y, float width, float height,
  167. Justification layout,
  168. int maximumLinesToUse,
  169. float minimumHorizontalScale = 0.0f);
  170. /** Appends another glyph arrangement to this one. */
  171. void addGlyphArrangement (const GlyphArrangement&);
  172. /** Appends a custom glyph to the arrangement. */
  173. void addGlyph (const PositionedGlyph&);
  174. //==============================================================================
  175. /** Draws this glyph arrangement to a graphics context.
  176. This uses cached bitmaps so is much faster than the draw (Graphics&, AffineTransform)
  177. method, which renders the glyphs as filled vectors.
  178. */
  179. void draw (const Graphics&) const;
  180. /** Draws this glyph arrangement to a graphics context.
  181. This renders the paths as filled vectors, so is far slower than the draw (Graphics&)
  182. method for non-transformed arrangements.
  183. */
  184. void draw (const Graphics&, AffineTransform) const;
  185. /** Converts the set of glyphs into a path.
  186. @param path the glyphs' outlines will be appended to this path
  187. */
  188. void createPath (Path& path) const;
  189. /** Looks for a glyph that contains the given coordinate.
  190. @returns the index of the glyph, or -1 if none were found.
  191. */
  192. int findGlyphIndexAt (float x, float y) const;
  193. //==============================================================================
  194. /** Finds the smallest rectangle that will enclose a subset of the glyphs.
  195. @param startIndex the first glyph to test
  196. @param numGlyphs the number of glyphs to include; if this is < 0, all glyphs after
  197. startIndex will be included
  198. @param includeWhitespace if true, the extent of any whitespace characters will also
  199. be taken into account
  200. */
  201. Rectangle<float> getBoundingBox (int startIndex, int numGlyphs, bool includeWhitespace) const;
  202. /** Shifts a set of glyphs by a given amount.
  203. @param startIndex the first glyph to transform
  204. @param numGlyphs the number of glyphs to move; if this is < 0, all glyphs after
  205. startIndex will be used
  206. @param deltaX the amount to add to their x-positions
  207. @param deltaY the amount to add to their y-positions
  208. */
  209. void moveRangeOfGlyphs (int startIndex, int numGlyphs,
  210. float deltaX, float deltaY);
  211. /** Removes a set of glyphs from the arrangement.
  212. @param startIndex the first glyph to remove
  213. @param numGlyphs the number of glyphs to remove; if this is < 0, all glyphs after
  214. startIndex will be deleted
  215. */
  216. void removeRangeOfGlyphs (int startIndex, int numGlyphs);
  217. /** Expands or compresses a set of glyphs horizontally.
  218. @param startIndex the first glyph to transform
  219. @param numGlyphs the number of glyphs to stretch; if this is < 0, all glyphs after
  220. startIndex will be used
  221. @param horizontalScaleFactor how much to scale their horizontal width by
  222. */
  223. void stretchRangeOfGlyphs (int startIndex, int numGlyphs,
  224. float horizontalScaleFactor);
  225. /** Justifies a set of glyphs within a given space.
  226. This moves the glyphs as a block so that the whole thing is located within the
  227. given rectangle with the specified layout.
  228. If the Justification::horizontallyJustified flag is specified, each line will
  229. be stretched out to fill the specified width.
  230. */
  231. void justifyGlyphs (int startIndex, int numGlyphs,
  232. float x, float y, float width, float height,
  233. Justification justification);
  234. private:
  235. //==============================================================================
  236. Array<PositionedGlyph> glyphs;
  237. int insertEllipsis (const Font&, float maxXPos, int startIndex, int endIndex);
  238. int fitLineIntoSpace (int start, int numGlyphs, float x, float y, float w, float h, const Font&,
  239. Justification, float minimumHorizontalScale);
  240. void spreadOutLine (int start, int numGlyphs, float targetWidth);
  241. void splitLines (const String&, Font, int start, float x, float y, float w, float h, int maxLines,
  242. float lineWidth, Justification, float minimumHorizontalScale);
  243. void addLinesWithLineBreaks (const String&, const Font&, float x, float y, float width, float height, Justification);
  244. void drawGlyphUnderline (const Graphics&, const PositionedGlyph&, int, AffineTransform) const;
  245. JUCE_LEAK_DETECTOR (GlyphArrangement)
  246. };
  247. } // namespace juce