1 //////////////////////////////////////////////////////////////////////////////
2 // Name:        imagsvg.cpp
3 // Purpose:     SVG Image Handler
4 // Author:      Alex Thuering
5 // Created:     2011/11/22
6 // RCS-ID:      $Id: imagsvg.cpp,v 1.2 2011/12/27 08:20:32 ntalex Exp $
7 // Copyright:   (c) Alex Thuering
8 // Licence:     wxWindows licence
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #include "imagsvg.h"
12 #include "SVGDocument.h"
13 
IMPLEMENT_DYNAMIC_CLASS(wxSVGHandler,wxImageHandler)14 IMPLEMENT_DYNAMIC_CLASS(wxSVGHandler, wxImageHandler)
15 
16 wxSVGHandler::wxSVGHandler() {
17 	m_name = wxT("SVG file");
18 	m_extension = wxT("svg");
19 	m_type = (wxBitmapType) wxBITMAP_TYPE_SVG;
20 	m_mime = wxT("image/svg");
21 }
22 
~wxSVGHandler()23 wxSVGHandler::~wxSVGHandler() {
24 	// nothing to do
25 }
26 
LoadFile(wxImage * image,wxInputStream & stream,bool verbose,int index)27 bool wxSVGHandler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int index) {
28 	// save this before calling Destroy()
29 	int maxWidth = image->HasOption(wxT("max_width")) ? image->GetOptionInt(wxT("max_width")) : -1;
30 	int maxHeight = image->HasOption(wxT("max_height")) ? image->GetOptionInt(wxT("max_height")) : -1;
31 	image->Destroy();
32 
33 	wxSVGDocument imgDoc;
34 	if (!imgDoc.Load(stream))
35 		return false;
36 
37 	*image = imgDoc.Render(maxWidth, maxHeight);
38 	return true;
39 }
40 
SaveFile(wxImage * image,wxOutputStream & stream,bool verbose)41 bool wxSVGHandler::SaveFile(wxImage *image, wxOutputStream& stream, bool verbose) {
42 	return false; // not implemented
43 }
44 
DoCanRead(wxInputStream & stream)45 bool wxSVGHandler::DoCanRead(wxInputStream& stream) {
46 	unsigned char hdr[5];
47 
48 	if (!stream.Read(hdr, WXSIZEOF(hdr))) // it's ok to modify the stream position here
49 		return false;
50 
51 	return hdr[0] == '<' && hdr[1] == '?' && hdr[2] == 'x' && hdr[3] == 'm' && hdr[4] == 'l'; // <?xml
52 }
53