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.

389 lines
15KB

  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 name 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 copy of another Font object. */
  63. Font (const Font& other) noexcept;
  64. /** Creates a font for a typeface. */
  65. Font (const Typeface::Ptr& typeface);
  66. /** Creates a basic sans-serif font at a default height.
  67. You should use one of the other constructors for creating a font that you're planning
  68. on drawing with - this constructor is here to help initialise objects before changing
  69. the font's settings later.
  70. */
  71. Font();
  72. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  73. Font (Font&& other) noexcept;
  74. Font& operator= (Font&& other) noexcept;
  75. #endif
  76. /** Copies this font from another one. */
  77. Font& operator= (const Font& other) noexcept;
  78. bool operator== (const Font& other) const noexcept;
  79. bool operator!= (const Font& other) const noexcept;
  80. /** Destructor. */
  81. ~Font() noexcept;
  82. //==============================================================================
  83. /** Changes the name of the typeface family.
  84. e.g. "Arial", "Courier", etc.
  85. This may also be set to Font::getDefaultSansSerifFontName(), Font::getDefaultSerifFontName(),
  86. or Font::getDefaultMonospacedFontName(), which are not actual platform-specific font names,
  87. but are generic names that are used to represent the various default fonts.
  88. If you need to know the exact typeface name being used, you can call
  89. Font::getTypeface()->getTypefaceName(), which will give you the platform-specific name.
  90. If a suitable font isn't found on the machine, it'll just use a default instead.
  91. */
  92. void setTypefaceName (const String& faceName);
  93. /** Returns the name of the typeface family that this font uses.
  94. e.g. "Arial", "Courier", etc.
  95. This may also be set to Font::getDefaultSansSerifFontName(), Font::getDefaultSerifFontName(),
  96. or Font::getDefaultMonospacedFontName(), which are not actual platform-specific font names,
  97. but are generic names that are used to represent the various default fonts.
  98. If you need to know the exact typeface name being used, you can call
  99. Font::getTypeface()->getTypefaceName(), which will give you the platform-specific name.
  100. */
  101. const String& getTypefaceName() const noexcept { return font->typefaceName; }
  102. //==============================================================================
  103. /** Returns a typeface name that represents the default sans-serif font.
  104. This is also the typeface that will be used when a font is created without
  105. specifying any typeface details.
  106. Note that this method just returns a generic placeholder string that means "the default
  107. sans-serif font" - it's not the actual name of this font.
  108. @see setTypefaceName, getDefaultSerifFontName, getDefaultMonospacedFontName
  109. */
  110. static const String& getDefaultSansSerifFontName();
  111. /** Returns a typeface name that represents the default sans-serif font.
  112. Note that this method just returns a generic placeholder string that means "the default
  113. serif font" - it's not the actual name of this font.
  114. @see setTypefaceName, getDefaultSansSerifFontName, getDefaultMonospacedFontName
  115. */
  116. static const String& getDefaultSerifFontName();
  117. /** Returns a typeface name that represents the default sans-serif font.
  118. Note that this method just returns a generic placeholder string that means "the default
  119. monospaced font" - it's not the actual name of this font.
  120. @see setTypefaceName, getDefaultSansSerifFontName, getDefaultSerifFontName
  121. */
  122. static const String& getDefaultMonospacedFontName();
  123. /** Returns the default system typeface for the given font. */
  124. static Typeface::Ptr getDefaultTypefaceForFont (const Font& font);
  125. //==============================================================================
  126. /** Returns the total height of this font.
  127. This is the maximum height, from the top of the ascent to the bottom of the
  128. descenders.
  129. @see setHeight, setHeightWithoutChangingWidth, getAscent
  130. */
  131. float getHeight() const noexcept { return font->height; }
  132. /** Changes the font's height.
  133. @see getHeight, setHeightWithoutChangingWidth
  134. */
  135. void setHeight (float newHeight);
  136. /** Changes the font's height without changing its width.
  137. This alters the horizontal scale to compensate for the change in height.
  138. */
  139. void setHeightWithoutChangingWidth (float newHeight);
  140. /** Returns the height of the font above its baseline.
  141. This is the maximum height from the baseline to the top.
  142. @see getHeight, getDescent
  143. */
  144. float getAscent() const;
  145. /** Returns the amount that the font descends below its baseline.
  146. This is calculated as (getHeight() - getAscent()).
  147. @see getAscent, getHeight
  148. */
  149. float getDescent() const;
  150. //==============================================================================
  151. /** Returns the font's style flags.
  152. This will return a bitwise-or'ed combination of values from the FontStyleFlags
  153. enum, to describe whether the font is bold, italic, etc.
  154. @see FontStyleFlags
  155. */
  156. int getStyleFlags() const noexcept { return font->styleFlags; }
  157. /** Changes the font's style.
  158. @param newFlags a bitwise-or'ed combination of values from the FontStyleFlags
  159. enum, to set the font's properties
  160. @see FontStyleFlags
  161. */
  162. void setStyleFlags (int newFlags);
  163. //==============================================================================
  164. /** Makes the font bold or non-bold. */
  165. void setBold (bool shouldBeBold);
  166. /** Returns a copy of this font with the bold attribute set. */
  167. Font boldened() const;
  168. /** Returns true if the font is bold. */
  169. bool isBold() const noexcept;
  170. /** Makes the font italic or non-italic. */
  171. void setItalic (bool shouldBeItalic);
  172. /** Returns a copy of this font with the italic attribute set. */
  173. Font italicised() const;
  174. /** Returns true if the font is italic. */
  175. bool isItalic() const noexcept;
  176. /** Makes the font underlined or non-underlined. */
  177. void setUnderline (bool shouldBeUnderlined);
  178. /** Returns true if the font is underlined. */
  179. bool isUnderlined() const noexcept;
  180. //==============================================================================
  181. /** Changes the font's horizontal scale factor.
  182. @param scaleFactor a value of 1.0 is the normal scale, less than this will be
  183. narrower, greater than 1.0 will be stretched out.
  184. */
  185. void setHorizontalScale (float scaleFactor);
  186. /** Returns the font's horizontal scale.
  187. A value of 1.0 is the normal scale, less than this will be narrower, greater
  188. than 1.0 will be stretched out.
  189. @see setHorizontalScale
  190. */
  191. float getHorizontalScale() const noexcept { return font->horizontalScale; }
  192. /** Changes the font's kerning.
  193. @param extraKerning a multiple of the font's height that will be added
  194. to space between the characters. So a value of zero is
  195. normal spacing, positive values spread the letters out,
  196. negative values make them closer together.
  197. */
  198. void setExtraKerningFactor (float extraKerning);
  199. /** Returns the font's kerning.
  200. This is the extra space added between adjacent characters, as a proportion
  201. of the font's height.
  202. A value of zero is normal spacing, positive values will spread the letters
  203. out more, and negative values make them closer together.
  204. */
  205. float getExtraKerningFactor() const noexcept { return font->kerning; }
  206. //==============================================================================
  207. /** Changes all the font's characteristics with one call. */
  208. void setSizeAndStyle (float newHeight,
  209. int newStyleFlags,
  210. float newHorizontalScale,
  211. float newKerningAmount);
  212. //==============================================================================
  213. /** Returns the total width of a string as it would be drawn using this font.
  214. For a more accurate floating-point result, use getStringWidthFloat().
  215. */
  216. int getStringWidth (const String& text) const;
  217. /** Returns the total width of a string as it would be drawn using this font.
  218. @see getStringWidth
  219. */
  220. float getStringWidthFloat (const String& text) const;
  221. /** Returns the series of glyph numbers and their x offsets needed to represent a string.
  222. An extra x offset is added at the end of the run, to indicate where the right hand
  223. edge of the last character is.
  224. */
  225. void getGlyphPositions (const String& text, Array <int>& glyphs, Array <float>& xOffsets) const;
  226. //==============================================================================
  227. /** Returns the typeface used by this font.
  228. Note that the object returned may go out of scope if this font is deleted
  229. or has its style changed.
  230. */
  231. Typeface* getTypeface() const;
  232. /** Creates an array of Font objects to represent all the fonts on the system.
  233. If you just need the names of the typefaces, you can also use
  234. findAllTypefaceNames() instead.
  235. @param results the array to which new Font objects will be added.
  236. */
  237. static void findFonts (Array<Font>& results);
  238. /** Returns a list of all the available typeface names.
  239. The names returned can be passed into setTypefaceName().
  240. You can use this instead of findFonts() if you only need their names, and not
  241. font objects.
  242. */
  243. static StringArray findAllTypefaceNames();
  244. //==============================================================================
  245. /** Returns the name of the typeface to be used for rendering glyphs that aren't found
  246. in the requested typeface.
  247. */
  248. static const String& getFallbackFontName();
  249. /** Sets the (platform-specific) name of the typeface to use to find glyphs that aren't
  250. available in whatever font you're trying to use.
  251. */
  252. static void setFallbackFontName (const String& name);
  253. //==============================================================================
  254. /** Creates a string to describe this font.
  255. The string will contain information to describe the font's typeface, size, and
  256. style. To recreate the font from this string, use fromString().
  257. */
  258. String toString() const;
  259. /** Recreates a font from its stringified encoding.
  260. This method takes a string that was created by toString(), and recreates the
  261. original font.
  262. */
  263. static Font fromString (const String& fontDescription);
  264. private:
  265. //==============================================================================
  266. friend class FontGlyphAlphaMap;
  267. friend class TypefaceCache;
  268. class SharedFontInternal : public SingleThreadedReferenceCountedObject
  269. {
  270. public:
  271. SharedFontInternal (float height, int styleFlags) noexcept;
  272. SharedFontInternal (const String& typefaceName, float height, int styleFlags) noexcept;
  273. SharedFontInternal (const Typeface::Ptr& typeface) noexcept;
  274. SharedFontInternal (const SharedFontInternal& other) noexcept;
  275. bool operator== (const SharedFontInternal&) const noexcept;
  276. String typefaceName;
  277. float height, horizontalScale, kerning, ascent;
  278. int styleFlags;
  279. Typeface::Ptr typeface;
  280. };
  281. ReferenceCountedObjectPtr <SharedFontInternal> font;
  282. void dupeInternalIfShared();
  283. JUCE_LEAK_DETECTOR (Font);
  284. };
  285. #endif // __JUCE_FONT_JUCEHEADER__