Cache GlyphRun
MODIFY
WalletWasabi.Fluent/Controls/PrivacyTextPresenter.cs
WalletWasabi.Fluent/Controls/PrivacyTextPresenter.cs
@@ -10,6 +10,28 @@ namespace WalletWasabi.Fluent.Controls;
public class PrivacyTextPresenter : UserControl
{
+ private static GlyphRun CreateGlyphRun(double width, FontFamily fontFamily, double fontSize)
+ {
+ var privacyChar = UIConstants.PrivacyChar;
+
+ var glyphTypeface = new Typeface((FontFamily?) fontFamily).GlyphTypeface;
+ var glyph = glyphTypeface.GetGlyph(privacyChar);
+
+ var scale = fontSize / glyphTypeface.DesignEmHeight;
+ var advance = glyphTypeface.GetGlyphAdvance(glyph) * scale;
+
+ var count = (int) (width / advance);
+
+ var advances = new ReadOnlySlice<double>(new ReadOnlyMemory<double>(Enumerable.Repeat(advance, count).ToArray()));
+ var characters = new ReadOnlySlice<char>(new ReadOnlyMemory<char>(Enumerable.Repeat(privacyChar, count).ToArray()));
+ var glyphs = new ReadOnlySlice<ushort>(new ReadOnlyMemory<ushort>(Enumerable.Repeat(glyph, count).ToArray()));
+
+ return new GlyphRun(glyphTypeface, fontSize, glyphs, advances, characters: characters);
+ }
+
+ private GlyphRun? _glyphRun;
+ private double _width;
+
protected override Size MeasureOverride(Size availableSize)
{
var formattedText = new FormattedText(
@@ -30,22 +52,14 @@ public class PrivacyTextPresenter : UserControl
return;
}
- var privacyChar = UIConstants.PrivacyChar;
-
- var glyphTypeface = new Typeface((FontFamily?)FontFamily).GlyphTypeface;
- var glyph = glyphTypeface.GetGlyph(privacyChar);
-
- var scale = FontSize / glyphTypeface.DesignEmHeight;
- var advance = glyphTypeface.GetGlyphAdvance(glyph) * scale;
-
- var count = (int)(Bounds.Width / advance);
-
- var advances = new ReadOnlySlice<double>(new ReadOnlyMemory<double>(Enumerable.Repeat(advance, count).ToArray()));
- var characters = new ReadOnlySlice<char>(new ReadOnlyMemory<char>(Enumerable.Repeat(privacyChar, count).ToArray()));
- var glyphs = new ReadOnlySlice<ushort>(new ReadOnlyMemory<ushort>(Enumerable.Repeat(glyph, count).ToArray()));
-
- var glyphRun = new GlyphRun(glyphTypeface, FontSize, glyphs, advances, characters: characters);
+ var width = Bounds.Width;
+ if (_glyphRun is null || _width != _width)
+ {
+ (_glyphRun as IDisposable)?.Dispose();
+ _glyphRun = CreateGlyphRun(width, FontFamily, FontSize);
+ _width = width;
+ }
- context.DrawGlyphRun(Foreground, glyphRun);
+ context.DrawGlyphRun(Foreground, _glyphRun);
}
}

Cache GlyphRun