Permalink
Browse files

More Image goodness

  • Loading branch information...
1 parent e6ba903 commit 06d1c7eafb88026f667f7cdd83e466d9028dabea @tj tj committed Nov 11, 2010
Showing with 58 additions and 2 deletions.
  1. +1 −0 index.js
  2. +2 −0 lib/canvas.js
  3. +50 −2 src/Image.cc
  4. +3 −0 src/Image.h
  5. +2 −0 src/init.cc
View
@@ -0,0 +1 @@
+module.exports = require('./lib/canvas');
View
@@ -11,6 +11,7 @@
var canvas = require('../build/default/canvas')
, Canvas = canvas.Canvas
+ , Image = canvas.Image
, cairoVersion = canvas.cairoVersion
, Context2d = require('./context2d')
, PNGStream = require('./pngstream')
@@ -40,6 +41,7 @@ exports.cairoVersion = cairoVersion;
exports.Context2d = Context2d;
exports.PNGStream = PNGStream;
+exports.Image = Image;
/**
* Context2d implementation.
View
@@ -5,8 +5,8 @@
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
//
-#include "canvas.h"
-#include "image.h"
+#include "Canvas.h"
+#include "Image.h"
/*
* Initialize Image.
@@ -20,6 +20,7 @@ Image::Initialize(Handle<Object> target) {
t->SetClassName(String::NewSymbol("Image"));
Local<ObjectTemplate> proto = t->PrototypeTemplate();
+ NODE_SET_PROTOTYPE_METHOD(t, "inspect", Inspect);
proto->SetAccessor(String::NewSymbol("src"), GetSrc, SetSrc);
target->Set(String::NewSymbol("Image"), t->GetFunction());
}
@@ -37,10 +38,51 @@ Image::New(const Arguments &args) {
}
/*
+ * 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(" "));
+ 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());
+ 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());
+ img->load(*src);
+ }
+}
+
+/*
* Initialize a new Image.
*/
Image::Image() {
+ _filename = NULL;
_surface = NULL;
}
@@ -50,4 +92,10 @@ Image::Image() {
Image::~Image() {
cairo_surface_destroy(_surface);
+}
+
+void
+Image::load(char *path) {
+ _filename = path;
+ // TODO: implement
}
View
@@ -16,10 +16,13 @@ class Image: public node::ObjectWrap {
public:
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
+ static Handle<Value> Inspect(const Arguments &args);
static Handle<Value> GetSrc(Local<String> prop, const AccessorInfo &info);
static void SetSrc(Local<String> prop, Local<Value> val, const AccessorInfo &info);
inline cairo_surface_t *surface(){ return _surface; }
inline char *filename(){ return _filename; }
+ inline bool loaded(){ return _filename; }
+ void load(char *path);
Image();
private:
View
@@ -6,13 +6,15 @@
//
#include "Canvas.h"
+#include "Image.h"
#include "CanvasGradient.h"
#include "CanvasRenderingContext2d.h"
extern "C" void
init (Handle<Object> target) {
HandleScope scope;
Canvas::Initialize(target);
+ Image::Initialize(target);
Context2d::Initialize(target);
Gradient::Initialize(target);
target->Set(String::New("cairoVersion"), String::New(cairo_version_string()));

0 comments on commit 06d1c7e

Please sign in to comment.