Attempt to fix bitmap bundle scaling issues

This commit is contained in:
Ocraftyone 2023-12-07 08:09:34 -05:00
parent b335055867
commit e94ba58d90
No known key found for this signature in database
GPG key ID: 85836ED21AD4D125
5 changed files with 65 additions and 34 deletions

View file

@ -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);

View file

@ -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<unsigned char> 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);

View file

@ -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})

View file

@ -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);
}
// ----------------------------------------------------------------------------

View file

@ -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 };
};