Browse Source
Merge pull request #2227 from jeffreye/system-fonts
System fonts
pull/2246/head
Nikita Tsukanov
7 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with
44 additions and
1 deletions
-
samples/ControlCatalog/Pages/DropDownPage.xaml
-
samples/ControlCatalog/Pages/DropDownPage.xaml.cs
-
src/Avalonia.Visuals/Media/FontFamily.cs
-
src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs
-
src/Skia/Avalonia.Skia/PlatformRenderInterface.cs
-
src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs
-
src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs
-
tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs
-
tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs
|
|
|
@ -27,6 +27,15 @@ |
|
|
|
<TextBox Text="TextBox"/> |
|
|
|
</DropDownItem> |
|
|
|
</DropDown> |
|
|
|
|
|
|
|
|
|
|
|
<DropDown x:Name="fontDropDown" SelectedIndex="0"> |
|
|
|
<DropDown.ItemTemplate> |
|
|
|
<DataTemplate> |
|
|
|
<TextBlock Text="{Binding Name}" FontFamily="{Binding}" /> |
|
|
|
</DataTemplate> |
|
|
|
</DropDown.ItemTemplate> |
|
|
|
</DropDown> |
|
|
|
</StackPanel> |
|
|
|
|
|
|
|
</StackPanel> |
|
|
|
|
|
|
|
@ -13,6 +13,9 @@ namespace ControlCatalog.Pages |
|
|
|
private void InitializeComponent() |
|
|
|
{ |
|
|
|
AvaloniaXamlLoader.Load(this); |
|
|
|
var fontDropDown = this.Find<DropDown>("fontDropDown"); |
|
|
|
fontDropDown.Items = Avalonia.Media.FontFamily.SystemFontFamilies; |
|
|
|
fontDropDown.SelectedIndex = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -5,6 +5,7 @@ using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using Avalonia.Media.Fonts; |
|
|
|
using Avalonia.Platform; |
|
|
|
|
|
|
|
namespace Avalonia.Media |
|
|
|
{ |
|
|
|
@ -51,6 +52,12 @@ namespace Avalonia.Media |
|
|
|
/// </summary>
|
|
|
|
public static FontFamily Default => new FontFamily(String.Empty); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents all font families in the system. This can be an expensive call depending on platform implementation.
|
|
|
|
/// </summary>
|
|
|
|
public static IEnumerable<FontFamily> SystemFontFamilies => |
|
|
|
AvaloniaLocator.Current.GetService<IPlatformRenderInterface>().InstalledFontNames.Select(name => new FontFamily(name)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the primary family name of the font family.
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
@ -13,6 +13,11 @@ namespace Avalonia.Platform |
|
|
|
/// </summary>
|
|
|
|
public interface IPlatformRenderInterface |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Get all installed fonts in the system
|
|
|
|
/// </summary>
|
|
|
|
IEnumerable<string> InstalledFontNames { get; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a formatted text implementation.
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
@ -19,6 +19,8 @@ namespace Avalonia.Skia |
|
|
|
{ |
|
|
|
private GRContext GrContext { get; } |
|
|
|
|
|
|
|
public IEnumerable<string> InstalledFontNames => SKFontManager.Default.FontFamilies; |
|
|
|
|
|
|
|
public PlatformRenderInterface() |
|
|
|
{ |
|
|
|
var gl = AvaloniaLocator.Current.GetService<IWindowingPlatformGlFeature>(); |
|
|
|
|
|
|
|
@ -41,6 +41,19 @@ namespace Avalonia.Direct2D1 |
|
|
|
|
|
|
|
public static SharpDX.DXGI.Device1 DxgiDevice { get; private set; } |
|
|
|
|
|
|
|
public IEnumerable<string> InstalledFontNames |
|
|
|
{ |
|
|
|
get |
|
|
|
{ |
|
|
|
var cache = Direct2D1FontCollectionCache.s_installedFontCollection; |
|
|
|
var length = cache.FontFamilyCount; |
|
|
|
for (int i = 0; i < length; i++) |
|
|
|
{ |
|
|
|
var names = cache.GetFontFamily(i).FamilyNames; |
|
|
|
yield return names.GetString(0); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static readonly object s_initLock = new object(); |
|
|
|
private static bool s_initialized = false; |
|
|
|
|
|
|
|
@ -7,7 +7,7 @@ namespace Avalonia.Direct2D1.Media |
|
|
|
internal static class Direct2D1FontCollectionCache |
|
|
|
{ |
|
|
|
private static readonly ConcurrentDictionary<FontFamilyKey, SharpDX.DirectWrite.FontCollection> s_cachedCollections; |
|
|
|
private static readonly SharpDX.DirectWrite.FontCollection s_installedFontCollection; |
|
|
|
internal static readonly SharpDX.DirectWrite.FontCollection s_installedFontCollection; |
|
|
|
|
|
|
|
static Direct2D1FontCollectionCache() |
|
|
|
{ |
|
|
|
|
|
|
|
@ -9,6 +9,8 @@ namespace Avalonia.UnitTests |
|
|
|
{ |
|
|
|
public class MockPlatformRenderInterface : IPlatformRenderInterface |
|
|
|
{ |
|
|
|
public IEnumerable<string> InstalledFontNames => new string[0]; |
|
|
|
|
|
|
|
public IFormattedTextImpl CreateFormattedText( |
|
|
|
string text, |
|
|
|
Typeface typeface, |
|
|
|
|
|
|
|
@ -8,6 +8,8 @@ namespace Avalonia.Visuals.UnitTests.VisualTree |
|
|
|
{ |
|
|
|
class MockRenderInterface : IPlatformRenderInterface |
|
|
|
{ |
|
|
|
public IEnumerable<string> InstalledFontNames => new string[0]; |
|
|
|
|
|
|
|
public IFormattedTextImpl CreateFormattedText( |
|
|
|
string text, |
|
|
|
Typeface typeface, |
|
|
|
|