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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. namespace window {
  9. /** Arbitrary DPI, standardized for Rack. */
  10. static const float SVG_DPI = 75.f;
  11. static const float MM_PER_IN = 25.4f;
  12. /** Converts inch measurements to pixels */
  13. inline float in2px(float in) {
  14. return in * SVG_DPI;
  15. }
  16. inline math::Vec in2px(math::Vec in) {
  17. return in.mult(SVG_DPI);
  18. }
  19. /** Converts millimeter measurements to pixels */
  20. inline float mm2px(float mm) {
  21. return mm * (SVG_DPI / MM_PER_IN);
  22. }
  23. inline math::Vec mm2px(math::Vec mm) {
  24. return mm.mult(SVG_DPI / MM_PER_IN);
  25. }
  26. /** Scalable Vector Graphics document */
  27. struct Svg {
  28. NSVGimage* handle = NULL;
  29. ~Svg();
  30. /** Don't call this directly. Use `Svg::load()` for caching. */
  31. void loadFile(const std::string& filename);
  32. /** Loads SVG data from a string. */
  33. void loadString(const std::string& str);
  34. /** Returns the SVG page size in pixels. */
  35. math::Vec getSize();
  36. int getNumShapes();
  37. int getNumPaths();
  38. int getNumPoints();
  39. void draw(NVGcontext* vg);
  40. /** Loads Svg from a cache. */
  41. static std::shared_ptr<Svg> load(const std::string& filename);
  42. };
  43. DEPRECATED typedef Svg SVG;
  44. void svgDraw(NVGcontext* vg, NSVGimage* svg);
  45. } // namespace window
  46. } // namespace rack