Skip to content

nwillc/ksvg

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

A Multiplatform Kotlin SVG image DSL.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages