Skip to content

Commit

Permalink
fix(svg): add default fill color when neither fillColor or stroke was…
Browse files Browse the repository at this point in the history
… specified (#45)
  • Loading branch information
rafaeltonholo authored Jun 9, 2024
1 parent 40fb876 commit 6adf206
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=1.3.0
VERSION=1.3.1
8 changes: 8 additions & 0 deletions samples/svg/smiley.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

/**
Expand Down Expand Up @@ -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()))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
Expand All @@ -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 <a href="http://www.w3.org/TR/SVG11/single-page.html#types-ColorKeywords">W3 Color Keywords</a>
Expand Down
9 changes: 9 additions & 0 deletions svg-to-compose/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6adf206

Please sign in to comment.