File tree Expand file tree Collapse file tree
components/parser/kmp/svg/src
commonMain/kotlin/io/github/composegears/valkyrie/parser/kmp/svg
commonTest/kotlin/io/github/composegears/valkyrie/parser/kmp/svg Expand file tree Collapse file tree Original file line number Diff line number Diff 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(),
Original file line number Diff line number Diff 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" ) {
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments