]> git.lyx.org Git - lyx.git/blob - lib/scripts/legacy_lyxpreview2ppm.py
remerge he.po
[lyx.git] / lib / scripts / legacy_lyxpreview2ppm.py
1 # -*- coding: utf-8 -*-
2
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.
6
7 # author Angus Leeming
8 # Full author contact details are available in file CREDITS
9
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.
15
16 # This script takes a LaTeX file and generates a collection of
17 # png or ppm image files, one per previewed snippet.
18 # Example usage:
19 # legacy_lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm 000000 faf0e6
20
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'.
29
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.
36
37 # The script uses several external programs and files:
38 # * python 2.4 or later (subprocess module);
39 # * A latex executable;
40 # * preview.sty;
41 # * dvips;
42 # * gs;
43 # * pdflatex (optional);
44 # * pnmcrop (optional).
45 # * pdftocairo (optional).
46 # * epstopdf (optional).
47
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/
52
53 # What does this script do?
54 # [legacy_conversion]
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)
68
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
74 # (step 3).
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,
77 # (step 5, 6).
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.
81
82 import glob, os, pipes, re, sys, tempfile
83
84 from lyxpreview_tools import check_latex_log, copyfileobj, error, filter_pages,\
85      find_exe, find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
86      latex_file_re, make_texcolor, pdflatex_commands, progress, \
87      run_command, run_latex, warning, write_metrics_info
88
89
90 def usage(prog_name):
91     return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n" \
92            "\twhere the colors are hexadecimal strings, eg 'faf0e6'" \
93            % prog_name
94
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):
99
100     log_re = re.compile(b"Preview: ([ST])")
101     data_re = re.compile(b"(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
102
103     tp_ascent  = 0.0
104     tp_descent = 0.0
105
106     success = 0
107     results = []
108     try:
109         for line in open(log_file, 'rb').readlines():
110             match = log_re.match(line)
111             if match == None:
112                 continue
113
114             snippet = (match.group(1) == b'S')
115             success = 1
116             match = data_re.search(line)
117             if match == None:
118                 error("Unexpected data in %s\n%s" % (log_file, line))
119
120             if snippet:
121                 ascent  = float(match.group(2))
122                 descent = float(match.group(3))
123
124                 frac = 0.5
125                 if ascent == 0 and descent == 0:
126                     # This is an empty image, forbid its display
127                     frac = -1.0
128                 elif ascent >= 0 or descent >= 0:
129                     ascent = ascent + tp_ascent
130                     descent = descent - tp_descent
131
132                     if abs(ascent + descent) > 0.1:
133                         frac = ascent / (ascent + descent)
134
135                     # Sanity check
136                     if frac < 0 or frac > 1:
137                             frac = 0.5
138
139                 results.append((int(match.group(1)), frac))
140
141             else:
142                 tp_descent = float(match.group(2))
143                 tp_ascent  = float(match.group(4))
144
145     except:
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(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
150
151     if success == 0:
152         error("Failed to extract metrics info from %s" % log_file)
153
154     return results
155
156 def extract_resolution(log_file, dpi):
157     fontsize_re = re.compile(b"Preview: Fontsize")
158     magnification_re = re.compile(b"Preview: Magnification")
159     extract_decimal_re = re.compile(br"([0-9\.]+)")
160     extract_integer_re = re.compile(b"([0-9]+)")
161
162     found_fontsize = 0
163     found_magnification = 0
164
165     # Default values
166     magnification = 1000.0
167     fontsize = 10.0
168
169     try:
170         for line in open(log_file, 'rb').readlines():
171             if found_fontsize and found_magnification:
172                 break
173
174             if not found_fontsize:
175                 match = fontsize_re.match(line)
176                 if match != None:
177                     match = extract_decimal_re.search(line)
178                     if match == None:
179                         error("Unable to parse: %s" % line)
180                     fontsize = float(match.group(1))
181                     found_fontsize = 1
182                     continue
183
184             if not found_magnification:
185                 match = magnification_re.match(line)
186                 if match != None:
187                     match = extract_integer_re.search(line)
188                     if match == None:
189                         error("Unable to parse: %s" % line)
190                     magnification = float(match.group(1))
191                     found_magnification = 1
192                     continue
193
194     except:
195         warning('Warning in extract_resolution! Unable to open "%s"' % log_file)
196         warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
197
198     # This is safe because both fontsize and magnification have
199     # non-zero default values.
200     return dpi * (10.0 / fontsize) * (1000.0 / magnification)
201
202
203 def legacy_latex_file(latex_file, fg_color, bg_color):
204     use_polyglossia_re = re.compile(b"\\s*\\\\usepackage{polyglossia}")
205     use_preview_re = re.compile(b"\\s*\\\\usepackage\\[([^]]+)\\]{preview}")
206     fg_color_gr = make_texcolor(fg_color, True)
207     bg_color_gr = make_texcolor(bg_color, True)
208
209     tmp = tempfile.TemporaryFile()
210
211     success = 0
212     try:
213         f = open(latex_file, 'rb')
214     except:
215         # Unable to open the file, but do nothing here because
216         # the calling function will act on the value of 'success'.
217         warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
218         warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
219
220     polyglossia = False
221     for line in f.readlines():
222         if success:
223             tmp.write(line)
224             continue
225         match = use_preview_re.match(line)
226         polymatch = use_polyglossia_re.match(line)
227         # Package order:
228         # * if polyglossia is used, we need to load color before that
229         #   (also, we do not have to load lmodern)
230         # * else, color should be loaded before preview
231         if match == None:
232             if polymatch == None:
233                 tmp.write(line)
234                 continue
235             else:
236                 tmp.write(b"""
237 \\usepackage{color}
238 \\definecolor{lyxfg}{rgb}{%s}
239 \\definecolor{lyxbg}{rgb}{%s}
240 \\pagecolor{lyxbg}
241 \\usepackage{polyglossia}
242 """ % (fg_color_gr, bg_color_gr))
243                 polyglossia = True
244                 continue
245         success = 1
246         # Preview options: add the options lyx and tightpage
247         previewopts = match.group(1)
248         if not polyglossia:
249             tmp.write(b"""
250 \\usepackage{color}
251 \\definecolor{lyxfg}{rgb}{%s}
252 \\definecolor{lyxbg}{rgb}{%s}
253 \\pagecolor{lyxbg}
254 \\usepackage[%s,tightpage]{preview}
255 \\usepackage{ifthen}
256 \\makeatletter
257 \\ifthenelse{\\equal{\\f@family}{cmr}}{
258 \\IfFileExists{lmodern.sty}{\\usepackage{lmodern}}{\\usepackage{ae,aecompl}}}{}
259 \\g@addto@macro\\preview{\\begingroup\\color{lyxbg}\\special{ps::clippath fill}\\color{lyxfg}}
260 \\g@addto@macro\\endpreview{\\endgroup}
261 \\makeatother
262 """ % (fg_color_gr, bg_color_gr, previewopts))
263         else:
264             tmp.write(b"""
265 \\usepackage[%s,tightpage]{preview}
266 \\makeatletter
267 \\g@addto@macro\\preview{\\begingroup\\color{lyxbg}\\special{ps::clippath fill}\\color{lyxfg}}
268 \\g@addto@macro\\endpreview{\\endgroup}
269 \\makeatother
270 """ % previewopts)
271     if success:
272         copyfileobj(tmp, open(latex_file,"wb"), 1)
273
274     return success
275
276
277 def crop_files(pnmcrop, basename):
278     t = pipes.Template()
279     t.append('%s -left' % pnmcrop, '--')
280     t.append('%s -right' % pnmcrop, '--')
281
282     for file in glob.glob("%s*.ppm" % basename):
283         tmp = tempfile.TemporaryFile()
284         new = t.open(file, "r")
285         copyfileobj(new, tmp)
286         if not new.close():
287             copyfileobj(tmp, open(file,"wb"), 1)
288
289
290 def legacy_conversion(argv, skipMetrics = False):
291     # Parse and manipulate the command line arguments.
292     if len(argv) == 7:
293         latex = [argv[6]]
294     elif len(argv) != 6:
295         error(usage(argv[0]))
296     else:
297         latex = None
298
299     dir, latex_file = os.path.split(argv[1])
300     if len(dir) != 0:
301         os.chdir(dir)
302
303     dpi = int(argv[2])
304
305     output_format = argv[3]
306
307     fg_color = argv[4]
308     bg_color = argv[5]
309
310     # External programs used by the script.
311     latex = find_exe_or_terminate(latex or latex_commands)
312
313     pdf_output = latex in pdflatex_commands
314
315     return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
316         bg_color, latex, pdf_output, skipMetrics)
317
318
319 # Add color info to the latex file, since ghostscript doesn't
320 # have the option to set foreground and background colors on
321 # the command line. Run the resulting file through latex.
322 def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
323                             latex, pdf_output = False, skipMetrics = False):
324
325     # Move color information, lyx and tightpage options into the latex file.
326     if not legacy_latex_file(latex_file, fg_color, bg_color):
327         error("""Unable to move the color information, and the lyx and tightpage
328             options of preview-latex, into the latex file""")
329
330     # Compile the latex file.
331     latex_status, latex_stdout = run_latex(latex, latex_file)
332     if latex_status:
333         progress("Will try to recover from %s failure" % latex)
334
335     if pdf_output:
336         return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
337     else:
338         return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
339
340 # Creates a new LaTeX file from the original with pages specified in
341 # failed_pages, pass it through pdflatex and updates the metrics
342 # from the standard legacy route
343 def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
344     use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution, output_format):
345
346     error_count = 0
347
348     # Search for pdflatex executable
349     pdflatex = find_exe(["pdflatex"])
350     if pdflatex == None:
351         warning("Can't find pdflatex. Some pages failed with all the possible routes.")
352         failed_pages = []
353     else:
354         # Create a new LaTeX file from the original but only with failed pages
355         pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
356         filter_pages(latex_file, pdf_latex_file, failed_pages)
357
358         # pdflatex call
359         error_pages = []
360         pdflatex_status, pdflatex_stdout = run_latex(pdflatex, pdf_latex_file)
361         if pdflatex_status:
362             error_pages = check_latex_log(latex_file_re.sub(".log", pdf_latex_file))
363
364         pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
365         latex_file_root = latex_file_re.sub("", pdf_latex_file)
366
367         # Converter call to produce bitmaps
368         if use_pdftocairo:
369             conv_call = '%s -png -transp -r %d "%s" "%s"' \
370                         % (conv, resolution, pdf_file, latex_file_root)
371             conv_status, conv_stdout = run_command(conv_call)
372             if not conv_status:
373                 seqnum_re = re.compile("-([0-9]+)")
374                 for name in glob.glob("%s-*.png" % latex_file_root):
375                     match = seqnum_re.search(name)
376                     if match != None:
377                         new_name = seqnum_re.sub(str(int(match.group(1))), name)
378                         os.rename(name, new_name)
379         else:
380             conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
381                         '-sOutputFile="%s%%d.%s" ' \
382                         '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
383                         '-r%f "%s"' \
384                         % (conv, gs_device, latex_file_root, \
385                             gs_ext, alpha, alpha, resolution, pdf_file)
386             conv_status, conv_stdout = run_command(conv_call)
387
388         if conv_status:
389             # Give up!
390             warning("Some pages failed with all the possible routes")
391             failed_pages = []
392         else:
393             # We've done it!
394             pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
395             pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
396
397             # Invalidate metrics for pages that produced errors
398             if len(error_pages) > 0:
399                 for index in error_pages:
400                     pdf_metrics.pop(index - 1)
401                     pdf_metrics.insert(index - 1, (index, -1.0))
402                     error_count += 1
403
404             original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
405             destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
406
407             # Join the metrics with the those from dvips and rename the bitmap images
408             join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
409                 original_bitmap, destination_bitmap)
410
411     return error_count
412
413
414 # The file has been processed through latex and we expect dvi output.
415 # Run dvips, taking note whether it was successful.
416 def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False):
417     # External programs used by the script.
418     dvips   = find_exe_or_terminate(["dvips"])
419
420     # Run the dvi file through dvips.
421     dvi_file = latex_file_re.sub(".dvi", latex_file)
422     ps_file  = latex_file_re.sub(".ps",  latex_file)
423
424     dvips_call = '%s -i -o "%s" "%s"' % (dvips, ps_file, dvi_file)
425     dvips_failed = False
426
427     dvips_status, dvips_stdout = run_command(dvips_call)
428     if dvips_status:
429         warning('Failed: %s %s ... looking for PDF' \
430             % (os.path.basename(dvips), dvi_file))
431         dvips_failed = True
432
433     return legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics)
434
435
436 # Either latex and dvips have been run and we have a ps file, or
437 # pdflatex has been run and we have a pdf file. Proceed with pdftocairo or gs.
438 def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMetrics = False):
439     # External programs used by the script.
440     gs      = find_exe_or_terminate(["gswin32c", "gswin64c", "gs"])
441     pnmcrop = find_exe(["pnmcrop"])
442     pdftocairo = find_exe(["pdftocairo"])
443     epstopdf   = find_exe(["epstopdf"])
444     use_pdftocairo = pdftocairo != None and output_format == "png"
445     if use_pdftocairo and os.name == 'nt':
446         # On Windows, check for png support (see #10718)
447         conv_status, conv_stdout = run_command("%s --help" % pdftocairo)
448         use_pdftocairo = '-png' in conv_stdout
449     if use_pdftocairo:
450         conv = pdftocairo
451     else:
452         conv = gs
453
454     # Files to process
455     pdf_file  = latex_file_re.sub(".pdf", latex_file)
456     ps_file  = latex_file_re.sub(".ps",  latex_file)
457
458     # The latex file name without extension
459     latex_file_root = latex_file_re.sub("", latex_file)
460
461     # Extract resolution data for the converter from the log file.
462     log_file = latex_file_re.sub(".log", latex_file)
463     resolution = extract_resolution(log_file, dpi)
464
465     # Check whether some pages produced errors
466     error_pages = check_latex_log(log_file)
467
468     # Older versions of gs have problems with a large degree of
469     # anti-aliasing at high resolutions
470     alpha = 4
471     if resolution > 150:
472         alpha = 2
473
474     gs_device = "png16m"
475     gs_ext = "png"
476     if output_format == "ppm":
477         gs_device = "pnmraw"
478         gs_ext = "ppm"
479
480     # Extract the metrics from the log file
481     legacy_metrics = legacy_extract_metrics_info(log_file)
482
483     # List of pages which failed to produce a correct output
484     failed_pages = []
485
486     # Generate the bitmap images
487     if dvips_failed:
488         # dvips failed, maybe there's a PDF, try to produce bitmaps
489         if use_pdftocairo:
490             conv_call = '%s -png -transp -r %d "%s" "%s"' \
491                         % (pdftocairo, resolution, pdf_file, latex_file_root)
492
493             conv_status, conv_stdout = run_command(conv_call)
494             if not conv_status:
495                 seqnum_re = re.compile("-([0-9]+)")
496                 for name in glob.glob("%s-*.png" % latex_file_root):
497                     match = seqnum_re.search(name)
498                     if match != None:
499                         new_name = seqnum_re.sub(str(int(match.group(1))), name)
500                         os.rename(name, new_name)
501         else:
502             conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
503                       '-sOutputFile="%s%%d.%s" ' \
504                       '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
505                       '-r%f "%s"' \
506                       % (gs, gs_device, latex_file_root, \
507                          gs_ext, alpha, alpha, resolution, pdf_file)
508
509             conv_status, conv_stdout = run_command(conv_call)
510
511         if conv_status:
512             error("Failed: %s %s" % (os.path.basename(conv), pdf_file))
513     else:
514         # Model for calling the converter on each file
515         if use_pdftocairo and epstopdf != None:
516             conv_call = '%s -png -transp -singlefile -r %d "%%s" "%s%%d"' \
517                         % (pdftocairo, resolution, latex_file_root)
518         else:
519             conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
520                         '-sOutputFile="%s%%d.%s" ' \
521                         '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
522                         '-r%f "%%s"' \
523                         % (gs, gs_device, latex_file_root, \
524                            gs_ext, alpha, alpha, resolution)
525
526         i = 0
527         # Collect all the PostScript files (like *.001, *.002, ...)
528         ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_root)
529         ps_files.sort()
530
531         # Call the converter for each file
532         for file in ps_files:
533             i = i + 1
534             progress("Processing page %s, file %s" % (i, file))
535             if use_pdftocairo and epstopdf != None:
536                 conv_name = "epstopdf"
537                 conv_status, conv_stdout = run_command("%s --outfile=%s.pdf %s"
538                                                        % (epstopdf, file, file))
539                 if not conv_status:
540                     conv_name = "pdftocairo"
541                     file = file + ".pdf"
542                     conv_status, conv_stdout = run_command(conv_call % (file, i))
543             else:
544                 conv_name = "ghostscript"
545                 conv_status, conv_stdout = run_command(conv_call % (i, file))
546
547             if conv_status:
548                 # The converter failed, keep track of this
549                 warning("%s failed on page %s, file %s" % (conv_name, i, file))
550                 failed_pages.append(i)
551
552     # Pass failed pages to pdflatex
553     if len(failed_pages) > 0:
554         warning("Now trying to obtain failed previews through pdflatex")
555         error_count = legacy_conversion_pdflatex(latex_file, failed_pages,
556             legacy_metrics, use_pdftocairo, conv, gs_device, gs_ext, alpha,
557             resolution, output_format)
558     else:
559         error_count = 0
560
561     # Invalidate metrics for pages that produced errors
562     if len(error_pages) > 0:
563         for index in error_pages:
564             if index not in failed_pages:
565                 legacy_metrics.pop(index - 1)
566                 legacy_metrics.insert(index - 1, (index, -1.0))
567                 error_count += 1
568
569     # Crop the ppm images
570     if pnmcrop != None and output_format == "ppm":
571         crop_files(pnmcrop, latex_file_root)
572
573     # Allow to skip .metrics creation for custom management
574     # (see the dvipng method)
575     if not skipMetrics:
576         # Extract metrics info from the log file.
577         metrics_file = latex_file_re.sub(".metrics", latex_file)
578         write_metrics_info(legacy_metrics, metrics_file)
579         if error_count:
580             warning("Failed to produce %d preview snippet(s)" % error_count)
581
582     return (0, legacy_metrics)
583
584
585 if __name__ == "__main__":
586     sys.exit(legacy_conversion(sys.argv)[0])