Skip to content

Commit

Permalink
Use new font loading mechanism from Compose 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Jan 12, 2024
1 parent b267475 commit 56c5fa0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.platform.Typeface
import androidx.compose.ui.text.platform.asComposeFontFamily
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
Expand All @@ -28,13 +28,8 @@ import com.intellij.util.ui.JBDimension
import com.intellij.util.ui.JBFont
import com.intellij.util.ui.JBInsets
import com.intellij.util.ui.JBValue
import kotlinx.coroutines.runBlocking
import org.jetbrains.skia.Typeface
import org.jetbrains.skiko.DependsOnJBR
import org.jetbrains.skiko.awt.font.AwtFontManager
import org.jetbrains.skiko.toSkikoTypefaceOrNull
import java.awt.Dimension
import java.awt.Font
import java.awt.Insets
import javax.swing.UIManager

Expand Down Expand Up @@ -158,17 +153,15 @@ public fun retrieveArcAsCornerSizeWithFallbacks(vararg keys: String): CornerSize
keysNotFound(keys.toList(), "Int")
}

@OptIn(DependsOnJBR::class)
private val awtFontManager = AwtFontManager()

@DependsOnJBR
public suspend fun retrieveTextStyle(fontKey: String, colorKey: String? = null): TextStyle {
public fun retrieveTextStyle(fontKey: String, colorKey: String? = null): TextStyle {
val baseColor = colorKey?.let { retrieveColorOrUnspecified(colorKey) } ?: Color.Unspecified
return retrieveTextStyle(fontKey, color = baseColor)
}

@OptIn(ExperimentalTextApi::class)
@DependsOnJBR
public suspend fun retrieveTextStyle(
public fun retrieveTextStyle(
key: String,
color: Color = Color.Unspecified,
lineHeight: TextUnit = TextUnit.Unspecified,
Expand All @@ -182,33 +175,17 @@ public suspend fun retrieveTextStyle(
val derivedFont = jbFont.let { if (bold) it.asBold() else it.asPlain() }
.let { if (fontStyle == FontStyle.Italic) it.asItalic() else it }

val typeface = derivedFont.toSkikoTypefaceOrNull(awtFontManager)
?: Typeface.makeDefault().also {
logger.warn(
"Unable to convert font ${jbFont.fontName} into a Skiko typeface, " +
"fallback to 'Typeface.makeDefault()'",
)
}

return TextStyle(
color = color,
fontSize = size.takeOrElse { derivedFont.size.sp / UISettingsUtils.getInstance().currentIdeScale },
fontWeight = if (bold) FontWeight.Bold else FontWeight.Normal,
fontStyle = fontStyle,
fontFamily = FontFamily(Typeface(typeface)),
// todo textDecoration might be defined in the awt theme
fontFamily = derivedFont.asComposeFontFamily(),
// TODO textDecoration might be defined in the AWT theme
lineHeight = lineHeight,
)
}

@DependsOnJBR
public fun Font.toFontFamily(): FontFamily {
val typeface = runBlocking { toSkikoTypefaceOrNull(awtFontManager) }
?: error("Can't turn $this into a Typeface")

return FontFamily(Typeface(typeface))
}

public val JBValue.dp: Dp
get() = unscaled.dp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import kotlinx.datetime.toJavaLocalDate
import org.jetbrains.jewel.bridge.retrieveColorOrUnspecified
import org.jetbrains.jewel.bridge.retrieveTextStyle
import org.jetbrains.jewel.bridge.toComposeColor
import org.jetbrains.jewel.bridge.toFontFamily
import org.jetbrains.jewel.foundation.lazy.SelectableLazyColumn
import org.jetbrains.jewel.foundation.lazy.SelectionMode
import org.jetbrains.jewel.foundation.lazy.items
Expand Down Expand Up @@ -559,8 +558,7 @@ private fun AndroidStudioReleaseDetails(item: ContentItem.AndroidStudio) {
private fun TextWithLabel(labelText: String, valueText: String) {
Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) {
Text(labelText)
val fontFamily = JBFont.label().asBold().toFontFamily()
Text(valueText, fontFamily = fontFamily, fontWeight = FontWeight.W600)
Text(valueText, fontWeight = FontWeight.W600)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.jetbrains.jewel.samples.standalone
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.ResourceLoader
import androidx.compose.ui.res.loadSvgPainter
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.Density
import androidx.compose.ui.window.application
import org.jetbrains.jewel.foundation.theme.JewelTheme
Expand All @@ -20,14 +23,18 @@ import org.jetbrains.jewel.window.DecoratedWindow
import org.jetbrains.jewel.window.styling.TitleBarStyle
import java.io.InputStream

@OptIn(ExperimentalTextApi::class)
fun main() {
val icon = svgResource("icons/jewel-logo.svg")

application {
val textStyle = TextStyle(fontFamily = FontFamily("Inter"))

val themeDefinition =
if (MainViewModel.theme.isDark()) {
JewelTheme.darkThemeDefinition()
JewelTheme.darkThemeDefinition(defaultTextStyle = textStyle)
} else {
JewelTheme.lightThemeDefinition()
JewelTheme.lightThemeDefinition(defaultTextStyle = textStyle)
}

IntUiTheme(
Expand Down

0 comments on commit 56c5fa0

Please sign in to comment.