Browse Source

Added Image#{width,height} getters

v1.x
Tj Holowaychuk 14 years ago
parent
commit
08ff03d0f0
  1. 26
      src/Image.cc
  2. 3
      src/Image.h
  3. 8
      test/image.test.js

26
src/Image.cc

@ -23,6 +23,8 @@ Image::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "inspect", Inspect);
proto->SetAccessor(String::NewSymbol("src"), GetSrc, SetSrc);
proto->SetAccessor(String::NewSymbol("complete"), GetComplete);
proto->SetAccessor(String::NewSymbol("width"), GetWidth);
proto->SetAccessor(String::NewSymbol("height"), GetHeight);
proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload);
proto->SetAccessor(String::NewSymbol("onerror"), GetOnerror, SetOnerror);
target->Set(String::NewSymbol("Image"), t->GetFunction());
@ -67,6 +69,26 @@ Image::GetComplete(Local<String>, const AccessorInfo &info) {
return Boolean::New(img->complete);
}
/*
* Get width.
*/
Handle<Value>
Image::GetWidth(Local<String>, const AccessorInfo &info) {
Image *img = ObjectWrap::Unwrap<Image>(info.This());
return Number::New(img->width);
}
/*
* Get height.
*/
Handle<Value>
Image::GetHeight(Local<String>, const AccessorInfo &info) {
Image *img = ObjectWrap::Unwrap<Image>(info.This());
return Number::New(img->height);
}
/*
* Get src path.
*/
@ -196,7 +218,7 @@ Image::load() {
}
/*
* Invoke onload (when assigned).
* Invoke onload (when assigned) and assign dimensions.
*/
void
@ -239,5 +261,7 @@ Image::error(Local<Value> err) {
cairo_status_t
Image::loadSurface() {
_surface = cairo_image_surface_create_from_png(filename);
width = cairo_image_surface_get_width(_surface);
height = cairo_image_surface_get_height(_surface);
return cairo_surface_status(_surface);
}

3
src/Image.h

@ -16,6 +16,7 @@ class Image: public node::ObjectWrap {
public:
bool complete;
char *filename;
int width, height;
Persistent<Function> onload;
Persistent<Function> onerror;
static void Initialize(Handle<Object> target);
@ -25,6 +26,8 @@ class Image: public node::ObjectWrap {
static Handle<Value> GetOnload(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetOnerror(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetComplete(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetWidth(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetHeight(Local<String> prop, const AccessorInfo &info);
static void SetSrc(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetOnload(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetOnerror(Local<String> prop, Local<Value> val, const AccessorInfo &info);

8
test/image.test.js

@ -19,14 +19,16 @@ module.exports = {
assert.strictEqual(false, img.complete);
img.onload = function(){
assert.strictEqual(true, img.complete);
++n;
};
img.src = png;
assert.equal(img.src, png);
beforeExit(function(){
assert.strictEqual(true, img.complete);
assert.strictEqual(320, img.width);
assert.strictEqual(320, img.height);
assert.equal(1, n);
});
},
@ -41,9 +43,9 @@ module.exports = {
};
img.onerror = function(err){
++n;
assert.strictEqual(false, img.complete);
assert.ok(err instanceof Error, 'did not invoke onerror() with error');
++n;
};
img.src = png + 's';

Loading…
Cancel
Save