maui
maui copied to clipboard
Windows FontImageSourceService assumes MSIX packaging
Description
The Windows FontRegistrar assumes the application is packaged with MSIX (all paths are prefixed with "ms-appx:///"), so font loading fails if/when MSIX packaging is disabled.
https://github.com/dotnet/maui/blob/main/src/Core/src/Fonts/FontRegistrar.Windows.cs#L11
13:05:02:928 Microsoft.Maui.FontManager: Error: Error loading font 'ms-appx:///OpenSans-Regular.ttf'.
Switching to AddEmbeddedResourceFont also fails unless using MSIX packaging.
Steps to Reproduce
- Create a new MAUI App
- Disable MSIX Packaging in csproj:
<!-- Disable MSIX -->
<WindowsPackageType>None</WindowsPackageType>
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
<Platforms>x64</Platforms>
<Platform>x64</Platform>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<WindowsAppSdkDeploymentManagerInitialize>false</WindowsAppSdkDeploymentManagerInitialize>
Run the app and see error like:
13:05:02:928 Microsoft.Maui.FontManager: Error: Error loading font 'ms-appx:///OpenSans-Regular.ttf'.
Link to public reproduction project repository
https://github.com/molesmoke/MauiFontImageSourceTest
Version with bug
7.0.86
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
10.0.19041.0
Did you find any workaround?
Could implement a custom registrar service, but haven't tried
Relevant log output
13:05:02:928 Microsoft.Maui.FontManager: Error: Error loading font 'ms-appx:///OpenSans-Regular.ttf'.
Microsoft.Maui.FontImageSourceService: Warning: Unable to generate font image ''.
System.ArgumentException: Value does not fall within the expected range.
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
at Microsoft.Graphics.Canvas.Text.CanvasTextLayout._ICanvasTextLayoutFactory.Create(ICanvasResourceCreator resourceCreator, String textString, CanvasTextFormat textFormat, Single requestedWidth, Single requestedHeight)
at Microsoft.Graphics.Canvas.Text.CanvasTextLayout.<>c__DisplayClass11_0.<.ctor>b__0()
at Microsoft.Graphics.Canvas.Text.CanvasTextLayout..ctor(ICanvasResourceCreator resourceCreator, String textString, CanvasTextFormat textFormat, Single requestedWidth, Single requestedHeight)
at Microsoft.Maui.FontImageSourceService.RenderImageSource(IFontImageSource imageSource, Single scale)
at Microsoft.Maui.FontImageSourceService.GetImageSourceAsync(IFontImageSource imageSource, Single scale, CancellationToken cancellationToken)
VS bug #1923183
I assume this would go in line with https://github.com/dotnet/maui/issues/10564, @mattleibow?
@drasticactions Thanks for pointing that out. This one might also be related to #9104
Now I understand what's going on a bit more, I believe this is actually a dupe of #9104. i.e. The ms-appx URI is intentional and the failure was also due to an issue in WinUI3/Win2D. Nonetheless, I still observe the issue on a local build of MAUI release/7.0.2xxsr7 even though all the PRs have been merged there. @mattleibow perhaps this should be closed in favour of reopening #9104?
I spoke to soon. The FontManager does manage to load the fonts OK, which is what #9104 is about, but FontImageSource is still broken, which my repro project was also testing. So, even though the initial error is resolved on release/7.0.2xxsr7, #9104 is not sufficient to resolve all the problems constructing CanvasTextLayouts:
Microsoft.Maui.FontImageSourceService: Warning: Unable to generate font image ''.
System.ArgumentException: Value does not fall within the expected range.
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
at Microsoft.Graphics.Canvas.Text.CanvasTextLayout._ICanvasTextLayoutFactory.Create(ICanvasResourceCreator resourceCreator, String textString, CanvasTextFormat textFormat, Single requestedWidth, Single requestedHeight)
at Microsoft.Graphics.Canvas.Text.CanvasTextLayout.<>c__DisplayClass11_0.<.ctor>b__0()
at Microsoft.Graphics.Canvas.Text.CanvasTextLayout..ctor(ICanvasResourceCreator resourceCreator, String textString, CanvasTextFormat textFormat, Single requestedWidth, Single requestedHeight)
at Microsoft.Maui.FontImageSourceService.RenderImageSource(IFontImageSource imageSource, Single scale)
at Microsoft.Maui.FontImageSourceService.GetImageSourceAsync(IFontImageSource imageSource, Single scale, CancellationToken cancellationToken)
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.
Verified this on Visual Studio Enterprise 17.7.0 Preview 5.0. Repro on Windows 11 .NET 8.0 with below Project: MauiFontImageSourceTest.zip
I had a look and I can't get the FontImageSource implementation to load fonts by file or in unpackaged apps (which load by file). The exception says:
The URI specified in the CanvasTextFormat's FontFamily has an invalid scheme; the scheme may be omitted, or must be one of ms-appx:// or ms-appdata://.
I will open a bug in Win2D
Opened this one and will follow up with that team: https://github.com/microsoft/Win2D/issues/941
I have been seeing this error for months. I am also doing an unpackaged deployment, but I am not seeing the same exact error that @jeremy-visionaid reported... but, very similar.
I have been seeing (formatting done by NLog):
2023-12-08 09:30:15.2885 ERROR
Call site: FontManager.FindFontFamilyName
Method name: Microsoft.Maui.FontManager.FindFontFamilyName
Line: 0
Exception Type: System.ArgumentException
Exception Message: The parameter is incorrect.
Assets/Fonts/Segoe UI.ttf is not a valid absolute URI.
Stack Trace: at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
at ABI.System.WinRTUriRuntimeClassFactory.CreateUri(String uri)
at ABI.System.Uri.CreateMarshaler(Uri value)
at Microsoft.Graphics.Canvas.Text.CanvasFontSet._ICanvasFontSetFactory.Create(Uri uri)
at Microsoft.Graphics.Canvas.Text.CanvasFontSet.<>c__DisplayClass11_0.<.ctor>b__0()
at Microsoft.Graphics.Canvas.Text.CanvasFontSet..ctor(Uri uri)
at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)
Additional Info: Error loading font 'Assets/Fonts/Segoe UI.ttf'.
I am not loading this particular font, nor is it listed anywhere in my solution. Here is the section where I load fonts:
.ConfigureFonts (fonts => {
_ = fonts.AddFont ("FontAwesome6Brands-Regular-400.otf", "FaBrands");
_ = fonts.AddFont ("FontAwesome6Duotone-Solid-900.otf", "FaDuotone");
_ = fonts.AddFont ("FontAwesome6Pro-Light-300.otf", "FaLight");
_ = fonts.AddFont ("FontAwesome6Pro-Regular-400.otf", "FaRegular");
_ = fonts.AddFont ("FontAwesome6Pro-Solid-900.otf", "FaSolid");
_ = fonts.AddFont ("FontAwesome6Pro-Thin-100.otf", "FaThin");
_ = fonts.AddFont ("OpenSans-Regular.ttf", "OpenSansRegular");
_ = fonts.AddFont ("OpenSans-Semibold.ttf", "OpenSansSemibold");
_ = fonts.AddFont ("Poppins-Black.otf", "PoppinsBlack");
_ = fonts.AddFont ("Poppins-BlackItalic.otf", "PoppinsBlackItalic");
_ = fonts.AddFont ("Poppins-Bold.otf", "PoppinsBold");
_ = fonts.AddFont ("Poppins-BoldItalic.otf", "PoppinsBoldItalic");
_ = fonts.AddFont ("Poppins-ExtraBold.otf", "PoppinsExtraBold");
_ = fonts.AddFont ("Poppins-ExtraBoldItalic.otf", "PoppinsExtraBoldItalic");
_ = fonts.AddFont ("Poppins-ExtraLight.otf", "PoppinsExtraLight");
_ = fonts.AddFont ("Poppins-ExtraLightItalic.otf", "PoppinsExtraLightItalic");
_ = fonts.AddFont ("Poppins-Italic.otf", "PoppinsItalic");
_ = fonts.AddFont ("Poppins-Light.otf", "PoppinsLight");
_ = fonts.AddFont ("Poppins-LightItalic.otf", "PoppinsLightItalic");
_ = fonts.AddFont ("Poppins-Medium.otf", "PoppinsMedium");
_ = fonts.AddFont ("Poppins-MediumItalic.otf", "PoppinsMediumItalic");
_ = fonts.AddFont ("Poppins-Regular.otf", "PoppinsRegular");
_ = fonts.AddFont ("Poppins-SemiBold.otf", "PoppinsSemiBold");
_ = fonts.AddFont ("Poppins-SemiBoldItalic.otf", "PoppinsSemiBoldItalic");
_ = fonts.AddFont ("Poppins-Thin.otf", "PoppinsThin");
_ = fonts.AddFont ("Poppins-ThinItalic.otf", "PoppinsThinItalic");
_ = fonts.AddFont ("Segoe-Ui-Bold.ttf", "SegoeUiBold");
_ = fonts.AddFont ("Segoe-Ui-Regular.ttf", "SegoeUiRegular");
_ = fonts.AddFont ("Segoe-Ui-Semibold.ttf", "SegoeUiSemibold");
_ = fonts.AddFont ("Segoe-Ui-Semilight.ttf", "SegoeUiSemilight");
})
All of these fonts are included in my Resources\Fonts folder, so I am confused as to where the 'Assets/Fonts/Segoe UI.ttf' is coming from. In my .csproj file there is this for fonts:
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
but that is a standard project file entry.
I remember during a preview release that the splash screen was defined like '.../Assets/...' and there were instructions in the release notes on how to get around that issue. The code that is throwing this error is located in Microsoft.Maui.FontManager inside the FindFontFamilyName method. There is a constant for TypicalFontAssetsPath
which is defined as Assets/Fonts/
and an array, TypicalFontFileExtensions
, which has .ttf
and '.otf' as elements that correlate to the exceptions in my log file (the other is exactly as the one shown, but it is looking for the .otf
version. In the FindFontFamilyName
method there is an if
statement that seems to only look for the actual path if the app has been deployed as a packaged app, which mine is not....
// unpackaged apps can't load files using packaged schemes
if (!AppInfoUtils.IsPackagedApp)
{
var path = fontUri.AbsolutePath.TrimStart('/');
if (FileSystemUtils.TryGetAppPackageFileUri(path, out var uri))
fontUri = new Uri(uri, UriKind.RelativeOrAbsolute);
}
If this is truly the case, the call to TryGetAppPackageFileUri
is perhaps being passed an incorrect initial path. My fonts are getting loaded, so something is happening that should not be for font look-ups for unpackaged apps.
As a rule, I do not like errors in the error log that I have no control over. If there is a way to stop this error, I'd love to know!!
met same issue, my product heavy depend on fontawesome.
Facing the same issue here as well https://github.com/AathifMahir/MauiIcons/issues/79
Is there perhaps a workaround for this issue?
@GuidoNeele I had to use SVG based icons instead
Opened this one and will follow up with that team: microsoft/Win2D#941
It doesn't look like there is a huge amount of activity on that repository (and there are bugs affecting WinUI open from 2017). Is there anything that can be done to prioritise that issue?
I wouldn't normally ask this but I see this issue is marked as a "p/1" and it feels like it might be a few .NET versions away from being resolved.
@BurkusCat I'm not sure how much you can tell about what's going on via GitHub. Seems like most of the work for WinUI & Win2D take part behind closed doors. You can see the work for a similar issue was tracked via a different system: https://github.com/microsoft/Win2D/issues/891
I was watching WinUI 3 for a year waiting for the release blockers to be fixed before I could target an app against it, and one day with no warning around v1.4 the issues were closed and it was finally "good enough". Though both WinUI and MAUI are improving, they're still pretty niggly.
Maybe @Redth could give this issue a poke, since he likely knows the folk involved in the related fix?
Still facing this issue with latest Net8 SR... :( Is there any schedule for this issue?
We are currently blocked by Win2D/Windows: https://github.com/microsoft/Win2D/issues/941
Thanks for the update. Sadly not even a reaction on your issue since 3 months 🫠
@AndreasReitberger @BurkusCat @GuidoNeele @AathifMahir @mobynet1 maybe you guys wanna make some noise on the upstream ticket https://github.com/microsoft/Win2D/issues/941 to see if that helps with prioritization? It's not really MAUI's fault...
We have a fix coming soon. Hopefully we can get the maui-related changes in SR3 https://github.com/dotnet/maui/pull/20790
There is still a required update of win2d that does not exist yet, but the team is working on it and hopefully that goes out soon.
For the fix to be really fixed you need both:
- upcoming win2d release
- that maui PR
If we release the fix in SR3 then you can just update the csproj to use the new Win2D.
In SR4, all the things should be release and we can update Win2D in maui and we can make sure all the other APIs are working as expected before we force the updates on everyone.
Thank you for the update and the comms! I really appreciate its a tricky one to coordinate since it has a dependency but its great to hear the plan for it :)
Reopening this just until the new Win2D is released and updated. But, SR3 will be ready for it!
Thank you so much for your help on that 🙂
Hey @mattleibow , I thought I'd give the changes a try early. Should the below changes work?
-
I've updated to the latest .NET MAUI version:
<MauiVersion>8.0.10</MauiVersion>
-
Updated Win2D for Windows builds
<!-- Windows specifics-->
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.2.0" />
</ItemGroup>
Before above changes: | After above changes: |
---|---|
With CLR exceptions turned on, I get this error:
System.ArgumentException: 'The parameter is incorrect.
The URI specified in the CanvasTextFormat's FontFamily has an invalid scheme; the scheme may be omitted, or must be one of ms-appx:// or ms-appdata://.'
Facing the same issue as @BurkusCat
I updated to 8.0.10
and set WinUI to 1.2.0
.
Fonts still are missing and icons are shown as [ ]
@mattleibow any chance for a feedback from you? Should this already work or do we need to wait for SR4? Thank you :)
We are also facing same issue with 8.0.10 and WinUI 1.2.0.
I opened a new issue here: https://github.com/dotnet/maui/issues/21333
Just to note here as well as on #21333, that this works OK for me with MAUI 8.0.14 and WinUI 1.2.0.
I was having a problem all of a sudden today even though my app was working just fine, but then the font doesn't fall within the expected etc. One of the fonts was mistyped as .ttv not .ttf, but then it did it for another font and I'm like wtf, the OpenSans ones were just fine but the only difference between this other font and the three others I have was that there were spaces. I removed them and the exception went away DESPITE, again, working until after I updated to the most recent build 8.0.1 and continued seeing it after upgrading to 9.0.0.