Skip to content

Commit

Permalink
fix(wasm): Invalidate TextBlock measure cache when a font successfull…
Browse files Browse the repository at this point in the history
…y loaded
  • Loading branch information
jeromelaban committed Sep 6, 2022
1 parent 3b9abd9 commit f49af2a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Windows.Foundation;
using Uno;
using CachedSize = Uno.CachedTuple<double, double>;
using Windows.UI.Xaml.Media;

namespace Windows.UI.Xaml.Controls
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlockMeasureCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Uno.Extensions;
using Uno.UI;
using Uno.Foundation.Logging;
using Windows.UI.Xaml.Media;

namespace Windows.UI.Xaml.Controls
{
Expand All @@ -23,6 +24,8 @@ internal partial class TextBlockMeasureCache

private readonly Dictionary<MeasureKey, MeasureEntry> _entries = new Dictionary<MeasureKey, MeasureEntry>(new MeasureKey.Comparer());
private readonly LinkedList<MeasureKey> _queue = new LinkedList<MeasureKey>();

public static readonly TextBlockMeasureCache Instance = new TextBlockMeasureCache();

/// <summary>
/// Finds a cached measure for the provided <see cref="TextBlock"/> characteristics
Expand Down Expand Up @@ -98,6 +101,29 @@ public void CacheMeasure(TextBlock source, Size availableSize, Size measuredSize
entry.CacheMeasure(availableSize, measuredSize);
}

/// <summary>
///
/// </summary>
/// <param name="fontFamily"></param>
internal void Clear(FontFamily fontFamily)
{
List<MeasureKey> 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)
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/FontFamilyLoader.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f49af2a

Please sign in to comment.