Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MauiFont not usable if added in MauiLibrary #21771

Closed
Tracked by #495
AndreasReitberger opened this issue Apr 11, 2024 · 5 comments
Closed
Tracked by #495

MauiFont not usable if added in MauiLibrary #21771

AndreasReitberger opened this issue Apr 11, 2024 · 5 comments
Labels
platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@AndreasReitberger
Copy link

Description

I have a shared maui style library which contains some custom fonts. Those fonts are registered as MauiFont resources in the library.

The library is added as nuget to a MauiApp project.

Library

The font is added via a AppHostBuilderExtension

 public static class AppHostBuilderExtensions
 {
     public static MauiAppBuilder InitializeSharedMauiStyles(this MauiAppBuilder builder)
     {
         builder.RegisterSharedFonts();
         return builder;
     }

     public static MauiAppBuilder RegisterSharedFonts(this MauiAppBuilder builder)
     {
         builder
             .ConfigureFonts(fonts =>
             {
                 fonts.AddFont("materialdesignicons-webfont.ttf", "MaterialDesignIcons");
             });
         return builder;
     }
 }

In the project file of the library, the fonts are all added as MauiFont

	<ItemGroup>
		<!-- Custom Fonts -->
		<MauiFont Include="Resources\Fonts\*" />
	</ItemGroup>

image

App

The nuget MauiLib is installed to the MauiApp.
In the MauiProgram.cs, the fonts are registered.

        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                // Init fonts from library
                .InitializeSharedMauiStyles()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");

#if DEBUG
                    foreach (var f in fonts)
                    {
                        Debug.WriteLine($"Font: {f.Filename} / {f.Alias}");
                    }
#endif
                });

#if DEBUG
    		builder.Logging.AddDebug();
#endif

            return builder.Build();
        }

Console output is as following:

Font: materialdesignicons-webfont.ttf / MaterialDesignIcons
Font: OpenSans-Regular.ttf / OpenSansRegular
Font: OpenSans-Semibold.ttf / OpenSansSemibold

If I start the MauiApp, I'll get this exception.

Microsoft.Maui.FontRegistrar: Warning: Unable to load font 'MaterialDesignIcons'.

System.IO.FileNotFoundException: Native font with the name materialdesignicons-webfont.ttf was not found.
at Microsoft.Maui.FontRegistrar.LoadNativeAppFont(String font, String filename, String alias)
at Microsoft.Maui.FontRegistrar.GetFont(String font)
'MauiApp.exe' (CoreCLR: clrhost): Loaded 'C:\Users\q36606\Documents\GitHub\MauiFontsIssueLibrary\src\MauiApp\MauiApp\bin\Debug\net8.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.Graphics.Canvas.Interop.dll'.
Exception thrown: 'System.ArgumentException' in WinRT.Runtime.dll
WinRT information: "Assets/Fonts/MaterialDesignIcons.ttf" ist kein gültiger absoluter URI.
Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/MaterialDesignIcons.ttf'.

System.ArgumentException: Falscher Parameter.

"Assets/Fonts/MaterialDesignIcons.ttf" ist kein gültiger absoluter URI.
at WinRT.ExceptionHelpers.g__Throw|39_0(Int32 hr)
at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
at ABI.System.WinRTUriRuntimeClassFactory.CreateUriForMarshaling(String uri)
at ABI.System.Uri.CreateMarshaler2(Uri value)
at Microsoft.Graphics.Canvas.Text.CanvasFontSet._ICanvasFontSetFactory.Create(Uri uri)
at Microsoft.Graphics.Canvas.Text.CanvasFontSet..ctor(Uri uri)
at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)
Exception thrown: 'System.ArgumentException' in WinRT.Runtime.dll
WinRT information: "Assets/Fonts/MaterialDesignIcons.otf" ist kein gültiger absoluter URI.
Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/MaterialDesignIcons.otf'.

System.ArgumentException: Falscher Parameter.

"Assets/Fonts/MaterialDesignIcons.otf" ist kein gültiger absoluter URI.
at WinRT.ExceptionHelpers.g__Throw|39_0(Int32 hr)
at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
at ABI.System.WinRTUriRuntimeClassFactory.CreateUriForMarshaling(String uri)
at ABI.System.Uri.CreateMarshaler2(Uri value)
at Microsoft.Graphics.Canvas.Text.CanvasFontSet._ICanvasFontSetFactory.Create(Uri uri)
at Microsoft.Graphics.Canvas.Text.CanvasFontSet..ctor(Uri uri)
at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)

image

If I'm adding just a reference to the MauiLib, instead of installing the nuget, it starts to work.

MauiApp

<ItemGroup>
	<!--If adding the nuget, fonts are not loaded 
	<PackageReference Include="MauiLib" Version="1.0.1" />-->
	<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.20" />
	<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.20" />
	<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
	<!-- If adding the library as reference, it does work -->
	<!----><ProjectReference Include="..\MauiLib\MauiLib.csproj" />
</ItemGroup>

image

So it seems that the font gets missing or moved when packaged as Nuget
Maybe this is relevant to:
#10112

Steps to Reproduce

  1. Run the project on Windows
  2. See that the font is not displayed
  3. Go to the MauiApp project file and disable the nuget and enable the library reference
  4. The font is now displayed

Link to public reproduction project repository

https://github.com/AndreasReitberger/MauiFontsIssueLibrary

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

Also add the MauiFont file to the MauiApp project.

Relevant log output

No response

@RoiChen001
Copy link

RoiChen001 commented Apr 11, 2024

Can repro this issue at Windows platform on the latest 17.10 Preview 3(8.0.10&8.0.14&8.0.20).

@RoiChen001 RoiChen001 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Apr 11, 2024
@PureWeen
Copy link
Member

Duplicate of #19804

@GuidoNeele
Copy link
Contributor

Thought we had to use this #10019 (comment) (demo repo https://github.com/awalker-dsg/MauiTestLib_10019/tree/add_targets_file/MauiTestLib_10019) for sharing resources between projects. This does work for svg files but sadly the FontImageSource for unpacked apps doesn't work with version 8.0.20.

@AndreasReitberger
Copy link
Author

AndreasReitberger commented Apr 19, 2024

@GuidoNeele
Thanks for the link to the workaround. I just tried this with MauiFont instead.

I created the file SharedMauiXamlStylesLibrary.targets with following content.

<Project>
    <!-- https://github.com/dotnet/maui/issues/10019#issuecomment-1248032520 -->
  <ItemGroup>
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\FluentFontIcons.ttf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\FontAwesome5Brands.otf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\FontAwesome5Solid.otf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\materialdesignicons-webfont.ttf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\Montserrat-Bold.ttf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\Montserrat-Medium.ttf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\Montserrat-Regular.ttf" />
    <MauiFont Include="$(MSBuildThisFileDirectory)\Fonts\Montserrat-SemiBold.ttf" />
  </ItemGroup>
</Project>

In the csproj file I added following addition.

<!-- Workaround: https://github.com/dotnet/maui/issues/10019#issuecomment-1248032520 -->
<ItemGroup>
	<None Include="..\..\SharedMauiXamlStylesLibrary.targets" Pack="True" PackagePath="buildTransitive\" />
</ItemGroup>
<ItemGroup>
	<!-- Custom Fonts -->
	<MauiFont Include="Resources\Fonts\*" />-->
	<!-- Workaround: https://github.com/dotnet/maui/issues/10019#issuecomment-1248032520 -->
	<None Include="Resources\Fonts\FluentFontIcons.ttf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\FontAwesome5Brands.otf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\FontAwesome5Regular.otf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\FontAwesome5Solid.otf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\materialdesignicons-webfont.ttf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\Montserrat-Bold.ttf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\Montserrat-Medium.ttf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\Montserrat-Regular.ttf" Pack="True" PackagePath="buildTransitive\Fonts\" />
	<None Include="Resources\Fonts\Montserrat-SemiBold.ttf" Pack="True" PackagePath="buildTransitive\Fonts\" />
</ItemGroup>

This seems to fix the issue as well as for svg files.
Thanks!

@mattleibow
Copy link
Member

Duplicate of #19804

@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants