@@ -39,10 +39,23 @@ object SVGParser {
3939 defaultHeight = height ? : DEFAULT_HEIGHT ,
4040 viewportWidth = rect?.width ? : width ? : DEFAULT_WIDTH ,
4141 viewportHeight = rect?.height ? : height ? : DEFAULT_HEIGHT ,
42- nodes = children.mapNotNull { it.toIrVectorNode() },
42+ nodes = with (toPaintContext()) {
43+ children.mapNotNull { it.toIrVectorNode() }
44+ },
4345 )
4446 }
4547
48+ private fun SVG.toPaintContext (): PaintContext = PaintContext (
49+ fill = fill,
50+ strokeColor = strokeColor,
51+ strokeWidth = strokeWidth,
52+ strokeLineCap = strokeLineCap,
53+ strokeLineJoin = strokeLineJoin,
54+ strokeAlpha = strokeAlpha,
55+ strokeMiter = strokeMiter,
56+ )
57+
58+ context(paintContext: PaintContext )
4659 private fun SVG.Child.toIrVectorNode (): IrVectorNode ? = when (this ) {
4760 is SVG .Path -> toVectorPath()
4861 is SVG .Circle -> toVectorPath()
@@ -52,11 +65,14 @@ object SVGParser {
5265 is SVG .Ellipse -> toVectorPath()
5366 }
5467
68+ context(paintContext: PaintContext )
5569 private fun SVG.Path.toVectorPath (): IrVectorNode .IrPath {
56- var fillColor: IrColor ? = fill?.let (SvgColorParser ::parse)
70+ val resolvedFill = fill ? : paintContext.fill
71+ val resolvedStrokeColor = strokeColor ? : paintContext.strokeColor
72+ var fillColor: IrColor ? = resolvedFill?.let (SvgColorParser ::parse)
5773 // NOTE: Only when fill and strokeColor is null use black FillColor as default color as
5874 // fill can be none resulting to null.
59- fillColor = if (fill == null && strokeColor == null ) Black else fillColor
75+ fillColor = if (resolvedFill == null && resolvedStrokeColor == null ) Black else fillColor
6076 val stroke = getSVGStrokeWithDefaults()
6177 return IrVectorNode .IrPath (
6278 name = id.orEmpty(),
@@ -73,15 +89,16 @@ object SVGParser {
7389 )
7490 }
7591
92+ context(paintContext: PaintContext )
7693 private fun SVG.Circle.toVectorPath (): IrVectorNode .IrPath {
7794 val cx = centerX.toFloat()
7895 val cy = centerY.toFloat()
7996 val r = radius.toFloat()
80- val color = fill?. let ( SvgColorParser ::parse) ? : Black
97+ val fill = resolveFill(fill)
8198 val stroke = getSVGStrokeWithDefaults()
8299 return IrVectorNode .IrPath (
83100 name = id.orEmpty(),
84- fill = IrFill . Color (color) ,
101+ fill = fill ,
85102 fillAlpha = fillAlpha?.toFloat() ? : 1f ,
86103 stroke = stroke.color?.let { IrStroke .Color (it) },
87104 strokeAlpha = stroke.alpha,
@@ -115,11 +132,12 @@ object SVGParser {
115132 )
116133 }
117134
135+ context(paintContext: PaintContext )
118136 private fun SVG.Polygon.toVectorPath (): IrVectorNode .IrPath {
119137 val stroke = getSVGStrokeWithDefaults()
120138 return IrVectorNode .IrPath (
121139 name = id.orEmpty(),
122- fill = if (fill != null ) SvgColorParser .parse(fill)?. let { IrFill . Color (it) } else IrFill . Color ( Black ),
140+ fill = resolveFill (fill),
123141 fillAlpha = fillAlpha?.toFloat() ? : 1f ,
124142 stroke = stroke.color?.let { IrStroke .Color (it) },
125143 strokeAlpha = stroke.alpha,
@@ -144,13 +162,25 @@ object SVGParser {
144162 )
145163 }
146164
165+ context(paintContext: PaintContext )
147166 private fun SVG.Group.toVectorGroup (): IrVectorNode .IrGroup {
167+ val groupContext = PaintContext (
168+ fill = fill ? : paintContext.fill,
169+ strokeColor = strokeColor ? : paintContext.strokeColor,
170+ strokeWidth = strokeWidth ? : paintContext.strokeWidth,
171+ strokeLineCap = strokeLineCap ? : paintContext.strokeLineCap,
172+ strokeLineJoin = strokeLineJoin ? : paintContext.strokeLineJoin,
173+ strokeAlpha = strokeAlpha ? : paintContext.strokeAlpha,
174+ strokeMiter = strokeMiter ? : paintContext.strokeMiter,
175+ )
148176 val pivot = transform?.getPivot() ? : Translation .Default
149177 val translation = transform?.getTranslation() ? : Translation .Default
150178 val scale = transform?.getScale() ? : Scale .Default
151179 return IrVectorNode .IrGroup (
152180 name = id.orEmpty(),
153- nodes = children.mapNotNull { it.toIrVectorNode() }.toMutableList(),
181+ nodes = with (groupContext) {
182+ children.mapNotNull { it.toIrVectorNode() }.toMutableList()
183+ },
154184 rotate = transform?.getRotation() ? : 0f ,
155185 pivotX = pivot.x,
156186 pivotY = pivot.y,
@@ -163,6 +193,7 @@ object SVGParser {
163193 )
164194 }
165195
196+ context(paintContext: PaintContext )
166197 private fun SVG.Rectangle.toVectorPath (): IrVectorNode .IrPath ? {
167198 val x = x.toFloat()
168199 val y = y.toFloat()
@@ -174,7 +205,7 @@ object SVGParser {
174205 return IrVectorNode .IrPath (
175206 name = id.orEmpty(),
176207 pathFillType = IrPathFillType .NonZero ,
177- fill = if (fill != null ) SvgColorParser .parse(fill)?. let { IrFill . Color (it) } else IrFill . Color ( Black ),
208+ fill = resolveFill (fill),
178209 fillAlpha = 1f ,
179210 stroke = stroke.color?.let { IrStroke .Color (it) },
180211 strokeAlpha = stroke.alpha,
@@ -192,6 +223,7 @@ object SVGParser {
192223 )
193224 }
194225
226+ context(paintContext: PaintContext )
195227 private fun SVG.Ellipse.toVectorPath (): IrVectorNode .IrPath {
196228 val cx = centerX.toFloat()
197229 val cy = centerY.toFloat()
@@ -203,7 +235,7 @@ object SVGParser {
203235 return IrVectorNode .IrPath (
204236 name = id.orEmpty(),
205237 pathFillType = IrPathFillType .NonZero ,
206- fill = if (fill != null ) SvgColorParser .parse(fill)?. let { IrFill . Color (it) } else IrFill . Color ( Black ),
238+ fill = resolveFill (fill),
207239 fillAlpha = 1f ,
208240 stroke = stroke.color?.let { IrStroke .Color (it) },
209241 strokeAlpha = stroke.alpha,
@@ -261,6 +293,26 @@ object SVGParser {
261293 return functionStart.substring(1 until endIndex).split(" " , " ," ).map { it.toFloat() }
262294 }
263295
296+ context(paintContext: PaintContext )
297+ private fun resolveFill (fill : String? ): IrFill .Color ? {
298+ val resolvedFill = fill ? : paintContext.fill
299+ val color = when (resolvedFill) {
300+ null -> Black
301+ else -> SvgColorParser .parse(resolvedFill)
302+ }
303+ return color?.let { IrFill .Color (it) }
304+ }
305+
264306 @Suppress(" PrivatePropertyName" )
265307 private val Black : IrColor = IrColor (0xff000000 )
266308}
309+
310+ internal data class PaintContext (
311+ val fill : String? ,
312+ val strokeColor : String? ,
313+ val strokeWidth : String? ,
314+ val strokeLineCap : String? ,
315+ val strokeLineJoin : String? ,
316+ val strokeAlpha : String? ,
317+ val strokeMiter : String? ,
318+ )
0 commit comments