1 # -*- coding: utf-8 -*-
3 # file legacy_lyxpreview2ppm.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
8 # Full author contact details are available in file CREDITS
10 # with much advice from members of the preview-latex project:
11 # David Kastrup, dak@gnu.org and
12 # Jan-Åke Larsson, jalar@mai.liu.se.
13 # and with much help testing the code under Windows from
14 # Paul A. Rubin, rubin@msu.edu.
16 # This script takes a LaTeX file and generates a collection of
17 # png or ppm image files, one per previewed snippet.
19 # legacy_lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm 000000 faf0e6
21 # This script takes five arguments:
22 # TEXFILE: the name of the .tex file to be converted.
23 # SCALEFACTOR: a scale factor, used to ascertain the resolution of the
24 # generated image which is then passed to gs.
25 # OUTPUTFORMAT: the format of the output bitmap image files.
26 # This particular script can produce only "ppm" format output.
27 # FG_COLOR: the foreground color as a hexadecimal string, eg '000000'.
28 # BG_COLOR: the background color as a hexadecimal string, eg 'faf0e6'.
30 # Decomposing TEXFILE's name as DIR/BASE.tex, this script will,
31 # if executed successfully, leave in DIR:
32 # * a (possibly large) number of image files with names
33 # like BASE[0-9]+.(ppm|png)
34 # * a file BASE.metrics, containing info needed by LyX to position
35 # the images correctly on the screen.
37 # The script uses several external programs and files:
38 # * python 2.4 or later (subprocess module);
39 # * A latex executable;
43 # * pdflatex (optional);
44 # * pnmcrop (optional).
45 # * pdftocairo (optional).
46 # * epstopdf (optional).
48 # preview.sty is part of the preview-latex project
49 # http://preview-latex.sourceforge.net/
50 # Alternatively, it can be obtained from
51 # CTAN/support/preview-latex/
53 # What does this script do?
55 # 0) Process command-line arguments
56 # [legacy_conversion_step1]
57 # 1) Call latex to create a DVI file from LaTeX
58 # [legacy_conversion_step2]
59 # 2) Call dvips to create one PS file for each DVI page
60 # [legacy_conversion_step3]
61 # 3) If dvips fails look for PDF and call pdftocairo or gs to produce bitmaps
62 # 4) Otherwise call pdftocairo or gs on each PostScript file to produce bitmaps
63 # [legacy_conversion_pdflatex]
64 # 5) Keep track of pages on which gs failed and pass them to pdflatex
65 # 6) Call pdftocairo or gs on the PDF output from pdflatex to produce bitmaps
66 # 7) Extract and write to file (or return to lyxpreview2bitmap)
67 # metrics from both methods (standard and pdflatex)
69 # The script uses the old dvi->ps->png conversion route,
70 # which is good when using PSTricks, TikZ or other packages involving
71 # PostScript literals (steps 1, 2, 4).
72 # This script also generates bitmaps from PDF created by a call to
73 # lyxpreview2bitmap.py passing "pdflatex" to the CONVERTER parameter
75 # Finally, there's also has a fallback method based on pdflatex, which
76 # is required in certain cases, if hyperref is active for instance,
78 # If possible, dvipng should be used, as it's much faster.
79 # If possible, the script will use pdftocairo instead of gs,
80 # as it's much faster and gives better results.
82 import glob, os, pipes, re, string, sys
84 from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
85 find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
86 latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \
87 run_command, run_latex, warning, write_metrics_info
91 return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n" \
92 "\twhere the colors are hexadecimal strings, eg 'faf0e6'" \
95 # Returns a list of tuples containing page number and ascent fraction
96 # extracted from dvipng output.
97 # Use write_metrics_info to create the .metrics file with this info
98 def legacy_extract_metrics_info(log_file):
100 log_re = re.compile("Preview: ([ST])")
101 data_re = re.compile("(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
109 for line in open(log_file, 'r').readlines():
110 match = log_re.match(line)
114 snippet = (match.group(1) == 'S')
116 match = data_re.search(line)
118 error("Unexpected data in %s\n%s" % (log_file, line))
121 ascent = string.atof(match.group(2))
122 descent = string.atof(match.group(3))
125 if ascent == 0 and descent == 0:
126 # This is an empty image, forbid its display
128 elif ascent >= 0 or descent >= 0:
129 ascent = ascent + tp_ascent
130 descent = descent - tp_descent
132 if abs(ascent + descent) > 0.1:
133 frac = ascent / (ascent + descent)
136 if frac < 0 or frac > 1:
139 results.append((int(match.group(1)), frac))
142 tp_descent = string.atof(match.group(2))
143 tp_ascent = string.atof(match.group(4))
146 # Unable to open the file, but do nothing here because
147 # the calling function will act on the value of 'success'.
148 warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file)
149 warning(`sys.exc_type` + ',' + `sys.exc_value`)
152 error("Failed to extract metrics info from %s" % log_file)
156 def extract_resolution(log_file, dpi):
157 fontsize_re = re.compile("Preview: Fontsize")
158 magnification_re = re.compile("Preview: Magnification")
159 extract_decimal_re = re.compile("([0-9\.]+)")
160 extract_integer_re = re.compile("([0-9]+)")
163 found_magnification = 0
166 magnification = 1000.0
170 for line in open(log_file, 'r').readlines():
171 if found_fontsize and found_magnification:
174 if not found_fontsize:
175 match = fontsize_re.match(line)
177 match = extract_decimal_re.search(line)
179 error("Unable to parse: %s" % line)
180 fontsize = string.atof(match.group(1))
184 if not found_magnification:
185 match = magnification_re.match(line)
187 match = extract_integer_re.search(line)
189 error("Unable to parse: %s" % line)
190 magnification = string.atof(match.group(1))
191 found_magnification = 1
195 warning('Warning in extract_resolution! Unable to open "%s"' % log_file)
196 warning(`sys.exc_type` + ',' + `sys.exc_value`)
198 # This is safe because both fontsize and magnification have
199 # non-zero default values.
200 return dpi * (10.0 / fontsize) * (1000.0 / magnification)
203 def legacy_latex_file(latex_file, fg_color, bg_color):
204 use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}")
205 fg_color_gr = make_texcolor(fg_color, True)
206 bg_color_gr = make_texcolor(bg_color, True)
212 f = open(latex_file, 'r')
214 # Unable to open the file, but do nothing here because
215 # the calling function will act on the value of 'success'.
216 warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
217 warning(`sys.exc_type` + ',' + `sys.exc_value`)
219 for line in f.readlines():
223 match = use_preview_re.match(line)
228 # Package order: color should be loaded before preview
229 # Preview options: add the options lyx and tightpage
232 \definecolor{fg}{rgb}{%s}
233 \definecolor{bg}{rgb}{%s}
235 \usepackage[%s,tightpage]{preview}
236 \IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecomp}}
238 \g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
239 \g@addto@macro\endpreview{\endgroup}
241 """ % (fg_color_gr, bg_color_gr, match.group(1)))
244 copyfileobj(tmp, open(latex_file,"wb"), 1)
249 def crop_files(pnmcrop, basename):
251 t.append('%s -left' % pnmcrop, '--')
252 t.append('%s -right' % pnmcrop, '--')
254 for file in glob.glob("%s*.ppm" % basename):
256 new = t.open(file, "r")
257 copyfileobj(new, tmp)
259 copyfileobj(tmp, open(file,"wb"), 1)
262 def legacy_conversion(argv, skipMetrics = False):
263 # Parse and manipulate the command line arguments.
267 error(usage(argv[0]))
271 dir, latex_file = os.path.split(argv[1])
275 dpi = string.atoi(argv[2])
277 output_format = argv[3]
282 # External programs used by the script.
283 latex = find_exe_or_terminate(latex or latex_commands)
285 pdf_output = latex in pdflatex_commands
287 return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
288 bg_color, latex, pdf_output, skipMetrics)
291 # Add color info to the latex file, since ghostscript doesn't
292 # have the option to set foreground and background colors on
293 # the command line. Run the resulting file through latex.
294 def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
295 latex, pdf_output = False, skipMetrics = False):
297 # Move color information, lyx and tightpage options into the latex file.
298 if not legacy_latex_file(latex_file, fg_color, bg_color):
299 error("""Unable to move the color information, and the lyx and tightpage
300 options of preview-latex, into the latex file""")
302 # Compile the latex file.
303 latex_status, latex_stdout = run_latex(latex, latex_file)
305 return (latex_status, [])
308 return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
310 return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
312 # Creates a new LaTeX file from the original with pages specified in
313 # failed_pages, pass it through pdflatex and updates the metrics
314 # from the standard legacy route
315 def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
316 use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution, output_format):
318 # Search for pdflatex executable
319 pdflatex = find_exe(["pdflatex"])
321 warning("Can't find pdflatex. Some pages failed with all the possible routes.")
323 # Create a new LaTeX file from the original but only with failed pages
324 pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
325 filter_pages(latex_file, pdf_latex_file, failed_pages)
328 pdflatex_status, pdflatex_stdout = run_latex(pdflatex, pdf_latex_file)
330 pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
331 latex_file_root = latex_file_re.sub("", pdf_latex_file)
333 # Converter call to produce bitmaps
335 conv_call = '%s -png -transp -r %d "%s" "%s"' \
336 % (conv, resolution, pdf_file, latex_file_root)
337 conv_status, conv_stdout = run_command(conv_call)
339 seqnum_re = re.compile("-([0-9]+)")
340 for name in glob.glob("%s-*.png" % latex_file_root):
341 match = seqnum_re.search(name)
343 new_name = seqnum_re.sub(str(int(match.group(1))), name)
344 os.rename(name, new_name)
346 conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
347 '-sOutputFile="%s%%d.%s" ' \
348 '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
350 % (conv, gs_device, latex_file_root, \
351 gs_ext, alpha, alpha, resolution, pdf_file)
352 conv_status, conv_stdout = run_command(conv_call)
356 warning("Some pages failed with all the possible routes")
359 pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
360 pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
362 original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
363 destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
365 # Join the metrics with the those from dvips and rename the bitmap images
366 join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
367 original_bitmap, destination_bitmap)
370 # The file has been processed through latex and we expect dvi output.
371 # Run dvips, taking note whether it was successful.
372 def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False):
373 # External programs used by the script.
374 dvips = find_exe_or_terminate(["dvips"])
376 # Run the dvi file through dvips.
377 dvi_file = latex_file_re.sub(".dvi", latex_file)
378 ps_file = latex_file_re.sub(".ps", latex_file)
380 dvips_call = '%s -i -o "%s" "%s"' % (dvips, ps_file, dvi_file)
383 dvips_status, dvips_stdout = run_command(dvips_call)
385 warning('Failed: %s %s ... looking for PDF' \
386 % (os.path.basename(dvips), dvi_file))
389 return legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics)
392 # Either latex and dvips have been run and we have a ps file, or
393 # pdflatex has been run and we have a pdf file. Proceed with pdftocairo or gs.
394 def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics = False):
395 # External programs used by the script.
396 gs = find_exe_or_terminate(["gswin32c", "gswin64c", "gs"])
397 pnmcrop = find_exe(["pnmcrop"])
398 pdftocairo = find_exe(["pdftocairo"])
399 epstopdf = find_exe(["epstopdf"])
400 use_pdftocairo = pdftocairo != None and output_format == "png"
407 pdf_file = latex_file_re.sub(".pdf", latex_file)
408 ps_file = latex_file_re.sub(".ps", latex_file)
410 # The latex file name without extension
411 latex_file_root = latex_file_re.sub("", latex_file)
413 # Extract resolution data for the converter from the log file.
414 log_file = latex_file_re.sub(".log", latex_file)
415 resolution = extract_resolution(log_file, dpi)
417 # Older versions of gs have problems with a large degree of
418 # anti-aliasing at high resolutions
425 if output_format == "ppm":
429 # Extract the metrics from the log file
430 legacy_metrics = legacy_extract_metrics_info(log_file)
432 # List of pages which failed to produce a correct output
435 # Generate the bitmap images
437 # dvips failed, maybe there's a PDF, try to produce bitmaps
439 conv_call = '%s -png -transp -r %d "%s" "%s"' \
440 % (pdftocairo, resolution, pdf_file, latex_file_root)
442 conv_status, conv_stdout = run_command(conv_call)
444 seqnum_re = re.compile("-([0-9]+)")
445 for name in glob.glob("%s-*.png" % latex_file_root):
446 match = seqnum_re.search(name)
448 new_name = seqnum_re.sub(str(int(match.group(1))), name)
449 os.rename(name, new_name)
451 conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
452 '-sOutputFile="%s%%d.%s" ' \
453 '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
455 % (gs, gs_device, latex_file_root, \
456 gs_ext, alpha, alpha, resolution, pdf_file)
458 conv_status, conv_stdout = run_command(conv_call)
461 error("Failed: %s %s" % (os.path.basename(conv), pdf_file))
463 # Model for calling the converter on each file
464 if use_pdftocairo and epstopdf != None:
465 conv_call = '%s -png -transp -singlefile -r %d "%%s.pdf" "%s%%d"' \
466 % (pdftocairo, resolution, latex_file_root)
468 conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
469 '-sOutputFile="%s%%d.%s" ' \
470 '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
472 % (gs, gs_device, latex_file_root, \
473 gs_ext, alpha, alpha, resolution)
476 # Collect all the PostScript files (like *.001, *.002, ...)
477 ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_root)
480 # Call the converter for each file
481 for file in ps_files:
483 progress("Processing page %s, file %s" % (i, file))
484 if use_pdftocairo and epstopdf != None:
485 conv_name = "pdftocairo"
486 conv_status, conv_stdout = run_command("%s --outfile=%s.pdf %s"
487 % (epstopdf, file, file))
489 conv_status, conv_stdout = run_command(conv_call % (file, i))
491 conv_name = "ghostscript"
492 conv_status, conv_stdout = run_command(conv_call % (i, file))
495 # The converter failed, keep track of this
496 warning("%s failed on page %s, file %s" % (conv_name, i, file))
497 failed_pages.append(i)
499 # Pass failed pages to pdflatex
500 if len(failed_pages) > 0:
501 warning("Now trying to obtain failed previews through pdflatex")
502 legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
503 use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution,
506 # Crop the ppm images
507 if pnmcrop != None and output_format == "ppm":
508 crop_files(pnmcrop, latex_file_root)
510 # Allow to skip .metrics creation for custom management
511 # (see the dvipng method)
513 # Extract metrics info from the log file.
514 metrics_file = latex_file_re.sub(".metrics", latex_file)
515 write_metrics_info(legacy_metrics, metrics_file)
517 return (0, legacy_metrics)
520 if __name__ == "__main__":
521 sys.exit(legacy_conversion(sys.argv)[0])