Subversion Repositories wimsdev

Rev

Rev 8103 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* tex 2 gif translator */
  19.  
  20. #include "texgif.h"
  21.  
  22. char *progname;
  23. char *tmpdir="/tmp";
  24. char *fontdir="/tmp";
  25. char *headerfile="";
  26. char *texstyle="";
  27. char *outfile="/tmp/texgif.gif";
  28. char cwd[1024];
  29. double blacker=0.4;
  30. int basedensity=100, density;
  31. int compressratio=1;
  32. int wrapexec=0;
  33. int currentcolor;
  34.  
  35. FONT wfont[FONT_NUMBER_LIMIT];
  36. int fontcnt;
  37.  
  38. enum {tt_plain, tt_latex};
  39. int textype=tt_plain;
  40. char *texname="tex";
  41.  
  42. void checktextype(char *p)
  43. {
  44.     if(strstr(p,"\\begin{")!=NULL ||
  45.        strstr(p,"\\end{")!=NULL ||
  46.        strstr(p,"\\underset")!=NULL ||
  47.        strstr(p,"\\overset")!=NULL ||
  48.        strstr(p,"\\displaystyle")!=NULL ||
  49.        strstr(p,"\\mathcal")!=NULL) {
  50.         textype=tt_latex; texname="latex";
  51.     }
  52. }
  53.  
  54.         /* pass thru tex */
  55. void tex(void)
  56. {
  57.     char *src;
  58.     char *hd;
  59.     char fbuf[1024], nbuf[2048];
  60.     char *parmbuf[8];
  61.     FILE *f;
  62.     long int l;
  63.     src=getenv("texgif_src");
  64.     if(src==NULL || *src==0) error("No source.");
  65.     checktextype(src);
  66.     snprintf(fbuf,sizeof(fbuf),"%s/texgif.tex",tmpdir);
  67.     if(*headerfile) {
  68.      if (strstr(headerfile,".latex")!=NULL) {textype=tt_latex; texname="latex";}
  69.          if(strstr(headerfile,".tex")==NULL && strstr(headerfile,".latex")==NULL)
  70.           snprintf(nbuf,sizeof(nbuf),"%s.%s",headerfile,texname);
  71.         else
  72.           snprintf(nbuf,sizeof(nbuf),"%s",headerfile);
  73.         f=fopen(nbuf,"r");
  74.         if(f!=NULL) {
  75.             fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
  76.             if(l>0 && l<FILE_LENGTH_LIMIT) {
  77.                 hd=xmalloc(l+16); (void)fread(hd,1,l,f);
  78.             }
  79.             else {l=0; hd="";}
  80.             fclose(f); f=fopen(fbuf,"w");
  81.             fwrite(hd,1,l,f); free(hd);
  82.         }
  83.         else f=fopen(fbuf,"w");
  84.     }
  85.     else f=fopen(fbuf,"w");
  86.     if(textype==tt_plain) {
  87.         fprintf(f,"%s %s %s\n\\end\n",texstyle, src, texstyle); fclose(f);
  88.     }
  89.     else {
  90.         fprintf(f,"{\\newpage\\clearpage\n\
  91. %s %s %s\n\
  92. \\clearpage}\n\
  93. \\end{document}\n",texstyle, src, texstyle); fclose(f);
  94.     }
  95.     if (chdir(tmpdir)) error("chdir failure");
  96.     parmbuf[0]=texname; parmbuf[1]="texgif"; parmbuf[2]=NULL;
  97.     wrapexec=1;
  98.     execredirected(texname,NULL,NULL,NULL,parmbuf);
  99.     if (cwd[0] && chdir(cwd)) error("chdir failure");
  100. }
  101.  
  102. void parms(void)
  103. {
  104.     char *p;
  105.     int t;
  106.     p=getenv("tmp_dir"); if(p!=NULL && *p!=0) tmpdir=p;
  107.     p=getenv("texgif_tmpdir"); if(p!=NULL && *p!=0) tmpdir=p;
  108.     p=getenv("texgif_fontdir"); if(p!=NULL && *p!=0) fontdir=p;
  109.     p=getenv("texgif_outfile"); if(p!=NULL && *p!=0) outfile=p;
  110.     p=getenv("texgif_texheader"); if(p!=NULL && *p!=0) headerfile=p;
  111.     p=getenv("texgif_style"); if(p!=NULL && *p!=0) texstyle=p;
  112.     p=getenv("texgif_density"); if(p!=NULL && *p!=0) {
  113.         t=atoi(p); if(t>10 && t<1000) basedensity=t;
  114.     }
  115.     p=getenv("texgif_compressratio"); if(p!=NULL && *p!=0) {
  116.         t=atoi(p); if(t>1 && t<=5) compressratio=t;
  117.     }
  118.     if(getcwd(cwd,sizeof(cwd))==NULL) cwd[0]=0;
  119. }
  120.  
  121. int main(int argc, char *argv[])
  122. {
  123.     progname=argv[0];
  124.     parms();
  125.     fontcnt=0;
  126.     tex();
  127.     setreuid(geteuid(),geteuid());setregid(getegid(),getegid());
  128.     dvi();
  129.     return 0;
  130. }
  131.  
  132.