Browse Source

OpenTypeFont support for embedded fonts

pull/2026/head
Benedikt Schroeder 8 years ago
parent
commit
daec1be033
  1. 35
      src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs
  2. 4
      src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs

35
src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs

@ -17,7 +17,10 @@ namespace Avalonia.Media.Fonts
/// <param name="source"></param> /// <param name="source"></param>
public FontFamilyKey(Uri source) public FontFamilyKey(Uri source)
{ {
if (source == null) throw new ArgumentNullException(nameof(source)); if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (source.AbsolutePath.Contains(".ttf")) if (source.AbsolutePath.Contains(".ttf"))
{ {
@ -26,6 +29,14 @@ namespace Avalonia.Media.Fonts
FileName = fileNameWithoutExtension + ".ttf"; FileName = fileNameWithoutExtension + ".ttf";
Location = new Uri(source.OriginalString.Replace("." + FileName, string.Empty), UriKind.RelativeOrAbsolute); Location = new Uri(source.OriginalString.Replace("." + FileName, string.Empty), UriKind.RelativeOrAbsolute);
} }
if (source.AbsolutePath.Contains(".otf"))
{
var filePathWithoutExtension = source.AbsolutePath.Replace(".otf", string.Empty);
var fileNameWithoutExtension = filePathWithoutExtension.Split('.').Last();
FileName = fileNameWithoutExtension + ".otf";
Location = new Uri(source.OriginalString.Replace("." + FileName, string.Empty), UriKind.RelativeOrAbsolute);
}
else else
{ {
Location = source; Location = source;
@ -77,11 +88,20 @@ namespace Avalonia.Media.Fonts
/// </returns> /// </returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (!(obj is FontFamilyKey other)) return false; if (!(obj is FontFamilyKey other))
{
return false;
}
if (Location != other.Location) return false; if (Location != other.Location)
{
return false;
}
if (FileName != other.FileName) return false; if (FileName != other.FileName)
{
return false;
}
return true; return true;
} }
@ -94,7 +114,10 @@ namespace Avalonia.Media.Fonts
/// </returns> /// </returns>
public override string ToString() public override string ToString()
{ {
if (FileName == null) return Location.PathAndQuery; if (FileName == null)
{
return Location.PathAndQuery;
}
var builder = new UriBuilder(Location); var builder = new UriBuilder(Location);
@ -103,4 +126,4 @@ namespace Avalonia.Media.Fonts
return builder.ToString(); return builder.ToString();
} }
} }
} }

4
src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs

@ -34,7 +34,7 @@ namespace Avalonia.Media.Fonts
{ {
var availableAssets = s_assetLoader.GetAssets(location); var availableAssets = s_assetLoader.GetAssets(location);
var matchingAssets = availableAssets.Where(x => x.absolutePath.EndsWith(".ttf")); var matchingAssets = availableAssets.Where(x => x.absolutePath.EndsWith(".ttf") || x.absolutePath.EndsWith(".otf"));
return matchingAssets.Select(x => GetAssetUri(x.absolutePath, x.assembly)); return matchingAssets.Select(x => GetAssetUri(x.absolutePath, x.assembly));
} }
@ -53,7 +53,7 @@ namespace Avalonia.Media.Fonts
var compareTo = location.AbsolutePath + "." + fileName.Split('*').First(); var compareTo = location.AbsolutePath + "." + fileName.Split('*').First();
var matchingResources = var matchingResources =
availableResources.Where(x => x.absolutePath.Contains(compareTo) && x.absolutePath.EndsWith(".ttf")); availableResources.Where(x => x.absolutePath.Contains(compareTo) && (x.absolutePath.EndsWith(".ttf") || x.absolutePath.EndsWith(".otf")));
return matchingResources.Select(x => GetAssetUri(x.absolutePath, x.assembly)); return matchingResources.Select(x => GetAssetUri(x.absolutePath, x.assembly));
} }

Loading…
Cancel
Save