cramos
Topic Author
Posts: 4
Joined: 30 Jan 2024, 16:40

How to only load current used font

17 Sep 2024, 10:45

We're using different fonts depending on the language, but we want to only load the current font needed in order to save memory (some platforms have very limited RAM).

This is what we have in our Root View:
[...]
    <noesis:Xaml.Dependencies>
        <noesis:Dependency Source="/Assets/UI/Fonts/All.Font.xaml"/>
    </noesis:Xaml.Dependencies>
[...]
This is All.Font.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">

    <FrameworkElement x:Key="Dependencies">
        <noesis:Xaml.Dependencies>
            <noesis:Dependency Source="Default/Default.Font.xaml"/>
            <noesis:Dependency Source="JP/Japanese.Font.xaml"/>
            <noesis:Dependency Source="KR/Korean.Font.xaml"/>
            <noesis:Dependency Source="SC/SimplifiedChinese.Font.xaml"/>
            <noesis:Dependency Source="TC/TraditionalChinese.Font.xaml"/>            
        </noesis:Xaml.Dependencies>
    </FrameworkElement>

</ResourceDictionary>
This is Default.Font.xaml:
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:sys="clr-namespace:System;assembly=mscorlib">
    
    <FontFamily x:Key="MainFont">
        /Assets/UI/Fonts/#OurDefaultFont
    </FontFamily>
</ResourceDictionary>
This is Japanese.Font.xaml:
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:sys="clr-namespace:System;assembly=mscorlib">
    
    <FontFamily x:Key="MainFont">
        /Assets/UI/Fonts/JP/#OurJapaneseFont
    </FontFamily>
</ResourceDictionary>
This is how we handle the resource dictionaries in the RootView code:
        protected void UpdateFont(LocGeneralLanguage generalLanguage)
        {
            ResourceDictionary newFont = GetFont("Default/Default.Font.xaml");

            switch (generalLanguage)
            {
                case LocGeneralLanguage.ja:
                    newFont = GetFont("JP/Japanese.Font.xaml");
                    break;

                case LocGeneralLanguage.ko:
                    newFont = GetFont("KR/Korean.Font.xaml");
                    break;

                case LocGeneralLanguage.zn_ch:
                    newFont = GetFont("SC/SimplifiedChinese.Font.xaml");
                    break;

                case LocGeneralLanguage.zh_tw:
                    newFont = GetFont("TC/TraditionalChinese.Font.xaml");
                    break;
            }

            RefreshResources(newFont);
        }

        protected ResourceDictionary GetFont(string fontName)
        {
            if (ResourceDictionaries.TryGetValue(fontName, out ResourceDictionary resourceDictionaryFont))
            {
                return resourceDictionaryFont;
            }

            Uri uri = new Uri("Assets/UI/Fonts/" + fontName, UriKind.Relative);
            resourceDictionaryFont = new ResourceDictionary { Source = uri };
            ResourceDictionaries.Add(fontName, resourceDictionaryFont);

            return resourceDictionaryFont;
        }
        
        private void RefreshResources(ResourceDictionary newFont)
        {
            Resources.MergedDictionaries.Clear();
            Resources.MergedDictionaries.Add(newFont);
            PersistentContext.GetPreSessionState().CurrentFont = newFont;
        }
With this setup we've seen that all the fonts are currently being loaded into memory. Looks like that by just using noesis:Dependency the font will be loaded. Is there a way to define the fonts the project needs, but only load the specific font needed for each case?
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: How to only load current used font

17 Sep 2024, 20:59

Hi, using noesis:Dependency should not load the referenced xaml, only register it in the Xaml provider (along with its dependencies) so it can be later found and loaded.
Perhaps you mean that the font asset is "loaded" by Unity (because it is referenced by another asset: the xaml)?
 
cramos
Topic Author
Posts: 4
Joined: 30 Jan 2024, 16:40

Re: How to only load current used font

18 Sep 2024, 11:13

Hi!
Perhaps you mean that the font asset is "loaded" by Unity (because it is referenced by another asset: the xaml)?
Yeah, that's what I mean.
 
User avatar
jsantos
Site Admin
Posts: 4123
Joined: 20 Jan 2012, 17:18
Contact:

Re: How to only load current used font

19 Sep 2024, 10:21

If the font is a dependency, it will be included in the final assets, but it will only be loaded if you use it. Unfortunately, I have noticed that when Unity launches, we load and unload all fonts to scan their information. This process could be optimized to avoid unnecessary loading. Could you please create a ticket to address this issue?

What behavior are you observing?

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests