Browse Source

streamPNG sig is now (err, buf, len)

v1.x
Tj Holowaychuk 15 years ago
parent
commit
6e4a4fc1b2
  1. 6
      lib/pngstream.js
  2. 19
      src/canvas.cc

6
lib/pngstream.js

@ -37,8 +37,10 @@ var PNGStream = module.exports = function PNGStream(canvas) {
var self = this; var self = this;
this.canvas = canvas; this.canvas = canvas;
process.nextTick(function(){ process.nextTick(function(){
canvas.streamPNG(function(chunk, len){ canvas.streamPNG(function(err, chunk, len){
if (len) { if (err) {
self.emit('error', err);
} else if (len) {
self.emit('data', chunk, len); self.emit('data', chunk, len);
} else { } else {
self.emit('end'); self.emit('end');

19
src/canvas.cc

@ -61,14 +61,11 @@ Canvas::New(const Arguments &args) {
static cairo_status_t static cairo_status_t
writeToBuffer(void *c, const uint8_t *data, unsigned len) { writeToBuffer(void *c, const uint8_t *data, unsigned len) {
closure_t *closure = (closure_t *) c; closure_t *closure = (closure_t *) c;
Handle<Value> argv[2];
Buffer *buf = Buffer::New(len); Buffer *buf = Buffer::New(len);
memcpy(buf->data(), data, len); memcpy(buf->data(), data, len);
argv[0] = buf->handle_; Handle<Value> argv[3] = { Null(), buf->handle_, Integer::New(len) };
argv[1] = Integer::New(len); closure->fn->Call(Context::GetCurrent()->Global(), 3, argv);
closure->fn->Call(Context::GetCurrent()->Global(), 2, argv);
// TODO: CAIRO_STATUS_NO_MEMORY // TODO: CAIRO_STATUS_NO_MEMORY
// TODO: leak
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;
} }
@ -79,16 +76,20 @@ writeToBuffer(void *c, const uint8_t *data, unsigned len) {
Handle<Value> Handle<Value>
Canvas::StreamPNG(const Arguments &args) { Canvas::StreamPNG(const Arguments &args) {
HandleScope scope; HandleScope scope;
// TODO: error handling // TODO: async
// TODO: nonblocking
if (!args[0]->IsFunction()) if (!args[0]->IsFunction())
return ThrowException(Exception::TypeError(String::New("callback function required"))); return ThrowException(Exception::TypeError(String::New("callback function required")));
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This()); Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This());
closure_t closure; closure_t closure;
closure.fn = Handle<Function>::Cast(args[0]); closure.fn = Handle<Function>::Cast(args[0]);
cairo_status_t status = cairo_surface_write_to_png_stream(canvas->getSurface(), writeToBuffer, &closure); cairo_status_t status = cairo_surface_write_to_png_stream(canvas->getSurface(), writeToBuffer, &closure);
Handle<Value> argv[2] = { Null(), Integer::New(0) }; if (status) {
closure.fn->Call(Context::GetCurrent()->Global(), 2, argv); 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(); return Undefined();
} }

Loading…
Cancel
Save