From 6ecdcec101af9aa56afee69da99eb1686ee67dfb Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Thu, 30 Sep 2010 13:27:33 -0700 Subject: [PATCH] Added Gradient#AddColorStopRGBA() --- src/canvas.h | 18 ++++++++++++++++++ src/context2d.cc | 22 ++-------------------- src/gradient.cc | 16 ++++++++++++++-- src/gradient.h | 4 +++- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/canvas.h b/src/canvas.h index 104080e..f359be5 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -13,6 +13,24 @@ #include #include +/* + * RGBA arg assertions. + */ + +#define RGBA_ARGS(N) \ + if (!args[N]->IsNumber()) \ + return ThrowException(Exception::TypeError(String::New("r required"))); \ + if (!args[N+1]->IsNumber()) \ + return ThrowException(Exception::TypeError(String::New("g required"))); \ + if (!args[N+2]->IsNumber()) \ + return ThrowException(Exception::TypeError(String::New("b required"))); \ + if (!args[N+3]->IsNumber()) \ + return ThrowException(Exception::TypeError(String::New("alpha required"))); \ + float r = args[N]->Int32Value(); \ + float g = args[N+1]->Int32Value(); \ + float b = args[N+2]->Int32Value(); \ + float a = args[N+3]->NumberValue(); + using namespace v8; class Canvas: public node::ObjectWrap { diff --git a/src/context2d.cc b/src/context2d.cc index c318918..45b0bb0 100644 --- a/src/context2d.cc +++ b/src/context2d.cc @@ -48,24 +48,6 @@ using namespace node; int width = args[2]->Int32Value(); \ int height = args[3]->Int32Value(); -/* - * RGBA arg assertions. - */ - -#define RGBA_ARGS \ - if (!args[0]->IsNumber()) \ - return ThrowException(Exception::TypeError(String::New("r required"))); \ - if (!args[1]->IsNumber()) \ - return ThrowException(Exception::TypeError(String::New("g required"))); \ - if (!args[2]->IsNumber()) \ - return ThrowException(Exception::TypeError(String::New("b required"))); \ - if (!args[3]->IsNumber()) \ - return ThrowException(Exception::TypeError(String::New("alpha required"))); \ - float r = args[0]->Int32Value(); \ - float g = args[1]->Int32Value(); \ - float b = args[2]->Int32Value(); \ - float a = args[3]->NumberValue(); - /* * Initialize Context2d. */ @@ -344,7 +326,7 @@ Context2d::SetLineCap(Local prop, Local val, const AccessorInfo & Handle Context2d::SetFillRGBA(const Arguments &args) { HandleScope scope; - RGBA_ARGS; + RGBA_ARGS(0); Context2d *context = ObjectWrap::Unwrap(args.This()); RGBA(context->fill,r,g,b,a); return Undefined(); @@ -357,7 +339,7 @@ Context2d::SetFillRGBA(const Arguments &args) { Handle Context2d::SetStrokeRGBA(const Arguments &args) { HandleScope scope; - RGBA_ARGS; + RGBA_ARGS(0); Context2d *context = ObjectWrap::Unwrap(args.This()); RGBA(context->stroke,r,g,b,a); return Undefined(); diff --git a/src/gradient.cc b/src/gradient.cc index a2cff54..645f73e 100644 --- a/src/gradient.cc +++ b/src/gradient.cc @@ -15,6 +15,7 @@ Gradient::Initialize(Handle target) { t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(String::NewSymbol("CanvasGradient")); + NODE_SET_PROTOTYPE_METHOD(t, "addColorStopRGBA", AddColorStopRGBA); target->Set(String::NewSymbol("CanvasGradient"), t->GetFunction()); } @@ -49,12 +50,23 @@ Gradient::New(const Arguments &args) { return ThrowException(Exception::TypeError(String::New("invalid arguments"))); } +Handle +Gradient::AddColorStopRGBA(const Arguments &args) { + HandleScope scope; + if (!args[0]->IsNumber()) \ + return ThrowException(Exception::TypeError(String::New("offset required"))); \ + RGBA_ARGS(1); + Gradient *grad = ObjectWrap::Unwrap(args.This()); + cairo_pattern_add_color_stop_rgba(grad->getPattern(), args[0]->NumberValue(), r, g, b, a); + return Undefined(); +} + Gradient::Gradient(double x0, double y0, double x1, double y1): _x0(x0), _y0(y0), _x1(x1), _y1(y1) { - _grad = cairo_pattern_create_linear(x0, y0, x1, y1); + _pattern = cairo_pattern_create_linear(x0, y0, x1, y1); } Gradient::Gradient(double x0, double y0, double r0, double x1, double y1, double r1): _x0(x0), _y0(y0), _x1(x1), _y1(y1), _r0(r0), _r1(r1) { - _grad = cairo_pattern_create_radial(x0, y0, r0, x1, y1, r1); + _pattern = cairo_pattern_create_radial(x0, y0, r0, x1, y1, r1); } diff --git a/src/gradient.h b/src/gradient.h index 8a79015..097d779 100644 --- a/src/gradient.h +++ b/src/gradient.h @@ -16,12 +16,14 @@ class Gradient: public node::ObjectWrap { public: static void Initialize(Handle target); static Handle New(const Arguments &args); + static Handle AddColorStopRGBA(const Arguments &args); Gradient(double x0, double y0, double x1, double y1); Gradient(double x0, double y0, double r0, double x1, double y1, double r1); + inline cairo_pattern_t *getPattern(){ return _pattern; } private: double _x0, _y0, _x1, _y1, _r0, _r1; - cairo_pattern_t *_grad; + cairo_pattern_t *_pattern; }; #endif \ No newline at end of file