> Why "import legacy_lyxpreview2ppm" ? You should add what you need to > the "from legacy_lyxpreview2ppm import" statement.
Remained from testing Python in VS2010 :P > > + > > + if success == 0: > > + error("Failed to extract metrics info from %s" % log_file) > > + > > + return results > > > > if success == 0: > > error("Failed to extract metrics info from %s" % log_file) > > > > return results > > It seems that the above lines are simply duplicated? Remained from moving some code in an external function. I've attached the fixed patch. venom00 Index: src/graphics/PreviewLoader.cpp =================================================================== --- src/graphics/PreviewLoader.cpp (revisione 38163) +++ src/graphics/PreviewLoader.cpp (copia locale) @@ -605,10 +605,6 @@ // FIXME what about LuaTeX? if (buffer_.params().useNonTeXFonts) cs << " xelatex"; - // DVI output fails sometimes with hyperref - // (probably a preview-latex/hyperref bug) - else if (buffer_.params().pdfoptions().use_hyperref) - cs << " pdflatex"; string const command = libScriptSearch(cs.str()); Index: lib/scripts/lyxpreview2bitmap.py =================================================================== --- lib/scripts/lyxpreview2bitmap.py (revisione 38163) +++ lib/scripts/lyxpreview2bitmap.py (copia locale) @@ -49,11 +49,11 @@ import glob, os, re, string, sys from legacy_lyxpreview2ppm import legacy_conversion, \ - legacy_conversion_step2, legacy_extract_metrics_info - + legacy_conversion_step2, legacy_extract_metrics_info, filter_pages + from lyxpreview_tools import copyfileobj, error, find_exe, \ find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \ - write_metrics_info + write_metrics_info, join_metrics_and_rename # Pre-compiled regular expressions. @@ -103,7 +103,7 @@ if frac < 0: frac = 0.5 - results.append((page, frac)) + results.append((int(page), frac)) pos = match.end() + 2 if success == 0: @@ -222,6 +222,11 @@ # The dvi output file name dvi_file = latex_file_re.sub(".dvi", latex_file) + # latex failed + # FIXME: try with pdflatex + if not os.path.isfile(dvi_file): + error("No DVI output.") + # Check for PostScript specials in the dvi, badly supported by dvipng # This is required for correct rendering of PSTricks and TikZ dv2dt = find_exe_or_terminate(["dv2dt"], path) @@ -254,6 +259,7 @@ page_has_ps = True pages_parameter = "" + if len(ps_pages) == page_index: # All pages need PostScript, so directly use the legacy method. vec = [argv[0], argv[2], argv[3], argv[1], argv[4], argv[5], latex] @@ -306,80 +312,45 @@ # FIXME: skip unnecessary dvips trial in legacy_conversion_step2 return legacy_conversion_step2(latex_file, dpi, output_format) + dvipng_metrics = [] if len(ps_pages) > 0: # Some pages require PostScript. # Create a new LaTeX file just for the snippets needing # the legacy method - original_latex = open(latex_file, "r") legacy_latex_file = latex_file_re.sub("_legacy.tex", latex_file) - legacy_latex = open(legacy_latex_file, "w") + filter_pages(latex_file, legacy_latex_file, ps_pages) - page_index = 0 - skip_page = False - for line in original_latex: - if line.startswith("\\begin{preview}"): - page_index += 1 - # Skips all pages processed by dvipng - skip_page = page_index not in ps_pages - - if not skip_page: - legacy_latex.write(line) - - if line.startswith("\\end{preview}"): - skip_page = False - - legacy_latex.close() - original_latex.close() - # Pass the new LaTeX file to the legacy method vec = [ argv[0], latex_file_re.sub("_legacy.tex", argv[2]), \ argv[3], argv[1], argv[4], argv[5], latex ] - legacy_conversion(vec, True) - + legacy_metrics = legacy_conversion(vec, True)[1] + # Now we need to mix metrics data from dvipng and the legacy method metrics_file = latex_file_re.sub(".metrics", latex_file) - dvipng_metrics = extract_metrics_info(dvipng_stdout) - legacy_metrics = legacy_extract_metrics_info(latex_file_re.sub("_legacy.log", latex_file)) + + original_bitmap = latex_file_re.sub("%d." + output_format, legacy_latex_file) + destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file) - # Check whether a page is present in dvipng_metrics, otherwise - # add it getting the metrics from legacy_metrics - legacy_index = -1; - for i in range(page_index): - # If we exceed the array bounds or the dvipng_metrics doesn't - # match the current one, this page belongs to the legacy method - if (i > len(dvipng_metrics) - 1) or (dvipng_metrics[i][0] != str(i + 1)): - legacy_index += 1 - - # Add this metric from the legacy output - dvipng_metrics.insert(i, (str(i + 1), legacy_metrics[legacy_index][1])) - # Legacy output filename - legacy_output = os.path.join(dir, latex_file_re.sub("_legacy%s.%s" % - (legacy_metrics[legacy_index][0], output_format), latex_file)) + # Join metrics from dvipng and legacy, and rename legacy bitmaps + join_metrics_and_rename(dvipng_metrics, legacy_metrics, ps_pages, + original_bitmap, destination_bitmap) - # Check whether legacy method actually created the file - if os.path.isfile(legacy_output): - # Rename the file by removing the "_legacy" suffix - # and adjusting the index - bitmap_output = os.path.join(dir, latex_file_re.sub("%s.%s" % - (str(i + 1), output_format), latex_file)) - os.rename(legacy_output, bitmap_output) - - # Actually create the .metrics file - write_metrics_info(dvipng_metrics, metrics_file) else: # Extract metrics info from dvipng_stdout. # In this case we just used dvipng, so no special metrics # handling is needed. metrics_file = latex_file_re.sub(".metrics", latex_file) - write_metrics_info(extract_metrics_info(dvipng_stdout), metrics_file) + dvipng_metrics = extract_metrics_info(dvipng_stdout) # Convert images to ppm format if necessary. if output_format == "ppm": convert_to_ppm_format(pngtopnm, latex_file_re.sub("", latex_file)) - return 0 + # Actually create the .metrics file + write_metrics_info(dvipng_metrics, metrics_file) + + return (0, dvipng_metrics) - if __name__ == "__main__": - main(sys.argv) + exit(main(sys.argv)[0]) Index: lib/scripts/lyxpreview_tools.py =================================================================== --- lib/scripts/lyxpreview_tools.py (revisione 38163) +++ lib/scripts/lyxpreview_tools.py (copia locale) @@ -220,3 +220,53 @@ for metric in metrics_info: metrics.write("Snippet %s %f\n" % metric) metrics.close() + +# Reads a .tex files and create an identical file but only with +# pages whose index is in pages_to_keep +def filter_pages(source_path, destination_path, pages_to_keep): + source_file = open(source_path, "r") + destination_file = open(destination_path, "w") + + page_index = 0 + skip_page = False + for line in source_file: + # We found a new page + if line.startswith("\\begin{preview}"): + page_index += 1 + # If the page index isn't in pages_to_keep we don't copy it + skip_page = page_index not in pages_to_keep + + if not skip_page: + destination_file.write(line) + + # End of a page, we reset the skip_page bool + if line.startswith("\\end{preview}"): + skip_page = False + + destination_file.close() + source_file.close() + +# Joins two metrics list, that is a list of tuple (page_index, metric) +# new_page_indexes contains the original page number of the pages in new_metrics +# e.g. new_page_indexes[3] == 14 means that the 4th item in new_metrics is the 15th in the original counting +# original_bitmap and destination_bitmap are file name models used to rename the new files +# e.g. image_new%d.png and image_%d.png +def join_metrics_and_rename(original_metrics, new_metrics, new_page_indexes, original_bitmap, destination_bitmap): + legacy_index = 0 + for (index, metric) in new_metrics: + # If the file exists we rename it + if os.path.isfile(original_bitmap % (index)): + os.rename(original_bitmap % (index), destination_bitmap % new_page_indexes[index-1]) + + # Extract the original page index + index = new_page_indexes[index-1] + # Goes through the array until the end is reached or the correct index is found + while legacy_index < len(original_metrics) and original_metrics[legacy_index][0] < index: + legacy_index += 1 + + + # Add or update the metric for this page + if legacy_index < len(original_metrics) and original_metrics[legacy_index][0] == index: + original_metrics[legacy_index] = (index, metric) + else: + original_metrics.insert(legacy_index, (index, metric)) Index: lib/scripts/legacy_lyxpreview2ppm.py =================================================================== --- lib/scripts/legacy_lyxpreview2ppm.py (revisione 38163) +++ lib/scripts/legacy_lyxpreview2ppm.py (copia locale) @@ -57,7 +57,7 @@ from lyxpreview_tools import copyfileobj, error, find_exe, \ find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \ - write_metrics_info + write_metrics_info, filter_pages, join_metrics_and_rename # Pre-compiled regular expression. latex_file_re = re.compile("\.tex$") @@ -109,7 +109,7 @@ if frac < 0 or frac > 1: frac = 0.5 - results.append((match.group(1), frac)) + results.append((int(match.group(1)), frac)) else: tp_descent = string.atof(match.group(2)) @@ -126,7 +126,6 @@ return results - def extract_resolution(log_file, dpi): fontsize_re = re.compile("Preview: Fontsize") magnification_re = re.compile("Preview: Magnification") @@ -305,8 +304,13 @@ gs_device = "pnmraw" gs_ext = "ppm" + # 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: gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \ '-sOutputFile="%s%%d.%s" ' \ @@ -328,13 +332,52 @@ i = 0 ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file)) ps_files.sort() + + # Call GhostScript for each page for file in ps_files: i = i + 1 gs_status, gs_stdout = run_command(gs_call % (i, file)) if gs_status != None: - warning("Failed: %s %s" % (os.path.basename(gs), file)) + # gs failed, keep track of this + failed_pages.append(i) + + + # Pass failed pages to pdflatex + if len(failed_pages) > 0: + pdflatex = find_exe(["pdflatex"], path) + if pdflatex != None: + # 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 + pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file) + pdflatex_status, pdflatex_stdout = run_command(pdflatex_call) + + pdf_file = latex_file_re.sub(".pdf", pdf_latex_file) + + # GhostScript call to produce bitmaps + gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \ + '-sOutputFile="%s%%d.%s" ' \ + '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \ + '-r%f "%s"' \ + % (gs, gs_device, latex_file_re.sub("", pdf_latex_file), \ + gs_ext, alpha, alpha, resolution, pdf_file) + + gs_status, gs_stdout = run_command(gs_call) + if gs_status != None: + # Give up! + warning("Some pages failed with all the possible routes") else: - os.remove(file) + # We've done it! + pdf_log_file = latex_file_re.sub(".log", pdf_latex_file) + pdf_metrics = legacy_extract_metrics_info(pdf_log_file) + + original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file) + destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file) + + # Join the metrics with the those from dvips and rename the bitmap images + join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages, original_bitmap, destination_bitmap) # Crop the images if pnmcrop != None: @@ -345,10 +388,10 @@ if not skipMetrics: # Extract metrics info from the log file. metrics_file = latex_file_re.sub(".metrics", latex_file) - write_metrics_info(legacy_extract_metrics_info(log_file), metrics_file) + write_metrics_info(legacy_metrics, metrics_file) - return 0 + return (0, legacy_metrics) if __name__ == "__main__": - legacy_conversion(sys.argv) + exit(legacy_conversion(sys.argv)[0])
Index: src/graphics/PreviewLoader.cpp =================================================================== --- src/graphics/PreviewLoader.cpp (revisione 38163) +++ src/graphics/PreviewLoader.cpp (copia locale) @@ -605,10 +605,6 @@ // FIXME what about LuaTeX? if (buffer_.params().useNonTeXFonts) cs << " xelatex"; - // DVI output fails sometimes with hyperref - // (probably a preview-latex/hyperref bug) - else if (buffer_.params().pdfoptions().use_hyperref) - cs << " pdflatex"; string const command = libScriptSearch(cs.str()); Index: lib/scripts/lyxpreview2bitmap.py =================================================================== --- lib/scripts/lyxpreview2bitmap.py (revisione 38163) +++ lib/scripts/lyxpreview2bitmap.py (copia locale) @@ -49,11 +49,11 @@ import glob, os, re, string, sys from legacy_lyxpreview2ppm import legacy_conversion, \ - legacy_conversion_step2, legacy_extract_metrics_info - + legacy_conversion_step2, legacy_extract_metrics_info, filter_pages + from lyxpreview_tools import copyfileobj, error, find_exe, \ find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \ - write_metrics_info + write_metrics_info, join_metrics_and_rename # Pre-compiled regular expressions. @@ -103,7 +103,7 @@ if frac < 0: frac = 0.5 - results.append((page, frac)) + results.append((int(page), frac)) pos = match.end() + 2 if success == 0: @@ -222,6 +222,11 @@ # The dvi output file name dvi_file = latex_file_re.sub(".dvi", latex_file) + # latex failed + # FIXME: try with pdflatex + if not os.path.isfile(dvi_file): + error("No DVI output.") + # Check for PostScript specials in the dvi, badly supported by dvipng # This is required for correct rendering of PSTricks and TikZ dv2dt = find_exe_or_terminate(["dv2dt"], path) @@ -254,6 +259,7 @@ page_has_ps = True pages_parameter = "" + if len(ps_pages) == page_index: # All pages need PostScript, so directly use the legacy method. vec = [argv[0], argv[2], argv[3], argv[1], argv[4], argv[5], latex] @@ -306,80 +312,45 @@ # FIXME: skip unnecessary dvips trial in legacy_conversion_step2 return legacy_conversion_step2(latex_file, dpi, output_format) + dvipng_metrics = [] if len(ps_pages) > 0: # Some pages require PostScript. # Create a new LaTeX file just for the snippets needing # the legacy method - original_latex = open(latex_file, "r") legacy_latex_file = latex_file_re.sub("_legacy.tex", latex_file) - legacy_latex = open(legacy_latex_file, "w") + filter_pages(latex_file, legacy_latex_file, ps_pages) - page_index = 0 - skip_page = False - for line in original_latex: - if line.startswith("\\begin{preview}"): - page_index += 1 - # Skips all pages processed by dvipng - skip_page = page_index not in ps_pages - - if not skip_page: - legacy_latex.write(line) - - if line.startswith("\\end{preview}"): - skip_page = False - - legacy_latex.close() - original_latex.close() - # Pass the new LaTeX file to the legacy method vec = [ argv[0], latex_file_re.sub("_legacy.tex", argv[2]), \ argv[3], argv[1], argv[4], argv[5], latex ] - legacy_conversion(vec, True) - + legacy_metrics = legacy_conversion(vec, True)[1] + # Now we need to mix metrics data from dvipng and the legacy method metrics_file = latex_file_re.sub(".metrics", latex_file) - dvipng_metrics = extract_metrics_info(dvipng_stdout) - legacy_metrics = legacy_extract_metrics_info(latex_file_re.sub("_legacy.log", latex_file)) + + original_bitmap = latex_file_re.sub("%d." + output_format, legacy_latex_file) + destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file) - # Check whether a page is present in dvipng_metrics, otherwise - # add it getting the metrics from legacy_metrics - legacy_index = -1; - for i in range(page_index): - # If we exceed the array bounds or the dvipng_metrics doesn't - # match the current one, this page belongs to the legacy method - if (i > len(dvipng_metrics) - 1) or (dvipng_metrics[i][0] != str(i + 1)): - legacy_index += 1 - - # Add this metric from the legacy output - dvipng_metrics.insert(i, (str(i + 1), legacy_metrics[legacy_index][1])) - # Legacy output filename - legacy_output = os.path.join(dir, latex_file_re.sub("_legacy%s.%s" % - (legacy_metrics[legacy_index][0], output_format), latex_file)) + # Join metrics from dvipng and legacy, and rename legacy bitmaps + join_metrics_and_rename(dvipng_metrics, legacy_metrics, ps_pages, + original_bitmap, destination_bitmap) - # Check whether legacy method actually created the file - if os.path.isfile(legacy_output): - # Rename the file by removing the "_legacy" suffix - # and adjusting the index - bitmap_output = os.path.join(dir, latex_file_re.sub("%s.%s" % - (str(i + 1), output_format), latex_file)) - os.rename(legacy_output, bitmap_output) - - # Actually create the .metrics file - write_metrics_info(dvipng_metrics, metrics_file) else: # Extract metrics info from dvipng_stdout. # In this case we just used dvipng, so no special metrics # handling is needed. metrics_file = latex_file_re.sub(".metrics", latex_file) - write_metrics_info(extract_metrics_info(dvipng_stdout), metrics_file) + dvipng_metrics = extract_metrics_info(dvipng_stdout) # Convert images to ppm format if necessary. if output_format == "ppm": convert_to_ppm_format(pngtopnm, latex_file_re.sub("", latex_file)) - return 0 + # Actually create the .metrics file + write_metrics_info(dvipng_metrics, metrics_file) + + return (0, dvipng_metrics) - if __name__ == "__main__": - main(sys.argv) + exit(main(sys.argv)[0]) Index: lib/scripts/lyxpreview_tools.py =================================================================== --- lib/scripts/lyxpreview_tools.py (revisione 38163) +++ lib/scripts/lyxpreview_tools.py (copia locale) @@ -220,3 +220,53 @@ for metric in metrics_info: metrics.write("Snippet %s %f\n" % metric) metrics.close() + +# Reads a .tex files and create an identical file but only with +# pages whose index is in pages_to_keep +def filter_pages(source_path, destination_path, pages_to_keep): + source_file = open(source_path, "r") + destination_file = open(destination_path, "w") + + page_index = 0 + skip_page = False + for line in source_file: + # We found a new page + if line.startswith("\\begin{preview}"): + page_index += 1 + # If the page index isn't in pages_to_keep we don't copy it + skip_page = page_index not in pages_to_keep + + if not skip_page: + destination_file.write(line) + + # End of a page, we reset the skip_page bool + if line.startswith("\\end{preview}"): + skip_page = False + + destination_file.close() + source_file.close() + +# Joins two metrics list, that is a list of tuple (page_index, metric) +# new_page_indexes contains the original page number of the pages in new_metrics +# e.g. new_page_indexes[3] == 14 means that the 4th item in new_metrics is the 15th in the original counting +# original_bitmap and destination_bitmap are file name models used to rename the new files +# e.g. image_new%d.png and image_%d.png +def join_metrics_and_rename(original_metrics, new_metrics, new_page_indexes, original_bitmap, destination_bitmap): + legacy_index = 0 + for (index, metric) in new_metrics: + # If the file exists we rename it + if os.path.isfile(original_bitmap % (index)): + os.rename(original_bitmap % (index), destination_bitmap % new_page_indexes[index-1]) + + # Extract the original page index + index = new_page_indexes[index-1] + # Goes through the array until the end is reached or the correct index is found + while legacy_index < len(original_metrics) and original_metrics[legacy_index][0] < index: + legacy_index += 1 + + + # Add or update the metric for this page + if legacy_index < len(original_metrics) and original_metrics[legacy_index][0] == index: + original_metrics[legacy_index] = (index, metric) + else: + original_metrics.insert(legacy_index, (index, metric)) Index: lib/scripts/legacy_lyxpreview2ppm.py =================================================================== --- lib/scripts/legacy_lyxpreview2ppm.py (revisione 38163) +++ lib/scripts/legacy_lyxpreview2ppm.py (copia locale) @@ -57,7 +57,7 @@ from lyxpreview_tools import copyfileobj, error, find_exe, \ find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \ - write_metrics_info + write_metrics_info, filter_pages, join_metrics_and_rename # Pre-compiled regular expression. latex_file_re = re.compile("\.tex$") @@ -109,7 +109,7 @@ if frac < 0 or frac > 1: frac = 0.5 - results.append((match.group(1), frac)) + results.append((int(match.group(1)), frac)) else: tp_descent = string.atof(match.group(2)) @@ -126,7 +126,6 @@ return results - def extract_resolution(log_file, dpi): fontsize_re = re.compile("Preview: Fontsize") magnification_re = re.compile("Preview: Magnification") @@ -305,8 +304,13 @@ gs_device = "pnmraw" gs_ext = "ppm" + # 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: gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \ '-sOutputFile="%s%%d.%s" ' \ @@ -328,13 +332,52 @@ i = 0 ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file)) ps_files.sort() + + # Call GhostScript for each page for file in ps_files: i = i + 1 gs_status, gs_stdout = run_command(gs_call % (i, file)) if gs_status != None: - warning("Failed: %s %s" % (os.path.basename(gs), file)) + # gs failed, keep track of this + failed_pages.append(i) + + + # Pass failed pages to pdflatex + if len(failed_pages) > 0: + pdflatex = find_exe(["pdflatex"], path) + if pdflatex != None: + # 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 + pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file) + pdflatex_status, pdflatex_stdout = run_command(pdflatex_call) + + pdf_file = latex_file_re.sub(".pdf", pdf_latex_file) + + # GhostScript call to produce bitmaps + gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \ + '-sOutputFile="%s%%d.%s" ' \ + '-dGraphicsAlphaBit=%d -dTextAlphaBits=%d ' \ + '-r%f "%s"' \ + % (gs, gs_device, latex_file_re.sub("", pdf_latex_file), \ + gs_ext, alpha, alpha, resolution, pdf_file) + + gs_status, gs_stdout = run_command(gs_call) + if gs_status != None: + # Give up! + warning("Some pages failed with all the possible routes") else: - os.remove(file) + # We've done it! + pdf_log_file = latex_file_re.sub(".log", pdf_latex_file) + pdf_metrics = legacy_extract_metrics_info(pdf_log_file) + + original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file) + destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file) + + # Join the metrics with the those from dvips and rename the bitmap images + join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages, original_bitmap, destination_bitmap) # Crop the images if pnmcrop != None: @@ -345,10 +388,10 @@ if not skipMetrics: # Extract metrics info from the log file. metrics_file = latex_file_re.sub(".metrics", latex_file) - write_metrics_info(legacy_extract_metrics_info(log_file), metrics_file) + write_metrics_info(legacy_metrics, metrics_file) - return 0 + return (0, legacy_metrics) if __name__ == "__main__": - legacy_conversion(sys.argv) + exit(legacy_conversion(sys.argv)[0])