Menu

[r135]: / src / xmlcopyimg.cpp  Maximize  Restore  History

Download this file

205 lines (179 with data), 6.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <string>
#include <vector>
#include <stdexcept>
#include <expat.h>
#include <cstring>
#include <wx/wx.h>
#include <wx/filename.h>
#include "xmlcopyimg.h"
#include "xmlcopyeditorcopy.h"
XmlCopyImg::XmlCopyImg (
const wxString& blankImage,
const wxString& imagesDir,
const wxString& mediaDir,
const wxString& path,
bool parseDeclaration,
bool expandInternalEntities,
size_t size ) :
d ( new ImgData() )
{
wxCopyFile (
blankImage,
imagesDir + wxFileName::GetPathSeparator() + _T("blank.jpg"),
true );
d->buffer.reserve ( size );
d->blankImage = blankImage;
d->imagesDir = imagesDir;
d->mediaDir = mediaDir;
// use only dir component of path
wxString volumeComponent, pathComponent;
wxFileName::SplitPath ( path, &volumeComponent, &pathComponent, NULL, NULL );
d->path = volumeComponent + _T(":") + pathComponent;
XML_SetUserData ( p, d.get() );
// parse declaration?
if ( parseDeclaration )
XML_SetXmlDeclHandler ( p, xmldeclhandler );
// internal entities
if ( expandInternalEntities )
XML_SetDefaultHandlerExpand ( p, defaulthandler );
else
XML_SetDefaultHandler ( p, defaulthandler );
XML_SetElementHandler ( p, start, end );
}
XmlCopyImg::~XmlCopyImg()
{}
void XMLCALL XmlCopyImg::xmldeclhandler (
void *data,
const XML_Char *version,
const XML_Char *encoding,
int standalone )
{
ImgData *d;
d = ( ImgData * ) data;
d->encoding = ( encoding ) ? encoding : "UTF-8";
d->buffer.append ( "<?xml version=\"" );
d->buffer.append ( version );
d->buffer.append ( "\" encoding=\"" );
d->buffer.append ( d->encoding );
d->buffer.append ( "\"" );
if ( standalone != -1 )
{
d->buffer.append ( " standalone=\"" );
d->buffer.append ( ( standalone == 1 ) ? "yes" : "no" );
d->buffer.append ( "\"" );
}
d->buffer.append ( "?>" );
}
void XMLCALL XmlCopyImg::defaulthandler (
void *data,
const XML_Char *s,
int len )
{
ImgData *d;
d = ( ImgData * ) data;
d->buffer.append ( s, len );
}
void XMLCALL XmlCopyImg::start ( void *data,
const XML_Char *el,
const XML_Char **attr )
{
ImgData *pd;
pd = ( ImgData * ) data;
std::string tag, value;
tag += "<";
tag += el;
wxString wideFile, wideDestination;
for ( ; *attr; attr += 2 )
{
value = ( const char * ) *(attr + 1);
if ( !strcmp ( el, "img" ) && !strcmp (*attr, "src") )
{
wideFile = wxString ( value.c_str(), wxConvUTF8, value.size() );
wxFileName fn ( wideFile );
if ( fn.IsRelative() )
{
fn.MakeAbsolute ( pd->path );
wideFile = fn.GetFullPath();
}
if ( !wxFileName::FileExists ( wideFile ) )
{
wideDestination = pd->blankImage;
value = "images/blank.jpg";
}
else
{
value = "images/";
value += fn.GetFullName().mb_str( wxConvUTF8 );
wideDestination = pd->imagesDir +
wxFileName::GetPathSeparator() +
fn.GetFullName();
wxCopyFile ( wideFile, wideDestination, true );
}
if (
wideDestination.Contains ( _T(".eps" ) ) ||
wideDestination.Contains ( _T(".tif") ) ||
wideDestination.Contains ( _T(".bmp") ) )
{
wxString cmd;
cmd += IMAGEMAGICK_CONVERT_PATH;
cmd += _T(" -resize 720x720>");
cmd += _T(" \"");
cmd += wideFile;
cmd += _T("\" \"");
cmd += wideDestination;
cmd += _T(".png\"");
if ( wxExecute ( cmd, wxEXEC_SYNC ) )
{
wxString error;
error.Printf ( _T("Please ensure that ImageMagick is installed on your system and that the conversion tool is located at:\n\n%s\n\nYou can download ImageMagick from:\n\n%s\n\nIf your document contains encapsulated PostScript (EPS) graphics, you need to install GhostScript as well.\n\nYou can download GhostScript from:\n\n%s"),
IMAGEMAGICK_CONVERT_PATH,
IMAGEMAGICK_INSTALL_URL,
GHOSTSCRIPT_INSTALL_URL );
wxMessageBox ( error, _T("ImageMagick conversion failed") );
}
value += ".png";
}
wideDestination.clear();
} // img + src
else if ( !strcmp ( el, "a" ) &&
!strcmp (*attr, "href") &&
( strstr ( value.c_str(), ".mp3" ) || strstr ( value.c_str(), ".pdf" ) ) )
{
wideFile = wxString ( value.c_str(), wxConvUTF8, value.size() );
wxFileName fn ( wideFile );
if ( fn.IsRelative() )
{
fn.MakeAbsolute ( pd->path );
wideFile = fn.GetFullPath();
}
if ( !wxFileName::FileExists ( wideFile ) )
{
value = "images/blank.jpg";
}
else
{
value = "media/";
value += fn.GetFullName().mb_str( wxConvUTF8 );
wideDestination = pd->mediaDir +
wxFileName::GetPathSeparator() +
fn.GetFullName();
wxCopyFile ( wideFile, wideDestination, true );
}
} // a + href
tag += " ";
tag += *attr;
tag += "=\"";
tag += value.c_str();
tag += "\"";
}
tag += ">";
pd->buffer += tag;
}
void XMLCALL XmlCopyImg::end ( void *data, const XML_Char *el )
{
ImgData *pd;
pd = ( ImgData * ) data;
pd->buffer += "</";
pd->buffer += el;
pd->buffer += ">";
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.