+ # 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 = []
+
+ # Generate the bitmap images
+ if dvips_failed:
+ # dvips failed, maybe there's a PDF, try to produce bitmaps
+ if use_pdftocairo:
+ conv_call = '%s -png -transp -r %d "%s" "%s"' \
+ % (pdftocairo, resolution, pdf_file, latex_file_root)
+
+ conv_status, conv_stdout = run_command(conv_call)
+ if not conv_status:
+ seqnum_re = re.compile("-([0-9]+)")
+ for name in glob.glob("%s-*.png" % latex_file_root):
+ match = seqnum_re.search(name)
+ if match != None:
+ new_name = seqnum_re.sub(str(int(match.group(1))), name)
+ os.rename(name, new_name)
+ else:
+ conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
+ '-sOutputFile="%s%%d.%s" ' \
+ '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
+ '-r%f "%s"' \
+ % (gs, gs_device, latex_file_root, \
+ gs_ext, alpha, alpha, resolution, pdf_file)
+
+ conv_status, conv_stdout = run_command(conv_call)
+
+ if conv_status:
+ error("Failed: %s %s" % (os.path.basename(conv), pdf_file))
+ else:
+ # Model for calling the converter on each file
+ if use_pdftocairo and epstopdf != None:
+ conv_call = '%s -png -transp -singlefile -r %d "%%s" "%s%%d"' \
+ % (pdftocairo, resolution, latex_file_root)
+ else:
+ conv_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
+ '-sOutputFile="%s%%d.%s" ' \
+ '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \
+ '-r%f "%%s"' \
+ % (gs, gs_device, latex_file_root, \
+ 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_root)
+ ps_files.sort()
+
+ # Call the converter for each file
+ for file in ps_files:
+ i = i + 1
+ progress("Processing page %s, file %s" % (i, file))
+ if use_pdftocairo and epstopdf != None:
+ conv_name = "epstopdf"
+ conv_status, conv_stdout = run_command("%s --outfile=%s.pdf %s"
+ % (epstopdf, file, file))
+ if not conv_status:
+ conv_name = "pdftocairo"
+ file = file + ".pdf"
+ conv_status, conv_stdout = run_command(conv_call % (file, i))
+ else:
+ conv_name = "ghostscript"
+ conv_status, conv_stdout = run_command(conv_call % (i, file))
+
+ if conv_status:
+ # The converter failed, keep track of this
+ warning("%s failed on page %s, file %s" % (conv_name, i, file))
+ failed_pages.append(i)
+
+ # Pass failed pages to pdflatex
+ if len(failed_pages) > 0:
+ warning("Now trying to obtain failed previews through pdflatex")
+ error_count = legacy_conversion_pdflatex(latex_file, failed_pages,
+ legacy_metrics, use_pdftocairo, conv, gs_device, gs_ext, alpha,
+ resolution, output_format)
+ else:
+ error_count = 0
+
+ # Invalidate metrics for pages that produced errors
+ if len(error_pages) > 0:
+ for index in error_pages:
+ if index not in failed_pages:
+ legacy_metrics.pop(index - 1)
+ legacy_metrics.insert(index - 1, (index, -1.0))
+ error_count += 1
+
+ # Crop the ppm images
+ if pnmcrop != None and output_format == "ppm":
+ crop_files(pnmcrop, latex_file_root)
+
+ # 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)
+ if error_count:
+ warning("Failed to produce %d preview snippet(s)" % error_count)
+
+ return (0, legacy_metrics)