]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/legacy_lyxpreview2ppm.py
The way this was previously, it had to fail if the GUI language
[lyx.git] / lib / scripts / legacy_lyxpreview2ppm.py
index 86a873502e40b7bf92a3e399b1778322938c1a53..ee49e6afa3465310ec0de6950377e10fedef24be 100644 (file)
@@ -81,8 +81,8 @@
 
 import glob, os, pipes, re, string, sys
 
-from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \
-     find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
+from lyxpreview_tools import check_latex_log, copyfileobj, error, filter_pages,\
+     find_exe, find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
      latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \
      run_command, run_latex, warning, write_metrics_info
 
@@ -233,8 +233,11 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
 \definecolor{bg}{rgb}{%s}
 \pagecolor{bg}
 \usepackage[%s,tightpage]{preview}
-\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecomp}}
 \makeatletter
+\def\t@a{cmr}
+\if\f@family\t@a
+\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}}
+\fi
 \g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
 \g@addto@macro\endpreview{\endgroup}
 \makeatother
@@ -302,7 +305,7 @@ def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
     # Compile the latex file.
     latex_status, latex_stdout = run_latex(latex, latex_file)
     if latex_status:
-        warning("trying to recover from failed compilation")
+        progress("Will try to recover from %s failure" % latex)
 
     if pdf_output:
         return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
@@ -315,17 +318,23 @@ def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
 def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
     use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution, output_format):
 
+    error_count = 0
+
     # Search for pdflatex executable
     pdflatex = find_exe(["pdflatex"])
     if pdflatex == None:
         warning("Can't find pdflatex. Some pages failed with all the possible routes.")
+        failed_pages = []
     else:
         # Create a new LaTeX file from the original but only with failed pages
         pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
         filter_pages(latex_file, pdf_latex_file, failed_pages)
 
         # pdflatex call
+        error_pages = []
         pdflatex_status, pdflatex_stdout = run_latex(pdflatex, pdf_latex_file)
+        if pdflatex_status:
+            error_pages = check_latex_log(latex_file_re.sub(".log", pdf_latex_file))
 
         pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
         latex_file_root = latex_file_re.sub("", pdf_latex_file)
@@ -354,11 +363,19 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
         if conv_status:
             # Give up!
             warning("Some pages failed with all the possible routes")
+            failed_pages = []
         else:
             # We've done it!
             pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
             pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
 
+            # Invalidate metrics for pages that produced errors
+            if len(error_pages) > 0:
+                for index in error_pages:
+                    pdf_metrics.pop(index - 1)
+                    pdf_metrics.insert(index - 1, (index, -1.0))
+                    error_count += 1
+
             original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
             destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
 
@@ -366,6 +383,8 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
             join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
                 original_bitmap, destination_bitmap)
 
+    return error_count
+
 
 # The file has been processed through latex and we expect dvi output.
 # Run dvips, taking note whether it was successful.
@@ -414,6 +433,9 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
     log_file = latex_file_re.sub(".log", latex_file)
     resolution = extract_resolution(log_file, dpi)
 
+    # Check whether some pages produced errors
+    error_pages = check_latex_log(log_file)
+
     # Older versions of gs have problems with a large degree of
     # anti-aliasing at high resolutions
     alpha = 4
@@ -462,7 +484,7 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
     else:
         # Model for calling the converter on each file
         if use_pdftocairo and epstopdf != None:
-            conv_call = '%s -png -transp -singlefile -r %d "%%s.pdf" "%s%%d"' \
+            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 ' \
@@ -482,10 +504,12 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
             i = i + 1
             progress("Processing page %s, file %s" % (i, file))
             if use_pdftocairo and epstopdf != None:
-                conv_name = "pdftocairo"
+                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"
@@ -499,9 +523,19 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
     # Pass failed pages to pdflatex
     if len(failed_pages) > 0:
         warning("Now trying to obtain failed previews through pdflatex")
-        legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics,
-            use_pdftocairo, conv, gs_device, gs_ext, alpha, resolution,
-            output_format)
+        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":
@@ -513,6 +547,8 @@ def legacy_conversion_step3(latex_file, dpi, output_format, dvips_failed, skipMe
         # 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)