Browse Source

Introduce abstract font manager

pull/3195/head
Benedikt Schroeder 7 years ago
parent
commit
872cef0176
  1. 6
      src/Avalonia.Visuals/Media/FontFamily.cs
  2. 106
      src/Avalonia.Visuals/Media/FontManager.cs
  3. 2
      src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs
  4. 2
      src/Avalonia.Visuals/Media/GlyphTypeface.cs
  5. 2
      src/Avalonia.Visuals/Media/Typeface.cs
  6. 2
      src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs
  7. 2
      src/Windows/Avalonia.Direct2D1/Media/GlyphTypefaceImpl.cs

6
src/Avalonia.Visuals/Media/FontFamily.cs

@ -8,11 +8,11 @@ using Avalonia.Media.Fonts;
namespace Avalonia.Media namespace Avalonia.Media
{ {
public class FontFamily public sealed class FontFamily
{ {
static FontFamily() static FontFamily()
{ {
Default = new FontFamily(FontManager.DefaultFontFamilyName); Default = new FontFamily(FontManager.Default.DefaultFontFamilyName);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -64,7 +64,7 @@ namespace Avalonia.Media
/// Consider using the new <see cref="FontManager"/> instead. /// Consider using the new <see cref="FontManager"/> instead.
/// </remarks> /// </remarks>
public static IEnumerable<FontFamily> SystemFontFamilies => public static IEnumerable<FontFamily> SystemFontFamilies =>
FontManager.GetInstalledFontFamilyNames().Select(name => new FontFamily(name)); FontManager.Default.GetInstalledFontFamilyNames().Select(name => new FontFamily(name));
/// <summary> /// <summary>
/// Gets the primary family name of the font family. /// Gets the primary family name of the font family.

106
src/Avalonia.Visuals/Media/FontManager.cs

@ -7,50 +7,106 @@ using Avalonia.Platform;
namespace Avalonia.Media namespace Avalonia.Media
{ {
public static class FontManager /// <summary>
/// The font manager is used to query the system's installed fonts and is responsible for caching loaded fonts.
/// It is also responsible for the font fallback.
/// </summary>
public abstract class FontManager
{ {
private static readonly IFontManagerImpl s_platformImpl = GetPlatformImpl(); public static readonly FontManager Default = CreateDefault();
/// <inheritdoc cref="IFontManagerImpl.DefaultFontFamilyName"/> /// <summary>
public static string DefaultFontFamilyName => s_platformImpl.DefaultFontFamilyName; /// Gets the system's default font family's name.
/// </summary>
public string DefaultFontFamilyName
{
get;
protected set;
}
/// <inheritdoc cref="IFontManagerImpl.GetInstalledFontFamilyNames"/> /// <summary>
public static IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false) => /// Get all installed fonts in the system.
s_platformImpl.GetInstalledFontFamilyNames(checkForUpdates); /// <param name="checkForUpdates">If <c>true</c> the font collection is updated.</param>
/// </summary>
public abstract IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false);
/// <inheritdoc cref="IFontManagerImpl.GetTypeface"/> /// <summary>
public static Typeface GetTypeface(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle) => /// Get a cached typeface from specified parameters.
s_platformImpl.GetTypeface(fontFamily, fontWeight, fontStyle); /// </summary>
/// <param name="fontFamily">The font family.</param>
/// <param name="fontWeight">The font weight.</param>
/// <param name="fontStyle">The font style.</param>
/// <returns>
/// The cached typeface.
/// </returns>
public abstract Typeface GetCachedTypeface(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle);
/// <inheritdoc cref="IFontManagerImpl.MatchCharacter"/> /// <summary>
public static Typeface MatchCharacter(int codepoint, FontWeight fontWeight = default, /// Tries to match a specified character to a typeface that supports specified font properties.
/// Returns <c>null</c> if no fallback was found.
/// </summary>
/// <param name="codepoint">The codepoint to match against.</param>
/// <param name="fontWeight">The font weight.</param>
/// <param name="fontStyle">The font style.</param>
/// <param name="fontFamily">The font family. This is optional and used for fallback lookup.</param>
/// <param name="culture">The culture.</param>
/// <returns>
/// The matched typeface.
/// </returns>
public abstract Typeface MatchCharacter(int codepoint, FontWeight fontWeight = default,
FontStyle fontStyle = default, FontStyle fontStyle = default,
FontFamily fontFamily = null, CultureInfo culture = null) => FontFamily fontFamily = null, CultureInfo culture = null);
s_platformImpl.MatchCharacter(codepoint, fontWeight, fontStyle, fontFamily, culture);
private static IFontManagerImpl GetPlatformImpl() public static FontManager CreateDefault()
{ {
var platformImpl = AvaloniaLocator.Current.GetService<IFontManagerImpl>(); var platformImpl = AvaloniaLocator.Current.GetService<IFontManagerImpl>();
return platformImpl ?? new EmptyFontManagerImpl(); if (platformImpl != null)
{
return new PlatformFontManager(platformImpl);
}
return new EmptyFontManager();
} }
private class EmptyFontManagerImpl : IFontManagerImpl private class PlatformFontManager : FontManager
{ {
public string DefaultFontFamilyName => "Arial"; private readonly IFontManagerImpl _platformImpl;
public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false) => new[] { "Arial" };
public Typeface GetTypeface(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle) public PlatformFontManager(IFontManagerImpl platformImpl)
{ {
return new Typeface(fontFamily, fontWeight, fontStyle); _platformImpl = platformImpl;
DefaultFontFamilyName = _platformImpl.DefaultFontFamilyName;
} }
public Typeface MatchCharacter(int codepoint, FontWeight fontWeight = default, FontStyle fontStyle = default, public override IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false) =>
FontFamily fontFamily = null, CultureInfo culture = null) _platformImpl.GetInstalledFontFamilyNames(checkForUpdates);
public override Typeface GetCachedTypeface(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle) =>
_platformImpl.GetTypeface(fontFamily, fontWeight, fontStyle);
public override Typeface MatchCharacter(int codepoint, FontWeight fontWeight = default,
FontStyle fontStyle = default,
FontFamily fontFamily = null, CultureInfo culture = null) =>
_platformImpl.MatchCharacter(codepoint, fontWeight, fontStyle, fontFamily, culture);
}
private class EmptyFontManager : FontManager
{
public EmptyFontManager()
{ {
return null; DefaultFontFamilyName = "Empty";
} }
public override IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false) =>
new[] { DefaultFontFamilyName };
public override Typeface GetCachedTypeface(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle) => new Typeface(fontFamily, fontWeight, fontStyle);
public override Typeface MatchCharacter(int codepoint, FontWeight fontWeight = default,
FontStyle fontStyle = default,
FontFamily fontFamily = null, CultureInfo culture = null) => null;
} }
} }
} }

2
src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs

@ -9,7 +9,7 @@ using System.Text;
namespace Avalonia.Media.Fonts namespace Avalonia.Media.Fonts
{ {
public class FamilyNameCollection : IReadOnlyList<string> public sealed class FamilyNameCollection : IReadOnlyList<string>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FamilyNameCollection"/> class. /// Initializes a new instance of the <see cref="FamilyNameCollection"/> class.

2
src/Avalonia.Visuals/Media/GlyphTypeface.cs

@ -7,7 +7,7 @@ using Avalonia.Platform;
namespace Avalonia.Media namespace Avalonia.Media
{ {
public class GlyphTypeface : IDisposable public sealed class GlyphTypeface : IDisposable
{ {
private static readonly IPlatformRenderInterface s_platformRenderInterface = private static readonly IPlatformRenderInterface s_platformRenderInterface =
AvaloniaLocator.Current.GetService<IPlatformRenderInterface>(); AvaloniaLocator.Current.GetService<IPlatformRenderInterface>();

2
src/Avalonia.Visuals/Media/Typeface.cs

@ -10,7 +10,7 @@ namespace Avalonia.Media
/// <summary> /// <summary>
/// Represents a typeface. /// Represents a typeface.
/// </summary> /// </summary>
[DebuggerDisplay("Name = {FontFamily.Name}, Style = {Style}, Weight = {Weight}")] [DebuggerDisplay("Name = {FontFamily.Name}, Weight = {Weight}, Style = {Style}")]
public class Typeface : IEquatable<Typeface> public class Typeface : IEquatable<Typeface>
{ {
public static readonly Typeface Default = new Typeface(FontFamily.Default); public static readonly Typeface Default = new Typeface(FontFamily.Default);

2
src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs

@ -10,7 +10,7 @@ using SkiaSharp;
namespace Avalonia.Skia namespace Avalonia.Skia
{ {
internal class GlyphTypefaceImpl : IGlyphTypefaceImpl public class GlyphTypefaceImpl : IGlyphTypefaceImpl
{ {
private bool _isDisposed; private bool _isDisposed;

2
src/Windows/Avalonia.Direct2D1/Media/GlyphTypefaceImpl.cs

@ -9,7 +9,7 @@ using SharpDX.DirectWrite;
namespace Avalonia.Direct2D1.Media namespace Avalonia.Direct2D1.Media
{ {
internal class GlyphTypefaceImpl : IGlyphTypefaceImpl public class GlyphTypefaceImpl : IGlyphTypefaceImpl
{ {
private bool _isDisposed; private bool _isDisposed;

Loading…
Cancel
Save