Subversion Repositories wimsdev

Rev

Rev 8155 | Rev 8499 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 */
8185 bpr 17
#include "wims.h"
10 reyssat 18
 
8185 bpr 19
/* automatic selection of math rendering method */
10 reyssat 20
 
21
void exec_instex(char *p);
22
void calc_instexst(char *p);
23
 
24
struct {
25
    char src[124], name[128];
26
    int size;
27
} oldinstex[100];
28
int oldtexcnt=0;
29
 
5504 bpr 30
    /* check whether the same tex source has already been produced */
10 reyssat 31
int instex_ready(char *p, char *n)
32
{
33
    int i;
34
    char *cl, buf[MAX_LINELEN+1];
7673 bpr 35
 
10 reyssat 36
    if(strlen(p)>=124) return 0;
37
    cl=getvar("instex_color"); if(cl!=NULL && *cl!=0) return 0;
38
    mystrncpy(buf,p,sizeof(buf)); tex_nospace(buf);
39
    for(i=0;i<oldtexcnt;i++) {
5504 bpr 40
      if(oldinstex[i].size==current_tex_size &&
41
       strcmp(oldinstex[i].src,buf)==0) {
42
        ovlstrcpy(n,oldinstex[i].name); return 1;
43
      }
10 reyssat 44
    }
45
    if(strlen(n)>=128 || oldtexcnt>=100) return 0;
3718 reyssat 46
    ovlstrcpy(oldinstex[oldtexcnt].src,buf);
47
    ovlstrcpy(oldinstex[oldtexcnt].name,n);
10 reyssat 48
    oldinstex[oldtexcnt].size=current_tex_size;
49
    oldtexcnt++; return 0;
50
}
51
 
5504 bpr 52
    /* returns NULL if instex can use static */
10 reyssat 53
char *instex_check_static(char *p)
54
{
55
    char *f;
56
    if(instex_usedynamic) return p;
57
    for(f=strchr(p,'$');
5504 bpr 58
    f!=NULL && *(f+1)!='(' && *(f+1)!='[' && *(f+1)!='_' && !isalnum(*(f+1));
59
    f=strchr(f+2,'$'));
10 reyssat 60
    if(f==NULL) f=strstr(m_file.name,"sessions/");
61
    return f;
62
}
63
 
64
char tnames[]="sqrt int integrate sum prod product \
65
Int Sum Prod conj abs";
66
 
5617 bpr 67
int __gototex (char *p, char *f, int ts)
5611 bpr 68
{
69
      char alignbak[2048];
5617 bpr 70
      char *pp, buf[MAX_LINELEN+1];
71
      ovlstrcpy(buf,p);
5611 bpr 72
      instex_style="$$";
73
      if(!ts) texmath(buf);
7673 bpr 74
         /* ts=0 but there is some computer matrix to transform
5611 bpr 75
          * done by texmath, but it does much more as replacing strings in tmathfn
76
          * OK if buf contains " math computer-syntax" ; if not, the result may be bad
77
        */
78
      else {// seems tex : need to interpret names of variables as \x or \calB
79
       //if (mathalign_base < 2) { //to check
80
        char *p1;
81
        p1=find_word_start(buf);
82
        if(*p1=='\\') {
83
          int i;
84
          char *pt;
85
          for(i=1;isalnum(p1[i]);i++); /* find an alphanumeric string beginning by \\ */
86
          if(p1[i]==0 && (pt=mathfont(p1))!=NULL) {
5617 bpr 87
            _output_(pt); *p=0; return 1;
5611 bpr 88
          }
89
        }
90
      // }
91
      }
92
      /* send to mathml */
7673 bpr 93
      if (mathalign_base == 2 && mathml(buf,0)) { *p=0 ;return 1; }
5611 bpr 94
/* end if mathml option in case ts=1 or "computer matrix" */
95
 
96
/* creating images*/
97
      pp=getvar("ins_align");
98
      if(pp!=NULL) mystrncpy(alignbak,pp,sizeof(alignbak));
99
      setvar("ins_align","middle");
100
      mystrncpy(ins_alt,buf,sizeof(ins_alt));
101
      if(f==NULL) {
102
        calc_instexst(buf); output("%s",buf);
103
      }
104
      else {
105
        instex_usedynamic=1; exec_instex(buf); instex_usedynamic=0;
106
      }
7673 bpr 107
      instex_style="";
5611 bpr 108
      if(alignbak[0]) setvar("ins_align",alignbak);
5617 bpr 109
      *p=0; return 0;
7673 bpr 110
}
5611 bpr 111
 
5504 bpr 112
    /* Intelligent insertion of math formulas, kernel */
10 reyssat 113
void __insmath(char *p)
114
{
115
    char *f, *pp, *pe, *p1, buf[MAX_LINELEN+1], nbuf[256];
116
    int ts, n, rawmathready;
117
 
5465 bpr 118
    ovlstrcpy(buf,p); strip_trailing_spaces(buf); singlespace(buf);
10 reyssat 119
    p1=getvar("insmath_slashsubst");
7673 bpr 120
    if(p1!=NULL && strstr(p1,"yes")!=NULL) slashsubst(buf); // substitute backslash parameters
5465 bpr 121
    f=instex_check_static(buf); //decide if image already exists
5512 bpr 122
    substit(buf);//substitute the variables
5465 bpr 123
    /* here replace .. by , : i=1 .. 5 -> i=1, 5 !*/
10 reyssat 124
    for(pp=strstr(buf,".."); pp!=NULL; pp=strstr(pp,"..")) {
5512 bpr 125
      if(*(pp+2)=='.' || *(pp+2)==',') {
5504 bpr 126
        do pp++; while(*pp=='.'); continue;
5512 bpr 127
      }
128
      *pp=','; *(pp+1)=' ';
10 reyssat 129
    }
5512 bpr 130
    /* decide if it should be tex */
10 reyssat 131
    ts=0; if(strchr(buf,'\\') || strchr(buf,'}')) ts=1;
5512 bpr 132
    /* if not and if variable insmath_rawmath is there, do rawmath */
7673 bpr 133
    rawmathready=0;
5523 bpr 134
    if(!ts) { /* not tex, looking if rawmath is asked */
5504 bpr 135
      pp=getvar("insmath_rawmath");
136
      if(pp!=NULL && strstr(pp,"yes")!=NULL) {
137
        rawmath(buf); rawmathready=1;
138
      }
10 reyssat 139
    }
5523 bpr 140
    if(ts) {
141
         _replace_matrix (buf,"\\matrix{","matrix"); //could be done in any case if ts=1
142
         _replace_matrix (buf,"\\pmatrix{","pmatrix");
143
    }
144
/* if ts=1 (it should be a tex formula)  or if there is a [ ,  ; ] matrix */
7673 bpr 145
    if(ts ||
5523 bpr 146
      (strchr(buf,'[')!=NULL && (strchr(buf,',')!=NULL || strchr(buf,';')!=NULL))) {
5611 bpr 147
       if(__gototex(buf, f, ts)) return;
10 reyssat 148
    }
5523 bpr 149
 
150
/* end creating images
151
 * we are now in the case where ts=0 and no need of tex for matrix */
152
 
153
/* find math variables */
10 reyssat 154
    for(pp=find_mathvar_start(buf); *pp; pp=find_mathvar_start(pe)) {
5504 bpr 155
      pe=find_mathvar_end(pp); n=pe-pp;
5523 bpr 156
      /* non alpha variable or too short or too long to be interpreted as tnames */
7673 bpr 157
      if(!isalpha(*pp) || n<3 || n>16) continue;
5504 bpr 158
      memmove(nbuf,pp,n); nbuf[n]=0;
5611 bpr 159
      if(wordchr(tnames,nbuf)!=NULL) { if(__gototex(buf, f, 0)) return;}
7673 bpr 160
      /* find sqrt int integrate sum prod product Int Sum Prod conj abs,
5523 bpr 161
       * so must be texmath interpretated ; after going to tex, return in any case
162
       */
10 reyssat 163
    }
7673 bpr 164
/* look for  /  to interpretate as quotients -
165
 * extend the version by accepting something else than (
166
*/
5523 bpr 167
    //for(pp=strchr(buf,'/'); pp!=NULL && *find_word_start(pp+1)!='('; pp=strchr(pp+1,'/'));
168
    pp=strchr(buf,'/');
5611 bpr 169
    if(pp!=NULL){ if( __gototex(buf,f,0)) return;} /* so a/4 can be reinterpreted as {a over 4 } ; transform also 5/(x+1) */
10 reyssat 170
    if(rawmathready) rawmath_easy=1;
171
    for(pp=strchr(buf,'<'); pp!=NULL; pp=strchr(pp+1,'<'))
172
      string_modify(buf,pp,pp+1,"&lt;");
173
    for(pp=strchr(buf,'>'); pp!=NULL; pp=strchr(pp+1,'>'))
174
      string_modify(buf,pp,pp+1,"&gt;");
5544 bpr 175
/* no tex has been introduced - so go to mathmlmath */
5611 bpr 176
    mathmlmath(buf); output("%s",buf);
5465 bpr 177
    rawmath_easy=0;
10 reyssat 178
}
179
 
5523 bpr 180
/* the following is not used in modules : no insmath_logic=yes somewhere */
181
 
10 reyssat 182
char *andor[]={"and","or","not","is","isnot"};
183
#define andorcnt (sizeof(andor)/sizeof(andor[0]))
184
char *andorlang[andorcnt], andorlangbuf[1024];
185
int andorlangcnt=-1;
186
 
8155 bpr 187
/* Processing logic statements in math formulas */
10 reyssat 188
void _mathlogic(char *p, void _put(char *pp))
189
{
190
    char *p1, *p2, *ps;
191
    int i;
192
    if(strstr(p,"qzis")==NULL) {
5504 bpr 193
      for(i=0;i<andorcnt && varchr(p,andor[i])==NULL; i++);
194
      if(i>=andorcnt) {
195
        _put(p); return;
196
      }
10 reyssat 197
    }
198
    if(andorlangcnt<0) {
5504 bpr 199
      char buf[MAX_LINELEN+1];
200
      accessfile(buf,"r","bases/sys/andor.%s",lang);
201
      mystrncpy(andorlangbuf,find_word_start(buf),sizeof(andorlangbuf));
202
      for(i=0,p1=andorlangbuf;i<andorcnt;i++,p1=find_word_start(p2)) {
203
        p2=strchr(p1,',');
204
        if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
205
        strip_trailing_spaces(p1);
206
        if(*p1) andorlang[i]=p1; else break;
10 reyssat 207
    }
5504 bpr 208
    andorlangcnt=i;
209
    }
10 reyssat 210
    for(ps=p, p1=find_mathvar_start(p); *p1; p1=find_mathvar_start(p2)) {
5504 bpr 211
      p2=find_mathvar_end(p1);
212
      if(!isalpha(*p1)) continue;
213
      if(strncmp(p1,"qzis",4)==0) {
214
        char *p3, *p4, *p5;
6525 bpr 215
        /*int tt;*/
5504 bpr 216
        p4=find_word_start(p2);
217
        if(*p4!='(') continue;
6525 bpr 218
        if(strncmp(p1+4,"not",3)==0) {/*tt=4;*/ p3=p1+7;}
219
        else {/*tt=3; */p3=p1+4;}
5504 bpr 220
        if(!isalpha(*p3)) continue;
221
        p4++; p5=find_matching(p4,')');
222
        if(*p5!=')') continue;
223
        *p5=0; *p2=0; p2=p5+1;
7673 bpr 224
 
225
 
5504 bpr 226
        continue;
227
      }
228
      for(i=0;i<andorlangcnt;i++) if(strncmp(p1,andor[i],p2-p1)==0) break;
229
      if(i<andorlangcnt) {
230
        *p1=0; ps=find_word_start(ps); if(*ps) _put(ps);
231
        output(" %s ",andorlang[i]); ps=p2;
232
      }
10 reyssat 233
    }
234
    ps=find_word_start(ps); if(*ps) _put(ps);
235
}
236
 
8155 bpr 237
/* Intelligent insertion of math formulas */
10 reyssat 238
void insmath(char *p)
239
{
240
    char *pt;
241
    if(!outputing) goto end;
242
    pt=getvar("insmath_logic");
243
    if(pt==NULL || strstr(pt,"yes")==NULL) {
7673 bpr 244
      __insmath(p);
5504 bpr 245
      end: *p=0; return;
10 reyssat 246
    }
247
    _mathlogic(p,__insmath);
248
}