]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/PreviewLoader.C
* GuiView.C (updateTab): do not update early if current tab has
[lyx.git] / src / graphics / PreviewLoader.C
index 9b8b084ae255e9015e59ee406326eb25cf06fb6b..978355a8d80ef4a081d961d8434e279af8541bc8 100644 (file)
@@ -24,7 +24,7 @@
 #include "outputparams.h"
 #include "paragraph.h"
 
-#include "frontends/lyx_gui.h" // hexname
+#include "frontends/Application.h" // hexName
 
 #include "insets/inset.h"
 
@@ -33,7 +33,7 @@
 #include "support/forkedcontr.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
-#include "support/tostr.h"
+#include "support/convert.h"
 
 #include <boost/bind.hpp>
 
@@ -41,7 +41,7 @@
 #include <fstream>
 #include <iomanip>
 
-namespace support = lyx::support;
+using lyx::support::FileName;
 
 using std::endl;
 using std::find;
@@ -54,8 +54,6 @@ using boost::bind;
 using std::ifstream;
 using std::list;
 using std::map;
-using std::ofstream;
-using std::ostream;
 using std::ostringstream;
 using std::pair;
 using std::vector;
@@ -64,25 +62,112 @@ using std::string;
 
 namespace {
 
-typedef pair<string, string> StrPair;
+typedef pair<string, FileName> SnippetPair;
 
 // A list of alll snippets to be converted to previews
 typedef list<string> PendingSnippets;
 
 // Each item in the vector is a pair<snippet, image file name>.
-typedef vector<StrPair> BitmapFile;
+typedef vector<SnippetPair> BitmapFile;
 
-string const unique_filename(string const bufferpath);
 
-Converter const * setConverter();
+string const unique_filename(string const & bufferpath)
+{
+       static int theCounter = 0;
+       string const filename = lyx::convert<string>(theCounter++) + "lyxpreview";
+       return lyx::support::addName(bufferpath, filename);
+}
+
+
+lyx::Converter const * setConverter()
+{
+       string const from = "lyxpreview";
+
+       typedef vector<string> FmtList;
+       typedef lyx::graphics::Cache GCache;
+       FmtList const loadableFormats = GCache::get().loadableFormats();
+       FmtList::const_iterator it = loadableFormats.begin();
+       FmtList::const_iterator const end = loadableFormats.end();
+
+       for (; it != end; ++it) {
+               string const to = *it;
+               if (from == to)
+                       continue;
+
+               lyx::Converter const * ptr = lyx::theConverters().getConverter(from, to);
+               if (ptr)
+                       return ptr;
+       }
+
+       static bool first = true;
+       if (first) {
+               first = false;
+               lyx::lyxerr << "PreviewLoader::startLoading()\n"
+                      << "No converter from \"lyxpreview\" format has been "
+                       "defined."
+                      << endl;
+       }
+       return 0;
+}
+
 
 void setAscentFractions(vector<double> & ascent_fractions,
-                       string const & metrics_file);
+                       FileName const & metrics_file)
+{
+       // If all else fails, then the images will have equal ascents and
+       // descents.
+       vector<double>::iterator it  = ascent_fractions.begin();
+       vector<double>::iterator end = ascent_fractions.end();
+       fill(it, end, 0.5);
+
+       ifstream in(metrics_file.toFilesystemEncoding().c_str());
+       if (!in.good()) {
+               lyx::lyxerr[lyx::Debug::GRAPHICS]
+                       << "setAscentFractions(" << metrics_file << ")\n"
+                       << "Unable to open file!" << endl;
+               return;
+       }
+
+       bool error = false;
+
+       int snippet_counter = 1;
+       while (!in.eof() && it != end) {
+               string snippet;
+               int id;
+               double ascent_fraction;
+
+               in >> snippet >> id >> ascent_fraction;
 
-class FindFirst : public std::unary_function<StrPair, bool> {
+               if (!in.good())
+                       // eof after all
+                       break;
+
+               error = snippet != "Snippet";
+               if (error)
+                       break;
+
+               error = id != snippet_counter;
+               if (error)
+                       break;
+
+               *it = ascent_fraction;
+
+               ++snippet_counter;
+               ++it;
+       }
+
+       if (error) {
+               lyx::lyxerr[lyx::Debug::GRAPHICS]
+                       << "setAscentFractions(" << metrics_file << ")\n"
+                       << "Error reading file!\n" << endl;
+       }
+}
+
+
+class FindFirst : public std::unary_function<SnippetPair, bool> {
 public:
        FindFirst(string const & comp) : comp_(comp) {}
-       bool operator()(StrPair const & sp) const
+       bool operator()(SnippetPair const & sp) const
        {
                return sp.first == comp_;
        }
@@ -92,7 +177,8 @@ private:
 
 
 /// Store info on a currently executing, forked process.
-struct InProgress {
+class InProgress {
+public:
        ///
        InProgress() : pid(0) {}
        ///
@@ -107,7 +193,7 @@ struct InProgress {
        ///
        string command;
        ///
-       string metrics_file;
+       FileName metrics_file;
        ///
        BitmapFile snippets;
 };
@@ -119,10 +205,13 @@ typedef InProgressProcesses::value_type InProgressProcess;
 } // namespace anon
 
 
+
 namespace lyx {
+
 namespace graphics {
 
-struct PreviewLoader::Impl : public boost::signals::trackable {
+class PreviewLoader::Impl : public boost::signals::trackable {
+public:
        ///
        Impl(PreviewLoader & p, Buffer const & b);
        /// Stop any InProgress items still executing.
@@ -147,9 +236,9 @@ private:
        /// Called by the Forkedcall process that generated the bitmap files.
        void finishedGenerating(pid_t, int);
        ///
-       void dumpPreamble(ostream &) const;
+       void dumpPreamble(odocstream &) const;
        ///
-       void dumpData(ostream &, BitmapFile const &) const;
+       void dumpData(odocstream &, BitmapFile const &) const;
 
        /** cache_ allows easy retrieval of already-generated images
         *  using the LaTeX snippet as the identifier.
@@ -186,8 +275,10 @@ private:
 Converter const * PreviewLoader::Impl::pconverter_;
 
 
+//
 // The public interface, defined in PreviewLoader.h
-// ================================================
+//
+
 PreviewLoader::PreviewLoader(Buffer const & b)
        : pimpl_(new Impl(*this, b))
 {}
@@ -253,19 +344,20 @@ Buffer const & PreviewLoader::buffer() const
 
 namespace {
 
-struct IncrementedFileName {
+class IncrementedFileName {
+public:
        IncrementedFileName(string const & to_format,
                            string const & filename_base)
                : to_format_(to_format), base_(filename_base), counter_(1)
        {}
 
-       StrPair const operator()(string const & snippet)
+       SnippetPair const operator()(string const & snippet)
        {
                ostringstream os;
                os << base_ << counter_++ << '.' << to_format_;
                string const file = os.str();
 
-               return make_pair(snippet, file);
+               return make_pair(snippet, FileName(file));
        }
 
 private:
@@ -279,7 +371,7 @@ InProgress::InProgress(string const & filename_base,
                       PendingSnippets const & pending,
                       string const & to_format)
        : pid(0),
-         metrics_file(filename_base + ".metrics"),
+         metrics_file(FileName(filename_base + ".metrics")),
          snippets(pending.size())
 {
        PendingSnippets::const_iterator pit  = pending.begin();
@@ -294,16 +386,16 @@ InProgress::InProgress(string const & filename_base,
 void InProgress::stop() const
 {
        if (pid)
-               support::ForkedcallsController::get().kill(pid, 0);
+               lyx::support::ForkedcallsController::get().kill(pid, 0);
 
        if (!metrics_file.empty())
-               support::unlink(metrics_file);
+               lyx::support::unlink(metrics_file);
 
        BitmapFile::const_iterator vit  = snippets.begin();
        BitmapFile::const_iterator vend = snippets.end();
        for (; vit != vend; ++vit) {
                if (!vit->second.empty())
-                       support::unlink(vit->second);
+                       lyx::support::unlink(vit->second);
        }
 }
 
@@ -317,7 +409,7 @@ PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
        : parent_(p), buffer_(b), font_scaling_factor_(0.0)
 {
        font_scaling_factor_ = 0.01 * lyxrc.dpi * lyxrc.zoom *
-               lyxrc.preview_scale_factor;
+               convert<double>(lyxrc.preview_scale_factor);
 
        lyxerr[Debug::GRAPHICS] << "The font scaling factor is "
                                << font_scaling_factor_ << endl;
@@ -407,7 +499,8 @@ void PreviewLoader::Impl::add(string const & latex_snippet)
 
 namespace {
 
-struct EraseSnippet {
+class EraseSnippet {
+public:
        EraseSnippet(string const & s) : snippet_(s) {}
        void operator()(InProgressProcess & process)
        {
@@ -465,7 +558,7 @@ void PreviewLoader::Impl::startLoading()
        // As used by the LaTeX file and by the resulting image files
        string const directory = buffer_.temppath();
 
-       string const filename_base(unique_filename(directory));
+       string const filename_base = unique_filename(directory);
 
        // Create an InProgress instance to place in the map of all
        // such processes if it starts correctly.
@@ -475,9 +568,12 @@ void PreviewLoader::Impl::startLoading()
        pending_.clear();
 
        // Output the LaTeX file.
-       string const latexfile = filename_base + ".tex";
+       FileName const latexfile(filename_base + ".tex");
 
-       ofstream of(latexfile.c_str());
+       // FIXME UNICODE
+       // This creates an utf8 encoded file, but the proper inputenc
+       // command is missing.
+       odocfstream of(latexfile.toFilesystemEncoding().c_str());
        if (!of) {
                lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()\n"
                                        << "Unable to create LaTeX file\n"
@@ -494,11 +590,12 @@ void PreviewLoader::Impl::startLoading()
        // The conversion command.
        ostringstream cs;
        cs << pconverter_->command << ' ' << pconverter_->to << ' '
-          << latexfile << ' ' << int(font_scaling_factor_) << ' '
-          << lyx_gui::hexname(LColor::preview) << ' '
-          << lyx_gui::hexname(LColor::background);
+          << support::quoteName(latexfile.toFilesystemEncoding()) << ' '
+          << int(font_scaling_factor_) << ' '
+          << theApp()->hexName(LColor::preview) << ' '
+          << theApp()->hexName(LColor::background);
 
-       string const command = support::LibScriptSearch(cs.str());
+       string const command = support::libScriptSearch(cs.str());
 
        // Initiate the conversion from LaTeX to bitmap images files.
        support::Forkedcall::SignalTypePtr
@@ -554,7 +651,7 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
        int metrics_counter = 0;
        for (; it != end; ++it, ++metrics_counter) {
                string const & snip = it->first;
-               string const & file = it->second;
+               FileName const & file = it->second;
                double af = ascent_fractions[metrics_counter];
 
                PreviewImagePtr ptr(new PreviewImage(parent_, snip, file, af));
@@ -577,7 +674,7 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
 }
 
 
-void PreviewLoader::Impl::dumpPreamble(ostream & os) const
+void PreviewLoader::Impl::dumpPreamble(odocstream & os) const
 {
        // Why on earth is Buffer::makeLaTeXFile a non-const method?
        Buffer & tmp = const_cast<Buffer &>(buffer_);
@@ -587,11 +684,11 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const
        runparams.nice = true;
        runparams.moving_arg = true;
        runparams.free_spacing = true;
-       tmp.makeLaTeXFile(os, buffer_.filePath(), runparams, true, false);
+       tmp.writeLaTeXSource(os, buffer_.filePath(), runparams, true, false);
 
        // FIXME! This is a HACK! The proper fix is to control the 'true'
        // passed to WriteStream below:
-       // int InsetFormula::latex(Buffer const &, ostream & os,
+       // int InsetFormula::latex(Buffer const &, odocstream & os,
        //                         OutputParams const & runparams) const
        // {
        //      WriteStream wi(os, runparams.moving_arg, true);
@@ -624,7 +721,7 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const
 }
 
 
-void PreviewLoader::Impl::dumpData(ostream & os,
+void PreviewLoader::Impl::dumpData(odocstream & os,
                                   BitmapFile const & vec) const
 {
        if (vec.empty())
@@ -634,8 +731,9 @@ void PreviewLoader::Impl::dumpData(ostream & os,
        BitmapFile::const_iterator end = vec.end();
 
        for (; it != end; ++it) {
+               // FIXME UNICODE
                os << "\\begin{preview}\n"
-                  << it->first
+                  << from_utf8(it->first)
                   << "\n\\end{preview}\n\n";
        }
 }
@@ -643,99 +741,3 @@ void PreviewLoader::Impl::dumpData(ostream & os,
 } // namespace graphics
 } // namespace lyx
 
-namespace {
-
-string const unique_filename(string const bufferpath)
-{
-       static int theCounter = 0;
-       string const filename = tostr(theCounter++) + "lyxpreview";
-       return support::AddName(bufferpath, filename);
-}
-
-
-Converter const * setConverter()
-{
-       string const from = "lyxpreview";
-
-       typedef vector<string> FmtList;
-       typedef lyx::graphics::Cache GCache;
-       FmtList const loadableFormats = GCache::get().loadableFormats();
-       FmtList::const_iterator it  = loadableFormats.begin();
-       FmtList::const_iterator const end = loadableFormats.end();
-
-       for (; it != end; ++it) {
-               string const to = *it;
-               if (from == to)
-                       continue;
-
-               Converter const * ptr = converters.getConverter(from, to);
-               if (ptr)
-                       return ptr;
-       }
-
-       static bool first = true;
-       if (first) {
-               first = false;
-               lyxerr << "PreviewLoader::startLoading()\n"
-                      << "No converter from \"lyxpreview\" format has been "
-                       "defined."
-                      << endl;
-       }
-
-       return 0;
-}
-
-
-void setAscentFractions(vector<double> & ascent_fractions,
-                       string const & metrics_file)
-{
-       // If all else fails, then the images will have equal ascents and
-       // descents.
-       vector<double>::iterator it  = ascent_fractions.begin();
-       vector<double>::iterator end = ascent_fractions.end();
-       fill(it, end, 0.5);
-
-       ifstream in(metrics_file.c_str());
-       if (!in.good()) {
-               lyxerr[Debug::GRAPHICS]
-                       << "setAscentFractions(" << metrics_file << ")\n"
-                       << "Unable to open file!" << endl;
-               return;
-       }
-
-       bool error = false;
-
-       int snippet_counter = 1;
-       while (!in.eof() && it != end) {
-               string snippet;
-               int id;
-               double ascent_fraction;
-
-               in >> snippet >> id >> ascent_fraction;
-
-               if (!in.good())
-                       // eof after all
-                       break;
-
-               error = snippet != "Snippet";
-               if (error)
-                       break;
-
-               error = id != snippet_counter;
-               if (error)
-                       break;
-
-               *it = ascent_fraction;
-
-               ++snippet_counter;
-               ++it;
-       }
-
-       if (error) {
-               lyxerr[Debug::GRAPHICS]
-                       << "setAscentFractions(" << metrics_file << ")\n"
-                       << "Error reading file!\n" << endl;
-       }
-}
-
-} // namespace anon