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.

248 lines
7.7KB

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