]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/PreviewLoader.C
If the graphics loader has a copy c-tor it should have copy assignment that does...
[lyx.git] / src / graphics / PreviewLoader.C
index 57d3749e05fe62ecedab9c7819e2f3cfb122fa05..cf4ae4faaf2f9bfce80d02a749a9af4b61cd4b72 100644 (file)
@@ -1,25 +1,25 @@
-/*
+/**
  *  \file PreviewLoader.C
- *  Copyright 2002 the LyX Team
- *  Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming <leeming@lyx.org>
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "PreviewLoader.h"
 #include "PreviewImage.h"
 
 #include "buffer.h"
 #include "converter.h"
+#include "format.h"
 #include "debug.h"
 #include "lyxrc.h"
 #include "LColor.h"
+#include "Lsstream.h"
 
 #include "insets/inset.h"
 
@@ -28,6 +28,7 @@
 #include "support/filetools.h"
 #include "support/forkedcall.h"
 #include "support/forkedcontr.h"
+#include "support/tostr.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 
@@ -79,23 +80,13 @@ struct FindFirst {
        FindFirst(string const & comp) : comp_(comp) {}
        bool operator()(StrPair const & sp)
        {
-               return sp.first < comp_;
+               return sp.first == comp_;
        }
 private:
        string const comp_;
 };
 
 
-// Given a base-10 number return the number of digits needed to store it.
-// Eg 2 requires 1 digit, 22 requires 2 digits and 999 requires 3 digits.
-// Note that AndrĂ© suggests just returning '12' here...
-    int ndigits(int /*num*/)
-{
-       //return 1 + int(std::log10(double(num)));
-       return 5;
-}
-
-
 /// Store info on a currently executing, forked process.
 struct InProgress {
        ///
@@ -110,12 +101,14 @@ struct InProgress {
        ///
        pid_t pid;
        ///
+       string command;
+       ///
        string metrics_file;
        ///
        BitmapFile snippets;
 };
 
-typedef map<string, InProgress>  InProgressProcesses;
+typedef map<pid_t, InProgress>  InProgressProcesses;
 
 typedef InProgressProcesses::value_type InProgressProcess;
 
@@ -147,7 +140,7 @@ struct PreviewLoader::Impl : public boost::signals::trackable {
 
 private:
        /// Called by the Forkedcall process that generated the bitmap files.
-       void finishedGenerating(string const &, pid_t, int);
+       void finishedGenerating(pid_t, int);
        ///
        void dumpPreamble(ostream &) const;
        ///
@@ -256,19 +249,15 @@ namespace {
 
 struct IncrementedFileName {
        IncrementedFileName(string const & to_format,
-                           string const & filename_base, int nd)
-               : to_format_(to_format), base_(filename_base),
-                 ndigits_(nd), counter_(1)
+                           string const & filename_base)
+               : to_format_(to_format), base_(filename_base), counter_(1)
        {}
 
        StrPair const operator()(string const & snippet)
        {
                ostringstream os;
-               os << base_
-                  << setfill('0') << setw(ndigits_) << counter_++
-                  << "." << to_format_;
-
-               string const file = os.str().c_str();
+               os << base_ << counter_++ << '.' << to_format_;
+               string const file = STRCONV(os.str());
 
                return make_pair(snippet, file);
        }
@@ -276,7 +265,6 @@ struct IncrementedFileName {
 private:
        string const & to_format_;
        string const & base_;
-       int const ndigits_;
        int counter_;
 };
 
@@ -293,8 +281,7 @@ InProgress::InProgress(string const & filename_base,
        BitmapFile::iterator sit = snippets.begin();
 
        std::transform(pit, pend, sit,
-                      IncrementedFileName(to_format, filename_base,
-                                          ndigits(int(snippets.size()))));
+                      IncrementedFileName(to_format, filename_base));
 }
 
 
@@ -465,12 +452,19 @@ void PreviewLoader::Impl::startLoading()
        lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()" << endl;
 
        // As used by the LaTeX file and by the resulting image files
-       string const filename_base(unique_filename(buffer_.tmppath));
+       string directory = buffer_.tmppath;
+       if (directory.empty())
+               directory = buffer_.filePath();
+       
+       string const filename_base(unique_filename(directory));
 
        // Create an InProgress instance to place in the map of all
        // such processes if it starts correctly.
        InProgress inprogress(filename_base, pending_, pconverter_->to);
 
+       // clear pending_, so we're ready to start afresh.
+       pending_.clear();
+
        // Output the LaTeX file.
        string const latexfile = filename_base + ".tex";
 
@@ -484,21 +478,15 @@ void PreviewLoader::Impl::startLoading()
 
        // The conversion command.
        ostringstream cs;
-       cs << pconverter_->command << " " << latexfile << " "
-          << int(font_scaling_factor_) << " "
-          << ndigits(int(pending_.size()));
+       cs << pconverter_->command << ' ' << latexfile << ' '
+          << int(font_scaling_factor_) << ' ' << pconverter_->to;
 
-       string const command = LibScriptSearch(cs.str().c_str());
-
-       // clear pending_, so we're ready to start afresh.
-       pending_.clear();
+       string const command = "sh " + LibScriptSearch(STRCONV(cs.str()));
 
        // Initiate the conversion from LaTeX to bitmap images files.
-       Forkedcall::SignalTypePtr convert_ptr;
-       convert_ptr.reset(new Forkedcall::SignalType);
-
+       Forkedcall::SignalTypePtr convert_ptr(new Forkedcall::SignalType);
        convert_ptr->connect(
-               boost::bind(&Impl::finishedGenerating, this, _1, _2, _3));
+               boost::bind(&Impl::finishedGenerating, this, _1, _2));
 
        Forkedcall call;
        int ret = call.startscript(command, convert_ptr);
@@ -512,13 +500,22 @@ void PreviewLoader::Impl::startLoading()
 
        // Store the generation process in a list of all such processes
        inprogress.pid = call.pid();
-       in_progress_[command] = inprogress;
+       inprogress.command = command;
+       in_progress_[inprogress.pid] = inprogress;
 }
 
 
-void PreviewLoader::Impl::finishedGenerating(string const & command,
-                                            pid_t /* pid */, int retval)
+void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
 {
+       // Paranoia check!
+       InProgressProcesses::iterator git = in_progress_.find(pid);
+       if (git == in_progress_.end()) {
+               lyxerr << "PreviewLoader::finishedGenerating(): unable to find "
+                       "data for PID " << pid << endl;
+               return;
+       }
+
+       string const command = git->second.command;
        string const status = retval > 0 ? "failed" : "succeeded";
        lyxerr[Debug::GRAPHICS] << "PreviewLoader::finishedInProgress("
                                << retval << "): processing " << status
@@ -526,15 +523,6 @@ void PreviewLoader::Impl::finishedGenerating(string const & command,
        if (retval > 0)
                return;
 
-       // Paranoia check!
-       InProgressProcesses::iterator git = in_progress_.find(command);
-       if (git == in_progress_.end()) {
-               lyxerr << "PreviewLoader::finishedGenerating(): unable to find "
-                       "data for\n"
-                      << command << "!" << endl;
-               return;
-       }
-
        // Read the metrics file, if it exists
        vector<double> ascent_fractions(git->second.snippets.size());
        setAscentFractions(ascent_fractions, git->second.metrics_file);
@@ -562,8 +550,10 @@ void PreviewLoader::Impl::finishedGenerating(string const & command,
        in_progress_.erase(git);
 
        // Tell the outside world
-       std::list<PreviewImagePtr>::const_iterator nit  = newimages.begin();
-       std::list<PreviewImagePtr>::const_iterator nend = newimages.end();
+       std::list<PreviewImagePtr>::const_reverse_iterator
+               nit  = newimages.rbegin();
+       std::list<PreviewImagePtr>::const_reverse_iterator
+               nend = newimages.rend();
        for (; nit != nend; ++nit) {
                imageReady(*nit->get());
        }
@@ -575,7 +565,25 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const
        // Why on earth is Buffer::makeLaTeXFile a non-const method?
        Buffer & tmp = const_cast<Buffer &>(buffer_);
        // Dump the preamble only.
-       tmp.makeLaTeXFile(os, buffer_.filePath(), true, false, true);
+       LatexRunParams runparams;
+       runparams.flavor = LatexRunParams::LATEX;
+       runparams.nice = true;
+       runparams.moving_arg = true;
+       runparams.free_spacing = true;
+       tmp.makeLaTeXFile(os, buffer_.filePath(), runparams, false, true);
+
+       // FIXME! This is a HACK! The proper fix is to control the 'true'
+       // passed to WriteStream below:
+       // int InsetFormula::latex(Buffer const *, ostream & os,
+       //                         LatexRunParams const & runparams) const
+       // {
+       //      WriteStream wi(os, runparams.moving_arg, true);
+       //      par_->write(wi);
+       //      return wi.line();
+       // }
+       os << "\n"
+          << "\\def\\lyxlock{}\n"
+          << "\n";
 
        // Loop over the insets in the buffer and dump all the math-macros.
        Buffer::inset_iterator it  = buffer_.inset_const_iterator_begin();
@@ -583,7 +591,7 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const
 
        for (; it != end; ++it)
                if (it->lyxCode() == Inset::MATHMACRO_CODE)
-                       it->latex(&buffer_, os, true, true);
+                       it->latex(&buffer_, os, runparams);
 
        // All equation lables appear as "(#)" + preview.sty's rendering of
        // the label name
@@ -606,7 +614,7 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const
 
        os << "\\AtBeginDocument{\\AtBeginDvi{%\n"
           << "\\special{!userdict begin/bop-hook{//bop-hook exec\n"
-          << "<" << fg << bg << ">{255 div}forall setrgbcolor\n"
+          << '<' << fg << bg << ">{255 div}forall setrgbcolor\n"
           << "clippath fill setrgbcolor}bind def end}}}\n";
 }