diff --git a/compose/ui/ui-text/api/desktop/ui-text.api b/compose/ui/ui-text/api/desktop/ui-text.api index ae0e131721702..85649d78c1766 100644 --- a/compose/ui/ui-text/api/desktop/ui-text.api +++ b/compose/ui/ui-text/api/desktop/ui-text.api @@ -1359,7 +1359,9 @@ public abstract class androidx/compose/ui/text/platform/PlatformFont : androidx/ } public final class androidx/compose/ui/text/platform/PlatformFont_skikoKt { + public static final fun Font-wCLgNak (Ljava/lang/String;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/text/font/FontWeight;I)Landroidx/compose/ui/text/font/Font; public static final fun Font-wCLgNak (Ljava/lang/String;[BLandroidx/compose/ui/text/font/FontWeight;I)Landroidx/compose/ui/text/font/Font; + public static synthetic fun Font-wCLgNak$default (Ljava/lang/String;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/text/font/FontWeight;IILjava/lang/Object;)Landroidx/compose/ui/text/font/Font; public static synthetic fun Font-wCLgNak$default (Ljava/lang/String;[BLandroidx/compose/ui/text/font/FontWeight;IILjava/lang/Object;)Landroidx/compose/ui/text/font/Font; public static final fun Typeface (Lorg/jetbrains/skia/Typeface;Ljava/lang/String;)Landroidx/compose/ui/text/font/Typeface; public static synthetic fun Typeface$default (Lorg/jetbrains/skia/Typeface;Ljava/lang/String;ILjava/lang/Object;)Landroidx/compose/ui/text/font/Typeface; diff --git a/compose/ui/ui-text/src/desktopMain/kotlin/androidx/compose/ui/text/platform/DesktopFont.desktop.kt b/compose/ui/ui-text/src/desktopMain/kotlin/androidx/compose/ui/text/platform/DesktopFont.desktop.kt index a5fee5389169a..302d80068fff2 100644 --- a/compose/ui/ui-text/src/desktopMain/kotlin/androidx/compose/ui/text/platform/DesktopFont.desktop.kt +++ b/compose/ui/ui-text/src/desktopMain/kotlin/androidx/compose/ui/text/platform/DesktopFont.desktop.kt @@ -163,7 +163,7 @@ internal actual fun loadTypeface(font: Font): SkTypeface { return when (font) { is ResourceFont -> typefaceResource(font.name) is FileFont -> SkTypeface.makeFromFile(font.file.toString()) - is LoadedFont -> SkTypeface.makeFromData(Data.makeFromBytes(font.data)) + is LoadedFont -> SkTypeface.makeFromData(Data.makeFromBytes(font.getData())) is SystemFont -> SkTypeface.makeFromName(font.identity, font.skFontStyle) } } diff --git a/compose/ui/ui-text/src/jsWasmMain/kotlin/androidx/compose/ui/text/platform/JsFont.js.kt b/compose/ui/ui-text/src/jsWasmMain/kotlin/androidx/compose/ui/text/platform/JsFont.js.kt index f7d2e72cd0e2f..94ca19494c49d 100644 --- a/compose/ui/ui-text/src/jsWasmMain/kotlin/androidx/compose/ui/text/platform/JsFont.js.kt +++ b/compose/ui/ui-text/src/jsWasmMain/kotlin/androidx/compose/ui/text/platform/JsFont.js.kt @@ -30,7 +30,7 @@ internal actual fun loadTypeface(font: Font): SkTypeface { throw IllegalArgumentException("Unsupported font type: $font") } return when (font) { - is LoadedFont -> SkTypeface.makeFromData(Data.makeFromBytes(font.data)) + is LoadedFont -> SkTypeface.makeFromData(Data.makeFromBytes(font.getData())) is SystemFont -> SkTypeface.makeFromName(font.identity, font.skFontStyle) } } diff --git a/compose/ui/ui-text/src/nativeMain/kotlin/androidx/compose/ui/text/platform/NativeFont.native.kt b/compose/ui/ui-text/src/nativeMain/kotlin/androidx/compose/ui/text/platform/NativeFont.native.kt index cd53863ef846f..dcbee374c457f 100644 --- a/compose/ui/ui-text/src/nativeMain/kotlin/androidx/compose/ui/text/platform/NativeFont.native.kt +++ b/compose/ui/ui-text/src/nativeMain/kotlin/androidx/compose/ui/text/platform/NativeFont.native.kt @@ -26,7 +26,7 @@ internal actual fun loadTypeface(font: Font): SkTypeface { } @Suppress("REDUNDANT_ELSE_IN_WHEN") return when (font) { - is LoadedFont -> SkTypeface.makeFromData(Data.makeFromBytes(font.data)) + is LoadedFont -> SkTypeface.makeFromData(Data.makeFromBytes(font.getData())) // TODO: compilation fails without `else` see https://youtrack.jetbrains.com/issue/KT-43875 else -> throw IllegalArgumentException("Unsupported font type: $font") } diff --git a/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/PlatformFont.skiko.kt b/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/PlatformFont.skiko.kt index 2f18cb2a34e57..24eab9b08821e 100644 --- a/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/PlatformFont.skiko.kt +++ b/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/PlatformFont.skiko.kt @@ -57,7 +57,7 @@ class SystemFont( * Defines a Font using byte array with loaded font data. * * @param identity Unique identity for a font. Used internally to distinguish fonts. - * @param data Byte array with loaded font data. + * @param getData should return Byte array with loaded font data. * @param weight The weight of the font. The system uses this to match a font to a font request * that is given in a [androidx.compose.ui.text.SpanStyle]. * @param style The style of the font, normal or italic. The system uses this to match a font to a @@ -67,13 +67,15 @@ class SystemFont( */ class LoadedFont internal constructor( override val identity: String, - val data: ByteArray, + internal val getData: () -> ByteArray, override val weight: FontWeight, override val style: FontStyle ) : PlatformFont() { @ExperimentalTextApi override val loadingStrategy: FontLoadingStrategy = FontLoadingStrategy.Blocking + val data: ByteArray get() = getData() + override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is LoadedFont) return false @@ -98,7 +100,7 @@ class LoadedFont internal constructor( * Creates a Font using byte array with loaded font data. * * @param identity Unique identity for a font. Used internally to distinguish fonts. - * @param data Byte array with loaded font data. + * @param getData should return Byte array with loaded font data. * @param weight The weight of the font. The system uses this to match a font to a font request * that is given in a [androidx.compose.ui.text.SpanStyle]. * @param style The style of the font, normal or italic. The system uses this to match a font to a @@ -108,10 +110,10 @@ class LoadedFont internal constructor( */ fun Font( identity: String, - data: ByteArray, + getData: () -> ByteArray, weight: FontWeight = FontWeight.Normal, style: FontStyle = FontStyle.Normal -): Font = LoadedFont(identity, data, weight, style) +): Font = LoadedFont(identity, getData, weight, style) private class SkiaBackedTypeface( alias: String?, @@ -121,6 +123,30 @@ private class SkiaBackedTypeface( override val fontFamily: FontFamily? = null } +/** + * Creates a Font using byte array with loaded font data. + * + * @param identity Unique identity for a font. Used internally to distinguish fonts. + * @param data Byte array with loaded font data. + * @param weight The weight of the font. The system uses this to match a font to a font request + * that is given in a [androidx.compose.ui.text.SpanStyle]. + * @param style The style of the font, normal or italic. The system uses this to match a font to a + * font request that is given in a [androidx.compose.ui.text.SpanStyle]. + * + * @see FontFamily + */ +fun Font( + identity: String, + data: ByteArray, + weight: FontWeight = FontWeight.Normal, + style: FontStyle = FontStyle.Normal +): Font = Font( + identity = identity, + getData = { data }, + weight = weight, + style = style, +) + /** * Returns a Compose [Typeface] from Skia [SkTypeface]. *