Add wxBitmapBundle::FromSVGFile() helper

This is just a trivial wrapper for wxBitmapBundle::FromSVG(), but it can
still be convenient to have.
This commit is contained in:
Vadim Zeitlin 2021-11-28 22:16:55 +01:00
parent 5efcaf4e59
commit 8adfaa37f7
10 changed files with 74 additions and 4 deletions

View File

@ -141,6 +141,7 @@ set(TEST_GUI_DATA
horse.pcx horse.pcx
horse.png horse.png
horse.pnm horse.pnm
horse.svg
horse.tga horse.tga
horse.tif horse.tif
horse.xpm horse.xpm

View File

@ -82,6 +82,9 @@ public:
// This overload currently makes a copy of the data. // This overload currently makes a copy of the data.
static wxBitmapBundle FromSVG(const char* data, const wxSize& sizeDef); 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 #endif // wxHAS_SVG
// Create from the resources: all existing versions of the bitmap of the // Create from the resources: all existing versions of the bitmap of the

View File

@ -247,6 +247,20 @@ public:
/// @overload /// @overload
static wxBitmapBundle FromSVG(const char* data, const wxSize& sizeDef); 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. Check if bitmap bundle is non-empty.

View File

@ -42,6 +42,13 @@
#include "wx/utils.h" // Only for wxMin() #include "wx/utils.h" // Only for wxMin()
#endif // WX_PRECOMP #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/rawbmp.h"
#include "wx/private/bmpbndl.h" #include "wx/private/bmpbndl.h"
@ -203,4 +210,34 @@ wxBitmapBundle wxBitmapBundle::FromSVG(const char* data, const wxSize& sizeDef)
return FromSVG(copy.data(), 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<size_t>(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 #endif // wxHAS_SVG

View File

@ -553,7 +553,7 @@ test$(EXEEXT): $(TEST_OBJECTS)
data: data:
@mkdir -p . @mkdir -p .
@for f in testdata.fc; do \ @for f in testdata.fc horse.svg; do \
if test ! -f ./$$f -a ! -d ./$$f ; \ if test ! -f ./$$f -a ! -d ./$$f ; \
then x=yep ; \ then x=yep ; \
else x=`find $(srcdir)/$$f -newer ./$$f -print` ; \ else x=`find $(srcdir)/$$f -newer ./$$f -print` ; \

View File

@ -136,4 +136,11 @@ TEST_CASE("BitmapBundle::FromSVG", "[bmpbundle][svg]")
CHECK( b.GetBitmap(wxSize(16, 16)).GetSize() == wxSize(16, 16) ); 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 #endif // wxHAS_SVG

8
tests/horse.svg Normal file
View File

@ -0,0 +1,8 @@
<?xml version='1.0' encoding='utf-8'?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 511 511" xmlns:xlink="http://www.w3.org/1999/xlink" enable-background="new 0 0 511 511">
<g>
<path d="m298.342,455.5c0,4.142 3.358,7.5 7.5,7.5h16.004c4.142,0 7.5-3.358 7.5-7.5s-3.358-7.5-7.5-7.5h-16.004c-4.142,0-7.5,3.358-7.5,7.5z"/>
<path d="m412.37,195.5c0-2.527 0.984-4.902 2.771-6.689 3.556-3.555 5.514-8.283 5.514-13.311s-1.958-9.755-5.514-13.311l-95.421-95.42c6.022-9.682 14.336-30.793 1.031-62.194-1.059-2.499-3.386-4.231-6.084-4.529-2.698-0.298-5.346,0.886-6.924,3.096-11.593,16.23-34.789,30.977-61.646,48.051-30.521,19.403-65.113,41.395-92.336,71.958-31.905,35.82-47.415,76.495-47.415,124.351 0,90.446 48.19,129.689 62.96,139.562 3.854,2.576 8.362,3.938 13.037,3.938h3.503c4.687,0 8.5,3.813 8.5,8.5s-3.813,8.5-8.5,8.5h-39.998c-1.754,0-3.454,0.615-4.801,1.738l-42.244,35.202c-5.375,4.478-8.457,11.058-8.457,18.054v40.504c0,4.142 3.358,7.5 7.5,7.5h304c4.142,0 7.5-3.358 7.5-7.5v-40.506c0-6.996-3.082-13.576-8.456-18.053l-42.244-35.203c-1.348-1.123-3.047-1.738-4.801-1.738h-39.999c-4.687,0-8.5-3.813-8.5-8.5s3.813-8.5 8.5-8.5h7.132c8.156,0 15.606-4.129 19.928-11.045l15.684-25.094c13.354-21.367 10.621-49.104-6.648-67.453l-85.432-90.772c8.883,1.34 20.024,2.364 33.337,2.364 30.493,0 44.728,15.705 57.287,29.561 4.204,4.639 8.175,9.02 12.461,12.389 6.195,4.87 15.047,4.362 20.592-1.183l26.963-26.964c2.929-2.929 2.922-7.685-0.007-10.614-1.788-1.786-2.773-4.162-2.773-6.689zm-31.241,252.5h-27.282c-4.142,0-7.5,3.358-7.5,7.5s3.358,7.5 7.5,7.5h39.998c0.168,0 0.333-0.014 0.499-0.025 0,0.006 0.001,0.013 0.001,0.019v33.006h-289v-33.006c0-0.006 0.001-0.013 0.001-0.019 0.166,0.011 0.331,0.025 0.499,0.025h168.003c4.142,0 7.5-3.358 7.5-7.5s-3.358-7.5-7.5-7.5h-155.286l30.001-25h202.566l30,25zm-89.186-40h-84.195c1.027-2.638 1.597-5.503 1.597-8.5s-0.57-5.862-1.597-8.5h84.195c-1.027,2.638-1.597,5.503-1.597,8.5s0.569,5.862 1.597,8.5zm112.592-229.795c-4.62,4.62-7.165,10.762-7.165,17.296 0,3.952 0.931,7.761 2.691,11.178l-22.482,22.483c-0.215,0.215-0.521,0.149-0.715-0.003-3.292-2.587-6.687-6.333-10.618-10.67-13.192-14.556-31.258-34.489-68.401-34.489-34.426,0-52.973-7.365-53.132-7.43-0.041-0.017-0.083-0.029-0.124-0.045-0.088-0.035-0.178-0.066-0.268-0.097-0.148-0.052-0.297-0.099-0.446-0.142-0.043-0.012-0.084-0.029-0.127-0.04-11.495-3.015-20.6-10.181-25.638-20.176-5.249-10.414-5.724-22.728-1.301-33.784 1.539-3.846-0.332-8.211-4.178-9.749-3.847-1.541-8.211,0.332-9.749,4.178-6.02,15.048-5.351,31.854 1.833,46.107 6.678,13.248 18.373,22.871 33.072,27.311l101.23,107.557c12.602,13.39 14.597,33.63 4.852,49.222l-15.684,25.094c-1.563,2.501-4.258,3.995-7.208,3.995h-138.635c-1.697,0-3.322-0.487-4.701-1.409-52.236-34.918-56.296-106.105-56.296-127.091 0-99.226 73.633-146.037 132.798-183.65 22.698-14.43 42.902-27.274 56.849-41.448 6.455,25.896-5.894,39.212-6.45,39.794-2.929,2.929-2.929,7.678 0,10.606l99.993,99.993c0.722,0.722 1.12,1.683 1.12,2.704s-0.398,1.982-1.12,2.705z"/>
<path d="m317.437,104.916c-17.317-9.447-29.686-2.67-38.716,2.278-4.707,2.579-8.772,4.806-12.875,4.806-4.142,0-7.5,3.358-7.5,7.5s3.358,7.5 7.5,7.5c7.943,0 14.392-3.534 20.083-6.651 1.538-0.843 2.997-1.642 4.417-2.348v1.499c0,4.142 3.358,7.5 7.5,7.5s7.5-3.358 7.5-7.5v-3.516c1.536,0.468 3.157,1.145 4.909,2.1 3.637,1.985 8.192,0.644 10.175-2.993 1.983-3.635 0.643-8.191-2.993-10.175z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -551,7 +551,7 @@ endif
data: data:
if not exist $(OBJS) mkdir $(OBJS) 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: data-image-sample:
if not exist $(OBJS) mkdir $(OBJS) if not exist $(OBJS) mkdir $(OBJS)

View File

@ -985,7 +985,7 @@ $(OBJS)\test_allheaders.exe: $(OBJS)\test_allheaders_dummy.obj $(TEST_ALLHEADER
data: data:
if not exist $(OBJS) mkdir $(OBJS) 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: data-image-sample:
if not exist $(OBJS) mkdir $(OBJS) if not exist $(OBJS) mkdir $(OBJS)

View File

@ -338,7 +338,7 @@
<wx-data id="data"> <wx-data id="data">
<files>testdata.fc</files> <files>testdata.fc horse.svg</files>
</wx-data> </wx-data>
<wx-data id="data-image-sample"> <wx-data id="data-image-sample">