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"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include "frontends/lyx_gui.h" // hexname
|
|
|
|
|
2003-09-09 11:24:33 +00:00
|
|
|
#include "insets/inset.h"
|
|
|
|
|
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"
|
2003-09-05 03:10:30 +00:00
|
|
|
#include "support/tostr.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2003-09-05 17:23:11 +00:00
|
|
|
#include "support/std_sstream.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2003-09-04 00:29:22 +00:00
|
|
|
namespace support = lyx::support;
|
|
|
|
|
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;
|
|
|
|
using std::ofstream;
|
|
|
|
using std::ostream;
|
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 {
|
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
typedef pair<string, string> StrPair;
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
// A list of alll snippets to be converted to previews
|
|
|
|
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>.
|
|
|
|
typedef vector<StrPair> BitmapFile;
|
2002-07-09 09:30:54 +00:00
|
|
|
|
|
|
|
string const unique_filename(string const bufferpath);
|
|
|
|
|
|
|
|
Converter const * setConverter();
|
|
|
|
|
|
|
|
void setAscentFractions(vector<double> & ascent_fractions,
|
|
|
|
string const & metrics_file);
|
2002-07-06 11:36:11 +00:00
|
|
|
|
2004-01-31 15:30:24 +00:00
|
|
|
class FindFirst : public std::unary_function<StrPair, bool> {
|
|
|
|
public:
|
2002-07-06 11:36:11 +00:00
|
|
|
FindFirst(string const & comp) : comp_(comp) {}
|
2004-01-31 15:30:24 +00:00
|
|
|
bool operator()(StrPair 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.
|
|
|
|
struct InProgress {
|
|
|
|
///
|
|
|
|
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;
|
|
|
|
///
|
2002-07-08 13:01:09 +00:00
|
|
|
string 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
|
|
|
|
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace graphics {
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
struct PreviewLoader::Impl : public boost::signals::trackable {
|
|
|
|
///
|
|
|
|
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.
|
|
|
|
boost::signal1<void, PreviewImage const &> imageReady;
|
|
|
|
|
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
|
|
|
///
|
|
|
|
void dumpPreamble(ostream &) const;
|
|
|
|
///
|
2002-07-16 18:10:51 +00:00
|
|
|
void dumpData(ostream &, 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
|
2002-07-17 16:56:42 +00:00
|
|
|
static Converter const * pconverter_;
|
2002-07-05 21:24:15 +00:00
|
|
|
};
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
Converter const * PreviewLoader::Impl::pconverter_;
|
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
// The public interface, defined in PreviewLoader.h
|
|
|
|
// ================================================
|
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 {
|
|
|
|
|
2002-07-16 18:10:51 +00:00
|
|
|
struct IncrementedFileName {
|
|
|
|
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
|
|
|
{}
|
|
|
|
|
|
|
|
StrPair const operator()(string const & snippet)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
return make_pair(snippet, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
metrics_file(filename_base + ".metrics"),
|
|
|
|
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)
|
2003-09-04 00:29:22 +00:00
|
|
|
support::ForkedcallsController::get().kill(pid, 0);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
|
|
|
if (!metrics_file.empty())
|
2003-09-04 00:29:22 +00:00
|
|
|
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())
|
2003-09-04 00:29:22 +00:00
|
|
|
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 *
|
|
|
|
lyxrc.preview_scale_factor;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "The font scaling factor is "
|
|
|
|
<< 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;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
struct EraseSnippet {
|
|
|
|
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;
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()" << endl;
|
|
|
|
|
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
|
|
|
|
2003-02-26 15:42:18 +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.
|
2002-07-08 13:01:09 +00:00
|
|
|
string const latexfile = filename_base + ".tex";
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
ofstream of(latexfile.c_str());
|
2004-02-25 12:00:53 +00:00
|
|
|
if (!of) {
|
|
|
|
lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()\n"
|
|
|
|
<< "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;
|
2002-11-27 10:30:28 +00:00
|
|
|
cs << pconverter_->command << ' ' << latexfile << ' '
|
|
|
|
<< int(font_scaling_factor_) << ' ' << pconverter_->to;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2003-09-15 11:00:00 +00:00
|
|
|
string const command = "sh " + 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) {
|
|
|
|
lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()\n"
|
|
|
|
<< "Unable to start process \n"
|
|
|
|
<< 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";
|
|
|
|
lyxerr[Debug::GRAPHICS] << "PreviewLoader::finishedInProgress("
|
|
|
|
<< 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;
|
|
|
|
string 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2003-11-05 12:06:20 +00:00
|
|
|
OutputParams runparams;
|
|
|
|
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;
|
2003-07-26 21:37:10 +00:00
|
|
|
tmp.makeLaTeXFile(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:
|
2003-08-28 07:41:31 +00:00
|
|
|
// int InsetFormula::latex(Buffer const &, ostream & os,
|
2003-11-05 12:06:20 +00:00
|
|
|
// 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-03-27 12:46:30 +00:00
|
|
|
for (InsetIterator it(buffer_.inset()); it; ++it)
|
2003-07-25 21:20:24 +00:00
|
|
|
if (it->lyxCode() == InsetOld::MATHMACRO_CODE)
|
2003-08-28 07:41:31 +00:00
|
|
|
it->latex(buffer_, os, runparams);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-10 09:09:37 +00:00
|
|
|
// All equation lables appear as "(#)" + preview.sty's rendering of
|
|
|
|
// 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"
|
2002-08-05 13:31:48 +00:00
|
|
|
<< "\\usepackage[active,delayed,dvips,tightpage,showlabels,lyx]{preview}\n"
|
2002-07-05 21:24:15 +00:00
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
// This piece of PostScript magic ensures that the foreground and
|
|
|
|
// background colors are the same as the LyX screen.
|
|
|
|
string fg = lyx_gui::hexname(LColor::preview);
|
|
|
|
if (fg.empty()) fg = "000000";
|
|
|
|
|
|
|
|
string bg = lyx_gui::hexname(LColor::background);
|
|
|
|
if (bg.empty()) bg = "ffffff";
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
os << "\\AtBeginDocument{\\AtBeginDvi{%\n"
|
|
|
|
<< "\\special{!userdict begin/bop-hook{//bop-hook exec\n"
|
2002-11-27 10:30:28 +00:00
|
|
|
<< '<' << fg << bg << ">{255 div}forall setrgbcolor\n"
|
2002-07-05 21:24:15 +00:00
|
|
|
<< "clippath fill setrgbcolor}bind def end}}}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
void PreviewLoader::Impl::dumpData(ostream & 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) {
|
|
|
|
os << "\\begin{preview}\n"
|
2002-07-08 13:01:09 +00:00
|
|
|
<< 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
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
namespace {
|
2002-07-08 18:25:30 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
string const unique_filename(string const bufferpath)
|
|
|
|
{
|
|
|
|
static int theCounter = 0;
|
|
|
|
string const filename = tostr(theCounter++) + "lyxpreview";
|
2003-09-04 00:29:22 +00:00
|
|
|
return support::AddName(bufferpath, filename);
|
2002-07-08 18:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
Converter const * setConverter()
|
|
|
|
{
|
|
|
|
string const from = "lyxpreview";
|
2002-07-08 18:25:30 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
Formats::FormatList::const_iterator it = formats.begin();
|
|
|
|
Formats::FormatList::const_iterator end = formats.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
string const to = it->name();
|
|
|
|
if (from == to)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Converter const * ptr = converters.getConverter(from, to);
|
|
|
|
if (ptr)
|
|
|
|
return ptr;
|
|
|
|
}
|
2002-07-08 18:25:30 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
static bool first = true;
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
lyxerr << "PreviewLoader::startLoading()\n"
|
|
|
|
<< "No converter from \"lyxpreview\" format has been "
|
|
|
|
"defined."
|
|
|
|
<< endl;
|
|
|
|
}
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
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);
|
|
|
|
|
2002-08-06 12:55:28 +00:00
|
|
|
ifstream in(metrics_file.c_str());
|
|
|
|
if (!in.good()) {
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "setAscentFractions(" << metrics_file << ")\n"
|
|
|
|
<< "Unable to open file!" << endl;
|
2002-07-09 09:30:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-06 12:55:28 +00:00
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
// Tightpage dimensions affect all subsequent dimensions
|
|
|
|
int tp_ascent;
|
|
|
|
int tp_descent;
|
|
|
|
|
|
|
|
int snippet_counter = 0;
|
|
|
|
while (!in.eof()) {
|
|
|
|
// Expecting lines of the form
|
|
|
|
// Preview: Tightpage tp_bl_x tp_bl_y tp_tr_x tp_tr_y
|
|
|
|
// Preview: Snippet id ascent descent width
|
|
|
|
string preview;
|
|
|
|
string type;
|
|
|
|
in >> preview >> type;
|
|
|
|
|
|
|
|
if (!in.good())
|
|
|
|
// eof after all
|
|
|
|
break;
|
|
|
|
|
|
|
|
error = preview != "Preview:"
|
|
|
|
|| (type != "Tightpage" && type != "Snippet");
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (type == "Tightpage") {
|
|
|
|
int dummy;
|
|
|
|
in >> dummy >> tp_descent >> dummy >> tp_ascent;
|
|
|
|
|
|
|
|
error = !in.good();
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int dummy;
|
|
|
|
int snippet_id;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
in >> snippet_id >> ascent >> descent >> dummy;
|
|
|
|
|
|
|
|
error = !in.good() || ++snippet_counter != snippet_id;
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
double const a = ascent + tp_ascent;
|
|
|
|
double const d = descent - tp_descent;
|
|
|
|
|
2003-09-04 00:29:22 +00:00
|
|
|
if (!support::float_equal(a + d, 0, 0.1))
|
2002-08-06 12:55:28 +00:00
|
|
|
*it = a / (a + d);
|
|
|
|
|
|
|
|
if (++it == end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "setAscentFractions(" << metrics_file << ")\n"
|
|
|
|
<< "Error reading file!\n" << endl;
|
2002-07-09 09:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-08 18:25:30 +00:00
|
|
|
} // namespace anon
|