// // Image.cc // // Copyright (c) 2010 LearnBoost // #include "Canvas.h" #include "Image.h" /* * Initialize Image. */ void Image::Initialize(Handle target) { HandleScope scope; Local t = FunctionTemplate::New(Image::New); t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(String::NewSymbol("Image")); Local proto = t->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(t, "inspect", Inspect); proto->SetAccessor(String::NewSymbol("src"), GetSrc, SetSrc); target->Set(String::NewSymbol("Image"), t->GetFunction()); } /* * Initialize a new Image. */ Handle Image::New(const Arguments &args) { HandleScope scope; Image *img = new Image; img->Wrap(args.This()); return args.This(); } /* * Custom inspect. */ Handle Image::Inspect(const Arguments &args) { HandleScope scope; Image *img = ObjectWrap::Unwrap(args.This()); Local str = String::New("[Image"); if (img->loaded()) { str = String::Concat(str, String::New(" ")); str = String::Concat(str, String::New(img->filename)); } str = String::Concat(str, String::New("]")); return scope.Close(str); } /* * Get src path. */ Handle Image::GetSrc(Local, const AccessorInfo &info) { Image *img = ObjectWrap::Unwrap(info.This()); return String::New(img->filename); } /* * Set src path. */ void Image::SetSrc(Local, Local val, const AccessorInfo &info) { if (val->IsString()) { String::AsciiValue src(val); Image *img = ObjectWrap::Unwrap(info.This()); img->filename = *src; Handle onload = info.This()->Get(String::New("onload")); if (onload->IsFunction()) { img->onload = Persistent::New(Handle::Cast(onload)); } } } /* * Initialize a new Image. */ Image::Image() { filename = NULL; _surface = NULL; } /* * Destroy image and associated surface. */ Image::~Image() { cairo_surface_destroy(_surface); }