diff --git a/lib/image.js b/lib/image.js index 231d6be..fa0040a 100644 --- a/lib/image.js +++ b/lib/image.js @@ -12,9 +12,41 @@ var Canvas = require('../build/default/canvas') , Image = Canvas.Image; +/** + * Src setter. + * + * - convert data uri to `Buffer` + * + * @param {String|Buffer} val filename, buffer, data uri + * @api public + */ + +Image.prototype.__defineSetter__('src', function(val){ + if ('string' == typeof val && 0 == val.indexOf('data:')) { + val = val.slice(val.indexOf(',') + 1); + this.source = new Buffer(val, 'base64'); + } else { + this.source = val; + } +}); + +/** + * Src getter. + * + * TODO: return buffer + * + * @api public + */ + +Image.prototype.__defineGetter__('src', function(){ + return this.source; +}); + /** * Inspect image. * + * TODO: indicate that the .src was a buffer, data uri etc + * * @return {String} * @api public */ diff --git a/src/Image.cc b/src/Image.cc index c0348dd..1d21a4b 100644 --- a/src/Image.cc +++ b/src/Image.cc @@ -51,7 +51,7 @@ Image::Initialize(Handle target) { // Prototype Local proto = constructor->PrototypeTemplate(); - proto->SetAccessor(String::NewSymbol("src"), GetSrc, SetSrc); + proto->SetAccessor(String::NewSymbol("source"), GetSource, SetSource); proto->SetAccessor(String::NewSymbol("complete"), GetComplete); proto->SetAccessor(String::NewSymbol("width"), GetWidth); proto->SetAccessor(String::NewSymbol("height"), GetHeight); @@ -109,7 +109,7 @@ Image::GetHeight(Local, const AccessorInfo &info) { */ Handle -Image::GetSrc(Local, const AccessorInfo &info) { +Image::GetSource(Local, const AccessorInfo &info) { HandleScope scope; Image *img = ObjectWrap::Unwrap(info.This()); return scope.Close(String::New(img->filename ? img->filename : "")); @@ -120,7 +120,7 @@ Image::GetSrc(Local, const AccessorInfo &info) { */ void -Image::SetSrc(Local, Local val, const AccessorInfo &info) { +Image::SetSource(Local, Local val, const AccessorInfo &info) { HandleScope scope; Image *img = ObjectWrap::Unwrap(info.This()); cairo_status_t status = CAIRO_STATUS_READ_ERROR; diff --git a/src/Image.h b/src/Image.h index b43376b..2d2ceb4 100644 --- a/src/Image.h +++ b/src/Image.h @@ -19,13 +19,13 @@ class Image: public node::ObjectWrap { static Persistent constructor; static void Initialize(Handle target); static Handle New(const Arguments &args); - static Handle GetSrc(Local prop, const AccessorInfo &info); + static Handle GetSource(Local prop, const AccessorInfo &info); static Handle GetOnload(Local prop, const AccessorInfo &info); static Handle GetOnerror(Local prop, const AccessorInfo &info); static Handle GetComplete(Local prop, const AccessorInfo &info); static Handle GetWidth(Local prop, const AccessorInfo &info); static Handle GetHeight(Local prop, const AccessorInfo &info); - static void SetSrc(Local prop, Local val, const AccessorInfo &info); + static void SetSource(Local prop, Local val, const AccessorInfo &info); static void SetOnload(Local prop, Local val, const AccessorInfo &info); static void SetOnerror(Local prop, Local val, const AccessorInfo &info); inline cairo_surface_t *surface(){ return _surface; }