From 55fa1f0dca7326f727fdcbe62b0d42fe694475c7 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 13 Feb 2021 15:34:12 -0500 Subject: [PATCH] Move Svg from window.hpp to svg.hpp. Deprecate Window::loadSvg(). Un-deprecate Svg::load(). --- include/helpers.hpp | 2 +- include/svg.hpp | 15 +++++++++++++++ include/window.hpp | 19 ++++++------------- src/svg.cpp | 33 +++++++++++++++++++++++++++++++++ src/window.cpp | 32 -------------------------------- 5 files changed, 55 insertions(+), 46 deletions(-) diff --git a/include/helpers.hpp b/include/helpers.hpp index 9886d4ab..79e44774 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -58,7 +58,7 @@ TWidget* createWidgetCentered(math::Vec pos) { inline app::SvgPanel* createPanel(std::string svgPath) { app::SvgPanel* panel = new app::SvgPanel; - std::shared_ptr svg = APP->window->loadSvg(svgPath); + std::shared_ptr svg = Svg::load(svgPath); panel->setBackground(svg); return panel; } diff --git a/include/svg.hpp b/include/svg.hpp index 96d3e82b..fd09255f 100644 --- a/include/svg.hpp +++ b/include/svg.hpp @@ -1,4 +1,6 @@ #pragma once +#include + #include #include @@ -8,6 +10,19 @@ namespace rack { +struct Svg { + NSVGimage* handle = NULL; + /** Don't call this directly. Use `Svg::load()` for caching. */ + void loadFile(const std::string& filename); + ~Svg(); + + /** Loads Svg from a cache. */ + static std::shared_ptr load(const std::string& filename); +}; + +DEPRECATED typedef Svg SVG; + + void svgDraw(NVGcontext* vg, NSVGimage* svg); diff --git a/include/window.hpp b/include/window.hpp index 98dfa27e..3f279eb0 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -42,17 +43,6 @@ struct Image { DEPRECATED static std::shared_ptr load(const std::string& filename); }; -struct Svg { - NSVGimage* handle = NULL; - /** Don't call this directly but instead use `APP->window->loadSvg()` */ - void loadFile(const std::string& filename); - ~Svg(); - /** Use `APP->window->loadSvg()` instead. */ - DEPRECATED static std::shared_ptr load(const std::string& filename); -}; - -DEPRECATED typedef Svg SVG; - struct Window { struct Internal; @@ -71,7 +61,6 @@ struct Window { /** Use load*() instead of modifying these directly. */ std::map> fontCache; std::map> imageCache; - std::map> svgCache; Window(); ~Window(); @@ -100,7 +89,11 @@ struct Window { std::shared_ptr loadFont(const std::string& filename); std::shared_ptr loadImage(const std::string& filename); - std::shared_ptr loadSvg(const std::string& filename); + + /** Use `Svg::load(filename)` in new code. */ + DEPRECATED std::shared_ptr loadSvg(const std::string& filename) { + return Svg::load(filename); + } }; diff --git a/src/svg.cpp b/src/svg.cpp index 889c05c4..75451c21 100644 --- a/src/svg.cpp +++ b/src/svg.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include // #define DEBUG_ONLY(x) x @@ -8,6 +10,37 @@ namespace rack { +void Svg::loadFile(const std::string& filename) { + handle = nsvgParseFromFile(filename.c_str(), "px", app::SVG_DPI); + if (handle) { + INFO("Loaded SVG %s", filename.c_str()); + } + else { + WARN("Failed to load SVG %s", filename.c_str()); + } +} + + +Svg::~Svg() { + if (handle) + nsvgDelete(handle); +} + + + +static std::map> svgCache; + + +std::shared_ptr Svg::load(const std::string& filename) { + auto sp = svgCache[filename].lock(); + if (!sp) { + svgCache[filename] = sp = std::make_shared(); + sp->load(filename); + } + return sp; +} + + static NVGcolor getNVGColor(uint32_t color) { return nvgRGBA( (color >> 0) & 0xff, diff --git a/src/window.cpp b/src/window.cpp index e0122b76..71e59c05 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -72,28 +72,6 @@ std::shared_ptr Image::load(const std::string& filename) { } -void Svg::loadFile(const std::string& filename) { - handle = nsvgParseFromFile(filename.c_str(), "px", app::SVG_DPI); - if (handle) { - INFO("Loaded SVG %s", filename.c_str()); - } - else { - WARN("Failed to load SVG %s", filename.c_str()); - } -} - - -Svg::~Svg() { - if (handle) - nsvgDelete(handle); -} - - -std::shared_ptr Svg::load(const std::string& filename) { - return APP->window->loadSvg(filename); -} - - struct Window::Internal { std::string lastWindowTitle; @@ -652,16 +630,6 @@ std::shared_ptr Window::loadImage(const std::string& filename) { } -std::shared_ptr Window::loadSvg(const std::string& filename) { - auto sp = svgCache[filename].lock(); - if (!sp) { - svgCache[filename] = sp = std::make_shared(); - sp->loadFile(filename); - } - return sp; -} - - void windowInit() { int err;