1
0
Fork 0
hvif-light/src/hvif-cairo.c

69 lines
1.5 KiB
C
Raw Normal View History

#include "hvif-light.h"
#include <stdint.h>
#include <assert.h>
#include <cairo.h>
#define INTERNAL_DATASTRUCTURES
#include "hvif-light.c"
#undef INTERNAL_DATASTRUCTURES
/* TODO:
* 1. Draw the path in each shape as outline
* 2. Then do the same for the styles
* 3. Put everything together
*/
void
create_path(cairo_t* cr, hvif_path* path)
{
assert(path->num_points > 0);
for (unsigned i = 0; i < path->num_points; ++i) {
hvif_point p = path->points[3 * i];
hvif_point cp1 = path->points[3 * i + 1];
hvif_point cp2 = path->points[3 * i + 2];
cairo_curve_to(cr, cp1.x, cp1.y, cp2.x, cp2.y, p.x, p.y);
}
if (path->closed)
cairo_close_path(cr);
}
bool
hvif_render_image(const char* filename, hvif_image* image)
{
cairo_surface_t* surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 254, 254);
cairo_t* cr = cairo_create(surface);
cairo_scale(cr, 4.0, 4.0);
cairo_set_line_width(cr, 0.2);
double r = 0;
double delta = 1.0 / image->num_shapes;
for (unsigned i = 0; i < image->num_shapes; ++i) {
cairo_set_source_rgb(cr, r, 0, 0);
r = r + delta;
hvif_shape s = image->shapes[i];
if (s.hinting)
printf("shape %u has hinting\n", i);
else
printf("shape %u has no hinting\n", i);
for (unsigned j = 0; j < s.num_paths; ++j) {
hvif_path* p = &image->paths[s.path_idxs[j]];
create_path(cr, p);
cairo_stroke(cr);
}
}
cairo_destroy(cr);
bool result = true;
if (cairo_surface_write_to_png(surface, filename) != CAIRO_STATUS_SUCCESS)
result = false;
cairo_surface_destroy(surface);
return result;
}