From 08ad6acd9a4de15fcc3631631915eff9a2cd03dc Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Fri, 23 May 2003 14:36:26 +0000 Subject: [PATCH] Enable the external inset to output different flavours of latex. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7031 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ChangeLog | 6 +++++ lib/external_templates | 10 ++++++-- lib/scripts/fig2png.py | 16 +++++++++++++ lib/scripts/fig2png.sh | 48 ++++++++++++++++++++++++++++++++++++++ lib/scripts/fig2pstex.py | 4 ++-- src/insets/ChangeLog | 6 +++++ src/insets/insetexternal.C | 32 ++++++++++++++++++------- 7 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 lib/scripts/fig2png.py create mode 100755 lib/scripts/fig2png.sh diff --git a/lib/ChangeLog b/lib/ChangeLog index bdc4ddeebf..c9d37577da 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,9 @@ +2003-05-23 Angus Leeming + + * external_templates: add a PDFLaTeX flavour to the xfig outputs. + * scripts/fig2png.py: a new and very simple script. + * scripts/fig2png.sh: another new and rather more sophisticated scriot. + 2003-05-22 Alfredo Braunstein * ui/stdmenus.ui: diff --git a/lib/external_templates b/lib/external_templates index 5de7fce4fb..5d758797f7 100644 --- a/lib/external_templates +++ b/lib/external_templates @@ -67,9 +67,15 @@ Template XFig EditCommand "xfig $$FName" AutomaticProduction true Format LaTeX - Product "\\begin{picture}(0,0)\\includegraphics{$$Basename.eps}\\end{picture}\\input{$$Basename.pstex_t}" + Product "\\input{$$Basename.pstex_t}" UpdateCommand "python $$Sysdir/scripts/fig2pstex.py $$FName $$Parameters" - UpdateResult "$$Basename.eps" + UpdateResult "$$Basename.pstex_t" + Requirement "graphicx" + FormatEnd + Format PDFLaTeX + Product "\\begin{picture}(0,0)\\includegraphics{$$Basename}\\end{picture}" + UpdateCommand "python $$Sysdir/scripts/fig2png.py $$FName $$Parameters" + UpdateResult "$$Basename.png" Requirement "graphicx" FormatEnd Format Ascii diff --git a/lib/scripts/fig2png.py b/lib/scripts/fig2png.py new file mode 100644 index 0000000000..3bce89707b --- /dev/null +++ b/lib/scripts/fig2png.py @@ -0,0 +1,16 @@ +#! /usr/bin/env python +# This script converts a xfig file into PNG files + +import sys +import os + +filename = sys.argv[1] +basename = os.path.splitext(filename)[0] +parameters = sys.argv[2:] + +pid = os.fork() +if pid == 0: + os.execvp("fig2dev", ["fig2dev", "-Lpng"] + parameters + [filename, basename + ".png"]) + print "fig2dev did not work" + os.exit(1) +os.wait() diff --git a/lib/scripts/fig2png.sh b/lib/scripts/fig2png.sh new file mode 100755 index 0000000000..ec51fdd4b6 --- /dev/null +++ b/lib/scripts/fig2png.sh @@ -0,0 +1,48 @@ +#! /bin/sh +# converts an image from XFIG to PNG format +# We go the long route to ensure that the image is of the highest +# possible quality. + +# We expect a single arg, the name of the input file. +test $# -eq 1 || exit 1 + +input=`basename $1` +base=`basename ${input} .fig` +test ${input} = ${base} && { + echo Expecting an XFIG file as input + exit 1 +} + +dir=`dirname $1` +base=${dir}/${base} + +# Generate the fig2dev output +eps=${base}.eps +pstex_t=${base}.pstex_t + +echo Entered FIG2PNG.SH + +fig2dev -Lpstex ${input} ${eps} +fig2dev -Lpstex_t -p${base} ${input} ${pstex_t} + +# Convert the EPS file (free of "special" text) to PNG format using gs +# gs is extremely fussy about the EPS files it converts, so ensure it is +# "clean" first. +clean_eps=${eps}.$$ +eps2eps ${eps} ${clean_eps} + +# Extract the width and height of the image using gs' bbox device. +# Ie, take output that includes a line "%%BoundingBox: 0 0 " +# and rewrite it as "-gx" +geometry=`gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox ${clean_eps} 2>&1 | \ + sed '/^%%BoundingBox/! d' | cut -d' ' -f4,5 | \ + sed 's/^\([0-9]\{1,\}\) \([0-9]\{1,\}\)$/-g\1x\2/'` + +# Generate the bitmap using the -g option to ensure the size is the same +# as the original. +# If we're using a version of gs that does not have a bbox device, then +# $GEOMETRY = "", so there are no unwanted side effects. +png=${base}.png +gs -q -dSAFER -dBATCH -dNOPAUSE ${geometry} -sDEVICE=png16m \ + -sOutputFile=${png} ${clean_eps} +rm -f ${clean_eps} ${eps} diff --git a/lib/scripts/fig2pstex.py b/lib/scripts/fig2pstex.py index e643290036..741665736f 100644 --- a/lib/scripts/fig2pstex.py +++ b/lib/scripts/fig2pstex.py @@ -17,7 +17,7 @@ os.wait() pid = os.fork() if pid == 0: - os.execvp("fig2dev", ["fig2dev", "-Lpstex_t"] + parameters + [filename, basename + ".pstex_t"]) - print "convert did not work second time" + os.execvp("fig2dev", ["fig2dev", "-Lpstex_t", "-p" + basename] + parameters + [filename, basename + ".pstex_t"]) + print "fig2dev did not work the second time" os.exit(1) os.wait() diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index f51c6c5b7b..3c761ca39c 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,9 @@ +2003-05-23 Angus Leeming + + * insetexternal.C (write): check how many lines are output. + (latex): use the "PDFLaTeX" flavour if outputting to pfdlatex and + if the template has defined it. + 2003-05-23 Angus Leeming * insetquotes (validate): use the new LaTeXFeatures::useBabel() method. diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index 0ffdcc7973..872bc63a87 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -11,14 +11,16 @@ #include #include "insetexternal.h" -#include "ExternalTemplate.h" -#include "BufferView.h" + #include "buffer.h" -#include "funcrequest.h" -#include "lyx_main.h" -#include "LaTeXFeatures.h" -#include "gettext.h" +#include "BufferView.h" #include "debug.h" +#include "ExternalTemplate.h" +#include "funcrequest.h" +#include "gettext.h" +#include "LaTeXFeatures.h" +#include "latexrunparams.h" +#include "lyx_main.h" #include "lyxlex.h" #include "Lsstream.h" @@ -27,6 +29,7 @@ #include "support/filetools.h" #include "support/lstrings.h" +#include "support/lyxalgo.h" #include "support/path.h" #include "support/systemcall.h" #include "support/FileInfo.h" @@ -161,14 +164,25 @@ int InsetExternal::write(string const & format, } updateExternal(format, buf); - os << doSubstitution(buf, cit->second.product); - return 0; // CHECK (FIXME check what ? - jbl) + string const str = doSubstitution(buf, cit->second.product); + os << str; + return int(lyx::count(str.begin(), str.end(),'\n') + 1); } int InsetExternal::latex(Buffer const * buf, ostream & os, - LatexRunParams const &) const + LatexRunParams const & runparams) const { + // If the template has specified a PDFLaTeX output, then we try and + // use that. + if (runparams.flavor == LatexRunParams::PDFLATEX) { + ExternalTemplate const & et = params_.templ; + ExternalTemplate::Formats::const_iterator cit = + et.formats.find("PDFLaTeX"); + if (cit != et.formats.end()) + return write("PDFLaTeX", buf, os); + } + return write("LaTeX", buf, os); }