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