From ab619010bdaf29e8f7a2d6f328fe399ed8b51684 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 2 Oct 2021 14:38:08 +0100 Subject: [PATCH] Add FromSVG() overload taking const data Passing non-const data is inconvenient and error-prone, as data can't be used again after it was modified by Nano SVG, which resulted in the button using SVG bitmap not working any longer in the toolbar sample after recreating the toolbar. So make it easier to do the right thing, while still keeping the overload taking non-const data for the situations when avoiding an extra copy is worth it. --- include/wx/bmpbndl.h | 3 +++ interface/wx/bmpbndl.h | 13 +++++++++---- samples/toolbar/toolbar.cpp | 2 +- src/generic/bmpsvg.cpp | 8 ++++++++ tests/graphics/bmpbundle.cpp | 6 ++---- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/wx/bmpbndl.h b/include/wx/bmpbndl.h index b73d192056..1f3d33f973 100644 --- a/include/wx/bmpbndl.h +++ b/include/wx/bmpbndl.h @@ -67,6 +67,9 @@ public: // Notice that the data here is non-const because it can be temporarily // modified while parsing it. static wxBitmapBundle FromSVG(char* data, const wxSize sizeDef); + + // This overload currently makes a copy of the data. + static wxBitmapBundle FromSVG(const char* data, const wxSize sizeDef); #endif // wxHAS_RAW_BITMAP // Create from the resources: all existing versions of the bitmap of the diff --git a/interface/wx/bmpbndl.h b/interface/wx/bmpbndl.h index 4d4b22c893..9808f60b5d 100644 --- a/interface/wx/bmpbndl.h +++ b/interface/wx/bmpbndl.h @@ -195,16 +195,21 @@ public: @param data This data may, or not, have the XML document preamble, i.e. it can start either with @c "" "" "" diff --git a/src/generic/bmpsvg.cpp b/src/generic/bmpsvg.cpp index 912817a37b..f440e5e151 100644 --- a/src/generic/bmpsvg.cpp +++ b/src/generic/bmpsvg.cpp @@ -172,4 +172,12 @@ wxBitmapBundle wxBitmapBundle::FromSVG(char* data, const wxSize sizeDef) return wxBitmapBundle(new wxBitmapBundleImplSVG(svgImage, sizeDef)); } +/* static */ +wxBitmapBundle wxBitmapBundle::FromSVG(const char* data, const wxSize sizeDef) +{ + wxCharBuffer copy(data); + + return FromSVG(copy.data(), sizeDef); +} + #endif // wxHAS_RAW_BITMAP diff --git a/tests/graphics/bmpbundle.cpp b/tests/graphics/bmpbundle.cpp index 3d0f58eedd..701b7e7a20 100644 --- a/tests/graphics/bmpbundle.cpp +++ b/tests/graphics/bmpbundle.cpp @@ -61,8 +61,7 @@ TEST_CASE("BitmapBundle::FromSVG", "[bmpbundle][svg]") "" ; - wxCharBuffer buf(svg_data); - wxBitmapBundle b = wxBitmapBundle::FromSVG(buf.data(), wxSize(20, 20)); + wxBitmapBundle b = wxBitmapBundle::FromSVG(svg_data, wxSize(20, 20)); REQUIRE( b.IsOk() ); CHECK( b.GetDefaultSize() == wxSize(20, 20) ); @@ -72,8 +71,7 @@ TEST_CASE("BitmapBundle::FromSVG", "[bmpbundle][svg]") const char* svg_tag_start = strstr(svg_data, "