# If possible, the script will use pdftocairo instead of gs,
# as it's much faster and gives better results.
-import glob, os, pipes, re, string, sys
+import glob, os, pipes, re, sys, tempfile
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, \
+ latex_file_re, make_texcolor, pdflatex_commands, progress, \
run_command, run_latex, warning, write_metrics_info
# Use write_metrics_info to create the .metrics file with this info
def legacy_extract_metrics_info(log_file):
- log_re = re.compile("Preview: ([ST])")
- data_re = re.compile("(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
+ log_re = re.compile(b"Preview: ([ST])")
+ data_re = re.compile(b"(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
tp_ascent = 0.0
tp_descent = 0.0
success = 0
results = []
try:
- for line in open(log_file, 'r').readlines():
+ for line in open(log_file, 'rb').readlines():
match = log_re.match(line)
if match == None:
continue
- snippet = (match.group(1) == 'S')
+ snippet = (match.group(1) == b'S')
success = 1
match = data_re.search(line)
if match == None:
error("Unexpected data in %s\n%s" % (log_file, line))
if snippet:
- ascent = string.atof(match.group(2))
- descent = string.atof(match.group(3))
+ ascent = float(match.group(2))
+ descent = float(match.group(3))
frac = 0.5
if ascent == 0 and descent == 0:
results.append((int(match.group(1)), frac))
else:
- tp_descent = string.atof(match.group(2))
- tp_ascent = string.atof(match.group(4))
+ tp_descent = float(match.group(2))
+ tp_ascent = float(match.group(4))
except:
# Unable to open the file, but do nothing here because
# the calling function will act on the value of 'success'.
warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file)
- warning(`sys.exc_type` + ',' + `sys.exc_value`)
+ warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
if success == 0:
error("Failed to extract metrics info from %s" % log_file)
return results
def extract_resolution(log_file, dpi):
- fontsize_re = re.compile("Preview: Fontsize")
- magnification_re = re.compile("Preview: Magnification")
- extract_decimal_re = re.compile("([0-9\.]+)")
- extract_integer_re = re.compile("([0-9]+)")
+ fontsize_re = re.compile(b"Preview: Fontsize")
+ magnification_re = re.compile(b"Preview: Magnification")
+ extract_decimal_re = re.compile(b"([0-9\.]+)")
+ extract_integer_re = re.compile(b"([0-9]+)")
found_fontsize = 0
found_magnification = 0
fontsize = 10.0
try:
- for line in open(log_file, 'r').readlines():
+ for line in open(log_file, 'rb').readlines():
if found_fontsize and found_magnification:
break
match = extract_decimal_re.search(line)
if match == None:
error("Unable to parse: %s" % line)
- fontsize = string.atof(match.group(1))
+ fontsize = float(match.group(1))
found_fontsize = 1
continue
match = extract_integer_re.search(line)
if match == None:
error("Unable to parse: %s" % line)
- magnification = string.atof(match.group(1))
+ magnification = float(match.group(1))
found_magnification = 1
continue
except:
warning('Warning in extract_resolution! Unable to open "%s"' % log_file)
- warning(`sys.exc_type` + ',' + `sys.exc_value`)
+ warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
# This is safe because both fontsize and magnification have
# non-zero default values.
def legacy_latex_file(latex_file, fg_color, bg_color):
- use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}")
+ use_polyglossia_re = re.compile(b"\\s*\\\\usepackage{polyglossia}")
+ use_preview_re = re.compile(b"\\s*\\\\usepackage\\[([^]]+)\\]{preview}")
fg_color_gr = make_texcolor(fg_color, True)
bg_color_gr = make_texcolor(bg_color, True)
- tmp = mkstemp()
+ tmp = tempfile.TemporaryFile()
success = 0
try:
- f = open(latex_file, 'r')
+ f = open(latex_file, 'rb')
except:
# Unable to open the file, but do nothing here because
# the calling function will act on the value of 'success'.
warning('Warning in legacy_latex_file! Unable to open "%s"' % latex_file)
- warning(`sys.exc_type` + ',' + `sys.exc_value`)
+ warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
+ polyglossia = False
for line in f.readlines():
if success:
tmp.write(line)
continue
match = use_preview_re.match(line)
+ polymatch = use_polyglossia_re.match(line)
+ # Package order:
+ # * if polyglossia is used, we need to load color before that
+ # (also, we do not have to load lmodern)
+ # * else, color should be loaded before preview
if match == None:
- tmp.write(line)
- continue
+ if polymatch == None:
+ tmp.write(line)
+ continue
+ else:
+ tmp.write(b"""
+\\usepackage{color}
+\\definecolor{fg}{rgb}{%s}
+\\definecolor{bg}{rgb}{%s}
+\\pagecolor{bg}
+\\usepackage{polyglossia}
+""" % (fg_color_gr, bg_color_gr))
+ polyglossia = True
+ continue
success = 1
- # Package order: color should be loaded before preview
# Preview options: add the options lyx and tightpage
- tmp.write(r"""
-\usepackage{color}
-\definecolor{fg}{rgb}{%s}
-\definecolor{bg}{rgb}{%s}
-\pagecolor{bg}
-\usepackage[%s,tightpage]{preview}
-\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecomp}}
-\makeatletter
-\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath fill}\color{fg}}
-\g@addto@macro\endpreview{\endgroup}
-\makeatother
-""" % (fg_color_gr, bg_color_gr, match.group(1)))
-
+ previewopts = match.group(1)
+ if not polyglossia:
+ tmp.write(b"""
+\\usepackage{color}
+\\definecolor{fg}{rgb}{%s}
+\\definecolor{bg}{rgb}{%s}
+\\pagecolor{bg}
+\\usepackage[%s,tightpage]{preview}
+\\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
+""" % (fg_color_gr, bg_color_gr, previewopts))
+ else:
+ tmp.write(b"""
+\\usepackage[%s,tightpage]{preview}
+\\makeatletter
+\\g@addto@macro\\preview{\\begingroup\\color{bg}\\special{ps::clippath fill}\\color{fg}}
+\\g@addto@macro\\endpreview{\\endgroup}
+\\makeatother
+""" % previewopts)
if success:
copyfileobj(tmp, open(latex_file,"wb"), 1)
t.append('%s -right' % pnmcrop, '--')
for file in glob.glob("%s*.ppm" % basename):
- tmp = mkstemp()
+ tmp = tempfile.TemporaryFile()
new = t.open(file, "r")
copyfileobj(new, tmp)
if not new.close():
if len(dir) != 0:
os.chdir(dir)
- dpi = string.atoi(argv[2])
+ dpi = int(argv[2])
output_format = argv[3]
# 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)
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)
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)
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)
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.
pdftocairo = find_exe(["pdftocairo"])
epstopdf = find_exe(["epstopdf"])
use_pdftocairo = pdftocairo != None and output_format == "png"
+ if use_pdftocairo and os.name == 'nt':
+ # On Windows, check for png support (see #10718)
+ conv_status, conv_stdout = run_command("%s --help" % pdftocairo)
+ use_pdftocairo = '-png' in conv_stdout
if use_pdftocairo:
conv = pdftocairo
else:
# 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:
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":
# 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)