|
|
|
@ -11,46 +11,49 @@ namespace Avalonia.Media |
|
|
|
{ |
|
|
|
public class FontFamily |
|
|
|
{ |
|
|
|
/// <inheritdoc />
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="T:Avalonia.Media.FontFamily" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the <see cref="FontFamily"/>.</param>
|
|
|
|
/// <exception cref="T:System.ArgumentNullException">name</exception>
|
|
|
|
public FontFamily(string name) |
|
|
|
/// <param name="name">The name of the <see cref="T:Avalonia.Media.FontFamily" />.</param>
|
|
|
|
public FontFamily(string name) : this(null, name) |
|
|
|
{ |
|
|
|
Contract.Requires<ArgumentNullException>(name != null); |
|
|
|
|
|
|
|
FamilyNames = new FamilyNameCollection(new[] { name }); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="T:Avalonia.Media.FontFamily" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="names">The names of the <see cref="FontFamily"/>.</param>
|
|
|
|
/// <exception cref="T:System.ArgumentNullException">name</exception>
|
|
|
|
public FontFamily(IEnumerable<string> names) |
|
|
|
/// <param name="baseUri">Specifies the base uri that is used to resolve font family assets.</param>
|
|
|
|
/// <param name="name">The name of the <see cref="T:Avalonia.Media.FontFamily" />.</param>
|
|
|
|
/// <exception cref="T:System.ArgumentException">Base uri must be an absolute uri.</exception>
|
|
|
|
public FontFamily(Uri baseUri, string name) |
|
|
|
{ |
|
|
|
Contract.Requires<ArgumentNullException>(names != null); |
|
|
|
if (string.IsNullOrEmpty(name)) |
|
|
|
{ |
|
|
|
FamilyNames = new FamilyNameCollection(string.Empty); |
|
|
|
|
|
|
|
FamilyNames = new FamilyNameCollection(names); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="T:Avalonia.Media.FontFamily" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the <see cref="T:Avalonia.Media.FontFamily" />.</param>
|
|
|
|
/// <param name="source">The source of font resources.</param>
|
|
|
|
/// <param name="baseUri"></param>
|
|
|
|
public FontFamily(string name, Uri source, Uri baseUri = null) : this(name) |
|
|
|
{ |
|
|
|
Key = new FontFamilyKey(source, baseUri); |
|
|
|
var fontFamilySegment = GetFontFamilyIdentifier(name); |
|
|
|
|
|
|
|
if (fontFamilySegment.Source != null) |
|
|
|
{ |
|
|
|
if (baseUri != null && !baseUri.IsAbsoluteUri) |
|
|
|
{ |
|
|
|
throw new ArgumentException("Base uri must be an absolute uri.", nameof(baseUri)); |
|
|
|
} |
|
|
|
|
|
|
|
Key = new FontFamilyKey(fontFamilySegment.Source, baseUri); |
|
|
|
} |
|
|
|
|
|
|
|
FamilyNames = new FamilyNameCollection(fontFamilySegment.Name); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents the default font family
|
|
|
|
/// </summary>
|
|
|
|
public static FontFamily Default => new FontFamily(String.Empty); |
|
|
|
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.
|
|
|
|
@ -88,46 +91,40 @@ namespace Avalonia.Media |
|
|
|
/// <param name="s"></param>
|
|
|
|
public static implicit operator FontFamily(string s) |
|
|
|
{ |
|
|
|
return Parse(s); |
|
|
|
return new FontFamily(s); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parses a <see cref="T:Avalonia.Media.FontFamily"/> string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="s">The <see cref="T:Avalonia.Media.FontFamily"/> string.</param>
|
|
|
|
/// <param name="baseUri"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
/// Specified family is not supported.
|
|
|
|
/// </exception>
|
|
|
|
public static FontFamily Parse(string s, Uri baseUri = null) |
|
|
|
private struct FontFamilyIdentifier |
|
|
|
{ |
|
|
|
if (string.IsNullOrEmpty(s)) |
|
|
|
public FontFamilyIdentifier(string name, Uri source) |
|
|
|
{ |
|
|
|
throw new ArgumentException("Specified family is not supported."); |
|
|
|
Name = name; |
|
|
|
Source = source; |
|
|
|
} |
|
|
|
|
|
|
|
var segments = s.Split('#'); |
|
|
|
public string Name { get; } |
|
|
|
|
|
|
|
public Uri Source { get; } |
|
|
|
} |
|
|
|
|
|
|
|
private static FontFamilyIdentifier GetFontFamilyIdentifier(string name) |
|
|
|
{ |
|
|
|
var segments = name.Split('#'); |
|
|
|
|
|
|
|
switch (segments.Length) |
|
|
|
{ |
|
|
|
case 1: |
|
|
|
{ |
|
|
|
var names = segments[0].Split(',') |
|
|
|
.Select(x => x.Trim()) |
|
|
|
.Where(x => !string.IsNullOrWhiteSpace(x)); |
|
|
|
return new FontFamily(names); |
|
|
|
return new FontFamilyIdentifier(segments[0], null); |
|
|
|
} |
|
|
|
|
|
|
|
case 2: |
|
|
|
{ |
|
|
|
var uri = segments[0].StartsWith("/") |
|
|
|
? new Uri(segments[0], UriKind.Relative) |
|
|
|
: new Uri(segments[0], UriKind.RelativeOrAbsolute); |
|
|
|
var source = segments[0].StartsWith("/") |
|
|
|
? new Uri(segments[0], UriKind.Relative) |
|
|
|
: new Uri(segments[0], UriKind.RelativeOrAbsolute); |
|
|
|
|
|
|
|
return uri.IsAbsoluteUri |
|
|
|
? new FontFamily(segments[1], uri) |
|
|
|
: new FontFamily(segments[1], uri, baseUri); |
|
|
|
return new FontFamilyIdentifier(segments[1], source); |
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
@ -137,6 +134,25 @@ namespace Avalonia.Media |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parses a <see cref="T:Avalonia.Media.FontFamily"/> string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="s">The <see cref="T:Avalonia.Media.FontFamily"/> string.</param>
|
|
|
|
/// <param name="baseUri">Specifies the base uri that is used to resolve font family assets.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
/// Specified family is not supported.
|
|
|
|
/// </exception>
|
|
|
|
public static FontFamily Parse(string s, Uri baseUri = null) |
|
|
|
{ |
|
|
|
if (string.IsNullOrEmpty(s)) |
|
|
|
{ |
|
|
|
throw new ArgumentException("Specified family is not supported.", nameof(s)); |
|
|
|
} |
|
|
|
|
|
|
|
return new FontFamily(baseUri, s); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a <see cref="string" /> that represents this instance.
|
|
|
|
/// </summary>
|
|
|
|
|