diff --git a/__test__/svg-canvas.spec.ts b/__test__/svg-canvas.spec.ts
new file mode 100644
index 00000000..8e062f29
--- /dev/null
+++ b/__test__/svg-canvas.spec.ts
@@ -0,0 +1,40 @@
+import { join } from 'path'
+
+import ava, { TestInterface } from 'ava'
+
+import { createCanvas, SvgCanvas, SvgExportFlag, GlobalFonts } from '../index'
+
+const test = ava as TestInterface<{
+ canvas: SvgCanvas
+}>
+
+test.beforeEach((t) => {
+ t.context.canvas = createCanvas(1024, 768, SvgExportFlag.ConvertTextToPaths)
+})
+
+test('should be able to export path/arc/rect', (t) => {
+ const { canvas } = t.context
+ const ctx = canvas.getContext('2d')
+ ctx.fillStyle = 'yellow'
+ ctx.fillRect(0, 0, canvas.width, canvas.height)
+ ctx.lineWidth = 3
+ ctx.strokeStyle = 'hotpink'
+ ctx.strokeRect(50, 450, 100, 100)
+ ctx.fillStyle = 'hotpink'
+ ctx.arc(500, 120, 90, 0, Math.PI * 2)
+ ctx.fill()
+ t.snapshot(canvas.getContent().toString('utf8'))
+})
+
+test('should be able to export text', (t) => {
+ GlobalFonts.registerFromPath(join(__dirname, 'fonts-dir', 'iosevka-curly-regular.woff2'), 'i-curly')
+ const { canvas } = t.context
+ const ctx = canvas.getContext('2d')
+ ctx.fillStyle = 'yellow'
+ ctx.fillRect(0, 0, canvas.width, canvas.height)
+ ctx.lineWidth = 3
+ ctx.strokeStyle = 'hotpink'
+ ctx.font = '50px i-curly'
+ ctx.strokeText('@napi-rs/canvas', 50, 300)
+ t.snapshot(canvas.getContent().toString('utf8'))
+})
diff --git a/__test__/svg-canvas.spec.ts.md b/__test__/svg-canvas.spec.ts.md
new file mode 100644
index 00000000..5e07b92d
--- /dev/null
+++ b/__test__/svg-canvas.spec.ts.md
@@ -0,0 +1,26 @@
+# Snapshot report for `__test__/svg-canvas.spec.ts`
+
+The actual snapshot is saved in `svg-canvas.spec.ts.snap`.
+
+Generated by [AVA](https://avajs.dev).
+
+## should be able to export path/arc/rect
+
+> Snapshot 1
+
+ `␊
+ `
+
+## should be able to export text
+
+> Snapshot 1
+
+ `␊
+ `
diff --git a/__test__/svg-canvas.spec.ts.snap b/__test__/svg-canvas.spec.ts.snap
new file mode 100644
index 00000000..1e097bf4
Binary files /dev/null and b/__test__/svg-canvas.spec.ts.snap differ
diff --git a/example/export-svg.js b/example/export-svg.js
new file mode 100644
index 00000000..4a1f45f8
--- /dev/null
+++ b/example/export-svg.js
@@ -0,0 +1,34 @@
+const { readFileSync, writeFileSync } = require('fs')
+const { join } = require('path')
+
+const { createCanvas, GlobalFonts, SvgExportFlag } = require('../index.js')
+
+const WoffFontPath = join(__dirname, '..', '__test__', 'fonts', 'Virgil.woff2')
+
+GlobalFonts.registerFromPath(WoffFontPath, 'Virgil')
+
+const canvas = createCanvas(1024, 768, SvgExportFlag.ConvertTextToPaths)
+const ctx = canvas.getContext('2d')
+ctx.fillStyle = 'yellow'
+ctx.fillRect(0, 0, canvas.width, canvas.height)
+ctx.strokeStyle = 'cyan'
+ctx.lineWidth = 3
+ctx.font = '50px Virgil'
+ctx.strokeText('skr canvas', 50, 150)
+
+ctx.strokeStyle = 'hotpink'
+
+ctx.strokeText('@napi-rs/canvas', 50, 300)
+
+ctx.strokeStyle = 'gray'
+
+ctx.strokeRect(50, 450, 100, 100)
+
+ctx.fillStyle = 'hotpink'
+
+ctx.arc(500, 120, 90, 0, Math.PI * 2)
+ctx.fill()
+
+const b = canvas.getContent()
+
+writeFileSync(join(__dirname, 'export-text.svg'), b)
diff --git a/example/export-text.svg b/example/export-text.svg
new file mode 100644
index 00000000..cc6f4363
--- /dev/null
+++ b/example/export-text.svg
@@ -0,0 +1,9 @@
+
+
\ No newline at end of file
diff --git a/index.d.ts b/index.d.ts
index 506d2305..b46269ef 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -273,6 +273,14 @@ export interface SKRSContext2D extends Omitstream = reinterpret_cast(w_stream);
+ c_surface->surface = reinterpret_cast(surface);
+ c_surface->canvas = reinterpret_cast(canvas.release());
+ }
+
skiac_surface *skiac_surface_create_rgba_premultiplied(int width, int height)
{
return reinterpret_cast(
@@ -156,7 +175,7 @@ extern "C"
auto png_data = image->encodeToData().release();
if (png_data)
{
- data->ptr = const_cast(png_data->bytes());
+ data->ptr = png_data->bytes();
data->size = png_data->size();
data->data = reinterpret_cast(png_data);
}
@@ -1072,7 +1091,7 @@ extern "C"
void skiac_sk_data_destroy(skiac_data *c_data)
{
auto data = reinterpret_cast(c_data);
- data->unref();
+ SkSafeUnref(data);
}
// Bitmap
@@ -1215,4 +1234,27 @@ extern "C"
{
delete c_font_collection;
}
+
+ // SkWStream
+ void skiac_sk_w_stream_get(skiac_w_memory_stream *c_w_memory_stream, skiac_sk_data *sk_data, int width, int height)
+ {
+ auto stream = reinterpret_cast(c_w_memory_stream);
+ stream->write("", 6);
+ auto data = stream->detachAsData().release();
+
+ sk_data->data = reinterpret_cast(data);
+ sk_data->ptr = data->bytes();
+ sk_data->size = data->size();
+ auto string = new SkString("\n