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.

473 lines
19KB

  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_FONT_H_INCLUDED
  18. #define JUCE_FONT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Represents a particular font, including its size, style, etc.
  22. Apart from the typeface to be used, a Font object also dictates whether
  23. the font is bold, italic, underlined, how big it is, and its kerning and
  24. horizontal scale factor.
  25. @see Typeface
  26. */
  27. class JUCE_API Font
  28. {
  29. public:
  30. //==============================================================================
  31. /** A combination of these values is used by the constructor to specify the
  32. style of font to use.
  33. */
  34. enum FontStyleFlags
  35. {
  36. plain = 0, /**< indicates a plain, non-bold, non-italic version of the font. @see setStyleFlags */
  37. bold = 1, /**< boldens the font. @see setStyleFlags */
  38. italic = 2, /**< finds an italic version of the font. @see setStyleFlags */
  39. underlined = 4 /**< underlines the font. @see setStyleFlags */
  40. };
  41. //==============================================================================
  42. /** Creates a sans-serif font in a given size.
  43. @param fontHeight the height in pixels (can be fractional)
  44. @param styleFlags the style to use - this can be a combination of the
  45. Font::bold, Font::italic and Font::underlined, or
  46. just Font::plain for the normal style.
  47. @see FontStyleFlags, getDefaultSansSerifFontName
  48. */
  49. Font (float fontHeight, int styleFlags = plain);
  50. /** Creates a font with a given typeface and parameters.
  51. @param typefaceName the font family of the typeface to use
  52. @param fontHeight the height in pixels (can be fractional)
  53. @param styleFlags the style to use - this can be a combination of the
  54. Font::bold, Font::italic and Font::underlined, or
  55. just Font::plain for the normal style.
  56. @see FontStyleFlags, getDefaultSansSerifFontName
  57. */
  58. Font (const String& typefaceName, float fontHeight, int styleFlags);
  59. /** Creates a font with a given typeface and parameters.
  60. @param typefaceName the font family of the typeface to use
  61. @param typefaceStyle the font style of the typeface to use
  62. @param fontHeight the height in pixels (can be fractional)
  63. */
  64. Font (const String& typefaceName, const String& typefaceStyle, float fontHeight);
  65. /** Creates a copy of another Font object. */
  66. Font (const Font& other) noexcept;
  67. /** Creates a font for a typeface. */
  68. Font (const Typeface::Ptr& typeface);
  69. /** Creates a basic sans-serif font at a default height.
  70. You should use one of the other constructors for creating a font that you're planning
  71. on drawing with - this constructor is here to help initialise objects before changing
  72. the font's settings later.
  73. */
  74. Font();
  75. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  76. Font (Font&& other) noexcept;
  77. Font& operator= (Font&& other) noexcept;
  78. #endif
  79. /** Copies this font from another one. */
  80. Font& operator= (const Font& other) noexcept;
  81. bool operator== (const Font& other) const noexcept;
  82. bool operator!= (const Font& other) const noexcept;
  83. /** Destructor. */
  84. ~Font() noexcept;
  85. //==============================================================================
  86. /** Changes the font family of the typeface.
  87. e.g. "Arial", "Courier", etc.
  88. This may also be set to Font::getDefaultSansSerifFontName(), Font::getDefaultSerifFontName(),
  89. or Font::getDefaultMonospacedFontName(), which are not actual platform-specific font family names,
  90. but are generic font family names that are used to represent the various default fonts.
  91. If you need to know the exact typeface font family being used, you can call
  92. Font::getTypeface()->getName(), which will give you the platform-specific font family.
  93. If a suitable font isn't found on the machine, it'll just use a default instead.
  94. */
  95. void setTypefaceName (const String& faceName);
  96. /** Returns the font family of the typeface that this font uses.
  97. e.g. "Arial", "Courier", etc.
  98. This may also be set to Font::getDefaultSansSerifFontName(), Font::getDefaultSerifFontName(),
  99. or Font::getDefaultMonospacedFontName(), which are not actual platform-specific font family names,
  100. but are generic font familiy names that are used to represent the various default fonts.
  101. If you need to know the exact typeface font family being used, you can call
  102. Font::getTypeface()->getName(), which will give you the platform-specific font family.
  103. */
  104. const String& getTypefaceName() const noexcept;
  105. //==============================================================================
  106. /** Returns the font style of the typeface that this font uses.
  107. @see withTypefaceStyle, getAvailableStyles()
  108. */
  109. const String& getTypefaceStyle() const noexcept;
  110. /** Changes the font style of the typeface.
  111. @see getAvailableStyles()
  112. */
  113. void setTypefaceStyle (const String& newStyle);
  114. /** Returns a copy of this font with a new typeface style.
  115. @see getAvailableStyles()
  116. */
  117. Font withTypefaceStyle (const String& newStyle) const;
  118. /** Returns a list of the styles that this font can use. */
  119. StringArray getAvailableStyles() const;
  120. //==============================================================================
  121. /** Returns a typeface font family that represents the default sans-serif font.
  122. This is also the typeface that will be used when a font is created without
  123. specifying any typeface details.
  124. Note that this method just returns a generic placeholder string that means "the default
  125. sans-serif font" - it's not the actual font family of this font.
  126. @see setTypefaceName, getDefaultSerifFontName, getDefaultMonospacedFontName
  127. */
  128. static const String& getDefaultSansSerifFontName();
  129. /** Returns a typeface font family that represents the default serif font.
  130. Note that this method just returns a generic placeholder string that means "the default
  131. serif font" - it's not the actual font family of this font.
  132. @see setTypefaceName, getDefaultSansSerifFontName, getDefaultMonospacedFontName
  133. */
  134. static const String& getDefaultSerifFontName();
  135. /** Returns a typeface font family that represents the default monospaced font.
  136. Note that this method just returns a generic placeholder string that means "the default
  137. monospaced font" - it's not the actual font family of this font.
  138. @see setTypefaceName, getDefaultSansSerifFontName, getDefaultSerifFontName
  139. */
  140. static const String& getDefaultMonospacedFontName();
  141. /** Returns a font style name that represents the default style.
  142. Note that this method just returns a generic placeholder string that means "the default
  143. font style" - it's not the actual name of the font style of any particular font.
  144. @see setTypefaceStyle
  145. */
  146. static const String& getDefaultStyle();
  147. /** Returns the default system typeface for the given font. */
  148. static Typeface::Ptr getDefaultTypefaceForFont (const Font& font);
  149. //==============================================================================
  150. /** Returns a copy of this font with a new height. */
  151. Font withHeight (float height) const;
  152. /** Returns a copy of this font with a new height, specified in points. */
  153. Font withPointHeight (float heightInPoints) const;
  154. /** Changes the font's height.
  155. @see getHeight, withHeight, setHeightWithoutChangingWidth
  156. */
  157. void setHeight (float newHeight);
  158. /** Changes the font's height without changing its width.
  159. This alters the horizontal scale to compensate for the change in height.
  160. */
  161. void setHeightWithoutChangingWidth (float newHeight);
  162. /** Returns the total height of this font, in pixels.
  163. This is the maximum height, from the top of the ascent to the bottom of the
  164. descenders.
  165. @see withHeight, setHeightWithoutChangingWidth, getAscent
  166. */
  167. float getHeight() const noexcept;
  168. /** Returns the total height of this font, in points.
  169. This is the maximum height, from the top of the ascent to the bottom of the
  170. descenders.
  171. @see withPointHeight, getHeight
  172. */
  173. float getHeightInPoints() const;
  174. /** Returns the height of the font above its baseline, in pixels.
  175. This is the maximum height from the baseline to the top.
  176. @see getHeight, getDescent
  177. */
  178. float getAscent() const;
  179. /** Returns the height of the font above its baseline, in points.
  180. This is the maximum height from the baseline to the top.
  181. @see getHeight, getDescent
  182. */
  183. float getAscentInPoints() const;
  184. /** Returns the amount that the font descends below its baseline, in pixels.
  185. This is calculated as (getHeight() - getAscent()).
  186. @see getAscent, getHeight
  187. */
  188. float getDescent() const;
  189. /** Returns the amount that the font descends below its baseline, in points.
  190. This is calculated as (getHeight() - getAscent()).
  191. @see getAscent, getHeight
  192. */
  193. float getDescentInPoints() const;
  194. //==============================================================================
  195. /** Returns the font's style flags.
  196. This will return a bitwise-or'ed combination of values from the FontStyleFlags
  197. enum, to describe whether the font is bold, italic, etc.
  198. @see FontStyleFlags, withStyle
  199. */
  200. int getStyleFlags() const noexcept;
  201. /** Returns a copy of this font with the given set of style flags.
  202. @param styleFlags a bitwise-or'ed combination of values from the FontStyleFlags enum.
  203. @see FontStyleFlags, getStyleFlags
  204. */
  205. Font withStyle (int styleFlags) const;
  206. /** Changes the font's style.
  207. @param newFlags a bitwise-or'ed combination of values from the FontStyleFlags enum.
  208. @see FontStyleFlags, withStyle
  209. */
  210. void setStyleFlags (int newFlags);
  211. //==============================================================================
  212. /** Makes the font bold or non-bold. */
  213. void setBold (bool shouldBeBold);
  214. /** Returns a copy of this font with the bold attribute set. */
  215. Font boldened() const;
  216. /** Returns true if the font is bold. */
  217. bool isBold() const noexcept;
  218. /** Makes the font italic or non-italic. */
  219. void setItalic (bool shouldBeItalic);
  220. /** Returns a copy of this font with the italic attribute set. */
  221. Font italicised() const;
  222. /** Returns true if the font is italic. */
  223. bool isItalic() const noexcept;
  224. /** Makes the font underlined or non-underlined. */
  225. void setUnderline (bool shouldBeUnderlined);
  226. /** Returns true if the font is underlined. */
  227. bool isUnderlined() const noexcept;
  228. //==============================================================================
  229. /** Returns the font's horizontal scale.
  230. A value of 1.0 is the normal scale, less than this will be narrower, greater
  231. than 1.0 will be stretched out.
  232. @see withHorizontalScale
  233. */
  234. float getHorizontalScale() const noexcept;
  235. /** Returns a copy of this font with a new horizontal scale.
  236. @param scaleFactor a value of 1.0 is the normal scale, less than this will be
  237. narrower, greater than 1.0 will be stretched out.
  238. @see getHorizontalScale
  239. */
  240. Font withHorizontalScale (float scaleFactor) const;
  241. /** Changes the font's horizontal scale factor.
  242. @param scaleFactor a value of 1.0 is the normal scale, less than this will be
  243. narrower, greater than 1.0 will be stretched out.
  244. */
  245. void setHorizontalScale (float scaleFactor);
  246. /** Returns the minimum horizontal scale to which fonts may be squashed when trying to
  247. create a layout.
  248. @see setDefaultMinimumHorizontalScaleFactor
  249. */
  250. static float getDefaultMinimumHorizontalScaleFactor() noexcept;
  251. /** Sets the minimum horizontal scale to which fonts may be squashed when trying to
  252. create a text layout.
  253. @see getDefaultMinimumHorizontalScaleFactor
  254. */
  255. static void setDefaultMinimumHorizontalScaleFactor (float newMinimumScaleFactor) noexcept;
  256. /** Returns the font's kerning.
  257. This is the extra space added between adjacent characters, as a proportion
  258. of the font's height.
  259. A value of zero is normal spacing, positive values will spread the letters
  260. out more, and negative values make them closer together.
  261. */
  262. float getExtraKerningFactor() const noexcept;
  263. /** Returns a copy of this font with a new kerning factor.
  264. @param extraKerning a multiple of the font's height that will be added
  265. to space between the characters. So a value of zero is
  266. normal spacing, positive values spread the letters out,
  267. negative values make them closer together.
  268. */
  269. Font withExtraKerningFactor (float extraKerning) const;
  270. /** Changes the font's kerning.
  271. @param extraKerning a multiple of the font's height that will be added
  272. to space between the characters. So a value of zero is
  273. normal spacing, positive values spread the letters out,
  274. negative values make them closer together.
  275. */
  276. void setExtraKerningFactor (float extraKerning);
  277. //==============================================================================
  278. /** Changes all the font's characteristics with one call. */
  279. void setSizeAndStyle (float newHeight,
  280. int newStyleFlags,
  281. float newHorizontalScale,
  282. float newKerningAmount);
  283. /** Changes all the font's characteristics with one call. */
  284. void setSizeAndStyle (float newHeight,
  285. const String& newStyle,
  286. float newHorizontalScale,
  287. float newKerningAmount);
  288. //==============================================================================
  289. /** Returns the total width of a string as it would be drawn using this font.
  290. For a more accurate floating-point result, use getStringWidthFloat().
  291. */
  292. int getStringWidth (const String& text) const;
  293. /** Returns the total width of a string as it would be drawn using this font.
  294. @see getStringWidth
  295. */
  296. float getStringWidthFloat (const String& text) const;
  297. /** Returns the series of glyph numbers and their x offsets needed to represent a string.
  298. An extra x offset is added at the end of the run, to indicate where the right hand
  299. edge of the last character is.
  300. */
  301. void getGlyphPositions (const String& text, Array <int>& glyphs, Array <float>& xOffsets) const;
  302. //==============================================================================
  303. /** Returns the typeface used by this font.
  304. Note that the object returned may go out of scope if this font is deleted
  305. or has its style changed.
  306. */
  307. Typeface* getTypeface() const;
  308. /** Creates an array of Font objects to represent all the fonts on the system.
  309. If you just need the font family names of the typefaces, you can also use
  310. findAllTypefaceNames() instead.
  311. @param results the array to which new Font objects will be added.
  312. */
  313. static void findFonts (Array<Font>& results);
  314. /** Returns a list of all the available typeface font families.
  315. The names returned can be passed into setTypefaceName().
  316. You can use this instead of findFonts() if you only need their font family names,
  317. and not font objects.
  318. */
  319. static StringArray findAllTypefaceNames();
  320. /** Returns a list of all the available typeface font styles.
  321. The names returned can be passed into setTypefaceStyle().
  322. You can use this instead of findFonts() if you only need their styles, and not
  323. font objects.
  324. */
  325. static StringArray findAllTypefaceStyles (const String& family);
  326. //==============================================================================
  327. /** Returns the font family of the typeface to be used for rendering glyphs that aren't
  328. found in the requested typeface.
  329. */
  330. static const String& getFallbackFontName();
  331. /** Sets the (platform-specific) font family of the typeface to use to find glyphs that
  332. aren't available in whatever font you're trying to use.
  333. */
  334. static void setFallbackFontName (const String& name);
  335. /** Returns the font style of the typeface to be used for rendering glyphs that aren't
  336. found in the requested typeface.
  337. */
  338. static const String& getFallbackFontStyle();
  339. /** Sets the (platform-specific) font style of the typeface to use to find glyphs that
  340. aren't available in whatever font you're trying to use.
  341. */
  342. static void setFallbackFontStyle (const String& style);
  343. //==============================================================================
  344. /** Creates a string to describe this font.
  345. The string will contain information to describe the font's typeface, size, and
  346. style. To recreate the font from this string, use fromString().
  347. */
  348. String toString() const;
  349. /** Recreates a font from its stringified encoding.
  350. This method takes a string that was created by toString(), and recreates the
  351. original font.
  352. */
  353. static Font fromString (const String& fontDescription);
  354. private:
  355. //==============================================================================
  356. class SharedFontInternal;
  357. ReferenceCountedObjectPtr<SharedFontInternal> font;
  358. void dupeInternalIfShared();
  359. void checkTypefaceSuitability();
  360. float getHeightToPointsFactor() const;
  361. JUCE_LEAK_DETECTOR (Font)
  362. };
  363. #endif // JUCE_FONT_H_INCLUDED