Skip to content

Commit

Permalink
Refactoring plutovg_gradient_paint_t
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Sep 22, 2024
1 parent 18276b5 commit a7af517
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
2 changes: 2 additions & 0 deletions source/plutovg-blend.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ static void plutovg_blend_gradient(plutovg_canvas_t* canvas, const plutovg_gradi
for(i = 0; i < nstops - 1; i++) {
curr = (start + i);
next = (start + i + 1);
if(curr->offset == next->offset)
continue;
delta = 1.f / (next->offset - curr->offset);
next_color = combine_color_with_opacity(&next->color, opacity);
while(fpos < next->offset && pos < COLOR_TABLE_SIZE) {
Expand Down
39 changes: 11 additions & 28 deletions source/plutovg-paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,14 @@ plutovg_paint_t* plutovg_paint_create_color(const plutovg_color_t* color)
return plutovg_paint_create_rgba(color->r, color->g, color->b, color->a);
}

static void plutovg_gradient_stops_init(plutovg_gradient_paint_t* gradient, const plutovg_gradient_stop_t* stops, int nstops)
static plutovg_gradient_paint_t* plutovg_gradient_create(plutovg_gradient_type_t type, plutovg_spread_method_t spread, const plutovg_gradient_stop_t* stops, int nstops, const plutovg_matrix_t* matrix)
{
if(nstops > 2) {
gradient->stops = malloc(nstops * sizeof(plutovg_gradient_stop_t));
} else {
gradient->stops = gradient->embedded_stops;
}
plutovg_gradient_paint_t* gradient = plutovg_paint_create(PLUTOVG_PAINT_TYPE_GRADIENT, sizeof(plutovg_gradient_paint_t) + nstops * sizeof(plutovg_gradient_stop_t));
gradient->type = type;
gradient->spread = spread;
gradient->matrix = matrix ? *matrix : PLUTOVG_IDENTITY_MATRIX;
gradient->stops = (plutovg_gradient_stop_t*)(gradient + 1);
gradient->nstops = nstops;

float prev_offset = 0.f;
for(int i = 0; i < nstops; ++i) {
Expand All @@ -395,43 +396,28 @@ static void plutovg_gradient_stops_init(plutovg_gradient_paint_t* gradient, cons
prev_offset = gradient->stops[i].offset;
}

gradient->nstops = nstops;
}

static void plutovg_gradient_stops_destroy(plutovg_gradient_paint_t* gradient)
{
if(gradient->stops == gradient->embedded_stops)
return;
free(gradient->stops);
return gradient;
}

plutovg_paint_t* plutovg_paint_create_linear_gradient(float x1, float y1, float x2, float y2, plutovg_spread_method_t spread, const plutovg_gradient_stop_t* stops, int nstops, const plutovg_matrix_t* matrix)
{
plutovg_gradient_paint_t* gradient = plutovg_paint_create(PLUTOVG_PAINT_TYPE_GRADIENT, sizeof(plutovg_gradient_paint_t));
gradient->type = PLUTOVG_GRADIENT_TYPE_LINEAR;
gradient->spread = spread;
gradient->matrix = matrix ? *matrix : PLUTOVG_IDENTITY_MATRIX;
plutovg_gradient_paint_t* gradient = plutovg_gradient_create(PLUTOVG_GRADIENT_TYPE_LINEAR, spread, stops, nstops, matrix);
gradient->values[0] = x1;
gradient->values[1] = y1;
gradient->values[2] = x2;
gradient->values[3] = y2;
plutovg_gradient_stops_init(gradient, stops, nstops);
return &gradient->base;
}

plutovg_paint_t* plutovg_paint_create_radial_gradient(float cx, float cy, float cr, float fx, float fy, float fr, plutovg_spread_method_t spread, const plutovg_gradient_stop_t* stops, int nstops, const plutovg_matrix_t* matrix)
{
plutovg_gradient_paint_t* gradient = plutovg_paint_create(PLUTOVG_PAINT_TYPE_GRADIENT, sizeof(plutovg_gradient_paint_t));
gradient->type = PLUTOVG_GRADIENT_TYPE_RADIAL;
gradient->spread = spread;
gradient->matrix = matrix ? *matrix : PLUTOVG_IDENTITY_MATRIX;
plutovg_gradient_paint_t* gradient = plutovg_gradient_create(PLUTOVG_GRADIENT_TYPE_RADIAL, spread, stops, nstops, matrix);
gradient->values[0] = cx;
gradient->values[1] = cy;
gradient->values[2] = cr;
gradient->values[3] = fx;
gradient->values[4] = fy;
gradient->values[5] = fr;
plutovg_gradient_stops_init(gradient, stops, nstops);
return &gradient->base;
}

Expand All @@ -458,10 +444,7 @@ void plutovg_paint_destroy(plutovg_paint_t* paint)
if(paint == NULL)
return;
if(--paint->ref_count == 0) {
if(paint->type == PLUTOVG_PAINT_TYPE_GRADIENT) {
plutovg_gradient_paint_t* gradient = (plutovg_gradient_paint_t*)(paint);
plutovg_gradient_stops_destroy(gradient);
} else if(paint->type == PLUTOVG_PAINT_TYPE_TEXTURE) {
if(paint->type == PLUTOVG_PAINT_TYPE_TEXTURE) {
plutovg_texture_paint_t* texture = (plutovg_texture_paint_t*)(paint);
plutovg_surface_destroy(texture->surface);
}
Expand Down
3 changes: 1 addition & 2 deletions source/plutovg-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ typedef struct {
plutovg_gradient_type_t type;
plutovg_spread_method_t spread;
plutovg_matrix_t matrix;
float values[6];
plutovg_gradient_stop_t embedded_stops[2];
plutovg_gradient_stop_t* stops;
int nstops;
float values[6];
} plutovg_gradient_paint_t;

typedef struct {
Expand Down

0 comments on commit a7af517

Please sign in to comment.