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.hpp 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <memory>
  3. #include <nanovg.h>
  4. #include <nanosvg.h>
  5. #include <common.hpp>
  6. #include <math.hpp>
  7. namespace rack {
  8. /** Arbitrary DPI, standardized for Rack. */
  9. static const float SVG_DPI = 75.f;
  10. static const float MM_PER_IN = 25.4f;
  11. /** Converts inch measurements to pixels */
  12. inline float in2px(float in) {
  13. return in * SVG_DPI;
  14. }
  15. inline math::Vec in2px(math::Vec in) {
  16. return in.mult(SVG_DPI);
  17. }
  18. /** Converts millimeter measurements to pixels */
  19. inline float mm2px(float mm) {
  20. return mm * (SVG_DPI / MM_PER_IN);
  21. }
  22. inline math::Vec mm2px(math::Vec mm) {
  23. return mm.mult(SVG_DPI / MM_PER_IN);
  24. }
  25. struct Svg {
  26. NSVGimage* handle = NULL;
  27. ~Svg();
  28. /** Don't call this directly. Use `Svg::load()` for caching. */
  29. void loadFile(const std::string& filename);
  30. /** Loads SVG data from a string. */
  31. void loadString(const std::string& str);
  32. /** Returns the SVG page size in pixels. */
  33. math::Vec getSize();
  34. int getNumShapes();
  35. int getNumPaths();
  36. int getNumPoints();
  37. void draw(NVGcontext* vg);
  38. /** Loads Svg from a cache. */
  39. static std::shared_ptr<Svg> load(const std::string& filename);
  40. };
  41. DEPRECATED typedef Svg SVG;
  42. void svgDraw(NVGcontext* vg, NSVGimage* svg);
  43. } // namespace rack