]> git.lyx.org Git - lyx.git/blob - lib/scripts/fig2pdftex.sh
clean up french language handling
[lyx.git] / lib / scripts / fig2pdftex.sh
1 #! /bin/sh
2
3 # file fig2pdf.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # author Angus Leeming
8 #
9 # Full author contact details are available in file CREDITS
10
11
12 # This script converts an XFIG image to something that pdflatex can process
13 # into high quality PDF.
14
15 # Usage: sh fig2pdf.sh ${base}.xfig
16 # to generate ${base}.pdftex_t
17 # Thereafter, you need only '\input{${base}.pdftex_t}' in your latex document.
18
19 # The external programs
20 FIG2DEV=fig2dev
21 # Used only by legacy_xfig
22 GS=gs
23
24 find_exe() {
25     test $# -eq 1 || exit 1
26
27     type $1 > /dev/null || {
28         echo "Unable to find \"$1\". Please install."
29         exit 1
30     }
31 }
32
33 # modern_xfig() and legacy_xfig() are the functions that do all the work.
34
35 # Modern versions of xfig can output the image without "special" text as
36 # a PDF file ${base}.pdf and place the text in a LaTeX file
37 # ${base}.pdftex_t for typesetting by pdflatex itself.
38 modern_xfig() {
39     # Can we find fig2dev?
40     find_exe ${FIG2DEV}
41
42     input=$1
43     pdftex_t=$2
44     pdftex=$3.pdf
45
46     ${FIG2DEV} -Lpdftex -p1 ${input} ${pdftex}
47     ${FIG2DEV} -Lpdftex_t -p${outbase} ${input} ${pdftex_t}
48
49     exit 0;
50 }
51
52
53 # This function is used only by legacy_xfig.
54 # It manipulates the Bounding Box info to enable gs to produce
55 # the appropriate PDF file from an EPS one.
56 # The generated PostScript commands are extracted from epstopdf, distributed
57 # with tetex.
58 clean_epsfile() {
59     test $# -eq 1 || exit 1
60
61     # No bounding box info
62     grep '%%BoundingBox' $1 > /dev/null || return 1;
63
64     bbox=`sed -n '/^%%BoundingBox/p' $1`
65     llx=`echo ${bbox} | cut -d' ' -f2`
66     lly=`echo ${bbox} | cut -d' ' -f3`
67     urx=`echo ${bbox} | cut -d' ' -f4`
68     ury=`echo ${bbox} | cut -d' ' -f5`
69
70     width=`expr $urx - $llx`
71     height=`expr $ury - $lly`
72     xoffset=`expr 0 - $llx`
73     yoffset=`expr 0 - $lly`
74
75     temp=$1.??
76     sed "/^%%BoundingBox/{
77 s/^\(%%BoundingBox:\).*/\1 0 0 ${width} ${height}\\
78 << \/PageSize  [${width} ${height}] >> setpagedevice\\
79 gsave ${xoffset} ${yoffset} translate/
80 }" $1 > $temp
81
82     mv -f $temp $1
83 }
84
85
86 # Older versions of xfig cannot do this, so we emulate the behaviour using
87 # pstex and pstex_t output.
88 legacy_xfig() {
89     # Can we find fig2dev and epstopdf?
90     find_exe ${FIG2DEV}
91     find_exe ${GS}
92
93     input=$1
94     pdftex_t=$2
95     pdf=$3.pdf
96     pstex=$3.pstex
97
98     ${FIG2DEV} -Lpstex ${input} ${pstex}
99     ${FIG2DEV} -Lpstex_t -p${outbase} ${input} ${pdftex_t}
100
101     # Convert the ${pstex} EPS file (free of "special" text) to PDF format
102     # using gs
103     clean_epsfile ${pstex}
104     ${GS} -q -dNOPAUSE -dBATCH -dSAFER \
105         -sDEVICE=pdfwrite -sOutputFile=${pdf} ${pstex}
106     rm -f ${pstex}
107
108     exit 0;
109 }
110
111 # The main logic of the script is below.
112 # All it does is ascertain which of the two functions above to call.
113
114 # We expect two args, the names of the input and output files.
115 test $# -eq 2 || exit 1
116
117 input=$1
118 output=$2
119
120 # Fail silently if the file doesn't exist
121 test -r $input || exit 0
122
123 # Strip the extension from ${output}
124 outbase=`echo ${output} | sed 's/[.][^.]*$//'`
125
126 # Ascertain whether fig2dev is "modern enough".
127 # If it is, then the help info will mention "pdftex_t" as one of the
128 # available outputs.
129 CONVERT_IT=modern_xfig
130 ${FIG2DEV} -h | grep 'pdftex_t' > /dev/null || CONVERT_IT=legacy_xfig
131
132 ${CONVERT_IT} ${input} ${output} ${outbase}
133
134 # The end