diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs index 767d16cca96b..d356eaea0f72 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs @@ -17,7 +17,6 @@ partial class TextBlock : FrameworkElement { private const int MaxMeasureCache = 50; - private static TextBlockMeasureCache _cache = new TextBlockMeasureCache(); private bool _fontStyleChanged; private bool _fontWeightChanged; private bool _textChanged; @@ -102,7 +101,7 @@ protected override Size MeasureOverride(Size availableSize) if (UseInlinesFastPath) { - if (_cache.FindMeasuredSize(this, availableSize) is Size desiredSize) + if (TextBlockMeasureCache.Instance.FindMeasuredSize(this, availableSize) is Size desiredSize) { UnoMetrics.TextBlock.MeasureCacheHits++; return desiredSize; @@ -112,7 +111,7 @@ protected override Size MeasureOverride(Size availableSize) UnoMetrics.TextBlock.MeasureCacheMisses++; desiredSize = MeasureView(availableSize); - _cache.CacheMeasure(this, availableSize, desiredSize); + TextBlockMeasureCache.Instance.CacheMeasure(this, availableSize, desiredSize); return desiredSize; } diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureEntry.cs b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureEntry.cs index bd429da9c29c..6d0fc14223cb 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureEntry.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureEntry.cs @@ -3,6 +3,7 @@ using Windows.Foundation; using Uno; using CachedSize = Uno.CachedTuple; +using Windows.UI.Xaml.Media; namespace Windows.UI.Xaml.Controls { diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureKey.cs b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureKey.cs index 30825f33eb3f..23ca5df5954d 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureKey.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.MeasureKey.cs @@ -89,6 +89,8 @@ public override bool Equals(object obj) private readonly int _characterSpacing; private readonly TextDecorations _textDecorations; + public FontFamily FontFamily => _fontFamily; + internal bool IsWrapping => _textWrapping != TextWrapping.NoWrap; internal bool IsClipping => _textTrimming != TextTrimming.None; } diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.cs b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.cs index 419e97ea068a..c1c6f3b1364e 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.cs @@ -8,6 +8,7 @@ using Uno.Extensions; using Uno.UI; using Uno.Foundation.Logging; +using Windows.UI.Xaml.Media; namespace Windows.UI.Xaml.Controls { @@ -23,6 +24,8 @@ internal partial class TextBlockMeasureCache private readonly Dictionary _entries = new Dictionary(new MeasureKey.Comparer()); private readonly LinkedList _queue = new LinkedList(); + + public static readonly TextBlockMeasureCache Instance = new TextBlockMeasureCache(); /// /// Finds a cached measure for the provided characteristics @@ -98,6 +101,29 @@ public void CacheMeasure(TextBlock source, Size availableSize, Size measuredSize entry.CacheMeasure(availableSize, measuredSize); } + /// + /// + /// + /// + internal void Clear(FontFamily fontFamily) + { + List keysToRemove = new(); + + foreach(var item in _queue) + { + if (item.FontFamily.CssFontName == fontFamily.CssFontName) + { + keysToRemove.Add(item); + } + } + + foreach(var keyToRemove in keysToRemove) + { + _queue.Remove(keyToRemove); + _entries.Remove(keyToRemove); + } + } + private void Scavenge() { while (_queue.Count >= MaxMeasureKeyEntries) diff --git a/src/Uno.UI/UI/Xaml/FontFamilyLoader.wasm.cs b/src/Uno.UI/UI/Xaml/FontFamilyLoader.wasm.cs index a4c7ce454e43..dc74dae3298d 100644 --- a/src/Uno.UI/UI/Xaml/FontFamilyLoader.wasm.cs +++ b/src/Uno.UI/UI/Xaml/FontFamilyLoader.wasm.cs @@ -72,6 +72,8 @@ internal static void NotifyFontLoaded(string cssFontName) if (loader._waitingList is { Count: > 0 }) { + Controls.TextBlockMeasureCache.Instance.Clear(loader._fontFamily); + foreach (var waiting in loader._waitingList) { if (waiting.IsAlive && waiting.Target is UIElement ue)