2002-07-05 21:24:15 +00:00
|
|
|
/*
|
|
|
|
* \file PreviewLoader.C
|
|
|
|
* Copyright 2002 the LyX Team
|
|
|
|
* Read the file COPYING
|
|
|
|
*
|
|
|
|
* \author Angus Leeming <a.leeming@ic.ac.uk>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "PreviewLoader.h"
|
|
|
|
#include "PreviewImage.h"
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "bufferparams.h"
|
|
|
|
#include "converter.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "lyxrc.h"
|
2002-07-08 18:25:30 +00:00
|
|
|
#include "lyxtextclasslist.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
#include "LColor.h"
|
|
|
|
|
|
|
|
#include "insets/inset.h"
|
|
|
|
|
|
|
|
#include "frontends/lyx_gui.h" // hexname
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/forkedcall.h"
|
2002-07-06 12:38:44 +00:00
|
|
|
#include "support/forkedcontr.h"
|
2002-07-05 21:24:15 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/signals/trackable.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
2002-07-09 09:30:54 +00:00
|
|
|
#include <list>
|
2002-07-05 21:24:15 +00:00
|
|
|
#include <map>
|
2002-07-09 09:30:54 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
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 18:25:30 +00:00
|
|
|
using std::getline;
|
2002-07-08 13:01:09 +00:00
|
|
|
using std::make_pair;
|
2002-07-05 21:24:15 +00:00
|
|
|
using std::setfill;
|
|
|
|
using std::setw;
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
using std::list;
|
2002-07-06 11:36:11 +00:00
|
|
|
using std::map;
|
2002-07-08 18:25:30 +00:00
|
|
|
using std::ifstream;
|
2002-07-06 11:36:11 +00:00
|
|
|
using std::ofstream;
|
|
|
|
using std::ostream;
|
|
|
|
using std::pair;
|
|
|
|
using std::vector;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
typedef pair<string, string> StrPair;
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
typedef list<string> PendingStore;
|
|
|
|
|
|
|
|
typedef vector<StrPair> InProgressStore;
|
|
|
|
|
|
|
|
|
|
|
|
double setFontScalingFactor(Buffer &);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
struct FindFirst {
|
|
|
|
FindFirst(string const & comp) : comp_(comp) {}
|
|
|
|
bool operator()(StrPair const & sp)
|
|
|
|
{
|
|
|
|
return sp.first < comp_;
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
PendingStore const & pending,
|
|
|
|
string const & to_format);
|
2002-07-08 13:01:09 +00:00
|
|
|
/// Remove any files left lying around and kill the forked process.
|
|
|
|
void stop() const;
|
|
|
|
|
|
|
|
///
|
|
|
|
pid_t pid;
|
|
|
|
///
|
|
|
|
string metrics_file;
|
|
|
|
/// Each item in the vector is a pair<snippet, image file name>.
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgressStore snippets;
|
2002-07-08 13:01:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-07-05 21:24:15 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
namespace grfx {
|
|
|
|
|
|
|
|
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-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.
|
|
|
|
void finishedGenerating(string const &, pid_t, int);
|
|
|
|
///
|
|
|
|
void dumpPreamble(ostream &) const;
|
|
|
|
///
|
2002-07-09 09:30:54 +00:00
|
|
|
void dumpData(ostream &, InProgressStore 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-09 09:30:54 +00:00
|
|
|
PendingStore 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-06 11:36:11 +00:00
|
|
|
typedef map<string, InProgress> InProgressMap;
|
2002-07-05 21:24:15 +00:00
|
|
|
///
|
|
|
|
InProgressMap in_progress_;
|
|
|
|
|
|
|
|
///
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::add(string const & latex_snippet)
|
|
|
|
{
|
|
|
|
pimpl_->add(latex_snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::remove(string const & latex_snippet)
|
|
|
|
{
|
|
|
|
pimpl_->remove(latex_snippet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::startLoading()
|
|
|
|
{
|
|
|
|
pimpl_->startLoading();
|
|
|
|
}
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
} // namespace grfx
|
|
|
|
|
|
|
|
|
|
|
|
// The details of the Impl
|
|
|
|
// =======================
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgress::InProgress(string const & filename_base,
|
|
|
|
PendingStore const & pending,
|
|
|
|
string const & to_format)
|
|
|
|
: pid(0),
|
|
|
|
metrics_file(filename_base + ".metrics"),
|
|
|
|
snippets(pending.size())
|
|
|
|
{
|
|
|
|
InProgressStore::iterator sit = snippets.begin();
|
|
|
|
PendingStore::const_iterator pit = pending.begin();
|
|
|
|
PendingStore::const_iterator pend = pending.end();
|
|
|
|
|
|
|
|
int counter = 1; // file numbers start at 1
|
|
|
|
for (; pit != pend; ++pit, ++sit, ++counter) {
|
|
|
|
ostringstream os;
|
|
|
|
os << filename_base
|
|
|
|
<< setfill('0') << setw(3) << counter
|
|
|
|
<< "." << to_format;
|
|
|
|
string const file = os.str().c_str();
|
|
|
|
|
|
|
|
*sit = make_pair(*pit, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
void InProgress::stop() const
|
|
|
|
{
|
|
|
|
if (pid)
|
|
|
|
ForkedcallsController::get().kill(pid, 0);
|
|
|
|
|
|
|
|
if (!metrics_file.empty())
|
|
|
|
lyx::unlink(metrics_file);
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgressStore::const_iterator vit = snippets.begin();
|
|
|
|
InProgressStore::const_iterator vend = snippets.end();
|
2002-07-08 13:01:09 +00:00
|
|
|
for (; vit != vend; ++vit) {
|
|
|
|
if (!vit->second.empty())
|
|
|
|
lyx::unlink(vit->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
namespace grfx {
|
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-07-09 09:30:54 +00:00
|
|
|
font_scaling_factor_ = setFontScalingFactor(const_cast<Buffer &>(b));
|
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-08 13:01:09 +00:00
|
|
|
InProgressMap::iterator ipit = in_progress_.begin();
|
|
|
|
InProgressMap::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-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-09 09:30:54 +00:00
|
|
|
PendingStore::const_iterator pit = pending_.begin();
|
|
|
|
PendingStore::const_iterator pend = pending_.end();
|
|
|
|
pit = find(pit, pend, latex_snippet);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
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-06 11:36:11 +00:00
|
|
|
InProgressMap::const_iterator ipit = in_progress_.begin();
|
|
|
|
InProgressMap::const_iterator ipend = in_progress_.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
for (; ipit != ipend; ++ipit) {
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgressStore const & snippets = ipit->second.snippets;
|
|
|
|
InProgressStore::const_iterator sit = snippets.begin();
|
|
|
|
InProgressStore::const_iterator send = snippets.end();
|
|
|
|
sit = find_if(sit, send, FindFirst(latex_snippet));
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
if (sit != send)
|
2002-07-08 13:01:09 +00:00
|
|
|
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;
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
pending_.push_back(latex_snippet);
|
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-09 09:30:54 +00:00
|
|
|
PendingStore::iterator pit = pending_.begin();
|
|
|
|
PendingStore::iterator pend = pending_.end();
|
|
|
|
pit = find(pit, pend, latex_snippet);
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
if (pit != pend) {
|
|
|
|
PendingStore::iterator first = pit++;
|
|
|
|
pending_.erase(first, pit);
|
|
|
|
}
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
InProgressMap::iterator ipit = in_progress_.begin();
|
|
|
|
InProgressMap::iterator ipend = in_progress_.end();
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
while (ipit != ipend) {
|
|
|
|
InProgressMap::iterator curr = ipit;
|
|
|
|
++ipit;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgressStore & snippets = curr->second.snippets;
|
|
|
|
InProgressStore::iterator sit = snippets.begin();
|
|
|
|
InProgressStore::iterator send = snippets.end();
|
|
|
|
sit = find_if(sit, send, FindFirst(latex_snippet));
|
2002-07-08 13:01:09 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
if (sit != send)
|
|
|
|
snippets.erase(sit, sit+1);
|
2002-07-05 21:24:15 +00:00
|
|
|
|
2002-07-06 11:36:11 +00:00
|
|
|
if (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
|
|
|
|
|
|
|
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
|
|
|
|
string const filename_base(unique_filename(buffer_.tmppath));
|
|
|
|
|
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
|
|
|
|
|
|
|
// clear pending_, so we're ready to start afresh.
|
|
|
|
pending_.clear();
|
2002-07-06 11:36:11 +00:00
|
|
|
|
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());
|
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;
|
|
|
|
cs << pconverter_->command << " " << latexfile << " "
|
2002-07-09 09:30:54 +00:00
|
|
|
<< font_scaling_factor_;
|
2002-07-05 21:24:15 +00:00
|
|
|
|
|
|
|
string const command = cs.str().c_str();
|
|
|
|
|
|
|
|
// Initiate the conversion from LaTeX to bitmap images files.
|
|
|
|
Forkedcall::SignalTypePtr convert_ptr;
|
|
|
|
convert_ptr.reset(new Forkedcall::SignalType);
|
|
|
|
|
|
|
|
convert_ptr->connect(
|
|
|
|
boost::bind(&Impl::finishedGenerating, this, _1, _2, _3));
|
|
|
|
|
|
|
|
Forkedcall call;
|
|
|
|
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-07-06 11:36:11 +00:00
|
|
|
in_progress_[command] = inprogress;
|
2002-07-05 21:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PreviewLoader::Impl::finishedGenerating(string const & command,
|
|
|
|
pid_t /* pid */, int retval)
|
|
|
|
{
|
|
|
|
string const status = retval > 0 ? "failed" : "succeeded";
|
|
|
|
lyxerr[Debug::GRAPHICS] << "PreviewLoader::finishedInProgress("
|
|
|
|
<< retval << "): processing " << status
|
|
|
|
<< " for " << command << endl;
|
|
|
|
if (retval > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
InProgressMap::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
|
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-09 09:30:54 +00:00
|
|
|
InProgressStore::const_iterator it = git->second.snippets.begin();
|
|
|
|
InProgressStore::const_iterator end = git->second.snippets.end();
|
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;
|
|
|
|
|
|
|
|
ptr->startLoading();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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, string(), true, false, true);
|
|
|
|
|
|
|
|
// Loop over the insets in the buffer and dump all the math-macros.
|
|
|
|
Buffer::inset_iterator it = buffer_.inset_const_iterator_begin();
|
|
|
|
Buffer::inset_iterator end = buffer_.inset_const_iterator_end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if ((*it)->lyxCode() == Inset::MATHMACRO_CODE) {
|
|
|
|
(*it)->latex(&buffer_, os, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-08 13:01:09 +00:00
|
|
|
<< "\\usepackage[active,delayed,dvips,tightpage,showlabels]{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"
|
|
|
|
<< "<" << fg << bg << ">{255 div}forall setrgbcolor\n"
|
|
|
|
<< "clippath fill setrgbcolor}bind def end}}}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-08 13:01:09 +00:00
|
|
|
void PreviewLoader::Impl::dumpData(ostream & os,
|
2002-07-09 09:30:54 +00:00
|
|
|
InProgressStore 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-09 09:30:54 +00:00
|
|
|
InProgressStore::const_iterator it = vec.begin();
|
|
|
|
InProgressStore::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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
} // namespace grfx
|
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";
|
|
|
|
return AddName(bufferpath, filename);
|
2002-07-08 18:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
Converter const * setConverter()
|
|
|
|
{
|
|
|
|
Converter const * converter = 0;
|
2002-07-08 18:25:30 +00:00
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double setFontScalingFactor(Buffer & buffer)
|
2002-07-08 18:25:30 +00:00
|
|
|
{
|
|
|
|
double scale_factor = 0.01 * lyxrc.dpi * lyxrc.zoom *
|
2002-07-10 09:09:37 +00:00
|
|
|
lyxrc.preview_scale_factor;
|
2002-07-08 18:25:30 +00:00
|
|
|
|
|
|
|
// Has the font size been set explicitly?
|
|
|
|
string const & fontsize = buffer.params.fontsize;
|
|
|
|
lyxerr[Debug::GRAPHICS] << "PreviewLoader::scaleToFitLyXView()\n"
|
|
|
|
<< "font size is " << fontsize << endl;
|
|
|
|
|
|
|
|
if (isStrUnsignedInt(fontsize))
|
|
|
|
return 10.0 * scale_factor / strToDbl(fontsize);
|
|
|
|
|
|
|
|
// No. We must extract it from the LaTeX class file.
|
|
|
|
LyXTextClass const & tclass = textclasslist[buffer.params.textclass];
|
|
|
|
string const textclass(tclass.latexname() + ".cls");
|
|
|
|
string const classfile(findtexfile(textclass, "cls"));
|
|
|
|
|
|
|
|
lyxerr[Debug::GRAPHICS] << "text class is " << textclass << '\n'
|
|
|
|
<< "class file is " << classfile << endl;
|
|
|
|
|
|
|
|
ifstream ifs(classfile.c_str());
|
|
|
|
if (!ifs.good()) {
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Unable to open class file!" << endl;
|
|
|
|
return scale_factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
string str;
|
|
|
|
double scaling = scale_factor;
|
|
|
|
|
|
|
|
while (ifs.good()) {
|
|
|
|
getline(ifs, str);
|
|
|
|
// To get the default font size, look for a line like
|
|
|
|
// "\ExecuteOptions{letterpaper,10pt,oneside,onecolumn,final}"
|
2002-07-09 09:30:54 +00:00
|
|
|
if (!prefixIs(frontStrip(str), "\\ExecuteOptions"))
|
2002-07-08 18:25:30 +00:00
|
|
|
continue;
|
|
|
|
|
2002-07-10 09:09:37 +00:00
|
|
|
// str contains just the options of \ExecuteOptions
|
|
|
|
string const tmp = split(str, '{');
|
|
|
|
split(tmp, str, '}');
|
|
|
|
|
2002-07-08 18:25:30 +00:00
|
|
|
int count = 0;
|
|
|
|
string tok = token(str, ',', count++);
|
|
|
|
while (!isValidLength(tok) && !tok.empty())
|
|
|
|
tok = token(str, ',', count++);
|
|
|
|
|
|
|
|
if (!tok.empty()) {
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "Extracted default font size from "
|
|
|
|
"LaTeX class file successfully!" << endl;
|
|
|
|
LyXLength fsize(tok);
|
|
|
|
scaling *= 10.0 / fsize.value();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scaling;
|
|
|
|
}
|
|
|
|
|
2002-07-09 09:30:54 +00:00
|
|
|
|
|
|
|
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 ifs(metrics_file.c_str());
|
|
|
|
if (!ifs.good()) {
|
|
|
|
lyxerr[Debug::GRAPHICS] << "setAscentFractions("
|
|
|
|
<< metrics_file << ")\n"
|
|
|
|
<< "Unable to open file!"
|
|
|
|
<< endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
string page;
|
|
|
|
string page_id;
|
|
|
|
int dummy;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
|
|
|
|
ifs >> page >> page_id >> dummy >> dummy >> dummy >> dummy
|
|
|
|
>> ascent >> descent >> dummy;
|
|
|
|
|
|
|
|
if (!ifs.good() ||
|
|
|
|
page != "%%Page" ||
|
|
|
|
!isStrUnsignedInt(strip(page_id, ':'))) {
|
|
|
|
lyxerr[Debug::GRAPHICS] << "setAscentFractions("
|
|
|
|
<< metrics_file << ")\n"
|
|
|
|
<< "Error reading file!"
|
|
|
|
<< endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*it = ascent / (ascent + descent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-08 18:25:30 +00:00
|
|
|
} // namespace anon
|