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;

View file

@ -87,7 +87,7 @@ Font HyperlinkButton::getFontToUse() const
void HyperlinkButton::changeWidthToFitText()
{
setSize (getFontToUse().getStringWidth (getButtonText()) + 6, getHeight());
setSize (GlyphArrangement::getStringWidthInt (getFontToUse(), getButtonText()) + 6, getHeight());
}
void HyperlinkButton::setJustificationType (Justification newJustification)

View file

@ -1204,7 +1204,7 @@ private:
const auto y = optY.value_or (layoutState.getNextStartingPos().getY());
Rectangle<float> bounds (x, y - font.getAscent(),
font.getStringWidthFloat (text), font.getHeight());
GlyphArrangement::getStringWidth (font, text), font.getHeight());
if (anchorStr == "middle") bounds.setX (bounds.getX() - bounds.getWidth() / 2.0f);
else if (anchorStr == "end") bounds.setX (bounds.getX() - bounds.getWidth());

View file

@ -265,7 +265,7 @@ Font LookAndFeel_V2::getTextButtonFont (TextButton&, int buttonHeight)
int LookAndFeel_V2::getTextButtonWidthToFitText (TextButton& b, int buttonHeight)
{
return getTextButtonFont (b, buttonHeight).getStringWidth (b.getButtonText()) + buttonHeight;
return GlyphArrangement::getStringWidthInt (getTextButtonFont (b, buttonHeight), b.getButtonText()) + buttonHeight;
}
void LookAndFeel_V2::drawButtonText (Graphics& g, TextButton& button,
@ -361,7 +361,7 @@ void LookAndFeel_V2::changeToggleButtonWidthToFitText (ToggleButton& button)
Font font (withDefaultMetrics (FontOptions { fontSize }));
button.setSize (font.getStringWidth (button.getButtonText()) + roundToInt (tickWidth) + 9,
button.setSize (GlyphArrangement::getStringWidthInt (font, button.getButtonText()) + roundToInt (tickWidth) + 9,
button.getHeight());
}
@ -883,7 +883,7 @@ void LookAndFeel_V2::getIdealPopupMenuItemSize (const String& text, const bool i
font.setHeight ((float) standardMenuItemHeight / 1.3f);
idealHeight = standardMenuItemHeight > 0 ? standardMenuItemHeight : roundToInt (font.getHeight() * 1.3f);
idealWidth = font.getStringWidth (text) + idealHeight * 2;
idealWidth = GlyphArrangement::getStringWidthInt (font, text) + idealHeight * 2;
}
}
@ -1117,8 +1117,7 @@ Font LookAndFeel_V2::getMenuBarFont (MenuBarComponent& menuBar, int /*itemIndex*
int LookAndFeel_V2::getMenuBarItemWidth (MenuBarComponent& menuBar, int itemIndex, const String& itemText)
{
return getMenuBarFont (menuBar, itemIndex, itemText)
.getStringWidth (itemText) + menuBar.getHeight();
return GlyphArrangement::getStringWidthInt (getMenuBarFont (menuBar, itemIndex, itemText), itemText) + menuBar.getHeight();
}
void LookAndFeel_V2::drawMenuBarItem (Graphics& g, int width, int height,
@ -1887,7 +1886,7 @@ void LookAndFeel_V2::drawDocumentWindowTitleBar (DocumentWindow& window, Graphic
Font font (withDefaultMetrics (FontOptions { (float) h * 0.65f, Font::bold }));
g.setFont (font);
int textW = font.getStringWidth (window.getName());
int textW = GlyphArrangement::getStringWidthInt (font, window.getName());
int iconW = 0;
int iconH = 0;
@ -2137,7 +2136,7 @@ void LookAndFeel_V2::drawGroupComponentOutline (Graphics& g, int width, int heig
auto textW = text.isEmpty() ? 0
: jlimit (0.0f,
jmax (0.0f, w - cs2 - textEdgeGap * 2),
(float) f.getStringWidth (text) + textEdgeGap * 2.0f);
(float) GlyphArrangement::getStringWidthInt (f, text) + textEdgeGap * 2.0f);
auto textX = cs + textEdgeGap;
if (position.testFlags (Justification::horizontallyCentred))
@ -2190,8 +2189,9 @@ int LookAndFeel_V2::getTabButtonSpaceAroundImage()
int LookAndFeel_V2::getTabButtonBestWidth (TabBarButton& button, int tabDepth)
{
int width = Font (withDefaultMetrics (FontOptions { (float) tabDepth * 0.6f })).getStringWidth (button.getButtonText().trim())
+ getTabButtonOverlap (tabDepth) * 2;
int width = GlyphArrangement::getStringWidthInt (withDefaultMetrics (FontOptions { (float) tabDepth * 0.6f }),
button.getButtonText().trim())
+ getTabButtonOverlap (tabDepth) * 2;
if (auto* extraComponent = button.getExtraComponent())
width += button.getTabbedButtonBar().isVertical() ? extraComponent->getHeight()

View file

@ -247,7 +247,7 @@ void LookAndFeel_V4::drawDocumentWindowTitleBar (DocumentWindow& window, Graphic
Font font (withDefaultMetrics (FontOptions { (float) h * 0.65f, Font::plain }));
g.setFont (font);
auto textW = font.getStringWidth (window.getName());
auto textW = GlyphArrangement::getStringWidthInt (font, window.getName());
auto iconW = 0;
auto iconH = 0;
@ -386,7 +386,7 @@ void LookAndFeel_V4::changeToggleButtonWidthToFitText (ToggleButton& button)
Font font (withDefaultMetrics (FontOptions { fontSize }));
button.setSize (font.getStringWidth (button.getButtonText()) + roundToInt (tickWidth) + 14, button.getHeight());
button.setSize (GlyphArrangement::getStringWidthInt (font, button.getButtonText()) + roundToInt (tickWidth) + 14, button.getHeight());
}
//==============================================================================
@ -882,7 +882,7 @@ void LookAndFeel_V4::getIdealPopupMenuItemSize (const String& text, const bool i
font.setHeight ((float) standardMenuItemHeight / 1.3f);
idealHeight = standardMenuItemHeight > 0 ? standardMenuItemHeight : roundToInt (font.getHeight() * 1.3f);
idealWidth = font.getStringWidth (text) + idealHeight * 2;
idealWidth = GlyphArrangement::getStringWidthInt (font, text) + idealHeight * 2;
}
}

View file

@ -174,7 +174,7 @@ void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bo
if (leftOfOwnerComp)
{
auto width = jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f)
auto width = jmin (roundToInt (GlyphArrangement::getStringWidth (f, textValue.toString()) + 0.5f)
+ borderSize.getLeftAndRight(),
component.getX());

View file

@ -1389,7 +1389,7 @@ public:
void getContentSize (int& w, int& h) override
{
w = font.getStringWidth (text) + 18;
w = GlyphArrangement::getStringWidthInt (font, text) + 18;
h = (int) (font.getHeight() * 1.6f);
}

View file

@ -104,7 +104,7 @@ public:
{
lastAtom.atomText += first.atomText;
lastAtom.numChars = (uint16) (lastAtom.numChars + first.numChars);
lastAtom.width = font.getStringWidthFloat (lastAtom.getText (passwordChar));
lastAtom.width = GlyphArrangement::getStringWidth (font, lastAtom.getText (passwordChar));
++i;
}
}
@ -143,13 +143,13 @@ public:
{
TextAtom secondAtom;
secondAtom.atomText = atom.atomText.substring (indexToBreakAt - index);
secondAtom.width = font.getStringWidthFloat (secondAtom.getText (passwordChar));
secondAtom.width = GlyphArrangement::getStringWidth (font, secondAtom.getText (passwordChar));
secondAtom.numChars = (uint16) secondAtom.atomText.length();
section2->atoms.add (secondAtom);
atom.atomText = atom.atomText.substring (0, indexToBreakAt - index);
atom.width = font.getStringWidthFloat (atom.getText (passwordChar));
atom.width = GlyphArrangement::getStringWidth (font, atom.getText (passwordChar));
atom.numChars = (uint16) (indexToBreakAt - index);
for (int j = i + 1; j < atoms.size(); ++j)
@ -212,7 +212,7 @@ public:
passwordChar = passwordCharToUse;
for (auto& atom : atoms)
atom.width = newFont.getStringWidthFloat (atom.getText (passwordChar));
atom.width = GlyphArrangement::getStringWidth (newFont, atom.getText (passwordChar));
}
}
@ -272,7 +272,7 @@ private:
TextAtom atom;
atom.atomText = String (start, numChars);
atom.width = (atom.isNewLine() ? 0.0f : font.getStringWidthFloat (atom.getText (passwordChar)));
atom.width = (atom.isNewLine() ? 0.0f : GlyphArrangement::getStringWidth (font, atom.getText (passwordChar)));
atom.numChars = (uint16) numChars;
atoms.add (atom);
}

View file

@ -273,7 +273,7 @@ public:
setFont (font);
setText (message, false);
bestWidth = 2 * (int) std::sqrt (font.getHeight() * (float) font.getStringWidth (message));
bestWidth = 2 * (int) std::sqrt (font.getHeight() * GlyphArrangement::getStringWidth (font, message));
}
void updateLayout (const int width)
@ -386,8 +386,8 @@ void AlertWindow::updateLayout (const bool onlyIncreaseSize)
auto& lf = getLookAndFeel();
auto messageFont (lf.getAlertWindowMessageFont());
auto wid = jmax (messageFont.getStringWidth (text),
messageFont.getStringWidth (getName()));
auto wid = jmax (GlyphArrangement::getStringWidth (messageFont, text),
GlyphArrangement::getStringWidth (messageFont, getName()));
auto sw = (int) std::sqrt (messageFont.getHeight() * (float) wid);
auto w = jmin (300 + sw * 2, (int) ((float) getParentWidth() * 0.7f));

View file

@ -1696,7 +1696,13 @@ int CodeEditorComponent::columnToIndex (int lineNum, int column) const noexcept
void CodeEditorComponent::setFont (const Font& newFont)
{
font = newFont;
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
charWidth = font.getStringWidthFloat ("0");
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
JUCE_END_IGNORE_WARNINGS_MSVC
lineHeight = roundToInt (font.getHeight());
resized();
}

View file

@ -352,7 +352,7 @@ public:
colourLabel.setColour (Label::textWhenEditingColourId, textColour);
colourLabel.setText (currentColour.toDisplayString ((owner.flags & showAlphaChannel) != 0), dontSendNotification);
labelWidth = labelFont.getStringWidth (colourLabel.getText());
labelWidth = GlyphArrangement::getStringWidthInt (labelFont, colourLabel.getText());
repaint();
}

View file

@ -95,9 +95,14 @@ public:
void fitToContent (const int h) noexcept
{
if (keyNum < 0)
{
setSize (h, h);
}
else
setSize (jlimit (h * 4, h * 8, 6 + Font (withDefaultMetrics (FontOptions { (float) h * 0.6f })).getStringWidth (getName())), h);
{
const auto idealWidth = GlyphArrangement::getStringWidthInt (withDefaultMetrics (FontOptions { (float) h * 0.6f }), getName());
setSize (jlimit (h * 4, h * 8, 6 + idealWidth), h);
}
}
//==============================================================================