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

[android] support more system font names #15759

Merged
merged 1 commit into from
Jun 20, 2023

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Jun 20, 2023

Context: #10713
Context: https://github.com/supershopping/ShellFlyoutLagging
Fixes #13239

In reviewing the above customer sample, I noticed time spent in:

79.52ms microsoft.maui!Microsoft.Maui.Handlers.LabelHandler.MapFont(Microsoft.Maui.Handlers.ILabelHandler,Microsoft.Maui.ILabel)
20.71ms microsoft.extensions.logging.abstractions!Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(Microsoft.Extensions

What warning are we trying to log? It seems like it is impacting the time it takes for the first Shell flyout to open.

Adding more logging, I found:

06-19 15:31:19.834  5706  5706 I DOTNET  : Unable to load font 'sans-serif-medium' from assets.
06-19 15:31:19.840  5706  5706 I DOTNET  : Unable to load font 'sans-serif-medium.ttf' from assets.
06-19 15:31:19.840  5706  5706 I DOTNET  : Unable to load font 'Fonts/sans-serif-medium.ttf' from assets.
06-19 15:31:19.841  5706  5706 I DOTNET  : Unable to load font 'fonts/sans-serif-medium.ttf' from assets.
06-19 15:31:19.841  5706  5706 I DOTNET  : Unable to load font 'sans-serif-medium.otf' from assets.
06-19 15:31:19.842  5706  5706 I DOTNET  : Unable to load font 'Fonts/sans-serif-medium.otf' from assets.
06-19 15:31:19.842  5706  5706 I DOTNET  : Unable to load font 'fonts/sans-serif-medium.otf' from assets.

Which is likely just trying to load one of:

In 2d354d1, we added support for certain Android system fonts. I think we should just expand this further to support various Android fonts like:

sans-serif-black
sans-serif-condensed
sans-serif-condensed-light
sans-serif-light
sans-serif-medium

You can use these in Android such as:

android:fontFamily="sans-serif-medium"

We can load this Typeface from C# such as:

Typeface.Create("sans-serif-medium", TypefaceStyle.Normal)

See: https://stackoverflow.com/a/14344132

In MAUI apps, customers are likely using a Fonts/ folder, so if they use these names, they are likely trying to load a system font.

After these changes, I see this result on a Pixel 5:

Before:
79.52ms microsoft.maui!Microsoft.Maui.Handlers.LabelHandler.MapFont(Microsoft.Maui.Handlers.ILabelHandler,Microsoft.Maui.ILabel)
After:
26.91ms microsoft.maui!Microsoft.Maui.Handlers.LabelHandler.MapFont(Microsoft.Maui.Handlers.ILabelHandler,Microsoft.Maui.ILabel)

So, I think this saves ~52ms of time for the first Shell flyout opening in this sample.

Context: dotnet#10713
Context: https://github.com/supershopping/ShellFlyoutLagging

In reviewing the above customer sample, I noticed time spent in:

    79.52ms microsoft.maui!Microsoft.Maui.Handlers.LabelHandler.MapFont(Microsoft.Maui.Handlers.ILabelHandler,Microsoft.Maui.ILabel)
    20.71ms microsoft.extensions.logging.abstractions!Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(Microsoft.Extensions

What warning are we trying to log? It seems like it is impacting the
time it takes for the first Shell flyout to open.

Adding more logging, I found:

    06-19 15:31:19.834  5706  5706 I DOTNET  : Unable to load font 'sans-serif-medium' from assets.
    06-19 15:31:19.840  5706  5706 I DOTNET  : Unable to load font 'sans-serif-medium.ttf' from assets.
    06-19 15:31:19.840  5706  5706 I DOTNET  : Unable to load font 'Fonts/sans-serif-medium.ttf' from assets.
    06-19 15:31:19.841  5706  5706 I DOTNET  : Unable to load font 'fonts/sans-serif-medium.ttf' from assets.
    06-19 15:31:19.841  5706  5706 I DOTNET  : Unable to load font 'sans-serif-medium.otf' from assets.
    06-19 15:31:19.842  5706  5706 I DOTNET  : Unable to load font 'Fonts/sans-serif-medium.otf' from assets.
    06-19 15:31:19.842  5706  5706 I DOTNET  : Unable to load font 'fonts/sans-serif-medium.otf' from assets.

Which is likely just trying to load one of:

* https://github.com/dotnet/maui/blob/0543e03cbe7331ae2c106c67a9eaefc575f75945/src/Controls/src/Core/Shell/BaseShellItem.cs#L517
* https://github.com/dotnet/maui/blob/0543e03cbe7331ae2c106c67a9eaefc575f75945/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellItemRenderer.cs#L248

In 2d354d1, we added support for certain Android system fonts. I think
we should just expand this further to support various Android fonts
like:

    sans-serif-black
    sans-serif-condensed
    sans-serif-condensed-light
    sans-serif-light
    sans-serif-medium

You can use these in Android such as:

    android:fontFamily="sans-serif-medium"

We can load this `Typeface` from C# such as:

    Typeface.Create("sans-serif-medium", TypefaceStyle.Normal)

See: https://stackoverflow.com/a/14344132

In MAUI apps, customers are likely using a `Fonts/` folder, so if they
use these names, they are likely trying to load a system font.

After these changes, I see this result on a Pixel 5:

    Before:
    79.52ms microsoft.maui!Microsoft.Maui.Handlers.LabelHandler.MapFont(Microsoft.Maui.Handlers.ILabelHandler,Microsoft.Maui.ILabel)
    After:
    26.91ms microsoft.maui!Microsoft.Maui.Handlers.LabelHandler.MapFont(Microsoft.Maui.Handlers.ILabelHandler,Microsoft.Maui.ILabel)

So, I think this saves ~52ms of time for the first Shell flyout opening
in this sample.
@jonathanpeppers
Copy link
Member Author

I think the font on the flyout menu in this app looks exactly the same:

image

@rmarinho rmarinho requested review from jsuarezruiz and PureWeen June 20, 2023 16:08
@jonathanpeppers jonathanpeppers marked this pull request as ready for review June 20, 2023 20:16
@mattleibow mattleibow merged commit 329046f into dotnet:main Jun 20, 2023
@jonathanpeppers jonathanpeppers deleted the AndroidSystemFonts branch June 20, 2023 22:35
@github-actions github-actions bot locked and limited conversation to collaborators Dec 9, 2023
@Eilon Eilon added t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) area-fonts Custom fonts and Font related API's and removed legacy-area-perf Startup / Runtime performance labels May 10, 2024
@samhouts samhouts added the fixed-in-8.0.0-preview.6.8686 Look for this fix in 8.0.0-preview.6.8686! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-fonts Custom fonts and Font related API's fixed-in-8.0.0-preview.6.8686 Look for this fix in 8.0.0-preview.6.8686! platform/android 🤖 t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Microsoft.Maui.FontManager: Warning: Unable to load font 'sans-serif-medium' from assets.
5 participants