Back to index

wims  3.65+svn20090927
texgif.c
Go to the documentation of this file.
00001 /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 /* tex 2 gif translator */
00019 
00020 #include "texgif.h"
00021 char *progname;
00022 char *tmpdir="/tmp";
00023 char *fontdir="/tmp";
00024 char *headerfile="";
00025 char *texstyle="";
00026 char *outfile="/tmp/texgif.gif";
00027 char cwd[1024];
00028 double blacker=0.4;
00029 int basedensity=100, density;
00030 int compressratio=1;
00031 int wrapexec=0;
00032 int currentcolor;
00033 
00034 FONT wfont[FONT_NUMBER_LIMIT];
00035 int fontcnt;
00036 
00037 enum {tt_plain, tt_latex};
00038 int textype=tt_plain;
00039 char *texname="tex";
00040 
00041 #include "basic.c"
00042 #include "colors.c"
00043 #include "tfm.c"
00044 #include "gf.c"
00045 #include "font.c"
00046 #include "image.c"
00047 #include "dvi.c"
00048 
00049 void checktextype(char *p)
00050 {
00051     if(strstr(p,"\\begin{")!=NULL ||
00052        strstr(p,"\\end{")!=NULL ||
00053        strstr(p,"\\underset")!=NULL ||
00054        strstr(p,"\\overset")!=NULL ||
00055        strstr(p,"\\displaystyle")!=NULL) {
00056        textype=tt_latex; texname="latex";
00057     }
00058 }
00059 
00060        /* pass thru tex */
00061 void tex(void)
00062 {
00063     char *src;
00064     char *hd;
00065     char fbuf[1024], nbuf[2048];
00066     char *parmbuf[8];
00067     FILE *f;
00068     long int l;
00069     src=getenv("texgif_src");
00070     if(src==NULL || *src==0) error("No source.");
00071     checktextype(src);
00072     snprintf(fbuf,sizeof(fbuf),"%s/texgif.tex",tmpdir);
00073     if(*headerfile) {
00074        if(strstr(headerfile,".tex")==NULL)
00075          snprintf(nbuf,sizeof(nbuf),"%s.%s",headerfile,texname);
00076        else
00077          snprintf(nbuf,sizeof(nbuf),"%s",headerfile);
00078        f=fopen(nbuf,"r");
00079        if(f!=NULL) {
00080            fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
00081            if(l>0 && l<FILE_LENGTH_LIMIT) {
00082               hd=xmalloc(l+16); fread(hd,1,l,f);
00083            }
00084            else {l=0; hd="";}
00085            fclose(f); f=fopen(fbuf,"w");
00086            fwrite(hd,1,l,f); free(hd);
00087        }
00088        else f=fopen(fbuf,"w");
00089     }
00090     else f=fopen(fbuf,"w");
00091     if(textype==tt_plain) {
00092        fprintf(f,"%s %s %s\n\\end\n",texstyle, src, texstyle); fclose(f);
00093     }
00094     else {
00095        fprintf(f,"{\\newpage\\clearpage\n\
00096 %s %s %s\n\
00097 \\clearpage}\n\
00098 \\end{document}\n",texstyle, src, texstyle); fclose(f);
00099     }
00100     chdir(tmpdir);
00101     parmbuf[0]=texname; parmbuf[1]="texgif"; parmbuf[2]=NULL;
00102     wrapexec=1;
00103     execredirected(texname,NULL,NULL,NULL,parmbuf);
00104     if(cwd[0]) chdir(cwd);
00105 }
00106 
00107 void parms(void)
00108 {
00109     char *p;
00110     int t;
00111     p=getenv("tmp_dir"); if(p!=NULL && *p!=0) tmpdir=p;
00112     p=getenv("texgif_tmpdir"); if(p!=NULL && *p!=0) tmpdir=p;
00113     p=getenv("texgif_fontdir"); if(p!=NULL && *p!=0) fontdir=p;
00114     p=getenv("texgif_outfile"); if(p!=NULL && *p!=0) outfile=p;
00115     p=getenv("texgif_texheader"); if(p!=NULL && *p!=0) headerfile=p;
00116     p=getenv("texgif_style"); if(p!=NULL && *p!=0) texstyle=p;
00117     p=getenv("texgif_density"); if(p!=NULL && *p!=0) {
00118        t=atoi(p); if(t>10 && t<1000) basedensity=t;
00119     }
00120     p=getenv("texgif_compressratio"); if(p!=NULL && *p!=0) {
00121        t=atoi(p); if(t>1 && t<=5) compressratio=t;
00122     }
00123     if(getcwd(cwd,sizeof(cwd))==NULL) cwd[0]=0;
00124 }
00125 
00126 int main(int argc, char *argv[])
00127 {
00128     progname=argv[0];
00129     parms();
00130     fontcnt=0;
00131     tex();
00132     setreuid(geteuid(),geteuid());setregid(getegid(),getegid());
00133     dvi();
00134     return 0;
00135 }
00136