Subversion Repositories wimsdev

Rev

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

  1. /*    Copyright (C) 1998-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.     /* automatic selection of math rendering method */
  19.  
  20. void exec_instex(char *p);
  21. void calc_instexst(char *p);
  22.  
  23. struct {
  24.     char src[124], name[128];
  25.     int size;
  26. } oldinstex[100];
  27. int oldtexcnt=0;
  28.  
  29.     /* Use mathml to output TeX formula.
  30.      * Returns 1 if OK, 0 if unknown. */
  31.     /* It doesn't work yet. */
  32. int mathml(char *p)
  33. {
  34. /*    char *p1, buf[MAX_LINELEN+1];
  35.     if(strlen(p)==1 && isalpha(*p)) {
  36.     output("<math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n");
  37.     snprintf(buf,sizeof(buf),
  38.          "<mrow><mi>%c</mi></mrow></math>\n",*p);
  39.     output("%s",buf); return 1;
  40.     }
  41.     output("<pre>%s</pre>\n",p); return 1;
  42. */    
  43.     return 0;
  44. }
  45.  
  46.     /* check whether the same tex source has already been produced */
  47. int instex_ready(char *p, char *n)
  48. {
  49.     int i;
  50.     char *cl, buf[MAX_LINELEN+1];
  51.    
  52.     if(strlen(p)>=124) return 0;
  53.     cl=getvar("instex_color"); if(cl!=NULL && *cl!=0) return 0;
  54.     mystrncpy(buf,p,sizeof(buf)); tex_nospace(buf);
  55.     for(i=0;i<oldtexcnt;i++) {
  56.       if(oldinstex[i].size==current_tex_size &&
  57.        strcmp(oldinstex[i].src,buf)==0) {
  58.         ovlstrcpy(n,oldinstex[i].name); return 1;
  59.       }
  60.     }
  61.     if(strlen(n)>=128 || oldtexcnt>=100) return 0;
  62.     ovlstrcpy(oldinstex[oldtexcnt].src,buf);
  63.     ovlstrcpy(oldinstex[oldtexcnt].name,n);
  64.     oldinstex[oldtexcnt].size=current_tex_size;
  65.     oldtexcnt++; return 0;
  66. }
  67.  
  68.     /* returns NULL if instex can use static */
  69. char *instex_check_static(char *p)
  70. {
  71.     char *f;
  72.     if(instex_usedynamic) return p;
  73.     for(f=strchr(p,'$');
  74.     f!=NULL && *(f+1)!='(' && *(f+1)!='[' && *(f+1)!='_' && !isalnum(*(f+1));
  75.     f=strchr(f+2,'$'));
  76.     if(f==NULL) f=strstr(m_file.name,"sessions/");
  77.     return f;
  78. }
  79.  
  80. char tnames[]="sqrt int integrate sum prod product \
  81. Int Sum Prod conj abs";
  82.  
  83.     /* Intelligent insertion of math formulas, kernel */
  84. void __insmath(char *p)
  85. {
  86.     char *f, *pp, *pe, *p1, buf[MAX_LINELEN+1], nbuf[256];
  87.     int ts, n, rawmathready;
  88.  
  89.     ovlstrcpy(buf,p); strip_trailing_spaces(buf); singlespace(buf);
  90.     p1=getvar("insmath_slashsubst");
  91.     if(p1!=NULL && strstr(p1,"yes")!=NULL) slashsubst(buf); // substitute backslash parameters
  92.     f=instex_check_static(buf); //decide if image already exists
  93.     substit(buf);//substitute the variables
  94.     /* here replace .. by , : i=1 .. 5 -> i=1, 5 !*/
  95.     for(pp=strstr(buf,".."); pp!=NULL; pp=strstr(pp,"..")) {
  96.       if(*(pp+2)=='.' || *(pp+2)==',') {
  97.         do pp++; while(*pp=='.'); continue;
  98.       }
  99.       *pp=','; *(pp+1)=' ';
  100.     }
  101.     /* decide if it should be tex */
  102.     ts=0; if(strchr(buf,'\\') || strchr(buf,'}')) ts=1;
  103.     /* if not and if variable insmath_rawmath is there, do rawmath */
  104.     rawmathready=0;
  105.     if(!ts) { /* should not be tex, looking if rawmath is asked */
  106.       pp=getvar("insmath_rawmath");
  107.       if(pp!=NULL && strstr(pp,"yes")!=NULL) {
  108.         rawmath(buf); rawmathready=1;
  109.       }
  110.     }
  111.     if(ts || mathalign_base==2 ||
  112.        (strchr(buf,'[')!=NULL &&
  113.     (strchr(buf,',')!=NULL || strchr(buf,';')!=NULL))) {
  114.       char alignbak[2048];
  115.       tex: instex_style="$$";
  116.       if(!ts) texmath(buf); // possibly tex - in particular, reinterpret variables and some fonts or functions as alpha pi cos
  117.     // see list in texmath.c : tmathfn
  118.       else {// need to interpret x y z
  119.         char *p1;
  120.         p1=find_word_start(buf);
  121.         if(*p1=='\\') {
  122.           int i;
  123.           char *pt;
  124.           for(i=1;isalnum(p1[i]);i++); /* find an alphanumeric string beginning by \\ */
  125.           if(p1[i]==0 && (pt=mathfont(p1))!=NULL) { /* find some mathfont as \calB */
  126.             _output_(pt); *p=0; return;
  127.           }
  128.         }
  129.       }
  130.       if(mathalign_base==2 && mathml(buf)) {*p=0; return;}
  131. /* only for images*/
  132.       pp=getvar("ins_align");
  133.       if(pp!=NULL) mystrncpy(alignbak,pp,sizeof(alignbak));
  134.       setvar("ins_align","middle");
  135.       mystrncpy(ins_alt,buf,sizeof(ins_alt));
  136.       if(f==NULL) {
  137.         calc_instexst(buf); output("%s",buf);
  138.       }
  139.       else {
  140.         instex_usedynamic=1; exec_instex(buf); instex_usedynamic=0;
  141.       }
  142.       instex_style="";
  143.       if(alignbak[0]) setvar("ins_align",alignbak);
  144.       return;
  145.     }
  146. /* end of the only for images*/
  147.     for(pp=find_mathvar_start(buf); *pp; pp=find_mathvar_start(pe)) {
  148.       pe=find_mathvar_end(pp); n=pe-pp;
  149.       if(!isalpha(*pp) || n<3 || n>16) continue;
  150.       memmove(nbuf,pp,n); nbuf[n]=0;
  151.       if(wordchr(tnames,nbuf)!=NULL) goto tex;
  152.     }
  153. /* only for html in case where gifs is activated ?? look for  / ?? */  
  154.     for(pp=strchr(buf,'/'); pp!=NULL && *find_word_start(pp+1)!='(';
  155.     pp=strchr(pp+1,'/'));
  156.     if(pp!=NULL) goto tex;  /* so a/4 can be reinterpreted as {1 over 4 } a */
  157.     if(rawmathready) rawmath_easy=1;
  158.     for(pp=strchr(buf,'<'); pp!=NULL; pp=strchr(pp+1,'<'))
  159.       string_modify(buf,pp,pp+1,"&lt;");
  160.     for(pp=strchr(buf,'>'); pp!=NULL; pp=strchr(pp+1,'>'))
  161.       string_modify(buf,pp,pp+1,"&gt;");
  162.     htmlmath(buf); output("%s",buf);
  163.     rawmath_easy=0;
  164. }
  165.  
  166. char *andor[]={"and","or","not","is","isnot"};
  167. #define andorcnt (sizeof(andor)/sizeof(andor[0]))
  168. char *andorlang[andorcnt], andorlangbuf[1024];
  169. int andorlangcnt=-1;
  170.  
  171.     /* Processing logic statements in math formulas */
  172. void _mathlogic(char *p, void _put(char *pp))
  173. {
  174.     char *p1, *p2, *ps;
  175.     int i;
  176.     if(strstr(p,"qzis")==NULL) {
  177.       for(i=0;i<andorcnt && varchr(p,andor[i])==NULL; i++);
  178.       if(i>=andorcnt) {
  179.         _put(p); return;
  180.       }
  181.     }
  182.     if(andorlangcnt<0) {
  183.       char buf[MAX_LINELEN+1];
  184.       accessfile(buf,"r","bases/sys/andor.%s",lang);
  185.       mystrncpy(andorlangbuf,find_word_start(buf),sizeof(andorlangbuf));
  186.       for(i=0,p1=andorlangbuf;i<andorcnt;i++,p1=find_word_start(p2)) {
  187.         p2=strchr(p1,',');
  188.         if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
  189.         strip_trailing_spaces(p1);
  190.         if(*p1) andorlang[i]=p1; else break;
  191.     }
  192.     andorlangcnt=i;
  193.     }
  194.     for(ps=p, p1=find_mathvar_start(p); *p1; p1=find_mathvar_start(p2)) {
  195.       p2=find_mathvar_end(p1);
  196.       if(!isalpha(*p1)) continue;
  197.       if(strncmp(p1,"qzis",4)==0) {
  198.         char *p3, *p4, *p5;
  199.         int tt;
  200.         p4=find_word_start(p2);
  201.         if(*p4!='(') continue;
  202.         if(strncmp(p1+4,"not",3)==0) {tt=4; p3=p1+7;}
  203.         else {tt=3; p3=p1+4;}
  204.         if(!isalpha(*p3)) continue;
  205.         p4++; p5=find_matching(p4,')');
  206.         if(*p5!=')') continue;
  207.         *p5=0; *p2=0; p2=p5+1;
  208.        
  209.        
  210.         continue;
  211.       }
  212.       for(i=0;i<andorlangcnt;i++) if(strncmp(p1,andor[i],p2-p1)==0) break;
  213.       if(i<andorlangcnt) {
  214.         *p1=0; ps=find_word_start(ps); if(*ps) _put(ps);
  215.         output(" %s ",andorlang[i]); ps=p2;
  216.       }
  217.     }
  218.     ps=find_word_start(ps); if(*ps) _put(ps);
  219. }
  220.  
  221.     /* Intelligent insertion of math formulas */
  222. void insmath(char *p)
  223. {
  224.     char *pt;
  225.     if(!outputing) goto end;
  226.     pt=getvar("insmath_logic");
  227.     if(pt==NULL || strstr(pt,"yes")==NULL) {
  228.       __insmath(p);
  229.       end: *p=0; return;
  230.     }
  231.     _mathlogic(p,__insmath);
  232. }
  233.