]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsConverter.cpp
Fix pdf format conversion
[lyx.git] / src / graphics / GraphicsConverter.cpp
1 /**
2  * \file GraphicsConverter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GraphicsConverter.h"
14
15 #include "Converter.h"
16 #include "Format.h"
17
18 #include "support/lassert.h"
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/FileName.h"
22 #include "support/filetools.h"
23 #include "support/ForkedCalls.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26
27 #include "support/bind.h"
28
29 #include <sstream>
30 #include <fstream>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37 namespace graphics {
38
39 class Converter::Impl : public boost::signals::trackable {
40 public:
41         ///
42         Impl(FileName const &, string const &, string const &, string const &);
43
44         ///
45         void startConversion();
46
47         /** This method is connected to a signal passed to the forked call
48          *  class, passing control back here when the conversion is completed.
49          *  Cleans-up the temporary files, emits the finishedConversion
50          *  signal and removes the Converter from the list of all processes.
51          */
52         void converted(pid_t pid, int retval);
53
54         /** At the end of the conversion process inform the outside world
55          *  by emitting a signal.
56          */
57         typedef boost::signal<void(bool)> SignalType;
58         ///
59         SignalType finishedConversion;
60
61         ///
62         string script_command_;
63         ///
64         FileName script_file_;
65         ///
66         FileName to_file_;
67         ///
68         bool valid_process_;
69         ///
70         bool finished_;
71 };
72
73
74 bool Converter::isReachable(string const & from_format_name,
75                             string const & to_format_name)
76 {
77         return theConverters().isReachable(from_format_name, to_format_name);
78 }
79
80
81 Converter::Converter(FileName const & from_file, string const & to_file_base,
82                      string const & from_format, string const & to_format)
83         : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
84 {}
85
86
87 Converter::~Converter()
88 {
89         delete pimpl_;
90 }
91
92
93 void Converter::startConversion() const
94 {
95         pimpl_->startConversion();
96 }
97
98
99 boost::signals::connection Converter::connect(slot_type const & slot) const
100 {
101         return pimpl_->finishedConversion.connect(slot);
102 }
103
104
105 FileName const & Converter::convertedFile() const
106 {
107         static FileName const empty;
108         return pimpl_->finished_ ? pimpl_->to_file_ : empty;
109 }
110
111 /** Build the conversion script.
112  *  The script is output to the stream \p script.
113  */
114 static void build_script(string const & from_file, string const & to_file_base,
115                   string const & from_format, string const & to_format,
116                   ostream & script);
117
118
119 Converter::Impl::Impl(FileName const & from_file, string const & to_file_base,
120                       string const & from_format, string const & to_format)
121         : valid_process_(false), finished_(false)
122 {
123         LYXERR(Debug::GRAPHICS, "Converter c-tor:\n"
124                 << "\tfrom_file:      " << from_file
125                 << "\n\tto_file_base: " << to_file_base
126                 << "\n\tfrom_format:  " << from_format
127                 << "\n\tto_format:    " << to_format);
128
129         // The converted image is to be stored in this file (we do not
130         // use ChangeExtension because this is a basename which may
131         // nevertheless contain a '.')
132         to_file_ = FileName(to_file_base + '.' +  formats.extension(to_format));
133
134         // The conversion commands are stored in a stringstream
135         ostringstream script;
136         build_script(from_file.toFilesystemEncoding(),
137                      to_file_.toFilesystemEncoding(),
138                      from_format, to_format, script);
139         LYXERR(Debug::GRAPHICS, "\tConversion script:"
140                    "\n--------------------------------------\n"
141                 << script.str()
142                 << "\n--------------------------------------\n");
143
144         // Output the script to file.
145         static int counter = 0;
146         script_file_ = FileName(onlyPath(to_file_base) + "lyxconvert" +
147                 convert<string>(counter++) + ".py");
148
149         ofstream fs(script_file_.toFilesystemEncoding().c_str());
150         if (!fs.good()) {
151                 lyxerr << "Unable to write the conversion script to \""
152                        << script_file_ << '\n'
153                        << "Please check your directory permissions."
154                        << endl;
155                 return;
156         }
157
158         fs << script.str();
159         fs.close();
160
161         // The command needed to run the conversion process
162         // We create a dummy command for ease of understanding of the
163         // list of forked processes.
164         // Note: 'python ' is absolutely essential, or execvp will fail.
165         script_command_ = os::python() + ' ' +
166                 quoteName(script_file_.toFilesystemEncoding()) + ' ' +
167                 quoteName(onlyFileName(from_file.toFilesystemEncoding())) + ' ' +
168                 quoteName(to_format);
169         // All is ready to go
170         valid_process_ = true;
171 }
172
173
174 void Converter::Impl::startConversion()
175 {
176         if (!valid_process_) {
177                 converted(0, 1);
178                 return;
179         }
180
181         ForkedCall::SignalTypePtr ptr =
182                 ForkedCallQueue::add(script_command_);
183         ptr->connect(bind(&Impl::converted, this, _1, _2));
184 }
185
186
187 void Converter::Impl::converted(pid_t /* pid */, int retval)
188 {
189         if (finished_)
190                 // We're done already!
191                 return;
192
193         finished_ = true;
194         // Clean-up behind ourselves
195         script_file_.removeFile();
196
197         if (retval > 0) {
198                 to_file_.removeFile();
199                 to_file_.erase();
200                 finishedConversion(false);
201         } else {
202                 finishedConversion(true);
203         }
204 }
205
206
207 static string const move_file(string const & from_file, string const & to_file)
208 {
209         if (from_file == to_file)
210                 return string();
211
212         ostringstream command;
213         command << "fromfile = " << from_file << "\n"
214                 << "tofile = "   << to_file << "\n\n"
215                 << "try:\n"
216                 << "  os.rename(fromfile, tofile)\n"
217                 << "except:\n"
218                 << "  try:\n"
219                 << "    shutil.copy(fromfile, tofile)\n"
220                 << "  except:\n"
221                 << "    sys.exit(1)\n"
222                 << "  unlinkNoThrow(fromfile)\n";
223
224         return command.str();
225 }
226
227
228 static void build_conversion_command(string const & command, ostream & script)
229 {
230         // Store in the python script
231         script << "\nif os.system(r'" << command << "') != 0:\n";
232
233         // Test that this was successful. If not, remove
234         // ${outfile} and exit the python script
235         script << "  unlinkNoThrow(outfile)\n"
236                << "  sys.exit(1)\n\n";
237
238         // Test that the outfile exists.
239         // ImageMagick's convert will often create ${outfile}.0,
240         // ${outfile}.1.
241         // If this occurs, move ${outfile}.0 to ${outfile}
242         // and delete ${outfile}.? (ignore errors)
243         script << "if not os.path.isfile(outfile):\n"
244                   "  if os.path.isfile(outfile + '.0'):\n"
245                   "    os.rename(outfile + '.0', outfile)\n"
246                   "    import glob\n"
247                   "    for file in glob.glob(outfile + '.?'):\n"
248                   "      unlinkNoThrow(file)\n"
249                   "  else:\n"
250                   "    sys.exit(1)\n\n";
251
252         // Delete the infile
253         script << "if infile != outfile:\n"
254                   "  unlinkNoThrow(infile)\n\n";
255 }
256
257
258 static string const strip_digit(string const & format)
259 {
260         // Strip trailing digits from format names e.g. "pdf6" -> "pdf"
261         return format.substr(0, format.find_last_not_of("0123456789") + 1);
262 }
263
264
265 static void build_script(string const & from_file,
266                   string const & to_file,
267                   string const & from_format,
268                   string const & to_format,
269                   ostream & script)
270 {
271         LASSERT(from_format != to_format, return);
272         LYXERR(Debug::GRAPHICS, "build_script ... ");
273         typedef Graph::EdgePath EdgePath;
274
275         script << "#!/usr/bin/env python\n"
276                   "# -*- coding: utf-8 -*-\n"
277                   "import os, shutil, sys\n\n"
278                   "def unlinkNoThrow(file):\n"
279                   "  ''' remove a file, do not throw if an error occurs '''\n"
280                   "  try:\n"
281                   "    os.unlink(file)\n"
282                   "  except:\n"
283                   "    pass\n\n";
284
285         EdgePath const edgepath = from_format.empty() ?
286                 EdgePath() :
287                 theConverters().getPath(from_format, to_format);
288
289         // Create a temporary base file-name for all intermediate steps.
290         // Remember to remove the temp file because we only want the name...
291         static int counter = 0;
292         string const tmp = "gconvert" + convert<string>(counter++);
293         string const to_base = FileName::tempName(tmp).toFilesystemEncoding();
294
295         // Create a copy of the file in case the original name contains
296         // problematic characters like ' or ". We can work around that problem
297         // in python, but the converters might be shell scripts and have more
298         // troubles with it.
299         string outfile = addExtension(to_base, getExtension(from_file));
300         script << "infile = "
301                         << quoteName(from_file, quote_python)
302                         << "\n"
303                   "outfile = "
304                         << quoteName(outfile, quote_python) << "\n"
305                   "shutil.copy(infile, outfile)\n";
306
307         // Some converters (e.g. lilypond) can only output files to the
308         // current directory, so we need to change the current directory.
309         // This has the added benefit that all other files that may be
310         // generated by the converter are deleted when LyX closes and do not
311         // clutter the real working directory.
312         script << "os.chdir("
313                << quoteName(onlyPath(outfile)) << ")\n";
314
315         if (edgepath.empty()) {
316                 // Either from_format is unknown or we don't have a
317                 // converter path from from_format to to_format, so we use
318                 // the default converter.
319                 script << "infile = outfile\n"
320                        << "outfile = "
321                        << quoteName(to_file, quote_python) << "\n";
322
323                 ostringstream os;
324                 os << os::python() << ' '
325                    << libScriptSearch("$$s/scripts/convertDefault.py",
326                                       quote_python) << ' ';
327                 if (!from_format.empty())
328                         os << strip_digit(from_format) << ':';
329                 // The extra " quotes around infile and outfile are needed
330                 // because the filename may contain spaces and it is used
331                 // as argument of os.system().
332                 os << "' + '\"' + infile + '\"' + ' "
333                    << strip_digit(to_format) << ":' + '\"' + outfile + '\"' + '";
334                 string const command = os.str();
335
336                 LYXERR(Debug::GRAPHICS,
337                         "\tNo converter defined! I use convertDefault.py\n\t"
338                         << command);
339
340                 build_conversion_command(command, script);
341         }
342
343         // The conversion commands may contain these tokens that need to be
344         // changed to infile, infile_base, outfile and output directory respectively.
345         string const token_from  = "$$i";
346         string const token_base  = "$$b";
347         string const token_to    = "$$o";
348         string const token_todir = "$$d";
349
350         EdgePath::const_iterator it  = edgepath.begin();
351         EdgePath::const_iterator end = edgepath.end();
352
353         for (; it != end; ++it) {
354                 lyx::Converter const & conv = theConverters().get(*it);
355
356                 // Build the conversion command
357                 string const infile      = outfile;
358                 string const infile_base = changeExtension(infile, string());
359                 outfile = conv.result_file.empty()
360                         ? addExtension(to_base, conv.To->extension())
361                         : addName(subst(conv.result_dir,
362                                         token_base, infile_base),
363                                   subst(conv.result_file,
364                                         token_base, onlyFileName(infile_base)));
365
366                 // If two formats share the same extension we may get identical names
367                 if (outfile == infile && conv.result_file.empty()) {
368                         string const new_base = FileName::tempName(tmp).toFilesystemEncoding();
369                         outfile = addExtension(new_base, conv.To->extension());
370                 }
371
372                 // Store these names in the python script
373                 script << "infile = "
374                                 << quoteName(infile, quote_python) << "\n"
375                           "infile_base = "
376                                 << quoteName(infile_base, quote_python) << "\n"
377                           "outfile = "
378                                 << quoteName(outfile, quote_python) << "\n"
379                           "outdir  = os.path.dirname(outfile)\n" ;
380
381                 // See comment about extra " quotes above (although that
382                 // applies only for the first loop run here).
383                 string command = conv.command;
384                 command = subst(command, token_from,  "' + '\"' + infile + '\"' + '");
385                 command = subst(command, token_base,  "' + '\"' + infile_base + '\"' + '");
386                 command = subst(command, token_to,    "' + '\"' + outfile + '\"' + '");
387                 command = subst(command, token_todir, "' + '\"' + outdir + '\"' + '");
388                 command = libScriptSearch(command, quote_python);
389
390                 build_conversion_command(command, script);
391         }
392
393         // Move the final outfile to to_file
394         script << move_file("outfile", quoteName(to_file, quote_python));
395         LYXERR(Debug::GRAPHICS, "ready!");
396 }
397
398 } // namespace graphics
399 } // namespace lyx