X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fscripts%2Flegacy_lyxpreview2ppm.py;h=7c6e1e6274dc562e675a5fce85aa64fbd5d64e15;hb=c0bbc020a56e0e55fe4a07230ab6d9851fef0c24;hp=6105e4418e377d3f3a83efc8a0a05ad1613e2343;hpb=2e66a01e8ff46086bf0f64381201b1cf55e473c1;p=lyx.git diff --git a/lib/scripts/legacy_lyxpreview2ppm.py b/lib/scripts/legacy_lyxpreview2ppm.py index 6105e4418e..7c6e1e6274 100644 --- a/lib/scripts/legacy_lyxpreview2ppm.py +++ b/lib/scripts/legacy_lyxpreview2ppm.py @@ -1,66 +1,98 @@ #! /usr/bin/env python +# -*- coding: utf-8 -*- # file legacy_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 -# This script converts a LaTeX file to a bunch of ppm files using the -# deprecated dvi->ps->ppm conversion route. - -# If possible, please grab 'dvipng'; it's faster and more robust. -# This legacy support will be removed one day... - -import glob, os, re, string, sys -import pipes, tempfile - - -# Pre-compiled regular expressions. -latex_file_re = re.compile("\.tex$") +# 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 def usage(prog_name): - return "Usage: %s \n"\ - "\twhere the colors are hexadecimal strings, eg 'faf0e6'"\ + return "Usage: %s ppm \n" \ + "\twhere the colors are hexadecimal strings, eg 'faf0e6'" \ % 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: - 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(cmd): - handle = os.popen(cmd, 'r') - cmd_stdout = handle.read() - cmd_status = handle.close() - - return cmd_status, cmd_stdout - - -def extract_metrics_info(log_file, metrics_file): - metrics = open(metrics_file, 'w') +# Returns a list of tuples containing page number and ascent fraction +# extracted from dvipng output. +# Use write_metrics_info to create the .metrics file with this info +def legacy_extract_metrics_info(log_file): log_re = re.compile("Preview: ([ST])") data_re = re.compile("(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)") @@ -69,33 +101,51 @@ def extract_metrics_info(log_file, metrics_file): tp_descent = 0.0 success = 0 - for line in open(log_file, 'r').readlines(): - match = log_re.match(line) - if match == None: - continue + results = [] + try: + for line in open(log_file, 'r').readlines(): + match = log_re.match(line) + if match == None: + continue - snippet = (match.group(1) == 'S') - success = 1 - match = data_re.search(line) - if match == None: - error("Unexpected data in %s\n%s" % (log_file, line)) + snippet = (match.group(1) == 'S') + success = 1 + match = data_re.search(line) + if match == None: + error("Unexpected data in %s\n%s" % (log_file, line)) - if snippet: - ascent = string.atof(match.group(2)) + tp_ascent - descent = string.atof(match.group(3)) - tp_descent + if snippet: + ascent = string.atoi(match.group(2)) + descent = string.atoi(match.group(3)) - frac = 0.5 - if abs(ascent + descent) > 0.1: - frac = ascent / (ascent + descent) + frac = 0.5 + if ascent >= 0 and descent >= 0: + ascent = float(ascent) + tp_ascent + descent = float(descent) - tp_descent - metrics.write("Snippet %s %f\n" % (match.group(1), frac)) + if abs(ascent + descent) > 0.1: + frac = ascent / (ascent + descent) - else: - tp_descent = string.atof(match.group(2)) - tp_ascent = string.atof(match.group(4)) + # Sanity check + if frac < 0 or frac > 1: + frac = 0.5 - return success + results.append((int(match.group(1)), frac)) + + else: + tp_descent = string.atof(match.group(2)) + tp_ascent = string.atof(match.group(4)) + + except: + # Unable to open the file, but do nothing here because + # the calling function will act on the value of 'success'. + warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file) + warning(`sys.exc_type` + ',' + `sys.exc_value`) + + if success == 0: + error("Failed to extract metrics info from %s" % log_file) + return results def extract_resolution(log_file, dpi): fontsize_re = re.compile("Preview: Fontsize") @@ -108,122 +158,80 @@ def extract_resolution(log_file, dpi): # 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 - + fontsize = 10.0 + + try: + 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 + + except: + warning('Warning in extract_resolution! Unable to open "%s"' % log_file) + warning(`sys.exc_type` + ',' + `sys.exc_value`) + + # This is safe because both fontsize and magnification have + # non-zero default values. 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 legacy_latex_file(latex_file, fg_color, bg_color): - use_preview_re = re.compile("(\\\\usepackage\[[^]]+)(\]{preview})") + use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}") + fg_color_gr = make_texcolor(fg_color, True) + bg_color_gr = make_texcolor(bg_color, True) tmp = mkstemp() success = 0 - for line in open(latex_file, 'r').readlines(): + try: + f = open(latex_file, 'r') + except: + # Unable to open the file, but do nothing here because + # the calling function will act on the value of 'success'. + warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file) + warning(`sys.exc_type` + ',' + `sys.exc_value`) + + for line in f.readlines(): + if success: + tmp.write(line) + continue match = use_preview_re.match(line) if match == None: tmp.write(line) continue - success = 1 - tmp.write("%s,dvips,tightpage%s\n\n" \ - "\\AtBeginDocument{\\AtBeginDvi{%%\n" \ - "\\special{!userdict begin/bop-hook{//bop-hook exec\n" \ - "<%s%s>{255 div}forall setrgbcolor\n" \ - "clippath fill setrgbcolor}bind def end}}}\n" \ - % (match.group(1), match.group(2), fg_color, bg_color)) + # Package order: color should be loaded before preview + # Preview options: add the options lyx and tightpage + tmp.write(r""" +\usepackage{color} +\definecolor{fg}{rgb}{%s} +\definecolor{bg}{rgb}{%s} +\pagecolor{bg} +\usepackage[%s,lyx,tightpage]{preview} +\makeatletter +\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}} +\g@addto@macro\endpreview{\endgroup} +\makeatother +""" % (fg_color_gr, bg_color_gr, match.group(1))) if success: copyfileobj(tmp, open(latex_file,"wb"), 1) @@ -233,8 +241,8 @@ def legacy_latex_file(latex_file, fg_color, bg_color): def crop_files(pnmcrop, basename): t = pipes.Template() - t.append("%s -left" % pnmcrop, '--') - t.append("%s -right" % pnmcrop, '--') + t.append('%s -left' % pnmcrop, '--') + t.append('%s -right' % pnmcrop, '--') for file in glob.glob("%s*.ppm" % basename): tmp = mkstemp() @@ -244,49 +252,130 @@ def crop_files(pnmcrop, basename): copyfileobj(tmp, open(file,"wb"), 1) -def legacy_conversion(argv): +def legacy_conversion(argv, skipMetrics = False): # Parse and manipulate the command line arguments. - if len(argv) != 6: + if len(argv) == 7: + latex = [argv[6]] + elif len(argv) != 6: error(usage(argv[0])) + else: + latex = None - # Ignore argv[1] - - dir, latex_file = os.path.split(argv[2]) + dir, latex_file = os.path.split(argv[1]) if len(dir) != 0: os.chdir(dir) - dpi = string.atoi(argv[3]) + dpi = string.atoi(argv[2]) + + output_format = argv[3] + fg_color = argv[4] bg_color = argv[5] # 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) + latex = find_exe_or_terminate(latex or latex_commands) + + pdf_output = latex in pdflatex_commands - # Move color information into the latex file. + 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, lyx and tightpage options into the latex file. if not legacy_latex_file(latex_file, fg_color, bg_color): - error("Unable to move color info into the latex file") + error("""Unable to move the color information, and the lyx and tightpage + options of preview-latex, into the latex file""") # Compile the latex file. - latex_call = "%s %s" % (latex, latex_file) + latex_status, latex_stdout = run_latex(latex, latex_file) + + 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 +# from the standard legacy route +def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs, + gs_device, gs_ext, alpha, resolution, output_format): + + # Search for pdflatex executable + pdflatex = find_exe(["pdflatex"]) + if pdflatex == None: + warning("Can't find pdflatex. Some pages failed with all the possible routes.") + 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 + pdflatex_status, pdflatex_stdout = run_latex(pdflatex, 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: + # Give up! + warning("Some pages failed with all the possible routes") + 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) + + original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file) + destination_bitmap = latex_file_re.sub("%d." + output_format, 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)) + # Join the metrics with the those from dvips and rename the bitmap images + join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages, + 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"]) # 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_call = '%s -i -o "%s" "%s"' % (dvips, ps_file, dvi_file) + dvips_failed = False + dvips_status, dvips_stdout = run_command(dvips_call) - if dvips_status != None: - error("Failed: %s %s" % (os.path.basename(dvips), dvi_file)) + if dvips_status: + warning('Failed: %s %s ... looking for PDF' \ + % (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) @@ -298,25 +387,73 @@ def legacy_conversion(argv): 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_device = "png16m" + gs_ext = "png" + if output_format == "ppm": + gs_device = "pnmraw" + gs_ext = "ppm" + + # Extract the metrics from the log file + legacy_metrics = legacy_extract_metrics_info(log_file) + + # List of pages which failed to produce a correct output + failed_pages = [] - gs_status, gs_stdout = run_command(gs_call) - if gs_status != None: - error("Failed: %s %s" % (os.path.basename(gs), ps_file)) + # 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)) + 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) + + 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.sort() + + # Call GhostScript 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)) + 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) # 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) + # Allow to skip .metrics creation for custom management + # (see the dvipng method) + if not skipMetrics: + # Extract metrics info from the log file. + metrics_file = latex_file_re.sub(".metrics", latex_file) + write_metrics_info(legacy_metrics, metrics_file) + + return (0, legacy_metrics) + - return 0 +if __name__ == "__main__": + sys.exit(legacy_conversion(sys.argv)[0])