Yan Pujante
02/25/2021, 7:55 PM.icns
file that you can use in your dmg image on macOS (because it is NOT trivial), using a svg file as an input and google chrome for the rendering (handle transparency properly)
// render a single icon with the size
fun renderIcon(size: Int, filename: String) {
val html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body style="margin:0;overflow:hidden">
<img style="padding; margin:0" src="${rootDir}/src/main/resources/images/logo.svg" width="$size" height="$size"></img>
</body>
</html>
""".trimIndent()
val inputFile = file("$buildDir/icon.html")
inputFile.writeText(html)
println(inputFile)
exec {
commandLine(
"/Applications/Google <http://Chrome.app/Contents/MacOS/Google|Chrome.app/Contents/MacOS/Google> Chrome",
"--headless",
"--screenshot=$filename",
"--window-size=$size,$size",
"--default-background-color=0",
inputFile.toString()
)
}
}
val iconsetDir = "${buildDir}/app-icon.iconset"
// render all icons required for iconset
tasks.register("iconset") {
doLast {
// make sure the folder exists
mkdir(iconsetDir)
// generate each icon
arrayOf(512, 256, 128, 32, 16).forEach { size ->
renderIcon(size * 2, "$iconsetDir/icon_${size}x${size}@2x.png")
renderIcon(size, "$iconsetDir/icon_${size}x${size}.png")
}
}
}
// render the .icns file needed for macOS
val icnsTask = tasks.register("icns") {
doLast {
exec {
commandLine("iconutil", "-c", "icns", iconsetDir)
workingDir = buildDir
}
}
dependsOn("iconset")
}
after running the icns
task you end up with app-icon.icns
file which you can then use in your app distributionTimo Drick
02/25/2021, 10:21 PMdef resizeImageWithJava(File infile, File outfile, int size) {
def IMG_WIDTH = size
def IMG_HEIGHT = size
println "convert:" + infile.name + " size:" + size
def BufferedImage originalImage = ImageIO.read(infile)
def BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, originalImage.getType())
println " (${originalImage.width}x${originalImage.height}) -> (${resizedImage.width}x${resizedImage.height})"
Graphics2D g = resizedImage.createGraphics()
g.setComposite(AlphaComposite.Src)
g.drawImage(originalImage.getScaledInstance(IMG_WIDTH, IMG_HEIGHT, Image.SCALE_AREA_AVERAGING), 0, 0, null)
g.finalize()
ImageIO.write(resizedImage, "png", outfile)
}
Timo Drick
02/25/2021, 10:22 PMYan Pujante
02/26/2021, 5:19 PM