Skip to content

Commit a291459

Browse files
Add support and demo for the stroke-dashoffset attribute
1 parent 4b1709b commit a291459

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024 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.various
7+
8+
import androidx.compose.foundation.layout.*
9+
import androidx.compose.material.MaterialTheme
10+
import androidx.compose.ui.Modifier
11+
import androidx.compose.ui.unit.dp
12+
import androidx.compose.ui.window.Window
13+
import androidx.compose.ui.window.application
14+
import org.jetbrains.letsPlot.skia.compose.PlotPanel
15+
import plotSpec.StrokeDashSpec
16+
17+
@OptIn(ExperimentalLayoutApi::class)
18+
fun main() = application {
19+
Window(onCloseRequest = ::exitApplication, title = "StrokeDash (Compose Desktop)") {
20+
MaterialTheme {
21+
FlowRow(
22+
modifier = Modifier.fillMaxSize().padding(start = 10.dp, top = 10.dp, end = 10.dp, bottom = 10.dp),
23+
) {
24+
StrokeDashSpec().createFigureList().forEach { figure ->
25+
Column {
26+
PlotPanel(
27+
figure = figure,
28+
modifier = Modifier.size(610.dp)
29+
) { computationMessages ->
30+
computationMessages.forEach { println("[DEMO APP MESSAGE] $it") }
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2024 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 plotSpec
7+
8+
import org.jetbrains.letsPlot.Figure
9+
import org.jetbrains.letsPlot.geom.geomSegment
10+
import org.jetbrains.letsPlot.ggplot
11+
12+
class StrokeDashSpec : PlotDemoSpec {
13+
override fun createFigureList(): List<Figure> {
14+
return listOf(
15+
ggplot()
16+
+ geomSegment(x=0, y=20, xend = 100, yend=20, linetype = listOf(5, listOf(10, 5)))
17+
+ geomSegment(x=0, y=10, xend = 100, yend=10, linetype = listOf(0, listOf(10, 5)))
18+
+ geomSegment(x=0, y=0, xend = 100, yend=0),
19+
)
20+
}
21+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal abstract class SvgShapeMapping<TargetT : Figure> : SvgAttrMapping<Targe
1818
SvgShape.STROKE.name -> target.stroke = toColor(value)
1919
SvgShape.STROKE_OPACITY.name -> target.strokeOpacity = value!!.asFloat
2020
SvgShape.STROKE_WIDTH.name -> target.strokeWidth = value!!.asFloat
21+
SvgShape.STROKE_DASHOFFSET.name -> target.strokeDashOffset = value?.asFloat ?: 0f
2122
SvgConstants.SVG_STROKE_DASHARRAY_ATTRIBUTE -> {
2223
val strokeDashArray = (value as String).split(",").map(String::toFloat)
2324
target.strokeDashArray = strokeDashArray

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal abstract class Figure : Element() {
1313
var strokeWidth: Float by visualProp(1f)
1414
var strokeOpacity: Float by visualProp(1f)
1515
var strokeDashArray: List<Float>? by visualProp(null)
16+
var strokeDashOffset: Float by visualProp(0f)
1617
var strokeMiter: Float? by visualProp(null) // not mandatory, default works fine
1718

1819
var fill: Color4f? by visualProp(null)
@@ -30,6 +31,13 @@ internal abstract class Figure : Element() {
3031
Figure::strokeMiter,
3132
managed = true
3233
) {
33-
return@computedProp strokePaint(stroke, strokeWidth, strokeOpacity, strokeDashArray, strokeMiter)
34+
return@computedProp strokePaint(
35+
stroke = stroke,
36+
strokeWidth = strokeWidth,
37+
strokeOpacity = strokeOpacity,
38+
strokeDashArray = strokeDashArray,
39+
strokeDashOffset = strokeDashOffset,
40+
strokeMiter = strokeMiter
41+
)
3442
}
3543
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ fun strokePaint(
157157
strokeWidth: Float = 1f,
158158
strokeOpacity: Float = 1f,
159159
strokeDashArray: List<Float>? = null,
160-
strokeMiter: Float? = null // not mandatory, default works fine
160+
strokeDashOffset: Float = 0f, // not mandatory, default works fine
161+
strokeMiter: Float? = null
161162
) : Paint? {
162163
if (stroke == null) return null
163164
if (strokeOpacity == 0f) return null
@@ -173,7 +174,7 @@ fun strokePaint(
173174
paint.color4f = stroke.withA(strokeOpacity)
174175
paint.strokeWidth = strokeWidth
175176
strokeMiter?.let { paint.strokeMiter = it }
176-
strokeDashArray?.let { paint.pathEffect = makeDash(it.toFloatArray(), 0.0f) }
177+
strokeDashArray?.let { paint.pathEffect = makeDash(it.toFloatArray(), strokeDashOffset) }
177178
return paint
178179
}
179180

0 commit comments

Comments
 (0)