]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewLoader.cpp
e1435ea72027f6fefde0b9e35de6aa16da2b56d9
[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(odocstream &) 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                 if (b.params().encoding().package() == Encoding::japanese)
393                         pconverter_ = setConverter("lyxpreview-platex");
394                 else
395                         pconverter_ = setConverter("lyxpreview");
396         }
397 }
398
399
400 PreviewLoader::Impl::~Impl()
401 {
402         InProgressProcesses::iterator ipit  = in_progress_.begin();
403         InProgressProcesses::iterator ipend = in_progress_.end();
404
405         for (; ipit != ipend; ++ipit)
406                 ipit->second.stop();
407 }
408
409
410 PreviewImage const *
411 PreviewLoader::Impl::preview(string const & latex_snippet) const
412 {
413         Cache::const_iterator it = cache_.find(latex_snippet);
414         return (it == cache_.end()) ? 0 : it->second.get();
415 }
416
417
418 namespace {
419
420 class FindSnippet {
421 public:
422         FindSnippet(string const & s) : snippet_(s) {}
423         bool operator()(InProgressProcess const & process) const
424         {
425                 BitmapFile const & snippets = process.second.snippets;
426                 BitmapFile::const_iterator beg  = snippets.begin();
427                 BitmapFile::const_iterator end = snippets.end();
428                 return find_if(beg, end, FindFirst(snippet_)) != end;
429         }
430
431 private:
432         string const snippet_;
433 };
434
435 } // namespace anon
436
437 PreviewLoader::Status
438 PreviewLoader::Impl::status(string const & latex_snippet) const
439 {
440         Cache::const_iterator cit = cache_.find(latex_snippet);
441         if (cit != cache_.end())
442                 return Ready;
443
444         PendingSnippets::const_iterator pit  = pending_.begin();
445         PendingSnippets::const_iterator pend = pending_.end();
446
447         pit = find(pit, pend, latex_snippet);
448         if (pit != pend)
449                 return InQueue;
450
451         InProgressProcesses::const_iterator ipit  = in_progress_.begin();
452         InProgressProcesses::const_iterator ipend = in_progress_.end();
453
454         ipit = find_if(ipit, ipend, FindSnippet(latex_snippet));
455         if (ipit != ipend)
456                 return Processing;
457
458         return NotFound;
459 }
460
461
462 void PreviewLoader::Impl::add(string const & latex_snippet)
463 {
464         if (!pconverter_ || status(latex_snippet) != NotFound)
465                 return;
466
467         string const snippet = trim(latex_snippet);
468         if (snippet.empty())
469                 return;
470
471         LYXERR(Debug::GRAPHICS, "adding snippet:\n" << snippet);
472
473         pending_.push_back(snippet);
474 }
475
476
477 namespace {
478
479 class EraseSnippet {
480 public:
481         EraseSnippet(string const & s) : snippet_(s) {}
482         void operator()(InProgressProcess & process)
483         {
484                 BitmapFile & snippets = process.second.snippets;
485                 BitmapFile::iterator it  = snippets.begin();
486                 BitmapFile::iterator end = snippets.end();
487
488                 it = find_if(it, end, FindFirst(snippet_));
489                 if (it != end)
490                         snippets.erase(it, it+1);
491         }
492
493 private:
494         string const & snippet_;
495 };
496
497 } // namespace anon
498
499
500 void PreviewLoader::Impl::remove(string const & latex_snippet)
501 {
502         Cache::iterator cit = cache_.find(latex_snippet);
503         if (cit != cache_.end())
504                 cache_.erase(cit);
505
506         PendingSnippets::iterator pit  = pending_.begin();
507         PendingSnippets::iterator pend = pending_.end();
508
509         pending_.erase(std::remove(pit, pend, latex_snippet), pend);
510
511         InProgressProcesses::iterator ipit  = in_progress_.begin();
512         InProgressProcesses::iterator ipend = in_progress_.end();
513
514         for_each(ipit, ipend, EraseSnippet(latex_snippet));
515
516         while (ipit != ipend) {
517                 InProgressProcesses::iterator curr = ipit++;
518                 if (curr->second.snippets.empty())
519                         in_progress_.erase(curr);
520         }
521 }
522
523
524 void PreviewLoader::Impl::startLoading(bool wait)
525 {
526         if (pending_.empty() || !pconverter_)
527                 return;
528
529         // Only start the process off after the buffer is loaded from file.
530         if (!buffer_.isFullyLoaded())
531                 return;
532
533         LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()");
534
535         // As used by the LaTeX file and by the resulting image files
536         string const directory = buffer_.temppath();
537
538         string const filename_base = unique_filename(directory);
539
540         // Create an InProgress instance to place in the map of all
541         // such processes if it starts correctly.
542         InProgress inprogress(filename_base, pending_, pconverter_->to);
543
544         // clear pending_, so we're ready to start afresh.
545         pending_.clear();
546
547         // Output the LaTeX file.
548         FileName const latexfile(filename_base + ".tex");
549
550         // we use the encoding of the buffer
551         Encoding const & enc = buffer_.params().encoding();
552         ofdocstream of;
553         try { of.reset(enc.iconvName()); }
554         catch (iconv_codecvt_facet_exception & e) {
555                 LYXERR0("Caught iconv exception: " << e.what()
556                         << "\nUnable to create LaTeX file: " << latexfile);
557                 return;
558         }
559
560         TexRow texrow;
561         OutputParams runparams(&enc);
562         LaTeXFeatures features(buffer_, buffer_.params(), runparams);
563
564         if (!openFileWrite(of, latexfile))
565                 return;
566
567         if (!of) {
568                 LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
569                                         << "Unable to create LaTeX file\n" << latexfile);
570                 return;
571         }
572         of << "\\batchmode\n";
573         dumpPreamble(of);
574         // handle inputenc etc.
575         buffer_.params().writeEncodingPreamble(of, features, texrow);
576         of << "\n\\begin{document}\n";
577         dumpData(of, inprogress.snippets);
578         of << "\n\\end{document}\n";
579         of.close();
580         if (of.fail()) {
581                 LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
582                                          << "File was not closed properly.");
583                 return;
584         }
585
586         double const font_scaling_factor = 
587                 buffer_.isExporting() ? 75.0 * buffer_.params().html_math_img_scale 
588                         : 0.01 * lyxrc.dpi * lyxrc.zoom * lyxrc.preview_scale_factor;
589
590         // FIXME XHTML 
591         // The colors should be customizable.
592         ColorCode const bg = buffer_.isExporting() 
593                        ? Color_white : PreviewLoader::backgroundColor();
594         ColorCode const fg = buffer_.isExporting() 
595                        ? Color_black : PreviewLoader::foregroundColor();
596         // The conversion command.
597         ostringstream cs;
598         cs << pconverter_->command << ' ' << pconverter_->to << ' '
599            << quoteName(latexfile.toFilesystemEncoding()) << ' '
600            << int(font_scaling_factor) << ' '
601            << theApp()->hexName(fg) << ' '
602            << theApp()->hexName(bg);
603         if (buffer_.params().useXetex)
604                 cs << " xelatex";
605
606         string const command = libScriptSearch(cs.str());
607
608         if (wait) {
609                 ForkedCall call;
610                 int ret = call.startScript(ForkedProcess::Wait, command);
611                 static int fake = (2^20) + 1;
612                 int pid = fake++;
613                 inprogress.pid = pid;
614                 inprogress.command = command;
615                 in_progress_[pid] = inprogress;
616                 finishedGenerating(pid, ret);
617                 return;
618         }
619
620         // Initiate the conversion from LaTeX to bitmap images files.
621         ForkedCall::SignalTypePtr
622                 convert_ptr(new ForkedCall::SignalType);
623         convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));
624
625         ForkedCall call;
626         int ret = call.startScript(command, convert_ptr);
627
628         if (ret != 0) {
629                 LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
630                                         << "Unable to start process\n" << command);
631                 return;
632         }
633
634         // Store the generation process in a list of all such processes
635         inprogress.pid = call.pid();
636         inprogress.command = command;
637         in_progress_[inprogress.pid] = inprogress;
638 }
639
640
641 void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
642 {
643         // Paranoia check!
644         InProgressProcesses::iterator git = in_progress_.find(pid);
645         if (git == in_progress_.end()) {
646                 lyxerr << "PreviewLoader::finishedGenerating(): unable to find "
647                         "data for PID " << pid << endl;
648                 return;
649         }
650
651         string const command = git->second.command;
652         string const status = retval > 0 ? "failed" : "succeeded";
653         LYXERR(Debug::GRAPHICS, "PreviewLoader::finishedInProgress("
654                                 << retval << "): processing " << status
655                                 << " for " << command);
656         if (retval > 0)
657                 return;
658
659         // Read the metrics file, if it exists
660         vector<double> ascent_fractions(git->second.snippets.size());
661         setAscentFractions(ascent_fractions, git->second.metrics_file);
662
663         // Add these newly generated bitmap files to the cache and
664         // start loading them into LyX.
665         BitmapFile::const_iterator it  = git->second.snippets.begin();
666         BitmapFile::const_iterator end = git->second.snippets.end();
667
668         list<PreviewImagePtr> newimages;
669
670         int metrics_counter = 0;
671         for (; it != end; ++it, ++metrics_counter) {
672                 string const & snip = it->first;
673                 FileName const & file = it->second;
674                 double af = ascent_fractions[metrics_counter];
675
676                 PreviewImagePtr ptr(new PreviewImage(parent_, snip, file, af));
677                 cache_[snip] = ptr;
678
679                 newimages.push_back(ptr);
680         }
681
682         // Remove the item from the list of still-executing processes.
683         in_progress_.erase(git);
684
685         // Tell the outside world
686         list<PreviewImagePtr>::const_reverse_iterator
687                 nit  = newimages.rbegin();
688         list<PreviewImagePtr>::const_reverse_iterator
689                 nend = newimages.rend();
690         for (; nit != nend; ++nit) {
691                 imageReady(*nit->get());
692         }
693 }
694
695
696 void PreviewLoader::Impl::dumpPreamble(odocstream & os) const
697 {
698         // Dump the preamble only.
699         OutputParams runparams(&buffer_.params().encoding());
700         runparams.flavor = OutputParams::LATEX;
701         runparams.nice = true;
702         runparams.moving_arg = true;
703         runparams.free_spacing = true;
704         buffer_.writeLaTeXSource(os, buffer_.filePath(), runparams, true, false);
705
706         // FIXME! This is a HACK! The proper fix is to control the 'true'
707         // passed to WriteStream below:
708         // int InsetMathNest::latex(Buffer const &, odocstream & os,
709         //                          OutputParams const & runparams) const
710         // {
711         //      WriteStream wi(os, runparams.moving_arg, true);
712         //      par_->write(wi);
713         //      return wi.line();
714         // }
715         os << "\n"
716            << "\\def\\lyxlock{}\n"
717            << "\n";
718
719         // All equation labels appear as "(#)" + preview.sty's rendering of
720         // the label name
721         if (lyxrc.preview_hashed_labels)
722                 os << "\\renewcommand{\\theequation}{\\#}\n";
723
724         // Use the preview style file to ensure that each snippet appears on a
725         // fresh page.
726         // Also support PDF output (automatically generated e.g. when
727         // \usepackage[pdftex]{hyperref} is used and XeTeX.
728         os << "\n"
729            << "\\newif\\ifxetex\n"
730            << "\\expandafter\\ifx\\csname XeTeXrevision\\endcsname\\relax\n"
731            << "   \\xetexfalse\n"
732            << "\\else\n"
733            << "    \\xetextrue\n"
734            << "\\fi\n"
735            << "\\newif\\ifpdf\n"
736            << "\\ifx\\pdfoutput\\undefined\n"
737            << "\\else\\ifx\\pdfoutput\\relax\n"
738            << "\\else\\ifnum0=\\pdfoutput\n"
739            << "\\else\\pdftrue\\fi\\fi\\fi\n"
740            << "\\ifxetex\n"
741            << "  \\usepackage[active,delayed,tightpage,showlabels,lyx,xetex]{preview}\n"
742            << "\\else\n"
743            << "\\ifpdf\n"
744            << "  \\usepackage[active,delayed,tightpage,showlabels,lyx,pdftex]{preview}\n"
745            << "\\else\n"
746            << "  \\usepackage[active,delayed,showlabels,lyx,dvips]{preview}\n"
747            << "\\fi\n"
748            << "\\fi\n"
749            << "\n";
750 }
751
752
753 void PreviewLoader::Impl::dumpData(odocstream & os,
754                                    BitmapFile const & vec) const
755 {
756         if (vec.empty())
757                 return;
758
759         BitmapFile::const_iterator it  = vec.begin();
760         BitmapFile::const_iterator end = vec.end();
761
762         for (; it != end; ++it) {
763                 // FIXME UNICODE
764                 os << "\\begin{preview}\n"
765                    << from_utf8(it->first)
766                    << "\n\\end{preview}\n\n";
767         }
768 }
769
770 } // namespace graphics
771 } // namespace lyx