// // canvas.cc // // Copyright (c) 2010 LearnBoost // #include "canvas.h" using namespace v8; /* * Initialize Canvas. */ void Canvas::Initialize(Handle target) { HandleScope scope; Local t = FunctionTemplate::New(Canvas::New); t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(String::NewSymbol("Canvas")); NODE_SET_PROTOTYPE_METHOD(t, "savePNG", SavePNG); target->Set(String::NewSymbol("Canvas"), t->GetFunction()); } /* * Initialize a Canvas with the given width and height. */ Handle Canvas::New(const Arguments &args) { HandleScope scope; if (!args[0]->IsNumber()) return ThrowException(Exception::TypeError(String::New("width required"))); if (!args[1]->IsNumber()) return ThrowException(Exception::TypeError(String::New("height required"))); Canvas *canvas = new Canvas(args[0]->Uint32Value(), args[1]->Uint32Value()); canvas->Wrap(args.This()); return args.This(); } /* * Initialize cairo surface. */ Canvas::Canvas(int width, int height): ObjectWrap() { _surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); } /* * Destroy cairo surface. */ Canvas::~Canvas() { cairo_surface_destroy(_surface); } /* * Save a PNG at the given path. */ Handle Canvas::SavePNG(const Arguments &args) { HandleScope scope; Canvas *canvas = ObjectWrap::Unwrap(args.This()); if (!args[0]->IsString()) return ThrowException(Exception::TypeError(String::New("path required"))); String::Utf8Value path(args[0]->ToString()); cairo_surface_write_to_png(canvas->getSurface(), *path); return Undefined(); }