31 static float limitFontHeight (
const float height)
noexcept
33 return jlimit (0.1f, 10000.0f, height);
36 const float defaultFontHeight = 14.0f;
37 float minimumHorizontalScale = 0.7f;
39 String fallbackFontStyle;
43GetTypefaceForFont juce_getTypefaceForFont =
nullptr;
76 setSize (faces.
size());
77 defaultFace =
nullptr;
90 for (
int i = faces.
size(); --i >= 0;)
96 && face.typeface !=
nullptr
97 && face.typeface->isSuitableForFont (font))
99 face.lastUsageCount = ++counter;
100 return face.typeface;
109 for (
int i = faces.
size(); --i >= 0;)
123 face.lastUsageCount = ++counter;
125 if (juce_getTypefaceForFont ==
nullptr)
128 face.typeface = juce_getTypefaceForFont (font);
130 jassert (face.typeface !=
nullptr);
132 if (defaultFace ==
nullptr && font ==
Font())
133 defaultFace = face.typeface;
135 return face.typeface;
147 CachedFace()
noexcept {}
153 String typefaceName, typefaceStyle;
154 size_t lastUsageCount = 0;
173void (*clearOpenGLGlyphCache)() =
nullptr;
177 TypefaceCache::getInstance()->clear();
179 RenderingHelpers::SoftwareRendererSavedState::clearGlyphCache();
181 NullCheckedInvocation::invoke (clearOpenGLGlyphCache);
189 : typeface (TypefaceCache::getInstance()->getDefaultFace()),
192 height (FontValues::defaultFontHeight)
198 typefaceStyle (FontStyleHelpers::getStyleName (styleFlags)),
202 if (styleFlags ==
plain)
203 typeface = TypefaceCache::getInstance()->getDefaultFace();
207 : typefaceName (name),
208 typefaceStyle (FontStyleHelpers::getStyleName (styleFlags)),
213 typeface = TypefaceCache::getInstance()->getDefaultFace();
217 : typefaceName (name), typefaceStyle (style), height (fontHeight)
225 typefaceName (face->
getName()),
227 height (FontValues::defaultFontHeight)
234 typeface (
other.typeface),
235 typefaceName (
other.typefaceName),
236 typefaceStyle (
other.typefaceStyle),
237 height (
other.height),
238 horizontalScale (
other.horizontalScale),
239 kerning (
other.kerning),
240 ascent (
other.ascent),
241 underline (
other.underline)
247 return std::tie (height, underline, horizontalScale, kerning, typefaceName, typefaceStyle);
252 return tie() ==
other.tie();
257 return tie() <
other.tie();
272 if (typeface ==
nullptr)
274 typeface = TypefaceCache::getInstance()->findTypefaceFor (f);
281 void checkTypefaceSuitability (
const Font& f)
296 return height * ascent;
304 String getTypefaceName()
const {
return typefaceName; }
305 String getTypefaceStyle()
const {
return typefaceStyle; }
306 float getHeight()
const {
return height; }
307 float getHorizontalScale()
const {
return horizontalScale; }
308 float getKerning()
const {
return kerning; }
309 bool getUnderline()
const {
return underline; }
321 typeface = std::move (x);
324 void setTypefaceName (
String x)
327 typefaceName = std::move (x);
330 void setTypefaceStyle (
String x)
333 typefaceStyle = std::move (x);
336 void setHeight (
float x)
342 void setHorizontalScale (
float x)
348 void setKerning (
float x)
354 void setAscent (
float x)
360 void setUnderline (
bool x)
368 String typefaceName, typefaceStyle;
369 float height = 0.0f, horizontalScale = 1.0f, kerning = 0.0f, ascent = 0.0f;
370 bool underline =
false;
381 : font (new
SharedFontInternal (styleFlags, FontValues::limitFontHeight (fontHeight)))
386 : font (new
SharedFontInternal (typefaceName, styleFlags, FontValues::limitFontHeight (fontHeight)))
391 : font (new
SharedFontInternal (typefaceName, typefaceStyle, FontValues::limitFontHeight (fontHeight)))
402 : font (std::move (
other.font))
408 font = std::move (
other.font);
416 return font ==
other.font
417 || *font == *
other.font;
420bool Font::operator!= (
const Font&
other)
const noexcept
422 return ! operator== (
other);
425bool Font::compare (
const Font& a,
const Font& b)
noexcept
427 return *a.font < *b.font;
430void Font::dupeInternalIfShared()
432 if (font->getReferenceCount() > 1)
433 font = *
new SharedFontInternal (*font);
436void Font::checkTypefaceSuitability()
438 font->checkTypefaceSuitability (*
this);
444 String sans {
"<Sans-Serif>" },
446 mono {
"<Monospaced>" },
447 regular {
"<Regular>" };
474 if (
faceName != font->getTypefaceName())
478 dupeInternalIfShared();
480 font->setTypeface (
nullptr);
487 if (typefaceStyle != font->getTypefaceStyle())
489 dupeInternalIfShared();
490 font->setTypefaceStyle (typefaceStyle);
491 font->setTypeface (
nullptr);
510 return font->getTypefacePtr (*
this);
521 return FontValues::fallbackFont;
526 FontValues::fallbackFont = name;
528 #if JUCE_MAC || JUCE_IOS
535 return FontValues::fallbackFontStyle;
540 FontValues::fallbackFontStyle = style;
542 #if JUCE_MAC || JUCE_IOS
555float Font::getHeightToPointsFactor()
const
573 dupeInternalIfShared();
575 checkTypefaceSuitability();
585 dupeInternalIfShared();
586 font->setHorizontalScale (font->getHorizontalScale() * (font->getHeight() /
newHeight));
588 checkTypefaceSuitability();
613 dupeInternalIfShared();
614 font->setTypeface (
nullptr);
615 font->setTypefaceStyle (FontStyleHelpers::getStyleName (
newFlags));
632 dupeInternalIfShared();
636 checkTypefaceSuitability();
653 dupeInternalIfShared();
657 checkTypefaceSuitability();
672 dupeInternalIfShared();
673 font->setHorizontalScale (scaleFactor);
674 checkTypefaceSuitability();
679 return font->getHorizontalScale();
684 return font->getKerning();
696 dupeInternalIfShared();
698 checkTypefaceSuitability();
704bool Font::isBold()
const noexcept {
return FontStyleHelpers::isBold (font->getTypefaceStyle()); }
724 dupeInternalIfShared();
726 checkTypefaceSuitability();
731 return font->getAscent (*
this);
753 return w * font->getHeight() * font->getHorizontalScale();
762 auto scale = font->getHeight() * font->getHorizontalScale();
763 auto* x =
xOffsets.getRawDataPointer();
767 for (
int i = 0; i < num; ++i)
768 x[i] = (x[i] + (
float) i * font->getKerning()) * scale;
772 for (
int i = 0; i < num; ++i)
786 if (!
styles.contains (style,
true))
789 destArray.add (
Font (name, style, FontValues::defaultFontHeight));
828 return Font (name, style, height);
Holds a resizable array of primitive or copy-by-value objects.
int size() const noexcept
Returns the current number of elements in the array.
void insertMultiple(int indexToInsertAt, ParameterType newElement, int numberOfTimesToInsertIt)
Inserts multiple copies of an element into the array at a given position.
void clear()
Removes all elements from the array.
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Classes derived from this will be automatically deleted when the application exits.
Represents a particular font, including its size, style, etc.
Font withExtraKerningFactor(float extraKerning) const
Returns a copy of this font with a new kerning factor.
void setExtraKerningFactor(float extraKerning)
Changes the font's kerning.
static const String & getFallbackFontStyle()
Returns the font style of the typeface to be used for rendering glyphs that aren't found in the reque...
Font withHeight(float height) const
Returns a copy of this font with a new height.
int getStyleFlags() const noexcept
Returns the font's style flags.
float getHeightInPoints() const
Returns the total height of this font, in points.
static const String & getDefaultStyle()
Returns a font style name that represents the default style.
static const String & getFallbackFontName()
Returns the font family of the typeface to be used for rendering glyphs that aren't found in the requ...
void getGlyphPositions(const String &text, Array< int > &glyphs, Array< float > &xOffsets) const
Returns the series of glyph numbers and their x offsets needed to represent a string.
void setUnderline(bool shouldBeUnderlined)
Makes the font underlined or non-underlined.
static float getDefaultMinimumHorizontalScaleFactor() noexcept
Returns the minimum horizontal scale to which fonts may be squashed when trying to create a layout.
static Typeface::Ptr getDefaultTypefaceForFont(const Font &font)
Returns the default system typeface for the given font.
void setSizeAndStyle(float newHeight, int newStyleFlags, float newHorizontalScale, float newKerningAmount)
Changes all the font's characteristics with one call.
static const String & getDefaultSansSerifFontName()
Returns a typeface font family that represents the default sans-serif font.
static Font fromString(const String &fontDescription)
Recreates a font from its stringified encoding.
float getDescent() const
Returns the amount that the font descends below its baseline, in pixels.
Typeface::Ptr getTypefacePtr() const
Returns the typeface used by this font.
float getHeight() const noexcept
Returns the total height of this font, in pixels.
String getTypefaceName() const noexcept
Returns the font family of the typeface that this font uses.
static const String & getDefaultMonospacedFontName()
Returns a typeface font family that represents the default monospaced font.
void setStyleFlags(int newFlags)
Changes the font's style.
String getTypefaceStyle() const noexcept
Returns the font style of the typeface that this font uses.
String toString() const
Creates a string to describe this font.
bool isItalic() const noexcept
Returns true if the font is italic.
static StringArray findAllTypefaceStyles(const String &family)
Returns a list of all the available typeface font styles.
float getStringWidthFloat(const String &text) const
Returns the total width of a string as it would be drawn using this font.
void setItalic(bool shouldBeItalic)
Makes the font italic or non-italic.
void setBold(bool shouldBeBold)
Makes the font bold or non-bold.
int getStringWidth(const String &text) const
Returns the total width of a string as it would be drawn using this font.
StringArray getAvailableStyles() const
Returns a list of the styles that this font can use.
void setTypefaceStyle(const String &newStyle)
Changes the font style of the typeface.
void setHorizontalScale(float scaleFactor)
Changes the font's horizontal scale factor.
void setTypefaceName(const String &faceName)
Changes the font family of the typeface.
float getAscent() const
Returns the height of the font above its baseline, in pixels.
~Font() noexcept
Destructor.
static StringArray findAllTypefaceNames()
Returns a list of all the available typeface font families.
static void findFonts(Array< Font > &results)
Creates an array of Font objects to represent all the fonts on the system.
Font boldened() const
Returns a copy of this font with the bold attribute set.
Font withTypefaceStyle(const String &newStyle) const
Returns a copy of this font with a new typeface style.
Font withStyle(int styleFlags) const
Returns a copy of this font with the given set of style flags.
bool isBold() const noexcept
Returns true if the font is bold.
float getExtraKerningFactor() const noexcept
Returns the font's kerning.
Font italicised() const
Returns a copy of this font with the italic attribute set.
@ underlined
underlines the font.
@ plain
indicates a plain, non-bold, non-italic version of the font.
@ italic
finds an italic version of the font.
bool isUnderlined() const noexcept
Returns true if the font is underlined.
Font()
Creates a basic sans-serif font at a default height.
float getAscentInPoints() const
Returns the height of the font above its baseline, in points.
float getDescentInPoints() const
Returns the amount that the font descends below its baseline, in points.
static const String & getDefaultSerifFontName()
Returns a typeface font family that represents the default serif font.
void setHeightWithoutChangingWidth(float newHeight)
Changes the font's height without changing its width.
void setHeight(float newHeight)
Changes the font's height.
Font withHorizontalScale(float scaleFactor) const
Returns a copy of this font with a new horizontal scale.
static void setDefaultMinimumHorizontalScaleFactor(float newMinimumScaleFactor) noexcept
Sets the minimum horizontal scale to which fonts may be squashed when trying to create a text layout.
Font & operator=(Font &&other) noexcept
Move assignment operator.
float getHorizontalScale() const noexcept
Returns the font's horizontal scale.
Font withPointHeight(float heightInPoints) const
Returns a copy of this font with a new height, specified in points.
static void setFallbackFontName(const String &name)
Sets the (platform-specific) font family of the typeface to use to find glyphs that aren't available ...
static void setFallbackFontStyle(const String &style)
Sets the (platform-specific) font style of the typeface to use to find glyphs that aren't available i...
Automatically locks and unlocks a mutex object.
A critical section that allows multiple simultaneous readers.
ReferencedType * get() const noexcept
Returns the object that this pointer references.
A base class which provides methods for reference-counting.
ReferenceCountedObject()=default
Creates the reference-counted object (with an initial ref count of zero).
int getReferenceCount() const noexcept
Returns the object's current reference count.
Automatically locks and unlocks a ReadWriteLock object.
Automatically locks and unlocks a ReadWriteLock object.
A special array for holding a list of strings.
int length() const noexcept
Returns the number of characters in the string.
String trim() const
Returns a copy of this string with any whitespace characters removed from the start and end.
bool isEmpty() const noexcept
Returns true if the string contains no characters.
String substring(int startIndex, int endIndex) const
Returns a subsection of the string.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
A typeface represents a size-independent font.
virtual float getStringWidth(const String &text)=0
Measures the width of a line of text.
virtual float getAscent() const =0
Returns the ascent of the font, as a proportion of its height.
const String & getStyle() const noexcept
Returns the font style of the typeface.
virtual bool isSuitableForFont(const Font &) const
Returns true if this typeface can be used to render the specified font.
const String & getName() const noexcept
Returns the font family of the typeface.
virtual float getHeightToPointsFactor() const =0
Returns the value by which you should multiply a JUCE font-height value to convert it to the equivale...
ReferenceCountedObjectPtr< Typeface > Ptr
A handy typedef for a pointer to a typeface.
static void clearTypefaceCache()
Clears any fonts that are currently cached in memory.
virtual void getGlyphPositions(const String &text, Array< int > &glyphs, Array< float > &xOffsets)=0
Converts a line of text into its glyph numbers and their positions.
#define JUCE_IMPLEMENT_SINGLETON(Classname)
This is a counterpart to the JUCE_DECLARE_SINGLETON macros.
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion)
Macro to generate the appropriate methods and boilerplate for a singleton class.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...