]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/PreviewLoader.cpp
de.po: fix doubled accelerator keys
[lyx.git] / src / graphics / PreviewLoader.cpp
index 5cd738f5d0da9ebd6cf0ebc7f352e1d14f4f91cf..514b6253d35facb1efdf88a337ad7e1c2b3a2aa3 100644 (file)
@@ -17,7 +17,6 @@
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "Converter.h"
-#include "debug.h"
 #include "Encoding.h"
 #include "Format.h"
 #include "InsetIterator.h"
 
 #include "insets/Inset.h"
 
+#include "support/convert.h"
+#include "support/debug.h"
+#include "support/FileName.h"
 #include "support/filetools.h"
-#include "support/Forkedcall.h"
-#include "support/ForkedcallsController.h"
+#include "support/ForkedCalls.h"
 #include "support/lstrings.h"
-#include "support/lyxlib.h"
-#include "support/convert.h"
 
 #include <boost/bind.hpp>
 
 #include <fstream>
 #include <iomanip>
 
-using lyx::support::FileName;
-
-using std::endl;
-using std::find;
-using std::fill;
-using std::find_if;
-using std::make_pair;
+using namespace std;
+using namespace lyx::support;
 
 using boost::bind;
 
-using std::ifstream;
-using std::list;
-using std::map;
-using std::ostringstream;
-using std::pair;
-using std::vector;
-using std::string;
-
-
 namespace {
 
 typedef pair<string, FileName> SnippetPair;
@@ -78,7 +63,7 @@ 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);
+       return addName(bufferpath, filename);
 }
 
 
@@ -88,7 +73,7 @@ lyx::Converter const * setConverter()
 
        typedef vector<string> FmtList;
        typedef lyx::graphics::Cache GCache;
-       FmtList const loadableFormats = GCache::get().loadableFormats();
+       FmtList const loadableFormats = GCache::get().loadableFormats();
        FmtList::const_iterator it = loadableFormats.begin();
        FmtList::const_iterator const end = loadableFormats.end();
 
@@ -105,10 +90,8 @@ lyx::Converter const * setConverter()
        static bool first = true;
        if (first) {
                first = false;
-               lyx::lyxerr << "PreviewLoader::startLoading()\n"
-                      << "No converter from \"lyxpreview\" format has been "
-                       "defined."
-                      << endl;
+               LYXERR0("PreviewLoader::startLoading()\n"
+                 << "No converter from \"lyxpreview\" format has been defined.");
        }
        return 0;
 }
@@ -125,9 +108,8 @@ void setAscentFractions(vector<double> & ascent_fractions,
 
        ifstream in(metrics_file.toFilesystemEncoding().c_str());
        if (!in.good()) {
-               lyx::lyxerr[lyx::Debug::GRAPHICS]
-                       << "setAscentFractions(" << metrics_file << ")\n"
-                       << "Unable to open file!" << endl;
+               LYXERR(lyx::Debug::GRAPHICS, "setAscentFractions(" << metrics_file << ")\n"
+                       << "Unable to open file!");
                return;
        }
 
@@ -160,20 +142,17 @@ void setAscentFractions(vector<double> & ascent_fractions,
        }
 
        if (error) {
-               lyx::lyxerr[lyx::Debug::GRAPHICS]
-                       << "setAscentFractions(" << metrics_file << ")\n"
-                       << "Error reading file!\n" << endl;
+               LYXERR(lyx::Debug::GRAPHICS, "setAscentFractions(" << metrics_file << ")\n"
+                       << "Error reading file!\n");
        }
 }
 
 
-class FindFirst : public std::unary_function<SnippetPair, bool> {
+class FindFirst
+{
 public:
        FindFirst(string const & comp) : comp_(comp) {}
-       bool operator()(SnippetPair const & sp) const
-       {
-               return sp.first == comp_;
-       }
+       bool operator()(SnippetPair const & sp) const { return sp.first == comp_; }
 private:
        string const comp_;
 };
@@ -235,7 +214,7 @@ public:
        Buffer const & buffer() const { return buffer_; }
 
 private:
-       /// Called by the Forkedcall process that generated the bitmap files.
+       /// Called by the ForkedCall process that generated the bitmap files.
        void finishedGenerating(pid_t, int);
        ///
        void dumpPreamble(odocstream &) const;
@@ -375,14 +354,14 @@ InProgress::InProgress(string const & filename_base,
                       PendingSnippets const & pending,
                       string const & to_format)
        : pid(0),
-         metrics_file(FileName(filename_base + ".metrics")),
+         metrics_file(filename_base + ".metrics"),
          snippets(pending.size())
 {
        PendingSnippets::const_iterator pit  = pending.begin();
        PendingSnippets::const_iterator pend = pending.end();
        BitmapFile::iterator sit = snippets.begin();
 
-       std::transform(pit, pend, sit,
+       transform(pit, pend, sit,
                       IncrementedFileName(to_format, filename_base));
 }
 
@@ -390,16 +369,16 @@ InProgress::InProgress(string const & filename_base,
 void InProgress::stop() const
 {
        if (pid)
-               lyx::support::ForkedcallsController::get().kill(pid, 0);
+               ForkedCallsController::kill(pid, 0);
 
        if (!metrics_file.empty())
-               lyx::support::unlink(metrics_file);
+               metrics_file.removeFile();
 
        BitmapFile::const_iterator vit  = snippets.begin();
        BitmapFile::const_iterator vend = snippets.end();
        for (; vit != vend; ++vit) {
                if (!vit->second.empty())
-                       lyx::support::unlink(vit->second);
+                       vit->second.removeFile();
        }
 }
 
@@ -428,9 +407,8 @@ PreviewLoader::Impl::~Impl()
        InProgressProcesses::iterator ipit  = in_progress_.begin();
        InProgressProcesses::iterator ipend = in_progress_.end();
 
-       for (; ipit != ipend; ++ipit) {
+       for (; ipit != ipend; ++ipit)
                ipit->second.stop();
-       }
 }
 
 
@@ -444,7 +422,7 @@ PreviewLoader::Impl::preview(string const & latex_snippet) const
 
 namespace {
 
-class FindSnippet : public std::unary_function<InProgressProcess, bool> {
+class FindSnippet {
 public:
        FindSnippet(string const & s) : snippet_(s) {}
        bool operator()(InProgressProcess const & process) const
@@ -491,7 +469,7 @@ void PreviewLoader::Impl::add(string const & latex_snippet)
        if (!pconverter_ || status(latex_snippet) != NotFound)
                return;
 
-       string const snippet = support::trim(latex_snippet);
+       string const snippet = trim(latex_snippet);
        if (snippet.empty())
                return;
 
@@ -538,7 +516,7 @@ void PreviewLoader::Impl::remove(string const & latex_snippet)
        InProgressProcesses::iterator ipit  = in_progress_.begin();
        InProgressProcesses::iterator ipend = in_progress_.end();
 
-       std::for_each(ipit, ipend, EraseSnippet(latex_snippet));
+       for_each(ipit, ipend, EraseSnippet(latex_snippet));
 
        while (ipit != ipend) {
                InProgressProcesses::iterator curr = ipit++;
@@ -576,7 +554,14 @@ void PreviewLoader::Impl::startLoading()
 
        // we use the encoding of the buffer
        Encoding const & enc = buffer_.params().encoding();
-       odocfstream of(enc.iconvName());
+       odocfstream of;
+       try { of.reset(enc.iconvName()); }
+       catch (iconv_codecvt_facet_exception & e) {
+               LYXERR0("Caught iconv exception: " << e.what()
+                       << "\nUnable to create LaTeX file: " << latexfile);
+               return;
+       }
+
        TexRow texrow;
        OutputParams runparams(&enc);
        LaTeXFeatures features(buffer_, buffer_.params(), runparams);
@@ -606,20 +591,20 @@ void PreviewLoader::Impl::startLoading()
        // The conversion command.
        ostringstream cs;
        cs << pconverter_->command << ' ' << pconverter_->to << ' '
-          << support::quoteName(latexfile.toFilesystemEncoding()) << ' '
+          << quoteName(latexfile.toFilesystemEncoding()) << ' '
           << int(font_scaling_factor_) << ' '
           << theApp()->hexName(Color_preview) << ' '
           << theApp()->hexName(Color_background);
 
-       string const command = support::libScriptSearch(cs.str());
+       string const command = libScriptSearch(cs.str());
 
        // Initiate the conversion from LaTeX to bitmap images files.
-       support::Forkedcall::SignalTypePtr
-               convert_ptr(new support::Forkedcall::SignalType);
+       ForkedCall::SignalTypePtr
+               convert_ptr(new ForkedCall::SignalType);
        convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));
 
-       support::Forkedcall call;
-       int ret = call.startscript(command, convert_ptr);
+       ForkedCall call;
+       int ret = call.startScript(command, convert_ptr);
 
        if (ret != 0) {
                LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
@@ -661,7 +646,7 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
        BitmapFile::const_iterator it  = git->second.snippets.begin();
        BitmapFile::const_iterator end = git->second.snippets.end();
 
-       std::list<PreviewImagePtr> newimages;
+       list<PreviewImagePtr> newimages;
 
        int metrics_counter = 0;
        for (; it != end; ++it, ++metrics_counter) {
@@ -679,9 +664,9 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
        in_progress_.erase(git);
 
        // Tell the outside world
-       std::list<PreviewImagePtr>::const_reverse_iterator
+       list<PreviewImagePtr>::const_reverse_iterator
                nit  = newimages.rbegin();
-       std::list<PreviewImagePtr>::const_reverse_iterator
+       list<PreviewImagePtr>::const_reverse_iterator
                nend = newimages.rend();
        for (; nit != nend; ++nit) {
                imageReady(*nit->get());
@@ -723,7 +708,7 @@ void PreviewLoader::Impl::dumpPreamble(odocstream & os) const
 
        for (; it != end; ++it)
                if (it->lyxCode() == MATHMACRO_CODE)
-                       it->latex(buffer_, os, runparams);
+                       it->latex(os, runparams);
 
        // All equation labels appear as "(#)" + preview.sty's rendering of
        // the label name
@@ -732,8 +717,19 @@ void PreviewLoader::Impl::dumpPreamble(odocstream & os) const
 
        // Use the preview style file to ensure that each snippet appears on a
        // fresh page.
+       // Also support PDF output (automatically generated e.g. when
+       // \usepackage[pdftex]{hyperref} is used.
        os << "\n"
-          << "\\usepackage[active,delayed,dvips,showlabels,lyx]{preview}\n"
+          << "\\newif\\ifpdf\n"
+          << "\\ifx\\pdfoutput\\undefined\n"
+          << "\\else\\ifx\\pdfoutput\\relax\n"
+          << "\\else\\ifnum0=\\pdfoutput\n"
+          << "\\else\\pdftrue\\fi\\fi\\fi\n"
+          << "\\ifpdf\n"
+          << "  \\usepackage[active,delayed,tightpage,showlabels,lyx,pdftex]{preview}\n"
+          << "\\else\n"
+          << "  \\usepackage[active,delayed,showlabels,lyx,dvips]{preview}\n"
+          << "\\fi\n"
           << "\n";
 }