From 0c0680e63d1282c479bb2ba950572a766cbfd950 Mon Sep 17 00:00:00 2001 From: Sebastiano Poggi Date: Mon, 9 Sep 2024 12:45:40 +0200 Subject: [PATCH] Use a line-height of 1.3x for the default text styles (#587) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use a line-height of 1.3x for the default text styles It looks like Swing uses a line height that is 1.3 times the font size, but that is not expressed anywhere I could find. I got the value by experimentation and pixel counting, like a proper DF review would 🙃 Using this value seems to work at both default and h[0-5] text sizes. Having a non-default line height is a bit of a pain for users, since they need to remember updating the line height too when they customize the font size in a text style; however, we can't do anything about it, and in general, folks should stick to the predefined text styles. To help, we now provide a `TextStyle.copyWithSize()` function that automatically sets the correct line height, making this slightly less painful. * Actually use the line height constant in the bridge Also solves the issue that we were ignoring the passed in line height; now we use the default only as fallback if a height is not passed in. --- ide-laf-bridge/api/ide-laf-bridge.api | 4 +- .../org/jetbrains/jewel/bridge/BridgeUtils.kt | 15 +- .../jewel/bridge/SwingBridgeService.kt | 3 +- .../org/jetbrains/jewel/bridge/Typography.kt | 5 +- .../intui/standalone/theme/TextStyles.kt | 9 +- .../ideplugin/SwingComparisonTabPanel.kt | 45 +++++ ui/api/ui.api | 6 + .../jewel/ui/component/Typography.kt | 175 +++++++++++++++++- 8 files changed, 246 insertions(+), 16 deletions(-) diff --git a/ide-laf-bridge/api/ide-laf-bridge.api b/ide-laf-bridge/api/ide-laf-bridge.api index f86286c20..c1b87cc2d 100644 --- a/ide-laf-bridge/api/ide-laf-bridge.api +++ b/ide-laf-bridge/api/ide-laf-bridge.api @@ -36,10 +36,10 @@ public final class org/jetbrains/jewel/bridge/BridgeUtilsKt { public static final fun retrieveIntAsDp-3F_vd3o (Ljava/lang/String;Landroidx/compose/ui/unit/Dp;)F public static synthetic fun retrieveIntAsDp-3F_vd3o$default (Ljava/lang/String;Landroidx/compose/ui/unit/Dp;ILjava/lang/Object;)F public static final fun retrieveIntAsDpOrUnspecified (Ljava/lang/String;)F - public static final fun retrieveTextStyle (Ljava/lang/String;Ljava/lang/String;)Landroidx/compose/ui/text/TextStyle; - public static synthetic fun retrieveTextStyle$default (Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle; public static final fun retrieveTextStyle-WdJyH8Q (Ljava/lang/String;JJZIJ)Landroidx/compose/ui/text/TextStyle; public static synthetic fun retrieveTextStyle-WdJyH8Q$default (Ljava/lang/String;JJZIJILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle; + public static final fun retrieveTextStyle-tD9LlGs (Ljava/lang/String;Ljava/lang/String;JZIJ)Landroidx/compose/ui/text/TextStyle; + public static synthetic fun retrieveTextStyle-tD9LlGs$default (Ljava/lang/String;Ljava/lang/String;JZIJILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle; public static final fun toComposeColor (Ljava/awt/Color;)J public static final fun toComposeColorOrUnspecified (Ljava/awt/Color;)J public static final fun toDpSize (Lcom/intellij/util/ui/JBDimension;)J diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt index f12ea65cb..e9174d9ef 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt @@ -36,6 +36,7 @@ import com.intellij.util.ui.JBValue import java.awt.Dimension import java.awt.Insets import javax.swing.UIManager +import org.jetbrains.jewel.ui.component.Typography private val logger = Logger.getInstance("JewelBridge") @@ -148,9 +149,19 @@ public fun retrieveArcAsCornerSizeWithFallbacks(vararg keys: String): CornerSize keysNotFound(keys.toList(), "Int") } -public fun retrieveTextStyle(fontKey: String, colorKey: String? = null): TextStyle { +public fun retrieveTextStyle( + fontKey: String, + colorKey: String? = null, + lineHeight: TextUnit = TextUnit.Unspecified, + bold: Boolean = false, + fontStyle: FontStyle = FontStyle.Normal, + size: TextUnit = TextUnit.Unspecified, +): TextStyle { val baseColor = colorKey?.let { retrieveColorOrUnspecified(colorKey) } ?: Color.Unspecified - return retrieveTextStyle(fontKey, color = baseColor) + val resolvedStyle = retrieveTextStyle(fontKey, color = baseColor, lineHeight, bold, fontStyle, size) + return resolvedStyle.copy( + lineHeight = lineHeight.takeOrElse { resolvedStyle.fontSize * Typography.DefaultLineHeightMultiplier } + ) } @OptIn(ExperimentalTextApi::class) diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt index b46775578..584f04770 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt @@ -16,6 +16,7 @@ import org.jetbrains.jewel.bridge.theme.createBridgeComponentStyling import org.jetbrains.jewel.bridge.theme.createBridgeThemeDefinition import org.jetbrains.jewel.foundation.theme.ThemeDefinition import org.jetbrains.jewel.ui.ComponentStyling +import org.jetbrains.jewel.ui.component.copyWithSize @Service(Level.APP) internal class SwingBridgeService(scope: CoroutineScope) { @@ -55,7 +56,7 @@ internal class SwingBridgeService(scope: CoroutineScope) { internal data class BridgeThemeData(val themeDefinition: ThemeDefinition, val componentStyling: ComponentStyling) { companion object { val DEFAULT = run { - val textStyle = TextStyle.Default.copy(fontSize = 13.sp) + val textStyle = TextStyle.Default.copyWithSize(fontSize = 13.sp) val monospaceTextStyle = textStyle.copy(fontFamily = FontFamily.Monospace) val themeDefinition = createBridgeThemeDefinition( diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/Typography.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/Typography.kt index a1565742e..9938c949a 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/Typography.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/Typography.kt @@ -7,6 +7,7 @@ import com.intellij.openapi.util.SystemInfo import com.intellij.ui.NewUiValue import org.jetbrains.jewel.foundation.theme.JewelTheme import org.jetbrains.jewel.ui.component.Typography +import org.jetbrains.jewel.ui.component.copyWithSize import org.jetbrains.jewel.ui.component.minus /** @@ -29,7 +30,7 @@ public fun Typography.medium(): TextStyle = if (mediumAndSmallFontsAsRegular()) { labelTextStyle() } else { - labelTextStyle().copy(fontSize = labelTextSize() - 1.sp) + labelTextStyle().copyWithSize(fontSize = labelTextSize() - 1.sp) } /** @@ -46,7 +47,7 @@ public fun Typography.small(): TextStyle = if (mediumAndSmallFontsAsRegular()) { labelTextStyle() } else { - labelTextStyle().copy(fontSize = labelTextSize() - 2.sp) + labelTextStyle().copyWithSize(fontSize = labelTextSize() - 2.sp) } // Copied from JBFont — current as of IJP 233. diff --git a/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/theme/TextStyles.kt b/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/theme/TextStyles.kt index 2406faca3..c5560cc14 100644 --- a/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/theme/TextStyles.kt +++ b/int-ui/int-ui-standalone/src/main/kotlin/org/jetbrains/jewel/intui/standalone/theme/TextStyles.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.unit.sp import org.jetbrains.jewel.foundation.theme.JewelTheme import org.jetbrains.jewel.intui.standalone.Inter import org.jetbrains.jewel.intui.standalone.JetBrainsMono +import org.jetbrains.jewel.ui.component.Typography public fun JewelTheme.Companion.createDefaultTextStyle( color: Color = Color.Unspecified, @@ -45,7 +46,7 @@ public fun JewelTheme.Companion.createDefaultTextStyle( drawStyle: DrawStyle? = null, textAlign: TextAlign = TextAlign.Unspecified, textDirection: TextDirection = TextDirection.Unspecified, - lineHeight: TextUnit = TextUnit.Unspecified, + lineHeight: TextUnit = fontSize * Typography.DefaultLineHeightMultiplier, textIndent: TextIndent? = null, platformStyle: PlatformTextStyle? = null, lineHeightStyle: LineHeightStyle? = null, @@ -99,7 +100,7 @@ public fun JewelTheme.Companion.createDefaultTextStyle( drawStyle: DrawStyle? = null, textAlign: TextAlign = TextAlign.Unspecified, textDirection: TextDirection = TextDirection.Unspecified, - lineHeight: TextUnit = TextUnit.Unspecified, + lineHeight: TextUnit = fontSize * Typography.DefaultLineHeightMultiplier, textIndent: TextIndent? = null, platformStyle: PlatformTextStyle? = null, lineHeightStyle: LineHeightStyle? = null, @@ -153,7 +154,7 @@ public fun JewelTheme.Companion.createEditorTextStyle( drawStyle: DrawStyle? = null, textAlign: TextAlign = TextAlign.Unspecified, textDirection: TextDirection = TextDirection.Unspecified, - lineHeight: TextUnit = TextUnit.Unspecified, + lineHeight: TextUnit = fontSize * Typography.EditorLineHeightMultiplier, textIndent: TextIndent? = null, platformStyle: PlatformTextStyle? = null, lineHeightStyle: LineHeightStyle? = null, @@ -207,7 +208,7 @@ public fun JewelTheme.Companion.createEditorTextStyle( drawStyle: DrawStyle? = null, textAlign: TextAlign = TextAlign.Unspecified, textDirection: TextDirection = TextDirection.Unspecified, - lineHeight: TextUnit = TextUnit.Unspecified, + lineHeight: TextUnit = fontSize * Typography.EditorLineHeightMultiplier, textIndent: TextIndent? = null, platformStyle: PlatformTextStyle? = null, lineHeightStyle: LineHeightStyle? = null, diff --git a/samples/ide-plugin/src/main/kotlin/org/jetbrains/jewel/samples/ideplugin/SwingComparisonTabPanel.kt b/samples/ide-plugin/src/main/kotlin/org/jetbrains/jewel/samples/ideplugin/SwingComparisonTabPanel.kt index e80525d7f..87f7687b0 100644 --- a/samples/ide-plugin/src/main/kotlin/org/jetbrains/jewel/samples/ideplugin/SwingComparisonTabPanel.kt +++ b/samples/ide-plugin/src/main/kotlin/org/jetbrains/jewel/samples/ideplugin/SwingComparisonTabPanel.kt @@ -2,11 +2,13 @@ package org.jetbrains.jewel.samples.ideplugin import androidx.compose.foundation.border import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.calculateEndPadding import androidx.compose.foundation.layout.calculateStartPadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.runtime.Composable import androidx.compose.runtime.remember @@ -103,6 +105,49 @@ internal class SwingComparisonTabPanel : BorderLayoutPanel() { } } .layout(RowLayout.PARENT_GRID) + + val longText = "WordWrapInsideWordsIsSupported:" + ("NoSpace".repeat(20) + " ").repeat(5) + "End" + row("Long text (Swing)") { text(longText, maxLineLength = 100) } + row("Long text (Compose)") { + compose { + BoxWithConstraints { + Text( + longText, + modifier = + Modifier.width( + with(LocalDensity.current) { + // Guesstimate how wide this should be — we can't tell it to be + // "fill", as it crashes natively + JewelTheme.defaultTextStyle.fontSize.toDp() * 60 + } + ), + ) + } + } + } + + row("Titles (Swing)") { + text("This will wrap over a couple rows", maxLineLength = 30).component.font = JBFont.h1() + } + row("Titles (Compose)") { + compose { + BoxWithConstraints { + val style = Typography.h1TextStyle() + Text( + "This will wrap over a couple rows", + modifier = + Modifier.width( + with(LocalDensity.current) { + // Guesstimate how wide this should be — we can't tell it to be + // "fill", as it crashes natively + style.fontSize.toDp() * 10 + } + ), + style = style, + ) + } + } + } } private fun Panel.iconsRow() { diff --git a/ui/api/ui.api b/ui/api/ui.api index d08782a86..ea83d484b 100644 --- a/ui/api/ui.api +++ b/ui/api/ui.api @@ -952,6 +952,8 @@ public final class org/jetbrains/jewel/ui/component/TooltipKt { public final class org/jetbrains/jewel/ui/component/Typography { public static final field $stable I + public static final field DefaultLineHeightMultiplier F + public static final field EditorLineHeightMultiplier F public static final field INSTANCE Lorg/jetbrains/jewel/ui/component/Typography; public final fun consoleTextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle; public final fun editorTextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle; @@ -965,6 +967,10 @@ public final class org/jetbrains/jewel/ui/component/Typography { } public final class org/jetbrains/jewel/ui/component/TypographyKt { + public static final fun copyWithSize-Ce1Lr_4 (Landroidx/compose/ui/text/TextStyle;JJLandroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontSynthesis;Landroidx/compose/ui/text/font/FontFamily;Ljava/lang/String;JLandroidx/compose/ui/text/style/BaselineShift;Landroidx/compose/ui/text/style/TextGeometricTransform;Landroidx/compose/ui/text/intl/LocaleList;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/graphics/Shadow;Landroidx/compose/ui/graphics/drawscope/DrawStyle;IILandroidx/compose/ui/text/style/TextIndent;Landroidx/compose/ui/text/PlatformTextStyle;Landroidx/compose/ui/text/style/LineHeightStyle;IILandroidx/compose/ui/text/style/TextMotion;)Landroidx/compose/ui/text/TextStyle; + public static synthetic fun copyWithSize-Ce1Lr_4$default (Landroidx/compose/ui/text/TextStyle;JJLandroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontSynthesis;Landroidx/compose/ui/text/font/FontFamily;Ljava/lang/String;JLandroidx/compose/ui/text/style/BaselineShift;Landroidx/compose/ui/text/style/TextGeometricTransform;Landroidx/compose/ui/text/intl/LocaleList;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/graphics/Shadow;Landroidx/compose/ui/graphics/drawscope/DrawStyle;IILandroidx/compose/ui/text/style/TextIndent;Landroidx/compose/ui/text/PlatformTextStyle;Landroidx/compose/ui/text/style/LineHeightStyle;IILandroidx/compose/ui/text/style/TextMotion;ILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle; + public static final fun copyWithSize-nfnV_I0 (Landroidx/compose/ui/text/TextStyle;JLandroidx/compose/ui/graphics/Brush;FLandroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontSynthesis;Landroidx/compose/ui/text/font/FontFamily;Ljava/lang/String;JLandroidx/compose/ui/text/style/BaselineShift;Landroidx/compose/ui/text/style/TextGeometricTransform;Landroidx/compose/ui/text/intl/LocaleList;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/graphics/Shadow;Landroidx/compose/ui/graphics/drawscope/DrawStyle;IILandroidx/compose/ui/text/style/TextIndent;Landroidx/compose/ui/text/PlatformTextStyle;Landroidx/compose/ui/text/style/LineHeightStyle;IILandroidx/compose/ui/text/style/TextMotion;)Landroidx/compose/ui/text/TextStyle; + public static synthetic fun copyWithSize-nfnV_I0$default (Landroidx/compose/ui/text/TextStyle;JLandroidx/compose/ui/graphics/Brush;FLandroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontSynthesis;Landroidx/compose/ui/text/font/FontFamily;Ljava/lang/String;JLandroidx/compose/ui/text/style/BaselineShift;Landroidx/compose/ui/text/style/TextGeometricTransform;Landroidx/compose/ui/text/intl/LocaleList;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/graphics/Shadow;Landroidx/compose/ui/graphics/drawscope/DrawStyle;IILandroidx/compose/ui/text/style/TextIndent;Landroidx/compose/ui/text/PlatformTextStyle;Landroidx/compose/ui/text/style/LineHeightStyle;IILandroidx/compose/ui/text/style/TextMotion;ILjava/lang/Object;)Landroidx/compose/ui/text/TextStyle; public static final fun minus-NB67dxo (JJ)J public static final fun plus-NB67dxo (JJ)J } diff --git a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Typography.kt b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Typography.kt index ef420d4ee..d3a89c5d0 100644 --- a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Typography.kt +++ b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Typography.kt @@ -1,13 +1,33 @@ package org.jetbrains.jewel.ui.component import androidx.compose.runtime.Composable +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shadow +import androidx.compose.ui.graphics.drawscope.DrawStyle +import androidx.compose.ui.text.PlatformTextStyle 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.FontSynthesis import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.intl.LocaleList +import androidx.compose.ui.text.style.BaselineShift +import androidx.compose.ui.text.style.Hyphens +import androidx.compose.ui.text.style.LineBreak +import androidx.compose.ui.text.style.LineHeightStyle +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.text.style.TextDecoration +import androidx.compose.ui.text.style.TextDirection +import androidx.compose.ui.text.style.TextGeometricTransform +import androidx.compose.ui.text.style.TextIndent +import androidx.compose.ui.text.style.TextMotion import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.TextUnitType import androidx.compose.ui.unit.isUnspecified import androidx.compose.ui.unit.sp import org.jetbrains.jewel.foundation.theme.JewelTheme +import org.jetbrains.jewel.ui.component.Typography.DefaultLineHeightMultiplier /** * A quick way to obtain text styles derived from [the default `TextStyle`][JewelTheme.defaultTextStyle]. These match @@ -20,28 +40,57 @@ public object Typography { /** The text size to use for labels. Identical to the size set in [JewelTheme.defaultTextStyle]. */ @Composable public fun labelTextSize(): TextUnit = JewelTheme.defaultTextStyle.fontSize + @Suppress("ktlint:standard:property-naming", "ConstPropertyName") + /** + * The factor to use when creating a [TextStyle] with a changed font size: + * ```kotlin + * myTextStyle.copy( + * fontSize = newFontSize, + * lineHeight = newFontSize * Typography.DefaultLineHeightMultiplier, + * ) + * ``` + * + * You should use [TextStyle.copyWithSize] to create copies of a [TextStyle] with a changed font size, as that + * function will automatically apply the correct line height, too. + * + * @see TextStyle.copyWithSize + */ + public const val DefaultLineHeightMultiplier: Float = 1.3f + + @Suppress("ktlint:standard:property-naming", "ConstPropertyName") + /** + * The factor to use when creating an editor [TextStyle] with a changed font size: + * ```kotlin + * myTextStyle.copy( + * fontSize = newFontSize, + * lineHeight = newFontSize * Typography.DefaultLineHeightMultiplier, + * ) + * ``` + */ + public const val EditorLineHeightMultiplier: Float = 1.2f + /** The text style to use for h0 titles. Derived from [JewelTheme.defaultTextStyle]. */ @Composable public fun h0TextStyle(): TextStyle = - JewelTheme.defaultTextStyle.copy(fontSize = labelTextSize() + 12.sp, fontWeight = FontWeight.Bold) + JewelTheme.defaultTextStyle.copyWithSize(fontSize = labelTextSize() + 12.sp, fontWeight = FontWeight.Bold) /** The text style to use for h1 titles. Derived from [JewelTheme.defaultTextStyle]. */ @Composable public fun h1TextStyle(): TextStyle = - JewelTheme.defaultTextStyle.copy(fontSize = labelTextSize() + 9.sp, fontWeight = FontWeight.Bold) + JewelTheme.defaultTextStyle.copyWithSize(fontSize = labelTextSize() + 9.sp, fontWeight = FontWeight.Bold) /** The text style to use for h2 titles. Derived from [JewelTheme.defaultTextStyle]. */ @Composable - public fun h2TextStyle(): TextStyle = JewelTheme.defaultTextStyle.copy(fontSize = labelTextSize() + 5.sp) + public fun h2TextStyle(): TextStyle = JewelTheme.defaultTextStyle.copyWithSize(fontSize = labelTextSize() + 5.sp) /** The text style to use for h3 titles. Derived from [JewelTheme.defaultTextStyle]. */ @Composable - public fun h3TextStyle(): TextStyle = JewelTheme.defaultTextStyle.copy(fontSize = labelTextSize() + 3.sp) + public fun h3TextStyle(): TextStyle = JewelTheme.defaultTextStyle.copyWithSize(fontSize = labelTextSize() + 3.sp) /** The text style to use for h4 titles. Derived from [JewelTheme.defaultTextStyle]. */ @Composable public fun h4TextStyle(): TextStyle = - JewelTheme.defaultTextStyle.copy(fontSize = labelTextSize() + 1.sp, fontWeight = FontWeight.Bold) + JewelTheme.defaultTextStyle.copyWithSize(fontSize = labelTextSize() + 1.sp, fontWeight = FontWeight.Bold) /** The text style used for code editors. Usually is a monospaced font. */ @Composable public fun editorTextStyle(): TextStyle = JewelTheme.editorTextStyle @@ -50,6 +99,122 @@ public object Typography { @Composable public fun consoleTextStyle(): TextStyle = JewelTheme.consoleTextStyle } +/** + * Creates a copy of this [TextStyle] with a new [fontSize] and an appropriately set line height. + * + * @see Typography.DefaultLineHeightMultiplier + */ +public fun TextStyle.copyWithSize( + fontSize: TextUnit, + color: Color = this.color, + fontWeight: FontWeight? = this.fontWeight, + fontStyle: FontStyle? = this.fontStyle, + fontSynthesis: FontSynthesis? = this.fontSynthesis, + fontFamily: FontFamily? = this.fontFamily, + fontFeatureSettings: String? = this.fontFeatureSettings, + letterSpacing: TextUnit = this.letterSpacing, + baselineShift: BaselineShift? = this.baselineShift, + textGeometricTransform: TextGeometricTransform? = this.textGeometricTransform, + localeList: LocaleList? = this.localeList, + background: Color = this.background, + textDecoration: TextDecoration? = this.textDecoration, + shadow: Shadow? = this.shadow, + drawStyle: DrawStyle? = this.drawStyle, + textAlign: TextAlign = this.textAlign, + textDirection: TextDirection = this.textDirection, + textIndent: TextIndent? = this.textIndent, + platformStyle: PlatformTextStyle? = this.platformStyle, + lineHeightStyle: LineHeightStyle? = this.lineHeightStyle, + lineBreak: LineBreak = this.lineBreak, + hyphens: Hyphens = this.hyphens, + textMotion: TextMotion? = this.textMotion, +): TextStyle = + copy( + color, + fontSize, + fontWeight, + fontStyle, + fontSynthesis, + fontFamily, + fontFeatureSettings, + letterSpacing, + baselineShift, + textGeometricTransform, + localeList, + background, + textDecoration, + shadow, + drawStyle, + textAlign, + textDirection, + lineHeight = fontSize * DefaultLineHeightMultiplier, + textIndent, + platformStyle, + lineHeightStyle, + lineBreak, + hyphens, + textMotion, + ) + +/** + * Creates a copy of this [TextStyle] with a new [fontSize] and an appropriately set line height. + * + * @see Typography.DefaultLineHeightMultiplier + */ +public fun TextStyle.copyWithSize( + fontSize: TextUnit, + brush: Brush?, + alpha: Float = this.alpha, + fontWeight: FontWeight? = this.fontWeight, + fontStyle: FontStyle? = this.fontStyle, + fontSynthesis: FontSynthesis? = this.fontSynthesis, + fontFamily: FontFamily? = this.fontFamily, + fontFeatureSettings: String? = this.fontFeatureSettings, + letterSpacing: TextUnit = this.letterSpacing, + baselineShift: BaselineShift? = this.baselineShift, + textGeometricTransform: TextGeometricTransform? = this.textGeometricTransform, + localeList: LocaleList? = this.localeList, + background: Color = this.background, + textDecoration: TextDecoration? = this.textDecoration, + shadow: Shadow? = this.shadow, + drawStyle: DrawStyle? = this.drawStyle, + textAlign: TextAlign = this.textAlign, + textDirection: TextDirection = this.textDirection, + textIndent: TextIndent? = this.textIndent, + platformStyle: PlatformTextStyle? = this.platformStyle, + lineHeightStyle: LineHeightStyle? = this.lineHeightStyle, + lineBreak: LineBreak = this.lineBreak, + hyphens: Hyphens = this.hyphens, + textMotion: TextMotion? = this.textMotion, +): TextStyle = + copy( + brush, + alpha, + fontSize, + fontWeight, + fontStyle, + fontSynthesis, + fontFamily, + fontFeatureSettings, + letterSpacing, + baselineShift, + textGeometricTransform, + localeList, + background, + textDecoration, + shadow, + drawStyle, + textAlign, + textDirection, + lineHeight = fontSize * DefaultLineHeightMultiplier, + textIndent, + platformStyle, + lineHeightStyle, + lineBreak, + hyphens, + textMotion, + ) + public operator fun TextUnit.plus(other: TextUnit): TextUnit = when { isSp && other.isSp -> TextUnit(value + other.value, TextUnitType.Sp)