Skip to content

Converts SVG and Android Vector Drawable in Compose Multiplatform source code

License

Notifications You must be signed in to change notification settings

Goooler/svg-to-compose

 
 

Repository files navigation

SVG to Compose [Experimental]

Converts SVG or Android Vector Drawable to Compose code.

Why this project

On the start of the Compose for Desktop it does not support SVG files and Android Vector Drawables. This also difficulties a migration of the App to Multiplatform (Desktop and Android). Currently, it does support Android Vector Drawables in the Desktop allowing share your vectors.

Use cases

Now Compose for Desktop supports android vector drawables, this means that the first reason that the project was created does not apply anymore, this does not mean that it is not useful anymore.

Use cases:

  • Manipulate dynamic an SVG file in code, you can generate and do source code modifications.
  • Create an Icon pack similar to how Material Icons works on Compose (compose-icons is build with SVG to Compose)

How its work

The project uses Android's Svg2Vector to convert SVG to Android Drawables and uses a customized material icon code generator from the Jetpack Compose source code to generate the source code of the SVG file.

Example 1: Running with Kotlin Scripting

file: jetnews-drawables-to-compose.main.kts

@file:Repository("https://jitpack.io")
@file:Repository("https://maven.google.com")
@file:Repository("https://jetbrains.bintray.com/trove4j")

@file:DependsOn("com.github.DevSrSouza:svg-to-compose:-SNAPSHOT")
@file:DependsOn("com.google.guava:guava:23.0")
@file:DependsOn("com.android.tools:sdk-common:27.2.0-alpha16")
@file:DependsOn("com.android.tools:common:27.2.0-alpha16")
@file:DependsOn("com.squareup:kotlinpoet:1.7.2")
@file:DependsOn("org.ogce:xpp3:1.1.6")

import br.com.devsrsouza.svg2compose.Svg2Compose
import br.com.devsrsouza.svg2compose.VectorType
import java.io.File

val assetsDir = File("assets")
val srcDir = File("src/main/kotlin")

Svg2Compose.parse(
    applicationIconPackage = "assets",
    accessorName = "JetnewsAssets",
    outputSourceDirectory = srcDir,
    vectorsDirectory = assetsDir,
    type = VectorType.DRAWABLE,
    allAssetsPropertyName = "AllAssets"
)

Generating code by using executing kotlin jetnews-drawables-to-compose

Using in code: JetnewsAssets.JetnewsLogo

Svg

The only difference for SVG files is the VectorType.SVG.

Example 2: Generating a whole Icon pack

For Icon Packs, subgroups is supported: IconPack/group/icon.svg

val assetsDir = File("linea-icons")
val srcDir = File("src/main/kotlin")

Svg2Compose.parse(
    applicationIconPackage = "assets",
    accessorName = "LineaIcons",
    outputSourceDirectory = srcDir,
    vectorsDirectory = assetsDir,
    type = VectorType.SVG,
    iconNameTransformer = { name, group -> name.removePrefix(group) },
    allAssetsPropertyName = "AllIcons"
)

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

About

Converts SVG and Android Vector Drawable in Compose Multiplatform source code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%