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

Font with lazy get data in skikoMain #906

Merged
merged 10 commits into from
Nov 23, 2023
2 changes: 2 additions & 0 deletions compose/ui/ui-text/api/desktop/ui-text.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but maybe better to make it by lazy to avoid unexpected multirun for the lambda

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggestion! Done!


override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is LoadedFont) return false
Expand All @@ -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
Expand All @@ -108,10 +110,10 @@ class LoadedFont internal constructor(
*/
fun Font(
identity: String,
data: ByteArray,
getData: () -> ByteArray,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getData: () -> ByteArray,
data: () -> ByteArray,

or

Suggested change
getData: () -> ByteArray,
dataReader: () -> ByteArray,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, better to leave getData(). It is simple name for this case.

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?,
Expand All @@ -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].
*
Expand Down