Browse Source

Initial

pull/1535/head
Benedikt Schroeder 8 years ago
parent
commit
d15aeef92b
  1. 99
      src/Avalonia.Visuals/Media/FontFamily.cs
  2. 33
      src/Avalonia.Visuals/Media/Typeface.cs

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

@ -0,0 +1,99 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Avalonia.Media
{
public class FontFamily
{
private IFontFamily _loadedFamily;
public FontFamily(string familyName) : this(familyName, null) { }
public FontFamily(string familyName, Uri baseUri)
{
if (familyName == null) throw new ArgumentNullException(nameof(familyName));
FontFamilyKey = new FontFamilyKey(familyName, baseUri);
}
public string Name => FontFamilyKey.FriendlyName;
public Uri BaseUri => FontFamilyKey.BaseUri;
internal FontFamilyKey FontFamilyKey { get; }
internal IFontFamily LoadedFamily
{
get
{
if (_loadedFamily == null)
{
_loadedFamily = AvaloniaLocator.Current.GetService<IFontFamilyLoader>().LoadFontFamily(FontFamilyKey);
}
return _loadedFamily;
}
}
public IEnumerable<FamilyTypeface> AvailableTypefaces => LoadedFamily.SupportedTypefaces;
}
public class FamilyTypeface
{
public FamilyTypeface()
{
FontStyle = FontStyle.Normal;
FontWeight = FontWeight.Normal;
}
public FamilyTypeface(Typeface typeface)
{
FontStyle = typeface.Style;
FontWeight = typeface.Weight;
}
public FontStyle FontStyle { get; }
public FontWeight FontWeight { get; }
}
public class FontFamilyKey
{
public FontFamilyKey(string friendlyName) : this(friendlyName, null) { }
public FontFamilyKey(string friendlyName, Uri baseUri)
{
FriendlyName = friendlyName;
BaseUri = baseUri;
}
public string FriendlyName { get; }
public Uri BaseUri { get; }
}
internal interface IFontFamily
{
IEnumerable<FamilyTypeface> SupportedTypefaces { get; }
}
internal class SystemFont : IFontFamily
{
public SystemFont() : this(new List<FamilyTypeface> { new FamilyTypeface() }) { }
public SystemFont(IEnumerable<FamilyTypeface> supportedTypefaces)
{
SupportedTypefaces = new ReadOnlyCollection<FamilyTypeface>(new List<FamilyTypeface>(supportedTypefaces));
}
public IEnumerable<FamilyTypeface> SupportedTypefaces { get; }
}
internal interface IFontFamilyLoader
{
IFontFamily LoadFontFamily(FontFamilyKey fontFamilyKey);
}
}

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

@ -7,18 +7,8 @@ namespace Avalonia.Media
/// </summary>
public class Typeface
{
/// <summary>
/// Initializes a new instance of the <see cref="Typeface"/> class.
/// </summary>
/// <param name="fontFamilyName">The name of the font family.</param>
/// <param name="fontSize">The font size, in DIPs.</param>
/// <param name="style">The font style.</param>
/// <param name="weight">The font weight.</param>
public Typeface(
string fontFamilyName,
double fontSize,
FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal)
public Typeface(FontFamily fontFamily, double fontSize, FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal)
{
if (fontSize <= 0)
{
@ -30,16 +20,29 @@ namespace Avalonia.Media
throw new ArgumentException("Font weight must be > 0.");
}
FontFamilyName = fontFamilyName;
FontFamily = fontFamily;
FontSize = fontSize;
Style = style;
Weight = weight;
}
/// <summary>
/// Gets the name of the font family.
/// Initializes a new instance of the <see cref="Typeface"/> class.
/// </summary>
/// <param name="fontFamilyName">The name of the font family.</param>
/// <param name="fontSize">The font size, in DIPs.</param>
/// <param name="style">The font style.</param>
/// <param name="weight">The font weight.</param>
public Typeface(
string fontFamilyName,
double fontSize,
FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal) : this(new FontFamily(fontFamilyName), fontSize, style, weight) { }
/// <summary>
/// Gets the font family.
/// </summary>
public string FontFamilyName { get; }
public FontFamily FontFamily { get; }
/// <summary>
/// Gets the size of the font in DIPs.

Loading…
Cancel
Save