The Instant Preview script in python.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_3_X@8810 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2004-06-09 12:35:49 +00:00
parent cd8d7e5090
commit bc3f8c8225
7 changed files with 405 additions and 238 deletions

View File

@ -1,9 +1,22 @@
2004-06-09 Angus Leeming <leeming@lyx.org>
* scripts/lyxpreview2bitmap.sh: removed.
* scripts/lyxpreview2ppm.py: added. lyxpreview2ppm.py is meant to
be a straight swap for lyxpreview2bitmap.sh, the idea being that
the python script has some hope of working on Win32 machines.
* configure.m4: adjust lyxpreview_to_bitmap_command.
2004-05-05 Angus Leeming <leeming@lyx.org>
* scripts/lyxpreview2bitmap.sh: enable the script to work on
2004-05-19 Uwe Stöhr <uwestoehr@web.de>
* lib/math.bind: fix some bindings to work with german keyboard;
new binding "M-m y" that inserts \oint
2003-05-05 Angus Leeming <leeming@lyx.org>
2004-05-05 Angus Leeming <leeming@lyx.org>
* scripts/lyxpreview2bitmap.sh: enable the script to work on
solaris machines by changing 'grep -E' to 'grep'.

View File

@ -281,7 +281,7 @@ SEARCH_PROG([for a DVI to PDF converter],dvi_to_pdf_command,dvipdfm)
test $dvi_to_pdf_command = "dvipdfm" && dvi_to_pdf_command="dvipdfm \$\$i"
# We have a script to convert previewlyx to ppm
lyxpreview_to_bitmap_command="lyxpreview2bitmap.sh"
lyxpreview_to_bitmap_command="lyxpreview2ppm.py"
# Search a *roff program (used to translate tables in ASCII export)
LYXRC_PROG([for a *roff formatter], \ascii_roff_command, dnl

View File

@ -1,235 +0,0 @@
#! /bin/sh
# file lyxpreview2bitmap.sh
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
#
# author Angus Leeming
# with much advice from David Kastrup, david.kastrup@t-online.de.
#
# Full author contact details are available in file CREDITS
# This script takes a LaTeX file and generates bitmap image files,
# one per page.
# 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.
# preview.sty can be obtained from
# CTAN/macros/latex/contrib/supported/preview.
# This script takes three arguments:
# TEXFILE: the name of the .tex file to be converted.
# SCALEFACTOR: a scale factor, used to ascertain the resolution of the
# generated image which is then passed to gs.
# OUTPUTFORMAT: the format of the output bitmap image files.
# Two formats are recognised: "ppm" and "png".
# If successful, this script will leave in dir ${DIR}:
# a (possibly large) number of image files with names like
# ${BASE}\([0-9]*\).${SUFFIX} where SUFFIX is ppm or png.
# a file containing info needed by LyX to position the images correctly
# on the screen.
# ${BASE}.metrics
# All other files ${BASE}* will be deleted.
# A quick note on the choice of OUTPUTFORMAT:
# In general files in PPM format are 10-100 times larger than the
# equivalent files in PNG format. Larger files results in longer
# reading and writing times as well as greater disk usage.
# However, whilst the Qt image loader can load files in PNG format
# without difficulty, the xforms image loader cannot. They must first
# be converted to a loadable format (eg PPM!). Thus, previews will take
# longer to appear if the xforms loader is used to load snippets in
# PNG format.
# You can always experiment by adding a line to your
# ${LYXUSERDIR}/preferences file
# \converter lyxpreview ${FORMAT} "lyxpreview2bitmap.sh" ""
# where ${FORMAT} is either ppm or png.
# These four programs are used by the script.
# Adjust their names to suit your setup.
LATEX=latex
DVIPS=dvips
GS=gs
PNMCROP=pnmcrop
readonly LATEX DVIPS GS PNMCROP
# Three helper functions.
FIND_IT ()
{
test $# -eq 1 || exit 1
type $1 > /dev/null || {
echo "Unable to find \"$1\". Please install."
exit 1
}
}
BAIL_OUT ()
{
# Remove everything except the original .tex file.
FILES=`ls ${BASE}* | sed -e "/${BASE}\.tex/d"`
rm -f ${FILES} texput.log
echo "Leaving ${BASE}.tex in ${DIR}"
exit 1
}
REQUIRED_VERSION ()
{
test $# -eq 1 || exit 1
echo "We require preview.sty version 0.73 or newer. You're using"
grep 'Package: preview' $1
}
# Preliminary check.
if [ $# -ne 3 ]; then
exit 1
fi
# Extract the params from the argument list.
DIR=`dirname $1`
BASE=`basename $1 .tex`
SCALEFACTOR=$2
if [ "$3" = "ppm" ]; then
GSDEVICE=pnmraw
GSSUFFIX=ppm
elif [ "$3" = "png" ]; then
GSDEVICE=png16m
GSSUFFIX=png
else
echo "Unrecognised output format ${OUTPUTFORMAT}."
echo "Expected either \"ppm\" or \"png\"."
BAIL_OUT
fi
# We use latex, dvips and gs, so check that they're all there.
FIND_IT ${LATEX}
FIND_IT ${DVIPS}
FIND_IT ${GS}
# Initialise some variables.
TEXFILE=${BASE}.tex
LOGFILE=${BASE}.log
DVIFILE=${BASE}.dvi
PSFILE=${BASE}.ps
METRICSFILE=${BASE}.metrics
readonly TEXFILE LOGFILE DVIFILE PSFILE METRICSFILE
# LaTeX -> DVI.
cd ${DIR}
${LATEX} ${TEXFILE} ||
{
echo "Failed: ${LATEX} ${TEXFILE}"
BAIL_OUT
}
# Parse ${LOGFILE} to obtain bounding box info to output to
# ${METRICSFILE}.
# This extracts lines starting "Preview: Tightpage" and
# "Preview: Snippet".
grep 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
{
echo "Failed: grep 'Preview: [ST]' ${LOGFILE}"
REQUIRED_VERSION ${LOGFILE}
BAIL_OUT
}
# 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}` ||
{
echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
REQUIRED_VERSION ${LOGFILE}
BAIL_OUT
}
# 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.)
# Note: use "" quotes in the echo to preserve newlines.
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 LINE=`grep 'Preview: Magnification' ${LOGFILE}`; then
# Strip out everything that won't form an /integer/.
MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
else
MAGNIFICATION=1000
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} ||
{
echo "Failed: ${DVIPS} -o ${PSFILE} ${DVIFILE}"
BAIL_OUT
}
# PostScript -> Bitmap files
# Older versions of gs have problems with a large degree of
# anti-aliasing at high resolutions
# test expects integer arguments.
# ${RESOLUTION} may be a float. Truncate it.
INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
ALPHA=4
if [ ${INT_RESOLUTION} -gt 150 ]; then
ALPHA=2
fi
${GS} -q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
-dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
-r${RESOLUTION} ${PSFILE} ||
{
echo "Failed: ${GS} ${PSFILE}"
BAIL_OUT
}
# 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]*\).${GSSUFFIX}/d"`
rm -f ${FILES} texput.log
# The bitmap files can have large amounts of whitespace to the left and
# right. This can be cropped if so desired.
CROP=1
type ${PNMCROP} > /dev/null || CROP=0
# There's no point cropping the image if using PNG images. If you want to
# crop, use PPM.
# Apparently dvipng will support cropping at some stage in the future...
if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
for FILE in ${BASE}*.${GSSUFFIX}
do
if ${PNMCROP} -left ${FILE} 2> /dev/null |\
${PNMCROP} -right 2> /dev/null > ${BASE}.tmp; then
mv ${BASE}.tmp ${FILE}
else
rm -f ${BASE}.tmp
fi
done
rm -f ${BASE}.tmp
fi
echo "Previews generated!"

380
lib/scripts/lyxpreview2ppm.py Executable file
View File

@ -0,0 +1,380 @@
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# file lyxpreview2ppm.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
# author Angus Leeming
# Full author contact details are available in file CREDITS
# with much advice from members of the preview-latex project:
# David Kastrup, dak@gnu.org and
# Jan-Åke Larsson, jalar@mai.liu.se.
# and with much help testing the code under Windows from
# Paul A. Rubin, rubin@msu.edu.
# This script takes a LaTeX file and generates a collection of
# ppm image files, one per previewed snippet.
# Pre-requisites:
# * A latex executable;
# * preview.sty;
# * dvips;
# * gs;
# * pnmcrop (optional).
# preview.sty is part of the preview-latex project
# http://preview-latex.sourceforge.net/
# preview.sty can alternatively be obtained from
# CTAN/support/preview-latex/
# Example usage:
# lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm
# This script takes three arguments:
# TEXFILE: the name of the .tex file to be converted.
# SCALEFACTOR: a scale factor, used to ascertain the resolution of the
# generated image which is then passed to gs.
# OUTPUTFORMAT: the format of the output bitmap image files.
# This particular script can produce only "ppm" format output.
# Decomposing TEXFILE's name as DIR/BASE.tex, this script will,
# if executed successfully, leave in DIR:
# * a (possibly large) number of image files with names
# like BASE[0-9]+.ppm
# * a file BASE.metrics, containing info needed by LyX to position
# the images correctly on the screen.
import glob, os, re, string, sys
import pipes, tempfile
use_win32_modules = 0
if os.name == "nt":
use_win32_modules = 1
try:
import pywintypes
import win32con
import win32event
import win32file
import win32pipe
import win32process
import win32security
import winerror
except:
sys.stderr.write("Consider installing the PyWin extension modules "\
"if you're irritated by the gs window being shown\n")
use_win32_modules = 0
# Pre-compiled regular expressions.
latex_file_re = re.compile("\.tex$")
def usage(prog_name):
return "Usage: %s <latex file> <dpi> ppm\n"\
% prog_name
def error(message):
sys.stderr.write(message + '\n')
sys.exit(1)
def find_exe(candidates, path):
for prog in candidates:
for directory in path:
if os.name == "nt":
full_path = os.path.join(directory, prog + ".exe")
else:
full_path = os.path.join(directory, prog)
if os.access(full_path, os.X_OK):
return full_path
return None
def find_exe_or_terminate(candidates, path):
exe = find_exe(candidates, path)
if exe == None:
error("Unable to find executable from '%s'" % string.join(candidates))
return exe
def run_command_popen(cmd):
handle = os.popen(cmd, 'r')
cmd_stdout = handle.read()
cmd_status = handle.close()
return cmd_status, cmd_stdout
def run_command_win32(cmd):
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = True
stdout_r, stdout_w = win32pipe.CreatePipe(sa, 0)
si = win32process.STARTUPINFO()
si.dwFlags = (win32process.STARTF_USESTDHANDLES
| win32process.STARTF_USESHOWWINDOW)
si.wShowWindow = win32con.SW_HIDE
si.hStdOutput = stdout_w
process, thread, pid, tid = \
win32process.CreateProcess(None, cmd, None, None, True,
0, None, None, si)
if process == None:
return -1, ""
# Must close the write handle in this process, or ReadFile will hang.
stdout_w.Close()
# Read the pipe until we get an error (including ERROR_BROKEN_PIPE,
# which is okay because it happens when child process ends).
data = ""
error = 0
while 1:
try:
hr, buffer = win32file.ReadFile(stdout_r, 4096)
if hr != winerror.ERROR_IO_PENDING:
data = data + buffer
except pywintypes.error, e:
if e.args[0] != winerror.ERROR_BROKEN_PIPE:
error = 1
break
if error:
return -2, ""
# Everything okay - called process has closed the pipe.
# For safety, check that the process ended, then pick up its exit code.
win32event.WaitForSingleObject(process, win32event.INFINITE)
if win32process.GetExitCodeProcess(process):
return -3, ""
return None, data
def run_command(cmd):
if use_win32_modules:
return run_command_win32(cmd)
else:
return run_command_popen(cmd)
def extract_metrics_info(log_file, metrics_file):
metrics = open(metrics_file, 'w')
log_re = re.compile("Preview: [ST]")
success = 0
for line in open(log_file, 'r').readlines():
match = log_re.match(line)
if match != None:
success = 1
metrics.write("%s\n" % line)
return success
def extract_resolution(log_file, dpi):
fontsize_re = re.compile("Preview: Fontsize")
magnification_re = re.compile("Preview: Magnification")
extract_decimal_re = re.compile("([0-9\.]+)")
extract_integer_re = re.compile("([0-9]+)")
found_fontsize = 0
found_magnification = 0
# Default values
magnification = 1000.0
fontsize = 0.0
for line in open(log_file, 'r').readlines():
if found_fontsize and found_magnification:
break
if not found_fontsize:
match = fontsize_re.match(line)
if match != None:
match = extract_decimal_re.search(line)
if match == None:
error("Unable to parse: %s" % line)
fontsize = string.atof(match.group(1))
found_fontsize = 1
continue
if not found_magnification:
match = magnification_re.match(line)
if match != None:
match = extract_integer_re.search(line)
if match == None:
error("Unable to parse: %s" % line)
magnification = string.atof(match.group(1))
found_magnification = 1
continue
return dpi * (10.0 / fontsize) * (1000.0 / magnification)
def get_version_info():
version_re = re.compile("([0-9])\.([0-9])")
match = version_re.match(sys.version)
if match == None:
error("Unable to extract version info from 'sys.version'")
return string.atoi(match.group(1)), string.atoi(match.group(2))
def copyfileobj(fsrc, fdst, rewind=0, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
if rewind:
fsrc.flush()
fsrc.seek(0)
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)
class TempFile:
"""clone of tempfile.TemporaryFile to use with python < 2.0."""
# Cache the unlinker so we don't get spurious errors at shutdown
# when the module-level "os" is None'd out. Note that this must
# be referenced as self.unlink, because the name TempFile
# may also get None'd out before __del__ is called.
unlink = os.unlink
def __init__(self):
self.filename = tempfile.mktemp()
self.file = open(self.filename,"w+b")
self.close_called = 0
def close(self):
if not self.close_called:
self.close_called = 1
self.file.close()
self.unlink(self.filename)
def __del__(self):
self.close()
def read(self, size = -1):
return self.file.read(size)
def write(self, line):
return self.file.write(line)
def seek(self, offset):
return self.file.seek(offset)
def flush(self):
return self.file.flush()
def mkstemp():
"""create a secure temporary file and return its object-like file"""
major, minor = get_version_info()
if major >= 2 and minor >= 0:
return tempfile.TemporaryFile()
else:
return TempFile()
def crop_files(pnmcrop, basename):
t = pipes.Template()
t.append("%s -left" % pnmcrop, '--')
t.append("%s -right" % pnmcrop, '--')
for file in glob.glob("%s*.ppm" % basename):
tmp = mkstemp()
new = t.open(file, "r")
copyfileobj(new, tmp)
if not new.close():
copyfileobj(tmp, open(file,"wb"), 1)
def main(argv):
# Parse and manipulate the command line arguments.
if len(argv) != 4:
error(usage(argv[0]))
dir, latex_file = os.path.split(argv[1])
if len(dir) != 0:
os.chdir(dir)
dpi = string.atoi(argv[2])
output_format = argv[3]
if output_format != "ppm":
error("This script can generate ppm format images only.")
# External programs used by the script.
path = string.split(os.getenv("PATH"), os.pathsep)
latex = find_exe_or_terminate(["pplatex", "latex2e", "latex"], path)
dvips = find_exe_or_terminate(["dvips"], path)
gs = find_exe_or_terminate(["gswin32", "gs"], path)
pnmcrop = find_exe(["pnmcrop"], path)
# Compile the latex file.
latex_call = "%s %s" % (latex, latex_file)
latex_status, latex_stdout = run_command(latex_call)
if latex_status != None:
error("%s failed to compile %s" \
% (os.path.basename(latex), latex_file))
# Run the dvi file through dvips.
dvi_file = latex_file_re.sub(".dvi", latex_file)
ps_file = latex_file_re.sub(".ps", latex_file)
dvips_call = "%s -o %s %s" % (dvips, ps_file, dvi_file)
dvips_status, dvips_stdout = run_command(dvips_call)
if dvips_status != None:
error("Failed: %s %s" % (os.path.basename(dvips), dvi_file))
# Extract resolution data for gs from the log file.
log_file = latex_file_re.sub(".log", latex_file)
resolution = extract_resolution(log_file, dpi)
# Older versions of gs have problems with a large degree of
# anti-aliasing at high resolutions
alpha = 4
if resolution > 150:
alpha = 2
# Generate the bitmap images
gs_call = "%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw " \
"-sOutputFile=%s%%d.ppm " \
"-dGraphicsAlphaBit=%d -dTextAlphaBits=%d " \
"-r%f %s" \
% (gs, latex_file_re.sub("", latex_file), \
alpha, alpha, resolution, ps_file)
gs_status, gs_stdout = run_command(gs_call)
if gs_status != None:
error("Failed: %s %s" % (os.path.basename(gs), ps_file))
# Crop the images
if pnmcrop != None:
crop_files(pnmcrop, latex_file_re.sub("", latex_file))
# Extract metrics info from the log file.
metrics_file = latex_file_re.sub(".metrics", latex_file)
if not extract_metrics_info(log_file, metrics_file):
error("Failed to extract metrics info from %s" % log_file)
return 0
if __name__ == "__main__":
main(sys.argv)

View File

@ -1,3 +1,8 @@
2004-06-09 Angus Leeming <leeming@lyx.org>
* PreviewLoader.C (startLoading): remove the "sh " prefix from the
preview -> bitmap command. Doesn't work with python scripts...
2003-10-22 Angus Leeming <leeming@lyx.org>
* GraphicsConverter.C (Impl c-tor): add a warning message if the file

View File

@ -482,7 +482,7 @@ void PreviewLoader::Impl::startLoading()
cs << pconverter_->command << ' ' << latexfile << ' '
<< int(font_scaling_factor_) << ' ' << pconverter_->to;
string const command = "sh " + LibScriptSearch(STRCONV(cs.str()));
string const command = LibScriptSearch(STRCONV(cs.str()));
// Initiate the conversion from LaTeX to bitmap images files.
Forkedcall::SignalTypePtr convert_ptr(new Forkedcall::SignalType);

View File

@ -25,6 +25,10 @@ What's new
- updated basque, danish, german, italian and romanian localizations;
updated basque and german documentations
- the script used to control the generation of bitmap images for use
in 'Instant Preview' of math equations has been re-written in python.
Windows users now have some chance of getting Instant Preview to
work for them.
** Bug fixes