2002-09-05 11:31:30 +00:00
|
|
|
/**
|
2003-08-23 00:17:00 +00:00
|
|
|
* \file PreviewLoader.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-05 21:24:15 +00:00
|
|
|
*
|
2002-11-04 02:12:42 +00:00
|
|
|
* \author Angus Leeming
|
2002-09-05 11:31:30 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-07-05 21:24:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "PreviewLoader.h"
|
|
|
|
#include "PreviewImage.h"
|
2004-04-16 14:34:41 +00:00
|
|
|
#include "GraphicsCache.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "converter.h"
|
|
|
|
#include "debug.h"
|
2003-09-06 23:36:02 +00:00
|
|
|
#include "format.h"
|
2004-03-27 12:46:30 +00:00
|
|
|
#include "insetiterator.h"
|
2003-09-16 09:44:34 +00:00
|
|
|
#include "LColor.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
#include "lyxrc.h"
|
2003-11-05 12:06:20 +00:00
|
|
|
#include "outputparams.h"
|
2003-11-20 16:40:09 +00:00
|
|
|
#include "paragraph.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2006-10-12 14:10:13 +00:00
|
|
|
#include "frontends/Application.h" // hexName
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "insets/Inset.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/forkedcall.h"
|
2002-07-06 12:38:44 +00:00
|
|
|
#include "support/forkedcontr.h"
|
2003-05-23 13:54:09 +00:00
|
|
|
#include "support/lstrings.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
#include "support/lyxlib.h"
|
2005-01-06 16:39:35 +00:00
|
|
|
#include "support/convert.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2002-07-05 21:24:15 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
using lyx::support::FileName;
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
using std::endl;
|
2002-07-08 13:01:09 +00:00
|
|
|
using std::find;
|
2002-07-09 09:30:54 +00:00
|
|
|
using std::fill;
|
2002-07-06 11:36:11 +00:00
|
|
|
using std::find_if;
|
2002-07-08 13:01:09 +00:00
|
|
|
using std::make_pair;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2004-01-31 15:30:24 +00:00
|
|
|
using boost::bind;
|
|
|
|
|
2003-09-06 23:36:02 +00:00
|
|
|
using std::ifstream;
|
2002-07-09 09:30:54 +00:00
|
|
|
using std::list;
|
2002-07-06 11:36:11 +00:00
|
|
|
using std::map;
|
2003-09-05 18:02:24 +00:00
|
|
|
using std::ostringstream;
|
2002-07-06 11:36:11 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::vector;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
namespace {
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
typedef pair<string, FileName> SnippetPair;
|
2002-07-06 11:36:11 +00:00
|
|
|
|
2007-02-25 22:18:13 +00:00
|
|
|
// A list of all snippets to be converted to previews
|
2002-07-16 18:10:51 +00:00
|
|
|
typedef list<string> PendingSnippets;
|
2002-07-09 09:30:54 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
// Each item in the vector is a pair<snippet, image file name>.
|
2006-11-26 21:30:39 +00:00
|
|
|
typedef vector<SnippetPair> BitmapFile;
|
2002-07-09 09:30:54 +00:00
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
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;
|
|
|
|
|
2007-01-17 13:18:16 +00:00
|
|
|
lyx::Converter const * ptr = lyx::theConverters().getConverter(from, to);
|
2006-10-21 00:16:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
|
|
|
|
void setAscentFractions(vector<double> & ascent_fractions,
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const & metrics_file)
|
2006-10-21 00:16:43 +00:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
ifstream in(metrics_file.toFilesystemEncoding().c_str());
|
2006-10-21 00:16:43 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
class FindFirst : public std::unary_function<SnippetPair, bool> {
|
2004-01-31 15:30:24 +00:00
|
|
|
public:
|
2002-07-06 11:36:11 +00:00
|
|
|
FindFirst(string const & comp) : comp_(comp) {}
|
2006-11-26 21:30:39 +00:00
|
|
|
bool operator()(SnippetPair const & sp) const
|
2002-07-06 11:36:11 +00:00
|
|
|
{
|
2003-02-25 11:32:40 +00:00
|
|
|
return sp.first == comp_;
|
2002-07-06 11:36:11 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
string const comp_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
/// Store info on a currently executing, forked process.
|
2005-01-19 15:03:31 +00:00
|
|
|
class InProgress {
|
|
|
|
public:
|
2002-07-08 13:01:09 +00:00
|
|
|
///
|
|
|
|
InProgress() : pid(0) {}
|
|
|
|
///
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgress(string const & filename_base,
|
2002-07-16 18:10:51 +00:00
|
|
|
PendingSnippets const & pending,
|
2002-07-09 09:30:54 +00:00
|
|
|
string const & to_format);
|
2002-07-17 16:56:42 +00:00
|
|
|
/// Remove any files left lying around and kill the forked process.
|
2002-07-08 13:01:09 +00:00
|
|
|
void stop() const;
|
|
|
|
|
|
|
|
///
|
|
|
|
pid_t pid;
|
|
|
|
///
|
2002-10-31 12:42:26 +00:00
|
|
|
string command;
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName metrics_file;
|
2002-07-16 18:10:51 +00:00
|
|
|
///
|
|
|
|
BitmapFile snippets;
|
2002-07-08 13:01:09 +00:00
|
|
|
};
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
typedef map<pid_t, InProgress> InProgressProcesses;
|
2002-07-16 18:10:51 +00:00
|
|
|
|
|
|
|
typedef InProgressProcesses::value_type InProgressProcess;
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace graphics {
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class PreviewLoader::Impl : public boost::signals::trackable {
|
|
|
|
public:
|
2002-07-05 21:24:15 +00:00
|
|
|
///
|
|
|
|
Impl(PreviewLoader & p, Buffer const & b);
|
2002-07-08 13:01:09 +00:00
|
|
|
/// Stop any InProgress items still executing.
|
2002-07-06 12:38:44 +00:00
|
|
|
~Impl();
|
|
|
|
///
|
2002-07-05 21:24:15 +00:00
|
|
|
PreviewImage const * preview(string const & latex_snippet) const;
|
|
|
|
///
|
|
|
|
PreviewLoader::Status status(string const & latex_snippet) const;
|
|
|
|
///
|
|
|
|
void add(string const & latex_snippet);
|
|
|
|
///
|
|
|
|
void remove(string const & latex_snippet);
|
|
|
|
///
|
|
|
|
void startLoading();
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
/// Emit this signal when an image is ready for display.
|
2004-09-26 14:19:47 +00:00
|
|
|
boost::signal<void(PreviewImage const &)> imageReady;
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-08-02 16:30:58 +00:00
|
|
|
Buffer const & buffer() const { return buffer_; }
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
private:
|
2002-07-05 21:24:15 +00:00
|
|
|
/// Called by the Forkedcall process that generated the bitmap files.
|
2002-10-31 12:42:26 +00:00
|
|
|
void finishedGenerating(pid_t, int);
|
2002-07-05 21:24:15 +00:00
|
|
|
///
|
2006-10-19 16:51:30 +00:00
|
|
|
void dumpPreamble(odocstream &) const;
|
2002-07-05 21:24:15 +00:00
|
|
|
///
|
2006-10-19 16:51:30 +00:00
|
|
|
void dumpData(odocstream &, BitmapFile const &) const;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
/** cache_ allows easy retrieval of already-generated images
|
2002-07-05 21:24:15 +00:00
|
|
|
* using the LaTeX snippet as the identifier.
|
|
|
|
*/
|
|
|
|
typedef boost::shared_ptr<PreviewImage> PreviewImagePtr;
|
|
|
|
///
|
2002-07-06 11:36:11 +00:00
|
|
|
typedef map<string, PreviewImagePtr> Cache;
|
2002-07-05 21:24:15 +00:00
|
|
|
///
|
|
|
|
Cache cache_;
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
/** pending_ stores the LaTeX snippets in anticipation of them being
|
|
|
|
* sent to the converter.
|
2002-07-05 21:24:15 +00:00
|
|
|
*/
|
2002-07-16 18:10:51 +00:00
|
|
|
PendingSnippets pending_;
|
2002-07-06 12:38:44 +00:00
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
/** in_progress_ stores all forked processes so that we can proceed
|
|
|
|
* thereafter.
|
|
|
|
The map uses the conversion commands as its identifiers.
|
|
|
|
*/
|
2002-07-16 18:10:51 +00:00
|
|
|
InProgressProcesses in_progress_;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
PreviewLoader & parent_;
|
|
|
|
///
|
|
|
|
Buffer const & buffer_;
|
2002-07-08 18:25:30 +00:00
|
|
|
///
|
2002-07-09 09:30:54 +00:00
|
|
|
double font_scaling_factor_;
|
|
|
|
|
|
|
|
/// We don't own this
|
2007-03-20 21:53:01 +00:00
|
|
|
static lyx::Converter const * pconverter_;
|
2002-07-05 21:24:15 +00:00
|
|
|
};
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2007-03-20 21:53:01 +00:00
|
|
|
lyx::Converter const * PreviewLoader::Impl::pconverter_;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
//
|
2002-07-08 13:01:09 +00:00
|
|
|
// The public interface, defined in PreviewLoader.h
|
2006-10-21 00:16:43 +00:00
|
|
|
//
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
PreviewLoader::PreviewLoader(Buffer const & b)
|
|
|
|
: pimpl_(new Impl(*this, b))
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
PreviewLoader::~PreviewLoader()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
PreviewImage const * PreviewLoader::preview(string const & latex_snippet) const
|
|
|
|
{
|
|
|
|
return pimpl_->preview(latex_snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PreviewLoader::Status PreviewLoader::status(string const & latex_snippet) const
|
|
|
|
{
|
|
|
|
return pimpl_->status(latex_snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void PreviewLoader::add(string const & latex_snippet) const
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
|
|
|
pimpl_->add(latex_snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void PreviewLoader::remove(string const & latex_snippet) const
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
|
|
|
pimpl_->remove(latex_snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void PreviewLoader::startLoading() const
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
|
|
|
pimpl_->startLoading();
|
|
|
|
}
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
|
|
|
boost::signals::connection PreviewLoader::connect(slot_type const & slot) const
|
|
|
|
{
|
|
|
|
return pimpl_->imageReady.connect(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::emitSignal(PreviewImage const & pimage) const
|
|
|
|
{
|
|
|
|
pimpl_->imageReady(pimage);
|
|
|
|
}
|
|
|
|
|
2002-08-02 16:30:58 +00:00
|
|
|
|
|
|
|
Buffer const & PreviewLoader::buffer() const
|
|
|
|
{
|
|
|
|
return pimpl_->buffer();
|
|
|
|
}
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // namespace lyx
|
2002-07-08 13:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
// The details of the Impl
|
|
|
|
// =======================
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class IncrementedFileName {
|
|
|
|
public:
|
2002-07-16 18:10:51 +00:00
|
|
|
IncrementedFileName(string const & to_format,
|
2002-09-04 10:15:56 +00:00
|
|
|
string const & filename_base)
|
|
|
|
: to_format_(to_format), base_(filename_base), counter_(1)
|
2002-07-16 18:10:51 +00:00
|
|
|
{}
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
SnippetPair const operator()(string const & snippet)
|
2002-07-16 18:10:51 +00:00
|
|
|
{
|
|
|
|
ostringstream os;
|
2002-11-27 10:30:28 +00:00
|
|
|
os << base_ << counter_++ << '.' << to_format_;
|
2003-09-15 11:00:00 +00:00
|
|
|
string const file = os.str();
|
2002-07-16 18:10:51 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
return make_pair(snippet, FileName(file));
|
2002-07-16 18:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
string const & to_format_;
|
|
|
|
string const & base_;
|
|
|
|
int counter_;
|
|
|
|
};
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgress::InProgress(string const & filename_base,
|
2002-07-16 18:10:51 +00:00
|
|
|
PendingSnippets const & pending,
|
2002-07-09 09:30:54 +00:00
|
|
|
string const & to_format)
|
|
|
|
: pid(0),
|
2006-11-26 21:30:39 +00:00
|
|
|
metrics_file(FileName(filename_base + ".metrics")),
|
2002-07-09 09:30:54 +00:00
|
|
|
snippets(pending.size())
|
|
|
|
{
|
2002-07-16 18:10:51 +00:00
|
|
|
PendingSnippets::const_iterator pit = pending.begin();
|
|
|
|
PendingSnippets::const_iterator pend = pending.end();
|
|
|
|
BitmapFile::iterator sit = snippets.begin();
|
2002-07-09 09:30:54 +00:00
|
|
|
|
2002-11-04 02:12:42 +00:00
|
|
|
std::transform(pit, pend, sit,
|
2002-09-04 10:15:56 +00:00
|
|
|
IncrementedFileName(to_format, filename_base));
|
2002-07-09 09:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
void InProgress::stop() const
|
|
|
|
{
|
|
|
|
if (pid)
|
2006-10-21 00:16:43 +00:00
|
|
|
lyx::support::ForkedcallsController::get().kill(pid, 0);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
|
|
|
if (!metrics_file.empty())
|
2006-10-21 00:16:43 +00:00
|
|
|
lyx::support::unlink(metrics_file);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
BitmapFile::const_iterator vit = snippets.begin();
|
|
|
|
BitmapFile::const_iterator vend = snippets.end();
|
2002-07-08 13:01:09 +00:00
|
|
|
for (; vit != vend; ++vit) {
|
|
|
|
if (!vit->second.empty())
|
2006-10-21 00:16:43 +00:00
|
|
|
lyx::support::unlink(vit->second);
|
2002-07-08 13:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace graphics {
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
|
|
|
|
: parent_(p), buffer_(b), font_scaling_factor_(0.0)
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
2002-08-06 12:55:28 +00:00
|
|
|
font_scaling_factor_ = 0.01 * lyxrc.dpi * lyxrc.zoom *
|
2005-01-27 21:05:44 +00:00
|
|
|
convert<double>(lyxrc.preview_scale_factor);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "The font scaling factor is "
|
2002-07-09 09:30:54 +00:00
|
|
|
<< font_scaling_factor_ << endl;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
if (!pconverter_)
|
|
|
|
pconverter_ = setConverter();
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-06 12:38:44 +00:00
|
|
|
PreviewLoader::Impl::~Impl()
|
|
|
|
{
|
2002-07-16 18:10:51 +00:00
|
|
|
InProgressProcesses::iterator ipit = in_progress_.begin();
|
|
|
|
InProgressProcesses::iterator ipend = in_progress_.end();
|
2002-07-06 12:38:44 +00:00
|
|
|
|
|
|
|
for (; ipit != ipend; ++ipit) {
|
2002-07-08 13:01:09 +00:00
|
|
|
ipit->second.stop();
|
|
|
|
}
|
|
|
|
}
|
2002-07-06 12:38:44 +00:00
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
PreviewImage const *
|
|
|
|
PreviewLoader::Impl::preview(string const & latex_snippet) const
|
|
|
|
{
|
|
|
|
Cache::const_iterator it = cache_.find(latex_snippet);
|
|
|
|
return (it == cache_.end()) ? 0 : it->second.get();
|
2002-07-06 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
namespace {
|
|
|
|
|
2004-01-31 15:30:24 +00:00
|
|
|
class FindSnippet : public std::unary_function<InProgressProcess, bool> {
|
|
|
|
public:
|
2002-07-16 18:10:51 +00:00
|
|
|
FindSnippet(string const & s) : snippet_(s) {}
|
2004-01-31 15:30:24 +00:00
|
|
|
bool operator()(InProgressProcess const & process) const
|
2002-07-16 18:10:51 +00:00
|
|
|
{
|
|
|
|
BitmapFile const & snippets = process.second.snippets;
|
2004-01-31 15:30:24 +00:00
|
|
|
BitmapFile::const_iterator beg = snippets.begin();
|
2002-07-16 18:10:51 +00:00
|
|
|
BitmapFile::const_iterator end = snippets.end();
|
2004-01-31 15:30:24 +00:00
|
|
|
return find_if(beg, end, FindFirst(snippet_)) != end;
|
2002-07-16 18:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2004-01-31 15:30:24 +00:00
|
|
|
string const snippet_;
|
2002-07-16 18:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
PreviewLoader::Status
|
|
|
|
PreviewLoader::Impl::status(string const & latex_snippet) const
|
|
|
|
{
|
|
|
|
Cache::const_iterator cit = cache_.find(latex_snippet);
|
|
|
|
if (cit != cache_.end())
|
2002-07-08 13:01:09 +00:00
|
|
|
return Ready;
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
PendingSnippets::const_iterator pit = pending_.begin();
|
|
|
|
PendingSnippets::const_iterator pend = pending_.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
pit = find(pit, pend, latex_snippet);
|
2002-07-09 09:30:54 +00:00
|
|
|
if (pit != pend)
|
2002-07-08 13:01:09 +00:00
|
|
|
return InQueue;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
InProgressProcesses::const_iterator ipit = in_progress_.begin();
|
|
|
|
InProgressProcesses::const_iterator ipend = in_progress_.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
ipit = find_if(ipit, ipend, FindSnippet(latex_snippet));
|
|
|
|
if (ipit != ipend)
|
|
|
|
return Processing;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
return NotFound;
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::Impl::add(string const & latex_snippet)
|
|
|
|
{
|
2002-07-09 09:30:54 +00:00
|
|
|
if (!pconverter_ || status(latex_snippet) != NotFound)
|
2002-07-05 21:24:15 +00:00
|
|
|
return;
|
|
|
|
|
2003-09-04 00:29:22 +00:00
|
|
|
string const snippet = support::trim(latex_snippet);
|
2002-08-02 16:30:58 +00:00
|
|
|
if (snippet.empty())
|
|
|
|
return;
|
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "adding snippet:\n" << snippet << endl;
|
2002-08-01 17:28:59 +00:00
|
|
|
|
2002-08-02 16:30:58 +00:00
|
|
|
pending_.push_back(snippet);
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
namespace {
|
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class EraseSnippet {
|
|
|
|
public:
|
2002-07-16 18:10:51 +00:00
|
|
|
EraseSnippet(string const & s) : snippet_(s) {}
|
|
|
|
void operator()(InProgressProcess & process)
|
|
|
|
{
|
|
|
|
BitmapFile & snippets = process.second.snippets;
|
|
|
|
BitmapFile::iterator it = snippets.begin();
|
|
|
|
BitmapFile::iterator end = snippets.end();
|
|
|
|
|
|
|
|
it = find_if(it, end, FindFirst(snippet_));
|
|
|
|
if (it != end)
|
|
|
|
snippets.erase(it, it+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
string const & snippet_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
void PreviewLoader::Impl::remove(string const & latex_snippet)
|
|
|
|
{
|
|
|
|
Cache::iterator cit = cache_.find(latex_snippet);
|
|
|
|
if (cit != cache_.end())
|
|
|
|
cache_.erase(cit);
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
PendingSnippets::iterator pit = pending_.begin();
|
|
|
|
PendingSnippets::iterator pend = pending_.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
pending_.erase(std::remove(pit, pend, latex_snippet), pend);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
InProgressProcesses::iterator ipit = in_progress_.begin();
|
|
|
|
InProgressProcesses::iterator ipend = in_progress_.end();
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
std::for_each(ipit, ipend, EraseSnippet(latex_snippet));
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2003-09-14 19:59:52 +00:00
|
|
|
while (ipit != ipend) {
|
2002-07-16 18:10:51 +00:00
|
|
|
InProgressProcesses::iterator curr = ipit++;
|
|
|
|
if (curr->second.snippets.empty())
|
2002-07-05 21:24:15 +00:00
|
|
|
in_progress_.erase(curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::Impl::startLoading()
|
|
|
|
{
|
2002-07-09 09:30:54 +00:00
|
|
|
if (pending_.empty() || !pconverter_)
|
2002-07-08 13:01:09 +00:00
|
|
|
return;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2003-10-22 13:15:18 +00:00
|
|
|
// Only start the process off after the buffer is loaded from file.
|
|
|
|
if (!buffer_.fully_loaded())
|
|
|
|
return;
|
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "PreviewLoader::startLoading()" << endl;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
// As used by the LaTeX file and by the resulting image files
|
2004-02-25 12:00:53 +00:00
|
|
|
string const directory = buffer_.temppath();
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
string const filename_base = unique_filename(directory);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
// Create an InProgress instance to place in the map of all
|
|
|
|
// such processes if it starts correctly.
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgress inprogress(filename_base, pending_, pconverter_->to);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-09-04 10:15:56 +00:00
|
|
|
// clear pending_, so we're ready to start afresh.
|
|
|
|
pending_.clear();
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
// Output the LaTeX file.
|
2006-12-04 15:46:57 +00:00
|
|
|
FileName const latexfile(filename_base + ".tex");
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
// FIXME UNICODE
|
|
|
|
// This creates an utf8 encoded file, but the proper inputenc
|
|
|
|
// command is missing.
|
2006-12-04 15:46:57 +00:00
|
|
|
odocfstream of(latexfile.toFilesystemEncoding().c_str());
|
2004-02-25 12:00:53 +00:00
|
|
|
if (!of) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "PreviewLoader::startLoading()\n"
|
2004-02-25 12:00:53 +00:00
|
|
|
<< "Unable to create LaTeX file\n"
|
|
|
|
<< latexfile << endl;
|
|
|
|
return;
|
|
|
|
}
|
2002-07-10 09:09:37 +00:00
|
|
|
of << "\\batchmode\n";
|
2002-07-05 21:24:15 +00:00
|
|
|
dumpPreamble(of);
|
|
|
|
of << "\n\\begin{document}\n";
|
2002-07-06 11:36:11 +00:00
|
|
|
dumpData(of, inprogress.snippets);
|
2002-07-05 21:24:15 +00:00
|
|
|
of << "\n\\end{document}\n";
|
|
|
|
of.close();
|
|
|
|
|
|
|
|
// The conversion command.
|
|
|
|
ostringstream cs;
|
2004-04-16 14:34:41 +00:00
|
|
|
cs << pconverter_->command << ' ' << pconverter_->to << ' '
|
2006-12-04 15:46:57 +00:00
|
|
|
<< support::quoteName(latexfile.toFilesystemEncoding()) << ' '
|
2005-06-13 23:23:00 +00:00
|
|
|
<< int(font_scaling_factor_) << ' '
|
2006-11-30 00:04:51 +00:00
|
|
|
<< theApp()->hexName(LColor::preview) << ' '
|
|
|
|
<< theApp()->hexName(LColor::background);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
string const command = support::libScriptSearch(cs.str());
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
// Initiate the conversion from LaTeX to bitmap images files.
|
2003-09-04 00:29:22 +00:00
|
|
|
support::Forkedcall::SignalTypePtr
|
|
|
|
convert_ptr(new support::Forkedcall::SignalType);
|
2004-01-31 15:30:24 +00:00
|
|
|
convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2003-09-04 00:29:22 +00:00
|
|
|
support::Forkedcall call;
|
2002-07-05 21:24:15 +00:00
|
|
|
int ret = call.startscript(command, convert_ptr);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "PreviewLoader::startLoading()\n"
|
2004-08-16 11:27:51 +00:00
|
|
|
<< "Unable to start process\n"
|
2002-07-05 21:24:15 +00:00
|
|
|
<< command << endl;
|
2002-07-06 11:36:11 +00:00
|
|
|
return;
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-06 12:50:09 +00:00
|
|
|
// Store the generation process in a list of all such processes
|
2002-07-06 12:38:44 +00:00
|
|
|
inprogress.pid = call.pid();
|
2002-10-31 12:42:26 +00:00
|
|
|
inprogress.command = command;
|
|
|
|
in_progress_[inprogress.pid] = inprogress;
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
2002-10-31 12:42:26 +00:00
|
|
|
// 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;
|
2002-07-05 21:24:15 +00:00
|
|
|
string const status = retval > 0 ? "failed" : "succeeded";
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GRAPHICS) << "PreviewLoader::finishedInProgress("
|
2002-07-05 21:24:15 +00:00
|
|
|
<< retval << "): processing " << status
|
|
|
|
<< " for " << command << endl;
|
|
|
|
if (retval > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Read the metrics file, if it exists
|
2002-07-09 09:30:54 +00:00
|
|
|
vector<double> ascent_fractions(git->second.snippets.size());
|
|
|
|
setAscentFractions(ascent_fractions, git->second.metrics_file);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
// Add these newly generated bitmap files to the cache and
|
|
|
|
// start loading them into LyX.
|
2002-07-16 18:10:51 +00:00
|
|
|
BitmapFile::const_iterator it = git->second.snippets.begin();
|
|
|
|
BitmapFile::const_iterator end = git->second.snippets.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
std::list<PreviewImagePtr> newimages;
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
int metrics_counter = 0;
|
2002-07-09 09:30:54 +00:00
|
|
|
for (; it != end; ++it, ++metrics_counter) {
|
2002-07-05 21:24:15 +00:00
|
|
|
string const & snip = it->first;
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const & file = it->second;
|
2002-07-09 09:30:54 +00:00
|
|
|
double af = ascent_fractions[metrics_counter];
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
PreviewImagePtr ptr(new PreviewImage(parent_, snip, file, af));
|
2002-07-05 21:24:15 +00:00
|
|
|
cache_[snip] = ptr;
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
newimages.push_back(ptr);
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
// Remove the item from the list of still-executing processes.
|
2002-07-05 21:24:15 +00:00
|
|
|
in_progress_.erase(git);
|
2002-07-15 11:08:46 +00:00
|
|
|
|
|
|
|
// Tell the outside world
|
2003-02-25 12:33:33 +00:00
|
|
|
std::list<PreviewImagePtr>::const_reverse_iterator
|
2003-02-25 11:20:59 +00:00
|
|
|
nit = newimages.rbegin();
|
2003-02-25 12:33:33 +00:00
|
|
|
std::list<PreviewImagePtr>::const_reverse_iterator
|
2003-02-25 11:20:59 +00:00
|
|
|
nend = newimages.rend();
|
2002-07-15 11:08:46 +00:00
|
|
|
for (; nit != nend; ++nit) {
|
2002-07-17 16:56:42 +00:00
|
|
|
imageReady(*nit->get());
|
2002-07-15 11:08:46 +00:00
|
|
|
}
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
void PreviewLoader::Impl::dumpPreamble(odocstream & os) const
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
|
|
|
// Why on earth is Buffer::makeLaTeXFile a non-const method?
|
|
|
|
Buffer & tmp = const_cast<Buffer &>(buffer_);
|
|
|
|
// Dump the preamble only.
|
2007-03-18 10:59:16 +00:00
|
|
|
// We don't need an encoding for runparams since it is not used by
|
|
|
|
// the preamble.
|
|
|
|
OutputParams runparams(0);
|
2003-11-05 12:06:20 +00:00
|
|
|
runparams.flavor = OutputParams::LATEX;
|
2003-05-22 21:10:22 +00:00
|
|
|
runparams.nice = true;
|
2003-05-23 09:23:03 +00:00
|
|
|
runparams.moving_arg = true;
|
2003-05-23 08:59:47 +00:00
|
|
|
runparams.free_spacing = true;
|
2006-08-04 13:59:12 +00:00
|
|
|
tmp.writeLaTeXSource(os, buffer_.filePath(), runparams, true, false);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2003-01-21 17:54:03 +00:00
|
|
|
// FIXME! This is a HACK! The proper fix is to control the 'true'
|
|
|
|
// passed to WriteStream below:
|
2007-02-25 22:18:13 +00:00
|
|
|
// int InsetMathNest::latex(Buffer const &, odocstream & os,
|
|
|
|
// OutputParams const & runparams) const
|
2003-01-21 17:54:03 +00:00
|
|
|
// {
|
2003-05-23 09:23:03 +00:00
|
|
|
// WriteStream wi(os, runparams.moving_arg, true);
|
2003-01-21 17:54:03 +00:00
|
|
|
// par_->write(wi);
|
|
|
|
// return wi.line();
|
|
|
|
// }
|
|
|
|
os << "\n"
|
|
|
|
<< "\\def\\lyxlock{}\n"
|
|
|
|
<< "\n";
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
// Loop over the insets in the buffer and dump all the math-macros.
|
2004-04-13 13:10:33 +00:00
|
|
|
InsetBase & inset = buffer_.inset();
|
|
|
|
InsetIterator it = inset_iterator_begin(inset);
|
|
|
|
InsetIterator const end = inset_iterator_end(inset);
|
|
|
|
|
|
|
|
for (; it != end; ++it)
|
2004-11-25 19:13:07 +00:00
|
|
|
if (it->lyxCode() == InsetBase::MATHMACRO_CODE)
|
2003-08-28 07:41:31 +00:00
|
|
|
it->latex(buffer_, os, runparams);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2007-02-25 22:18:13 +00:00
|
|
|
// All equation labels appear as "(#)" + preview.sty's rendering of
|
2002-07-10 09:09:37 +00:00
|
|
|
// the label name
|
|
|
|
if (lyxrc.preview_hashed_labels)
|
|
|
|
os << "\\renewcommand{\\theequation}{\\#}\n";
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
// Use the preview style file to ensure that each snippet appears on a
|
|
|
|
// fresh page.
|
|
|
|
os << "\n"
|
2004-04-16 14:34:41 +00:00
|
|
|
<< "\\usepackage[active,delayed,dvips,showlabels,lyx]{preview}\n"
|
2002-07-05 21:24:15 +00:00
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
void PreviewLoader::Impl::dumpData(odocstream & os,
|
2002-07-16 18:10:51 +00:00
|
|
|
BitmapFile const & vec) const
|
2002-07-05 21:24:15 +00:00
|
|
|
{
|
2002-07-06 11:36:11 +00:00
|
|
|
if (vec.empty())
|
2002-07-05 21:24:15 +00:00
|
|
|
return;
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
BitmapFile::const_iterator it = vec.begin();
|
|
|
|
BitmapFile::const_iterator end = vec.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
2006-10-19 16:51:30 +00:00
|
|
|
// FIXME UNICODE
|
2002-07-05 21:24:15 +00:00
|
|
|
os << "\\begin{preview}\n"
|
2006-10-21 00:16:43 +00:00
|
|
|
<< from_utf8(it->first)
|
2002-07-05 21:24:15 +00:00
|
|
|
<< "\n\\end{preview}\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // namespace lyx
|
2002-07-08 18:25:30 +00:00
|
|
|
|