Skip to content

Commit

Permalink
ignore ctx.restore() if in the initial state
Browse files Browse the repository at this point in the history
calling cairo_restore on the initial state makes all drawing operations
afterwards do nothing, which does not match browser behavior

also merges save+saveState, restore+restoreState functions
  • Loading branch information
chearon committed Oct 29, 2016
1 parent a19f790 commit 8ef00a4
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,13 @@ Context2d::~Context2d() {

void
Context2d::save() {
cairo_save(_context);
saveState();
if (stateno < CANVAS_MAX_STATES) {
cairo_save(_context);
states[++stateno] = (canvas_state_t *) malloc(sizeof(canvas_state_t));
memcpy(states[stateno], state, sizeof(canvas_state_t));
states[stateno]->fontDescription = pango_font_description_copy(states[stateno-1]->fontDescription);
state = states[stateno];
}
}

/*
Expand All @@ -201,35 +206,14 @@ Context2d::save() {

void
Context2d::restore() {
cairo_restore(_context);
restoreState();
}

/*
* Save the current state.
*/

void
Context2d::saveState() {
if (stateno == CANVAS_MAX_STATES) return;
states[++stateno] = (canvas_state_t *) malloc(sizeof(canvas_state_t));
memcpy(states[stateno], state, sizeof(canvas_state_t));
states[stateno]->fontDescription = pango_font_description_copy(states[stateno-1]->fontDescription);
state = states[stateno];
}

/*
* Restore state.
*/

void
Context2d::restoreState() {
if (0 == stateno) return;
pango_font_description_free(states[stateno]->fontDescription);
free(states[stateno]);
states[stateno] = NULL;
state = states[--stateno];
pango_layout_set_font_description(_layout, state->fontDescription);
if (stateno > 0) {
cairo_restore(_context);
pango_font_description_free(states[stateno]->fontDescription);
free(states[stateno]);
states[stateno] = NULL;
state = states[--stateno];
pango_layout_set_font_description(_layout, state->fontDescription);
}
}

/*
Expand Down

0 comments on commit 8ef00a4

Please sign in to comment.