Further clean ups.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4561 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2002-07-09 09:30:54 +00:00
parent 736d068866
commit 0746000cb7
6 changed files with 195 additions and 307 deletions

View File

@ -1,3 +1,12 @@
2002-07-08 Angus Leeming <leeming@lyx.org>
* PreviewLoader.C: Further refactoring of the code. Should now be
pretty clean.
* PreviewMetrics.[Ch]: removed. Had a v. short shlf life and is now
past it's sell-by date. The code to read the metrics file is now found
as a helper function in PreviewLoader.C.
2002-07-08 Herbert Voss <voss@lyx.org> 2002-07-08 Herbert Voss <voss@lyx.org>
* PreviewLoader.C: use of preview_size_factor to get the right * PreviewLoader.C: use of preview_size_factor to get the right

View File

@ -28,8 +28,6 @@ libgraphics_la_SOURCES = \
PreviewImage.C \ PreviewImage.C \
PreviewLoader.h \ PreviewLoader.h \
PreviewLoader.C \ PreviewLoader.C \
PreviewMetrics.h \
PreviewMetrics.C \
Previews.h \ Previews.h \
Previews.C Previews.C

View File

@ -92,7 +92,8 @@ int PreviewImage::descent() const
if (!image) if (!image)
return 0; return 0;
return int((1.0 - pimpl_->ascent_frac_) * double(image->getHeight())); // Avoids rounding errors.
return image->getHeight() - ascent();
} }

View File

@ -14,7 +14,6 @@
#include "PreviewLoader.h" #include "PreviewLoader.h"
#include "PreviewImage.h" #include "PreviewImage.h"
#include "PreviewMetrics.h"
#include "buffer.h" #include "buffer.h"
#include "bufferparams.h" #include "bufferparams.h"
@ -39,17 +38,21 @@
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <list>
#include <map> #include <map>
#include <utility>
#include <vector>
using std::endl; using std::endl;
using std::find; using std::find;
using std::fill;
using std::find_if; using std::find_if;
using std::getline; using std::getline;
using std::make_pair; using std::make_pair;
using std::setfill; using std::setfill;
using std::setw; using std::setw;
using std::sort;
using std::list;
using std::map; using std::map;
using std::ifstream; using std::ifstream;
using std::ofstream; using std::ofstream;
@ -59,16 +62,21 @@ using std::vector;
namespace { namespace {
double getScalingFactor(Buffer &);
typedef pair<string, string> StrPair; typedef pair<string, string> StrPair;
struct CompSecond { typedef list<string> PendingStore;
bool operator()(StrPair const & lhs, StrPair const & rhs)
{ typedef vector<StrPair> InProgressStore;
return lhs.second < rhs.second;
}
}; double setFontScalingFactor(Buffer &);
string const unique_filename(string const bufferpath);
Converter const * setConverter();
void setAscentFractions(vector<double> & ascent_fractions,
string const & metrics_file);
struct FindFirst { struct FindFirst {
FindFirst(string const & comp) : comp_(comp) {} FindFirst(string const & comp) : comp_(comp) {}
@ -81,22 +89,14 @@ private:
}; };
string const unique_filename(string const bufferpath)
{
static int theCounter = 0;
string const filename = tostr(theCounter++) + "lyxpreview";
return AddName(bufferpath, filename);
}
/// Store info on a currently executing, forked process. /// Store info on a currently executing, forked process.
struct InProgress { struct InProgress {
/// ///
InProgress() : pid(0) {} InProgress() : pid(0) {}
/// ///
InProgress(string const & f, vector<StrPair> const & s) InProgress(string const & filename_base,
: pid(0), metrics_file(f), snippets(s) PendingStore const & pending,
{} string const & to_format);
/// Remove any files left lying around and kill the forked process. /// Remove any files left lying around and kill the forked process.
void stop() const; void stop() const;
@ -105,7 +105,7 @@ struct InProgress {
/// ///
string metrics_file; string metrics_file;
/// Each item in the vector is a pair<snippet, image file name>. /// Each item in the vector is a pair<snippet, image file name>.
vector<StrPair> snippets; InProgressStore snippets;
}; };
@ -131,19 +131,12 @@ struct PreviewLoader::Impl : public boost::signals::trackable {
void startLoading(); void startLoading();
private: private:
///
static bool haveConverter();
/// We don't own this
static Converter const * pconverter_;
/// Called by the Forkedcall process that generated the bitmap files. /// Called by the Forkedcall process that generated the bitmap files.
void finishedGenerating(string const &, pid_t, int); void finishedGenerating(string const &, pid_t, int);
/// ///
void dumpPreamble(ostream &) const; void dumpPreamble(ostream &) const;
/// ///
void dumpData(ostream &, vector<StrPair> const &) const; void dumpData(ostream &, InProgressStore const &) const;
///
double fontScalingFactor() const;
/** cache_ allows easy retrieval of already-generated images /** cache_ allows easy retrieval of already-generated images
* using the LaTeX snippet as the identifier. * using the LaTeX snippet as the identifier.
@ -157,7 +150,7 @@ private:
/** pending_ stores the LaTeX snippets in anticipation of them being /** pending_ stores the LaTeX snippets in anticipation of them being
* sent to the converter. * sent to the converter.
*/ */
vector<string> pending_; PendingStore pending_;
/** in_progress_ stores all forked processes so that we can proceed /** in_progress_ stores all forked processes so that we can proceed
* thereafter. * thereafter.
@ -172,7 +165,10 @@ private:
/// ///
Buffer const & buffer_; Buffer const & buffer_;
/// ///
mutable double font_scaling_factor_; double font_scaling_factor_;
/// We don't own this
static Converter const * pconverter_;
}; };
@ -227,6 +223,30 @@ void PreviewLoader::startLoading()
namespace { namespace {
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);
}
}
void InProgress::stop() const void InProgress::stop() const
{ {
if (pid) if (pid)
@ -235,8 +255,8 @@ void InProgress::stop() const
if (!metrics_file.empty()) if (!metrics_file.empty())
lyx::unlink(metrics_file); lyx::unlink(metrics_file);
vector<StrPair>::const_iterator vit = snippets.begin(); InProgressStore::const_iterator vit = snippets.begin();
vector<StrPair>::const_iterator vend = snippets.end(); InProgressStore::const_iterator vend = snippets.end();
for (; vit != vend; ++vit) { for (; vit != vend; ++vit) {
if (!vit->second.empty()) if (!vit->second.empty())
lyx::unlink(vit->second); lyx::unlink(vit->second);
@ -248,46 +268,17 @@ void InProgress::stop() const
namespace grfx { namespace grfx {
bool PreviewLoader::Impl::haveConverter()
{
if (pconverter_)
return true;
string const from = "lyxpreview";
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) {
pconverter_ = ptr;
break;
}
}
if (pconverter_)
return true;
static bool first = true;
if (first) {
first = false;
lyxerr << "PreviewLoader::startLoading()\n"
<< "No converter from \"lyxpreview\" format has been "
"defined."
<< endl;
}
return false;
}
PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b) PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
: parent_(p), buffer_(b), font_scaling_factor_(0.0) : parent_(p), buffer_(b), font_scaling_factor_(0.0)
{} {
font_scaling_factor_ = setFontScalingFactor(const_cast<Buffer &>(b));
lyxerr[Debug::GRAPHICS] << "The font scaling factor is "
<< font_scaling_factor_ << endl;
if (!pconverter_)
pconverter_ = setConverter();
}
PreviewLoader::Impl::~Impl() PreviewLoader::Impl::~Impl()
@ -316,23 +307,23 @@ PreviewLoader::Impl::status(string const & latex_snippet) const
if (cit != cache_.end()) if (cit != cache_.end())
return Ready; return Ready;
vector<string>::const_iterator vit = pending_.begin(); PendingStore::const_iterator pit = pending_.begin();
vector<string>::const_iterator vend = pending_.end(); PendingStore::const_iterator pend = pending_.end();
vit = find(vit, vend, latex_snippet); pit = find(pit, pend, latex_snippet);
if (vit != vend) if (pit != pend)
return InQueue; return InQueue;
InProgressMap::const_iterator ipit = in_progress_.begin(); InProgressMap::const_iterator ipit = in_progress_.begin();
InProgressMap::const_iterator ipend = in_progress_.end(); InProgressMap::const_iterator ipend = in_progress_.end();
for (; ipit != ipend; ++ipit) { for (; ipit != ipend; ++ipit) {
vector<StrPair> const & snippets = ipit->second.snippets; InProgressStore const & snippets = ipit->second.snippets;
vector<StrPair>::const_iterator vit = snippets.begin(); InProgressStore::const_iterator sit = snippets.begin();
vector<StrPair>::const_iterator vend = snippets.end(); InProgressStore::const_iterator send = snippets.end();
vit = find_if(vit, vend, FindFirst(latex_snippet)); sit = find_if(sit, send, FindFirst(latex_snippet));
if (vit != vend) if (sit != send)
return Processing; return Processing;
} }
@ -342,14 +333,10 @@ PreviewLoader::Impl::status(string const & latex_snippet) const
void PreviewLoader::Impl::add(string const & latex_snippet) void PreviewLoader::Impl::add(string const & latex_snippet)
{ {
if (!haveConverter()) if (!pconverter_ || status(latex_snippet) != NotFound)
return;
if (status(latex_snippet) != NotFound)
return; return;
pending_.push_back(latex_snippet); pending_.push_back(latex_snippet);
sort(pending_.begin(), pending_.end());
} }
@ -359,12 +346,14 @@ void PreviewLoader::Impl::remove(string const & latex_snippet)
if (cit != cache_.end()) if (cit != cache_.end())
cache_.erase(cit); cache_.erase(cit);
vector<string>::iterator vit = pending_.begin(); PendingStore::iterator pit = pending_.begin();
vector<string>::iterator vend = pending_.end(); PendingStore::iterator pend = pending_.end();
vit = find(vit, vend, latex_snippet); pit = find(pit, pend, latex_snippet);
if (vit != vend) if (pit != pend) {
pending_.erase(vit, vit+1); PendingStore::iterator first = pit++;
pending_.erase(first, pit);
}
InProgressMap::iterator ipit = in_progress_.begin(); InProgressMap::iterator ipit = in_progress_.begin();
InProgressMap::iterator ipend = in_progress_.end(); InProgressMap::iterator ipend = in_progress_.end();
@ -373,13 +362,13 @@ void PreviewLoader::Impl::remove(string const & latex_snippet)
InProgressMap::iterator curr = ipit; InProgressMap::iterator curr = ipit;
++ipit; ++ipit;
vector<StrPair> & snippets = curr->second.snippets; InProgressStore & snippets = curr->second.snippets;
vector<StrPair>::iterator vit = snippets.begin(); InProgressStore::iterator sit = snippets.begin();
vector<StrPair>::iterator vend = snippets.end(); InProgressStore::iterator send = snippets.end();
vit = find_if(vit, vend, FindFirst(latex_snippet)); sit = find_if(sit, send, FindFirst(latex_snippet));
if (vit != vend) if (sit != send)
snippets.erase(vit, vit+1); snippets.erase(sit, sit+1);
if (snippets.empty()) if (snippets.empty())
in_progress_.erase(curr); in_progress_.erase(curr);
@ -389,10 +378,7 @@ void PreviewLoader::Impl::remove(string const & latex_snippet)
void PreviewLoader::Impl::startLoading() void PreviewLoader::Impl::startLoading()
{ {
if (pending_.empty()) if (pending_.empty() || !pconverter_)
return;
if (!haveConverter())
return; return;
lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()" << endl; lyxerr[Debug::GRAPHICS] << "PreviewLoader::startLoading()" << endl;
@ -402,24 +388,7 @@ void PreviewLoader::Impl::startLoading()
// Create an InProgress instance to place in the map of all // Create an InProgress instance to place in the map of all
// such processes if it starts correctly. // such processes if it starts correctly.
vector<StrPair> snippets(pending_.size()); InProgress inprogress(filename_base, pending_, pconverter_->to);
vector<StrPair>::iterator sit = snippets.begin();
vector<string>::const_iterator pit = pending_.begin();
vector<string>::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
<< "." << pconverter_->to;
string const file = os.str().c_str();
*sit = make_pair(*pit, file);
}
string const metrics_file = filename_base + ".metrics";
InProgress inprogress(metrics_file, snippets);
// clear pending_, so we're ready to start afresh. // clear pending_, so we're ready to start afresh.
pending_.clear(); pending_.clear();
@ -435,12 +404,9 @@ void PreviewLoader::Impl::startLoading()
of.close(); of.close();
// The conversion command. // The conversion command.
double const scaling_factor = fontScalingFactor();
lyxerr[Debug::GRAPHICS] << "The font scaling factor is "
<< scaling_factor << endl;
ostringstream cs; ostringstream cs;
cs << pconverter_->command << " " << latexfile << " " cs << pconverter_->command << " " << latexfile << " "
<< scaling_factor; << font_scaling_factor_;
string const command = cs.str().c_str(); string const command = cs.str().c_str();
@ -486,35 +452,21 @@ void PreviewLoader::Impl::finishedGenerating(string const & command,
} }
// Read the metrics file, if it exists // Read the metrics file, if it exists
PreviewMetrics metrics_file(git->second.metrics_file); vector<double> ascent_fractions(git->second.snippets.size());
setAscentFractions(ascent_fractions, git->second.metrics_file);
// Add these newly generated bitmap files to the cache and // Add these newly generated bitmap files to the cache and
// start loading them into LyX. // start loading them into LyX.
vector<StrPair>::const_iterator it = git->second.snippets.begin(); InProgressStore::const_iterator it = git->second.snippets.begin();
vector<StrPair>::const_iterator end = git->second.snippets.end(); InProgressStore::const_iterator end = git->second.snippets.end();
int metrics_counter = 0; int metrics_counter = 0;
for (; it != end; ++it) { for (; it != end; ++it, ++metrics_counter) {
string const & snip = it->first; string const & snip = it->first;
// Paranoia check
Cache::const_iterator chk = cache_.find(snip);
if (chk != cache_.end())
continue;
// Mental note (Angus, 4 July 2002, having just found out the
// hard way :-().
// We /must/ first add to the cache and then start the
// image loading process.
// If not, then outside functions can be called before by the
// image loader before the PreviewImage/map is properly
// constucted.
// This can lead to all sorts of horribleness if such a
// function attempts to access the cache's internals.
string const & file = it->second; string const & file = it->second;
double af = metrics_file.ascent_fraction(metrics_counter++); double af = ascent_fractions[metrics_counter];
PreviewImagePtr ptr(new PreviewImage(parent_, snip, file, af));
PreviewImagePtr ptr(new PreviewImage(parent_, snip, file, af));
cache_[snip] = ptr; cache_[snip] = ptr;
ptr->startLoading(); ptr->startLoading();
@ -564,13 +516,13 @@ void PreviewLoader::Impl::dumpPreamble(ostream & os) const
void PreviewLoader::Impl::dumpData(ostream & os, void PreviewLoader::Impl::dumpData(ostream & os,
vector<StrPair> const & vec) const InProgressStore const & vec) const
{ {
if (vec.empty()) if (vec.empty())
return; return;
vector<StrPair>::const_iterator it = vec.begin(); InProgressStore::const_iterator it = vec.begin();
vector<StrPair>::const_iterator end = vec.end(); InProgressStore::const_iterator end = vec.end();
for (; it != end; ++it) { for (; it != end; ++it) {
os << "\\begin{preview}\n" os << "\\begin{preview}\n"
@ -579,25 +531,52 @@ void PreviewLoader::Impl::dumpData(ostream & os,
} }
} }
double PreviewLoader::Impl::fontScalingFactor() const
{
static double const lyxrc_preview_scale_factor = 0.9;
if (font_scaling_factor_ > 0.01)
return font_scaling_factor_;
font_scaling_factor_ = getScalingFactor(const_cast<Buffer &>(buffer_));
return font_scaling_factor_;
}
} // namespace grfx } // namespace grfx
namespace { namespace {
double getScalingFactor(Buffer & buffer) string const unique_filename(string const bufferpath)
{
static int theCounter = 0;
string const filename = tostr(theCounter++) + "lyxpreview";
return AddName(bufferpath, filename);
}
Converter const * setConverter()
{
Converter const * converter = 0;
string const from = "lyxpreview";
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;
}
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)
{ {
static double const lyxrc_preview_scale_factor = 0.9; static double const lyxrc_preview_scale_factor = 0.9;
double scale_factor = 0.01 * lyxrc.dpi * lyxrc.zoom * double scale_factor = 0.01 * lyxrc.dpi * lyxrc.zoom *
@ -632,7 +611,7 @@ double getScalingFactor(Buffer & buffer)
getline(ifs, str); getline(ifs, str);
// To get the default font size, look for a line like // To get the default font size, look for a line like
// "\ExecuteOptions{letterpaper,10pt,oneside,onecolumn,final}" // "\ExecuteOptions{letterpaper,10pt,oneside,onecolumn,final}"
if (!prefixIs(str, "\\ExecuteOptions")) if (!prefixIs(frontStrip(str), "\\ExecuteOptions"))
continue; continue;
str = split(str, '{'); str = split(str, '{');
@ -654,4 +633,47 @@ double getScalingFactor(Buffer & buffer)
return scaling; return scaling;
} }
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);
}
}
} // namespace anon } // namespace anon

View File

@ -1,99 +0,0 @@
/*
* \file PreviewMetrics.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 "PreviewMetrics.h"
#include "debug.h"
#include "lyxlex.h"
#include "support/lyxlib.h"
namespace grfx {
namespace {
keyword_item MetricsTags[] = {
{ "%%Page", 1 }
};
} // namespace anon
PreviewMetrics::PreviewMetrics(string const & file)
{
LyXLex lex(MetricsTags, 1);
lex.setFile(file);
if (!lex.isOK()) {
lyxerr[Debug::GRAPHICS]
<< "PreviewMetricsFile(" << file << ")\n"
<< "Unable to open file." << std::endl;
return;
}
int line = 0;
while (lex.isOK()) {
int le = lex.lex();
switch (le) {
case LyXLex::LEX_UNDEF:
lex.printError("Unknown tag `$$Token'");
continue;
case LyXLex::LEX_FEOF:
continue;
default: break;
}
if (le != 1 || !lex.next())
continue;
int store[7];
for (int i = 0; i < 7; ++i) {
if (lex.next())
store[i] = lex.getInteger();
if (lex.isOK())
continue;
lyxerr[Debug::GRAPHICS]
<< "PreviewMetricsFile(" << file << ")\n"
<< "Error reading file." << std::endl;
break;
}
if (!lex.isOK())
break;
// These 7 numbers are
// bb_ll_x bb_ll_y bb_ur_x bb_ur_y ht dp wd
// We are interested only in the ratio ht:dp
double const a = double(store[4]);
double const d = double(store[5]);
double const af = a / (a + d);
store_.push_back(af);
}
lyx::unlink(file);
}
double PreviewMetrics::ascent_fraction(size_type counter)
{
if (store_.empty() || counter >= store_.size())
return 0.5;
return store_[counter];
}
} // namespace grfx

View File

@ -1,43 +0,0 @@
// -*- C++ -*-
/**
* \file PreviewMetrics.h
* Copyright 2002 the LyX Team
* Read the file COPYING
*
* \author Angus Leeming <a.leeming@ic.ac.uk>
*/
#ifndef PREVIEWMETRICS_H
#define PREVIEWMETRICS_H
#ifdef __GNUG__
#pragma interface
#endif
#include "LString.h"
#include <boost/utility.hpp>
#include <vector>
namespace grfx {
class PreviewMetrics : boost::noncopyable {
public:
///
typedef std::vector<PreviewMetrics>::size_type size_type;
/// Reads in file and then deletes it.
PreviewMetrics(string const & file);
/** Return the data from the metrics file if found,
* or 0.5
*/
double ascent_fraction(size_type);
private:
///
std::vector<double> store_;
};
} // namespace grfx
#endif // PREVIEWMETRICS_H