The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

450 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FONT_JUCEHEADER__
  19. #define __JUCE_FONT_JUCEHEADER__
  20. #include "juce_Typeface.h"
  21. class LowLevelGraphicsContext;
  22. //==============================================================================
  23. /**
  24. Represents a particular font, including its size, style, etc.
  25. Apart from the typeface to be used, a Font object also dictates whether
  26. the font is bold, italic, underlined, how big it is, and its kerning and
  27. horizontal scale factor.
  28. @see Typeface
  29. */
  30. class JUCE_API Font
  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 sans-serif font in a given style and size.
  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& typefaceStyle, float fontHeight);
  67. /** Creates a font with a given typeface and parameters.
  68. @param typefaceName the font family of the typeface to use
  69. @param typefaceStyle the font style of the typeface to use
  70. @param fontHeight the height in pixels (can be fractional)
  71. */
  72. Font (const String& typefaceName, const String& typefaceStyle, float fontHeight);
  73. /** Creates a copy of another Font object. */
  74. Font (const Font& other) noexcept;
  75. /** Creates a font for a typeface. */
  76. Font (const Typeface::Ptr& typeface);
  77. /** Creates a basic sans-serif font at a default height.
  78. You should use one of the other constructors for creating a font that you're planning
  79. on drawing with - this constructor is here to help initialise objects before changing
  80. the font's settings later.
  81. */
  82. Font();
  83. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  84. Font (Font&& other) noexcept;
  85. Font& operator= (Font&& other) noexcept;
  86. #endif
  87. /** Copies this font from another one. */
  88. Font& operator= (const Font& other) noexcept;
  89. bool operator== (const Font& other) const noexcept;
  90. bool operator!= (const Font& other) const noexcept;
  91. /** Destructor. */
  92. ~Font() noexcept;
  93. //==============================================================================
  94. /** Changes the font family of the typeface.
  95. e.g. "Arial", "Courier", etc.
  96. This may also be set to Font::getDefaultSansSerifFontName(), Font::getDefaultSerifFontName(),
  97. or Font::getDefaultMonospacedFontName(), which are not actual platform-specific font family names,
  98. but are generic font family names that are used to represent the various default fonts.
  99. If you need to know the exact typeface font family being used, you can call
  100. Font::getTypeface()->getName(), which will give you the platform-specific font family.
  101. If a suitable font isn't found on the machine, it'll just use a default instead.
  102. */
  103. void setTypefaceName (const String& faceName);
  104. /** Returns the font family of the typeface that this font uses.
  105. e.g. "Arial", "Courier", etc.
  106. This may also be set to Font::getDefaultSansSerifFontName(), Font::getDefaultSerifFontName(),
  107. or Font::getDefaultMonospacedFontName(), which are not actual platform-specific font family names,
  108. but are generic font familiy names that are used to represent the various default fonts.
  109. If you need to know the exact typeface font family being used, you can call
  110. Font::getTypeface()->getName(), which will give you the platform-specific font family.
  111. */
  112. const String& getTypefaceName() const noexcept;
  113. //==============================================================================
  114. /** Changes the font style of the typeface
  115. e.g. "Regular", "Italic", etc.
  116. */
  117. void setTypefaceStyle (const String& typefaceStyle);
  118. /** Returns the font style of the typeface that this font uses.
  119. e.g. "Regular", "Italic", etc.
  120. */
  121. const String& getTypefaceStyle() const noexcept;
  122. /** Returns a list of the styles that this font can use. */
  123. StringArray getAvailableStyles() const;
  124. //==============================================================================
  125. /** Returns a typeface font family that represents the default sans-serif font.
  126. This is also the typeface that will be used when a font is created without
  127. specifying any typeface details.
  128. Note that this method just returns a generic placeholder string that means "the default
  129. sans-serif font" - it's not the actual font family of this font.
  130. @see setTypefaceName, getDefaultSerifFontName, getDefaultMonospacedFontName
  131. */
  132. static const String& getDefaultSansSerifFontName();
  133. /** Returns a typeface font family that represents the default sans-serif font.
  134. Note that this method just returns a generic placeholder string that means "the default
  135. serif font" - it's not the actual font family of this font.
  136. @see setTypefaceName, getDefaultSansSerifFontName, getDefaultMonospacedFontName
  137. */
  138. static const String& getDefaultSerifFontName();
  139. /** Returns a typeface font family that represents the default sans-serif font.
  140. Note that this method just returns a generic placeholder string that means "the default
  141. monospaced font" - it's not the actual font family of this font.
  142. @see setTypefaceName, getDefaultSansSerifFontName, getDefaultSerifFontName
  143. */
  144. static const String& getDefaultMonospacedFontName();
  145. /** Returns a typeface font style that represents the default sans-serif font.
  146. Note that this method just returns a generic placeholder string that means "the default
  147. font style" - it's not the actual font style of this font.
  148. @see setTypefaceStyle
  149. */
  150. static const String& getDefaultStyle();
  151. /** Returns the default system typeface for the given font. */
  152. static Typeface::Ptr getDefaultTypefaceForFont (const Font& font);
  153. //==============================================================================
  154. /** Returns the total height of this font.
  155. This is the maximum height, from the top of the ascent to the bottom of the
  156. descenders.
  157. @see withHeight, setHeightWithoutChangingWidth, getAscent
  158. */
  159. float getHeight() const noexcept;
  160. /** Returns a copy of this font with a new height. */
  161. Font withHeight (float height) const;
  162. /** Changes the font's height.
  163. @see getHeight, withHeight, setHeightWithoutChangingWidth
  164. */
  165. void setHeight (float newHeight);
  166. /** Changes the font's height without changing its width.
  167. This alters the horizontal scale to compensate for the change in height.
  168. */
  169. void setHeightWithoutChangingWidth (float newHeight);
  170. /** Returns the height of the font above its baseline.
  171. This is the maximum height from the baseline to the top.
  172. @see getHeight, getDescent
  173. */
  174. float getAscent() const;
  175. /** Returns the amount that the font descends below its baseline.
  176. This is calculated as (getHeight() - getAscent()).
  177. @see getAscent, getHeight
  178. */
  179. float getDescent() const;
  180. //==============================================================================
  181. /** Returns the font's style flags.
  182. This will return a bitwise-or'ed combination of values from the FontStyleFlags
  183. enum, to describe whether the font is bold, italic, etc.
  184. @see FontStyleFlags, withStyle
  185. */
  186. int getStyleFlags() const noexcept;
  187. /** Returns a copy of this font with the given set of style flags.
  188. @param styleFlags a bitwise-or'ed combination of values from the FontStyleFlags enum.
  189. @see FontStyleFlags, getStyleFlags
  190. */
  191. Font withStyle (int styleFlags) const;
  192. /** Changes the font's style.
  193. @param newFlags a bitwise-or'ed combination of values from the FontStyleFlags enum.
  194. @see FontStyleFlags, withStyle
  195. */
  196. void setStyleFlags (int newFlags);
  197. //==============================================================================
  198. /** Makes the font bold or non-bold. */
  199. void setBold (bool shouldBeBold);
  200. /** Returns a copy of this font with the bold attribute set. */
  201. Font boldened() const;
  202. /** Returns true if the font is bold. */
  203. bool isBold() const noexcept;
  204. /** Makes the font italic or non-italic. */
  205. void setItalic (bool shouldBeItalic);
  206. /** Returns a copy of this font with the italic attribute set. */
  207. Font italicised() const;
  208. /** Returns true if the font is italic. */
  209. bool isItalic() const noexcept;
  210. /** Makes the font underlined or non-underlined. */
  211. void setUnderline (bool shouldBeUnderlined);
  212. /** Returns true if the font is underlined. */
  213. bool isUnderlined() const noexcept;
  214. //==============================================================================
  215. /** Returns the font's horizontal scale.
  216. A value of 1.0 is the normal scale, less than this will be narrower, greater
  217. than 1.0 will be stretched out.
  218. @see withHorizontalScale
  219. */
  220. float getHorizontalScale() const noexcept;
  221. /** Returns a copy of this font with a new horizontal scale.
  222. @param scaleFactor a value of 1.0 is the normal scale, less than this will be
  223. narrower, greater than 1.0 will be stretched out.
  224. @see getHorizontalScale
  225. */
  226. Font withHorizontalScale (float scaleFactor) const;
  227. /** Changes the font's horizontal scale factor.
  228. @param scaleFactor a value of 1.0 is the normal scale, less than this will be
  229. narrower, greater than 1.0 will be stretched out.
  230. */
  231. void setHorizontalScale (float scaleFactor);
  232. /** Returns the font's kerning.
  233. This is the extra space added between adjacent characters, as a proportion
  234. of the font's height.
  235. A value of zero is normal spacing, positive values will spread the letters
  236. out more, and negative values make them closer together.
  237. */
  238. float getExtraKerningFactor() const noexcept;
  239. /** Returns a copy of this font with a new kerning factor.
  240. @param extraKerning a multiple of the font's height that will be added
  241. to space between the characters. So a value of zero is
  242. normal spacing, positive values spread the letters out,
  243. negative values make them closer together.
  244. */
  245. Font withExtraKerningFactor (float extraKerning) const;
  246. /** Changes the font's kerning.
  247. @param extraKerning a multiple of the font's height that will be added
  248. to space between the characters. So a value of zero is
  249. normal spacing, positive values spread the letters out,
  250. negative values make them closer together.
  251. */
  252. void setExtraKerningFactor (float extraKerning);
  253. //==============================================================================
  254. /** Changes all the font's characteristics with one call. */
  255. void setSizeAndStyle (float newHeight,
  256. int newStyleFlags,
  257. float newHorizontalScale,
  258. float newKerningAmount);
  259. /** Changes all the font's characteristics with one call. */
  260. void setSizeAndStyle (float newHeight,
  261. const String& newStyle,
  262. float newHorizontalScale,
  263. float newKerningAmount);
  264. //==============================================================================
  265. /** Returns the total width of a string as it would be drawn using this font.
  266. For a more accurate floating-point result, use getStringWidthFloat().
  267. */
  268. int getStringWidth (const String& text) const;
  269. /** Returns the total width of a string as it would be drawn using this font.
  270. @see getStringWidth
  271. */
  272. float getStringWidthFloat (const String& text) const;
  273. /** Returns the series of glyph numbers and their x offsets needed to represent a string.
  274. An extra x offset is added at the end of the run, to indicate where the right hand
  275. edge of the last character is.
  276. */
  277. void getGlyphPositions (const String& text, Array <int>& glyphs, Array <float>& xOffsets) const;
  278. //==============================================================================
  279. /** Returns the typeface used by this font.
  280. Note that the object returned may go out of scope if this font is deleted
  281. or has its style changed.
  282. */
  283. Typeface* getTypeface() const;
  284. /** Creates an array of Font objects to represent all the fonts on the system.
  285. If you just need the font family names of the typefaces, you can also use
  286. findAllTypefaceNames() instead.
  287. @param results the array to which new Font objects will be added.
  288. */
  289. static void findFonts (Array<Font>& results);
  290. /** Returns a list of all the available typeface font families.
  291. The names returned can be passed into setTypefaceName().
  292. You can use this instead of findFonts() if you only need their font family names,
  293. and not font objects.
  294. */
  295. static StringArray findAllTypefaceNames();
  296. /** Returns a list of all the available typeface font styles.
  297. The names returned can be passed into setTypefaceStyle().
  298. You can use this instead of findFonts() if you only need their styles, and not
  299. font objects.
  300. */
  301. static StringArray findAllTypefaceStyles (const String& family);
  302. //==============================================================================
  303. /** Returns the font family of the typeface to be used for rendering glyphs that aren't
  304. found in the requested typeface.
  305. */
  306. static const String& getFallbackFontName();
  307. /** Sets the (platform-specific) font family of the typeface to use to find glyphs that
  308. aren't available in whatever font you're trying to use.
  309. */
  310. static void setFallbackFontName (const String& name);
  311. /** Returns the font style of the typeface to be used for rendering glyphs that aren't
  312. found in the requested typeface.
  313. */
  314. static const String& getFallbackFontStyle();
  315. /** Sets the (platform-specific) font style of the typeface to use to find glyphs that
  316. aren't available in whatever font you're trying to use.
  317. */
  318. static void setFallbackFontStyle (const String& style);
  319. //==============================================================================
  320. /** Creates a string to describe this font.
  321. The string will contain information to describe the font's typeface, size, and
  322. style. To recreate the font from this string, use fromString().
  323. */
  324. String toString() const;
  325. /** Recreates a font from its stringified encoding.
  326. This method takes a string that was created by toString(), and recreates the
  327. original font.
  328. */
  329. static Font fromString (const String& fontDescription);
  330. private:
  331. //==============================================================================
  332. class SharedFontInternal;
  333. ReferenceCountedObjectPtr <SharedFontInternal> font;
  334. void dupeInternalIfShared();
  335. JUCE_LEAK_DETECTOR (Font);
  336. };
  337. #endif // __JUCE_FONT_JUCEHEADER__