Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Fix shape building #555

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ open class GraphicsPath(
override fun clone() = GraphicsPath(IntArrayList(commands), DoubleArrayList(data), winding)
override fun toString(): String = "GraphicsPath(\"${this.toSvgPathString()}\")"
}

fun VectorPath.toGraphicsPath() = GraphicsPath(commands, data, winding)
16 changes: 13 additions & 3 deletions korim/src/commonMain/kotlin/com/soywiz/korim/vector/Shape.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.soywiz.korim.font.*
import com.soywiz.korim.paint.*
import com.soywiz.korim.text.HorizontalAlign
import com.soywiz.korim.text.VerticalAlign
import com.soywiz.korim.vector.format.*
import com.soywiz.korio.serialization.xml.Xml
import com.soywiz.korio.util.niceStr
import com.soywiz.korio.util.toStringDecimal
Expand Down Expand Up @@ -126,6 +127,7 @@ fun Shape.getBounds(out: Rectangle = Rectangle(), bb: BoundsBuilder = BoundsBuil
return out
}

fun Shape.toSvgInstance(scale: Double = 1.0): SVG = SVG(toSvg(scale))
fun Shape.toSvg(scale: Double = 1.0): Xml = SvgBuilder(this.getBounds(), scale).apply { buildSvg(this) }.toXml()
fun Drawable.toShape(width: Int, height: Int): Shape = buildShape(width, height) { draw(this@toShape) }
fun Drawable.toSvg(width: Int, height: Int, scale: Double = 1.0): Xml = toShape(width, height).toSvg(scale)
Expand All @@ -134,12 +136,21 @@ fun SizedDrawable.toShape(): Shape = toShape(width, height)
fun SizedDrawable.toSvg(scale: Double = 1.0): Xml = toSvg(width, height, scale)

interface StyledShape : Shape {
/**
* Path with transform already applied
*
* @TODO: Probably it shouldn't have the transform applied
*/
val path: GraphicsPath? get() = null
val clip: GraphicsPath?
val paint: Paint
val transform: Matrix
val globalAlpha: Double

fun getUntransformedPath(): GraphicsPath? {
return path?.clone()?.applyTransform(transform.inverted())?.toGraphicsPath()
}

override fun addBounds(bb: BoundsBuilder, includeStrokes: Boolean): Unit {
path?.let { path ->
// path is already transformed, so using `transform` is not required
Expand All @@ -150,8 +161,7 @@ interface StyledShape : Shape {
override fun buildSvg(svg: SvgBuilder) {
svg.nodes += Xml.Tag(
"path", mapOf(
//"d" to path.toSvgPathString(svg.scale, svg.tx, svg.ty)
"d" to (path?.toSvgPathString() ?: ""),
"d" to (getUntransformedPath()?.toSvgPathString() ?: ""),
"transform" to transform.toSvg()
) + getSvgXmlAttributes(svg), listOf()
)
Expand All @@ -167,7 +177,7 @@ interface StyledShape : Shape {

override fun draw(c: Context2d) {
c.keepTransform {
c.transform(transform)
//c.transform(transform) // Already applied to the path
c.beginPath()
path?.draw(c)
if (clip != null) {
Expand Down
14 changes: 14 additions & 0 deletions korim/src/commonTest/kotlin/com/soywiz/korim/vector/SvgTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ class SvgTest {
assertEquals(listOf('m', -100.123, 100.456, 'c', -1.1234, 3.3E-4, -1.111, 0.123), tokens)
}

@Test
fun testShapeCoords() {
val svg = SVG("""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg id="svg2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 900" version="1.1">
<g transform="matrix(2,0,0,2,0,0)">
<rect x="0" y="0" width="100" height="100" fill="#fff" />
</g>
</svg>
""".trimIndent())
val svgShape = svg.toShape().toSvgInstance().toShape()
val svgShape2 = svgShape.toSvgInstance().toShape()
assertEquals(svgShape, svgShape2)
}

val SAMPLE_LOGO = """
<svg id="7fe010bd-4468-4253-af77-c0b7be09145b" data-name="Capa 1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="462" height="462" viewBox="0 0 462 462">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ open class VectorPath(
}

override fun moveTo(x: Double, y: Double) {
if (commands.isNotEmpty() && commands.last() == Command.MOVE_TO) {
if (lastX == x && lastY == y) return
}
commands.add(Command.MOVE_TO)
data.add(x, y)
lastXY(x, y)
Expand Down