doclib: added encoding options for doxygen
[senf.git] / doclib / textogif
1 #! /usr/bin/perl
2 #
3 #                          T E X T O G I F
4 #
5 #                          by John Walker
6 #                      http://www.fourmilab.ch/
7 #
8                      $version = '1.1 (2003-11-07)';
9 #
10 #
11 #   Converts a LaTeX file containing equations(s) into a GIF file for
12 #   embedding into an HTML document.  The black and white image of the
13 #   equation is created at high resolution and then resampled to the
14 #   target resolution to antialias what would otherwise be jagged
15 #   edges.
16 #
17 #   Online documentation with sample output is available on the Web
18 #   at http://www.fourmilab.ch/webtools/textogif/
19 #
20 #   Write your equation (or anything else you can typeset with LaTeX)
21 #   in a file like:
22 #
23 #       \documentclass[12pt]{article}
24 #       \pagestyle{empty}
25 #       \begin{document}
26 #
27 #       \begin{displaymath}
28 #       \bf  % Compiled formulae often look better in boldface
29 #       \int H(x,x')\psi(x')dx' = -\frac{\hbar2}{2m}\frac{d2}{dx2}
30 #                                 \psi(x)+V(x)\psi(x)
31 #       \end{displaymath}
32 #
33 #       \end{document}
34 #
35 #   The "\pagestyle{empty}" is required to avoid generating a huge
36 #   image with a page number at the bottom.
37 #
38 #   Then (assuming you have all the software described below installed
39 #   properly), you can simply say:
40 #
41 #       textogif [options] filename ...
42 #
43 #   to compile filename.tex to filename.gif, an interlaced,
44 #   transparent background GIF file ready to use an an inline image.
45 #   You can specify the base name, for example, "schrod", rather than
46 #   the full name of the TeX file ("schrod.tex").  TeX requires the
47 #   input file to have an extension of ".tex".  The command line
48 #   options are described in the help text at the end of this program
49 #   and in the "Default Configuration" section below.
50 #
51 #   A sample IMG tag, including the image width and height is printed
52 #   on standard error, for example:
53 #
54 #       <img src="schrod.gif" width=508 height=56>
55 #
56 #                         Required Software
57 #
58 #   This script requires the following software to be installed
59 #   in the standard manner.  Version numbers are those used in the
60 #   development and testing of the script.
61 #
62 #   Perl        5.8.0 (anything later than 4.036 should work)
63 #   TeX         3.14159 (Web2C 7.3.1)
64 #   LaTeX2e     <2000/06/01>
65 #   dvips       dvipsk 5.86
66 #   Ghostscript 6.52 (2001-10-20)
67 #   Netpbm      9.24
68 #
69 #
70 #                       Default Configuration
71 #
72 #   The following settings are the defaults used if the -dpi and
73 #   -res options are not specified on the command line.
74 #
75 #   The parameter $dpi controls how large the equation will appear
76 #   with respect to other inline images and the surrounding text.
77 #   The parameter is expressed in "dots per inch" in the PostScript
78 #   sense.  Unfortunately, since there's no standard text size in
79 #   Web browsers (and most allow the user to change fonts and
80 #   point sizes), there's no "right" value for this setting.  The
81 #   default of 150 seems about right for most documents.  A setting
82 #   of 75 generates equations at half the normal size, while 300
83 #   doubles the size of equations.  The setting of $dpi can always be
84 #   overridden by specifying the "-dpi" command line option.
85 #
86     $dpi = 150;
87 #
88 #   The parameter $res specifies the oversampling as the ratio
89 #   of the final image size to the initial black and white image.
90 #   Smaller values produce smoothing with more levels of grey but
91 #   require (much) more memory and intermediate file space to create
92 #   the image.  If you run out of memory or disc space with the
93 #   default value of 0.5, try changing it to 0.75.  A $res setting of
94 #   1.0 disables antialiasing entirely.  The setting of $res can
95 #   always be overridden by specifying the "res" command line option.
96 #
97     $res = 0.5;
98 #
99 #   The $background parameter supplies a command, which may be
100 #   void, to be inserted in the image processing pipeline to
101 #   adjust the original black-on-white image so that its background
102 #   agrees with that of the document in which it is to be inserted.
103 #   For a document with the default grey background used by Mosaic
104 #   and old versions of Netscape, use:
105 #
106 #       $background = "ppmdim 0.7 |";  $transparent = "b2/b2/b2";
107 #
108 #   If your document uses a white background, the void specification:
109 #
110 #       $background = "";  $transparent = "ff/ff/ff";
111 #
112 #   should be used.  For colour or pattern backgrounds, you'll have
113 #   to hack the code.  The reason for adjusting the background is to
114 #   ensure that when the image is resampled and then output with a
115 #   transparent background the edges of the characters will fade
116 #   smoothly into the page background.  Otherwise you'll get a
117 #   distracting "halo" around each character.  You can override this
118 #   default specification with the -grey command line option.
119 #
120     $background = "";  $transparent = "ff/ff/ff";
121 #
122 #   Image generation and decoding commands for GIF and PNG output.
123 #
124     $cmdGIF = 'ppmtogif';
125     $cmdGIFdecode = 'giftopnm';
126     $cmdPNG = 'pnmtopng';
127     $cmdPNGdecode = 'pngtopnm';
128 #
129 #   Default image creation modes
130 #
131     $imageCmd = $cmdGIF;
132     $imageCmdD = $cmdGIFdecode;
133     $imageExt = 'gif';
134
135     #
136     #   Command line option processing
137     #
138     while ($ARGV[0] =~ m/^-/) {
139         $_ = shift(@ARGV);
140         s/^--/-/;                     # Allow GNU-style -- options
141         if (m/^-d/) {                 # -dpi nnn
142             $dpi = shift(@ARGV);
143         } elsif (m/^-gi/) {           # -gif
144             $imageCmd = $cmdGIF;
145             $imageCmdD = $cmdGIFdecode;
146             $imageExt = 'gif';
147         } elsif (m/^-gr/) {           # -grey n
148             $grey = shift(@ARGV);
149             $background = "ppmdim $grey | ";
150             $greylev = int(255 * $grey);
151             $transparent = sprintf("%02x/%02x/%02x", $greylev, $greylev, $greylev);
152         } elsif (m/^-h/) {            # -help
153             &help();
154             exit(0);
155         } elsif (m/^-p/) {            # -png
156             $imageCmd = $cmdPNG;
157             $imageCmdD = $cmdPNGdecode;
158             $imageExt = 'png';
159         } elsif (m/^-r/) {            # -res nnn
160             $res = shift(@ARGV);
161         } elsif (m/^-v/) {            # -version
162             print("Version $version\n");
163             exit(0);
164         }
165     }
166     #
167     #   Main file processing loop
168     #
169     foreach $f (@ARGV) {
170         $f =~ s/(.*)\.tex$/$1/;
171         &syscmd("echo x | latex  $f \n");
172         &syscmd("dvips -f $f >_temp_$$.ps\n");
173             
174         #   Assemble and execute the command pipeline which generates the image.
175
176         #   Start by invoking Ghostscript with the pbmraw output device and
177         #   output file set to standard output ("-") and the requested resolution.
178         #   The -q (Quiet) option is required; otherwise Ghostscript will send
179         #   processing information to standard output and corrupt transmission
180         #   of the bitmap to the next component in the pipeline.
181         $cmd = "echo quit | gs -q -dNOPAUSE  -r" . int($dpi / $res). "x". int($dpi / $res) .
182                 " -sOutputFile=- -sDEVICE=ppmraw _temp_$$.ps | " .
183                 
184         #   Next we crop white space surrounding the generated text, promote
185         #   the monochrome bitmap to a grey scale image with 8 bits per pixel,
186         #   apply whatever background adjustment transform is requested, and
187         #   scale the image to the desired size.
188                 "pnmcrop -white | pnmpad -white -left " . int(10/$res) . 
189                 " -right " . int(10/$res) . " -top " . int(10/$res) . 
190                 " -bottom " . int(10/$res) . " | " .
191                 " $background pnmscale " . $res . " | " .
192                 
193         #   Finally, convert the image to the desired output format and write
194         #   the output file.
195                 "$imageCmd -interlace -transparent rgb:$transparent >$f.$imageExt";
196         &syscmd($cmd);
197
198         #   Sweep up debris left around by the various intermediate steps
199         &syscmd("rm $f.dvi $f.aux $f.log _temp_$$.ps");
200
201         #   Print the reference to include this figure, including width and height,
202         #   to standard error.
203         $r = `$imageCmdD $f.$imageExt | pnmfile`;
204         $r =~ m/(\d+) by (\d+)/;
205         print(STDERR "<img src=\"$f.$imageExt\" width=\"$1\" height=\"$2\">\n");
206     }
207     
208     #   Echo and execute a system command
209     
210     sub syscmd {
211         local ($cmd) = @_;
212         
213         print(STDERR "$cmd\n");
214         system($cmd) == 0 || die("Error processing command:\n\t$cmd\n\t");
215     }
216
217     #   Print help text
218     
219     sub help {
220         print <<"EOD"
221 usage: textogif [ options ] texfile...
222     Options:
223         -dpi n          Set rendering dots per inch to n (default 150)
224         -gif            Generate GIF image (default)
225         -grey           Grey scale background level: 0 = black, 1 = white (default)
226         -help           Print this message
227         -png            Generate PNG image
228         -res n          Set oversampling ratio, smaller = finer (default 0.5)
229         -version        Print version number
230 For documentation and the latest version of this program
231 please visit the Web page:
232     http://www.fourmilab.ch/webtools/textogif/
233 EOD
234 ;
235     }