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

Font: Return correct result from getStringWidth after setting size in points

Previously, code such as the following would return a smaller string
width for larger tracking values:

    juce::Font f { juce::FontOptions{}.withPointHeight (16.0f) };
    const auto g = f.withExtraKerningFactor (1.0f);

    const auto a = f.getStringWidth ("foobar");
    const auto b = g.getStringWidth ("foobar");

With this change applied, the width 'b' is greater than the width 'a',
as expected.
This commit is contained in:
reuk 2024-05-29 13:39:41 +01:00
parent e8b38b8922
commit d69dee0f5b
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C

View file

@ -763,22 +763,22 @@ int Font::getStringWidth (const String& text) const
float Font::getStringWidthFloat (const String& text) const
{
const auto w = getTypefacePtr()->getStringWidth (font->getMetricsKind(), text, getHeight(), getHorizontalScale());
return w + (font->getHeight() * font->getHorizontalScale() * font->getKerning() * (float) text.length());
const auto w = getTypefacePtr()->getStringWidth (getMetricsKind(), text, getHeight(), getHorizontalScale());
return w + (getHeight() * getHorizontalScale() * getExtraKerningFactor() * (float) text.length());
}
void Font::getGlyphPositions (const String& text, Array<int>& glyphs, Array<float>& xOffsets) const
{
getTypefacePtr()->getGlyphPositions (font->getMetricsKind(), text, glyphs, xOffsets, getHeight(), getHorizontalScale());
getTypefacePtr()->getGlyphPositions (getMetricsKind(), text, glyphs, xOffsets, getHeight(), getHorizontalScale());
if (auto num = xOffsets.size())
{
auto scale = font->getHeight() * font->getHorizontalScale();
auto scale = getHeight() * getHorizontalScale();
auto* x = xOffsets.getRawDataPointer();
if (! approximatelyEqual (font->getKerning(), 0.0f))
if (! approximatelyEqual (getExtraKerningFactor(), 0.0f))
for (int i = 0; i < num; ++i)
x[i] += ((float) i * font->getKerning() * scale);
x[i] += ((float) i * getExtraKerningFactor() * scale);
}
}