2 # -*- coding: utf-8 -*-
4 # file legacy_lyxpreview2ppm.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
9 # Full author contact details are available in file CREDITS
11 # with much advice from members of the preview-latex project:
12 # David Kastrup, dak@gnu.org and
13 # Jan-Åke Larsson, jalar@mai.liu.se.
14 # and with much help testing the code under Windows from
15 # Paul A. Rubin, rubin@msu.edu.
17 # This script takes a LaTeX file and generates a collection of
18 # png or ppm image files, one per previewed snippet.
20 # legacy_lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm 000000 faf0e6
22 # This script takes five arguments:
23 # TEXFILE: the name of the .tex file to be converted.
24 # SCALEFACTOR: a scale factor, used to ascertain the resolution of the
25 # generated image which is then passed to gs.
26 # OUTPUTFORMAT: the format of the output bitmap image files.
27 # This particular script can produce only "ppm" format output.
28 # FG_COLOR: the foreground color as a hexadecimal string, eg '000000'.
29 # BG_COLOR: the background color as a hexadecimal string, eg 'faf0e6'.
31 # Decomposing TEXFILE's name as DIR/BASE.tex, this script will,
32 # if executed successfully, leave in DIR:
33 # * a (possibly large) number of image files with names
34 # like BASE[0-9]+.(ppm|png)
35 # * a file BASE.metrics, containing info needed by LyX to position
36 # the images correctly on the screen.
38 # The script uses several external programs and files:
39 # * python 2.4 or later (subprocess module);
40 # * A latex executable;
44 # * pdflatex (optional);
45 # * pnmcrop (optional).
47 # preview.sty is part of the preview-latex project
48 # http://preview-latex.sourceforge.net/
49 # Alternatively, it can be obtained from
50 # CTAN/support/preview-latex/
52 # What does this script do?
54 # 0) Process command-line arguments
55 # [legacy_conversion_step1]
56 # 1) Call latex to create a DVI file from LaTeX
57 # [legacy_conversion_step2]
58 # 2) Call dvips to create one PS file for each DVI page
59 # [legacy_conversion_step3]
60 # 3) If dvips fails look for PDF and call gs to produce bitmaps
61 # 4) Otherwise call gs on each PostScript file to produce bitmaps
62 # [legacy_conversion_pdflatex]
63 # 5) Keep track of pages on which gs failed and pass them to pdflatex
64 # 6) Call gs on the PDF output from pdflatex to produce bitmaps
65 # 7) Extract and write to file (or return to lyxpreview2bitmap)
66 # metrics from both methods (standard and pdflatex)
68 # The script uses the old dvi->ps->png conversion route,
69 # which is good when using PSTricks, TikZ or other packages involving
70 # PostScript literals (steps 1, 2, 4).
71 # This script also generates bitmaps from PDF created by a call to
72 # lyxpreview2bitmap.py passing "pdflatex" to the CONVERTER parameter
74 # Finally, there's also has a fallback method based on pdflatex, which
75 # is required in certain cases, if hyperref is active for instance,
77 # If possible, dvipng should be used, as it's much faster.
79 import glob, os, pipes, re, string, sys
81 from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
82 find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
83 latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \
84 run_command, warning, write_metrics_info
88 return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n" \
89 "\twhere the colors are hexadecimal strings, eg 'faf0e6'" \
92 # Returns a list of tuples containing page number and ascent fraction
93 # extracted from dvipng output.
94 # Use write_metrics_info to create the .metrics file with this info
95 def legacy_extract_metrics_info(log_file):
97 log_re = re.compile("Preview: ([ST])")
98 data_re = re.compile("(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
106 for line in open(log_file, 'r').readlines():
107 match = log_re.match(line)
111 snippet = (match.group(1) == 'S')
113 match = data_re.search(line)
115 error("Unexpected data in %s\n%s" % (log_file, line))
118 ascent = string.atoi(match.group(2))
119 descent = string.atoi(match.group(3))
122 if ascent >= 0 and descent >= 0:
123 ascent = float(ascent) + tp_ascent
124 descent = float(descent) - tp_descent
126 if abs(ascent + descent) > 0.1:
127 frac = ascent / (ascent + descent)
130 if frac < 0 or frac > 1:
133 results.append((int(match.group(1)), frac))
136 tp_descent = string.atof(match.group(2))
137 tp_ascent = string.atof(match.group(4))
140 # Unable to open the file, but do nothing here because
141 # the calling function will act on the value of 'success'.
142 warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file)
143 warning(`sys.exc_type` + ',' + `sys.exc_value`)
146 error("Failed to extract metrics info from %s" % log_file)
150 def extract_resolution(log_file, dpi):
151 fontsize_re = re.compile("Preview: Fontsize")
152 magnification_re = re.compile("Preview: Magnification")
153 extract_decimal_re = re.compile("([0-9\.]+)")
154 extract_integer_re = re.compile("([0-9]+)")
157 found_magnification = 0
160 magnification = 1000.0
164 for line in open(log_file, 'r').readlines():
165 if found_fontsize and found_magnification:
168 if not found_fontsize:
169 match = fontsize_re.match(line)
171 match = extract_decimal_re.search(line)
173 error("Unable to parse: %s" % line)
174 fontsize = string.atof(match.group(1))
178 if not found_magnification:
179 match = magnification_re.match(line)
181 match = extract_integer_re.search(line)
183 error("Unable to parse: %s" % line)
184 magnification = string.atof(match.group(1))
185 found_magnification = 1
189 warning('Warning in extract_resolution! Unable to open "%s"' % log_file)
190 warning(`sys.exc_type` + ',' + `sys.exc_value`)
192 # This is safe because both fontsize and magnification have
193 # non-zero default values.
194 return dpi * (10.0 / fontsize) * (1000.0 / magnification)
197 def legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
198 use_preview_dvi_re = re.compile("(\s*\\\\usepackage\[[^]]+)(dvips\]{preview})")
199 use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)(pdftex\]{preview})")
205 for line in open(latex_file, 'r').readlines():
206 match = use_preview_dvi_re.match(line)
208 match = use_preview_pdf_re.match(line)
213 tmp.write(" \\usepackage{color}\n" \
214 " \\pagecolor[rgb]{%s}\n" \
216 % (bg_color_gr, match.group()))
220 tmp.write("%stightpage,%s\n" \
221 " \\AtBeginDocument{\\AtBeginDvi{%%\n" \
222 " \\special{!userdict begin/bop-hook{//bop-hook exec\n" \
223 " <%s%s>{255 div}forall setrgbcolor\n" \
224 " clippath fill setrgbcolor}bind def end}}}\n" \
225 % (match.group(1), match.group(2), fg_color, bg_color))
228 # Unable to open the file, but do nothing here because
229 # the calling function will act on the value of 'success'.
230 warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
231 warning(`sys.exc_type` + ',' + `sys.exc_value`)
234 copyfileobj(tmp, open(latex_file,"wb"), 1)
239 def crop_files(pnmcrop, basename):
241 t.append('%s -left' % pnmcrop, '--')
242 t.append('%s -right' % pnmcrop, '--')
244 for file in glob.glob("%s*.ppm" % basename):
246 new = t.open(file, "r")
247 copyfileobj(new, tmp)
249 copyfileobj(tmp, open(file,"wb"), 1)
252 def legacy_conversion(argv, skipMetrics = False):
253 # Parse and manipulate the command line arguments.
257 error(usage(argv[0]))
261 dir, latex_file = os.path.split(argv[1])
265 dpi = string.atoi(argv[2])
267 output_format = argv[3]
272 # External programs used by the script.
273 latex = find_exe_or_terminate(latex or latex_commands)
275 pdf_output = latex in pdflatex_commands
277 return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
278 bg_color, latex, pdf_output, skipMetrics)
281 # Add color info to the latex file, since ghostscript doesn't
282 # have the option to set foreground and background colors on
283 # the command line. Run the resulting file through latex.
284 def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
285 latex, pdf_output = False, skipMetrics = False):
287 # Move color information into the latex file.
288 bg_color_gr = make_texcolor(bg_color, True)
289 if not legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
290 error("Unable to move color info into the latex file")
292 # Compile the latex file.
293 latex_call = '%s "%s"' % (latex, latex_file)
295 latex_status, latex_stdout = run_command(latex_call)
297 warning("%s had problems compiling %s" \
298 % (os.path.basename(latex), latex_file))
301 return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
303 return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
305 # Creates a new LaTeX file from the original with pages specified in
306 # failed_pages, pass it through pdflatex and updates the metrics
307 # from the standard legacy route
308 def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
309 gs_device, gs_ext, alpha, resolution, output_format):
311 # Search for pdflatex executable
312 pdflatex = find_exe(["pdflatex"])
314 warning("Can't find pdflatex. Some pages failed with all the possible routes.")
316 # Create a new LaTeX file from the original but only with failed pages
317 pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
318 filter_pages(latex_file, pdf_latex_file, failed_pages)
321 pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file)
322 pdflatex_status, pdflatex_stdout = run_command(pdflatex_call)
324 warning("%s had problems compiling %s" \
325 % (os.path.basename(pdflatex), pdf_latex_file))
327 pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
329 # GhostScript call to produce bitmaps
330 gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
331 '-sOutputFile="%s%%d.%s" ' \
332 '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
334 % (gs, gs_device, latex_file_re.sub("", pdf_latex_file), \
335 gs_ext, alpha, alpha, resolution, pdf_file)
336 gs_status, gs_stdout = run_command(gs_call)
339 warning("Some pages failed with all the possible routes")
342 pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
343 pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
345 original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
346 destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
348 # Join the metrics with the those from dvips and rename the bitmap images
349 join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
350 original_bitmap, destination_bitmap)
353 # The file has been processed through latex and we expect dvi output.
354 # Run dvips, taking note whether it was successful.
355 def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False):
356 # External programs used by the script.
357 dvips = find_exe_or_terminate(["dvips"])
359 # Run the dvi file through dvips.
360 dvi_file = latex_file_re.sub(".dvi", latex_file)
361 ps_file = latex_file_re.sub(".ps", latex_file)
363 dvips_call = '%s -i -o "%s" "%s"' % (dvips, ps_file, dvi_file)
366 dvips_status, dvips_stdout = run_command(dvips_call)
368 warning('Failed: %s %s ... looking for PDF' \
369 % (os.path.basename(dvips), dvi_file))
372 return legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics)
375 # Either latex and dvips have been run and we have a ps file, or
376 # pdflatex has been run and we have a pdf file. Proceed with gs.
377 def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics = False):
378 # External programs used by the script.
379 gs = find_exe_or_terminate(["gswin32c", "gs"])
380 pnmcrop = find_exe(["pnmcrop"])
383 pdf_file = latex_file_re.sub(".pdf", latex_file)
384 ps_file = latex_file_re.sub(".ps", latex_file)
386 # Extract resolution data for gs from the log file.
387 log_file = latex_file_re.sub(".log", latex_file)
388 resolution = extract_resolution(log_file, dpi)
390 # Older versions of gs have problems with a large degree of
391 # anti-aliasing at high resolutions
398 if output_format == "ppm":
402 # Extract the metrics from the log file
403 legacy_metrics = legacy_extract_metrics_info(log_file)
405 # List of pages which failed to produce a correct output
408 # Generate the bitmap images
410 # dvips failed, maybe there's a PDF, try to produce bitmaps
411 gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
412 '-sOutputFile="%s%%d.%s" ' \
413 '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
415 % (gs, gs_device, latex_file_re.sub("", latex_file), \
416 gs_ext, alpha, alpha, resolution, pdf_file)
418 gs_status, gs_stdout = run_command(gs_call)
420 error("Failed: %s %s" % (os.path.basename(gs), ps_file))
422 # Model for calling gs on each file
423 gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
424 '-sOutputFile="%s%%d.%s" ' \
425 '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
427 % (gs, gs_device, latex_file_re.sub("", latex_file), \
428 gs_ext, alpha, alpha, resolution)
431 # Collect all the PostScript files (like *.001, *.002, ...)
432 ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file))
435 # Call GhostScript for each file
436 for file in ps_files:
438 progress("Processing page %s, file %s" % (i, file))
439 gs_status, gs_stdout = run_command(gs_call % (i, file))
441 # gs failed, keep track of this
442 warning("Ghostscript failed on page %s, file %s" % (i, file))
443 failed_pages.append(i)
445 # Pass failed pages to pdflatex
446 if len(failed_pages) > 0:
447 legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
448 gs_device, gs_ext, alpha, resolution, output_format)
452 crop_files(pnmcrop, latex_file_re.sub("", latex_file))
454 # Allow to skip .metrics creation for custom management
455 # (see the dvipng method)
457 # Extract metrics info from the log file.
458 metrics_file = latex_file_re.sub(".metrics", latex_file)
459 write_metrics_info(legacy_metrics, metrics_file)
461 return (0, legacy_metrics)
464 if __name__ == "__main__":
465 exit(legacy_conversion(sys.argv)[0])