# 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]
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):
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")
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
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
% (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)
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, \
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):
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)
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)))
# 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"' \
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)
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)