Skip to content

Commit

Permalink
Font with lazy get data in skikoMain (#906)
Browse files Browse the repository at this point in the history
We just added new function Font with lazy getData lambda.
We don't need changes in Skiko library.
  • Loading branch information
dima-avdeev-jb authored and mazunin-v-jb committed Dec 7, 2023
1 parent 8c2ad22 commit f1760a3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
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()

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,
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

0 comments on commit f1760a3

Please sign in to comment.