This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix for issue #10307, embedded fonts not working on UWP #11741
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue10307.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Xamarin.Forms.CustomAttributes; | ||
using Xamarin.Forms.Internals; | ||
|
||
#if UITEST | ||
using Xamarin.Forms.Core.UITests; | ||
using Xamarin.UITest; | ||
using NUnit.Framework; | ||
#endif | ||
|
||
namespace Xamarin.Forms.Controls.Issues | ||
{ | ||
#if UITEST | ||
[Category(UITestCategories.ManualReview)] | ||
#endif | ||
[Preserve(AllMembers = true)] | ||
[Issue(IssueTracker.Github, 10307, "Embedded Fonts not working", PlatformAffected.UWP)] | ||
public class Issue10307 : TestContentPage // or TestMasterDetailPage, etc ... | ||
{ | ||
protected override void Init() | ||
{ | ||
Content = new StackLayout() | ||
{ | ||
Children = | ||
{ | ||
new Label() { Text = "Four bell icons should be visible below", Margin = new Thickness(10)}, | ||
|
||
new Label { FontFamily = "FontAwesome", FontSize = 50, TextColor = Color.Black, Text = "\xf0f3" }, | ||
new Label { FontFamily = "fa-regular-400.ttf", FontSize = 50, TextColor = Color.Black, Text = "\xf0f3" }, | ||
new Image() { Source = new FontImageSource() { FontFamily = "FontAwesome", Glyph = "\xf0f3", Color = Color.Black, Size = 50}, HorizontalOptions = LayoutOptions.Start}, | ||
new Image() { Source = new FontImageSource() { FontFamily = "fa-regular-400.ttf", Glyph = "\xf0f3", Color = Color.Black, Size = 50}, HorizontalOptions = LayoutOptions.Start}, | ||
} | ||
}; | ||
|
||
|
||
BindingContext = new ViewModelIssue1(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Graphics.Canvas.Text; | ||
using Windows.UI.Text; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Media; | ||
|
@@ -105,16 +107,27 @@ public static FontFamily ToFontFamily(this string fontFamily) | |
return font; | ||
} | ||
|
||
static string FindFontFamilyName(string fontFile) | ||
{ | ||
using (var fontSet = new CanvasFontSet(new Uri(fontFile))) | ||
{ | ||
if (fontSet.Fonts.Count == 0) | ||
return null; | ||
|
||
return fontSet.GetPropertyValues(CanvasFontPropertyIdentifier.FamilyName).FirstOrDefault().Value; | ||
} | ||
} | ||
|
||
static IEnumerable<string> GetAllFontPossibilities(string fontFamily) | ||
{ | ||
//First check Alias | ||
var (hasFontAlias, fontPostScriptName) = FontRegistrar.HasFont(fontFamily); | ||
if (hasFontAlias) | ||
{ | ||
var familyName = FindFontFamilyName(fontPostScriptName); | ||
var file = FontFile.FromString(IOPath.GetFileName(fontPostScriptName)); | ||
var formated = $"{fontPostScriptName}#{file.GetPostScriptNameWithSpaces()}"; | ||
yield return formated; | ||
yield return fontFamily; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this line was not needed when the font was found in the FontRegistrar. In fact, it caused problems when setting FontFamily in a CanvasTextFormat object (see FontImageSourceHandler.cs) |
||
var formatted = $"{fontPostScriptName}#{familyName ?? file.GetPostScriptNameWithSpaces()}"; | ||
yield return formatted; | ||
yield break; | ||
} | ||
|
||
|
@@ -133,8 +146,9 @@ static IEnumerable<string> GetAllFontPossibilities(string fontFamily) | |
var (hasFont, filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension()); | ||
if (hasFont) | ||
{ | ||
var formated = $"{filePath}#{fontFile.GetPostScriptNameWithSpaces()}"; | ||
yield return formated; | ||
var familyName = FindFontFamilyName(filePath); | ||
var formatted = $"{filePath}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}"; | ||
yield return formatted; | ||
yield break; | ||
} | ||
else | ||
|
@@ -147,7 +161,8 @@ static IEnumerable<string> GetAllFontPossibilities(string fontFamily) | |
var (hasFont, filePath) = FontRegistrar.HasFont(fontFile.FileNameWithExtension(ext)); | ||
if (hasFont) | ||
{ | ||
var formatted = $"{filePath}#{fontFile.GetPostScriptNameWithSpaces()}"; | ||
var familyName = FindFontFamilyName(filePath); | ||
var formatted = $"{filePath}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}"; | ||
yield return formatted; | ||
yield break; | ||
} | ||
|
@@ -158,7 +173,9 @@ static IEnumerable<string> GetAllFontPossibilities(string fontFamily) | |
|
||
foreach (var ext in extensions) | ||
{ | ||
var formatted = $"{path}{fontFile.FileNameWithExtension(ext)}#{fontFile.GetPostScriptNameWithSpaces()}"; | ||
var fileName = $"{path}{fontFile.FileNameWithExtension(ext)}"; | ||
var familyName = FindFontFamilyName(fileName); | ||
var formatted = $"{fileName}#{familyName ?? fontFile.GetPostScriptNameWithSpaces()}"; | ||
yield return formatted; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will
FirstOrDefault
here always return something?Does this need a null check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result is a struct: https://microsoft.github.io/Win2D/html/T_Microsoft_Graphics_Canvas_Text_CanvasFontProperty.htm
so the
.Value
should benull
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What @mattleibow said. Also, I was thinking about caching the return value but it would be pointless because the resulting FontFamily objects are already being cached here.