From 35b580fb30f266767938755140c9496141b297da Mon Sep 17 00:00:00 2001 From: Julien Rioux Date: Mon, 3 Oct 2011 16:43:33 +0000 Subject: [PATCH] lyxpreview: Simplify the color handling. Let's have this in trunk for testing. The real difference maker when it comes to color is whether we use dvipng or ghostscript. For dvipng: - The color info is passed as command-line arguments. - The tightpage option is not necessary, and since it adds ps specials to the output, we shouldn't use it. For ghostscript: - The color info needs to be in the latex file. - The foreground color is set for each preview inset. - The background color is set by \pagecolor in the preamble, which is understood by pdflatex, but ignored in dvips mode. Thus dvips is handled with a ps special. - The tightpage option is necessary to crop the images. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39797 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/scripts/legacy_lyxpreview2ppm.py | 61 +++++++++++++++------------- lib/scripts/lyxpreview2bitmap.py | 43 -------------------- src/graphics/PreviewLoader.cpp | 21 +--------- 3 files changed, 33 insertions(+), 92 deletions(-) diff --git a/lib/scripts/legacy_lyxpreview2ppm.py b/lib/scripts/legacy_lyxpreview2ppm.py index af01505398..7c6e1e6274 100644 --- a/lib/scripts/legacy_lyxpreview2ppm.py +++ b/lib/scripts/legacy_lyxpreview2ppm.py @@ -194,42 +194,45 @@ def extract_resolution(log_file, dpi): return dpi * (10.0 / fontsize) * (1000.0 / magnification) -def legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr): - use_preview_dvi_re = re.compile("(\s*\\\\usepackage\[[^]]+)(dvips\]{preview})") - use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)(pdftex\]{preview})") +def legacy_latex_file(latex_file, fg_color, bg_color): + use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}") + fg_color_gr = make_texcolor(fg_color, True) + bg_color_gr = make_texcolor(bg_color, True) tmp = mkstemp() success = 0 try: - for line in open(latex_file, 'r').readlines(): - match = use_preview_dvi_re.match(line) - if match == None: - match = use_preview_pdf_re.match(line) - if match == None: - tmp.write(line) - continue - success = 1 - tmp.write(" \\usepackage{color}\n" \ - " \\pagecolor[rgb]{%s}\n" \ - "%s\n" \ - % (bg_color_gr, match.group())) - continue - - success = 1 - tmp.write("%stightpage,%s\n" \ - " \\AtBeginDocument{\\AtBeginDvi{%%\n" \ - " \\special{!userdict begin/bop-hook{//bop-hook exec\n" \ - " <%s%s>{255 div}forall setrgbcolor\n" \ - " clippath fill setrgbcolor}bind def end}}}\n" \ - % (match.group(1), match.group(2), fg_color, bg_color)) - + f = open(latex_file, 'r') 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`) + for line in f.readlines(): + if success: + tmp.write(line) + continue + match = use_preview_re.match(line) + if match == None: + tmp.write(line) + 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,lyx,tightpage]{preview} +\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))) + if success: copyfileobj(tmp, open(latex_file,"wb"), 1) @@ -284,10 +287,10 @@ def legacy_conversion(argv, skipMetrics = False): def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color, latex, pdf_output = False, skipMetrics = False): - # Move color information into the latex file. - bg_color_gr = make_texcolor(bg_color, True) - if not legacy_latex_file(latex_file, fg_color, bg_color, bg_color_gr): - error("Unable to move color info into the latex file") + # Move color information, lyx and tightpage options into the latex file. + if not legacy_latex_file(latex_file, fg_color, bg_color): + error("""Unable to move the color information, and the lyx and tightpage + options of preview-latex, into the latex file""") # Compile the latex file. latex_status, latex_stdout = run_latex(latex, latex_file) diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py index c4c1844b66..09ac3a9368 100755 --- a/lib/scripts/lyxpreview2bitmap.py +++ b/lib/scripts/lyxpreview2bitmap.py @@ -155,42 +155,6 @@ def extract_metrics_info(dvipng_stdout): return results -def color_pdf(latex_file, bg_color, fg_color): - use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)((pdftex|xetex)\]{preview})") - - tmp = mkstemp() - - fg = "" - if fg_color != "0.000000,0.000000,0.000000": - fg = ' \\AtBeginDocument{\\let\\oldpreview\\preview\\renewcommand\\preview{\\oldpreview\\color[rgb]{%s}}}\n' % (fg_color) - - success = 0 - try: - for line in open(latex_file, 'r').readlines(): - match = use_preview_pdf_re.match(line) - if match == None: - tmp.write(line) - continue - success = 1 - tmp.write(" \\usepackage{color}\n" \ - " \\pagecolor[rgb]{%s}\n" \ - "%s" \ - "%s\n" \ - % (bg_color, fg, match.group())) - continue - - except: - # Unable to open the file, but do nothing here because - # the calling function will act on the value of 'success'. - warning('Warning in color_pdf! Unable to open "%s"' % latex_file) - warning(`sys.exc_type` + ',' + `sys.exc_value`) - - if success: - copyfileobj(tmp, open(latex_file,"wb"), 1) - - return success - - def fix_latex_file(latex_file): documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)") @@ -385,9 +349,6 @@ def main(argv): fg_color_dvipng = make_texcolor(fg_color, False) bg_color_dvipng = make_texcolor(bg_color, False) - fg_color_gr = make_texcolor(fg_color, True) - bg_color_gr = make_texcolor(bg_color, True) - # External programs used by the script. latex = find_exe_or_terminate(latex or latex_commands) bibtex = find_exe(bibtex or bibtex_commands) @@ -448,10 +409,6 @@ def main(argv): return legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color, latex, pdf_output) - # Move color information for PDF into the latex file. - if not color_pdf(latex_file, bg_color_gr, fg_color_gr): - warning("Unable to move color info into the latex file") - # Compile the latex file. latex_status, latex_stdout = run_latex(latex, latex_file, bibtex) diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp index a71fcd84d3..32b4ec1d86 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -741,26 +741,7 @@ void PreviewLoader::Impl::dumpPreamble(otexstream & os) const // Also support PDF output (automatically generated e.g. when // \usepackage[pdftex]{hyperref} is used and XeTeX. os << "\n" - << "\\newif\\ifxetex\n" - << "\\expandafter\\ifx\\csname XeTeXrevision\\endcsname\\relax\n" - << " \\xetexfalse\n" - << "\\else\n" - << " \\xetextrue\n" - << "\\fi\n" - << "\\newif\\ifpdf\n" - << "\\ifx\\pdfoutput\\undefined\n" - << "\\else\\ifx\\pdfoutput\\relax\n" - << "\\else\\ifnum0=\\pdfoutput\n" - << "\\else\\pdftrue\\fi\\fi\\fi\n" - << "\\ifxetex\n" - << " \\usepackage[active,delayed,tightpage,showlabels,lyx,xetex]{preview}\n" - << "\\else\n" - << "\\ifpdf\n" - << " \\usepackage[active,delayed,tightpage,showlabels,lyx,pdftex]{preview}\n" - << "\\else\n" - << " \\usepackage[active,delayed,showlabels,lyx,dvips]{preview}\n" - << "\\fi\n" - << "\\fi\n" + << "\\usepackage[active,delayed,showlabels,lyx]{preview}\n" << "\n"; } -- 2.39.5