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