/* svg_raster: a small freestanding SVG geometry rasterizer. * * Shapes are flattened into device-space polylines (path data with every * SVG command, including curves and elliptical arcs, plus rect/ellipse/ * polygon helpers), transformed by an affine matrix, and then filled with * the nonzero or even-odd rule or stroked. Coverage is antialiased with * four vertical subsamples and exact horizontal span coverage, and is * composited "over" into an RGB565 + 8-bit alpha layer, so a whole * can be rendered once and blitted on every repaint. * * Nothing here knows about the DOM: browser.c walks the SVG tree, resolves * paint and transforms, and drives this module. Host test: svg_test.c. */ #ifndef SVG_RASTER_H #define SVG_RASTER_H struct svgr_layer { unsigned short *px; /* RGB565, w*h */ unsigned char *a; /* coverage/alpha, w*h */ int w, h; }; struct svgr { float *pt; /* x,y pairs in device space */ unsigned char *fl; /* SVGR_PT_* per point */ unsigned int n, cap; int overflow; double m[6]; /* user -> device: a b c d e f */ /* path state, user space */ double cx, cy, sx, sy, lcx, lcy; char last; /* previous command (upper case) */ int open; /* a subpath is in progress */ unsigned int sp; /* index of the current subpath's first point */ }; #define SVGR_PT_START 1 /* first point of a subpath */ #define SVGR_PT_CLOSED 2 /* set on a subpath's first point when closed */ /* buf must hold cap points: cap * (2 * sizeof(float) + 1) bytes. */ void svgr_init(struct svgr *r, void *buf, unsigned int cap); void svgr_reset(struct svgr *r, const double m[6]); void svgr_move(struct svgr *r, double x, double y); void svgr_line(struct svgr *r, double x, double y); void svgr_cubic(struct svgr *r, double x1, double y1, double x2, double y2, double x, double y); void svgr_quad(struct svgr *r, double x1, double y1, double x, double y); void svgr_arc(struct svgr *r, double rx, double ry, double rot, int large, int sweep, double x, double y); void svgr_close(struct svgr *r); /* Parse SVG path data ("M10 10h5c..."); returns 0, -1 on a parse error * (whatever was valid before the error is kept, as browsers do). */ int svgr_path(struct svgr *r, const char *d); void svgr_rect(struct svgr *r, double x, double y, double w, double h, double rx, double ry); void svgr_ellipse(struct svgr *r, double cx, double cy, double rx, double ry); /* "x1,y1 x2,y2 ..."; close != 0 for . */ void svgr_points(struct svgr *r, const char *pts, int close); /* Composite the current geometry into the layer. alpha 0..255. */ void svgr_fill(struct svgr *r, struct svgr_layer *L, unsigned short color, unsigned int alpha, int evenodd); /* width is in device pixels. */ void svgr_stroke(struct svgr *r, struct svgr_layer *L, double width, unsigned short color, unsigned int alpha); /* Number/transform helpers shared with the DOM side. */ double svgr_num(const char **pp, int *ok); /* Multiply: out = a * b (apply b first, then a). */ void svgr_mat_mul(double out[6], const double a[6], const double b[6]); /* Parse a transform list and post-multiply it onto m. */ void svgr_transform(double m[6], const char *s); #endif