Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit bde715b

Browse files
authored
Fix for issue #10307, embedded fonts not working on UWP (#11741)
* Fix GitHub issue #10307, some embedded fonts not working on UWP (cherry picked from commit 4ed0819) * Update UWP embedded font handler to retrieve font family name from font file using Win2D Co-authored-by: pleybaert <[email protected]> fixes #10307
1 parent dfb866b commit bde715b

File tree

6 files changed

+74
-13
lines changed

6 files changed

+74
-13
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Xamarin.Forms.CustomAttributes;
2+
using Xamarin.Forms.Internals;
3+
4+
#if UITEST
5+
using Xamarin.Forms.Core.UITests;
6+
using Xamarin.UITest;
7+
using NUnit.Framework;
8+
#endif
9+
10+
namespace Xamarin.Forms.Controls.Issues
11+
{
12+
#if UITEST
13+
[Category(UITestCategories.ManualReview)]
14+
#endif
15+
[Preserve(AllMembers = true)]
16+
[Issue(IssueTracker.Github, 10307, "Embedded Fonts not working", PlatformAffected.UWP)]
17+
public class Issue10307 : TestContentPage // or TestMasterDetailPage, etc ...
18+
{
19+
protected override void Init()
20+
{
21+
Content = new StackLayout()
22+
{
23+
Children =
24+
{
25+
new Label() { Text = "Four bell icons should be visible below", Margin = new Thickness(10)},
26+
27+
new Label { FontFamily = "FontAwesome", FontSize = 50, TextColor = Color.Black, Text = "\xf0f3" },
28+
new Label { FontFamily = "fa-regular-400.ttf", FontSize = 50, TextColor = Color.Black, Text = "\xf0f3" },
29+
new Image() { Source = new FontImageSource() { FontFamily = "FontAwesome", Glyph = "\xf0f3", Color = Color.Black, Size = 50}, HorizontalOptions = LayoutOptions.Start},
30+
new Image() { Source = new FontImageSource() { FontFamily = "fa-regular-400.ttf", Glyph = "\xf0f3", Color = Color.Black, Size = 50}, HorizontalOptions = LayoutOptions.Start},
31+
}
32+
};
33+
34+
35+
BindingContext = new ViewModelIssue1();
36+
}
37+
}
38+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@
835835
</Compile>
836836
<Compile Include="$(MSBuildThisFileDirectory)Issue10699.cs" />
837837
<Compile Include="$(MSBuildThisFileDirectory)Issue11185.cs" />
838+
<Compile Include="$(MSBuildThisFileDirectory)Issue10307.cs" />
838839
<Compile Include="$(MSBuildThisFileDirectory)_TemplateMarkup.xaml.cs">
839840
<DependentUpon>_TemplateMarkup.xaml</DependentUpon>
840841
<SubType>Code</SubType>

Xamarin.Forms.Controls/GalleryPages/EmbeddedFonts.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[assembly: ExportFont("CuteFont-Regular.ttf", Alias = "Foo")]
66
[assembly: ExportFont("PTM55FT.ttf")]
77
[assembly: ExportFont("Dokdo-Regular.ttf")]
8-
[assembly: ExportFont("fa-regular-400.ttf")]
8+
[assembly: ExportFont("fa-regular-400.ttf", Alias="FontAwesome")]
99

1010
namespace Xamarin.Forms.Controls.GalleryPages
1111
{

Xamarin.Forms.Core/ExportFontAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Xamarin.Forms
66
public class ExportFontAttribute : Attribute
77
{
88
public string Alias { get; set; }
9+
910
public ExportFontAttribute(string fontFileName)
1011
{
1112
FontFileName = fontFileName;

Xamarin.Forms.Platform.UAP/FontExtensions.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Graphics.Canvas.Text;
35
using Windows.UI.Text;
46
using Windows.UI.Xaml.Controls;
57
using Windows.UI.Xaml.Media;
@@ -105,16 +107,27 @@ public static FontFamily ToFontFamily(this string fontFamily)
105107
return font;
106108
}
107109

110+
static string FindFontFamilyName(string fontFile)
111+
{
112+
using (var fontSet = new CanvasFontSet(new Uri(fontFile)))
113+
{
114+
if (fontSet.Fonts.Count == 0)
115+
return null;
116+
117+
return fontSet.GetPropertyValues(CanvasFontPropertyIdentifier.FamilyName).FirstOrDefault().Value;
118+
}
119+
}
120+
108121
static IEnumerable<string> GetAllFontPossibilities(string fontFamily)
109122
{
110123
//First check Alias
111124
var (hasFontAlias, fontPostScriptName) = FontRegistrar.HasFont(fontFamily);
112125
if (hasFontAlias)
113126
{
127+
var familyName = FindFontFamilyName(fontPostScriptName);
114128
var file = FontFile.FromString(IOPath.GetFileName(fontPostScriptName));
115-
var formated = $"{fontPostScriptName}#{file.GetPostScriptNameWithSpaces()}";
116-
yield return formated;
117-
yield return fontFamily;
129+
var formatted = $"{fontPostScriptName}#{familyName ?? file.GetPostScriptNameWithSpaces()}";
130+
yield return formatted;
118131
yield break;
119132
}
120133

@@ -133,8 +146,9 @@ static IEnumerable<string> GetAllFontPossibilities(string fontFamily)
133146
var (hasFont, filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
134147
if (hasFont)
135148
{
136-
var formated = $"{filePath}#{fontFile.GetPostScriptNameWithSpaces()}";
137-
yield return formated;
149+
var familyName = FindFontFamilyName(filePath);
150+
var formatted = $"{filePath}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}";
151+
yield return formatted;
138152
yield break;
139153
}
140154
else
@@ -147,7 +161,8 @@ static IEnumerable<string> GetAllFontPossibilities(string fontFamily)
147161
var (hasFont, filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension(ext));
148162
if (hasFont)
149163
{
150-
var formatted = $"{filePath}#{fontFile.GetPostScriptNameWithSpaces()}";
164+
var familyName = FindFontFamilyName(filePath);
165+
var formatted = $"{filePath}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}";
151166
yield return formatted;
152167
yield break;
153168
}
@@ -158,7 +173,9 @@ static IEnumerable<string> GetAllFontPossibilities(string fontFamily)
158173

159174
foreach (var ext in extensions)
160175
{
161-
var formatted = $"{path}{fontFile.FileNameWithExtension(ext)}#{fontFile.GetPostScriptNameWithSpaces()}";
176+
var fileName = $"{path}{fontFile.FileNameWithExtension(ext)}";
177+
var familyName = FindFontFamilyName(fileName);
178+
var formatted = $"{fileName}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}";
162179
yield return formatted;
163180
}
164181
}

Xamarin.Forms.Platform.UAP/FontImageSourceHandler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
2525

2626
var textFormat = new CanvasTextFormat
2727
{
28-
FontFamily = fontsource.FontFamily,
28+
FontFamily = fontsource.FontFamily.ToFontFamily().Source,
2929
FontSize = (float)fontsource.Size,
3030
HorizontalAlignment = CanvasHorizontalAlignment.Center,
3131
VerticalAlignment = CanvasVerticalAlignment.Center,
@@ -65,8 +65,10 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
6565
Foreground = fontImageSource.Color.ToBrush()
6666
};
6767

68-
if (!string.IsNullOrEmpty(fontImageSource.FontFamily))
69-
((WFontIconSource)image).FontFamily = new FontFamily(fontImageSource.FontFamily);
68+
var uwpFontFamily = fontImageSource.FontFamily.ToFontFamily().Source;
69+
70+
if (!string.IsNullOrEmpty(uwpFontFamily))
71+
((WFontIconSource)image).FontFamily = new FontFamily(uwpFontFamily);
7072
}
7173

7274
return Task.FromResult(image);
@@ -85,8 +87,10 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
8587
Foreground = fontImageSource.Color.ToBrush()
8688
};
8789

88-
if (!string.IsNullOrEmpty(fontImageSource.FontFamily))
89-
((FontIcon)image).FontFamily = new FontFamily(fontImageSource.FontFamily);
90+
var uwpFontFamily = fontImageSource.FontFamily.ToFontFamily().Source;
91+
92+
if (!string.IsNullOrEmpty(uwpFontFamily))
93+
((FontIcon)image).FontFamily = new FontFamily(uwpFontFamily);
9094
}
9195

9296
return Task.FromResult(image);

0 commit comments

Comments
 (0)