diff --git a/src/canvas.h b/src/canvas.h index e5010f4..bdf0d5b 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -13,6 +13,17 @@ #include #include +using namespace v8; + +/* + * Maxmimum states per context. + * TODO: remove/resize + */ + +#ifndef CANVAS_MAX_STATES +#define CANVAS_MAX_STATES 64 +#endif + /* * RGBA arg assertions. */ @@ -31,7 +42,9 @@ float b = args[N+2]->Int32Value(); \ float a = args[N+3]->NumberValue(); -using namespace v8; +/* + * Canvas. + */ class Canvas: public node::ObjectWrap { public: diff --git a/src/context2d.cc b/src/context2d.cc index 9bae2a6..5b3a0c0 100644 --- a/src/context2d.cc +++ b/src/context2d.cc @@ -148,17 +148,25 @@ Context2d::~Context2d() { cairo_destroy(_context); } +/* + * Save the current state. + */ + void Context2d::saveState() { - printf("state %d -> %d\n", stateno, stateno + 1); + if (stateno == CANVAS_MAX_STATES) return; states[++stateno] = (canvas_state_t *) malloc(sizeof(canvas_state_t)); memcpy(states[stateno], state, sizeof(canvas_state_t)); state = states[stateno]; } +/* + * Restore state. + */ + void Context2d::restoreState() { - printf("state %d -> %d\n", stateno, stateno - 1); + if (0 == stateno) return; state = states[--stateno]; } @@ -548,6 +556,7 @@ Context2d::Save(const Arguments &args) { HandleScope scope; Context2d *context = ObjectWrap::Unwrap(args.This()); cairo_save(context->getContext()); + context->saveState(); return Undefined(); } @@ -560,6 +569,7 @@ Context2d::Restore(const Arguments &args) { HandleScope scope; Context2d *context = ObjectWrap::Unwrap(args.This()); cairo_restore(context->getContext()); + context->restoreState(); return Undefined(); } diff --git a/src/context2d.h b/src/context2d.h index 2c417c1..d7376cf 100644 --- a/src/context2d.h +++ b/src/context2d.h @@ -20,7 +20,7 @@ typedef struct { } rgba_t; /* - * State struct. + * State struct used in conjunction with Save()/Restore(). */ typedef struct { @@ -32,9 +32,8 @@ typedef struct { class Context2d: public node::ObjectWrap { public: - // TODO: resize short stateno; - canvas_state_t *states[64]; + canvas_state_t *states[CANVAS_MAX_STATES]; canvas_state_t *state; rgba_t shadow; double shadowBlur;