1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Font: Deprecate getStringWidth and getGlyphPositions

This commit is contained in:
reuk 2024-09-06 16:20:20 +01:00
parent eddedc2d13
commit 29213e07a1
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C
21 changed files with 179 additions and 58 deletions

View file

@ -756,7 +756,11 @@ float Font::getDescentInPoints() const { return getDescent() * getHeightToP
int Font::getStringWidth (const String& text) const
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
return (int) std::ceil (getStringWidthFloat (text));
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
JUCE_END_IGNORE_WARNINGS_MSVC
}
float Font::getStringWidthFloat (const String& text) const
@ -765,21 +769,6 @@ float Font::getStringWidthFloat (const String& text) const
return w + (getHeight() * getHorizontalScale() * getExtraKerningFactor() * (float) text.length());
}
void Font::getGlyphPositions (const String& text, Array<int>& glyphs, Array<float>& xOffsets) const
{
getTypefacePtr()->getGlyphPositions (getMetricsKind(), text, glyphs, xOffsets, getHeight(), getHorizontalScale());
if (auto num = xOffsets.size())
{
auto scale = getHeight() * getHorizontalScale();
auto* x = xOffsets.getRawDataPointer();
if (! approximatelyEqual (getExtraKerningFactor(), 0.0f))
for (int i = 0; i < num; ++i)
x[i] += ((float) i * getExtraKerningFactor() * scale);
}
}
void Font::findFonts (Array<Font>& destArray)
{
for (auto& name : findAllTypefaceNames())

View file

@ -450,21 +450,40 @@ public:
//==============================================================================
/** Returns the total width of a string as it would be drawn using this font.
For a more accurate floating-point result, use getStringWidthFloat().
This function does not take font fallback into account. If this font doesn't
include glyphs to represent all characters in the string, then the width
will be computed as though those characters were replaced with the "glyph not
found" character.
If you are trying to find the amount of space required to display a given string,
you'll get more accurate results by actually measuring the results of whichever
text layout engine (e.g. GlyphArrangement, TextLayout) you'll use when displaying
the string.
@see TextLayout::getStringWidth(), GlyphArrangement::getStringWidthInt()
*/
[[deprecated ("Use GlyphArrangement or TextLayout to compute text layouts")]]
int getStringWidth (const String& text) const;
/** Returns the total width of a string as it would be drawn using this font.
@see getStringWidth
This function does not take font fallback into account. If this font doesn't
include glyphs to represent all characters in the string, then the width
will be computed as though those characters were replaced with the "glyph not
found" character.
If you are trying to find the amount of space required to display a given string,
you'll get more accurate results by actually measuring the results of whichever
text layout engine (e.g. GlyphArrangement, TextLayout) you'll use when displaying
the string.
@see TextLayout::getStringWidth(), GlyphArrangement::getStringWidth()
*/
[[deprecated ("Use GlyphArrangement or TextLayout to compute text layouts")]]
float getStringWidthFloat (const String& text) const;
/** Returns the series of glyph numbers and their x offsets needed to represent a string.
An extra x offset is added at the end of the run, to indicate where the right hand
edge of the last character is.
*/
void getGlyphPositions (const String& text, Array<int>& glyphs, Array<float>& xOffsets) const;
//==============================================================================
/** Returns the main typeface used by this font.

View file

@ -310,6 +310,35 @@ public:
float x, float y, float width, float height,
Justification justification);
/** This convenience function adds text to a GlyphArrangement using the specified font
and returns the bounding box of the text after shaping.
The returned bounding box is positioned with its origin at the left end of the text's
baseline.
*/
static Rectangle<float> getStringBounds (const Font& font, StringRef text)
{
GlyphArrangement arrangement;
arrangement.addLineOfText (font, text, 0.0f, 0.0f);
return arrangement.getBoundingBox (0, arrangement.getNumGlyphs(), true);
}
/** This convenience function adds text to a GlyphArrangement using the specified font
and returns the width of the bounding box of the text after shaping.
*/
static float getStringWidth (const Font& font, StringRef text)
{
return getStringBounds (font, text).getWidth();
}
/** This convenience function adds text to a GlyphArrangement using the specified font
and returns the width of the bounding box of the text after shaping, rounded up to the
next integer.
*/
static int getStringWidthInt (const Font& font, StringRef text)
{
return (int) std::ceil (getStringWidth (font, text));
}
private:
//==============================================================================

View file

@ -265,6 +265,48 @@ public:
*/
void recalculateSize();
/** This convenience function adds an AttributedString to a TextLayout
and returns the bounding box of the text after shaping.
The returned bounding box is positioned with its origin at the left end of the text's
baseline.
*/
static Rectangle<float> getStringBounds (const AttributedString& string)
{
TextLayout layout;
layout.createLayout (string, std::numeric_limits<float>::max());
return layout.getLine (0).getLineBounds();
}
/** This convenience function adds text to a TextLayout using the specified font
and returns the bounding box of the text after shaping.
The returned bounding box is positioned with its origin at the left end of the text's
baseline.
*/
static Rectangle<float> getStringBounds (const Font& font, StringRef text)
{
AttributedString string;
string.append (text, font);
return getStringBounds (string);
}
/** This convenience function adds an AttributedString to a TextLayout
and returns the bounding box of the text after shaping.
*/
static float getStringWidth (const AttributedString& string)
{
return getStringBounds (string).getWidth();
}
/** This convenience function adds text to a TextLayout using the specified font
and returns the width of the bounding box of the text after shaping.
*/
static float getStringWidth (const Font& font, StringRef text)
{
return getStringBounds (font, text).getWidth();
}
private:
OwnedArray<Line> lines;
float width, height;