From d15aeef92bc42c2551b0bb7ac93f3798baed151e Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Mon, 30 Apr 2018 08:59:42 +0200 Subject: [PATCH] Initial --- src/Avalonia.Visuals/Media/FontFamily.cs | 99 ++++++++++++++++++++++++ src/Avalonia.Visuals/Media/Typeface.cs | 33 ++++---- 2 files changed, 117 insertions(+), 15 deletions(-) create mode 100644 src/Avalonia.Visuals/Media/FontFamily.cs diff --git a/src/Avalonia.Visuals/Media/FontFamily.cs b/src/Avalonia.Visuals/Media/FontFamily.cs new file mode 100644 index 0000000000..ea2d3701b2 --- /dev/null +++ b/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().LoadFontFamily(FontFamilyKey); + } + + return _loadedFamily; + } + } + + public IEnumerable 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 SupportedTypefaces { get; } + } + + internal class SystemFont : IFontFamily + { + public SystemFont() : this(new List { new FamilyTypeface() }) { } + + public SystemFont(IEnumerable supportedTypefaces) + { + SupportedTypefaces = new ReadOnlyCollection(new List(supportedTypefaces)); + } + + public IEnumerable SupportedTypefaces { get; } + } + + internal interface IFontFamilyLoader + { + IFontFamily LoadFontFamily(FontFamilyKey fontFamilyKey); + } +} diff --git a/src/Avalonia.Visuals/Media/Typeface.cs b/src/Avalonia.Visuals/Media/Typeface.cs index 12540b67e7..c85144d48e 100644 --- a/src/Avalonia.Visuals/Media/Typeface.cs +++ b/src/Avalonia.Visuals/Media/Typeface.cs @@ -7,18 +7,8 @@ namespace Avalonia.Media /// public class Typeface { - /// - /// Initializes a new instance of the class. - /// - /// The name of the font family. - /// The font size, in DIPs. - /// The font style. - /// The font weight. - 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; } /// - /// Gets the name of the font family. + /// Initializes a new instance of the class. + /// + /// The name of the font family. + /// The font size, in DIPs. + /// The font style. + /// The font weight. + public Typeface( + string fontFamilyName, + double fontSize, + FontStyle style = FontStyle.Normal, + FontWeight weight = FontWeight.Normal) : this(new FontFamily(fontFamilyName), fontSize, style, weight) { } + + /// + /// Gets the font family. /// - public string FontFamilyName { get; } + public FontFamily FontFamily { get; } /// /// Gets the size of the font in DIPs.