Subversion Repositories wimsdev

Rev

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