Tj Holowaychuk 15 years ago
parent
commit
0ceeaf477e
  1. 22
      src/canvas.cc

22
src/canvas.cc

@ -5,10 +5,12 @@
// Copyright (c) 2010 LearnBoost <tj@learnboost.com> // Copyright (c) 2010 LearnBoost <tj@learnboost.com>
// //
#include <string.h>
#include "canvas.h" #include "canvas.h"
#include <string.h>
#include <node_buffer.h>
using namespace v8; using namespace v8;
using namespace node;
/* /*
* Initialize Canvas. * Initialize Canvas.
@ -45,22 +47,19 @@ Canvas::New(const Arguments &args) {
} }
typedef struct { typedef struct {
unsigned char *pos;
unsigned char *end;
Handle<Function> fn; Handle<Function> fn;
} closure_t; } closure_t;
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) {
printf("bytes %d\n", len);
closure_t *closure = (closure_t *) c; closure_t *closure = (closure_t *) c;
if (closure->pos + len > closure->end)
return CAIRO_STATUS_WRITE_ERROR;
memcpy(closure->pos, data, len);
Handle<Value> argv[1]; Handle<Value> argv[1];
argv[0] = String::New("test"); Buffer *buf = Buffer::New(len);
closure->fn.Call(Context::GetCurrent()->Global(), 1, argv); memcpy(buf->data(), data, len);
closure->pos += len; argv[0] = buf->handle_;
closure->fn->Call(Context::GetCurrent()->Global(), 1, argv);
// TODO: CAIRO_STATUS_NO_MEMORY
// TODO: pass len
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;
} }
@ -76,9 +75,6 @@ Canvas::StreamPNG(const Arguments &args) {
return ThrowException(Exception::TypeError(String::New("function required"))); return ThrowException(Exception::TypeError(String::New("function required")));
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This()); Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This());
closure_t closure; closure_t closure;
uint8_t data[64 * 1024];
closure.pos = data;
closure.end = data + sizeof(data);
closure.fn = Handle<Function>::Cast(args[0]); // TODO: leakage? closure.fn = Handle<Function>::Cast(args[0]); // TODO: leakage?
cairo_surface_write_to_png_stream(canvas->getSurface(), writeToBuffer, &closure); cairo_surface_write_to_png_stream(canvas->getSurface(), writeToBuffer, &closure);
return Undefined(); return Undefined();

Loading…
Cancel
Save