From e94ba58d907b1b2da5a9e27ad552e1d7f1288c18 Mon Sep 17 00:00:00 2001 From: Ocraftyone Date: Thu, 7 Dec 2023 08:09:34 -0500 Subject: [PATCH] Attempt to fix bitmap bundle scaling issues --- src/slic3r/GUI/AboutDialog.cpp | 4 +- src/slic3r/GUI/BitmapCache.cpp | 60 ++++++++++++++++++++++--- src/slic3r/GUI/Widgets/SwitchButton.cpp | 4 +- src/slic3r/GUI/wxExtensions.cpp | 26 ++--------- src/slic3r/GUI/wxExtensions.hpp | 5 +-- 5 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/slic3r/GUI/AboutDialog.cpp b/src/slic3r/GUI/AboutDialog.cpp index de4aca579c..55cee85f97 100644 --- a/src/slic3r/GUI/AboutDialog.cpp +++ b/src/slic3r/GUI/AboutDialog.cpp @@ -232,8 +232,8 @@ AboutDialog::AboutDialog() main_sizer->Add(ver_sizer, 0, wxEXPAND | wxALL, 0); // logo - m_logo_bitmap = ScalableBitmap(this, "OrcaSlicer_about", 250); - m_logo = new wxStaticBitmap(this, wxID_ANY, m_logo_bitmap.bmp(), wxDefaultPosition,wxDefaultSize, 0); + m_logo_bitmap = ScalableBitmap(this, "OrcaSlicer_about", FromDIP(250)); + m_logo = new wxStaticBitmap(this, wxID_ANY, m_logo_bitmap.bmp(), wxDefaultPosition, wxDefaultSize, 0); m_logo->SetSizer(vesizer); panel_versizer->Add(m_logo, 1, wxALL | wxEXPAND, 0); diff --git a/src/slic3r/GUI/BitmapCache.cpp b/src/slic3r/GUI/BitmapCache.cpp index 9eab03ddf9..9f7d100e41 100644 --- a/src/slic3r/GUI/BitmapCache.cpp +++ b/src/slic3r/GUI/BitmapCache.cpp @@ -421,12 +421,62 @@ wxBitmapBundle* BitmapCache::from_svg(const std::string& bitmap_name, unsigned t replaces["\"#009688\""] = "\"#00675b\""; } - std::string str; - nsvgGetDataFromFileWithReplace(Slic3r::var(bitmap_name + ".svg").c_str(), str, replaces); - if (str.empty()) + // Orca: Replace nsvgGetDataFromFileWithReplace with the parse variant so that we can use the image data + // to properly scale the image rather than just using the px_cnt for both height and width + NSVGimage* nsvg_img = nsvgParseFromFileWithReplace(Slic3r::var(bitmap_name + ".svg").c_str(),"px", 96.f, replaces); + if (nsvg_img == nullptr) return nullptr; - return insert_bndl(bitmap_key, str.data(), target_width, target_height); + if (target_height == 0 && target_width == 0) + target_height = nsvg_img->height; + + target_height != 0 ? target_height *= m_scale : target_width *= m_scale; + + float svg_scale = target_height != 0 ? + (float)target_height / nsvg_img->height : target_width != 0 ? + (float)target_width / nsvg_img->width : 1.f; + + wxSize size((svg_scale * nsvg_img->width + 0.5f), (svg_scale * nsvg_img->height + 0.5f)); + int n_pixels = size.x * size.y; + if (n_pixels <= 0) { + ::nsvgDelete(nsvg_img); + return nullptr; + } + + NSVGrasterizer *rast = ::nsvgCreateRasterizer(); + if (rast == nullptr) { + ::nsvgDelete(nsvg_img); + return nullptr; + } + wxVector buffer(n_pixels*4); + nsvgRasterize(rast,nsvg_img,0.0, 0.0, // no offset + svg_scale,&buffer[0],size.x, size.y, size.x*4); + + wxBitmap bitmap(size, 32); + wxAlphaPixelData bmpdata(bitmap); + wxAlphaPixelData::Iterator dst(bmpdata); + + const unsigned char* src = &buffer[0]; + for ( int y = 0; y < size.y; ++y ) + { + dst.MoveTo(bmpdata, 0, y); + for ( int x = 0; x < size.x; ++x ) + { + const unsigned char a = src[3]; + dst.Red() = src[0] * a / 255; + dst.Green() = src[1] * a / 255; + dst.Blue() = src[2] * a / 255; + dst.Alpha() = a; + + ++dst; + src += 4; + } + } + + ::nsvgDelete(nsvg_img); + ::nsvgDeleteRasterizer(rast); + + return insert_bndl(bitmap_key, bitmap); } wxBitmapBundle* BitmapCache::from_png(const std::string& bitmap_name, unsigned width, unsigned height) @@ -507,7 +557,7 @@ wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_ float svg_scale = target_height != 0 ? (float)target_height / image->height : target_width != 0 ? - (float)target_width / image->width : 1; + (float)target_width / image->width : 1.f; int width = (int)(svg_scale * image->width + 0.5f); int height = (int)(svg_scale * image->height + 0.5f); diff --git a/src/slic3r/GUI/Widgets/SwitchButton.cpp b/src/slic3r/GUI/Widgets/SwitchButton.cpp index 67aa424578..dde3c35b05 100644 --- a/src/slic3r/GUI/Widgets/SwitchButton.cpp +++ b/src/slic3r/GUI/Widgets/SwitchButton.cpp @@ -9,8 +9,8 @@ SwitchButton::SwitchButton(wxWindow* parent, wxWindowID id) : wxBitmapToggleButton(parent, id, wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE | wxBU_EXACTFIT) - , m_on(this, "toggle_on", 16, false, false, true) - , m_off(this, "toggle_off", 16, false, false, true) + , m_on(this, "toggle_on", FromDIP(16)) + , m_off(this, "toggle_off", FromDIP(16)) , text_color(std::pair{0xfffffe, (int) StateColor::Checked}, std::pair{0x6B6B6B, (int) StateColor::Normal}) , track_color(0xD9D9D9) , thumb_color(std::pair{0x009688, (int) StateColor::Checked}, std::pair{0xD9D9D9, (int) StateColor::Normal}) diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index dc9bf65e17..b6b6fa2948 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -884,35 +884,17 @@ ScalableBitmap::ScalableBitmap( wxWindow *parent, const std::string& icon_name/* = ""*/, const int px_cnt/* = 16*/, const bool grayscale/* = false*/, - const bool resize/* = false*/, - const bool use_legacy_bmp/* = false*/): - m_parent(parent), m_icon_name(icon_name), m_legacy_bmp(use_legacy_bmp), + const bool resize/* = false*/ ): + m_parent(parent), m_icon_name(icon_name), m_px_cnt(px_cnt), m_grayscale(grayscale), m_resize(resize) // BBS: support resize by fill border { - // Orca: there is currently an issue causing the advanced SwitchButton to not scale properly - // when using get_bmp_bundle. This allows for the older method of getting a scaled bitmap to be - // used in this edge case while the underlying issue is determined. - if (m_legacy_bmp) { - m_bmp = create_scaled_bitmap(icon_name, parent, px_cnt, m_grayscale, std::string(), false, resize); - if (px_cnt == 0) { - m_px_cnt = GetHeight(); // scale - unsigned int height = (unsigned int) (parent->FromDIP(m_px_cnt) + 0.5f); - if (height != GetHeight()) - sys_color_changed(); - } - } else { - m_bmp = *get_bmp_bundle(icon_name, px_cnt); - } + m_bmp = *get_bmp_bundle(icon_name, px_cnt); } void ScalableBitmap::sys_color_changed() { - if (m_legacy_bmp) { - // BBS: support resize by fill border - m_bmp = create_scaled_bitmap(m_icon_name, m_parent, m_px_cnt, m_grayscale, std::string(), false, m_resize); - } else - m_bmp = *get_bmp_bundle(m_icon_name, m_px_cnt); + m_bmp = *get_bmp_bundle(m_icon_name, m_px_cnt); } // ---------------------------------------------------------------------------- diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index b2e2eebc86..70812e311a 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -169,8 +169,8 @@ public: const std::string& icon_name = "", const int px_cnt = 16, const bool grayscale = false, - const bool resize = false, // BBS: support resize by fill border - const bool use_legacy_bmp = false); + const bool resize = false // BBS: support resize by fill border + ); ~ScalableBitmap() {} @@ -202,7 +202,6 @@ private: int m_px_cnt {16}; bool m_grayscale{ false }; bool m_resize{ false }; - bool m_legacy_bmp{ false }; };