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