|
|
@ -61,14 +61,10 @@ Canvas::New(const Arguments &args) { |
|
|
|
static cairo_status_t |
|
|
|
writeToBuffer(void *c, const uint8_t *data, unsigned len) { |
|
|
|
closure_t *closure = (closure_t *) c; |
|
|
|
Handle<Value> argv[2]; |
|
|
|
Buffer *buf = Buffer::New(len); |
|
|
|
memcpy(buf->data(), data, len); |
|
|
|
argv[0] = buf->handle_; |
|
|
|
argv[1] = Integer::New(len); |
|
|
|
closure->fn->Call(Context::GetCurrent()->Global(), 2, argv); |
|
|
|
// TODO: CAIRO_STATUS_NO_MEMORY
|
|
|
|
// TODO: leak
|
|
|
|
Handle<Value> argv[3] = { Null(), buf->handle_, Integer::New(len) }; |
|
|
|
closure->fn->Call(Context::GetCurrent()->Global(), 3, argv); |
|
|
|
return CAIRO_STATUS_SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
@ -79,18 +75,20 @@ writeToBuffer(void *c, const uint8_t *data, unsigned len) { |
|
|
|
Handle<Value> |
|
|
|
Canvas::StreamPNG(const Arguments &args) { |
|
|
|
HandleScope scope; |
|
|
|
// TODO: error handling
|
|
|
|
// TODO: nonblocking
|
|
|
|
// TODO: async
|
|
|
|
if (!args[0]->IsFunction()) |
|
|
|
return ThrowException(Exception::TypeError(String::New("callback function required"))); |
|
|
|
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This()); |
|
|
|
closure_t closure; |
|
|
|
closure.fn = Handle<Function>::Cast(args[0]); |
|
|
|
cairo_surface_write_to_png_stream(canvas->getSurface(), writeToBuffer, &closure); |
|
|
|
Handle<Value> argv[2]; |
|
|
|
argv[0] = Null(); |
|
|
|
argv[1] = Integer::New(0); |
|
|
|
closure.fn->Call(Context::GetCurrent()->Global(), 2, argv); |
|
|
|
cairo_status_t status = cairo_surface_write_to_png_stream(canvas->getSurface(), writeToBuffer, &closure); |
|
|
|
if (status) { |
|
|
|
Handle<Value> argv[1] = { Canvas::Error(status) }; |
|
|
|
closure.fn->Call(Context::GetCurrent()->Global(), 1, argv); |
|
|
|
} else { |
|
|
|
Handle<Value> argv[3] = { Null(), Null(), Integer::New(0) }; |
|
|
|
closure.fn->Call(Context::GetCurrent()->Global(), 3, argv); |
|
|
|
} |
|
|
|
return Undefined(); |
|
|
|
} |
|
|
|
|
|
|
@ -110,6 +108,15 @@ Canvas::~Canvas() { |
|
|
|
cairo_surface_destroy(_surface); |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct an Error from the given cairo status. |
|
|
|
*/ |
|
|
|
|
|
|
|
Handle<Value> |
|
|
|
Canvas::Error(cairo_status_t status) { |
|
|
|
return Exception::Error(String::New(cairo_status_to_string(status))); |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Save a PNG at the given path. |
|
|
|
*/ |
|
|
|