Subversion Repositories wimsdev

Rev

Rev 3835 | 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. /*#include "basic.c"*/
  43. /*#include "colors.c"*/
  44. /*#include "tfm.c"*/
  45. /*#include "gf.c"*/
  46. /*#include "font.c"*/
  47. /*#include "image.c"*/
  48. /*#include "dvi.c"*/
  49.  
  50. void checktextype(char *p)
  51. {
  52.     if(strstr(p,"\\begin{")!=NULL ||
  53.        strstr(p,"\\end{")!=NULL ||
  54.        strstr(p,"\\underset")!=NULL ||
  55.        strstr(p,"\\overset")!=NULL ||
  56.        strstr(p,"\\displaystyle")!=NULL ||
  57.        strstr(p,"\\mathcal")!=NULL) {
  58.         textype=tt_latex; texname="latex";
  59.     }
  60. }
  61.  
  62.         /* pass thru tex */
  63. void tex(void)
  64. {
  65.     char *src;
  66.     char *hd;
  67.     char fbuf[1024], nbuf[2048];
  68.     char *parmbuf[8];
  69.     FILE *f;
  70.     long int l;
  71.     src=getenv("texgif_src");
  72.     if(src==NULL || *src==0) error("No source.");
  73.     checktextype(src);
  74.     snprintf(fbuf,sizeof(fbuf),"%s/texgif.tex",tmpdir);
  75.     if(*headerfile) {
  76.      if (strstr(headerfile,".latex")!=NULL) {textype=tt_latex; texname="latex";}
  77.          if(strstr(headerfile,".tex")==NULL && strstr(headerfile,".latex")==NULL)
  78.           snprintf(nbuf,sizeof(nbuf),"%s.%s",headerfile,texname);
  79.         else
  80.           snprintf(nbuf,sizeof(nbuf),"%s",headerfile);
  81.         f=fopen(nbuf,"r");
  82.         if(f!=NULL) {
  83.             fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
  84.             if(l>0 && l<FILE_LENGTH_LIMIT) {
  85.                 hd=xmalloc(l+16); (void)fread(hd,1,l,f);
  86.             }
  87.             else {l=0; hd="";}
  88.             fclose(f); f=fopen(fbuf,"w");
  89.             fwrite(hd,1,l,f); free(hd);
  90.         }
  91.         else f=fopen(fbuf,"w");
  92.     }
  93.     else f=fopen(fbuf,"w");
  94.     if(textype==tt_plain) {
  95.         fprintf(f,"%s %s %s\n\\end\n",texstyle, src, texstyle); fclose(f);
  96.     }
  97.     else {
  98.         fprintf(f,"{\\newpage\\clearpage\n\
  99. %s %s %s\n\
  100. \\clearpage}\n\
  101. \\end{document}\n",texstyle, src, texstyle); fclose(f);
  102.     }
  103.     if (chdir(tmpdir)) error("chdir failure");
  104.     parmbuf[0]=texname; parmbuf[1]="texgif"; parmbuf[2]=NULL;
  105.     wrapexec=1;
  106.     execredirected(texname,NULL,NULL,NULL,parmbuf);
  107.     if (cwd[0] && chdir(cwd)) error("chdir failure");
  108. }
  109.  
  110. void parms(void)
  111. {
  112.     char *p;
  113.     int t;
  114.     p=getenv("tmp_dir"); if(p!=NULL && *p!=0) tmpdir=p;
  115.     p=getenv("texgif_tmpdir"); if(p!=NULL && *p!=0) tmpdir=p;
  116.     p=getenv("texgif_fontdir"); if(p!=NULL && *p!=0) fontdir=p;
  117.     p=getenv("texgif_outfile"); if(p!=NULL && *p!=0) outfile=p;
  118.     p=getenv("texgif_texheader"); if(p!=NULL && *p!=0) headerfile=p;
  119.     p=getenv("texgif_style"); if(p!=NULL && *p!=0) texstyle=p;
  120.     p=getenv("texgif_density"); if(p!=NULL && *p!=0) {
  121.         t=atoi(p); if(t>10 && t<1000) basedensity=t;
  122.     }
  123.     p=getenv("texgif_compressratio"); if(p!=NULL && *p!=0) {
  124.         t=atoi(p); if(t>1 && t<=5) compressratio=t;
  125.     }
  126.     if(getcwd(cwd,sizeof(cwd))==NULL) cwd[0]=0;
  127. }
  128.  
  129. int main(int argc, char *argv[])
  130. {
  131.     progname=argv[0];
  132.     parms();
  133.     fontcnt=0;
  134.     tex();
  135.     setreuid(geteuid(),geteuid());setregid(getegid(),getegid());
  136.     dvi();
  137.     return 0;
  138. }
  139.  
  140.