diff --git a/src/nanosvg/nanosvg.h b/src/nanosvg/nanosvg.h index 4eaff9dadc..57bcb7c2c1 100644 --- a/src/nanosvg/nanosvg.h +++ b/src/nanosvg/nanosvg.h @@ -165,11 +165,6 @@ typedef struct NSVGimage // Parses SVG file from a file, returns SVG image as paths. NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi); -// Parses SVG file from a file, returns SVG image as paths. -// And makes replases befor parsing -// replace_map containes old_value->new_value -NSVGimage* nsvgParseFromFileWithReplace(const char* filename, const char* units, float dpi, const std::map& replace_map); - // Parses SVG file from a null terminated string, returns SVG image as paths. // Important note: changes the string. NSVGimage* nsvgParse(char* input, const char* units, float dpi); @@ -2908,12 +2903,6 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi) { - return nsvgParseFromFileWithReplace(filename, units, dpi, { {} }); -} - -NSVGimage* nsvgParseFromFileWithReplace(const char* filename, const char* units, float dpi, const std::map& replaces) -{ - std::string str; FILE* fp = NULL; size_t size; char* data = NULL; @@ -2930,14 +2919,7 @@ NSVGimage* nsvgParseFromFileWithReplace(const char* filename, const char* units, data[size] = '\0'; // Must be null terminated. fclose(fp); - if(replaces.empty()) - image = nsvgParse(data, units, dpi); - else { - str.assign(data); - for (auto val : replaces) - boost::replace_all(str, val.first, val.second); - image = nsvgParse(str.data(), units, dpi); - } + image = nsvgParse(data, units, dpi); free(data); return image; diff --git a/src/slic3r/GUI/BitmapCache.cpp b/src/slic3r/GUI/BitmapCache.cpp index 749fc5ef28..39ba849d33 100644 --- a/src/slic3r/GUI/BitmapCache.cpp +++ b/src/slic3r/GUI/BitmapCache.cpp @@ -260,6 +260,43 @@ wxBitmap* BitmapCache::load_png(const std::string &bitmap_name, unsigned width, return this->insert(bitmap_key, wxImage_to_wxBitmap_with_alpha(std::move(image))); } +NSVGimage* BitmapCache::nsvgParseFromFileWithReplace(const char* filename, const char* units, float dpi, const std::map& replaces) +{ + std::string str; + FILE* fp = NULL; + size_t size; + char* data = NULL; + NSVGimage* image = NULL; + + fp = boost::nowide::fopen(filename, "rb"); + if (!fp) goto error; + fseek(fp, 0, SEEK_END); + size = ftell(fp); + fseek(fp, 0, SEEK_SET); + data = (char*)malloc(size + 1); + if (data == NULL) goto error; + if (fread(data, 1, size, fp) != size) goto error; + data[size] = '\0'; // Must be null terminated. + fclose(fp); + + if (replaces.empty()) + image = nsvgParse(data, units, dpi); + else { + str.assign(data); + for (auto val : replaces) + boost::replace_all(str, val.first, val.second); + image = nsvgParse(str.data(), units, dpi); + } + free(data); + return image; + +error: + if (fp) fclose(fp); + if (data) free(data); + if (image) nsvgDelete(image); + return NULL; +} + wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_width, unsigned target_height, const bool grayscale/* = false*/, const bool dark_mode/* = false*/, const std::string& new_color /*= ""*/) { @@ -278,11 +315,11 @@ wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_ // map of color replaces std::map replaces; if (dark_mode) - replaces["#808080"] = "#FFFFFF"; + replaces["\"#808080\""] = "\"#FFFFFF\""; if (!new_color.empty()) - replaces["#ED6B21"] = new_color; + replaces["\"#ED6B21\""] = "\"" + new_color + "\""; - NSVGimage *image = ::nsvgParseFromFileWithReplace(Slic3r::var(bitmap_name + ".svg").c_str(), "px", 96.0f, replaces); + NSVGimage *image = nsvgParseFromFileWithReplace(Slic3r::var(bitmap_name + ".svg").c_str(), "px", 96.0f, replaces); if (image == nullptr) return nullptr; diff --git a/src/slic3r/GUI/BitmapCache.hpp b/src/slic3r/GUI/BitmapCache.hpp index 5fa8643b5a..4d1d383c41 100644 --- a/src/slic3r/GUI/BitmapCache.hpp +++ b/src/slic3r/GUI/BitmapCache.hpp @@ -9,6 +9,8 @@ #include #endif +struct NSVGimage; + namespace Slic3r { namespace GUI { class BitmapCache @@ -32,6 +34,11 @@ public: // Load png from resources/icons. bitmap_key is given without the .png suffix. Bitmap will be rescaled to provided height/width if nonzero. wxBitmap* load_png(const std::string &bitmap_key, unsigned width = 0, unsigned height = 0, const bool grayscale = false); + + // Parses SVG file from a file, returns SVG image as paths. + // And makes replases befor parsing + // replace_map containes old_value->new_value + static NSVGimage* nsvgParseFromFileWithReplace(const char* filename, const char* units, float dpi, const std::map& replaces); // Load svg from resources/icons. bitmap_key is given without the .svg suffix. SVG will be rasterized to provided height/width. wxBitmap* load_svg(const std::string &bitmap_key, unsigned width = 0, unsigned height = 0, const bool grayscale = false, const bool dark_mode = false, const std::string& new_color = ""); diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 894cff3a2f..cac2740b0a 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -31,6 +31,7 @@ #include "GUI.hpp" #include "I18N.hpp" #include "Search.hpp" +#include "BitmapCache.hpp" #include "../Utils/MacDarkMode.hpp" #include "nanosvg/nanosvg.h" @@ -1030,7 +1031,7 @@ std::vector ImGuiWrapper::load_svg(const std::string& bitmap_name { std::vector empty_vector; - NSVGimage* image = ::nsvgParseFromFileWithReplace(Slic3r::var(bitmap_name + ".svg").c_str(), "px", 96.0f, { { "#808080", "#FFFFFF" } }); + NSVGimage* image = BitmapCache::nsvgParseFromFileWithReplace(Slic3r::var(bitmap_name + ".svg").c_str(), "px", 96.0f, { { "\"#808080\"", "\"#FFFFFF\"" } }); if (image == nullptr) return empty_vector;