diff --git a/src/context2d.cc b/src/context2d.cc index e2a2704..6ae127b 100644 --- a/src/context2d.cc +++ b/src/context2d.cc @@ -100,6 +100,7 @@ Context2d::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "arc", Arc); NODE_SET_PROTOTYPE_METHOD(t, "setFillRGBA", SetFillRGBA); NODE_SET_PROTOTYPE_METHOD(t, "setStrokeRGBA", SetStrokeRGBA); + proto->SetAccessor(String::NewSymbol("globalCompositeOperation"), GetGlobalCompositeOperation, SetGlobalCompositeOperation); proto->SetAccessor(String::NewSymbol("globalAlpha"), GetGlobalAlpha, SetGlobalAlpha); proto->SetAccessor(String::NewSymbol("miterLimit"), GetMiterLimit, SetMiterLimit); proto->SetAccessor(String::NewSymbol("lineWidth"), GetLineWidth, SetLineWidth); @@ -162,6 +163,66 @@ Context2d::SetGlobalAlpha(Local prop, Local val, const AccessorIn context->globalAlpha = val->NumberValue(); } +/* + * Get global composite operation. + */ + +Handle +Context2d::GetGlobalCompositeOperation(Local prop, const AccessorInfo &info) { + Context2d *context = ObjectWrap::Unwrap(info.This()); + cairo_t *ctx = context->getContext(); + switch (cairo_get_operator(ctx)) { + case CAIRO_OPERATOR_ATOP: + return String::NewSymbol("source-atop"); + case CAIRO_OPERATOR_IN: + return String::NewSymbol("source-in"); + case CAIRO_OPERATOR_OUT: + return String::NewSymbol("source-out"); + case CAIRO_OPERATOR_XOR: + return String::NewSymbol("xor"); + case CAIRO_OPERATOR_DEST_ATOP: + return String::NewSymbol("destination-atop"); + case CAIRO_OPERATOR_DEST_IN: + return String::NewSymbol("destination-in"); + case CAIRO_OPERATOR_DEST_OUT: + return String::NewSymbol("destination-out"); + case CAIRO_OPERATOR_DEST_OVER: + return String::NewSymbol("destination-over"); + default: + return String::NewSymbol("source-over"); + } +} + +/* + * Set global composite operation. + */ + +void +Context2d::SetGlobalCompositeOperation(Local prop, Local val, const AccessorInfo &info) { + Context2d *context = ObjectWrap::Unwrap(info.This()); + cairo_t *ctx = context->getContext(); + String::AsciiValue type(val->ToString()); + if (0 == strcmp("xor", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_XOR); + }else if (0 == strcmp("source-atop", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_ATOP); + } else if (0 == strcmp("source-in", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_IN); + } else if (0 == strcmp("source-out", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_OUT); + } else if (0 == strcmp("destination-atop", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_DEST_ATOP); + } else if (0 == strcmp("destination-in", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_DEST_OVER); + } else if (0 == strcmp("destination-out", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_DEST_OVER); + } else if (0 == strcmp("destination-over", *type)) { + cairo_set_operator(ctx, CAIRO_OPERATOR_DEST_OVER); + } else { + cairo_set_operator(ctx, CAIRO_OPERATOR_OVER); + } +} + /* * Get miter limit. */ @@ -227,7 +288,7 @@ void Context2d::SetLineJoin(Local prop, Local val, const AccessorInfo &info) { Context2d *context = ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->getContext(); - String::AsciiValue type(val); + String::AsciiValue type(val->ToString()); if (0 == strcmp("round", *type)) { cairo_set_line_join(ctx, CAIRO_LINE_JOIN_ROUND); } else if (0 == strcmp("bevel", *type)) { @@ -262,7 +323,7 @@ void Context2d::SetLineCap(Local prop, Local val, const AccessorInfo &info) { Context2d *context = ObjectWrap::Unwrap(info.This()); cairo_t *ctx = context->getContext(); - String::AsciiValue type(val); + String::AsciiValue type(val->ToString()); if (0 == strcmp("round", *type)) { cairo_set_line_cap(ctx, CAIRO_LINE_CAP_ROUND); } else if (0 == strcmp("square", *type)) { diff --git a/src/context2d.h b/src/context2d.h index b9c3695..e5fc5f9 100644 --- a/src/context2d.h +++ b/src/context2d.h @@ -41,11 +41,13 @@ class Context2d: public node::ObjectWrap { static Handle ClearRect(const Arguments &args); static Handle Rect(const Arguments &args); static Handle Arc(const Arguments &args); + static Handle GetGlobalCompositeOperation(Local prop, const AccessorInfo &info); static Handle GetGlobalAlpha(Local prop, const AccessorInfo &info); static Handle GetMiterLimit(Local prop, const AccessorInfo &info); static Handle GetLineCap(Local prop, const AccessorInfo &info); static Handle GetLineJoin(Local prop, const AccessorInfo &info); static Handle GetLineWidth(Local prop, const AccessorInfo &info); + static void SetGlobalCompositeOperation(Local prop, Local val, const AccessorInfo &info); static void SetGlobalAlpha(Local prop, Local val, const AccessorInfo &info); static void SetMiterLimit(Local prop, Local val, const AccessorInfo &info); static void SetLineCap(Local prop, Local val, const AccessorInfo &info);