Refine the lyxpreview legacy conversion (the ghostscript one).

- Break up steps 1, 2 and 3 from the legacy preview mechanism.
    These are really separate steps.

      1) Add color info, run latex
      2) Run dvips
      3) Run ghostscript

  - In the case of pdf output, skip step 2 and go directly to step 3.
  - Make sure that we fall back to the legacy conversion whenever we can.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39661 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Julien Rioux 2011-09-11 18:23:41 +00:00
parent bb55234cac
commit 45280f6bb5
2 changed files with 66 additions and 28 deletions

View File

@ -51,9 +51,12 @@
# 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]
@ -77,8 +80,8 @@ 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, progress, run_command, warning, \
write_metrics_info
latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \
run_command, warning, write_metrics_info
def usage(prog_name):
@ -265,12 +268,24 @@ def legacy_conversion(argv, skipMetrics = False):
fg_color = argv[4]
bg_color = argv[5]
bg_color_gr = make_texcolor(argv[5], True)
# External programs used by the script.
latex = find_exe_or_terminate(latex or latex_commands)
pdf_output = latex in pdflatex_commands
return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
bg_color, latex, pdf_output, skipMetrics)
# Add color info to the latex file, since ghostscript doesn't
# have the option to set foreground and background colors on
# the command line. Run the resulting file through latex.
def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
latex, pdf_output = False, skipMetrics = False):
# Move color information into the latex file.
bg_color_gr = make_texcolor(bg_color, True)
if not legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
error("Unable to move color info into the latex file")
@ -282,7 +297,10 @@ def legacy_conversion(argv, skipMetrics = False):
warning("%s had problems compiling %s" \
% (os.path.basename(latex), latex_file))
return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
if pdf_output:
return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
else:
return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
# Creates a new LaTeX file from the original with pages specified in
# failed_pages, pass it through pdflatex and updates the metrics
@ -332,16 +350,15 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
original_bitmap, destination_bitmap)
# The file has been processed through latex and we expect dvi output.
# Run dvips, taking note whether it was successful.
def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False):
# External programs used by the script.
dvips = find_exe_or_terminate(["dvips"])
gs = find_exe_or_terminate(["gswin32c", "gs"])
pnmcrop = find_exe(["pnmcrop"])
# Run the dvi file through dvips.
dvi_file = latex_file_re.sub(".dvi", latex_file)
ps_file = latex_file_re.sub(".ps", latex_file)
pdf_file = latex_file_re.sub(".pdf", latex_file)
dvips_call = '%s -i -o "%s" "%s"' % (dvips, ps_file, dvi_file)
dvips_failed = False
@ -352,6 +369,20 @@ def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False)
% (os.path.basename(dvips), dvi_file))
dvips_failed = True
return legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics)
# Either latex and dvips have been run and we have a ps file, or
# pdflatex has been run and we have a pdf file. Proceed with gs.
def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics = False):
# External programs used by the script.
gs = find_exe_or_terminate(["gswin32c", "gs"])
pnmcrop = find_exe(["pnmcrop"])
# Files to process
pdf_file = latex_file_re.sub(".pdf", latex_file)
ps_file = latex_file_re.sub(".ps", latex_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)

View File

@ -77,8 +77,7 @@
import getopt, glob, os, re, shutil, string, sys
from legacy_lyxpreview2ppm import legacy_conversion, \
legacy_conversion_step2, legacy_extract_metrics_info
from legacy_lyxpreview2ppm import legacy_conversion_step1
from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
@ -419,19 +418,31 @@ def main(argv):
warning("%s failed to compile %s" \
% (os.path.basename(lilypond_book), lytex_file))
if pdf_output:
progress("Using the legacy conversion method (PDF support)")
return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
bg_color, latex, pdf_output)
# This can go once dvipng becomes widespread.
dvipng = find_exe(["dvipng"])
if dvipng == None:
# The data is input to legacy_conversion in as similar
# as possible a manner to that input to the code used in
# LyX 1.3.x.
progress("Using the legacy conversion method (dvipng not found)")
vec = [ script_name, input_path, str(dpi), output_format, fg_color, bg_color, latex ]
return legacy_conversion(vec)
return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
bg_color, latex, pdf_output)
dv2dt = find_exe(["dv2dt"])
if dv2dt == None:
progress("Using the legacy conversion method (dv2dt not found)")
return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
bg_color, latex, pdf_output)
pngtopnm = ""
if output_format == "ppm":
pngtopnm = find_exe_or_terminate(["pngtopnm"])
pngtopnm = find_exe(["pngtopnm"])
if pngtopnm == None:
progress("Using the legacy conversion method (pngtopnm not found)")
return legacy_conversion_step1(latex_file, dpi, output_format,
fg_color, bg_color, latex, pdf_output)
# Move color information for PDF into the latex file.
if not color_pdf(latex_file, bg_color_gr, fg_color_gr):
@ -445,11 +456,6 @@ def main(argv):
warning("%s had problems compiling %s" \
% (os.path.basename(latex), latex_file))
if latex == "xelatex":
warning("Using XeTeX")
# FIXME: skip unnecessary dvips trial in legacy_conversion_step2
return legacy_conversion_step2(latex_file, dpi, output_format)
# The dvi output file name
dvi_file = latex_file_re.sub(".dvi", latex_file)
@ -460,7 +466,9 @@ def main(argv):
if os.path.isfile(pdf_file):
progress("%s produced a PDF output, fallback to legacy." \
% (os.path.basename(latex)))
return legacy_conversion_step2(latex_file, dpi, output_format)
progress("Using the legacy conversion method (PDF support)")
return legacy_conversion_step1(latex_file, dpi, output_format,
fg_color, bg_color, latex, True)
else:
error("No DVI or PDF output. %s failed." \
% (os.path.basename(latex)))
@ -473,9 +481,9 @@ def main(argv):
# If all pages need PostScript, directly use the legacy method.
if len(ps_pages) == page_count:
vec = [ script_name, input_path, str(dpi), output_format, fg_color, bg_color, latex ]
progress("Using the legacy conversion method (PostScript support)")
return legacy_conversion(vec)
return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
bg_color, latex, pdf_output)
# Run the dvi file through dvipng.
dvipng_call = '%s -Ttight -depth -height -D %d -fg "%s" -bg "%s" %s "%s"' \
@ -485,9 +493,9 @@ def main(argv):
if dvipng_status:
warning("%s failed to generate images from %s... fallback to legacy method" \
% (os.path.basename(dvipng), dvi_file))
# FIXME: skip unnecessary dvips trial in legacy_conversion_step2
progress("Using the legacy conversion method (dvipng failed)")
return legacy_conversion_step2(latex_file, dpi, output_format)
return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
bg_color, latex, pdf_output)
# Extract metrics info from dvipng_stdout.
metrics_file = latex_file_re.sub(".metrics", latex_file)
@ -501,11 +509,10 @@ def main(argv):
filter_pages(latex_file, legacy_latex_file, ps_pages)
# Pass the new LaTeX file to the legacy method
vec = [ script_name, latex_file_re.sub("_legacy.tex", input_path),
str(dpi), output_format, fg_color, bg_color, latex ]
progress("Pages %s include postscript specials" % ps_pages)
progress("Using the legacy conversion method (PostScript support)")
legacy_metrics = legacy_conversion(vec, True)[1]
legacy_status, legacy_metrics = legacy_conversion_step1(legacy_latex_file,
dpi, output_format, fg_color, bg_color, latex, pdf_output, True)
# Now we need to mix metrics data from dvipng and the legacy method
original_bitmap = latex_file_re.sub("%d." + output_format, legacy_latex_file)