Skip to content

Commit 5b0a2b9

Browse files
Font: Cache HarfBuzz fonts and font details
1 parent c0f164e commit 5b0a2b9

File tree

8 files changed

+287
-186
lines changed

8 files changed

+287
-186
lines changed

modules/juce_graphics/detail/juce_SimpleShapedText.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static std::vector<ShapedGlyph> lowLevelShape (Span<const juce_wchar> string,
219219
{
220220
static const auto language = SystemStats::getDisplayLanguage();
221221

222-
HbBuffer buffer { hb_buffer_create() };
222+
HbBuffer buffer { hb_buffer_create(), IncrementRef::no };
223223
hb_buffer_clear_contents (buffer.get());
224224

225225
hb_buffer_set_cluster_level (buffer.get(), HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES);
@@ -316,20 +316,14 @@ static std::vector<ShapedGlyph> lowLevelShape (Span<const juce_wchar> string,
316316
const auto xAdvanceBase = HbScale::hbToJuce (positions[visualIndex].x_advance);
317317
const auto yAdvanceBase = -HbScale::hbToJuce (positions[visualIndex].y_advance);
318318

319-
// For certain OS, Font and glyph ID combinations harfbuzz will not find extents data and
320-
// hb_font_get_glyph_extents will return false. In such cases Typeface::getGlyphBounds
321-
// will return an empty rectangle. Here we need to distinguish this situation from the one
322-
// where extents information is available and is an empty rectangle, which indicates a
323-
// whitespace.
324-
const auto extentsDataAvailable = std::invoke ([&]
325-
{
326-
hb_glyph_extents_t extents{};
327-
return hb_font_get_glyph_extents (font.getTypefacePtr()->getNativeDetails().getFont(),
328-
(hb_codepoint_t) glyphId,
329-
&extents);
330-
});
319+
// For certain OS, Font and glyph ID combinations harfbuzz will not find extents data.
320+
// In such cases Typeface::getGlyphBounds will return an empty rectangle. Here we need
321+
// to distinguish this situation from the one where extents information is available
322+
// and is an empty rectangle, which indicates a whitespace.
323+
const auto* native = font.getTypefacePtr()->getNativeDetails();
324+
const auto extents = native->getGlyphExtents (glyphId);
331325

332-
const auto whitespace = extentsDataAvailable
326+
const auto whitespace = extents.has_value()
333327
&& font.getTypefacePtr()->getGlyphBounds (font.getMetricsKind(), (int) glyphId).isEmpty()
334328
&& xAdvanceBase > 0;
335329

modules/juce_graphics/fonts/juce_Font.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Font::SharedFontInternal : public ReferenceCountedObject
220220
const ScopedLock lock (mutex);
221221

222222
if (auto ptr = getTypefacePtr (f))
223-
return ptr->getNativeDetails().getFontAtPointSizeAndScale (f.getHeightInPoints(), f.getHorizontalScale());
223+
return ptr->getNativeDetails()->getFontAtPointSizeAndScale (f.getHeightInPoints(), f.getHorizontalScale());
224224

225225
return {};
226226
}
@@ -231,7 +231,7 @@ class Font::SharedFontInternal : public ReferenceCountedObject
231231

232232
if (auto ptr = getTypefacePtr (f))
233233
{
234-
const auto ascentDescent = ptr->getNativeDetails().getAscentDescent (f.getMetricsKind());
234+
const auto ascentDescent = ptr->getNativeDetails()->getAscentDescent (f.getMetricsKind());
235235

236236
auto adjusted = ascentDescent;
237237
adjusted.ascent = getAscentOverride().value_or (adjusted.ascent);
@@ -886,7 +886,7 @@ static bool characterNotRendered (uint32_t c)
886886

887887
static bool isFontSuitableForCodepoint (const Font& font, juce_wchar c)
888888
{
889-
const auto& hbFont = font.getNativeDetails().font;
889+
const auto hbFont = font.getNativeDetails().font;
890890

891891
if (hbFont == nullptr)
892892
return false;
@@ -994,7 +994,7 @@ Font::Native Font::getNativeDetails() const
994994

995995
Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
996996
{
997-
const auto resolvedTypeface = [&]() -> Typeface::Ptr
997+
const auto resolvedTypeface = std::invoke ([&]() -> Typeface::Ptr
998998
{
999999
if (font.getTypefaceName() != getSystemUIFontName())
10001000
return {};
@@ -1010,7 +1010,7 @@ Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font)
10101010
auto copy = font;
10111011
copy.setTypefaceName (systemTypeface->getName());
10121012
return getDefaultTypefaceForFont (copy);
1013-
}();
1013+
});
10141014

10151015
if (resolvedTypeface != nullptr)
10161016
return resolvedTypeface;

0 commit comments

Comments
 (0)