mirror of
https://git.lyx.org/repos/lyx.git
synced 2025-01-26 01:49:22 +00:00
Stylictic-only changes to lyxpreview.
Whitespace and line endings only, in preparation for some serious work. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39655 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
796bcb8c25
commit
2dfa11904f
@ -58,7 +58,7 @@
|
|||||||
# [legacy_conversion_pdflatex]
|
# [legacy_conversion_pdflatex]
|
||||||
# 5) Keep track of pages on which gs failed and pass them to pdflatex
|
# 5) Keep track of pages on which gs failed and pass them to pdflatex
|
||||||
# 6) Call gs on the PDF output from pdflatex to produce bitmaps
|
# 6) Call gs on the PDF output from pdflatex to produce bitmaps
|
||||||
# 7) Extract and write to file (or return to lyxpreview2bitmap)
|
# 7) Extract and write to file (or return to lyxpreview2bitmap)
|
||||||
# metrics from both methods (standard and pdflatex)
|
# metrics from both methods (standard and pdflatex)
|
||||||
|
|
||||||
# The script uses the old dvi->ps->png conversion route,
|
# The script uses the old dvi->ps->png conversion route,
|
||||||
@ -67,7 +67,7 @@
|
|||||||
# This script also generates bitmaps from PDF created by a call to
|
# This script also generates bitmaps from PDF created by a call to
|
||||||
# lyxpreview2bitmap.py passing "pdflatex" to the CONVERTER parameter
|
# lyxpreview2bitmap.py passing "pdflatex" to the CONVERTER parameter
|
||||||
# (step 3).
|
# (step 3).
|
||||||
# Finally, there's also has a fallback method based on pdflatex, which
|
# Finally, there's also has a fallback method based on pdflatex, which
|
||||||
# is required in certain cases, if hyperref is active for instance,
|
# is required in certain cases, if hyperref is active for instance,
|
||||||
# (step 5, 6).
|
# (step 5, 6).
|
||||||
# If possible, dvipng should be used, as it's much faster.
|
# If possible, dvipng should be used, as it's much faster.
|
||||||
@ -85,8 +85,8 @@ latex_file_re = re.compile("\.tex$")
|
|||||||
path = string.split(os.environ["PATH"], os.pathsep)
|
path = string.split(os.environ["PATH"], os.pathsep)
|
||||||
|
|
||||||
def usage(prog_name):
|
def usage(prog_name):
|
||||||
return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n"\
|
return "Usage: %s <latex file> <dpi> ppm <fg color> <bg color>\n" \
|
||||||
"\twhere the colors are hexadecimal strings, eg 'faf0e6'"\
|
"\twhere the colors are hexadecimal strings, eg 'faf0e6'" \
|
||||||
% prog_name
|
% prog_name
|
||||||
|
|
||||||
# Returns a list of tuples containing page number and ascent fraction
|
# Returns a list of tuples containing page number and ascent fraction
|
||||||
@ -141,10 +141,10 @@ def legacy_extract_metrics_info(log_file):
|
|||||||
# the calling function will act on the value of 'success'.
|
# the calling function will act on the value of 'success'.
|
||||||
warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file)
|
warning('Warning in legacy_extract_metrics_info! Unable to open "%s"' % log_file)
|
||||||
warning(`sys.exc_type` + ',' + `sys.exc_value`)
|
warning(`sys.exc_type` + ',' + `sys.exc_value`)
|
||||||
|
|
||||||
if success == 0:
|
if success == 0:
|
||||||
error("Failed to extract metrics info from %s" % log_file)
|
error("Failed to extract metrics info from %s" % log_file)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def extract_resolution(log_file, dpi):
|
def extract_resolution(log_file, dpi):
|
||||||
@ -286,12 +286,12 @@ def legacy_conversion(argv, skipMetrics = False):
|
|||||||
|
|
||||||
return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
|
return legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics)
|
||||||
|
|
||||||
# Creates a new LaTeX file from the original with pages specified in
|
# Creates a new LaTeX file from the original with pages specified in
|
||||||
# failed_pages, pass it through pdflatex and updates the metrics
|
# failed_pages, pass it through pdflatex and updates the metrics
|
||||||
# from the standard legacy route
|
# from the standard legacy route
|
||||||
def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
|
def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
|
||||||
gs_device, gs_ext, alpha, resolution, output_format):
|
gs_device, gs_ext, alpha, resolution, output_format):
|
||||||
|
|
||||||
# Search for pdflatex executable
|
# Search for pdflatex executable
|
||||||
pdflatex = find_exe(["pdflatex"], path)
|
pdflatex = find_exe(["pdflatex"], path)
|
||||||
if pdflatex == None:
|
if pdflatex == None:
|
||||||
@ -300,13 +300,13 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
|
|||||||
# Create a new LaTeX file from the original but only with failed pages
|
# Create a new LaTeX file from the original but only with failed pages
|
||||||
pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
|
pdf_latex_file = latex_file_re.sub("_pdflatex.tex", latex_file)
|
||||||
filter_pages(latex_file, pdf_latex_file, failed_pages)
|
filter_pages(latex_file, pdf_latex_file, failed_pages)
|
||||||
|
|
||||||
# pdflatex call
|
# pdflatex call
|
||||||
pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file)
|
pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file)
|
||||||
pdflatex_status, pdflatex_stdout = run_command(pdflatex_call)
|
pdflatex_status, pdflatex_stdout = run_command(pdflatex_call)
|
||||||
|
|
||||||
pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
|
pdf_file = latex_file_re.sub(".pdf", pdf_latex_file)
|
||||||
|
|
||||||
# GhostScript call to produce bitmaps
|
# GhostScript call to produce bitmaps
|
||||||
gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
|
gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s ' \
|
||||||
'-sOutputFile="%s%%d.%s" ' \
|
'-sOutputFile="%s%%d.%s" ' \
|
||||||
@ -322,12 +322,12 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
|
|||||||
# We've done it!
|
# We've done it!
|
||||||
pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
|
pdf_log_file = latex_file_re.sub(".log", pdf_latex_file)
|
||||||
pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
|
pdf_metrics = legacy_extract_metrics_info(pdf_log_file)
|
||||||
|
|
||||||
original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
|
original_bitmap = latex_file_re.sub("%d." + output_format, pdf_latex_file)
|
||||||
destination_bitmap = latex_file_re.sub("%d." + output_format, 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 the metrics with the those from dvips and rename the bitmap images
|
||||||
join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
|
join_metrics_and_rename(legacy_metrics, pdf_metrics, failed_pages,
|
||||||
original_bitmap, destination_bitmap)
|
original_bitmap, destination_bitmap)
|
||||||
|
|
||||||
|
|
||||||
@ -369,10 +369,10 @@ def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False)
|
|||||||
|
|
||||||
# Extract the metrics from the log file
|
# Extract the metrics from the log file
|
||||||
legacy_metrics = legacy_extract_metrics_info(log_file)
|
legacy_metrics = legacy_extract_metrics_info(log_file)
|
||||||
|
|
||||||
# List of pages which failed to produce a correct output
|
# List of pages which failed to produce a correct output
|
||||||
failed_pages = []
|
failed_pages = []
|
||||||
|
|
||||||
# Generate the bitmap images
|
# Generate the bitmap images
|
||||||
if dvips_failed:
|
if dvips_failed:
|
||||||
# dvips failed, maybe there's a PDF, try to produce bitmaps
|
# dvips failed, maybe there's a PDF, try to produce bitmaps
|
||||||
@ -394,12 +394,12 @@ def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False)
|
|||||||
'-r%f "%%s"' \
|
'-r%f "%%s"' \
|
||||||
% (gs, gs_device, latex_file_re.sub("", latex_file), \
|
% (gs, gs_device, latex_file_re.sub("", latex_file), \
|
||||||
gs_ext, alpha, alpha, resolution)
|
gs_ext, alpha, alpha, resolution)
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
# Collect all the PostScript files (like *.001, *.002, ...)
|
# Collect all the PostScript files (like *.001, *.002, ...)
|
||||||
ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file))
|
ps_files = glob.glob("%s.[0-9][0-9][0-9]" % latex_file_re.sub("", latex_file))
|
||||||
ps_files.sort()
|
ps_files.sort()
|
||||||
|
|
||||||
# Call GhostScript for each file
|
# Call GhostScript for each file
|
||||||
for file in ps_files:
|
for file in ps_files:
|
||||||
i = i + 1
|
i = i + 1
|
||||||
@ -407,10 +407,10 @@ def legacy_conversion_step2(latex_file, dpi, output_format, skipMetrics = False)
|
|||||||
if gs_status != None:
|
if gs_status != None:
|
||||||
# gs failed, keep track of this
|
# gs failed, keep track of this
|
||||||
failed_pages.append(i)
|
failed_pages.append(i)
|
||||||
|
|
||||||
# Pass failed pages to pdflatex
|
# Pass failed pages to pdflatex
|
||||||
if len(failed_pages) > 0:
|
if len(failed_pages) > 0:
|
||||||
legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
|
legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs,
|
||||||
gs_device, gs_ext, alpha, resolution, output_format)
|
gs_device, gs_ext, alpha, resolution, output_format)
|
||||||
|
|
||||||
# Crop the images
|
# Crop the images
|
||||||
|
@ -50,8 +50,8 @@
|
|||||||
# What does this script do?
|
# What does this script do?
|
||||||
# 1) Call latex/pdflatex/xelatex/whatever (CONVERTER parameter)
|
# 1) Call latex/pdflatex/xelatex/whatever (CONVERTER parameter)
|
||||||
# 2) If the output is a PDF fallback to legacy
|
# 2) If the output is a PDF fallback to legacy
|
||||||
# 3) Otherwise check each page of the DVI (with dv2dt) looking for
|
# 3) Otherwise check each page of the DVI (with dv2dt) looking for
|
||||||
# PostScript literals, not well supported by dvipng. Pages
|
# PostScript literals, not well supported by dvipng. Pages
|
||||||
# containing them are passed to the legacy method in a new LaTeX file.
|
# containing them are passed to the legacy method in a new LaTeX file.
|
||||||
# 4) Call dvipng on the pages without PS literals
|
# 4) Call dvipng on the pages without PS literals
|
||||||
# 5) Join metrics info coming from both methods (legacy and dvipng)
|
# 5) Join metrics info coming from both methods (legacy and dvipng)
|
||||||
@ -67,7 +67,7 @@ import glob, os, re, string, sys
|
|||||||
|
|
||||||
from legacy_lyxpreview2ppm import legacy_conversion, \
|
from legacy_lyxpreview2ppm import legacy_conversion, \
|
||||||
legacy_conversion_step2, legacy_extract_metrics_info, filter_pages
|
legacy_conversion_step2, legacy_extract_metrics_info, filter_pages
|
||||||
|
|
||||||
from lyxpreview_tools import copyfileobj, error, find_exe, \
|
from lyxpreview_tools import copyfileobj, error, find_exe, \
|
||||||
find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \
|
find_exe_or_terminate, make_texcolor, mkstemp, run_command, warning, \
|
||||||
write_metrics_info, join_metrics_and_rename
|
write_metrics_info, join_metrics_and_rename
|
||||||
@ -80,10 +80,10 @@ latex_file_re = re.compile("\.tex$")
|
|||||||
path = string.split(os.environ["PATH"], os.pathsep)
|
path = string.split(os.environ["PATH"], os.pathsep)
|
||||||
|
|
||||||
def usage(prog_name):
|
def usage(prog_name):
|
||||||
return "Usage: %s <format> <latex file> <dpi> <fg color> <bg color>\n"\
|
return "Usage: %s <format> <latex file> <dpi> <fg color> <bg color>\n" \
|
||||||
"\twhere the colors are hexadecimal strings, eg 'faf0e6'"\
|
"\twhere the colors are hexadecimal strings, eg 'faf0e6'" \
|
||||||
% prog_name
|
% prog_name
|
||||||
|
|
||||||
# Returns a list of tuples containing page number and ascent fraction
|
# Returns a list of tuples containing page number and ascent fraction
|
||||||
# extracted from dvipng output.
|
# extracted from dvipng output.
|
||||||
# Use write_metrics_info to create the .metrics file with this info
|
# Use write_metrics_info to create the .metrics file with this info
|
||||||
@ -127,7 +127,7 @@ def extract_metrics_info(dvipng_stdout):
|
|||||||
|
|
||||||
if success == 0:
|
if success == 0:
|
||||||
error("Failed to extract metrics info from dvipng")
|
error("Failed to extract metrics info from dvipng")
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
@ -135,11 +135,11 @@ def color_pdf(latex_file, bg_color, fg_color):
|
|||||||
use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)((pdftex|xetex)\]{preview})")
|
use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)((pdftex|xetex)\]{preview})")
|
||||||
|
|
||||||
tmp = mkstemp()
|
tmp = mkstemp()
|
||||||
|
|
||||||
fg = ""
|
fg = ""
|
||||||
if fg_color != "0.000000,0.000000,0.000000":
|
if fg_color != "0.000000,0.000000,0.000000":
|
||||||
fg = ' \\AtBeginDocument{\\let\\oldpreview\\preview\\renewcommand\\preview{\\oldpreview\\color[rgb]{%s}}}\n' % (fg_color)
|
fg = ' \\AtBeginDocument{\\let\\oldpreview\\preview\\renewcommand\\preview{\\oldpreview\\color[rgb]{%s}}}\n' % (fg_color)
|
||||||
|
|
||||||
success = 0
|
success = 0
|
||||||
try:
|
try:
|
||||||
for line in open(latex_file, 'r').readlines():
|
for line in open(latex_file, 'r').readlines():
|
||||||
@ -212,12 +212,12 @@ def find_ps_pages(dvi_file):
|
|||||||
# FIXME: try with pdflatex
|
# FIXME: try with pdflatex
|
||||||
if not os.path.isfile(dvi_file):
|
if not os.path.isfile(dvi_file):
|
||||||
error("No DVI output.")
|
error("No DVI output.")
|
||||||
|
|
||||||
# Check for PostScript specials in the dvi, badly supported by dvipng
|
# Check for PostScript specials in the dvi, badly supported by dvipng
|
||||||
# This is required for correct rendering of PSTricks and TikZ
|
# This is required for correct rendering of PSTricks and TikZ
|
||||||
dv2dt = find_exe_or_terminate(["dv2dt"], path)
|
dv2dt = find_exe_or_terminate(["dv2dt"], path)
|
||||||
dv2dt_call = '%s "%s"' % (dv2dt, dvi_file)
|
dv2dt_call = '%s "%s"' % (dv2dt, dvi_file)
|
||||||
|
|
||||||
# The output from dv2dt goes to stdout
|
# The output from dv2dt goes to stdout
|
||||||
dv2dt_status, dv2dt_output = run_command(dv2dt_call)
|
dv2dt_status, dv2dt_output = run_command(dv2dt_call)
|
||||||
psliteral_re = re.compile("^special[1-4] [0-9]+ '(\"|ps:)")
|
psliteral_re = re.compile("^special[1-4] [0-9]+ '(\"|ps:)")
|
||||||
@ -243,7 +243,7 @@ def find_ps_pages(dvi_file):
|
|||||||
if psliteral_re.match(line) != None:
|
if psliteral_re.match(line) != None:
|
||||||
# Literal PostScript special detected!
|
# Literal PostScript special detected!
|
||||||
page_has_ps = True
|
page_has_ps = True
|
||||||
|
|
||||||
# Create the -pp parameter for dvipng
|
# Create the -pp parameter for dvipng
|
||||||
pages_parameter = ""
|
pages_parameter = ""
|
||||||
if len(ps_pages) > 0 and len(ps_pages) < page_index:
|
if len(ps_pages) > 0 and len(ps_pages) < page_index:
|
||||||
@ -362,7 +362,7 @@ def main(argv):
|
|||||||
# page_count: total number of pages
|
# page_count: total number of pages
|
||||||
# pages_parameter: parameter for dvipng to exclude pages with PostScript
|
# pages_parameter: parameter for dvipng to exclude pages with PostScript
|
||||||
(ps_pages, page_count, pages_parameter) = find_ps_pages(dvi_file)
|
(ps_pages, page_count, pages_parameter) = find_ps_pages(dvi_file)
|
||||||
|
|
||||||
# If all pages need PostScript, directly use the legacy method.
|
# If all pages need PostScript, directly use the legacy method.
|
||||||
if len(ps_pages) == page_count:
|
if len(ps_pages) == page_count:
|
||||||
vec = [argv[0], argv[2], argv[3], argv[1], argv[4], argv[5], latex]
|
vec = [argv[0], argv[2], argv[3], argv[1], argv[4], argv[5], latex]
|
||||||
@ -391,16 +391,16 @@ def main(argv):
|
|||||||
filter_pages(latex_file, legacy_latex_file, ps_pages)
|
filter_pages(latex_file, legacy_latex_file, ps_pages)
|
||||||
|
|
||||||
# Pass the new LaTeX file to the legacy method
|
# Pass the new LaTeX file to the legacy method
|
||||||
vec = [ argv[0], latex_file_re.sub("_legacy.tex", argv[2]), \
|
vec = [ argv[0], latex_file_re.sub("_legacy.tex", argv[2]),
|
||||||
argv[3], argv[1], argv[4], argv[5], latex ]
|
argv[3], argv[1], argv[4], argv[5], latex ]
|
||||||
legacy_metrics = legacy_conversion(vec, True)[1]
|
legacy_metrics = legacy_conversion(vec, True)[1]
|
||||||
|
|
||||||
# Now we need to mix metrics data from dvipng and the legacy method
|
# Now we need to mix metrics data from dvipng and the legacy method
|
||||||
original_bitmap = latex_file_re.sub("%d." + output_format, legacy_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)
|
destination_bitmap = latex_file_re.sub("%d." + output_format, latex_file)
|
||||||
|
|
||||||
# Join metrics from dvipng and legacy, and rename legacy bitmaps
|
# Join metrics from dvipng and legacy, and rename legacy bitmaps
|
||||||
join_metrics_and_rename(dvipng_metrics, legacy_metrics, ps_pages,
|
join_metrics_and_rename(dvipng_metrics, legacy_metrics, ps_pages,
|
||||||
original_bitmap, destination_bitmap)
|
original_bitmap, destination_bitmap)
|
||||||
|
|
||||||
# Convert images to ppm format if necessary.
|
# Convert images to ppm format if necessary.
|
||||||
@ -409,7 +409,7 @@ def main(argv):
|
|||||||
|
|
||||||
# Actually create the .metrics file
|
# Actually create the .metrics file
|
||||||
write_metrics_info(dvipng_metrics, metrics_file)
|
write_metrics_info(dvipng_metrics, metrics_file)
|
||||||
|
|
||||||
return (0, dvipng_metrics)
|
return (0, dvipng_metrics)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -29,7 +29,7 @@ if os.name == "nt":
|
|||||||
import win32security
|
import win32security
|
||||||
import winerror
|
import winerror
|
||||||
except:
|
except:
|
||||||
sys.stderr.write("Consider installing the PyWin extension modules "\
|
sys.stderr.write("Consider installing the PyWin extension modules " \
|
||||||
"if you're irritated by windows appearing briefly.\n")
|
"if you're irritated by windows appearing briefly.\n")
|
||||||
use_win32_modules = 0
|
use_win32_modules = 0
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ def filter_pages(source_path, destination_path, pages_to_keep):
|
|||||||
|
|
||||||
destination_file.close()
|
destination_file.close()
|
||||||
source_file.close()
|
source_file.close()
|
||||||
|
|
||||||
# Joins two metrics list, that is a list of tuple (page_index, metric)
|
# 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
|
# 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
|
# e.g. new_page_indexes[3] == 14 means that the 4th item in new_metrics is the 15th in the original counting
|
||||||
@ -263,8 +263,7 @@ def join_metrics_and_rename(original_metrics, new_metrics, new_page_indexes, ori
|
|||||||
# Goes through the array until the end is reached or the correct index is found
|
# 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:
|
while legacy_index < len(original_metrics) and original_metrics[legacy_index][0] < index:
|
||||||
legacy_index += 1
|
legacy_index += 1
|
||||||
|
|
||||||
|
|
||||||
# Add or update the metric for this page
|
# Add or update the metric for this page
|
||||||
if legacy_index < len(original_metrics) and original_metrics[legacy_index][0] == index:
|
if legacy_index < len(original_metrics) and original_metrics[legacy_index][0] == index:
|
||||||
original_metrics[legacy_index] = (index, metric)
|
original_metrics[legacy_index] = (index, metric)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user