Check that we parsed something in wxBitmapBundle::FromSVG()

Don't always return success from this function, NanoSVG just skips
everything until the start of the XML prologue and doesn't return an
error even if it doesn't find it at all, so check that it could parse at
least something to avoid returning a "valid" bundle not containing
anything at all.

Add a unit test checking that we actually can't create an SVG from a
.bmp file (which is something that "worked" before).
This commit is contained in:
Vadim Zeitlin
2022-02-15 19:37:22 +00:00
parent 3aad506c5b
commit 4ff445af4d
2 changed files with 15 additions and 2 deletions

View File

@@ -207,6 +207,15 @@ wxBitmapBundle wxBitmapBundle::FromSVG(char* data, const wxSize& sizeDef)
if ( !svgImage )
return wxBitmapBundle();
// Somewhat unexpectedly, a non-null but empty image is returned even if
// the data is not SVG at all, e.g. without this check creating a bundle
// from any random file with FromSVGFile() would "work".
if ( svgImage->width == 0 && svgImage->height == 0 && !svgImage->shapes )
{
nsvgDelete(svgImage);
return wxBitmapBundle();
}
return wxBitmapBundle(new wxBitmapBundleImplSVG(svgImage, sizeDef));
}

View File

@@ -140,9 +140,13 @@ TEST_CASE("BitmapBundle::FromSVG", "[bmpbundle][svg]")
TEST_CASE("BitmapBundle::FromSVGFile", "[bmpbundle][svg][file]")
{
wxBitmapBundle b = wxBitmapBundle::FromSVGFile("horse.svg", wxSize(20, 20));
const wxSize size(20, 20); // completely arbitrary
CHECK( !wxBitmapBundle::FromSVGFile("horse.bmp", size).IsOk() );
wxBitmapBundle b = wxBitmapBundle::FromSVGFile("horse.svg", size);
REQUIRE( b.IsOk() );
CHECK( b.GetDefaultSize() == wxSize(20, 20) );
CHECK( b.GetDefaultSize() == size );
}
#endif // wxHAS_SVG