Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,24 @@ Svg2Compose.parse(
**Using in code**: `LineaIcons.Arrows.Buhttps://github.com/overpas/svg-to-compose-intellijttonUp`

The project also generate an accessor for all yours assets, for the given example, it should be `LineaIcons.AllIcons` and it also generated for child groups `LineaIcons.Arrows.AllIcons`

### Example 3: Configuring autoMirror

`autoMirrorRule` is an optional parameter that allows you to set a rule to set the `autoMirror`
property the icons.

```kotlin
val svgDir = File("dist/icons/svg")
val outputDir = File("src/main/kotlin")

Svg2Compose.parse(
applicationIconPackage = "com.example",
accessorName = "Icons",
outputSourceDirectory = outputDir,
vectorsDirectory = svgDir,
type = VectorType.SVG,
iconNameTransformer = { name, group -> name.removePrefix(group) },
allAssetsPropertyName = "AllIcons",
autoMirrorRule = { name -> autoMirroredIconsList.contains(name.lowercase()) },
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package androidx.compose.material.icons.generator

import br.com.devsrsouza.svg2compose.AutoMirrorRule
import br.com.devsrsouza.svg2compose.IconNameTransformer
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.MemberName
Expand All @@ -32,7 +33,8 @@ class IconWriter(
private val icons: Collection<Icon>,
private val groupClass: ClassName,
private val groupPackage: String,
private val generatePreview: Boolean
private val generatePreview: Boolean,
private val autoMirrorRule: AutoMirrorRule?,
) {
/**
* Generates icons and writes them to [outputSrcDirectory], using [iconNamePredicate] to
Expand Down Expand Up @@ -62,7 +64,8 @@ class IconWriter(
iconName,
groupPackage,
vector,
generatePreview
generatePreview,
autoMirrorRule?.let { autoMirror -> autoMirror(iconName) },
).createFileSpec(groupClass)

fileSpec.writeTo(outputSrcDirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ data class VectorAssetGenerationResult(
* correct receiver object, and also for the package name of the generated file.
* @param vector the parsed vector to generate VectorAssetBuilder commands for
* @param generatePreview if true a preview for the icon will be created.
* @param autoMirrorSpec if not null, autoMirror parameter will be set with the value.
*/
class VectorAssetGenerator(
private val iconName: String,
private val iconGroupPackage: String,
private val vector: Vector,
private val generatePreview: Boolean
private val generatePreview: Boolean,
private val autoMirrorSpec: Boolean? = null
) {
/**
* @return a [FileSpec] representing a Kotlin source file containing the property for this
Expand Down Expand Up @@ -92,7 +94,10 @@ class VectorAssetGenerator(
"defaultWidth = ${width.withMemberIfNotNull}",
"defaultHeight = ${height.withMemberIfNotNull}",
"viewportWidth = ${viewportWidth}f",
"viewportHeight = ${viewportHeight}f"
"viewportHeight = ${viewportHeight}f",
autoMirrorSpec?.let { autoMirror ->
"autoMirror = $autoMirror"
}
)
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/br/com/devsrsouza/svg2compose/Svg2Compose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.io.File
import java.util.*

typealias IconNameTransformer = (iconName: String, group: String) -> String
typealias AutoMirrorRule = (iconName: String) -> Boolean

object Svg2Compose {

Expand All @@ -21,6 +22,7 @@ object Svg2Compose {
* @param accessorName will be usage to access the Vector in the code like `MyIconPack.IconName` or `MyIconPack.IconGroupDir.IconName`
* @param generatePreview it will generate long side each group a String accessor called `Group.{allAssetsPropertyName}Named: Map<String, VectorImage>`
* for parent groups, it will find child group icons by `childgroup.icon_name`.
* @param autoMirrorRule used to add the autoMirror attribute to the generated vector based on the icon name
*/
fun parse(
applicationIconPackage: String,
Expand All @@ -32,6 +34,7 @@ object Svg2Compose {
allAssetsPropertyName: String = "AllAssets",
generatePreview: Boolean = true,
generateStringAccessor: Boolean = false,
autoMirrorRule: AutoMirrorRule? = null,
): ParsingResult {
fun nameRelative(vectorFile: File) = vectorFile.relativeTo(vectorsDirectory).path

Expand Down Expand Up @@ -92,7 +95,8 @@ object Svg2Compose {
icons.values,
groupClassName,
iconsPackage,
generatePreview
generatePreview,
autoMirrorRule
)

val memberNames = writer.generateTo(outputSourceDirectory) { true }
Expand Down Expand Up @@ -174,4 +178,4 @@ private fun GeneratedGroup.asParsingResult(): ParsingResult = ParsingResult(
groupName = groupName,
generatedIconsMemberNames = generatedIconsMemberNames,
generatedGroups = childGroups.map { it.asParsingResult() },
)
)
35 changes: 35 additions & 0 deletions src/test/kotlin/AutoMirrorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package br.com.devsrsouza.svg2compose

import java.io.File
import java.util.Locale


fun main() {
val iconTest = File("dist/icons/svg")
val src = File("build/generated-icons").apply { mkdirs() }

Svg2Compose.parse(
applicationIconPackage = "com.test",
accessorName = "Icons",
outputSourceDirectory = src,
vectorsDirectory = iconTest,
type = VectorType.SVG,
iconNameTransformer = { name, group ->
name.removePrefix(group)
.toUpperCamelCase()
},
allAssetsPropertyName = "AllIcons",
generatePreview = true,
autoMirrorRule = { name ->
name.contains("forward", ignoreCase = true) ||
name.contains("backward", ignoreCase = true)
}
)
}

fun String.toUpperCamelCase(): String {
return this.split("[-_. ]".toRegex())
.joinToString("") { part ->
part.replaceFirstChar { it.titlecase(Locale.getDefault()) }
}
}