From 6adf2065bee1fda37900bac57179b3c4175abd71 Mon Sep 17 00:00:00 2001 From: Rafael Tonholo Date: Sun, 9 Jun 2024 16:49:11 -0300 Subject: [PATCH] fix(svg): add default fill color when neither fillColor or stroke was specified (#45) --- app.properties | 2 +- samples/svg/smiley.svg | 8 ++++++++ .../kotlin/dev/tonholo/s2c/domain/ImageVectorNode.kt | 9 +++++++++ .../kotlin/dev/tonholo/s2c/domain/svg/SvgColor.kt | 6 +++++- svg-to-compose/src/jvmMain/kotlin/Main.kt | 9 +++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 samples/svg/smiley.svg diff --git a/app.properties b/app.properties index 0743293d..f406e043 100644 --- a/app.properties +++ b/app.properties @@ -1 +1 @@ -VERSION=1.3.0 +VERSION=1.3.1 diff --git a/samples/svg/smiley.svg b/samples/svg/smiley.svg new file mode 100644 index 00000000..89d77bc5 --- /dev/null +++ b/samples/svg/smiley.svg @@ -0,0 +1,8 @@ + + + + + diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/ImageVectorNode.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/ImageVectorNode.kt index d383ebca..9add2ace 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/ImageVectorNode.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/ImageVectorNode.kt @@ -4,6 +4,8 @@ import dev.tonholo.s2c.domain.compose.ComposeBrush import dev.tonholo.s2c.domain.compose.PathFillType import dev.tonholo.s2c.domain.compose.StrokeCap import dev.tonholo.s2c.domain.compose.StrokeJoin +import dev.tonholo.s2c.domain.svg.SvgColor +import dev.tonholo.s2c.domain.svg.toBrush import dev.tonholo.s2c.error.ErrorCode import dev.tonholo.s2c.error.ExitProgramException import dev.tonholo.s2c.extensions.EMPTY @@ -100,6 +102,9 @@ sealed interface ImageVectorNode : MethodSizeAccountable { params.strokeLineJoin?.let { addAll(it.imports) } params.fill?.let { addAll(it.imports) } params.stroke?.let { addAll(it.imports) } + if (params.fill == null && params.stroke == null) { + addAll(SvgColor.Default.toBrush().imports) + } } /** @@ -167,6 +172,10 @@ sealed interface ImageVectorNode : MethodSizeAccountable { strokeLineWidth?.let { add("strokeLineWidth" to "${it}f") } + + if (params.fill == null && params.stroke == null) { + add("fill" to requireNotNull(SvgColor.Default.toBrush().toCompose())) + } } } } diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgColor.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgColor.kt index dad58ad6..c936a271 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgColor.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgColor.kt @@ -1,11 +1,14 @@ package dev.tonholo.s2c.domain.svg +import dev.tonholo.s2c.domain.compose.ComposeBrush import dev.tonholo.s2c.domain.compose.ComposeColor +import dev.tonholo.s2c.domain.compose.toBrush import kotlin.jvm.JvmInline @JvmInline value class SvgColor private constructor(val value: String) { companion object { + val Default = SvgColor("#FF000000") operator fun invoke(value: String): SvgColor = SvgColor( namedColors[value] ?: value ) @@ -15,9 +18,10 @@ value class SvgColor private constructor(val value: String) { /** * @return black color when [SvgColor] is `null`. */ -fun SvgColor?.orDefault(): SvgColor = this ?: SvgColor("black") +fun SvgColor?.orDefault(): SvgColor = this ?: SvgColor.Default fun SvgColor.toComposeColor(): ComposeColor = ComposeColor(value) +fun SvgColor.toBrush(): ComposeBrush = value.toBrush() /** * @see W3 Color Keywords diff --git a/svg-to-compose/src/jvmMain/kotlin/Main.kt b/svg-to-compose/src/jvmMain/kotlin/Main.kt index 2c38a4f0..f1454518 100644 --- a/svg-to-compose/src/jvmMain/kotlin/Main.kt +++ b/svg-to-compose/src/jvmMain/kotlin/Main.kt @@ -131,6 +131,15 @@ private sealed class SampleFile( output = "$ROOT_SAMPLE_APP_PATH/${sampleAppPackage.svg().toDirectory()}/ShieldSolid.$suffix.kt", ) + class Smiley( + sampleAppPackage: SampleAppPackage, + suffix: String, + ) : SampleFile( + sampleAppPackage = sampleAppPackage.svg(), + input = "$BASE_PATH/smiley.svg", + output = "$ROOT_SAMPLE_APP_PATH/${sampleAppPackage.svg().toDirectory()}/Smiley.$suffix.kt", + ) + class Illustration( sampleAppPackage: SampleAppPackage, suffix: String,