# * gs;
# * pdflatex (optional);
# * pnmcrop (optional).
+# * pdftocairo (optional).
+# * epstopdf (optional).
# preview.sty is part of the preview-latex project
# http://preview-latex.sourceforge.net/
# [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
+# 3) If dvips fails look for PDF and call pdftocairo or gs to produce bitmaps
+# 4) Otherwise call pdftocairo or 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
+# 6) Call pdftocairo or 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)
# 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.
+# If possible, the script will use pdftocairo instead of gs,
+# as it's much faster and gives better results.
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, \
+from lyxpreview_tools import check_latex_log, 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
error("Unexpected data in %s\n%s" % (log_file, line))
if snippet:
- ascent = string.atoi(match.group(2))
- descent = string.atoi(match.group(3))
+ ascent = string.atof(match.group(2))
+ descent = string.atof(match.group(3))
frac = 0.5
- if ascent >= 0 and descent >= 0:
- ascent = float(ascent) + tp_ascent
- descent = float(descent) - tp_descent
+ if ascent == 0 and descent == 0:
+ # This is an empty image, forbid its display
+ frac = -1.0
+ elif ascent >= 0 or descent >= 0:
+ ascent = ascent + tp_ascent
+ descent = descent - tp_descent
if abs(ascent + descent) > 0.1:
frac = ascent / (ascent + descent)
\definecolor{fg}{rgb}{%s}
\definecolor{bg}{rgb}{%s}
\pagecolor{bg}
-\usepackage[%s,lyx,tightpage]{preview}
+\usepackage[%s,tightpage]{preview}
\makeatletter
+\def\t@a{cmr}
+\if\f@family\t@a
+\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}}
+\fi
\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
\g@addto@macro\endpreview{\endgroup}
\makeatother
# Compile the latex file.
latex_status, latex_stdout = run_latex(latex, latex_file)
if latex_status:
- return (latex_status, [])
+ progress("Will try to recover from %s failure" % latex)
if pdf_output:
return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
# Creates a new LaTeX file from the original with pages specified in
# failed_pages, pass it through pdflatex and updates the metrics
# from the standard legacy route
-def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
- gs_device, gs_ext, alpha, resolution, output_format):
+def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
+ use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution, output_format):
+
+ error_count = 0
# Search for pdflatex executable
pdflatex = find_exe(["pdflatex"])
if pdflatex == None:
warning("Can't find pdflatex. Some pages failed with all the possible routes.")
+ failed_pages = []
else:
# Create a new LaTeX file from the original but only with failed pages
pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
filter_pages(latex_file, pdf_latex_file, failed_pages)
# pdflatex call
+ error_pages = []
pdflatex_status, pdflatex_stdout = run_latex(pdflatex, pdf_latex_file)
+ if pdflatex_status:
+ error_pages = check_latex_log(latex_file_re.sub(".log", pdf_latex_file))
pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
-
- # GhostScript call to produce bitmaps
- gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
- '-sOutputFile="%s%%d.%s" ' \
- '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
- '-r%f "%s"' \
- % (gs, gs_device, latex_file_re.sub("", pdf_latex_file), \
- gs_ext, alpha, alpha, resolution, pdf_file)
- gs_status, gs_stdout = run_command(gs_call)
- if gs_status:
+ latex_file_root = latex_file_re.sub("", pdf_latex_file)
+
+ # Converter call to produce bitmaps
+ if use_pdftocairo:
+ conv_call = '%s -png -transp -r %d "%s" "%s"' \
+ % (conv, resolution, pdf_file, latex_file_root)
+ conv_status, conv_stdout = run_command(conv_call)
+ if not conv_status:
+ seqnum_re = re.compile("-([0-9]+)")
+ for name in glob.glob("%s-*.png" % latex_file_root):
+ match = seqnum_re.search(name)
+ if match != None:
+ new_name = seqnum_re.sub(str(int(match.group(1))), name)
+ os.rename(name, new_name)
+ else:
+ conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
+ '-sOutputFile="%s%%d.%s" ' \
+ '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
+ '-r%f "%s"' \
+ % (conv, gs_device, latex_file_root, \
+ gs_ext, alpha, alpha, resolution, pdf_file)
+ conv_status, conv_stdout = run_command(conv_call)
+
+ if conv_status:
# Give up!
warning("Some pages failed with all the possible routes")
+ failed_pages = []
else:
# We've done it!
pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
+ # Invalidate metrics for pages that produced errors
+ if len(error_pages) > 0:
+ for index in error_pages:
+ pdf_metrics.pop(index - 1)
+ pdf_metrics.insert(index - 1, (index, -1.0))
+ error_count += 1
+
original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
original_bitmap, destination_bitmap)
+ return error_count
+
# The file has been processed through latex and we expect dvi output.
# Run dvips, taking note whether it was successful.
# 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.
+# pdflatex has been run and we have a pdf file. Proceed with pdftocairo or 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", "gswin64c", "gs"])
pnmcrop = find_exe(["pnmcrop"])
+ pdftocairo = find_exe(["pdftocairo"])
+ epstopdf = find_exe(["epstopdf"])
+ use_pdftocairo = pdftocairo != None and output_format == "png"
+ if use_pdftocairo:
+ conv = pdftocairo
+ else:
+ conv = gs
# 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.
+ # The latex file name without extension
+ latex_file_root = latex_file_re.sub("", latex_file)
+
+ # Extract resolution data for the converter from the log file.
log_file = latex_file_re.sub(".log", latex_file)
resolution = extract_resolution(log_file, dpi)
+ # Check whether some pages produced errors
+ error_pages = check_latex_log(log_file)
+
# Older versions of gs have problems with a large degree of
# anti-aliasing at high resolutions
alpha = 4
# Generate the bitmap images
if dvips_failed:
# dvips failed, maybe there's a PDF, try to produce bitmaps
- gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
- '-sOutputFile="%s%%d.%s" ' \
- '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
- '-r%f "%s"' \
- % (gs, gs_device, latex_file_re.sub("", latex_file), \
- gs_ext, alpha, alpha, resolution, pdf_file)
-
- gs_status, gs_stdout = run_command(gs_call)
- if gs_status:
- error("Failed: %s %s" % (os.path.basename(gs), ps_file))
+ if use_pdftocairo:
+ conv_call = '%s -png -transp -r %d "%s" "%s"' \
+ % (pdftocairo, resolution, pdf_file, latex_file_root)
+
+ conv_status, conv_stdout = run_command(conv_call)
+ if not conv_status:
+ seqnum_re = re.compile("-([0-9]+)")
+ for name in glob.glob("%s-*.png" % latex_file_root):
+ match = seqnum_re.search(name)
+ if match != None:
+ new_name = seqnum_re.sub(str(int(match.group(1))), name)
+ os.rename(name, new_name)
+ else:
+ conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
+ '-sOutputFile="%s%%d.%s" ' \
+ '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
+ '-r%f "%s"' \
+ % (gs, gs_device, latex_file_root, \
+ gs_ext, alpha, alpha, resolution, pdf_file)
+
+ conv_status, conv_stdout = run_command(conv_call)
+
+ if conv_status:
+ error("Failed: %s %s" % (os.path.basename(conv), pdf_file))
else:
- # Model for calling gs on each file
- gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
- '-sOutputFile="%s%%d.%s" ' \
- '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
- '-r%f "%%s"' \
- % (gs, gs_device, latex_file_re.sub("", latex_file), \
- gs_ext, alpha, alpha, resolution)
+ # Model for calling the converter on each file
+ if use_pdftocairo and epstopdf != None:
+ conv_call = '%s -png -transp -singlefile -r %d "%%s" "%s%%d"' \
+ % (pdftocairo, resolution, latex_file_root)
+ else:
+ conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
+ '-sOutputFile="%s%%d.%s" ' \
+ '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
+ '-r%f "%%s"' \
+ % (gs, gs_device, latex_file_root, \
+ gs_ext, alpha, alpha, resolution)
i = 0
# Collect all the PostScript files (like *.001, *.002, ...)
- ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file))
+ ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_root)
ps_files.sort()
- # Call GhostScript for each file
+ # Call the converter for each file
for file in ps_files:
i = i + 1
progress("Processing page %s, file %s" % (i, file))
- gs_status, gs_stdout = run_command(gs_call % (i, file))
- if gs_status:
- # gs failed, keep track of this
- warning("Ghostscript failed on page %s, file %s" % (i, file))
+ if use_pdftocairo and epstopdf != None:
+ conv_name = "epstopdf"
+ conv_status, conv_stdout = run_command("%s --outfile=%s.pdf %s"
+ % (epstopdf, file, file))
+ if not conv_status:
+ conv_name = "pdftocairo"
+ file = file + ".pdf"
+ conv_status, conv_stdout = run_command(conv_call % (file, i))
+ else:
+ conv_name = "ghostscript"
+ conv_status, conv_stdout = run_command(conv_call % (i, file))
+
+ if conv_status:
+ # The converter failed, keep track of this
+ warning("%s failed on page %s, file %s" % (conv_name, i, file))
failed_pages.append(i)
# Pass failed pages to pdflatex
if len(failed_pages) > 0:
- legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
- gs_device, gs_ext, alpha, resolution, output_format)
+ warning("Now trying to obtain failed previews through pdflatex")
+ error_count = legacy_conversion_pdflatex(latex_file, failed_pages,
+ legacy_metrics, use_pdftocairo, conv, gs_device, gs_ext, alpha,
+ resolution, output_format)
+ else:
+ error_count = 0
+
+ # Invalidate metrics for pages that produced errors
+ if len(error_pages) > 0:
+ for index in error_pages:
+ if index not in failed_pages:
+ legacy_metrics.pop(index - 1)
+ legacy_metrics.insert(index - 1, (index, -1.0))
+ error_count += 1
- # Crop the images
- if pnmcrop != None:
- crop_files(pnmcrop, latex_file_re.sub("", latex_file))
+ # Crop the ppm images
+ if pnmcrop != None and output_format == "ppm":
+ crop_files(pnmcrop, latex_file_root)
# Allow to skip .metrics creation for custom management
# (see the dvipng method)
# Extract metrics info from the log file.
metrics_file = latex_file_re.sub(".metrics", latex_file)
write_metrics_info(legacy_metrics, metrics_file)
+ if error_count:
+ warning("Failed to produce %d preview snippet(s)" % error_count)
return (0, legacy_metrics)