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

Fix for issue #10307, embedded fonts not working on UWP #11741

Merged
merged 2 commits into from
Aug 31, 2020

Conversation

activa
Copy link
Contributor

@activa activa commented Aug 11, 2020

Embedded fonts on UWP don't work when the font name does not match the file name of the font, which is the case with many Google fonts (and FontAwesome icons)

To fix this, the font family name has to be extracted from the font file and appended to the font name when the font is created from the font file.

Issues Resolved

API Changes

None

Platforms Affected

  • UWP

Behavioral/Visual Changes

None (fixes bug #10307)

Before/After Screenshots

Before:
ApplicationFrameHost_2020-08-10_19-42-17

After:
ApplicationFrameHost_2020-08-10_19-38-44

Not applicable

PR Checklist

  • Targets the correct branch
  • Tests are passing (or failures are unrelated)

@activa
Copy link
Contributor Author

activa commented Aug 11, 2020

This would require a documentation change on embedded fonts. The ExportFont attribute needs the property FontName when the name of the font is different than the file name.

var file = FontFile.FromString(IOPath.GetFileName(fontPostScriptName));
var formated = $"{fontPostScriptName}#{file.GetPostScriptNameWithSpaces()}";
yield return formated;
yield return fontFamily;
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)

@samhouts samhouts self-requested a review August 11, 2020 21:44
@samhouts samhouts added the retarget-branch-required PR or associated issues target a milestone. Please target this PR to the matching branch. label Aug 13, 2020
@activa
Copy link
Contributor Author

activa commented Aug 24, 2020

@samhouts Is anything else needed before this can be reviewed?

@samhouts
Copy link
Member

@activa This looks great! Since this is an API change, can we move it to 5.0.0? Thanks!

@mattleibow
Copy link
Contributor

mattleibow commented Aug 25, 2020

Hey wait there @samhouts, before we start moving this "bug" 😄 to next releases...

@activa I was thinking that I probably wouldn't want to scratch around in files to find things and add names and all that... seems like too much work 😆

So I had a look at maybe trying to just ask the font file what the correct name was. Seems this is totally doable without adding any new dependencies or special logic:

I just made use of the already used Win2D:

var name = "fa-solid-900.ttf";
var uriString = $"ms-appdata:///local/fonts/{name}";
var uri = new Uri(uriString);
var cfs = new CanvasFontSet(uri);
var familyName = cfs.GetPropertyValues(CanvasFontPropertyIdentifier.FamilyName).FirstOrDefault().Value;

var fullFontName = $"{uriString}#{familyName}";

Content = new TextBlock
{
	Text = "hello \uf0c8",
	VerticalAlignment = VerticalAlignment.Center,
	FontFamily = new FontFamily(fullFontName)
};

If there is no font name, then I am not sure what is supposed to happen... but, we can probably just fall back to the plain filename for now. Unless you have an example font that has no name and can test things?

Maybe you could tweak your PR to do this instead? And, then this will also clean up all the other logic bits when loading fonts.

Together @activa my fine friend, we shall merge this into the next release of Xamarin.Forms 4.8! We shall oppose this "API change" and "move it to 5.0.0" oppression of our beloved UWP! o7

@mattleibow
Copy link
Contributor

Using Win2D will also work on the WPF platform as well. Might be able to use things there too.

@mattleibow mattleibow closed this Aug 25, 2020
@mattleibow mattleibow reopened this Aug 25, 2020
@mattleibow
Copy link
Contributor

mattleibow commented Aug 25, 2020

My gosh, why do I have the power to close issues? I can't be trusted! My mouse slipped!

Sorry for this mess.

@PureWeen PureWeen requested a review from Clancey August 25, 2020 16:13
@activa
Copy link
Contributor Author

activa commented Aug 25, 2020

@mattleibow I'll update the PR with your suggested changes.

@activa
Copy link
Contributor Author

activa commented Aug 26, 2020

PR was updated. No API changes were introduced and the font family is now extracted from the font file on UWP.

Copy link
Contributor

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! Now that is what I call development! 😎

if (fontSet.Fonts.Count == 0)
return null;

return fontSet.GetPropertyValues(CanvasFontPropertyIdentifier.FamilyName).FirstOrDefault().Value;
Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

@mattleibow
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@mattleibow
Copy link
Contributor

Confirmation that it just runs on UWP without any changes to my existing app:
image

@activa you are a beautiful dev! We need more like you in the world! Thanks for getting this PR in! Now we just got to get the Team to merge this for the next SR!

Copy link
Member

@samhouts samhouts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean. We're technically only supposed to be putting regression fixes into 4.8.0 right now, but I'm feeling the love here. Let's make it happen.

@samhouts samhouts removed the retarget-branch-required PR or associated issues target a milestone. Please target this PR to the matching branch. label Aug 26, 2020
@samhouts samhouts requested a review from PureWeen August 26, 2020 22:15
@mattleibow
Copy link
Contributor

So much yay!!! My app will now look beautiful again. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants