ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - nwillc/ksvg: A Multiplatform Kotlin SVG image DSL.
A Multiplatform Kotlin SVG image DSL. Contribute to nwillc/ksvg development by creating an account on GitHub.
Visit Site

GitHub - nwillc/ksvg: A Multiplatform Kotlin SVG image DSL.

GitHub - nwillc/ksvg: A Multiplatform Kotlin SVG image DSL.

Coverage license Travis Download

Kotlin Multiplatform SVG DSL

I had a Kotlin microservice that needed to produce a simple heatmap chart on an HTML page to a large audience very rapidly. This was an ad hoc DevOps requirement, so I didn't want to take on an involved Javascript solution. Instead, I went with spitting out a half page of inline SVG text generated by the server itself. This DSL made the server code trivial and has easily met the performance needs.

Installation

The library can be obtained from JitPack.io.

In build.gradle.kts:

repositories {
    mavenCentral()
    maven("https://jitpack.io")
}
sourceSets {
    commonMain {
        dependencies {
            implementation("com.github.nwillc.ksvg:ksvg:master-SNAPSHOT")
        }
    }

    val jvmMain by getting {
        dependencies {
            // No dependency required!
        }
    }

    val jsMain by getting  {
        dependencies {
            // No dependency required!
        }
    }
}

Example


fun codeMonkey() {
    val svg = SVG.svg(true) {
         height = "300"
         width = "300"
         style {
             body = """

                 svg .black-stroke { stroke: black; stroke-width: 2; }
                 svg .fur-color { fill: white; }

             """.trimIndent()
         }
         // Label
         text {
             x = "40"
             y = "50"
             body = "#CODE"
             fontFamily = "monospace"
             fontSize = "40px"
         }
         // Ears - example using a function because USE tag doesn't work in Safari
         ear(100, 100)
         ear(240, 70)
         // Face
         circle {
             cssClass = "black-stroke"
             id = "face"
             cx = "180"
             cy = "140"
             r = "80"
             fill = "#aa450f"
         }
         // Eyes
         circle {
             cssClass = "black-stroke fur-color"
             id = "eye"
             cx = "160"
             cy = "95"
             r = "20"
         }
         use {
             x = "45"
             y = "-5"
             href = "#eye"
         }
         // Muzzle
         circle {
             cssClass = "black-stroke fur-color"
             cx = "195"
             cy = "178"
             r = "65"
         }
         // Nostrils
         circle {
             id = "nostril"
             cx = "178"
             cy = "138"
             r = "4"
             fill = "black"
         }
         use {
             x = "35"
             y = "-5"
             href = "#nostril"
         }
         // Mouth
         path {
             cssClass = "black-stroke"
             d = "M 150 150 C 100,250 305,260 230,140 C 205,190 165,170 150,150 Z"
             fill = "red"
         }
     }

     FileWriter("build/tmp/codeMonkey.svg").use {
         svg.render(it, SVG.RenderMode.FILE)
     }
    }

    private fun Container.ear(x: Int, y: Int) {
        circle {
            cssClass = "black-stroke fur-color"
            cx = x.toString()
            cy = y.toString()
            r = "40"
        }
        circle {
            cssClass = "black-stroke fur-color"
            cx = x.toString()
            cy = y.toString()
            r = "28"
        }
    }

Yields a code monkey: code monkey

Validation

Originally the attribute values were appropriate types, but eventually as support for percentage values etc. was added, and considering they are always represented as strings in SVG, the value type was changed to String. But that made errors like length = "foo" possible. Therefore, attribute validation was added. Over time other validations became apparent too. With validation on, while the SVG is created, things are checked, for example when an attribute string is assigned, if it can be properly validated, it is. Validation can be turned off for performance reasons.

About Inline vs File SVG

SVG is an XML tag based format. Those tags can be put into an .svg file, or in modern browsers appear directly inline in the HTML5. However some attributes and other details differ slightly between these modes. This DSL is biased toward the inline representation because that's its origin, but it supports indicating the rendering mode and in the limitted scenarios tested it works.

A Limited Set of Elements

Currently only a small set of SVG Elements are supported. Adding more is straight forward, I just met my own needs, and so additions can be done by others, or as my needs increase.

Multiplatform Support

This is a multiplatform project currently targeting JVM and JavaScript. The JVM target is fully baked, the JavaScript is still a bit doughy. Looking for JavaScript folks to help me polish it.

As Compared To kotlinx.html

Why did I write yet another SVG DSL when SVG is covered by the kotlinx.html? Let's just say ... you try using it. I could not figure out how to use it based on the SVG specification. This package is as close to a one to one mapping as I could make it. So what if you want to combine them? Not a problem, just use unsafe/raw:


 val svg = SVG.svg {
   // ....
 }
 System.out.appendHTML().html {
     body {
        unsafe {
            raw(svg.toString())
        }
     }
 }

See Also

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory