Kotlin / kotlin-jupyter

Kotlin kernel for Jupyter/IPython
Apache License 2.0
1.11k stars 107 forks source link

Add Skija as a supported library #272

Closed ebraminio closed 3 years ago

ebraminio commented 3 years ago
@file:DependsOn("org.jetbrains.kotlinx:kotlin-jupyter-lib-ext:0.10.0-40")
@file:Repository("https://packages.jetbrains.team/maven/p/skija/maven")
@file:DependsOn("org.jetbrains.skija:skija-macos-x64:0.92.0")

// Based on https://github.com/JetBrains/skija/blob/master/examples/bitmap/src/RenderToBitmap.java
import org.jetbrains.skija.*
import javax.imageio.ImageIO
import java.io.ByteArrayInputStream
import org.jetbrains.kotlinx.jupyter.ext.Image

val surface = Surface.makeRasterN32Premul(640, 360)
val canvas = surface.getCanvas()
canvas.clear(0xFFFFFFFF.toInt())
canvas.drawTriangles(
    arrayOf(Point(320f, 70f), Point(194f, 287f), Point(446f, 287f)),
    listOf(0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00).map { it.toInt() }.toIntArray(),
    Paint()
)
Image(ImageIO.read(ByteArrayInputStream(surface.makeImageSnapshot().encodeToData()?.getBytes())))

skija demo

This looks just great as is but maybe there can be tweaks you can add before having Skija listed in readme like providing some shorthand for image display or not.

tbh I wanted to file this so I won't lose this code and get some feedbacks so feel free to close this with no action as the situation looks just great as is. Thanks!

ebraminio commented 3 years ago

So let's close this as no action was required from the beginning except a possibility to add to the list of supported which will be up to maintainers. Thanks! :)