You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svg.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <svg.hpp>
  2. #include <math.hpp>
  3. // #define DEBUG_ONLY(x) x
  4. #define DEBUG_ONLY(x)
  5. namespace rack {
  6. static NVGcolor getNVGColor(uint32_t color) {
  7. return nvgRGBA(
  8. (color >> 0) & 0xff,
  9. (color >> 8) & 0xff,
  10. (color >> 16) & 0xff,
  11. (color >> 24) & 0xff);
  12. }
  13. static NVGpaint getPaint(NVGcontext* vg, NSVGpaint* p) {
  14. assert(p->type == NSVG_PAINT_LINEAR_GRADIENT || p->type == NSVG_PAINT_RADIAL_GRADIENT);
  15. NSVGgradient* g = p->gradient;
  16. assert(g->nstops >= 1);
  17. NVGcolor icol = getNVGColor(g->stops[0].color);
  18. NVGcolor ocol = getNVGColor(g->stops[g->nstops - 1].color);
  19. float inverse[6];
  20. nvgTransformInverse(inverse, g->xform);
  21. DEBUG_ONLY(printf(" inverse: %f %f %f %f %f %f\n", inverse[0], inverse[1], inverse[2], inverse[3], inverse[4], inverse[5]);)
  22. math::Vec s, e;
  23. DEBUG_ONLY(printf(" sx: %f sy: %f ex: %f ey: %f\n", s.x, s.y, e.x, e.y);)
  24. // Is it always the case that the gradient should be transformed from (0, 0) to (0, 1)?
  25. nvgTransformPoint(&s.x, &s.y, inverse, 0, 0);
  26. nvgTransformPoint(&e.x, &e.y, inverse, 0, 1);
  27. DEBUG_ONLY(printf(" sx: %f sy: %f ex: %f ey: %f\n", s.x, s.y, e.x, e.y);)
  28. NVGpaint paint;
  29. if (p->type == NSVG_PAINT_LINEAR_GRADIENT)
  30. paint = nvgLinearGradient(vg, s.x, s.y, e.x, e.y, icol, ocol);
  31. else
  32. paint = nvgRadialGradient(vg, s.x, s.y, 0.0, 160, icol, ocol);
  33. return paint;
  34. }
  35. /** Returns the parameterized value of the line p2--p3 where it intersects with p0--p1 */
  36. static float getLineCrossing(math::Vec p0, math::Vec p1, math::Vec p2, math::Vec p3) {
  37. math::Vec b = p2.minus(p0);
  38. math::Vec d = p1.minus(p0);
  39. math::Vec e = p3.minus(p2);
  40. float m = d.x * e.y - d.y * e.x;
  41. // Check if lines are parallel, or if either pair of points are equal
  42. if (std::abs(m) < 1e-6)
  43. return NAN;
  44. return -(d.x * b.y - d.y * b.x) / m;
  45. }
  46. void svgDraw(NVGcontext* vg, NSVGimage* svg) {
  47. DEBUG_ONLY(printf("new image: %g x %g px\n", svg->width, svg->height);)
  48. int shapeIndex = 0;
  49. // Iterate shape linked list
  50. for (NSVGshape* shape = svg->shapes; shape; shape = shape->next, shapeIndex++) {
  51. DEBUG_ONLY(printf(" new shape: %d id \"%s\", fillrule %d, from (%f, %f) to (%f, %f)\n", shapeIndex, shape->id, shape->fillRule, shape->bounds[0], shape->bounds[1], shape->bounds[2], shape->bounds[3]);)
  52. // Visibility
  53. if (!(shape->flags & NSVG_FLAGS_VISIBLE))
  54. continue;
  55. nvgSave(vg);
  56. // Opacity
  57. if (shape->opacity < 1.0)
  58. nvgGlobalAlpha(vg, shape->opacity);
  59. // Build path
  60. nvgBeginPath(vg);
  61. // Iterate path linked list
  62. for (NSVGpath* path = shape->paths; path; path = path->next) {
  63. DEBUG_ONLY(printf(" new path: %d points, %s, from (%f, %f) to (%f, %f)\n", path->npts, path->closed ? "closed" : "open", path->bounds[0], path->bounds[1], path->bounds[2], path->bounds[3]);)
  64. nvgMoveTo(vg, path->pts[0], path->pts[1]);
  65. for (int i = 1; i < path->npts; i += 3) {
  66. float* p = &path->pts[2 * i];
  67. nvgBezierTo(vg, p[0], p[1], p[2], p[3], p[4], p[5]);
  68. // nvgLineTo(vg, p[4], p[5]);
  69. DEBUG_ONLY(printf(" bezier (%f, %f) to (%f, %f)\n", p[-2], p[-1], p[4], p[5]);)
  70. }
  71. // Close path
  72. if (path->closed)
  73. nvgClosePath(vg);
  74. // Compute whether this is a hole or a solid.
  75. // Assume that no paths are crossing (usually true for normal SVG graphics).
  76. // Also assume that the topology is the same if we use straight lines rather than Beziers (not always the case but usually true).
  77. // Using the even-odd fill rule, if we draw a line from a point on the path to a point outside the boundary (e.g. top left) and count the number of times it crosses another path, the parity of this count determines whether the path is a hole (odd) or solid (even).
  78. int crossings = 0;
  79. math::Vec p0 = math::Vec(path->pts[0], path->pts[1]);
  80. math::Vec p1 = math::Vec(path->bounds[0] - 1.0, path->bounds[1] - 1.0);
  81. // Iterate all other paths
  82. for (NSVGpath* path2 = shape->paths; path2; path2 = path2->next) {
  83. if (path2 == path)
  84. continue;
  85. // Iterate all lines on the path
  86. if (path2->npts < 4)
  87. continue;
  88. for (int i = 1; i < path2->npts + 3; i += 3) {
  89. float* p = &path2->pts[2 * i];
  90. // The previous point
  91. math::Vec p2 = math::Vec(p[-2], p[-1]);
  92. // The current point
  93. math::Vec p3 = (i < path2->npts) ? math::Vec(p[4], p[5]) : math::Vec(path2->pts[0], path2->pts[1]);
  94. float crossing = getLineCrossing(p0, p1, p2, p3);
  95. float crossing2 = getLineCrossing(p2, p3, p0, p1);
  96. if (0.0 <= crossing && crossing < 1.0 && 0.0 <= crossing2) {
  97. crossings++;
  98. }
  99. }
  100. }
  101. if (crossings % 2 == 0)
  102. nvgPathWinding(vg, NVG_SOLID);
  103. else
  104. nvgPathWinding(vg, NVG_HOLE);
  105. /*
  106. // Shoelace algorithm for computing the area, and thus the winding direction
  107. float area = 0.0;
  108. math::Vec p0 = math::Vec(path->pts[0], path->pts[1]);
  109. for (int i = 1; i < path->npts; i += 3) {
  110. float *p = &path->pts[2*i];
  111. math::Vec p1 = (i < path->npts) ? math::Vec(p[4], p[5]) : math::Vec(path->pts[0], path->pts[1]);
  112. area += 0.5 * (p1.x - p0.x) * (p1.y + p0.y);
  113. printf("%f %f, %f %f\n", p0.x, p0.y, p1.x, p1.y);
  114. p0 = p1;
  115. }
  116. printf("%f\n", area);
  117. if (area < 0.0)
  118. nvgPathWinding(vg, NVG_CCW);
  119. else
  120. nvgPathWinding(vg, NVG_CW);
  121. */
  122. }
  123. // Fill shape
  124. if (shape->fill.type) {
  125. switch (shape->fill.type) {
  126. case NSVG_PAINT_COLOR: {
  127. NVGcolor color = getNVGColor(shape->fill.color);
  128. nvgFillColor(vg, color);
  129. DEBUG_ONLY(printf(" fill color (%g, %g, %g, %g)\n", color.r, color.g, color.b, color.a);)
  130. } break;
  131. case NSVG_PAINT_LINEAR_GRADIENT:
  132. case NSVG_PAINT_RADIAL_GRADIENT: {
  133. NSVGgradient* g = shape->fill.gradient;
  134. (void)g;
  135. DEBUG_ONLY(printf(" gradient: type: %s xform: %f %f %f %f %f %f spread: %d fx: %f fy: %f nstops: %d\n", (shape->fill.type == NSVG_PAINT_LINEAR_GRADIENT ? "linear" : "radial"), g->xform[0], g->xform[1], g->xform[2], g->xform[3], g->xform[4], g->xform[5], g->spread, g->fx, g->fy, g->nstops);)
  136. for (int i = 0; i < g->nstops; i++) {
  137. DEBUG_ONLY(printf(" stop: #%08x\t%f\n", g->stops[i].color, g->stops[i].offset);)
  138. }
  139. nvgFillPaint(vg, getPaint(vg, &shape->fill));
  140. } break;
  141. }
  142. nvgFill(vg);
  143. }
  144. // Stroke shape
  145. if (shape->stroke.type) {
  146. nvgStrokeWidth(vg, shape->strokeWidth);
  147. // strokeDashOffset, strokeDashArray, strokeDashCount not yet supported
  148. nvgLineCap(vg, (NVGlineCap) shape->strokeLineCap);
  149. nvgLineJoin(vg, (int) shape->strokeLineJoin);
  150. switch (shape->stroke.type) {
  151. case NSVG_PAINT_COLOR: {
  152. NVGcolor color = getNVGColor(shape->stroke.color);
  153. nvgStrokeColor(vg, color);
  154. DEBUG_ONLY(printf(" stroke color (%g, %g, %g, %g)\n", color.r, color.g, color.b, color.a);)
  155. } break;
  156. case NSVG_PAINT_LINEAR_GRADIENT: {
  157. // NSVGgradient *g = shape->stroke.gradient;
  158. // printf(" lin grad: %f\t%f\n", g->fx, g->fy);
  159. } break;
  160. }
  161. nvgStroke(vg);
  162. }
  163. nvgRestore(vg);
  164. }
  165. DEBUG_ONLY(printf("\n");)
  166. }
  167. } // namespace rack