+# 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
+# png or ppm image files, one per previewed snippet.
+# Example usage:
+# legacy_lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm 000000 faf0e6
+
+# This script takes five 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.
+# FG_COLOR: the foreground color as a hexadecimal string, eg '000000'.
+# BG_COLOR: the background color as a hexadecimal string, eg 'faf0e6'.
+
+# 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|png)
+# * a file BASE.metrics, containing info needed by LyX to position
+# the images correctly on the screen.
+
+# The script uses several external programs and files:
+# * python 2.4 or later (subprocess module);
+# * A latex executable;
+# * preview.sty;
+# * dvips;
+# * gs;
+# * pdflatex (optional);
+# * pnmcrop (optional).
+
+# preview.sty is part of the preview-latex project
+# http://preview-latex.sourceforge.net/
+# Alternatively, it can be obtained from
+# CTAN/support/preview-latex/
+
+# What does this script do?
+# [legacy_conversion]
+# 0) Process command-line arguments
+# [legacy_conversion_step1]
+# 1) Call latex to create a DVI file from LaTeX
+# [legacy_conversion_step2]
+# 2) Call dvips to create one PS file for each DVI page
+# [legacy_conversion_step3]
+# 3) If dvips fails look for PDF and call gs to produce bitmaps
+# 4) Otherwise call gs on each PostScript file to produce bitmaps
+# [legacy_conversion_pdflatex]
+# 5) Keep track of pages on which gs failed and pass them to pdflatex
+# 6) Call gs on the PDF output from pdflatex to produce bitmaps
+# 7) Extract and write to file (or return to lyxpreview2bitmap)
+# metrics from both methods (standard and pdflatex)
+
+# The script uses the old dvi->ps->png conversion route,
+# which is good when using PSTricks, TikZ or other packages involving
+# PostScript literals (steps 1, 2, 4).
+# This script also generates bitmaps from PDF created by a call to
+# lyxpreview2bitmap.py passing "pdflatex" to the CONVERTER parameter
+# (step 3).
+# Finally, there's also has a fallback method based on pdflatex, which
+# is required in certain cases, if hyperref is active for instance,
+# (step 5, 6).
+# If possible, dvipng should be used, as it's much faster.
+
+import glob, os, pipes, re, string, sys
+
+from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
+ find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
+ latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \
+ run_command, run_latex, warning, write_metrics_info