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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 font's kerning.
  247. This is the extra space added between adjacent characters, as a proportion
  248. of the font's height.
  249. A value of zero is normal spacing, positive values will spread the letters
  250. out more, and negative values make them closer together.
  251. */
  252. float getExtraKerningFactor() const noexcept;
  253. /** Returns a copy of this font with a new kerning factor.
  254. @param extraKerning a multiple of the font's height that will be added
  255. to space between the characters. So a value of zero is
  256. normal spacing, positive values spread the letters out,
  257. negative values make them closer together.
  258. */
  259. Font withExtraKerningFactor (float extraKerning) const;
  260. /** Changes the font's kerning.
  261. @param extraKerning a multiple of the font's height that will be added
  262. to space between the characters. So a value of zero is
  263. normal spacing, positive values spread the letters out,
  264. negative values make them closer together.
  265. */
  266. void setExtraKerningFactor (float extraKerning);
  267. //==============================================================================
  268. /** Changes all the font's characteristics with one call. */
  269. void setSizeAndStyle (float newHeight,
  270. int newStyleFlags,
  271. float newHorizontalScale,
  272. float newKerningAmount);
  273. /** Changes all the font's characteristics with one call. */
  274. void setSizeAndStyle (float newHeight,
  275. const String& newStyle,
  276. float newHorizontalScale,
  277. float newKerningAmount);
  278. //==============================================================================
  279. /** Returns the total width of a string as it would be drawn using this font.
  280. For a more accurate floating-point result, use getStringWidthFloat().
  281. */
  282. int getStringWidth (const String& text) const;
  283. /** Returns the total width of a string as it would be drawn using this font.
  284. @see getStringWidth
  285. */
  286. float getStringWidthFloat (const String& text) const;
  287. /** Returns the series of glyph numbers and their x offsets needed to represent a string.
  288. An extra x offset is added at the end of the run, to indicate where the right hand
  289. edge of the last character is.
  290. */
  291. void getGlyphPositions (const String& text, Array <int>& glyphs, Array <float>& xOffsets) const;
  292. //==============================================================================
  293. /** Returns the typeface used by this font.
  294. Note that the object returned may go out of scope if this font is deleted
  295. or has its style changed.
  296. */
  297. Typeface* getTypeface() const;
  298. /** Creates an array of Font objects to represent all the fonts on the system.
  299. If you just need the font family names of the typefaces, you can also use
  300. findAllTypefaceNames() instead.
  301. @param results the array to which new Font objects will be added.
  302. */
  303. static void findFonts (Array<Font>& results);
  304. /** Returns a list of all the available typeface font families.
  305. The names returned can be passed into setTypefaceName().
  306. You can use this instead of findFonts() if you only need their font family names,
  307. and not font objects.
  308. */
  309. static StringArray findAllTypefaceNames();
  310. /** Returns a list of all the available typeface font styles.
  311. The names returned can be passed into setTypefaceStyle().
  312. You can use this instead of findFonts() if you only need their styles, and not
  313. font objects.
  314. */
  315. static StringArray findAllTypefaceStyles (const String& family);
  316. //==============================================================================
  317. /** Returns the font family of the typeface to be used for rendering glyphs that aren't
  318. found in the requested typeface.
  319. */
  320. static const String& getFallbackFontName();
  321. /** Sets the (platform-specific) font family of the typeface to use to find glyphs that
  322. aren't available in whatever font you're trying to use.
  323. */
  324. static void setFallbackFontName (const String& name);
  325. /** Returns the font style of the typeface to be used for rendering glyphs that aren't
  326. found in the requested typeface.
  327. */
  328. static const String& getFallbackFontStyle();
  329. /** Sets the (platform-specific) font style of the typeface to use to find glyphs that
  330. aren't available in whatever font you're trying to use.
  331. */
  332. static void setFallbackFontStyle (const String& style);
  333. //==============================================================================
  334. /** Creates a string to describe this font.
  335. The string will contain information to describe the font's typeface, size, and
  336. style. To recreate the font from this string, use fromString().
  337. */
  338. String toString() const;
  339. /** Recreates a font from its stringified encoding.
  340. This method takes a string that was created by toString(), and recreates the
  341. original font.
  342. */
  343. static Font fromString (const String& fontDescription);
  344. private:
  345. //==============================================================================
  346. class SharedFontInternal;
  347. ReferenceCountedObjectPtr<SharedFontInternal> font;
  348. void dupeInternalIfShared();
  349. void checkTypefaceSuitability();
  350. float getHeightToPointsFactor() const;
  351. JUCE_LEAK_DETECTOR (Font)
  352. };
  353. #endif // JUCE_FONT_H_INCLUDED