From daec1be03337e9774869a150c5b245dbec3f476c Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Fri, 26 Oct 2018 15:47:38 +0200 Subject: [PATCH 1/3] OpenTypeFont support for embedded fonts --- .../Media/Fonts/FontFamilyKey.cs | 35 +++++++++++++++---- .../Media/Fonts/FontFamilyLoader.cs | 4 +-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs b/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs index cb996867c4..9af9509e4e 100644 --- a/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs +++ b/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs @@ -17,7 +17,10 @@ namespace Avalonia.Media.Fonts /// 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")) { @@ -26,6 +29,14 @@ namespace Avalonia.Media.Fonts FileName = fileNameWithoutExtension + ".ttf"; 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 { Location = source; @@ -77,11 +88,20 @@ namespace Avalonia.Media.Fonts /// 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; } @@ -94,7 +114,10 @@ namespace Avalonia.Media.Fonts /// public override string ToString() { - if (FileName == null) return Location.PathAndQuery; + if (FileName == null) + { + return Location.PathAndQuery; + } var builder = new UriBuilder(Location); @@ -103,4 +126,4 @@ namespace Avalonia.Media.Fonts return builder.ToString(); } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs b/src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs index 57e6c756cc..b6b83e2097 100644 --- a/src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs +++ b/src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs @@ -34,7 +34,7 @@ namespace Avalonia.Media.Fonts { 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)); } @@ -53,7 +53,7 @@ namespace Avalonia.Media.Fonts var compareTo = location.AbsolutePath + "." + fileName.Split('*').First(); 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)); } From 2a7de6681b622294fabba11ca580315f34c53a60 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Sat, 27 Oct 2018 00:47:55 +0200 Subject: [PATCH 2/3] Fix FamilyKey --- .../Media/Fonts/FontFamilyKey.cs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs b/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs index 9af9509e4e..99b0091139 100644 --- a/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs +++ b/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs @@ -29,19 +29,21 @@ namespace Avalonia.Media.Fonts FileName = fileNameWithoutExtension + ".ttf"; 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 { - Location = source; + 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 + { + Location = source; + } } - } + } /// /// Location of stored font asset that belongs to a From 1bb863134030f5d1ed752124dbb9b6ecb1113d5d Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Sat, 27 Oct 2018 11:26:40 +0200 Subject: [PATCH 3/3] Remove extra whitespaces --- src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs b/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs index 99b0091139..90ccac0e46 100644 --- a/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs +++ b/src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs @@ -43,7 +43,7 @@ namespace Avalonia.Media.Fonts Location = source; } } - } + } /// /// Location of stored font asset that belongs to a