Skip to content

Commit a12f032

Browse files
committed
refactor: Fix inherited stroke path fill fallback
1 parent a589e31 commit a12f032

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

  • components/parser/kmp/svg/src

components/parser/kmp/svg/src/commonMain/kotlin/io/github/composegears/valkyrie/parser/kmp/svg/SVGParser.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ object SVGParser {
6868
context(paintContext: PaintContext)
6969
private fun SVG.Path.toVectorPath(): IrVectorNode.IrPath {
7070
val resolvedFill = fill ?: paintContext.fill
71-
val resolvedStrokeColor = strokeColor ?: paintContext.strokeColor
7271
var fillColor: IrColor? = resolvedFill?.let(SvgColorParser::parse)
7372
// NOTE: Only when fill and strokeColor is null use black FillColor as default color as
7473
// fill can be none resulting to null.
75-
fillColor = if (resolvedFill == null && resolvedStrokeColor == null) Black else fillColor
74+
fillColor = if (resolvedFill == null && strokeColor == null) Black else fillColor
7675
val stroke = getSVGStrokeWithDefaults()
7776
return IrVectorNode.IrPath(
7877
name = id.orEmpty(),

components/parser/kmp/svg/src/commonTest/kotlin/io/github/composegears/valkyrie/parser/kmp/svg/SVGParserTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@ internal class SVGParserTest {
9999
)
100100
}
101101

102+
@Test
103+
fun parse_path_with_inherited_root_stroke_defaults_to_black_fill() {
104+
val svg = svg(fill = null, stroke = "#ff0000") {
105+
"""<path d="M4 4h16"/>"""
106+
}
107+
108+
assertEquals(
109+
actual = SVGParser.parse(svg).nodes,
110+
expected = listOf(
111+
IrVectorNode.IrPath(
112+
pathFillType = IrPathFillType.NonZero,
113+
fill = IrFill.Color(IrColor(0xFF000000)),
114+
paths = listOf(
115+
IrPathNode.MoveTo(4f, 4f),
116+
IrPathNode.RelativeHorizontalTo(16f),
117+
),
118+
fillAlpha = 1f,
119+
stroke = IrStroke.Color(IrColor(0xFFFF0000)),
120+
strokeAlpha = 1f,
121+
strokeLineWidth = 0f,
122+
strokeLineCap = IrStrokeLineCap.Butt,
123+
strokeLineJoin = IrStrokeLineJoin.Miter,
124+
strokeLineMiter = 4f,
125+
),
126+
),
127+
)
128+
}
129+
102130
@Test
103131
fun parse_path_with_inherited_group_fill() {
104132
val svg = svg(fill = "none") {

components/parser/kmp/svg/src/commonTest/kotlin/io/github/composegears/valkyrie/parser/kmp/svg/Utils.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ internal fun testSVG(
44
height: String = "24px",
55
viewBox: String = "0 0 24 24",
66
width: String = "24px",
7-
fill: String = "#000000",
7+
fill: String? = "#000000",
88
children: List<SVG.Child>,
99
) = SVG(
1010
width = width,
@@ -18,7 +18,7 @@ internal inline fun svg(
1818
width: String = "24px",
1919
height: String = "24px",
2020
viewBox: String? = "0 0 24 24",
21-
fill: String = "#000000",
21+
fill: String? = "#000000",
2222
stroke: String? = null,
2323
strokeWidth: String? = null,
2424
strokeLineCap: String? = null,
@@ -35,7 +35,11 @@ internal inline fun svg(
3535
if (strokeWidth != null) appendLine("""stroke-width="$strokeWidth"""")
3636
if (strokeLineCap != null) appendLine("""stroke-linecap="$strokeLineCap"""")
3737
if (strokeLineJoin != null) appendLine("""stroke-linejoin="$strokeLineJoin"""")
38-
appendLine("""fill="$fill">""")
38+
if (fill != null) {
39+
appendLine("""fill="$fill">""")
40+
} else {
41+
appendLine(">")
42+
}
3943
appendLine(block())
4044
appendLine("</svg>")
4145
}

0 commit comments

Comments
 (0)