From 3a26c7da384a2367e8da1688070ec89f73d118dd Mon Sep 17 00:00:00 2001 From: "Kaloyan D. Bozhkov" Date: Sun, 13 Oct 2024 19:14:55 -0700 Subject: [PATCH] fix: Creating canvas with width or height of zero or less (#739) * fix: Creating canvas with width or height of zero or less * test: createCanvas default sizes for non-positive values * fix --------- Co-authored-by: LongYinan --- __test__/canvas-class.spec.ts | 17 +++++++++++++++++ src/lib.rs | 18 +++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/__test__/canvas-class.spec.ts b/__test__/canvas-class.spec.ts index 90fbf4f8..1236d366 100644 --- a/__test__/canvas-class.spec.ts +++ b/__test__/canvas-class.spec.ts @@ -23,5 +23,22 @@ test('ctx.canvas should be equal to canvas', (t) => { test('[SVG] ctx.canvas should be equal to canvas', (t) => { const canvas = createCanvas(100, 100, SvgExportFlag.NoPrettyXML) const ctx = canvas.getContext('2d') + // @ts-expect-error t.is(ctx.canvas, canvas) }) + +test('Canvas size should equal 350x150 when provided non-positive values', (t) => { + let canvas = createCanvas(0, 0) + t.is(canvas.width, 350) + t.is(canvas.height, 150) + canvas = createCanvas(-1, 10) + t.is(canvas.width, 350) + t.is(canvas.height, 10) + canvas = createCanvas(10, -10) + t.is(canvas.height, 150) + t.is(canvas.width, 10) + + const svgCanvas = createCanvas(10, -10, SvgExportFlag.ConvertTextToPaths) + t.is(svgCanvas.height, 150) + t.is(svgCanvas.width, 10) +}) diff --git a/src/lib.rs b/src/lib.rs index 8160d84b..276d418c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,7 +103,10 @@ impl<'scope> CanvasElement<'scope> { } #[napi(constructor)] - pub fn new(env: &Env, mut this: This, width: u32, height: u32) -> Result { + pub fn new(env: &Env, mut this: This, width: i32, height: i32) -> Result { + // Default fallback of canvas on browser and skia-canvas is 350x150 + let width = (if width <= 0 { 350 } else { width }) as u32; + let height = (if height <= 0 { 150 } else { height }) as u32; let ctx = Self::create_context(env, width, height)?; let ctx = ctx.assign_to_this_with_attributes("ctx", PropertyAttributes::Default, &mut this)?; let mut ctx_obj = ctx.as_object(env); @@ -116,7 +119,8 @@ impl<'scope> CanvasElement<'scope> { } #[napi(setter)] - pub fn set_width(&mut self, env: Env, width: u32) -> Result<()> { + 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( @@ -133,7 +137,8 @@ impl<'scope> CanvasElement<'scope> { } #[napi(setter)] - pub fn set_height(&mut self, env: Env, height: u32) -> Result<()> { + 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( @@ -444,10 +449,13 @@ impl<'scope> SVGCanvas<'scope> { pub fn new( env: &Env, mut this: This, - width: u32, - height: u32, + width: i32, + height: i32, flag: SvgExportFlag, ) -> Result> { + // Default fallback of canvas on browser and skia-canvas is 350x150 + let width = (if width <= 0 { 350 } else { width }) as u32; + let height = (if height <= 0 { 150 } else { height }) as u32; let ctx = CanvasRenderingContext2D::into_instance( CanvasRenderingContext2D { context: Context::new_svg(width, height, flag.into(), ColorSpace::default())?,