]> git.lyx.org Git - features.git/blob - lib/scripts/legacy_lyxpreview2ppm.py
revert r37696 and apply a fallback mechanism to pdflatex
[features.git] / lib / scripts / legacy_lyxpreview2ppm.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
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.
7
8 # author Angus Leeming
9 # Full author contact details are available in file CREDITS
10
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.
16
17 # This script takes a LaTeX file and generates a collection of
18 # ppm image files, one per previewed snippet.
19 # Example usage:
20 # legacy_lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm 000000 faf0e6
21
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'.
30
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
35 # * a file BASE.metrics, containing info needed by LyX to position
36 #   the images correctly on the screen.
37
38 # The script uses several external programs and files:
39 # * A latex executable;
40 # * preview.sty;
41 # * dvips;
42 # * gs;
43 # * pnmcrop (optional).
44
45 # preview.sty is part of the preview-latex project
46 #   http://preview-latex.sourceforge.net/
47 # Alternatively, it can be obtained from
48 #   CTAN/support/preview-latex/
49
50 # The script uses the deprecated dvi->ps->ppm conversion route.
51 # If possible, please grab 'dvipng'; it's faster and more robust.
52 # If you have it then this script will not be invoked by
53 # lyxpreview2bitmap.py.
54 # Warning: this legacy support will be removed one day...
55
56 import glob, os, pipes, re, string, sys
57
58 from lyxpreview_tools import copyfileobj, error, find_exe, \
59      find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \
60      write_metrics_info, filter_pages, join_metrics_and_rename
61
62 # Pre-compiled regular expression.
63 latex_file_re = re.compile("\.tex$")
64
65
66 def usage(prog_name):
67     return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n"\
68            "\twhere the colors are hexadecimal strings, eg 'faf0e6'"\
69            % prog_name
70
71 # Returns a list of tuples containing page number and ascent fraction
72 # extracted from dvipng output.
73 # Use write_metrics_info to create the .metrics file with this info
74 def legacy_extract_metrics_info(log_file):
75
76     log_re = re.compile("Preview: ([ST])")
77     data_re = re.compile("(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
78
79     tp_ascent  = 0.0
80     tp_descent = 0.0
81
82     success = 0
83     results = []
84     try:
85         for line in open(log_file, 'r').readlines():
86             match = log_re.match(line)
87             if match == None:
88                 continue
89
90             snippet = (match.group(1) == 'S')
91             success = 1
92             match = data_re.search(line)
93             if match == None:
94                 error("Unexpected data in %s\n%s" % (log_file, line))
95
96             if snippet:
97                 ascent  = string.atoi(match.group(2))
98                 descent = string.atoi(match.group(3))
99
100                 frac = 0.5
101                 if ascent >= 0 and descent >= 0:
102                     ascent = float(ascent) + tp_ascent
103                     descent = float(descent) - tp_descent
104
105                     if abs(ascent + descent) > 0.1:
106                         frac = ascent / (ascent + descent)
107
108                     # Sanity check
109                     if frac < 0 or frac > 1:
110                             frac = 0.5
111
112                 results.append((int(match.group(1)), frac))
113
114             else:
115                 tp_descent = string.atof(match.group(2))
116                 tp_ascent  = string.atof(match.group(4))
117
118     except:
119         # Unable to open the file, but do nothing here because
120         # the calling function will act on the value of 'success'.
121         warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file)
122         warning(`sys.exc_type` + ',' + `sys.exc_value`)
123                 
124     if success == 0:
125         error("Failed to extract metrics info from %s" % log_file)
126         
127     return results
128
129 def extract_resolution(log_file, dpi):
130     fontsize_re = re.compile("Preview: Fontsize")
131     magnification_re = re.compile("Preview: Magnification")
132     extract_decimal_re = re.compile("([0-9\.]+)")
133     extract_integer_re = re.compile("([0-9]+)")
134
135     found_fontsize = 0
136     found_magnification = 0
137
138     # Default values
139     magnification = 1000.0
140     fontsize = 10.0
141
142     try:
143         for line in open(log_file, 'r').readlines():
144             if found_fontsize and found_magnification:
145                 break
146
147             if not found_fontsize:
148                 match = fontsize_re.match(line)
149                 if match != None:
150                     match = extract_decimal_re.search(line)
151                     if match == None:
152                         error("Unable to parse: %s" % line)
153                     fontsize = string.atof(match.group(1))
154                     found_fontsize = 1
155                     continue
156
157             if not found_magnification:
158                 match = magnification_re.match(line)
159                 if match != None:
160                     match = extract_integer_re.search(line)
161                     if match == None:
162                         error("Unable to parse: %s" % line)
163                     magnification = string.atof(match.group(1))
164                     found_magnification = 1
165                     continue
166
167     except:
168         warning('Warning in extract_resolution! Unable to open "%s"' % log_file)
169         warning(`sys.exc_type` + ',' + `sys.exc_value`)
170
171     # This is safe because both fontsize and magnification have
172     # non-zero default values.
173     return dpi * (10.0 / fontsize) * (1000.0 / magnification)
174
175
176 def legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
177     use_preview_dvi_re = re.compile("(\s*\\\\usepackage\[[^]]+)(dvips\]{preview})")
178     use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)(pdftex\]{preview})")
179
180     tmp = mkstemp()
181
182     success = 0
183     try:
184         for line in open(latex_file, 'r').readlines():
185             match = use_preview_dvi_re.match(line)
186             if match == None:
187                 match = use_preview_pdf_re.match(line)
188                 if match == None:
189                     tmp.write(line)
190                     continue
191                 success = 1
192                 tmp.write("  \\usepackage{color}\n" \
193                       "  \\pagecolor[rgb]{%s}\n" \
194                       "%s\n" \
195                       % (bg_color_gr, match.group()))
196                 continue
197
198             success = 1
199             tmp.write("%stightpage,%s\n" \
200                       "  \\AtBeginDocument{\\AtBeginDvi{%%\n" \
201                       "  \\special{!userdict begin/bop-hook{//bop-hook exec\n" \
202                       "  <%s%s>{255 div}forall setrgbcolor\n" \
203                       "  clippath fill setrgbcolor}bind def end}}}\n" \
204                       % (match.group(1), match.group(2), fg_color, bg_color))
205
206     except:
207         # Unable to open the file, but do nothing here because
208         # the calling function will act on the value of 'success'.
209         warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
210         warning(`sys.exc_type` + ',' + `sys.exc_value`)
211
212     if success:
213         copyfileobj(tmp, open(latex_file,"wb"), 1)
214
215     return success
216
217
218 def crop_files(pnmcrop, basename):
219     t = pipes.Template()
220     t.append('%s -left' % pnmcrop, '--')
221     t.append('%s -right' % pnmcrop, '--')
222
223     for file in glob.glob("%s*.ppm" % basename):
224         tmp = mkstemp()
225         new = t.open(file, "r")
226         copyfileobj(new, tmp)
227         if not new.close():
228             copyfileobj(tmp, open(file,"wb"), 1)
229
230
231 def legacy_conversion(argv, skipMetrics = False):
232     latex_commands = ["latex", "pplatex", "platex", "latex2e"]
233     # Parse and manipulate the command line arguments.
234     if len(argv) == 7:
235         latex_commands = [argv[6]]
236     elif len(argv) != 6:
237         error(usage(argv[0]))
238
239     dir, latex_file = os.path.split(argv[1])
240     if len(dir) != 0:
241         os.chdir(dir)
242
243     dpi = string.atoi(argv[2])
244
245     output_format = argv[3]
246
247     fg_color = argv[4]
248     bg_color = argv[5]
249     bg_color_gr = make_texcolor(argv[5], True)
250
251     # External programs used by the script.
252     path  = string.split(os.environ["PATH"], os.pathsep)
253     latex = find_exe_or_terminate(latex_commands, path)
254
255     # Move color information into the latex file.
256     if not legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr):
257         error("Unable to move color info into the latex file")
258
259     # Compile the latex file.
260     latex_call = '%s "%s"' % (latex, latex_file)
261
262     latex_status, latex_stdout = run_command(latex_call)
263     if latex_status != None:
264         warning("%s had problems compiling %s" \
265               % (os.path.basename(latex), latex_file))
266
267     return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
268
269
270 def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False):
271     # External programs used by the script.
272     path    = string.split(os.environ["PATH"], os.pathsep)
273     dvips   = find_exe_or_terminate(["dvips"], path)
274     gs      = find_exe_or_terminate(["gswin32c", "gs"], path)
275     pnmcrop = find_exe(["pnmcrop"], path)
276
277     # Run the dvi file through dvips.
278     dvi_file = latex_file_re.sub(".dvi", latex_file)
279     ps_file  = latex_file_re.sub(".ps",  latex_file)
280     pdf_file  = latex_file_re.sub(".pdf", latex_file)
281
282     dvips_call = '%s -i -o "%s" "%s"' % (dvips, ps_file, dvi_file)
283     dvips_failed = False
284
285     dvips_status, dvips_stdout = run_command(dvips_call)
286     if dvips_status != None:
287         warning('Failed: %s %s ... looking for PDF' \
288             % (os.path.basename(dvips), dvi_file))
289         dvips_failed = True
290
291     # Extract resolution data for gs from the log file.
292     log_file = latex_file_re.sub(".log", latex_file)
293     resolution = extract_resolution(log_file, dpi)
294
295     # Older versions of gs have problems with a large degree of
296     # anti-aliasing at high resolutions
297     alpha = 4
298     if resolution > 150:
299         alpha = 2
300
301     gs_device = "png16m"
302     gs_ext = "png"
303     if output_format == "ppm":
304         gs_device = "pnmraw"
305         gs_ext = "ppm"
306
307     # Extract the metrics from the log file
308     legacy_metrics = legacy_extract_metrics_info(log_file)
309     
310     # List of pages which failed to produce a correct output
311     failed_pages = []
312     
313     # Generate the bitmap images
314     if dvips_failed:
315         gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
316                   '-sOutputFile="%s%%d.%s" ' \
317                   '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
318                   '-r%f "%s"' \
319                   % (gs, gs_device, latex_file_re.sub("", latex_file), \
320                      gs_ext, alpha, alpha, resolution, pdf_file)
321
322         gs_status, gs_stdout = run_command(gs_call)
323         if gs_status != None:
324             error("Failed: %s %s" % (os.path.basename(gs), ps_file))
325     else:
326         gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
327                   '-sOutputFile="%s%%d.%s" ' \
328                   '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
329                   '-r%f "%%s"' \
330                   % (gs, gs_device, latex_file_re.sub("", latex_file), \
331                      gs_ext, alpha, alpha, resolution)
332         i = 0
333         ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file))
334         ps_files.sort()
335         
336         # Call GhostScript for each page
337         for file in ps_files:
338             i = i + 1
339             gs_status, gs_stdout = run_command(gs_call % (i, file))
340             if gs_status != None:
341                 # gs failed, keep track of this
342                 failed_pages.append(i)
343                 
344     
345     # Pass failed pages to pdflatex
346     if len(failed_pages) > 0:
347         pdflatex = find_exe(["pdflatex"], path)
348         if pdflatex != None:
349             # Create a new LaTeX file from the original but only with failed pages
350             pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
351             filter_pages(latex_file, pdf_latex_file, failed_pages)
352             
353             # pdflatex call
354             pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file)
355             pdflatex_status, pdflatex_stdout = run_command(pdflatex_call)
356             
357             pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
358             
359             # GhostScript call to produce bitmaps
360             gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
361                       '-sOutputFile="%s%%d.%s" ' \
362                       '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
363                       '-r%f "%s"' \
364                       % (gs, gs_device, latex_file_re.sub("", pdf_latex_file), \
365                          gs_ext, alpha, alpha, resolution, pdf_file)
366
367             gs_status, gs_stdout = run_command(gs_call)
368             if gs_status != None:
369                 # Give up!
370                 warning("Some pages failed with all the possible routes")
371             else:
372                 # We've done it!
373                 pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
374                 pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
375                 
376                 original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
377                 destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
378                 
379                 # Join the metrics with the those from dvips and rename the bitmap images
380                 join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages, original_bitmap, destination_bitmap)
381
382     # Crop the images
383     if pnmcrop != None:
384         crop_files(pnmcrop, latex_file_re.sub("", latex_file))
385
386     # Allow to skip .metrics creation for custom management
387     # (see the dvipng method)
388     if not skipMetrics:
389         # Extract metrics info from the log file.
390         metrics_file = latex_file_re.sub(".metrics", latex_file)
391         write_metrics_info(legacy_metrics, metrics_file)
392
393     return (0, legacy_metrics)
394
395
396 if __name__ == "__main__":
397     exit(legacy_conversion(sys.argv)[0])