From 517db4126918138dddab2ac9cb651d71af15087e Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 11 Jul 2021 21:24:16 -0400 Subject: [PATCH] Add Svg::getSize(). --- include/svg.hpp | 3 ++- src/app/RackRail.cpp | 9 ++++----- src/svg.cpp | 19 +++++++++++++++++-- src/widget/SvgWidget.cpp | 6 ++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/svg.hpp b/include/svg.hpp index 8ef12ed6..768d44d5 100644 --- a/include/svg.hpp +++ b/include/svg.hpp @@ -43,10 +43,11 @@ struct Svg { void loadFile(const std::string& filename); /** Loads SVG data from a string. */ void loadString(const std::string& str); - void draw(NVGcontext* vg); + math::Vec getSize(); int getNumShapes(); int getNumPaths(); int getNumPoints(); + void draw(NVGcontext* vg); /** Loads Svg from a cache. */ static std::shared_ptr load(const std::string& filename); diff --git a/src/app/RackRail.cpp b/src/app/RackRail.cpp index 37640ed6..9b5b363e 100644 --- a/src/app/RackRail.cpp +++ b/src/app/RackRail.cpp @@ -24,21 +24,20 @@ void RackRail::draw(const DrawArgs& args) { // Rails for (float y = 0; y < box.size.y; y += RACK_GRID_HEIGHT) { - const float busBoardWidth = busBoardSvg->handle->width; - const float busBoardHeight = busBoardSvg->handle->height; - const float busBoardY = y + (RACK_GRID_HEIGHT - busBoardHeight) / 2; + const math::Vec busBoardSize = busBoardSvg->getSize(); + const float busBoardY = y + (RACK_GRID_HEIGHT - busBoardSize.y) / 2; const NVGcolor shadowColor = nvgRGBA(0, 0, 0, 0x20); // Bus board shadow nvgBeginPath(args.vg); - const float busBoardShadowY = busBoardY + busBoardHeight; + const float busBoardShadowY = busBoardY + busBoardSize.y; const float busBoardShadowHeight = 10; nvgRect(args.vg, 0, busBoardShadowY, box.size.x, busBoardShadowHeight); nvgFillPaint(args.vg, nvgLinearGradient(args.vg, 0, busBoardShadowY, 0, busBoardShadowY + busBoardShadowHeight, shadowColor, color::BLACK_TRANSPARENT)); nvgFill(args.vg); // Bus board - for (float x = 0; x < box.size.x; x += busBoardWidth) { + for (float x = 0; x < box.size.x; x += busBoardSize.x) { nvgSave(args.vg); nvgTranslate(args.vg, x, busBoardY); busBoardSvg->draw(args.vg); diff --git a/src/svg.cpp b/src/svg.cpp index b102b684..b7f1654b 100644 --- a/src/svg.cpp +++ b/src/svg.cpp @@ -18,6 +18,9 @@ Svg::~Svg() { void Svg::loadFile(const std::string& filename) { + if (handle) + nsvgDelete(handle); + handle = nsvgParseFromFile(filename.c_str(), "px", SVG_DPI); if (!handle) throw Exception("Failed to load SVG %s", filename.c_str()); @@ -26,6 +29,9 @@ void Svg::loadFile(const std::string& filename) { void Svg::loadString(const std::string& str) { + if (handle) + nsvgDelete(handle); + // nsvgParse modifies the input string std::string strCopy = str; handle = nsvgParse(&strCopy[0], "px", SVG_DPI); @@ -36,8 +42,10 @@ void Svg::loadString(const std::string& str) { } -void Svg::draw(NVGcontext* vg) { - svgDraw(vg, handle); +math::Vec Svg::getSize() { + if (!handle) + return math::Vec(); + return math::Vec(handle->width, handle->height); } @@ -78,6 +86,13 @@ int Svg::getNumPoints() { } +void Svg::draw(NVGcontext* vg) { + if (!handle) + return; + svgDraw(vg, handle); +} + + static std::map> svgCache; diff --git a/src/widget/SvgWidget.cpp b/src/widget/SvgWidget.cpp index 8208bac7..93650ad8 100644 --- a/src/widget/SvgWidget.cpp +++ b/src/widget/SvgWidget.cpp @@ -7,8 +7,8 @@ namespace widget { void SvgWidget::wrap() { - if (svg && svg->handle) { - box.size = math::Vec(svg->handle->width, svg->handle->height); + if (svg) { + box.size = svg->getSize(); } else { box.size = math::Vec(); @@ -23,8 +23,6 @@ void SvgWidget::setSvg(std::shared_ptr svg) { void SvgWidget::draw(const DrawArgs& args) { if (!svg) return; - if (!svg->handle) - return; svgDraw(args.vg, svg->handle); }