Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure SVGCanvas get content is complete #990

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __test__/pathkit.spec.ts.snap
Binary file not shown.
6 changes: 4 additions & 2 deletions __test__/svg-canvas.spec.ts.md

Large diffs are not rendered by default.

Binary file modified __test__/svg-canvas.spec.ts.snap
Binary file not shown.
Binary file modified scripts/__test__/utils.spec.ts.snap
Binary file not shown.
26 changes: 13 additions & 13 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,12 @@ extern "C"
CANVAS_CAST->drawPicture(picture, MATRIX_CAST, PAINT_CAST);
}

void skiac_canvas_destroy(skiac_canvas *c_canvas) {
if (c_canvas) {
delete CANVAS_CAST;
}
}


// Paint

Expand Down Expand Up @@ -1741,19 +1747,13 @@ extern "C"
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<SkDynamicMemoryWStream *>(c_w_memory_stream);
stream->write("</svg>", 6);
auto data = stream->detachAsData().release();

sk_data->data = reinterpret_cast<skiac_data *>(data);
sk_data->ptr = data->bytes();
sk_data->size = data->size();
auto string = new SkString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"");
string->appendS32(width);
string->append("\" height=\"");
string->appendS32(height);
string->append("\">\n");
stream->write(string->c_str(), string->size());
stream->flush();
auto size = stream->bytesWritten();
auto data = SkData::MakeUninitialized(size);
stream->copyTo(data->writable_data());
auto data_ptr = data.release();
sk_data->data = reinterpret_cast<skiac_data *>(data_ptr);
sk_data->ptr = data_ptr->bytes();
sk_data->size = data_ptr->size();
}

void skiac_sk_w_stream_destroy(skiac_w_memory_stream *c_w_memory_stream)
Expand Down
1 change: 1 addition & 0 deletions skia-c/skia_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ extern "C"
void skiac_canvas_write_pixels(skiac_canvas *c_canvas, int width, int height, uint8_t *pixels, size_t row_bytes, int x, int y);
void skiac_canvas_write_pixels_dirty(skiac_canvas *c_canvas, int width, int height, uint8_t *pixels, size_t row_bytes, size_t length, float x, float y, float dirty_x, float dirty_y, float dirty_width, float dirty_height, uint8_t cs);
void skiac_canvas_draw_picture(skiac_canvas *c_canvas, skiac_picture *c_picture, skiac_matrix *c_matrix, skiac_paint *c_paint);
void skiac_canvas_destroy(skiac_canvas *c_canvas);

// Paint
skiac_paint *skiac_paint_create();
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,27 @@ impl<'scope> SVGCanvas<'scope> {
}

#[napi]
pub fn get_content(&self, env: Env) -> Result<BufferSlice> {
let svg_data_stream = self.ctx.context.stream.as_ref().unwrap();
pub fn get_content(&mut self, env: Env) -> Result<BufferSlice> {
let svg_data_stream = self
.ctx
.context
.stream
.take()
.ok_or_else(|| Error::new(Status::GenericFailure, "SVGCanvas has no stream"))?;
unsafe {
sk::ffi::skiac_canvas_destroy(self.ctx.context.surface.0);
};
let svg_data = svg_data_stream.data(self.ctx.context.width, self.ctx.context.height);
let (surface, stream) = sk::Surface::new_svg(
self.ctx.context.width,
self.ctx.context.height,
self.ctx.context.surface.alpha_type(),
self.flag.into(),
ColorSpace::default(),
)
.ok_or_else(|| Error::new(Status::GenericFailure, "Failed to create surface"))?;
self.ctx.context.surface = surface;
self.ctx.context.stream = Some(stream);
unsafe {
BufferSlice::from_external(&env, svg_data.0.ptr, svg_data.0.size, svg_data, |_, d| {
mem::drop(d)
Expand Down
4 changes: 3 additions & 1 deletion src/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ pub mod ffi {
paint: *mut skiac_paint,
);

pub fn skiac_canvas_destroy(canvas: *mut skiac_canvas);

pub fn skiac_paint_create() -> *mut skiac_paint;

pub fn skiac_paint_clone(source: *mut skiac_paint) -> *mut skiac_paint;
Expand Down Expand Up @@ -2001,7 +2003,7 @@ impl Color {
}

#[repr(transparent)]
pub struct Canvas(*mut ffi::skiac_canvas);
pub struct Canvas(pub(crate) *mut ffi::skiac_canvas);

impl Canvas {
pub fn clear(&mut self) {
Expand Down