JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16k stars 1.16k forks source link

Desktop: Allow passing class loader when loading font. #4184

Closed SalomonBrys closed 1 week ago

SalomonBrys commented 8 months ago

Loading a Font resource which TTF file is in the resource directory does not work if the file is in another module (or JAR) file than the main application module.

For example, this only works if JetBrainsMono-Regular.ttf is in the application resource directory, and not any other module:

Font(resource = "JetBrainsMono/JetBrainsMono-Regular.ttf", weight = FontWeight.Normal, style = FontStyle.Normal)

The exception, using 1.6.0-beta01 is:

java.lang.IllegalStateException: Can't load font from JetBrainsMono/JetBrainsMono-Regular.ttf
    at androidx.compose.ui.text.platform.DesktopFont_desktopKt.typefaceResource(DesktopFont.desktop.kt:181)
    at androidx.compose.ui.text.platform.DesktopFont_desktopKt.loadTypeface(DesktopFont.desktop.kt:169)
    at androidx.compose.ui.text.platform.FontCache$load$typeface$1.invoke(PlatformFont.skiko.kt:212)
    at androidx.compose.ui.text.platform.FontCache$load$typeface$1.invoke(PlatformFont.skiko.kt:211)
    at androidx.compose.ui.text.ExpireAfterAccessCache.get(Cache.skiko.kt:45)
    at androidx.compose.ui.text.platform.FontCache.load$ui_text(PlatformFont.skiko.kt:211)
    at androidx.compose.ui.text.font.SkiaFontLoader.loadBlocking(SkiaFontLoader.skiko.kt:43)
    at androidx.compose.ui.text.font.SkiaFontLoader.loadBlocking(SkiaFontLoader.skiko.kt:27)
    at androidx.compose.ui.text.font.FontListFontFamilyTypefaceAdapterKt.firstImmediatelyAvailable(FontListFontFamilyTypefaceAdapter.kt:191)
    ... 100 more

This happens because the class loader used is Thread.currentThread().contextClassLoader (See DesktopFont.desktop.kt:178).

This makes impossible to create JAR libraries with compose function that provides their own font files.

There should be an optional parameter to the Font function that takes a class loader

vlohachov commented 7 months ago

Faced same issue but for wasm, fonts from multiplatform shared library cannot be properly loaded that leads to this

Screenshot 2024-03-05 at 09 44 27
okushnikov commented 1 month ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.