Skip to content

Commit

Permalink
Add test in jvm for SVGCanvas and fix SVGCanvas native parts (#660)
Browse files Browse the repository at this point in the history
Co-authored-by: Steyn Geldenhuys <[email protected]>
  • Loading branch information
Phaestion and Phaestion authored Feb 10, 2023
1 parent a51e1cf commit fed511e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object SVGCanvas {
fun make(bounds: Rect, out: WStream, convertTextToPaths: Boolean, prettyXML: Boolean): Canvas {
Stats.onNativeCall()
val ptr = try {
SVGCanvas_nMake(
_nMake(
bounds.left,
bounds.top,
bounds.right,
Expand All @@ -61,5 +61,6 @@ object SVGCanvas {
}
}

@ExternalSymbolName("org_jetbrains_skia_svg_SVGCanvas__1nMake")
private external fun SVGCanvas_nMake(left: Float, top: Float, right: Float, bottom: Float, wstreamPtr: NativePointer, flags: Int): NativePointer
@ExternalSymbolName("org_jetbrains_skia_svg_SVGCanvasKt__1nMake")
private external fun _nMake(left: Float, top: Float, right: Float, bottom: Float, wstreamPtr: NativePointer, flags: Int): NativePointer

62 changes: 62 additions & 0 deletions skiko/src/jvmTest/kotlin/org/jetbrains/skia/svg/SVGCanvasTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.jetbrains.skiko.tests.org.jetbrains.skia.svg

import org.jetbrains.skia.Data
import org.jetbrains.skia.OutputWStream
import org.jetbrains.skia.Point
import org.jetbrains.skia.Rect
import org.jetbrains.skia.svg.*
import java.io.ByteArrayOutputStream
import kotlin.test.Test
import kotlin.test.assertEquals

class SVGCanvasTest {

val svgText = """
<svg version="1.1"
width="300" height="200"
xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="red" />
<circle cx="150" cy="100" r="80" fill="green" />
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>
""".trimIndent()

@Test
fun svgCanvasSmoke() {
val data = Data.makeFromBytes(svgText.encodeToByteArray())
val inputDom = SVGDOM(data)

val baos = ByteArrayOutputStream()
val svgCanvas = SVGCanvas.make(Rect.Companion.makeWH(300f, 200f), OutputWStream(baos))
inputDom.render(svgCanvas)
svgCanvas.close()

val svgCanvasData = Data.makeFromBytes(baos.toByteArray())
val dom = SVGDOM(svgCanvasData)

require(!dom.isClosed)
dom.setContainerSize(Point(100f, 100f))
dom.setContainerSize(101f, 101f)
require(dom.root != null)
val e = dom.root!!
require(e.x.unit == SVGLengthUnit.NUMBER)
require(e.y.unit == SVGLengthUnit.NUMBER)
require(e.width.unit == SVGLengthUnit.NUMBER)
require(e.height.unit == SVGLengthUnit.NUMBER)
require(e.viewBox == null)
require(e.tag == SVGTag.SVG)
e.viewBox = Rect(0f, 1f, 100f, 200f)

val aspectRatio =
SVGPreserveAspectRatio(SVGPreserveAspectRatioAlign.XMIN_YMIN, SVGPreserveAspectRatioScale.MEET)
e.preserveAspectRatio = aspectRatio
assertEquals(aspectRatio, e.preserveAspectRatio)
require(e.getIntrinsicSize(SVGLengthContext(100f, 100f)).x == 300f)
e.viewBox = Rect.makeXYWH(0f, 1f, 2f, 3f)
require(e.viewBox == Rect.makeXYWH(0f, 1f, 2f, 3f))
}
}

0 comments on commit fed511e

Please sign in to comment.