From f01152c0e2506f1e330d8bc9d31fdc5fb7c0359a Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Mon, 4 Nov 2024 06:00:36 +0545 Subject: [PATCH 1/2] fix(SvgCanvas): allow canvas size modification --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 276d418c..2758b834 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -531,6 +531,42 @@ impl<'scope> SVGCanvas<'scope> { }) } } + + #[napi(setter)] + pub fn set_width(&mut self, env: Env, width: i32) -> Result<()> { + let width = (if width <= 0 { 350 } else { width }) as u32; + self.width = width; + let height = self.height; + let old_ctx = mem::replace( + &mut self.ctx.context, + Context::new(width, height, ColorSpace::default())?, + ); + env.adjust_external_memory((width as i64 - old_ctx.width as i64) * 4)?; + Ok(()) + } + + #[napi(getter)] + pub fn get_width(&self) -> u32 { + self.width + } + + #[napi(setter)] + pub fn set_height(&mut self, env: Env, height: i32) -> Result<()> { + let height = (if height <= 0 { 150 } else { height }) as u32; + self.height = height; + let width = self.width; + let old_ctx = mem::replace( + &mut self.ctx.context, + Context::new(width, height, ColorSpace::default())?, + ); + env.adjust_external_memory((height as i64 - old_ctx.height as i64) * 4)?; + Ok(()) + } + + #[napi(getter)] + pub fn get_height(&self) -> u32 { + self.height + } } #[napi] From 6d2e9ff992f7f621b327d112e70fe1e0c11f5ca9 Mon Sep 17 00:00:00 2001 From: twlite <46562212+twlite@users.noreply.github.com> Date: Mon, 4 Nov 2024 02:18:20 +0000 Subject: [PATCH 2/2] test: add tests --- __test__/svg-canvas.spec.ts | 9 +++++++++ src/lib.rs | 2 +- src/path.rs | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/__test__/svg-canvas.spec.ts b/__test__/svg-canvas.spec.ts index 120c1269..6f792f4a 100644 --- a/__test__/svg-canvas.spec.ts +++ b/__test__/svg-canvas.spec.ts @@ -12,6 +12,15 @@ test.beforeEach((t) => { t.context.canvas = createCanvas(1024, 768, SvgExportFlag.ConvertTextToPaths) }) +test('should be able to adjust size', (t) => { + const { canvas } = t.context + canvas.width = 512 + canvas.height = 384 + + t.is(canvas.width, 512) + t.is(canvas.height, 384) +}) + test('should be able to export path/arc/rect', (t) => { const { canvas } = t.context const ctx = canvas.getContext('2d') diff --git a/src/lib.rs b/src/lib.rs index 2758b834..9b8dac5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -532,7 +532,7 @@ impl<'scope> SVGCanvas<'scope> { } } - #[napi(setter)] + #[napi(setter)] pub fn set_width(&mut self, env: Env, width: i32) -> Result<()> { let width = (if width <= 0 { 350 } else { width }) as u32; self.width = width; diff --git a/src/path.rs b/src/path.rs index b098374e..759729ec 100644 --- a/src/path.rs +++ b/src/path.rs @@ -298,7 +298,7 @@ impl Path { #[napi(js_name = "toSVGString")] pub fn to_svg_string(&self, env: Env) -> Result { let sk_string = self.inner.to_svg_string(); - unsafe { env.create_string_from_c_char(sk_string.ptr, sk_string.length) } + unsafe { env.create_string_from_c_char(sk_string.ptr, sk_string.length as isize) } } #[napi]