commonMain.Path.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kanvas-jvm Show documentation
Show all versions of kanvas-jvm Show documentation
A collection of drawing/charting utilities
The newest version!
package com.juul.krayon.kanvas
public class Path internal constructor(
private val instructions: SegmentedPath,
) {
public constructor(
path: PathBuilder<*>.() -> Unit,
) : this(SegmentedPathBuilder().apply(path).build())
private val cache = mutableMapOf, Any>()
/**
* Bridge the gap between this platform-agnostic type and its platform-specific type. The
* platform specific type is cached inside this [Path] instance, and multiple calls with the
* same marker will return the same object. As such, it is _very important_ that you don't
* mutate the returned platform type.
*/
public fun get(marker: PathTypeMarker): T {
@Suppress("UNCHECKED_CAST")
return cache.getOrPut(marker) { instructions.rebuildWith(marker.builder) } as T
}
/**
* Bridge the gap between this platform-agnostic type and its platform-specific type.
*
* Unlike [get], the built platform-specific type is not cached. This can be useful when an
* implementation requires some manual cleanup that the general [Path] would be unable to do.
*/
public fun buildWith(builder: PathBuilder
): P = instructions.rebuildWith(builder)
}