You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.0 KiB

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