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 fd628709..3d3a6c80 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 @@ -9,7 +9,7 @@ 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 -import dev.tonholo.s2c.extensions.indented +import dev.tonholo.s2c.extensions.prependIndent import dev.tonholo.s2c.geom.AffineTransformation import dev.tonholo.s2c.geom.applyTransformations import dev.tonholo.s2c.logger.debug @@ -128,7 +128,7 @@ sealed interface ImageVectorNode : MethodSizeAccountable { val pathParamsString = if (pathParams.isNotEmpty()) { """( - |${pathParams.joinToString("\n") { (param, value) -> "$param = $value,".indented(4) }} + |${pathParams.joinToString("\n") { (param, value) -> "$param = $value,".prependIndent(4) }} |)""" } else { "" @@ -260,8 +260,8 @@ sealed interface ImageVectorNode : MethodSizeAccountable { } val value = """ |PathData { - | ${clipPathData.indented(indentSize = 4)} - |${"}".indented(indentSize)}""" + | ${clipPathData.prependIndent(indentSize = 4)} + |${"}".prependIndent(indentSize)}""" .trimMargin() add(CLIP_PATH_PARAM_NAME to value) } @@ -289,10 +289,10 @@ sealed interface ImageVectorNode : MethodSizeAccountable { val groupParamsString = if (groupParams.isNotEmpty()) { val params = groupParams.joinToString("\n") { (param, value) -> if (param == CLIP_PATH_PARAM_NAME && minified.not() && params.clipPath != null) { - "${"// ${params.clipPath.normalizedPath}".indented(4)}\n" + "${"// ${params.clipPath.normalizedPath}".prependIndent(4)}\n" } else { "" - } + "$param = $value,".indented(indentSize) + } + "$param = $value,".prependIndent(indentSize) } """( |$params @@ -470,10 +470,10 @@ private fun createNode( throw e } -private inline fun resetDotCount(current: Char): Int = +private fun resetDotCount(current: Char): Int = if (current == '.') 1 else 0 -private inline fun calculateDotCount(char: Char, dotCount: Int, lastChar: Char): Int = +private fun calculateDotCount(char: Char, dotCount: Int, lastChar: Char): Int = if (char == '.') { dotCount + 1 } else if ((lastChar.isDigit() && char.isWhitespace()) || lastChar.isWhitespace()) { diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathCommand.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathCommand.kt index 210ea3e5..de313a9e 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathCommand.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathCommand.kt @@ -32,10 +32,10 @@ enum class PathCommand( } } -inline fun String.removeSuffix(pathCommand: PathCommand): String = +fun String.removeSuffix(pathCommand: PathCommand): String = removeSuffix(pathCommand.value.toString()) -inline fun Char.toPathCommand(): PathCommand? { +fun Char.toPathCommand(): PathCommand? { return when (this) { PathCommand.ArcTo.value -> PathCommand.ArcTo PathCommand.CurveTo.value -> PathCommand.CurveTo diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathNodes.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathNodes.kt index c9ce1296..bdd1a02d 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathNodes.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/PathNodes.kt @@ -4,16 +4,7 @@ package dev.tonholo.s2c.domain -import dev.tonholo.s2c.domain.PathNodes.ArcTo -import dev.tonholo.s2c.domain.PathNodes.CurveTo -import dev.tonholo.s2c.domain.PathNodes.HorizontalLineTo -import dev.tonholo.s2c.domain.PathNodes.LineTo -import dev.tonholo.s2c.domain.PathNodes.MoveTo -import dev.tonholo.s2c.domain.PathNodes.QuadTo -import dev.tonholo.s2c.domain.PathNodes.ReflectiveCurveTo -import dev.tonholo.s2c.domain.PathNodes.ReflectiveQuadTo -import dev.tonholo.s2c.domain.PathNodes.VerticalLineTo -import dev.tonholo.s2c.extensions.indented +import dev.tonholo.s2c.extensions.prependIndent import dev.tonholo.s2c.extensions.removeTrailingZero import dev.tonholo.s2c.extensions.toInt import dev.tonholo.s2c.parser.method.MethodSizeAccountable @@ -119,7 +110,7 @@ sealed class PathNodes( val separator = if (minified || forceInline) "" else "\n" val scape = if (minified || forceInline) " " else "|" return joinToString(separator) { - "$scape${it.indented(indentSize)}," + "$scape${it.prependIndent(indentSize)}," }.let { if (minified || forceInline) it.substring(1, it.length - 1) else "\n$it\n" } diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/avg/AvgNode.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/avg/AvgNode.kt index 099cd7ca..bc75f636 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/avg/AvgNode.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/avg/AvgNode.kt @@ -30,13 +30,13 @@ class AvgRootNode( } } -inline fun AvgRootNode.asNodes( +fun AvgRootNode.asNodes( minified: Boolean, ): List = children.mapNotNull { node -> (node as? AvgNode)?.asNode(minified) } -inline fun AvgNode.asNode( +fun AvgNode.asNode( minified: Boolean, ): ImageVectorNode? = when (this) { is AvgGroupNode -> asNode(minified) diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/compose/ComposeBrush.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/compose/ComposeBrush.kt index 70388ba4..97779c63 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/compose/ComposeBrush.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/compose/ComposeBrush.kt @@ -1,6 +1,6 @@ package dev.tonholo.s2c.domain.compose -import dev.tonholo.s2c.extensions.indented +import dev.tonholo.s2c.extensions.prependIndent import dev.tonholo.s2c.parser.method.MethodSizeAccountable import kotlin.jvm.JvmInline @@ -111,18 +111,18 @@ sealed interface ComposeBrush : ComposeType, MethodSizeAccountable { appendColors(stops, colors, INDENT_SIZE) start.toCompose().let { if (it != ComposeOffset.ZERO) { - appendLine("start = $it,".indented(INDENT_SIZE)) + appendLine("start = $it,".prependIndent(INDENT_SIZE)) } } end.toCompose().let { if (it != ComposeOffset.INFINITE) { - appendLine("end = $it,".indented(INDENT_SIZE)) + appendLine("end = $it,".prependIndent(INDENT_SIZE)) } } if (tileMode != null && tileMode != GradientTileMode.Clamp) { - appendLine("tileMode = ${tileMode.toCompose()},".indented(INDENT_SIZE)) + appendLine("tileMode = ${tileMode.toCompose()},".prependIndent(INDENT_SIZE)) } - append(")".indented(INDENT_SIZE / 2)) + append(")".prependIndent(INDENT_SIZE / 2)) } } @@ -153,15 +153,15 @@ sealed interface ComposeBrush : ComposeType, MethodSizeAccountable { appendLine(".radialGradient(") appendColors(stops, colors, INDENT_SIZE) if (center != null && center != ComposeOffset.Infinite) { - appendLine("center = ${center.toCompose()},".indented(INDENT_SIZE)) + appendLine("center = ${center.toCompose()},".prependIndent(INDENT_SIZE)) } if (radius != null) { - appendLine("radius = ${radius}f,".indented(INDENT_SIZE)) + appendLine("radius = ${radius}f,".prependIndent(INDENT_SIZE)) } if (tileMode != null && tileMode != GradientTileMode.Clamp) { - appendLine("tileMode = ${tileMode.toCompose()},".indented(INDENT_SIZE)) + appendLine("tileMode = ${tileMode.toCompose()},".prependIndent(INDENT_SIZE)) } - append(")".indented(INDENT_SIZE / 2)) + append(")".prependIndent(INDENT_SIZE / 2)) } } @@ -189,9 +189,9 @@ sealed interface ComposeBrush : ComposeType, MethodSizeAccountable { appendLine(".sweepGradient(") appendColors(stops, colors, INDENT_SIZE) if (center != null) { - appendLine("center = ${center.toCompose()},".indented(INDENT_SIZE)) + appendLine("center = ${center.toCompose()},".prependIndent(INDENT_SIZE)) } - append(")".indented(INDENT_SIZE / 2)) + append(")".prependIndent(INDENT_SIZE / 2)) } } } @@ -203,15 +203,15 @@ private fun StringBuilder.appendColors( indent: Int, ) { if (stops.isNullOrEmpty()) { - appendLine("colors = listOf(".indented(indent)) + appendLine("colors = listOf(".prependIndent(indent)) colors .asSequence() // filter not valid Compose colors .mapNotNull { it.toCompose() } // add indentation - .map { "$it,".indented(indent * 2) } + .map { "$it,".prependIndent(indent * 2) } .forEach(::appendLine) - appendLine("),".indented(indent)) + appendLine("),".prependIndent(indent)) } else { stops .zip(colors) @@ -222,7 +222,7 @@ private fun StringBuilder.appendColors( } .map { (stop, color) -> // map to vararg arguments - "${stop}f to $color,".indented(indent) + "${stop}f to $color,".prependIndent(indent) } .forEach(::appendLine) } diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgElementNode.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgElementNode.kt index 2ef4179d..327c0685 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgElementNode.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgElementNode.kt @@ -40,7 +40,6 @@ abstract class SvgElementNode( when (it) { is SvgChildNode<*> -> it.copy(parent = this) is SvgElementNode<*> -> it.copy(parent = this) - else -> it } } ) diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/xml/XmlNode.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/xml/XmlNode.kt index 7e51294f..75d1d12e 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/xml/XmlNode.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/xml/XmlNode.kt @@ -36,16 +36,12 @@ abstract class XmlChildNode( private set val rootParent: XmlParentNode by lazy { - var current: XmlChildNode + var current: XmlChildNode = this var currentParent = parent - do { + while (currentParent !is XmlRootNode) { current = currentParent as XmlChildNode - currentParent = when (currentParent) { - is XmlRootNode -> break - else -> current.parent - } - } while (currentParent !is XmlRootNode) - + currentParent = current.parent + } // XmlRootNode is the Document itself and not an actual node. // Because of that, we return the direct child that we use to // access its parent to reach the XmlRootNode. diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/error/ExitProgramException.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/error/ExitProgramException.kt index 27dd46a7..5193b7f8 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/error/ExitProgramException.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/error/ExitProgramException.kt @@ -31,7 +31,7 @@ open class ExitProgramException( } } -inline fun ExitProgramException( +fun ExitProgramException( errorCode: ErrorCode, message: String, throwable: Throwable? = null, diff --git a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/extensions/String.extension.kt b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/extensions/String.extension.kt index 5ead5128..1ad0af71 100644 --- a/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/extensions/String.extension.kt +++ b/svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/extensions/String.extension.kt @@ -32,12 +32,6 @@ fun String.camelCase(): String = replaceDividers() fun String.pascalCase(): String = replaceDividers() .replaceFirstChar { it.uppercaseChar() } -@Deprecated( - message = "Use prependIndent instead", - replaceWith = ReplaceWith(expression = "this.prependIndent(indentSize)"), -) -fun String.indented(indentSize: Int) = " ".repeat(indentSize) + this - /** * Prepends an indent to the string. */ @@ -102,4 +96,4 @@ fun String.toPercentage(): Float { /** * Removes trailing zeros from a string representing a number. */ -inline fun String.removeTrailingZero(): String = replace("\\.0\\b".toRegex(), "") +fun String.removeTrailingZero(): String = replace("\\.0\\b".toRegex(), "")