Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual test #10

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 15 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.5.21"
kotlin("jvm") version "1.6.10"
id("org.jetbrains.compose") version "1.1.1"
id("maven-publish")
}

Expand All @@ -10,18 +11,29 @@ version = "0.7.0"

repositories {
mavenCentral()
google()
mavenCentral()
maven("https://jitpack.io")
maven("https://maven.google.com")
maven("https://jetbrains.bintray.com/trove4j")
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

dependencies {
implementation("com.google.guava:guava:23.0")
val composeVersion = "1.1.1"

implementation("com.google.guava:guava:31.1-jre")
implementation("com.android.tools:sdk-common:27.2.0-alpha16")
implementation("com.android.tools:common:27.2.0-alpha16")
implementation("com.squareup:kotlinpoet:1.9.0")
implementation("com.squareup:kotlinpoet:1.10.2")
implementation("org.ogce:xpp3:1.1.6")

testImplementation(kotlin("test-junit"))
implementation("org.jetbrains.compose.ui:ui:$composeVersion")
implementation("org.jetbrains.compose.runtime:runtime:$composeVersion")
implementation("org.jetbrains.compose.foundation:foundation:$composeVersion")
implementation(compose.desktop.currentOs)

}

tasks.test {
Expand Down
7 changes: 7 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
pluginManagement {
repositories {
google()
gradlePluginPortal()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

}
rootProject.name = "svg-to-compose"

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright 2020 The Android Open Source Project
*
Expand All @@ -18,13 +17,16 @@
package androidx.compose.material.icons.generator

import androidx.compose.material.icons.generator.vector.*
import br.com.devsrsouza.svg2compose.Size
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParser.END_DOCUMENT
import org.xmlpull.v1.XmlPullParser.END_TAG
import org.xmlpull.v1.XmlPullParser.START_TAG
import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory
import kotlin.math.log10


data class ScaleFactor(val x: Float = 1f, val y: Float = 1f)

/**
* Parser that converts [icon]s into [Vector]s
Expand All @@ -34,7 +36,7 @@ class IconParser(private val icon: Icon) {
/**
* @return a [Vector] representing the provided [icon].
*/
fun parse(): Vector {
fun parse(defaultSize: Size? = null): Vector {
val parser = XmlPullParserFactory.newInstance().newPullParser().apply {
setInput(icon.fileContent.byteInputStream(), null)
seekToStartTag()
Expand All @@ -47,6 +49,13 @@ class IconParser(private val icon: Icon) {
val viewportWidth = parser.getAttributeValue(null, VIEWPORT_WIDTH).toFloat()
val viewportHeight = parser.getAttributeValue(null, VIEWPORT_HEIGHT).toFloat()

val scale = defaultSize?.let { requestedSize ->
ScaleFactor(
requestedSize.width / viewportWidth,
requestedSize.height / viewportHeight
)
} ?: ScaleFactor()

parser.next()

val nodes = mutableListOf<VectorNode>()
Expand Down Expand Up @@ -98,7 +107,7 @@ class IconParser(private val icon: Icon) {
strokeLineJoin = strokeJoin ?: StrokeJoin.Miter,
strokeLineMiter = strokeMiterLimit ?: 4.0f,
fillType = fillType,
nodes = PathParser.parsePathString(pathData)
nodes = PathParser.parsePathString(pathData, scale)
)
if (currentGroup != null) {
currentGroup.paths.add(path)
Expand All @@ -115,7 +124,7 @@ class IconParser(private val icon: Icon) {
CLIP_PATH -> { /* TODO: b/147418351 - parse clipping paths */
}
GRADIENT -> {
val gradient = when (parser.getAttributeValue(null, TYPE)){
val gradient = when (parser.getAttributeValue(null, TYPE)) {
LINEAR -> {
val startX = parser.getValueAsFloat(START_X) ?: 0f
val startY = parser.getValueAsFloat(START_Y) ?: 0f
Expand All @@ -142,8 +151,8 @@ class IconParser(private val icon: Icon) {
}

val lastPath = currentGroup?.paths?.removeLast() ?: nodes.removeLast()
if (lastPath as? VectorNode.Path != null && lastPath.fill == null){
val gradientPath = lastPath.copy (fill = gradient)
if (lastPath as? VectorNode.Path != null && lastPath.fill == null) {
val gradientPath = lastPath.copy(fill = gradient)
if (currentGroup != null) {
currentGroup.paths.add(gradientPath)
} else {
Expand All @@ -155,9 +164,9 @@ class IconParser(private val icon: Icon) {
val offset = parser.getValueAsFloat(OFFSET) ?: 0f
val colorHex = parser.getAttributeValue(null, COLOR).toHexColor()

val colorStop = Pair(offset,colorHex)
val colorStop = Pair(offset, colorHex)
val lastPath = (currentGroup?.paths?.last() ?: nodes.last()) as? VectorNode.Path
when (lastPath?.fill){
when (lastPath?.fill) {
is Fill.LinearGradient -> lastPath.fill.colorStops.add(colorStop)
is Fill.RadialGradient -> lastPath.fill.colorStops.add(colorStop)
else -> {}
Expand All @@ -172,8 +181,8 @@ class IconParser(private val icon: Icon) {
return Vector(
width,
height,
viewportWidth,
viewportHeight,
viewportWidth * scale.x,
viewportHeight * scale.y,
nodes
)
}
Expand Down Expand Up @@ -205,7 +214,7 @@ private val hexRegex = "^[0-9a-fA-F]{6,8}".toRegex()
private fun String.toHexColor(): String {
return removePrefix("#")
.let {
if(hexRegex.matches(it)) {
if (hexRegex.matches(it)) {
if (it.length > 6) it
else "FF$it"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package androidx.compose.material.icons.generator

import br.com.devsrsouza.svg2compose.IconNameTransformer
import br.com.devsrsouza.svg2compose.Size
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.MemberName
import java.io.File
Expand All @@ -32,6 +32,7 @@ class IconWriter(
private val icons: Collection<Icon>,
private val groupClass: ClassName,
private val groupPackage: String,
private val defaultSize: Size?,
) {
/**
* Generates icons and writes them to [outputSrcDirectory], using [iconNamePredicate] to
Expand All @@ -55,17 +56,36 @@ class IconWriter(
}.map { icon ->
val iconName = icon.kotlinName

val vector = IconParser(icon).parse()
/**check [androidx.compose.material.icons.generator.vector.Vector]**/
val vector = IconParser(icon).parse(defaultSize).let { parsedVector ->
defaultSize?.let {
parsedVector.copy(
width = when (parsedVector.width) {
is Pixel -> Pixel(defaultSize.width)
is Dp -> Dp(defaultSize.width)
},
height = when (parsedVector.height) {
is Pixel -> Pixel(defaultSize.height)
is Dp -> Dp(defaultSize.height)
}
)
} ?: parsedVector
}

val (fileSpec, accessProperty) = VectorAssetGenerator(
iconName,
defaultSize?.let {
"$iconName${it.maxValue}"
} ?: iconName,
groupPackage,
vector
).createFileSpec(groupClass)

fileSpec.writeTo(outputSrcDirectory)

MemberName(fileSpec.packageName, accessProperty)

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class VectorAssetGenerator(
// Kotlin 1.4) each property with the same name will be considered as a possible candidate
// for resolution, regardless of the access modifier, so by using unique names we reduce
// the size from ~6000 to 1, and speed up compilation time for these icons.
@OptIn(ExperimentalStdlibApi::class)
val backingPropertyName = "_" + iconName.decapitalize(Locale.ROOT)
val backingPropertyName = "_" + iconName.replaceFirstChar { it.lowercase(Locale.ROOT) }
val backingProperty = backingPropertySpec(name = backingPropertyName, ClassNames.ImageVector)

val generation = FileSpec.builder(
Expand Down
Loading