diff --git a/build/cmake/tests/gui/CMakeLists.txt b/build/cmake/tests/gui/CMakeLists.txt index 7ff31daf37..a412248df5 100644 --- a/build/cmake/tests/gui/CMakeLists.txt +++ b/build/cmake/tests/gui/CMakeLists.txt @@ -141,6 +141,7 @@ set(TEST_GUI_DATA horse.pcx horse.png horse.pnm + horse.svg horse.tga horse.tif horse.xpm diff --git a/include/wx/bmpbndl.h b/include/wx/bmpbndl.h index 646c006b72..5f2b414c7b 100644 --- a/include/wx/bmpbndl.h +++ b/include/wx/bmpbndl.h @@ -82,6 +82,9 @@ public: // This overload currently makes a copy of the data. static wxBitmapBundle FromSVG(const char* data, const wxSize& sizeDef); + + // Load SVG image from the given file (must be a local file, not an URL). + static wxBitmapBundle FromSVGFile(const wxString& path, const wxSize& sizeDef); #endif // wxHAS_SVG // 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 400b2471c5..a5f9e061c2 100644 --- a/interface/wx/bmpbndl.h +++ b/interface/wx/bmpbndl.h @@ -247,6 +247,20 @@ public: /// @overload static wxBitmapBundle FromSVG(const char* data, const wxSize& sizeDef); + /** + Create a bundle from the SVG image loaded from the given file. + + This function loads the SVG data from the given @a path and calls + FromSVG() with it. As it is just a wrapper for FromSVG(), please see + that function documentation for more information about SVG support. + + @param path Path to the SVG file. Notice that it should a local file, + not an URL. + @param sizeDef The default size to return from GetDefaultSize() for + this bundle. + */ + static wxBitmapBundle FromSVGFile(const wxString& path, const wxSize& sizeDef); + /** Check if bitmap bundle is non-empty. diff --git a/src/generic/bmpsvg.cpp b/src/generic/bmpsvg.cpp index 1c34e5215b..8517508896 100644 --- a/src/generic/bmpsvg.cpp +++ b/src/generic/bmpsvg.cpp @@ -42,6 +42,13 @@ #include "wx/utils.h" // Only for wxMin() #endif // WX_PRECOMP +#ifdef wxUSE_FFILE + #include "wx/ffile.h" +#elif wxUSE_FILE + #include "wx/file.h" +#else + #define wxNO_SVG_FILE +#endif #include "wx/rawbmp.h" #include "wx/private/bmpbndl.h" @@ -203,4 +210,34 @@ wxBitmapBundle wxBitmapBundle::FromSVG(const char* data, const wxSize& sizeDef) return FromSVG(copy.data(), sizeDef); } +/* static */ +wxBitmapBundle wxBitmapBundle::FromSVGFile(const wxString& path, const wxSize& sizeDef) +{ + // There is nsvgParseFromFile(), but it doesn't work with Unicode filenames + // under MSW and does exactly the same thing that we do here in any case, + // so it seems better to use our code. +#ifndef wxNO_SVG_FILE +#if wxUSE_FFILE + wxFFile file(path, "rb"); +#elif wxUSE_FILE + wxFile file(path); +#endif + if ( file.IsOpened() ) + { + const wxFileOffset lenAsOfs = file.Length(); + if ( lenAsOfs != wxInvalidOffset ) + { + const size_t len = static_cast(lenAsOfs); + + wxCharBuffer buf(len); + char* const ptr = buf.data(); + if ( file.Read(ptr, len) == len ) + return wxBitmapBundle::FromSVG(ptr, sizeDef); + } + } +#endif // !wxNO_SVG_FILE + + return wxBitmapBundle(); +} + #endif // wxHAS_SVG diff --git a/tests/Makefile.in b/tests/Makefile.in index ee17afc496..21e8258c99 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -553,7 +553,7 @@ test$(EXEEXT): $(TEST_OBJECTS) data: @mkdir -p . - @for f in testdata.fc; do \ + @for f in testdata.fc horse.svg; do \ if test ! -f ./$$f -a ! -d ./$$f ; \ then x=yep ; \ else x=`find $(srcdir)/$$f -newer ./$$f -print` ; \ diff --git a/tests/graphics/bmpbundle.cpp b/tests/graphics/bmpbundle.cpp index 0f0abfe913..cebbc0c110 100644 --- a/tests/graphics/bmpbundle.cpp +++ b/tests/graphics/bmpbundle.cpp @@ -136,4 +136,11 @@ TEST_CASE("BitmapBundle::FromSVG", "[bmpbundle][svg]") CHECK( b.GetBitmap(wxSize(16, 16)).GetSize() == wxSize(16, 16) ); } +TEST_CASE("BitmapBundle::FromSVGFile", "[bmpbundle][svg][file]") +{ + wxBitmapBundle b = wxBitmapBundle::FromSVGFile("horse.svg", wxSize(20, 20)); + REQUIRE( b.IsOk() ); + CHECK( b.GetDefaultSize() == wxSize(20, 20) ); +} + #endif // wxHAS_SVG diff --git a/tests/horse.svg b/tests/horse.svg new file mode 100644 index 0000000000..2483101380 --- /dev/null +++ b/tests/horse.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/makefile.gcc b/tests/makefile.gcc index c9ee403ca7..0cdc08d7aa 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -551,7 +551,7 @@ endif data: if not exist $(OBJS) mkdir $(OBJS) - for %%f in (testdata.fc) do if not exist $(OBJS)\%%f copy .\%%f $(OBJS) + for %%f in (testdata.fc horse.svg) do if not exist $(OBJS)\%%f copy .\%%f $(OBJS) data-image-sample: if not exist $(OBJS) mkdir $(OBJS) diff --git a/tests/makefile.vc b/tests/makefile.vc index 5d9b0f676b..4aa2cbf510 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -985,7 +985,7 @@ $(OBJS)\test_allheaders.exe: $(OBJS)\test_allheaders_dummy.obj $(TEST_ALLHEADER data: if not exist $(OBJS) mkdir $(OBJS) - for %f in (testdata.fc) do if not exist $(OBJS)\%f copy .\%f $(OBJS) + for %f in (testdata.fc horse.svg) do if not exist $(OBJS)\%f copy .\%f $(OBJS) data-image-sample: if not exist $(OBJS) mkdir $(OBJS) diff --git a/tests/test.bkl b/tests/test.bkl index 3b3d45afb8..e45d45a5df 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -338,7 +338,7 @@ - testdata.fc + testdata.fc horse.svg