Subversion Repositories wimsdev

Rev

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