|
@@ -9,6 +9,10 @@ using Avalonia.Platform;
|
|
|
|
|
|
|
|
namespace Avalonia.Media.Fonts
|
|
namespace Avalonia.Media.Fonts
|
|
|
{
|
|
{
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Implementation of <see cref="T:Avalonia.Media.Fonts.IFontResourceLoader" />
|
|
|
|
|
+ /// </summary>
|
|
|
public class FontResourceLoader : IFontResourceLoader
|
|
public class FontResourceLoader : IFontResourceLoader
|
|
|
{
|
|
{
|
|
|
private static readonly Dictionary<string, AssemblyDescriptor> s_assemblyNameCache
|
|
private static readonly Dictionary<string, AssemblyDescriptor> s_assemblyNameCache
|
|
@@ -16,6 +20,10 @@ namespace Avalonia.Media.Fonts
|
|
|
|
|
|
|
|
private readonly AssemblyDescriptor _defaultAssembly;
|
|
private readonly AssemblyDescriptor _defaultAssembly;
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Initializes a new instance of the <see cref="FontResourceLoader"/> class.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="assembly">The default assembly.</param>
|
|
|
public FontResourceLoader(Assembly assembly = null)
|
|
public FontResourceLoader(Assembly assembly = null)
|
|
|
{
|
|
{
|
|
|
if (assembly == null)
|
|
if (assembly == null)
|
|
@@ -28,6 +36,12 @@ namespace Avalonia.Media.Fonts
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Returns a quanity of <see cref="T:Avalonia.Media.Fonts.FontResource" /> that belongs to a given <see cref="T:Avalonia.Media.Fonts.FontFamilyKey" />
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="fontFamilyKey"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
public IEnumerable<FontResource> GetFontResources(FontFamilyKey fontFamilyKey)
|
|
public IEnumerable<FontResource> GetFontResources(FontFamilyKey fontFamilyKey)
|
|
|
{
|
|
{
|
|
|
return fontFamilyKey.FileName != null
|
|
return fontFamilyKey.FileName != null
|
|
@@ -35,6 +49,11 @@ namespace Avalonia.Media.Fonts
|
|
|
: GetFontResourcesByLocation(fontFamilyKey.Location);
|
|
: GetFontResourcesByLocation(fontFamilyKey.Location);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Searches for font resources at a given location and returns a quanity of found resources
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="location"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private IEnumerable<FontResource> GetFontResourcesByLocation(Uri location)
|
|
private IEnumerable<FontResource> GetFontResourcesByLocation(Uri location)
|
|
|
{
|
|
{
|
|
|
var assembly = GetAssembly(location);
|
|
var assembly = GetAssembly(location);
|
|
@@ -46,6 +65,13 @@ namespace Avalonia.Media.Fonts
|
|
|
return matchingResources.Select(x => new FontResource(GetResourceUri(x, assembly.Name)));
|
|
return matchingResources.Select(x => new FontResource(GetResourceUri(x, assembly.Name)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Searches for font resources at a given location and only accepts resources that fit to a given filename expression.
|
|
|
|
|
+ /// <para>Filenames can target multible files with * wildcard. For example "FontFile*.ttf"</para>
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="location"></param>
|
|
|
|
|
+ /// <param name="fileName"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private IEnumerable<FontResource> GetFontResourcesByFileName(Uri location, string fileName)
|
|
private IEnumerable<FontResource> GetFontResourcesByFileName(Uri location, string fileName)
|
|
|
{
|
|
{
|
|
|
var assembly = GetAssembly(location);
|
|
var assembly = GetAssembly(location);
|
|
@@ -57,11 +83,22 @@ namespace Avalonia.Media.Fonts
|
|
|
return matchingResources.Select(x => new FontResource(GetResourceUri(x, assembly.Name)));
|
|
return matchingResources.Select(x => new FontResource(GetResourceUri(x, assembly.Name)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Returns a valid resource <see cref="Uri"/> that follows the resm scheme
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="path"></param>
|
|
|
|
|
+ /// <param name="assemblyName"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private static Uri GetResourceUri(string path, string assemblyName)
|
|
private static Uri GetResourceUri(string path, string assemblyName)
|
|
|
{
|
|
{
|
|
|
return new Uri("resm:" + path + "?assembly=" + assemblyName);
|
|
return new Uri("resm:" + path + "?assembly=" + assemblyName);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Translates a given <see cref="Uri"/> to a <see cref="Uri"/> that follows the resm scheme.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="uri"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private static string GetLocationPath(Uri uri)
|
|
private static string GetLocationPath(Uri uri)
|
|
|
{
|
|
{
|
|
|
if (uri.Scheme == "resm") return uri.AbsolutePath;
|
|
if (uri.Scheme == "resm") return uri.AbsolutePath;
|
|
@@ -71,6 +108,11 @@ namespace Avalonia.Media.Fonts
|
|
|
return path;
|
|
return path;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Extracts a <see cref="AssemblyDescriptor"/> from a given <see cref="Uri"/>
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="uri"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private AssemblyDescriptor GetAssembly(Uri uri)
|
|
private AssemblyDescriptor GetAssembly(Uri uri)
|
|
|
{
|
|
{
|
|
|
if (uri == null) return null;
|
|
if (uri == null) return null;
|
|
@@ -80,6 +122,14 @@ namespace Avalonia.Media.Fonts
|
|
|
return parameters.TryGetValue("assembly", out var assemblyName) ? GetAssembly(assemblyName) : null;
|
|
return parameters.TryGetValue("assembly", out var assemblyName) ? GetAssembly(assemblyName) : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Returns a <see cref="AssemblyDescriptor"/> that is identified by a given name.
|
|
|
|
|
+ /// <para>
|
|
|
|
|
+ /// If name is <value>null</value> the default assembly is used.
|
|
|
|
|
+ /// </para>
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="name"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private AssemblyDescriptor GetAssembly(string name)
|
|
private AssemblyDescriptor GetAssembly(string name)
|
|
|
{
|
|
{
|
|
|
if (name == null)
|
|
if (name == null)
|
|
@@ -113,6 +163,11 @@ namespace Avalonia.Media.Fonts
|
|
|
return rv;
|
|
return rv;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Parses the parameters.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="uri">The URI.</param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
private static Dictionary<string, string> ParseParameters(Uri uri)
|
|
private static Dictionary<string, string> ParseParameters(Uri uri)
|
|
|
{
|
|
{
|
|
|
return uri.Query.TrimStart('?')
|
|
return uri.Query.TrimStart('?')
|
|
@@ -134,8 +189,28 @@ namespace Avalonia.Media.Fonts
|
|
|
Name = Assembly.GetName().Name;
|
|
Name = Assembly.GetName().Name;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Gets the name.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <value>
|
|
|
|
|
+ /// The name.
|
|
|
|
|
+ /// </value>
|
|
|
public string Name { get; }
|
|
public string Name { get; }
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Gets the assembly.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <value>
|
|
|
|
|
+ /// The assembly.
|
|
|
|
|
+ /// </value>
|
|
|
public Assembly Assembly { get; }
|
|
public Assembly Assembly { get; }
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Gets the resources.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <value>
|
|
|
|
|
+ /// The resources.
|
|
|
|
|
+ /// </value>
|
|
|
public List<string> Resources { get; }
|
|
public List<string> Resources { get; }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|