diff --git a/src/Canvas.cc b/src/Canvas.cc index c4a186b..c19622a 100644 --- a/src/Canvas.cc +++ b/src/Canvas.cc @@ -41,6 +41,7 @@ Canvas::Initialize(Handle target) { #ifdef HAVE_JPEG NODE_SET_PROTOTYPE_METHOD(constructor, "streamJPEGSync", StreamJPEGSync); #endif + proto->SetAccessor(String::NewSymbol("type"), GetType); proto->SetAccessor(String::NewSymbol("width"), GetWidth, SetWidth); proto->SetAccessor(String::NewSymbol("height"), GetHeight, SetHeight); target->Set(String::NewSymbol("Canvas"), constructor->GetFunction()); @@ -65,6 +66,17 @@ Canvas::New(const Arguments &args) { return args.This(); } +/* + * Get type string. + */ + +Handle +Canvas::GetType(Local prop, const AccessorInfo &info) { + HandleScope scope; + Canvas *canvas = ObjectWrap::Unwrap(info.This()); + return scope.Close(String::New(canvas->isPDF() ? "pdf" : "image")); +} + /* * Get width. */ diff --git a/src/Canvas.h b/src/Canvas.h index 846693f..023591f 100644 --- a/src/Canvas.h +++ b/src/Canvas.h @@ -48,6 +48,7 @@ class Canvas: public node::ObjectWrap { static void Initialize(Handle target); static Handle New(const Arguments &args); static Handle ToBuffer(const Arguments &args); + static Handle GetType(Local prop, const AccessorInfo &info); static Handle GetWidth(Local prop, const AccessorInfo &info); static Handle GetHeight(Local prop, const AccessorInfo &info); static void SetWidth(Local prop, Local val, const AccessorInfo &info); diff --git a/test/canvas.test.js b/test/canvas.test.js index 7049c5c..0ee1266 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -157,7 +157,16 @@ module.exports = { ctx.fillStyle = 'rgba(0, 0, 0, 42.42)'; assert.equal('#000000', ctx.fillStyle); }, - + + 'test Canvas#type': function(){ + var canvas = new Canvas(10, 10); + assert('image' == canvas.type); + var canvas = new Canvas(10, 10, 'pdf'); + assert('pdf' == canvas.type); + var canvas = new Canvas(10, 10, 'hey'); + assert('image' == canvas.type); + }, + 'test Canvas#getContext("2d")': function(){ var canvas = new Canvas(200, 300) , ctx = canvas.getContext('2d');