Skip to content

Commit

Permalink
Add vector graphic support for images
Browse files Browse the repository at this point in the history
  • Loading branch information
warnyul committed Apr 18, 2023
1 parent 4ed22eb commit 2de386a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 20 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
kotlinVersion = "1.8.10"
androidGradleVersion = "7.4.2"
androidSdkCommonVersion = "30.4.2"

# kotlinx
kotlinxSerializationVersion = "1.5.0"
Expand Down Expand Up @@ -85,6 +86,7 @@ kotlinTestAnnotations = { module = "org.jetbrains.kotlin:kotlin-test-annotations
# gradle
kotlinGradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlinVersion" }
androidGradlePlugin = { module = "com.android.tools.build:gradle", version.ref = "androidGradleVersion" }
androidSdkCommon = { module = "com.android.tools:sdk-common", version.ref = "androidSdkCommonVersion" }
kotlinCompilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlinVersion" }
detektGradlePlugin = { module = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", version.ref = "detektVersion" }
mokoMultiplatformPlugin = { module = "dev.icerock:mobile-multiplatform", version.ref = "mokoMultiplatformPluginVersion" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ publishing {
name.set("Nagy Robert")
email.set("[email protected]")
}
developer {
id.set("warnyul")
name.set("Balázs Varga")
email.set("[email protected]")
}
}

scm {
Expand Down
1 change: 1 addition & 0 deletions resources-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
compileOnly(libs.kotlinGradlePlugin)
compileOnly(libs.androidGradlePlugin)
compileOnly(libs.kotlinCompilerEmbeddable)
implementation(libs.androidSdkCommon)
implementation(libs.kotlinPoet)
implementation(libs.kotlinxSerialization)
implementation(libs.apacheCommonsText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ abstract class ImagesGenerator(
objectBuilder: TypeSpec.Builder
): TypeSpec {
val fileMap = inputFileTree.groupBy { file ->
"${file.name.substringBefore("@")}.${file.extension}"
// SVGs do not have scale suffixes, so we need to remove the extension first
"${file.name.substringBefore(".").substringBefore("@")}.${file.extension}"
}

beforeGenerateResources(objectBuilder, fileMap.keys.sorted())
Expand Down Expand Up @@ -93,7 +94,7 @@ abstract class ImagesGenerator(
private val mrSettings: MRGenerator.MRSettings
) : ResourceGeneratorFeature<ImagesGenerator> {
private val stringsFileTree = info.commonResources.matching {
it.include("MR/images/**/*.png", "MR/images/**/*.jpg")
it.include("MR/images/**/*.png", "MR/images/**/*.jpg", "MR/images/**/*.svg")
}

override fun createCommonGenerator() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import com.squareup.kotlinpoet.KModifier
import dev.icerock.gradle.generator.ImagesGenerator
import dev.icerock.gradle.generator.NOPObjectBodyExtendable
import dev.icerock.gradle.generator.ObjectBodyExtendable
import dev.icerock.gradle.utils.svg
import org.gradle.api.file.FileTree
import java.io.File
import com.android.ide.common.vectordrawable.Svg2Vector
import java.io.FileOutputStream
import java.io.IOException

class AndroidImagesGenerator(
inputFileTree: FileTree,
Expand All @@ -38,22 +42,50 @@ class AndroidImagesGenerator(
files.map { key to it }
}.forEach { (key, file) ->
val scale = file.nameWithoutExtension.substringAfter("@").substringBefore("x")
val drawableDirName = "drawable-" + when (scale) {
"0.75" -> "ldpi"
"1" -> "mdpi"
"1.5" -> "hdpi"
"2" -> "xhdpi"
"3" -> "xxhdpi"
"4" -> "xxxhdpi"
val drawableDirName = "drawable" + when (scale) {
"0.75" -> "-ldpi"
"1" -> "-mdpi"
"1.5" -> "-hdpi"
"2" -> "-xhdpi"
"3" -> "-xxhdpi"
"4" -> "-xxxhdpi"
else -> {
println("ignore $file - unknown scale ($scale)")
return@forEach
if (file.svg) {
""
} else {
println("ignore $file - unknown scale ($scale)")
return@forEach
}
}
}

val drawableDir = File(resourcesGenerationDir, drawableDirName)
val processedKey = processKey(key)
file.copyTo(File(drawableDir, "$processedKey.${file.extension}"))

val resourceExtension = if (file.svg) "xml" else file.extension
val resourceFile = File(drawableDir, "$processedKey.$resourceExtension")
if (file.svg) {
parseSvgToVectorDrawable(file, resourceFile)
} else {
file.copyTo(resourceFile)
}
}
}

private fun parseSvgToVectorDrawable(svgFile: File, vectorDrawableFile: File) {
try {
vectorDrawableFile.parentFile.mkdirs()
vectorDrawableFile.createNewFile()
val os = FileOutputStream(vectorDrawableFile, false)
try {
Svg2Vector.parseSvgToXml(svgFile, os)
.takeIf { it.isNotEmpty() }
?.let { error -> println("parse from $svgFile to xml error: $error") }
} finally {
os.flush()
}
} catch (e: IOException) {
println("parse from $svgFile to xml error: $e")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.squareup.kotlinpoet.KModifier
import dev.icerock.gradle.generator.ImagesGenerator
import dev.icerock.gradle.generator.ObjectBodyExtendable
import dev.icerock.gradle.generator.apple.AppleMRGenerator.Companion.ASSETS_DIR_NAME
import dev.icerock.gradle.utils.svg
import org.gradle.api.file.FileTree
import java.io.File

Expand Down Expand Up @@ -40,7 +41,7 @@ class AppleImagesGenerator(
val contentsFile = File(assetDir, "Contents.json")

val validFiles = files.filter { file ->
VALID_SIZES.any { size -> file.scale == "${size}x" }
file.svg || VALID_SIZES.any { size -> file.scale == "${size}x" }
}

val uniqueNames = files.map { it.nameWithoutScale }.distinct()
Expand All @@ -60,16 +61,23 @@ class AppleImagesGenerator(
val imagesContent = validFiles.joinToString(separator = ",\n") { file ->
val scale = file.scale

// language=js
val scaleString = if (file.svg) "" else ",\n\t\"scale\" : \"$scale\""

// language=js
return@joinToString """
{
"idiom" : "universal",
"filename" : "${file.name}",
"scale" : "$scale"
"filename" : "${file.name}"$scaleString
}
""".trimIndent()
}

val svgProperties = if (validFiles.any { file -> file.svg }) {
// language=js
",\n\t\"properties\" : {\t\t\"preserves-vector-representation\" : true\n\t}"
} else ""

// language=js
val content = """
{
Expand All @@ -79,7 +87,7 @@ class AppleImagesGenerator(
"info" : {
"version" : 1,
"author" : "xcode"
}
}$svgProperties
}
""".trimIndent()

Expand All @@ -90,9 +98,11 @@ class AppleImagesGenerator(
private companion object {
val VALID_SIZES: IntRange = 1..3

private val File.scale: String get() =
nameWithoutExtension.substringAfter("@")
private val File.nameWithoutScale: String get() =
nameWithoutExtension.substringBefore("@")
private val File.scale: String
get() =
nameWithoutExtension.substringAfter("@")
private val File.nameWithoutScale: String
get() =
nameWithoutExtension.substringBefore("@")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.gradle.utils

import java.io.File

internal val File.svg: Boolean get() =
extension == "svg"

0 comments on commit 2de386a

Please sign in to comment.