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

Multiple font assets specified using <font> tag aren't preloaded by lime.utils.Preloader #1758

Closed
mallyskies opened this issue Feb 7, 2024 · 2 comments

Comments

@mallyskies
Copy link

Declaring multiple fonts in a Lime project.xml file using the assets snippet below don't preload on HTML5 builds:

<assets path="/usr/local/lib/haxe/lib/openfl/9,3,2/assets/fonts"
        rename="fonts">
    <font path="fonts/NotoSans-Regular.ttf"/>
    <font path="fonts/NotoSerif-Regular.ttf"/>
    <font path="fonts/NotoMono-Regular.ttf"/>
</assets>

You can see this by creating a simple project, such as the AddingText sample project, adding the above to the project.xml file (using your own path to the TTF files), and running it with the verbose flag to enable verbose logging by Lime (e.g. openfl -verbose -debug test html5), which should produce this output:

[Log] [lime.utils.Preloader] Preloading asset library (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.woff [BINARY] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.svg [TEXT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.eot [BINARY] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.ttf [FONT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset:  [FONT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.woff [BINARY] (1/5) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.eot [BINARY] (2/5) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.svg [TEXT] (3/5) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.ttf [FONT] (4/5) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset:  [FONT] (5/5) (AddingText.js, line 24958)
[Log] [lime.utils.Preloader] Loaded asset library [1/1] (AddingText.js, line 24958)
[Log] [lime.utils.Preloader] Preload complete (AddingText.js, line 24958)

The assets included by the sample project's existing <assets> tag are all loaded, but the three fonts added by the new tag are not mentioned, though there is a line stating "Loaded asset: [FONT] (5/5)" without any asset name.
The explanation for that console output is due to how lime.utils.AssetLibrary.__fromManifest() processes the asset. On this statement, the id is assigned:

id = Reflect.hasField(asset, "id") ? asset.id : asset.path;

However, for the fonts using the <font> tag, they have no asset id at all and the path is the empty string "", which is why the output for [FONT] (5/5) doesn't show a path like the other assets. And since id is used as the key in a StringMap, only one font asset ends up being added to the map instead of three.

A fairly simple workaround is to include all the assets in a specified folder, or a subset using <assets include="..."/> or <assets exclude="..."/>. That's what the sample project does, and why its fonts do preload. Another workaround is to specify each font as a separate asset, like this:

<assets rename="fonts/NotoSans-Regular.ttf"
        path="/usr/local/lib/haxe/lib/openfl/9,3,2/assets/fonts/NotoSans-Regular.ttf"/>
<assets rename="fonts/NotoSerif-Regular.ttf"
        path="/usr/local/lib/haxe/lib/openfl/9,3,2/assets/fonts/NotoSerif-Regular.ttf"/>
<assets rename="fonts/NotoMono-Regular.ttf"
        path="/usr/local/lib/haxe/lib/openfl/9,3,2/assets/fonts/NotoMono-Regular.ttf"/>

Using this syntax, the verbose console output looks correct:

[Log] [lime.utils.Preloader] Preloading asset library (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.woff [BINARY] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.svg [TEXT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.eot [BINARY] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: assets/KatamotzIkasi.ttf [FONT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: fonts/NotoSans-Regular.ttf [FONT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: fonts/NotoSerif-Regular.ttf [FONT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Preloading asset: fonts/NotoMono-Regular.ttf [FONT] (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.woff [BINARY] (1/7) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.eot [BINARY] (2/7) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.svg [TEXT] (3/7) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: assets/KatamotzIkasi.ttf [FONT] (4/7) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: fonts/NotoSans-Regular.ttf [FONT] (5/7) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: fonts/NotoSerif-Regular.ttf [FONT] (6/7) (AddingText.js, line 24958)
[Log] [lime.utils.AssetLibrary] Loaded asset: fonts/NotoMono-Regular.ttf [FONT] (7/7) (AddingText.js, line 24958)
[Log] [lime.utils.Preloader] Loaded asset library [1/1] (AddingText.js, line 24958)

Since the workaround is fairly straightforward, I don't think this issue is particularly important to fix. But I wanted it documented for others since it isn't clear how to successfully preload fonts for HTML5 targets in OpenFL. I tested this using OpenFL 9.3.2 and Lime 8.0.2, but I suspect it's been this way a long time.

@player-03
Copy link
Contributor

<assets path="/usr/local/lib/haxe/lib/openfl/9,3,2/assets/fonts"
        rename="fonts">
    <font path="fonts/NotoSans-Regular.ttf"/>
    <font path="fonts/NotoSerif-Regular.ttf"/>
    <font path="fonts/NotoMono-Regular.ttf"/>
</assets>

I assume this was a typo? Because that would produce "/usr/local/lib/haxe/lib/openfl/9,3,2/assets/fonts/fonts/NotoSans-Regular.ttf", which shouldn't exist.

There were a couple other bits I couldn't reproduce either, but it all ended up being close enough. Your insight about the id being an empty string was correct, and that saved me a bunch of time tracking down the source. So, thanks for the detailed report!

@mallyskies
Copy link
Author

mallyskies commented Feb 13, 2024 via email

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

No branches or pull requests

2 participants