From e704823ced4edac89a38ec1be916108bc6ebb055 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Fri, 6 Dec 2019 16:11:01 +0100 Subject: [PATCH] Introduce a struct enumerator implementation for ReadOnlyList that can be reused --- .../ImmutableReadOnlyListStructEnumerator.cs | 48 +++++++++++++++++++ src/Avalonia.Visuals/Media/FontManager.cs | 4 +- .../Media/Fonts/FamilyNameCollection.cs | 48 ++----------------- src/Skia/Avalonia.Skia/FontManagerImpl.cs | 14 +++--- .../VisualTree/MockRenderInterface.cs | 3 +- 5 files changed, 62 insertions(+), 55 deletions(-) create mode 100644 src/Avalonia.Base/Utilities/ImmutableReadOnlyListStructEnumerator.cs diff --git a/src/Avalonia.Base/Utilities/ImmutableReadOnlyListStructEnumerator.cs b/src/Avalonia.Base/Utilities/ImmutableReadOnlyListStructEnumerator.cs new file mode 100644 index 0000000000..a1246c57b5 --- /dev/null +++ b/src/Avalonia.Base/Utilities/ImmutableReadOnlyListStructEnumerator.cs @@ -0,0 +1,48 @@ +using System.Collections; +using System.Collections.Generic; + +namespace Avalonia.Utilities +{ + public struct ImmutableReadOnlyListStructEnumerator : IEnumerator, IEnumerator + { + private readonly IReadOnlyList _readOnlyList; + private int _pos; + + public ImmutableReadOnlyListStructEnumerator(IReadOnlyList readOnlyList) + { + _readOnlyList = readOnlyList; + _pos = -1; + Current = default; + } + + public T Current + { + get; + private set; + } + + object IEnumerator.Current => Current; + + public void Dispose() { } + + public bool MoveNext() + { + if (_pos >= _readOnlyList.Count - 1) + { + return false; + } + + Current = _readOnlyList[++_pos]; + + return true; + + } + + public void Reset() + { + _pos = -1; + + Current = default; + } + } +} diff --git a/src/Avalonia.Visuals/Media/FontManager.cs b/src/Avalonia.Visuals/Media/FontManager.cs index 27ed9e64da..0c5e88b47a 100644 --- a/src/Avalonia.Visuals/Media/FontManager.cs +++ b/src/Avalonia.Visuals/Media/FontManager.cs @@ -70,9 +70,9 @@ namespace Avalonia.Media } /// - /// Get all installed fonts. - /// If true the font collection is updated. + /// Get all installed font family names. /// + /// If true the font collection is updated. public IEnumerable GetInstalledFontFamilyNames(bool checkForUpdates = false) => PlatformImpl.GetInstalledFontFamilyNames(checkForUpdates); diff --git a/src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs b/src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs index 8d2fd076c8..cd08bba7b2 100644 --- a/src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs +++ b/src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; +using Avalonia.Utilities; namespace Avalonia.Media.Fonts { @@ -57,9 +58,9 @@ namespace Avalonia.Media.Fonts /// /// Returns an enumerator for the name collection. /// - public Enumerator GetEnumerator() + public ImmutableReadOnlyListStructEnumerator GetEnumerator() { - return new Enumerator(this); + return new ImmutableReadOnlyListStructEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() @@ -72,49 +73,6 @@ namespace Avalonia.Media.Fonts return GetEnumerator(); } - public struct Enumerator : IEnumerator, IEnumerator - { - private readonly IReadOnlyList _names; - private int _pos; - - public Enumerator(IReadOnlyList names) - { - _names = names; - _pos = -1; - Current = default; - } - - public string Current - { - get; - private set; - } - - object IEnumerator.Current => Current; - - public void Dispose() { } - - public bool MoveNext() - { - if (_pos >= _names.Count - 1) - { - return false; - } - - Current = _names[++_pos]; - - return true; - - } - - public void Reset() - { - _pos = -1; - - Current = default; - } - } - /// /// Returns a that represents this instance. /// diff --git a/src/Skia/Avalonia.Skia/FontManagerImpl.cs b/src/Skia/Avalonia.Skia/FontManagerImpl.cs index 9cfa685191..727947e59d 100644 --- a/src/Skia/Avalonia.Skia/FontManagerImpl.cs +++ b/src/Skia/Avalonia.Skia/FontManagerImpl.cs @@ -30,7 +30,7 @@ namespace Avalonia.Skia return _skFontManager.FontFamilies; } - [ThreadStatic] private static string[] s_languageTagBuffer; + [ThreadStatic] private static string[] t_languageTagBuffer; public bool TryMatchCharacter(int codepoint, FontWeight fontWeight, FontStyle fontStyle, FontFamily fontFamily, CultureInfo culture, out FontKey fontKey) @@ -40,20 +40,20 @@ namespace Avalonia.Skia culture = CultureInfo.CurrentUICulture; } - if (s_languageTagBuffer == null) + if (t_languageTagBuffer == null) { - s_languageTagBuffer = new string[2]; + t_languageTagBuffer = new string[2]; } - s_languageTagBuffer[0] = culture.TwoLetterISOLanguageName; - s_languageTagBuffer[1] = culture.ThreeLetterISOLanguageName; + t_languageTagBuffer[0] = culture.TwoLetterISOLanguageName; + t_languageTagBuffer[1] = culture.ThreeLetterISOLanguageName; if (fontFamily != null) { foreach (var familyName in fontFamily.FamilyNames) { var skTypeface = _skFontManager.MatchCharacter(familyName, (SKFontStyleWeight)fontWeight, - SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, s_languageTagBuffer, codepoint); + SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, t_languageTagBuffer, codepoint); if (skTypeface == null) { @@ -68,7 +68,7 @@ namespace Avalonia.Skia else { var skTypeface = _skFontManager.MatchCharacter(null, (SKFontStyleWeight)fontWeight, - SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, s_languageTagBuffer, codepoint); + SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, t_languageTagBuffer, codepoint); if (skTypeface != null) { diff --git a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs index 5cd313b169..300c6e359e 100644 --- a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs +++ b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using Avalonia.Media; using Avalonia.Platform; +using Avalonia.UnitTests; namespace Avalonia.Visuals.UnitTests.VisualTree { @@ -57,7 +58,7 @@ namespace Avalonia.Visuals.UnitTests.VisualTree public IFontManagerImpl CreateFontManager() { - throw new NotImplementedException(); + return new MockFontManagerImpl(); } public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat? fmt)