juce_Font.h 19 KB

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