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.

243 lines
7.7KB

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