Skip to content

Commit

Permalink
Add some useful macros
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Aug 16, 2024
1 parent c7794dc commit 400d098
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 67 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ int main(void)
plutovg_surface_t* surface = plutovg_surface_create(width, height);
plutovg_canvas_t* canvas = plutovg_canvas_create(surface);

float center_x = width * 0.5;
float center_y = height * 0.5;
float center_x = width / 2.f;
float center_y = height / 2.f;
float face_radius = 70;
float eye_radius = 10;
float mouth_radius = 50;
Expand All @@ -36,10 +36,8 @@ int main(void)
float eye_x = center_x - eye_offset_x;
float eye_y = center_y - eye_offset_y;

const float pi = 3.14159265358979323846;

plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, center_x, center_y, face_radius, 0, 2 * pi, 0);
plutovg_canvas_arc(canvas, center_x, center_y, face_radius, 0, 2 * PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 1, 1, 0);
plutovg_canvas_fill_preserve(canvas);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
Expand All @@ -48,27 +46,27 @@ int main(void)
plutovg_canvas_restore(canvas);

plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, eye_x, eye_y, eye_radius, 0, 2 * pi, 0);
plutovg_canvas_arc(canvas, center_x + eye_offset_x, eye_y, eye_radius, 0, 2 * pi, 0);
plutovg_canvas_arc(canvas, eye_x, eye_y, eye_radius, 0, 2 * PLUTOVG_PI, 0);
plutovg_canvas_arc(canvas, center_x + eye_offset_x, eye_y, eye_radius, 0, 2 * PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_fill(canvas);
plutovg_canvas_restore(canvas);

plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, center_x, center_y, mouth_radius, 0, pi, 0);
plutovg_canvas_arc(canvas, center_x, center_y, mouth_radius, 0, PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_set_line_width(canvas, 5);
plutovg_canvas_stroke(canvas);
plutovg_canvas_restore(canvas);

plutovg_surface_write_to_png(surface, "smiley.png");
plutovg_surface_destroy(surface);
plutovg_canvas_destroy(canvas);
plutovg_surface_destroy(surface);
return 0;
}
```
output :
output:
![smiley.png](smiley.png)
Expand Down
16 changes: 7 additions & 9 deletions examples/smiley.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ int main(void)
plutovg_surface_t* surface = plutovg_surface_create(width, height);
plutovg_canvas_t* canvas = plutovg_canvas_create(surface);

float center_x = width * 0.5;
float center_y = height * 0.5;
float center_x = width / 2.f;
float center_y = height / 2.f;
float face_radius = 70;
float eye_radius = 10;
float mouth_radius = 50;
Expand All @@ -18,10 +18,8 @@ int main(void)
float eye_x = center_x - eye_offset_x;
float eye_y = center_y - eye_offset_y;

const float pi = 3.14159265358979323846;

plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, center_x, center_y, face_radius, 0, 2 * pi, 0);
plutovg_canvas_arc(canvas, center_x, center_y, face_radius, 0, 2 * PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 1, 1, 0);
plutovg_canvas_fill_preserve(canvas);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
Expand All @@ -30,21 +28,21 @@ int main(void)
plutovg_canvas_restore(canvas);

plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, eye_x, eye_y, eye_radius, 0, 2 * pi, 0);
plutovg_canvas_arc(canvas, center_x + eye_offset_x, eye_y, eye_radius, 0, 2 * pi, 0);
plutovg_canvas_arc(canvas, eye_x, eye_y, eye_radius, 0, 2 * PLUTOVG_PI, 0);
plutovg_canvas_arc(canvas, center_x + eye_offset_x, eye_y, eye_radius, 0, 2 * PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_fill(canvas);
plutovg_canvas_restore(canvas);

plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, center_x, center_y, mouth_radius, 0, pi, 0);
plutovg_canvas_arc(canvas, center_x, center_y, mouth_radius, 0, PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_set_line_width(canvas, 5);
plutovg_canvas_stroke(canvas);
plutovg_canvas_restore(canvas);

plutovg_surface_write_to_png(surface, "smiley.png");
plutovg_surface_destroy(surface);
plutovg_canvas_destroy(canvas);
plutovg_surface_destroy(surface);
return 0;
}
30 changes: 30 additions & 0 deletions include/plutovg.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ typedef void (*plutovg_destroy_func_t)(void* closure);
*/
typedef void (*plutovg_write_func_t)(void* closure, void* data, int size);

#define PLUTOVG_PI 3.14159265358979323846f
#define PLUTOVG_TWO_PI 6.28318530717958647693f
#define PLUTOVG_HALF_PI 1.57079632679489661923f
#define PLUTOVG_SQRT2 1.41421356237309504880f
#define PLUTOVG_KAPPA 0.55228474983079339840f

#define PLUTOVG_DEG2RAD(x) ((x) * (PLUTOVG_PI / 180.0f))
#define PLUTOVG_RAD2DEG(x) ((x) * (180.0f / PLUTOVG_PI))

/**
* @brief A structure representing a point in 2D space.
*/
Expand All @@ -91,6 +100,8 @@ typedef struct plutovg_point {
float y; ///< The y-coordinate of the point.
} plutovg_point_t;

#define PLUTOVG_MAKE_POINT(x, y) ((plutovg_point_t){x, y})

/**
* @brief A structure representing a rectangle in 2D space.
*/
Expand All @@ -101,6 +112,8 @@ typedef struct plutovg_rect {
float h; ///< The height of the rectangle.
} plutovg_rect_t;

#define PLUTOVG_MAKE_RECT(x, y, w, h) ((plutovg_rect_t){x, y, w, h})

/**
* @brief A structure representing a 2D transformation matrix.
*/
Expand All @@ -113,6 +126,12 @@ typedef struct plutovg_matrix {
float f; ///< The vertical translation offset.
} plutovg_matrix_t;

#define PLUTOVG_MAKE_MATRIX(a, b, c, d, e, f) ((plutovg_matrix_t){a, b, c, d, e, f})

#define PLUTOVG_MAKE_SCALE(x, y) PLUTOVG_MAKE_MATRIX(x, 0, 0, y, 0, 0)
#define PLUTOVG_MAKE_TRANSLATE(x, y) PLUTOVG_MAKE_MATRIX(1, 0, 0, 1, x, y)
#define PLUTOVG_IDENTITY_MATRIX PLUTOVG_MAKE_MATRIX(1, 0, 0, 1, 0, 0)

/**
* @brief Initializes a 2D transformation matrix.
* @param matrix A pointer to the `plutovg_matrix_t` object to be initialized.
Expand Down Expand Up @@ -1040,6 +1059,17 @@ typedef struct plutovg_color {
float a; ///< Alpha (opacity) component (0 to 1).
} plutovg_color_t;

#define PLUTOVG_MAKE_COLOR(r, g, b, a) ((plutovg_color_t){r, g, b, a})

#define PLUTOVG_BLACK_COLOR PLUTOVG_MAKE_COLOR(0, 0, 0, 1)
#define PLUTOVG_WHITE_COLOR PLUTOVG_MAKE_COLOR(1, 1, 1, 1)
#define PLUTOVG_RED_COLOR PLUTOVG_MAKE_COLOR(1, 0, 0, 1)
#define PLUTOVG_GREEN_COLOR PLUTOVG_MAKE_COLOR(0, 1, 0, 1)
#define PLUTOVG_BLUE_COLOR PLUTOVG_MAKE_COLOR(0, 0, 1, 1)
#define PLUTOVG_YELLOW_COLOR PLUTOVG_MAKE_COLOR(1, 1, 0, 1)
#define PLUTOVG_CYAN_COLOR PLUTOVG_MAKE_COLOR(0, 1, 1, 1)
#define PLUTOVG_MAGENTA_COLOR PLUTOVG_MAKE_COLOR(1, 0, 1, 1)

/**
* @brief Defines the type of texture, either plain or tiled.
*/
Expand Down
19 changes: 9 additions & 10 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@ project('plutovg', 'c',
)

plutovg_sources = [
'source/plutovg-paint.c',
'source/plutovg-blend.c',
'source/plutovg-canvas.c',
'source/plutovg-font.c',
'source/plutovg-matrix.c',
'source/plutovg-paint.c',
'source/plutovg-path.c',
'source/plutovg-font.c',
'source/plutovg-surface.c',
'source/plutovg-canvas.c',
'source/plutovg-rasterize.c',
'source/plutovg-blend.c',
'source/plutovg-surface.c',
'source/plutovg-ft-math.c',
'source/plutovg-ft-raster.c',
'source/plutovg-ft-stroker.c',
'source/plutovg-ft-math.c'
'source/plutovg-ft-stroker.c'
]

plutovg_compile_args = []
if get_option('default_library') == 'static'
plutovg_compile_args += ['-DPLUTOVG_BUILD_STATIC']
add_project_arguments('-DPLUTOVG_BUILD_STATIC', language: ['c', 'cpp'])
endif

plutovg_lib = library('plutovg', plutovg_sources,
include_directories: include_directories('include', 'source', 'stb'),
version: meson.project_version(),
c_args: ['-DPLUTOVG_BUILD'],
cpp_args: ['-DPLUTOVG_BUILD'],
c_args: ['-DPLUTOVG_BUILD'] + plutovg_compile_args,
cpp_args: ['-DPLUTOVG_BUILD'] + plutovg_compile_args,
link_args: ['-lm'],
gnu_symbol_visibility: 'hidden',
install: true
Expand Down
16 changes: 7 additions & 9 deletions source/plutovg-canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static plutovg_paint_t* plutovg_paint_default(void)
{
static PLUTOVG_THREAD_LOCAL plutovg_solid_paint_t paint = {
{1, PLUTOVG_PAINT_TYPE_COLOR},
PLUTOVG_DEFAULT_COLOR
PLUTOVG_BLACK_COLOR
};

return plutovg_paint_reference(&paint.base);
Expand All @@ -44,10 +44,10 @@ static plutovg_paint_t* plutovg_paint_default(void)
static void plutovg_state_reset(plutovg_state_t* state)
{
plutovg_paint_destroy(state->paint);
plutovg_font_face_destroy(state->font_face);
plutovg_matrix_init_identity(&state->matrix);
plutovg_stroke_data_reset(&state->stroke);
plutovg_span_buffer_reset(&state->clip_spans);
plutovg_matrix_init_identity(&state->matrix);
plutovg_font_face_destroy(state->font_face);
state->paint = plutovg_paint_default();
state->font_face = NULL;
state->font_size = 12.f;
Expand All @@ -73,7 +73,8 @@ static void plutovg_state_copy(plutovg_state_t* state, const plutovg_state_t* so

static plutovg_state_t* plutovg_state_create(void)
{
plutovg_state_t* state = calloc(1, sizeof(plutovg_state_t));
plutovg_state_t* state = malloc(sizeof(plutovg_state_t));
memset(state, 0, sizeof(plutovg_state_t));
plutovg_state_reset(state);
return state;
}
Expand All @@ -91,13 +92,10 @@ plutovg_canvas_t* plutovg_canvas_create(plutovg_surface_t* surface)
plutovg_canvas_t* canvas = malloc(sizeof(plutovg_canvas_t));
canvas->ref_count = 1;
canvas->surface = plutovg_surface_reference(surface);
canvas->state = plutovg_state_create();
canvas->path = plutovg_path_create();
canvas->state = plutovg_state_create();
canvas->freed_state = NULL;
canvas->clip_rect.x = 0;
canvas->clip_rect.y = 0;
canvas->clip_rect.w = surface->width;
canvas->clip_rect.h = surface->height;
canvas->clip_rect = PLUTOVG_MAKE_RECT(0, 0, surface->width, surface->height);
plutovg_span_buffer_init(&canvas->clip_spans);
plutovg_span_buffer_init(&canvas->fill_spans);
return canvas;
Expand Down
32 changes: 14 additions & 18 deletions source/plutovg-path.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ void plutovg_path_quad_to(plutovg_path_t* path, float x1, float y1, float x2, fl
{
float current_x, current_y;
plutovg_path_get_current_point(path, &current_x, &current_y);
if(path->elements.size == 0)
plutovg_path_move_to(path, 0, 0);
float cx = 2.f / 3.f * x1 + 1.f / 3.f * current_x;
float cy = 2.f / 3.f * y1 + 1.f / 3.f * current_y;
float cx1 = 2.f / 3.f * x1 + 1.f / 3.f * x2;
Expand All @@ -145,8 +143,6 @@ void plutovg_path_arc_to(plutovg_path_t* path, float rx, float ry, float angle,
{
float current_x, current_y;
plutovg_path_get_current_point(path, &current_x, &current_y);
if(path->elements.size == 0)
plutovg_path_move_to(path, 0, 0);
if(rx == 0.f || ry == 0.f || (current_x == x && current_y == y)) {
plutovg_path_line_to(path, x, y);
return;
Expand Down Expand Up @@ -201,12 +197,12 @@ void plutovg_path_arc_to(plutovg_path_t* path, float rx, float ry, float angle,
float th2 = atan2f(y2 - cy1, x2 - cx1);
float th_arc = th2 - th1;
if(th_arc < 0 && sweep_flag)
th_arc += 2.f * PLUTOVG_PI;
th_arc += PLUTOVG_TWO_PI;
else if(th_arc > 0 && !sweep_flag)
th_arc -= 2.f * PLUTOVG_PI;
th_arc -= PLUTOVG_TWO_PI;
plutovg_matrix_init_rotate(&matrix, angle);
plutovg_matrix_scale(&matrix, rx, ry);
int segments = ceilf(fabsf(th_arc / (PLUTOVG_PI * 0.5f + 0.001f)));
int segments = ceilf(fabsf(th_arc / (PLUTOVG_HALF_PI + 0.001f)));
for(int i = 0; i < segments; i++) {
float th_start = th1 + i * th_arc / segments;
float th_end = th1 + (i + 1) * th_arc / segments;
Expand All @@ -215,19 +211,19 @@ void plutovg_path_arc_to(plutovg_path_t* path, float rx, float ry, float angle,
float x3 = cosf(th_end) + cx1;
float y3 = sinf(th_end) + cy1;

float x2 = x3 + t * sinf(th_end);
float y2 = y3 - t * cosf(th_end);
float cp2x = x3 + t * sinf(th_end);
float cp2y = y3 - t * cosf(th_end);

float x1 = cosf(th_start) - t * sinf(th_start);
float y1 = sinf(th_start) + t * cosf(th_start);
float cp1x = cosf(th_start) - t * sinf(th_start);
float cp1y = sinf(th_start) + t * cosf(th_start);

x1 += cx1;
y1 += cy1;
cp1x += cx1;
cp1y += cy1;

plutovg_matrix_map(&matrix, x1, y1, &x1, &y1);
plutovg_matrix_map(&matrix, x2, y2, &x2, &y2);
plutovg_matrix_map(&matrix, cp1x, cp1y, &cp1x, &cp1y);
plutovg_matrix_map(&matrix, cp2x, cp2y, &cp2x, &cp2y);
plutovg_matrix_map(&matrix, x3, y3, &x3, &y3);
plutovg_path_cubic_to(path, x1, y1, x2, y2, x3, y3);
plutovg_path_cubic_to(path, cp1x, cp1y, cp2x, cp2y, x3, y3);
}
}

Expand Down Expand Up @@ -340,7 +336,7 @@ void plutovg_path_add_arc(plutovg_path_t* path, float cx, float cy, float r, flo
da += PLUTOVG_TWO_PI * (ccw ? -1 : 1);
}

int seg_n = (int)(ceilf(fabsf(da) / PLUTOVG_HALF_PI));
int seg_n = (int)(ceilf(fabsf(da) / PLUTOVG_HALF_PI + 0.001f));
float a = a0;
float ax = cx + cosf(a) * r;
float ay = cy + sinf(a) * r;
Expand Down Expand Up @@ -1015,7 +1011,7 @@ bool plutovg_path_parse(plutovg_path_t* path, const char* data, int length)
values[4] += current_y;
}

plutovg_path_arc_to(path, values[0], values[1], values[2] * PLUTOVG_PI / 180.f, flags[0], flags[1], values[3], values[4]);
plutovg_path_arc_to(path, values[0], values[1], PLUTOVG_DEG2RAD(values[2]), flags[0], flags[1], values[3], values[4]);
current_x = values[3];
current_y = values[4];
} else if(command == 'Z' || command == 'z'){
Expand Down
12 changes: 1 addition & 11 deletions source/plutovg-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ typedef struct plutovg_state {
struct plutovg_canvas {
int ref_count;
plutovg_surface_t* surface;
plutovg_path_t* path;
plutovg_state_t* state;
plutovg_state_t* freed_state;
plutovg_rect_t clip_rect;
plutovg_span_buffer_t clip_spans;
plutovg_span_buffer_t fill_spans;
plutovg_path_t* path;
};

void plutovg_span_buffer_init(plutovg_span_buffer_t* span_buffer);
Expand All @@ -146,12 +146,6 @@ void plutovg_span_buffer_intersect(plutovg_span_buffer_t* span_buffer, const plu
void plutovg_rasterize(plutovg_span_buffer_t* span_buffer, const plutovg_path_t* path, const plutovg_matrix_t* matrix, const plutovg_rect_t* clip_rect, const plutovg_stroke_data_t* stroke_data, plutovg_fill_rule_t winding);
void plutovg_blend(plutovg_canvas_t* canvas, const plutovg_span_buffer_t* span_buffer);

#define PLUTOVG_SQRT2 1.41421356237309504880f
#define PLUTOVG_PI 3.14159265358979323846f
#define PLUTOVG_TWO_PI 6.28318530717958647693f
#define PLUTOVG_HALF_PI 1.57079632679489661923f
#define PLUTOVG_KAPPA 0.55228474983079339840f

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define PLUTOVG_THREAD_LOCAL _Thread_local
#elif defined(_MSC_VER)
Expand All @@ -177,10 +171,6 @@ void plutovg_blend(plutovg_canvas_t* canvas, const plutovg_span_buffer_t* span_b
#define plutovg_green(c) (((c) >> 8) & 0xff)
#define plutovg_blue(c) (((c) >> 0) & 0xff)

#define PLUTOVG_DEFAULT_STROKE_STYLE ((plutovg_stroke_style_t){1, PLUTOVG_LINE_CAP_BUTT, PLUTOVG_LINE_JOIN_MITER, 10})
#define PLUTOVG_IDENTITY_MATRIX ((plutovg_matrix_t){1, 0, 0, 1, 0, 0})
#define PLUTOVG_DEFAULT_COLOR ((plutovg_color_t){0, 0, 0, 1})

#define plutovg_array_init(array) \
do { \
(array).data = NULL; \
Expand Down

0 comments on commit 400d098

Please sign in to comment.