Activate the code using preview.sty 0.73. Ditch support for older versions.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5194 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2002-09-03 13:52:37 +00:00
parent 6bfcc23b3f
commit 1ccc528e28
4 changed files with 87 additions and 216 deletions

View File

@ -1,3 +1,8 @@
2002-09-03 Angus Leeming <leeming@lyx.org>
* scripts/lyxpreview2ppm.sh: re-written to make use of the "lyx"
option parsed by preview.sty v 0.73.
2002-08-31 Dekel Tsur <dekelts@tau.ac.il>
* lyx2lyx/lyxconvert_218.py (update_tabular): Work with nested tabulars

View File

@ -1,30 +1,32 @@
#!/bin/sh
#! /bin/sh
#
# \file lyxpreview2ppm.sh
# Copyright 2002 the LyX Team
# Read the file COPYING
#
# \author Angus Leeming, leeming@lyx.org
#
# with much help from David Kastrup, david.kastrup@t-online.de.
# The sed script was created with advice from Praveen D V, praveend@sasken.com
# and the sed users' list, sed-users@yahoogroups.com.
#
# This script takes a LaTeX file and generates PPM files, one per page.
# The idea is to use it with preview.sty to create small bitmap previews of
# The idea is to use it with preview.sty from the preview-latex project
# (http://preview-latex.sourceforge.net/) to create small bitmap previews of
# things like math equations.
# The script takes two arguments, the name of the file to be converted and
# the resolution of the generated image, to be passed to gs.
if [ $# -ne 2 ]; then
exit 1
fi
# preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
# A couple of helper functions
# This script, lyxpreview2ppm.sh, takes two arguments, the name of the file
# to be converted and a scale factor, used to ascertain the resolution of the
# generated image which is then passed to gs.
# If successful it will leave in dir ${DIR} a number of image files
# ${BASE}[0-9]\{3\}.ppm and a file ${BASE}.metrics containing info needed by
# LyX to position the images correctly on the screen. All other files ${BASE}*
# will be deleted.
# Three helper functions.
FIND_IT () {
which ${EXECUTABLE} > /dev/null
STATUS=$?
if [ ${STATUS} -ne 0 ]; then
if [ $? -ne 0 ]; then
echo "Unable to find \"${EXECUTABLE}\". Please install."
exit 1
fi
@ -37,17 +39,29 @@ BAIL_OUT () {
exit 1
}
REQUIRED_VERSION () {
echo "We require preview.sty version 0.73 or newer. You're using"
grep 'Package: preview' ${LOGFILE}
}
# Preliminary check
if [ $# -ne 2 ]; then
exit 1
fi
# We use latex, dvips and gs, so check that they're all there.
EXECUTABLE=latex; FIND_IT
EXECUTABLE=dvips; FIND_IT
EXECUTABLE=gs; FIND_IT
# Initialise some variables.
TEXFILE=`basename $1`
RESOLUTION=$2
DIR=`dirname $1`
BASE=`basename $1 .tex`
SCALEFACTOR=$2
TEXFILE=${BASE}.tex
LOGFILE=${BASE}.log
DVIFILE=${BASE}.dvi
PSFILE=${BASE}.ps
METRICSFILE=${BASE}.metrics
@ -55,35 +69,54 @@ METRICSFILE=${BASE}.metrics
# LaTeX -> DVI.
cd ${DIR}
latex ${TEXFILE}
STATUS=$?
if [ ${STATUS} -ne 0 ]; then
# LaTeX failed.
# preview.sty has known problems with the showlabels option,
# so remove it and try again.
# This "fix" should be removed once preview-latex 0.73 is released.
sed -e '/^\\usepackage/,/{preview}$/s/,showlabels//' \
< ${TEXFILE} > .${TEXFILE}
cmp -s ${TEXFILE} .${TEXFILE}
STATUS=$?
if [ ${STATUS} -eq 0 ]; then
rm -f .${TEXFILE}
echo "Failed: latex ${TEXFILE}"
BAIL_OUT
fi
mv -f .${TEXFILE} ${TEXFILE}
latex ${TEXFILE}
STATUS=$?
if [ ${STATUS} -ne 0 ]; then
echo "Failed: latex ${TEXFILE}"
BAIL_OUT
fi
if [ $? -ne 0 ]; then
echo "Failed: latex ${TEXFILE}"
BAIL_OUT
fi
# Parse ${LOGFILE} to obtain bounding box info to output to ${METRICSFILE}.
# This extracts lines starting "Preview: Tightpage" and "Preview: Snippet".
grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
if [ $? -ne 0 ]; then
echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
REQUIRED_VERSION
BAIL_OUT
fi
# Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
# 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
# Use grep for speed and because it gives an error if the line is not found.
LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
if [ $? -ne 0 ]; then
echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
REQUIRED_VERSION
BAIL_OUT
fi
# Use "" quotes in the echo to preserve newlines (technically IFS separators).
# The sed script strips out everything that won't form a decimal number from the
# line. It bails out after the first match has been made in case there are
# multiple lines "Preview: Fontsize". (There shouldn't be.)
LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
# 2. Extract magnification from a line like "Preview: Magnification 2074"
# If no such line found, default to MAGNIFICATION=1000.
LINE=`grep 'Preview: Magnification' ${LOGFILE}`
if [ $? -ne 0 ]; then
MAGNIFICATION=1000
else
# The sed script strips out everything that won't form an /integer/.
MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
fi
# 3. Compute resolution.
# "bc" allows floating-point arithmetic, unlike "expr" or "dc".
RESOLUTION=`echo "scale=2; \
${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
| bc`
# DVI -> PostScript
dvips -o ${PSFILE} ${DVIFILE}
STATUS=$?
if [ ${STATUS} -ne 0 ]; then
if [ $? -ne 0 ]; then
echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
BAIL_OUT
fi
@ -100,67 +133,12 @@ gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
-dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
${PSFILE}
STATUS=$?
if [ ${STATUS} -ne 0 ]; then
if [ $? -ne 0 ]; then
echo "Failed: gs ${PSFILE}"
BAIL_OUT
fi
# Attempt to generate a file ${METRICSFILE} that contains only the tightpage
# bounding box info, extract from ${PSFILE}
# 1. Create a file containing the sed instructions
SEDFILE=${BASE}.sed
cat - > ${SEDFILE} <<EOF
# Delete everything that's enclosed between %%BeginDocument and %%EndDocument
/^\%\%BeginDocument/,/^\%\%EndDocument/d
# Extract the tightpage bounding box info.
# Given this snippet:
# %%Page: 1 1
# 1 0 bop
# -32890 -32890 32890 32890 492688 0 744653
# The sed command gives this:
# %%Page 1: -32890 -32890 32890 32890 492688 0 744653
/^\%\%Page:/{
s/\: \(.*\) .*$/ \1: /;N;N
s/\n[^\n]*\n//p
}
# Delete everything (so only the stuff that's printed, above, goes into the
# metrics file).
d
EOF
# 2. Run sed!
sed -f ${SEDFILE} < ${PSFILE} > ${METRICSFILE}
rm -f ${SEDFILE}
# The ppm files have spurious (?! say some !) white space on the left and right
# sides. If you want this removed set REMOVE_WS=1.
REMOVE_WS=0
which pnmcrop > /dev/null
STATUS=$?
if [ ${STATUS} -ne 0 ]; then
REMOVE_WS=0
fi
if [ ${REMOVE_WS} -eq 1 ]; then
TMP=.${BASE}.ppm
for FILE in `ls ${BASE}???.ppm`
do
pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
STATUS=$?
if [ ${STATUS} -eq 0 ]; then
mv -f ${TMP} ${FILE}
fi
done
fi
# All was successful, so remove everything except the ppm files and the
# metrics file.
# All has been successful, so remove everything except the bitmap files
# and the metrics file.
FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{3\}.ppm/d"`
rm -f ${FILES}

View File

@ -1,3 +1,7 @@
2002-09-03 Angus Leeming <leeming@lyx.org>
* PreviewLoader.C: activate #ifdef USING_NEW_PREVIEW_STY code.
2002-09-03 Angus Leeming <leeming@lyx.org>
* GraphicsConverter.C (Impl c-tor): remove lyxpreview2xpm cruft.

View File

@ -12,17 +12,10 @@
#pragma implementation
#endif
// Set to 1 if using preview.sty >= 0.73 and a version of lyxpreview2ppm.sh
// that extracts the metrics info from the latex log file.
#define USING_NEW_PREVIEW_STY 0
#include "PreviewLoader.h"
#include "PreviewImage.h"
#include "buffer.h"
#if !USING_NEW_PREVIEW_STY
#include "bufferparams.h"
#endif
#include "converter.h"
#include "debug.h"
#include "lyxrc.h"
@ -75,11 +68,6 @@ typedef list<string> PendingSnippets;
// Each item in the vector is a pair<snippet, image file name>.
typedef vector<StrPair> BitmapFile;
#if !USING_NEW_PREVIEW_STY
double setFontScalingFactor(Buffer &);
#endif
string const unique_filename(string const bufferpath);
Converter const * setConverter();
@ -321,12 +309,8 @@ namespace grfx {
PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
: parent_(p), buffer_(b), font_scaling_factor_(0.0)
{
#if USING_NEW_PREVIEW_STY
font_scaling_factor_ = 0.01 * lyxrc.dpi * lyxrc.zoom *
lyxrc.preview_scale_factor;
#else
font_scaling_factor_ = setFontScalingFactor(const_cast<Buffer &>(b));
#endif
lyxerr[Debug::GRAPHICS] << "The font scaling factor is "
<< font_scaling_factor_ << endl;
@ -672,68 +656,6 @@ Converter const * setConverter()
}
#if !USING_NEW_PREVIEW_STY
double setFontScalingFactor(Buffer & buffer)
{
double scale_factor = 0.01 * lyxrc.dpi * lyxrc.zoom *
lyxrc.preview_scale_factor;
// 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 = buffer.params.getLyXTextClass();
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}"
if (!prefixIs(ltrim(str), "\\ExecuteOptions"))
continue;
// str contains just the options of \ExecuteOptions
string const tmp = split(str, '{');
split(tmp, str, '}');
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;
}
#endif
void setAscentFractions(vector<double> & ascent_fractions,
string const & metrics_file)
{
@ -753,7 +675,6 @@ void setAscentFractions(vector<double> & ascent_fractions,
bool error = false;
#if USING_NEW_PREVIEW_STY
// Tightpage dimensions affect all subsequent dimensions
int tp_ascent;
int tp_descent;
@ -806,43 +727,6 @@ void setAscentFractions(vector<double> & ascent_fractions,
}
}
#else
int snippet_counter = 0;
for (; it != end; ++it) {
// Extracting lines of the form
// %%Page id: tp_bl_x tp_bl_y tp_tr_x tp_tr_y asc desc width
string page;
string page_id;
int dummy;
int tp_ascent;
int tp_descent;
int ascent;
int descent;
in >> page >> page_id
>> dummy >> tp_descent >> dummy >> tp_ascent
>> ascent >> descent >> dummy;
page_id = rtrim(page_id, ":");
error = !in.good()
|| !isStrUnsignedInt(page_id);
if (error)
break;
int const snippet_id = strToInt(page_id);
error = page != "%%Page"
|| ++snippet_counter != snippet_id;
if (error)
break;
double const a = ascent + tp_ascent;
double const d = descent - tp_descent;
if (!lyx::float_equal(a + d, 0, 0.1))
*it = a / (a + d);
}
#endif
if (error) {
lyxerr[Debug::GRAPHICS]
<< "setAscentFractions(" << metrics_file << ")\n"