]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewLoader.cpp
10eeee321d580f31b97ae07368054c52a6e2ba27
[lyx.git] / src / graphics / PreviewLoader.cpp
1 /**
2  * \file PreviewLoader.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 "PreviewLoader.h"
14 #include "PreviewImage.h"
15 #include "GraphicsCache.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "Converter.h"
20 #include "Encoding.h"
21 #include "Format.h"
22 #include "InsetIterator.h"
23 #include "LaTeXFeatures.h"
24 #include "LyXRC.h"
25 #include "output.h"
26 #include "OutputParams.h"
27 #include "TexRow.h"
28
29 #include "frontends/Application.h" // hexName
30
31 #include "insets/Inset.h"
32
33 #include "support/convert.h"
34 #include "support/debug.h"
35 #include "support/FileName.h"
36 #include "support/filetools.h"
37 #include "support/ForkedCalls.h"
38 #include "support/lstrings.h"
39
40 #include "support/bind.h"
41
42 #include <sstream>
43 #include <fstream>
44 #include <iomanip>
45
46 using namespace std;
47 using namespace lyx::support;
48
49
50
51 namespace {
52
53 typedef pair<string, FileName> SnippetPair;
54
55 // A list of all snippets to be converted to previews
56 typedef list<string> PendingSnippets;
57
58 // Each item in the vector is a pair<snippet, image file name>.
59 typedef vector<SnippetPair> BitmapFile;
60
61
62 string const unique_filename(string const & bufferpath)
63 {
64         static int theCounter = 0;
65         string const filename = lyx::convert<string>(theCounter++) + "lyxpreview";
66         return addName(bufferpath, filename);
67 }
68
69
70 lyx::Converter const * setConverter(string const from)
71 {
72         typedef vector<string> FmtList;
73         typedef lyx::graphics::Cache GCache;
74         FmtList const & loadableFormats = GCache::get().loadableFormats();
75         FmtList::const_iterator it = loadableFormats.begin();
76         FmtList::const_iterator const end = loadableFormats.end();
77
78         for (; it != end; ++it) {
79                 string const to = *it;
80                 if (from == to)
81                         continue;
82
83                 lyx::Converter const * ptr = lyx::theConverters().getConverter(from, to);
84                 if (ptr)
85                         return ptr;
86         }
87
88         static bool first = true;
89         if (first) {
90                 first = false;
91                 LYXERR0("PreviewLoader::startLoading()\n"
92                         << "No converter from \"" << from << "\" format has been defined.");
93         }
94         return 0;
95 }
96
97
98 void setAscentFractions(vector<double> & ascent_fractions,
99                         FileName const & metrics_file)
100 {
101         // If all else fails, then the images will have equal ascents and
102         // descents.
103         vector<double>::iterator it  = ascent_fractions.begin();
104         vector<double>::iterator end = ascent_fractions.end();
105         fill(it, end, 0.5);
106
107         ifstream in(metrics_file.toFilesystemEncoding().c_str());
108         if (!in.good()) {
109                 LYXERR(lyx::Debug::GRAPHICS, "setAscentFractions(" << metrics_file << ")\n"
110                         << "Unable to open file!");
111                 return;
112         }
113
114         bool error = false;
115
116         int snippet_counter = 1;
117         while (!in.eof() && it != end) {
118                 string snippet;
119                 int id;
120                 double ascent_fraction;
121
122                 in >> snippet >> id >> ascent_fraction;
123
124                 if (!in.good())
125                         // eof after all
126                         break;
127
128                 error = snippet != "Snippet";
129                 if (error)
130                         break;
131
132                 error = id != snippet_counter;
133                 if (error)
134                         break;
135
136                 *it = ascent_fraction;
137
138                 ++snippet_counter;
139                 ++it;
140         }
141
142         if (error) {
143                 LYXERR(lyx::Debug::GRAPHICS, "setAscentFractions(" << metrics_file << ")\n"
144                         << "Error reading file!\n");
145         }
146 }
147
148
149 class FindFirst
150 {
151 public:
152         FindFirst(string const & comp) : comp_(comp) {}
153         bool operator()(SnippetPair const & sp) const { return sp.first == comp_; }
154 private:
155         string const comp_;
156 };
157
158
159 /// Store info on a currently executing, forked process.
160 class InProgress {
161 public:
162         ///
163         InProgress() : pid(0) {}
164         ///
165         InProgress(string const & filename_base,
166                    PendingSnippets const & pending,
167                    string const & to_format);
168         /// Remove any files left lying around and kill the forked process.
169         void stop() const;
170
171         ///
172         pid_t pid;
173         ///
174         string command;
175         ///
176         FileName metrics_file;
177         ///
178         BitmapFile snippets;
179 };
180
181 typedef map<pid_t, InProgress>  InProgressProcesses;
182
183 typedef InProgressProcesses::value_type InProgressProcess;
184
185 } // namespace anon
186
187
188
189 namespace lyx {
190 namespace graphics {
191
192 class PreviewLoader::Impl : public boost::signals::trackable {
193 public:
194         ///
195         Impl(PreviewLoader & p, Buffer const & b);
196         /// Stop any InProgress items still executing.
197         ~Impl();
198         ///
199         PreviewImage const * preview(string const & latex_snippet) const;
200         ///
201         PreviewLoader::Status status(string const & latex_snippet) const;
202         ///
203         void add(string const & latex_snippet);
204         ///
205         void remove(string const & latex_snippet);
206         /// \p wait whether to wait for the process to complete or, instead,
207         /// to do it in the background.
208         void startLoading(bool wait = false);
209
210         /// Emit this signal when an image is ready for display.
211         boost::signal<void(PreviewImage const &)> imageReady;
212
213         Buffer const & buffer() const { return buffer_; }
214
215 private:
216         /// Called by the ForkedCall process that generated the bitmap files.
217         void finishedGenerating(pid_t, int);
218         ///
219         void dumpPreamble(otexstream &) const;
220         ///
221         void dumpData(odocstream &, BitmapFile const &) const;
222
223         /** cache_ allows easy retrieval of already-generated images
224          *  using the LaTeX snippet as the identifier.
225          */
226         typedef shared_ptr<PreviewImage> PreviewImagePtr;
227         ///
228         typedef map<string, PreviewImagePtr> Cache;
229         ///
230         Cache cache_;
231
232         /** pending_ stores the LaTeX snippets in anticipation of them being
233          *  sent to the converter.
234          */
235         PendingSnippets pending_;
236
237         /** in_progress_ stores all forked processes so that we can proceed
238          *  thereafter.
239             The map uses the conversion commands as its identifiers.
240          */
241         InProgressProcesses in_progress_;
242
243         ///
244         PreviewLoader & parent_;
245         ///
246         Buffer const & buffer_;
247
248         /// We don't own this
249         static lyx::Converter const * pconverter_;
250 };
251
252
253 lyx::Converter const * PreviewLoader::Impl::pconverter_;
254
255
256 //
257 // The public interface, defined in PreviewLoader.h
258 //
259
260 PreviewLoader::PreviewLoader(Buffer const & b)
261         : pimpl_(new Impl(*this, b))
262 {}
263
264
265 PreviewLoader::~PreviewLoader()
266 {
267         delete pimpl_;
268 }
269
270
271 PreviewImage const * PreviewLoader::preview(string const & latex_snippet) const
272 {
273         return pimpl_->preview(latex_snippet);
274 }
275
276
277 PreviewLoader::Status PreviewLoader::status(string const & latex_snippet) const
278 {
279         return pimpl_->status(latex_snippet);
280 }
281
282
283 void PreviewLoader::add(string const & latex_snippet) const
284 {
285         pimpl_->add(latex_snippet);
286 }
287
288
289 void PreviewLoader::remove(string const & latex_snippet) const
290 {
291         pimpl_->remove(latex_snippet);
292 }
293
294
295 void PreviewLoader::startLoading(bool wait) const
296 {
297         pimpl_->startLoading(wait);
298 }
299
300
301 boost::signals::connection PreviewLoader::connect(slot_type const & slot) const
302 {
303         return pimpl_->imageReady.connect(slot);
304 }
305
306
307 void PreviewLoader::emitSignal(PreviewImage const & pimage) const
308 {
309         pimpl_->imageReady(pimage);
310 }
311
312
313 Buffer const & PreviewLoader::buffer() const
314 {
315         return pimpl_->buffer();
316 }
317
318 } // namespace graphics
319 } // namespace lyx
320
321
322 // The details of the Impl
323 // =======================
324
325 namespace {
326
327 class IncrementedFileName {
328 public:
329         IncrementedFileName(string const & to_format,
330                             string const & filename_base)
331                 : to_format_(to_format), base_(filename_base), counter_(1)
332         {}
333
334         SnippetPair const operator()(string const & snippet)
335         {
336                 ostringstream os;
337                 os << base_ << counter_++ << '.' << to_format_;
338                 string const file = os.str();
339
340                 return make_pair(snippet, FileName(file));
341         }
342
343 private:
344         string const & to_format_;
345         string const & base_;
346         int counter_;
347 };
348
349
350 InProgress::InProgress(string const & filename_base,
351                        PendingSnippets const & pending,
352                        string const & to_format)
353         : pid(0),
354           metrics_file(filename_base + ".metrics"),
355           snippets(pending.size())
356 {
357         PendingSnippets::const_iterator pit  = pending.begin();
358         PendingSnippets::const_iterator pend = pending.end();
359         BitmapFile::iterator sit = snippets.begin();
360
361         transform(pit, pend, sit,
362                        IncrementedFileName(to_format, filename_base));
363 }
364
365
366 void InProgress::stop() const
367 {
368         if (pid)
369                 ForkedCallsController::kill(pid, 0);
370
371         if (!metrics_file.empty())
372                 metrics_file.removeFile();
373
374         BitmapFile::const_iterator vit  = snippets.begin();
375         BitmapFile::const_iterator vend = snippets.end();
376         for (; vit != vend; ++vit) {
377                 if (!vit->second.empty())
378                         vit->second.removeFile();
379         }
380 }
381
382 } // namespace anon
383
384
385 namespace lyx {
386 namespace graphics {
387
388 PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
389         : parent_(p), buffer_(b)
390 {
391         if (!pconverter_)
392                 pconverter_ = setConverter("lyxpreview");
393 }
394
395
396 PreviewLoader::Impl::~Impl()
397 {
398         InProgressProcesses::iterator ipit  = in_progress_.begin();
399         InProgressProcesses::iterator ipend = in_progress_.end();
400
401         for (; ipit != ipend; ++ipit)
402                 ipit->second.stop();
403 }
404
405
406 PreviewImage const *
407 PreviewLoader::Impl::preview(string const & latex_snippet) const
408 {
409         Cache::const_iterator it = cache_.find(latex_snippet);
410         return (it == cache_.end()) ? 0 : it->second.get();
411 }
412
413
414 namespace {
415
416 class FindSnippet {
417 public:
418         FindSnippet(string const & s) : snippet_(s) {}
419         bool operator()(InProgressProcess const & process) const
420         {
421                 BitmapFile const & snippets = process.second.snippets;
422                 BitmapFile::const_iterator beg  = snippets.begin();
423                 BitmapFile::const_iterator end = snippets.end();
424                 return find_if(beg, end, FindFirst(snippet_)) != end;
425         }
426
427 private:
428         string const snippet_;
429 };
430
431 } // namespace anon
432
433 PreviewLoader::Status
434 PreviewLoader::Impl::status(string const & latex_snippet) const
435 {
436         Cache::const_iterator cit = cache_.find(latex_snippet);
437         if (cit != cache_.end())
438                 return Ready;
439
440         PendingSnippets::const_iterator pit  = pending_.begin();
441         PendingSnippets::const_iterator pend = pending_.end();
442
443         pit = find(pit, pend, latex_snippet);
444         if (pit != pend)
445                 return InQueue;
446
447         InProgressProcesses::const_iterator ipit  = in_progress_.begin();
448         InProgressProcesses::const_iterator ipend = in_progress_.end();
449
450         ipit = find_if(ipit, ipend, FindSnippet(latex_snippet));
451         if (ipit != ipend)
452                 return Processing;
453
454         return NotFound;
455 }
456
457
458 void PreviewLoader::Impl::add(string const & latex_snippet)
459 {
460         if (!pconverter_ || status(latex_snippet) != NotFound)
461                 return;
462
463         string const snippet = trim(latex_snippet);
464         if (snippet.empty())
465                 return;
466
467         LYXERR(Debug::GRAPHICS, "adding snippet:\n" << snippet);
468
469         pending_.push_back(snippet);
470 }
471
472
473 namespace {
474
475 class EraseSnippet {
476 public:
477         EraseSnippet(string const & s) : snippet_(s) {}
478         void operator()(InProgressProcess & process)
479         {
480                 BitmapFile & snippets = process.second.snippets;
481                 BitmapFile::iterator it  = snippets.begin();
482                 BitmapFile::iterator end = snippets.end();
483
484                 it = find_if(it, end, FindFirst(snippet_));
485                 if (it != end)
486                         snippets.erase(it, it+1);
487         }
488
489 private:
490         string const & snippet_;
491 };
492
493 } // namespace anon
494
495
496 void PreviewLoader::Impl::remove(string const & latex_snippet)
497 {
498         Cache::iterator cit = cache_.find(latex_snippet);
499         if (cit != cache_.end())
500                 cache_.erase(cit);
501
502         PendingSnippets::iterator pit  = pending_.begin();
503         PendingSnippets::iterator pend = pending_.end();
504
505         pending_.erase(std::remove(pit, pend, latex_snippet), pend);
506
507         InProgressProcesses::iterator ipit  = in_progress_.begin();
508         InProgressProcesses::iterator ipend = in_progress_.end();
509
510         for_each(ipit, ipend, EraseSnippet(latex_snippet));
511
512         while (ipit != ipend) {
513                 InProgressProcesses::iterator curr = ipit++;
514                 if (curr->second.snippets.empty())
515                         in_progress_.erase(curr);
516         }
517 }
518
519
520 void PreviewLoader::Impl::startLoading(bool wait)
521 {
522         if (pending_.empty() || !pconverter_)
523                 return;
524
525         // Only start the process off after the buffer is loaded from file.
526         if (!buffer_.isFullyLoaded())
527                 return;
528
529         LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()");
530
531         // As used by the LaTeX file and by the resulting image files
532         string const directory = buffer_.temppath();
533
534         string const filename_base = unique_filename(directory);
535
536         // Create an InProgress instance to place in the map of all
537         // such processes if it starts correctly.
538         InProgress inprogress(filename_base, pending_, pconverter_->to);
539
540         // clear pending_, so we're ready to start afresh.
541         pending_.clear();
542
543         // Output the LaTeX file.
544         FileName const latexfile(filename_base + ".tex");
545
546         // we use the encoding of the buffer
547         Encoding const & enc = buffer_.params().encoding();
548         ofdocstream of;
549         try { of.reset(enc.iconvName()); }
550         catch (iconv_codecvt_facet_exception const & e) {
551                 LYXERR0("Caught iconv exception: " << e.what()
552                         << "\nUnable to create LaTeX file: " << latexfile);
553                 return;
554         }
555
556         TexRow texrow;
557         otexstream os(of, texrow);
558         OutputParams runparams(&enc);
559         LaTeXFeatures features(buffer_, buffer_.params(), runparams);
560
561         if (!openFileWrite(of, latexfile))
562                 return;
563
564         if (!of) {
565                 LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
566                                         << "Unable to create LaTeX file\n" << latexfile);
567                 return;
568         }
569         of << "\\batchmode\n";
570         dumpPreamble(os);
571         // handle inputenc etc.
572         buffer_.params().writeEncodingPreamble(os, features);
573         of << "\n\\begin{document}\n";
574         dumpData(of, inprogress.snippets);
575         of << "\n\\end{document}\n";
576         of.close();
577         if (of.fail()) {
578                 LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
579                                          << "File was not closed properly.");
580                 return;
581         }
582
583         double const font_scaling_factor = 
584                 buffer_.isExporting() ? 75.0 * buffer_.params().html_math_img_scale 
585                         : 0.01 * lyxrc.dpi * lyxrc.zoom * lyxrc.preview_scale_factor;
586         
587         // The conversion command.
588         ostringstream cs;
589         cs << pconverter_->command
590            << " " << quoteName(latexfile.toFilesystemEncoding())
591            << " --dpi " << int(font_scaling_factor);
592
593         // FIXME XHTML 
594         // The colors should be customizable.
595         if (!buffer_.isExporting()) {
596                 ColorCode const fg = PreviewLoader::foregroundColor();
597                 ColorCode const bg = PreviewLoader::backgroundColor();
598                 cs << " --fg " << theApp()->hexName(fg) 
599                    << " --bg " << theApp()->hexName(bg);
600         }
601
602         // FIXME what about LuaTeX?
603         if (buffer_.params().useNonTeXFonts)
604                 cs << " --latex=xelatex";
605         if (buffer_.params().encoding().package() == Encoding::japanese)
606                 cs << " --latex=platex";
607         if (buffer_.params().bibtex_command != "default")
608                 cs << " --bibtex=" << quoteName(buffer_.params().bibtex_command);
609         else if (buffer_.params().encoding().package() == Encoding::japanese)
610                 cs << " --bibtex=" << quoteName(lyxrc.jbibtex_command);
611         else
612                 cs << " --bibtex=" << quoteName(lyxrc.bibtex_command);
613         if (buffer_.params().bufferFormat() == "lilypond-book")
614                 cs << " --lilypond";
615
616         string const command = libScriptSearch(cs.str());
617
618         if (wait) {
619                 ForkedCall call(buffer_.filePath());
620                 int ret = call.startScript(ForkedProcess::Wait, command);
621                 static int fake = (2^20) + 1;
622                 int pid = fake++;
623                 inprogress.pid = pid;
624                 inprogress.command = command;
625                 in_progress_[pid] = inprogress;
626                 finishedGenerating(pid, ret);
627                 return;
628         }
629
630         // Initiate the conversion from LaTeX to bitmap images files.
631         ForkedCall::SignalTypePtr
632                 convert_ptr(new ForkedCall::SignalType);
633         convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));
634
635         ForkedCall call(buffer_.filePath());
636         int ret = call.startScript(command, convert_ptr);
637
638         if (ret != 0) {
639                 LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
640                                         << "Unable to start process\n" << command);
641                 return;
642         }
643
644         // Store the generation process in a list of all such processes
645         inprogress.pid = call.pid();
646         inprogress.command = command;
647         in_progress_[inprogress.pid] = inprogress;
648 }
649
650
651 void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
652 {
653         // Paranoia check!
654         InProgressProcesses::iterator git = in_progress_.find(pid);
655         if (git == in_progress_.end()) {
656                 lyxerr << "PreviewLoader::finishedGenerating(): unable to find "
657                         "data for PID " << pid << endl;
658                 return;
659         }
660
661         string const command = git->second.command;
662         string const status = retval > 0 ? "failed" : "succeeded";
663         LYXERR(Debug::GRAPHICS, "PreviewLoader::finishedInProgress("
664                                 << retval << "): processing " << status
665                                 << " for " << command);
666         if (retval > 0)
667                 return;
668
669         // Read the metrics file, if it exists
670         vector<double> ascent_fractions(git->second.snippets.size());
671         setAscentFractions(ascent_fractions, git->second.metrics_file);
672
673         // Add these newly generated bitmap files to the cache and
674         // start loading them into LyX.
675         BitmapFile::const_iterator it  = git->second.snippets.begin();
676         BitmapFile::const_iterator end = git->second.snippets.end();
677
678         list<PreviewImagePtr> newimages;
679
680         int metrics_counter = 0;
681         for (; it != end; ++it, ++metrics_counter) {
682                 string const & snip = it->first;
683                 FileName const & file = it->second;
684                 double af = ascent_fractions[metrics_counter];
685
686                 // Add the image to the cache only if it's actually present
687                 if (file.isReadableFile()) {
688                         PreviewImagePtr ptr(new PreviewImage(parent_, snip, file, af));
689                         cache_[snip] = ptr;
690
691                         newimages.push_back(ptr);
692                 }
693
694         }
695
696         // Remove the item from the list of still-executing processes.
697         in_progress_.erase(git);
698
699         // Tell the outside world
700         list<PreviewImagePtr>::const_reverse_iterator
701                 nit  = newimages.rbegin();
702         list<PreviewImagePtr>::const_reverse_iterator
703                 nend = newimages.rend();
704         for (; nit != nend; ++nit) {
705                 imageReady(*nit->get());
706         }
707 }
708
709
710 void PreviewLoader::Impl::dumpPreamble(otexstream & os) const
711 {
712         // Dump the preamble only.
713         OutputParams runparams(&buffer_.params().encoding());
714         if (buffer_.params().useNonTeXFonts)
715                 runparams.flavor = OutputParams::XETEX;
716         else
717                 runparams.flavor = OutputParams::LATEX;
718         runparams.nice = true;
719         runparams.moving_arg = true;
720         runparams.free_spacing = true;
721         buffer_.writeLaTeXSource(os, buffer_.filePath(), runparams, Buffer::OnlyPreamble);
722
723         // FIXME! This is a HACK! The proper fix is to control the 'true'
724         // passed to WriteStream below:
725         // int InsetMathNest::latex(Buffer const &, odocstream & os,
726         //                          OutputParams const & runparams) const
727         // {
728         //      WriteStream wi(os, runparams.moving_arg, true);
729         //      par_->write(wi);
730         //      return wi.line();
731         // }
732         os << "\n"
733            << "\\def\\lyxlock{}\n"
734            << "\n";
735
736         // All equation labels appear as "(#)" + preview.sty's rendering of
737         // the label name
738         if (lyxrc.preview_hashed_labels)
739                 os << "\\renewcommand{\\theequation}{\\#}\n";
740
741         // Use the preview style file to ensure that each snippet appears on a
742         // fresh page.
743         // Also support PDF output (automatically generated e.g. when
744         // \usepackage[pdftex]{hyperref} is used and XeTeX.
745         os << "\n"
746            << "\\usepackage[active,delayed,showlabels,lyx]{preview}\n"
747            << "\n";
748 }
749
750
751 void PreviewLoader::Impl::dumpData(odocstream & os,
752                                    BitmapFile const & vec) const
753 {
754         if (vec.empty())
755                 return;
756
757         BitmapFile::const_iterator it  = vec.begin();
758         BitmapFile::const_iterator end = vec.end();
759
760         for (; it != end; ++it) {
761                 // FIXME UNICODE
762                 os << "\\begin{preview}\n"
763                    << from_utf8(it->first)
764                    << "\n\\end{preview}\n\n";
765         }
766 }
767
768 } // namespace graphics
769 } // namespace lyx