Skip to content

Commit

Permalink
fix: segfault while drawing empty text (#528)
Browse files Browse the repository at this point in the history
- Close #526
  • Loading branch information
Brooooooklyn committed Aug 13, 2022
1 parent d798696 commit 88f736c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions __test__/draw.spec.ts
Expand Up @@ -870,6 +870,12 @@ test('strokeText', async (t) => {
await snapshotImage(t, { canvas, ctx }, 'png', 3.5)
})

test('empty text', async (t) => {
const { ctx } = t.context
t.notThrows(() => ctx.fillText('', 50, 50))
t.notThrows(() => ctx.strokeText('', 50, 50))
})

test('draw-text-emoji', async (t) => {
if (platform() === 'darwin') {
t.pass('macOS definitely supports emoji')
Expand Down
12 changes: 10 additions & 2 deletions src/ctx.rs
Expand Up @@ -1471,6 +1471,10 @@ fn stroke_rect(ctx: CallContext) -> Result<JsUndefined> {
#[js_function(4)]
fn stroke_text(ctx: CallContext) -> Result<JsUndefined> {
let text = ctx.get::<JsString>(0)?.into_utf8()?;
let text = text.as_str()?;
if text.is_empty() {
return ctx.env.get_undefined();
}
let x = ctx.get::<JsNumber>(1)?.get_double()? as f32;
let y = ctx.get::<JsNumber>(2)?.get_double()? as f32;
let max_width = if ctx.length == 3 {
Expand All @@ -1481,7 +1485,7 @@ fn stroke_text(ctx: CallContext) -> Result<JsUndefined> {

let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;
context_2d.stroke_text(text.as_str()?, x, y, max_width)?;
context_2d.stroke_text(text, x, y, max_width)?;

ctx.env.get_undefined()
}
Expand All @@ -1504,6 +1508,10 @@ fn fill_rect(ctx: CallContext) -> Result<JsUndefined> {
#[js_function(4)]
fn fill_text(ctx: CallContext) -> Result<JsUndefined> {
let text = ctx.get::<JsString>(0)?.into_utf8()?;
let text = text.as_str()?;
if text.is_empty() {
return ctx.env.get_undefined();
}
let x = ctx.get::<JsNumber>(1)?.get_double()? as f32;
let y = ctx.get::<JsNumber>(2)?.get_double()? as f32;
let max_width = if ctx.length == 3 {
Expand All @@ -1514,7 +1522,7 @@ fn fill_text(ctx: CallContext) -> Result<JsUndefined> {

let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;
context_2d.fill_text(text.as_str()?, x, y, max_width)?;
context_2d.fill_text(text, x, y, max_width)?;

ctx.env.get_undefined()
}
Expand Down

0 comments on commit 88f736c

Please sign in to comment.