Skip to content

Commit 1fb1924

Browse files
Add support for font slant and weight in Compose Desktop
1 parent 90f9595 commit 1fb1924

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2023 JetBrains s.r.o.
3+
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
4+
*/
5+
6+
package demo.plot.minimal
7+
8+
import androidx.compose.foundation.layout.Column
9+
import androidx.compose.foundation.layout.fillMaxSize
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.material.MaterialTheme
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.unit.dp
14+
import androidx.compose.ui.window.Window
15+
import androidx.compose.ui.window.application
16+
import org.jetbrains.letsPlot.skia.compose.PlotPanelRaw
17+
import plotSpec.MarkdownSpec
18+
19+
fun main() = application {
20+
Window(onCloseRequest = ::exitApplication, title = "Markdown (Compose Desktop)") {
21+
MaterialTheme {
22+
Column(
23+
modifier = Modifier.fillMaxSize().padding(start = 10.dp, top = 10.dp, end = 10.dp, bottom = 10.dp),
24+
) {
25+
26+
PlotPanelRaw(
27+
rawSpec = MarkdownSpec().mpg().rawSpec,
28+
modifier = Modifier.fillMaxSize(),
29+
preserveAspectRatio = true,
30+
) { computationMessages ->
31+
computationMessages.forEach { println("[DEMO APP MESSAGE] $it") }
32+
}
33+
}
34+
}
35+
}
36+
}
37+

platf-skia/src/commonMain/kotlin/org/jetbrains/letsPlot/skia/mapping/svg/SvgTextElementMapper.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ internal class SvgTextElementMapper(
5656
target.fill = style.color.asSkiaColor
5757
target.fontFamily = style.family.split(",").map { it.trim(' ', '"') }
5858
target.fontSize = style.size.toFloat()
59-
target.fontStyle = toFontStyle(style.face)
59+
60+
val fontStyle = toFontStyle(style.face)
61+
target.fontSlant = fontStyle.slant
62+
target.fontWeight = fontStyle.weight
6063

6164
myTextAttrSupport.setAttribute(SvgConstants.SVG_STYLE_ATTRIBUTE, "fill:${style.color.toHexColor()};")
6265
}
@@ -133,7 +136,9 @@ internal class SvgTextElementMapper(
133136
tspan.fontFamily = it
134137
}
135138

136-
tspan.fontStyle = toFontStyle(classStyle.face)
139+
val fontStyle = toFontStyle(classStyle.face)
140+
tspan.fontSlant = fontStyle.slant
141+
tspan.fontWeight = fontStyle.weight
137142
}
138143

139144
return@map tspan

platf-skia/src/commonMain/kotlin/org/jetbrains/letsPlot/skia/mapping/svg/attr/SvgTSpanElementAttrMapping.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import org.jetbrains.letsPlot.skia.mapping.svg.SvgUtils.toColor
1111
import org.jetbrains.letsPlot.skia.shape.TSpan
1212
import org.jetbrains.letsPlot.skia.shape.Text.BaselineShift
1313
import org.jetbrains.letsPlot.skia.shape.Text.Companion.DEFAULT_FONT_FAMILY
14+
import org.jetbrains.skia.FontSlant
15+
import org.jetbrains.skia.FontWeight
1416

1517
internal object SvgTSpanElementAttrMapping : SvgShapeMapping<TSpan>() {
1618
override fun setAttribute(target: TSpan, name: String, value: Any?) {
@@ -36,6 +38,16 @@ internal object SvgTSpanElementAttrMapping : SvgShapeMapping<TSpan>() {
3638
else -> error("Unexpected baseline-shift value: $value")
3739
}
3840

41+
"font-style" -> target.fontSlant = when(value) {
42+
"italic" -> FontSlant.ITALIC
43+
else -> FontSlant.UPRIGHT // normal and others
44+
}
45+
46+
"font-weight" -> target.fontWeight = when(value) {
47+
"bold" -> FontWeight.BOLD
48+
else -> FontWeight.NORMAL // normal and others
49+
}
50+
3951
"dy" -> {
4052
require(value is String) { "dy: only string value is supported" }
4153
target.dy = when {

platf-skia/src/commonMain/kotlin/org/jetbrains/letsPlot/skia/mapping/svg/attr/SvgTextElementAttrMapping.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@ import org.jetbrains.letsPlot.skia.shape.Text.Companion.DEFAULT_FONT_FAMILY
1717
import org.jetbrains.letsPlot.skia.shape.Text.Companion.DEFAULT_FONT_SIZE
1818
import org.jetbrains.letsPlot.skia.shape.Text.HorizontalAlignment
1919
import org.jetbrains.letsPlot.skia.shape.Text.VerticalAlignment
20+
import org.jetbrains.skia.FontSlant
21+
import org.jetbrains.skia.FontWeight
2022

2123
internal object SvgTextElementAttrMapping : SvgAttrMapping<Text>() {
2224
override fun setAttribute(target: Text, name: String, value: Any?) {
2325
when (name) {
2426
"font-size" -> target.fontSize = value?.asPxSize ?: DEFAULT_FONT_SIZE
2527
"font-family" -> target.fontFamily = value?.asFontFamily ?: DEFAULT_FONT_FAMILY
28+
29+
"font-style" -> target.fontSlant = when(value) {
30+
"italic" -> FontSlant.ITALIC
31+
else -> FontSlant.UPRIGHT // normal and others
32+
}
33+
34+
"font-weight" -> target.fontWeight = when(value) {
35+
"bold" -> FontWeight.BOLD
36+
else -> FontWeight.NORMAL // normal and others
37+
}
38+
2639
SvgTextElement.X.name -> target.x = value?.asFloat ?: 0.0f
2740
SvgTextElement.Y.name -> target.y = value?.asFloat ?: 0.0f
2841
SvgTextContent.TEXT_ANCHOR.name -> {

platf-skia/src/commonMain/kotlin/org/jetbrains/letsPlot/skia/shape/TSpan.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ internal class TSpan(
2323
var fontScale: Float by visualProp(1f)
2424

2525
var fontFamily: List<String> by visualProp(emptyList())
26-
var fontStyle: FontStyle by visualProp(FontStyle.NORMAL)
26+
var fontSlant: FontSlant by visualProp(FontSlant.UPRIGHT)
27+
var fontWeight: Int by visualProp(FontWeight.NORMAL)
2728
var fontSize by visualProp(Text.DEFAULT_FONT_SIZE)
2829

2930
var layoutX: Float by visualProp(0.0f)
3031
var layoutY: Float by visualProp(0.0f)
3132

33+
private val fontStyle: FontStyle by computedProp(TSpan::fontSlant, TSpan::fontWeight) {
34+
FontStyle(fontWeight, FontWidth.NORMAL, fontSlant)
35+
}
36+
3237
private val typeface by computedProp(TSpan::fontFamily, TSpan::fontStyle) {
3338
fontManager.matchFamiliesStyle(fontFamily, fontStyle)
3439
}

platf-skia/src/commonMain/kotlin/org/jetbrains/letsPlot/skia/shape/Text.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ package org.jetbrains.letsPlot.skia.shape
77

88
import org.jetbrains.letsPlot.commons.intern.observable.collections.CollectionItemEvent
99
import org.jetbrains.letsPlot.skia.mapping.svg.FontManager
10-
import org.jetbrains.skia.Canvas
11-
import org.jetbrains.skia.Color
12-
import org.jetbrains.skia.Color4f
13-
import org.jetbrains.skia.FontStyle
10+
import org.jetbrains.skia.*
1411
import kotlin.reflect.KProperty
1512

1613
internal class Text(
@@ -31,11 +28,16 @@ internal class Text(
3128
var fillOpacity: Float by visualProp(1f)
3229

3330
var fontFamily: List<String> by visualProp(emptyList())
34-
var fontStyle: FontStyle by visualProp(FontStyle.NORMAL)
31+
var fontSlant: FontSlant by visualProp(FontSlant.UPRIGHT)
32+
var fontWeight: Int by visualProp(FontWeight.NORMAL)
3533
var fontSize by visualProp(DEFAULT_FONT_SIZE)
3634

3735
private var needLayout = true
3836

37+
private val fontStyle by computedProp(Text::fontSlant, Text::fontWeight) {
38+
FontStyle(fontWeight, FontWidth.NORMAL, fontSlant)
39+
}
40+
3941
private val typeface by computedProp(Text::fontFamily, Text::fontStyle) {
4042
fontManager.matchFamiliesStyle(fontFamily, fontStyle)
4143
}
@@ -112,7 +114,8 @@ internal class Text(
112114
Text::stroke -> el.inheritValue(TSpan::stroke, stroke)
113115
Text::strokeDashArray -> el.inheritValue(TSpan::strokeDashArray, strokeDashArray)
114116
Text::fontFamily -> el.inheritValue(TSpan::fontFamily, fontFamily)
115-
Text::fontStyle -> el.inheritValue(TSpan::fontStyle, fontStyle)
117+
Text::fontSlant -> el.inheritValue(TSpan::fontSlant, fontSlant)
118+
Text::fontWeight -> el.inheritValue(TSpan::fontWeight, fontWeight)
116119
Text::fontSize -> el.inheritValue(TSpan::fontSize, fontSize)
117120
Text::strokeWidth -> el.inheritValue(TSpan::strokeWidth, strokeWidth)
118121
Text::strokeOpacity -> el.inheritValue(TSpan::strokeOpacity, strokeOpacity)
@@ -128,7 +131,8 @@ internal class Text(
128131
el.inheritValue(TSpan::strokeOpacity, strokeOpacity)
129132
el.inheritValue(TSpan::strokeDashArray, strokeDashArray)
130133
el.inheritValue(TSpan::fontFamily, fontFamily)
131-
el.inheritValue(TSpan::fontStyle, fontStyle)
134+
el.inheritValue(TSpan::fontSlant, fontSlant)
135+
el.inheritValue(TSpan::fontWeight, fontWeight)
132136
el.inheritValue(TSpan::fontSize, fontSize)
133137

134138
invalidateLayout()

0 commit comments

Comments
 (0)