Browse Source

save()/restore() fixed

v1.x
Tj Holowaychuk 15 years ago
parent
commit
223f684821
  1. 15
      src/canvas.h
  2. 14
      src/context2d.cc
  3. 5
      src/context2d.h

15
src/canvas.h

@ -13,6 +13,17 @@
#include <node_object_wrap.h>
#include <cairo.h>
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:

14
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<Context2d>(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<Context2d>(args.This());
cairo_restore(context->getContext());
context->restoreState();
return Undefined();
}

5
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;

Loading…
Cancel
Save