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_Font.h 19KB

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