svg_raster.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* svg_raster: a small freestanding SVG geometry rasterizer.
  2. *
  3. * Shapes are flattened into device-space polylines (path data with every
  4. * SVG command, including curves and elliptical arcs, plus rect/ellipse/
  5. * polygon helpers), transformed by an affine matrix, and then filled with
  6. * the nonzero or even-odd rule or stroked. Coverage is antialiased with
  7. * four vertical subsamples and exact horizontal span coverage, and is
  8. * composited "over" into an RGB565 + 8-bit alpha layer, so a whole <svg>
  9. * can be rendered once and blitted on every repaint.
  10. *
  11. * Nothing here knows about the DOM: browser.c walks the SVG tree, resolves
  12. * paint and transforms, and drives this module. Host test: svg_test.c. */
  13. #ifndef SVG_RASTER_H
  14. #define SVG_RASTER_H
  15. struct svgr_layer {
  16. unsigned short *px; /* RGB565, w*h */
  17. unsigned char *a; /* coverage/alpha, w*h */
  18. int w, h;
  19. };
  20. struct svgr {
  21. float *pt; /* x,y pairs in device space */
  22. unsigned char *fl; /* SVGR_PT_* per point */
  23. unsigned int n, cap;
  24. int overflow;
  25. double m[6]; /* user -> device: a b c d e f */
  26. /* path state, user space */
  27. double cx, cy, sx, sy, lcx, lcy;
  28. char last; /* previous command (upper case) */
  29. int open; /* a subpath is in progress */
  30. unsigned int sp; /* index of the current subpath's first point */
  31. };
  32. #define SVGR_PT_START 1 /* first point of a subpath */
  33. #define SVGR_PT_CLOSED 2 /* set on a subpath's first point when closed */
  34. /* buf must hold cap points: cap * (2 * sizeof(float) + 1) bytes. */
  35. void svgr_init(struct svgr *r, void *buf, unsigned int cap);
  36. void svgr_reset(struct svgr *r, const double m[6]);
  37. void svgr_move(struct svgr *r, double x, double y);
  38. void svgr_line(struct svgr *r, double x, double y);
  39. void svgr_cubic(struct svgr *r, double x1, double y1, double x2, double y2,
  40. double x, double y);
  41. void svgr_quad(struct svgr *r, double x1, double y1, double x, double y);
  42. void svgr_arc(struct svgr *r, double rx, double ry, double rot, int large,
  43. int sweep, double x, double y);
  44. void svgr_close(struct svgr *r);
  45. /* Parse SVG path data ("M10 10h5c..."); returns 0, -1 on a parse error
  46. * (whatever was valid before the error is kept, as browsers do). */
  47. int svgr_path(struct svgr *r, const char *d);
  48. void svgr_rect(struct svgr *r, double x, double y, double w, double h,
  49. double rx, double ry);
  50. void svgr_ellipse(struct svgr *r, double cx, double cy, double rx, double ry);
  51. /* "x1,y1 x2,y2 ..."; close != 0 for <polygon>. */
  52. void svgr_points(struct svgr *r, const char *pts, int close);
  53. /* Composite the current geometry into the layer. alpha 0..255. */
  54. void svgr_fill(struct svgr *r, struct svgr_layer *L, unsigned short color,
  55. unsigned int alpha, int evenodd);
  56. /* width is in device pixels. */
  57. void svgr_stroke(struct svgr *r, struct svgr_layer *L, double width,
  58. unsigned short color, unsigned int alpha);
  59. /* Number/transform helpers shared with the DOM side. */
  60. double svgr_num(const char **pp, int *ok);
  61. /* Multiply: out = a * b (apply b first, then a). */
  62. void svgr_mat_mul(double out[6], const double a[6], const double b[6]);
  63. /* Parse a transform list and post-multiply it onto m. */
  64. void svgr_transform(double m[6], const char *s);
  65. #endif