WxSVGFileDC

From WxWiki
Jump to navigation Jump to search

wxSVGFileDC (class reference) is a device context that enables the user to draw vector graphics to an SVG file. There is, however, no support for rendering the SVG itself; libraries such as wxSVG, or even better, wxArt2D can be used for that task. The wxSVGFileDC class is not part of the main wxWidgets source tree, but is in instead found in the contrib/ folder off the main wxWidgets folder. Additionally, there is a decent SVG test example found in contrib/samples/svg/.

Example Code

wxSVGFileDC svgDC (filename, 600, 650) ;    

svgDC.SetBrush(*wxCYAN_BRUSH);
svgDC.SetPen(*wxRED_PEN);
svgDC.DrawRectangle(10, 10, 100, 70);
wB = wxBrush (_T("DARK ORCHID"), wxTRANSPARENT);
svgDC.SetBrush (wB);
svgDC.DrawRoundedRectangle(50, 50, 100, 70, 20);
svgDC.SetBrush (wxBrush(_T("GOLDENROD"), wxSOLID) );
svgDC.DrawEllipse(100, 100, 100, 50);

points[0].x = 100; points[0].y = 200;
points[1].x = 70; points[1].y = 260;
points[2].x = 160; points[2].y = 230;
points[3].x = 40; points[3].y = 230;
points[4].x = 130; points[4].y = 260;
points[5].x = 100; points[5].y = 200;

svgDC.DrawPolygon(5, points);
svgDC.DrawLines (6, points, 160);

return svgDC.Ok();

Notes on the SVG output

Browsers, such a Mozilla Firefox, may not render the SVG as a graphic, but rather interpret it as an XML file. This is because the XML namespace is not defined in the <svg> tag. For example the raw output of wxSVGFileDC:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" 
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> 
<svg width="21cm" height="23cm" viewBox="0 0 600 650 ">

...
 
</svg> 

Adding a namespace to the svg tag appears to fix this:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" 
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> 
<svg width="21cm" height="23cm" viewBox="0 0 600 650 " 
    xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

...
 
</svg> 

Compiling for Win32

  • Go to $(WXWIN)\contrib\build\svg
  • Double-click svg.dsw to start the project conversion process. Important: Use svg.dsw and not svg.vcp, or it will convert for portable devices.
  • After the conversion completes, the library can be compiled for Win32. The library will be placed in the standard location.

To use wxSVGFileDC, add $(WXWIN)\contrib\include to your list of include folders.