Subversion Repositories wimsdev

Rev

Rev 5465 | Rev 5512 | 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
    /* Use mathml to output TeX formula.
30
     * Returns 1 if OK, 0 if unknown. */
31
    /* It doesn't work yet. */
10 reyssat 32
int mathml(char *p)
33
{
34
/*    char *p1, buf[MAX_LINELEN+1];
35
    if(strlen(p)==1 && isalpha(*p)) {
5504 bpr 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;
10 reyssat 40
    }
41
    output("<pre>%s</pre>\n",p); return 1;
42
*/    
43
    return 0;
44
}
45
 
5504 bpr 46
    /* check whether the same tex source has already been produced */
10 reyssat 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++) {
5504 bpr 56
      if(oldinstex[i].size==current_tex_size &&
57
       strcmp(oldinstex[i].src,buf)==0) {
58
        ovlstrcpy(n,oldinstex[i].name); return 1;
59
      }
10 reyssat 60
    }
61
    if(strlen(n)>=128 || oldtexcnt>=100) return 0;
3718 reyssat 62
    ovlstrcpy(oldinstex[oldtexcnt].src,buf);
63
    ovlstrcpy(oldinstex[oldtexcnt].name,n);
10 reyssat 64
    oldinstex[oldtexcnt].size=current_tex_size;
65
    oldtexcnt++; return 0;
66
}
67
 
5504 bpr 68
    /* returns NULL if instex can use static */
10 reyssat 69
char *instex_check_static(char *p)
70
{
71
    char *f;
72
    if(instex_usedynamic) return p;
73
    for(f=strchr(p,'$');
5504 bpr 74
    f!=NULL && *(f+1)!='(' && *(f+1)!='[' && *(f+1)!='_' && !isalnum(*(f+1));
75
    f=strchr(f+2,'$'));
10 reyssat 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
 
5504 bpr 83
    /* Intelligent insertion of math formulas, kernel */
10 reyssat 84
void __insmath(char *p)
85
{
86
    char *f, *pp, *pe, *p1, buf[MAX_LINELEN+1], nbuf[256];
87
    int ts, n, rawmathready;
88
 
5465 bpr 89
    ovlstrcpy(buf,p); strip_trailing_spaces(buf); singlespace(buf);
10 reyssat 90
    p1=getvar("insmath_slashsubst");
5465 bpr 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);
94
    /* here replace .. by , : i=1 .. 5 -> i=1, 5 !*/
10 reyssat 95
    for(pp=strstr(buf,".."); pp!=NULL; pp=strstr(pp,"..")) {
5504 bpr 96
    if(*(pp+2)=='.' || *(pp+2)==',') {
97
        do pp++; while(*pp=='.'); continue;
10 reyssat 98
    }
5504 bpr 99
    *pp=','; *(pp+1)=' ';
100
    }
10 reyssat 101
    ts=0; if(strchr(buf,'\\') || strchr(buf,'}')) ts=1;
102
    rawmathready=0; if(!ts) {
5504 bpr 103
      pp=getvar("insmath_rawmath");
104
      if(pp!=NULL && strstr(pp,"yes")!=NULL) {
105
        rawmath(buf); rawmathready=1;
106
      }
10 reyssat 107
    }
108
    if(ts || mathalign_base==2 ||
109
       (strchr(buf,'[')!=NULL &&
5504 bpr 110
    (strchr(buf,',')!=NULL || strchr(buf,';')!=NULL))) {
111
      char alignbak[2048];
112
      tex: instex_style="$$";
113
      if(!ts) texmath(buf); // in particular, reinterpret variables and some fonts or functions as alpha pi cos 
114
    // see list in texmath.c : tmathfn 
115
      else {// need to interpret x y z 
116
        char *p1;
117
        p1=find_word_start(buf);
118
        if(*p1=='\\') {
119
          int i;
120
          char *pt;
121
          for(i=1;isalnum(p1[i]);i++);
122
          if(p1[i]==0 && (pt=mathfont(p1))!=NULL) {
123
            _output_(pt); *p=0; return;
124
          }
125
        }
126
      }
127
      if(mathalign_base==2 && mathml(buf)) {*p=0; return;}
128
      pp=getvar("ins_align");
129
      if(pp!=NULL) mystrncpy(alignbak,pp,sizeof(alignbak));
130
      setvar("ins_align","middle");
131
      mystrncpy(ins_alt,buf,sizeof(ins_alt));
132
      if(f==NULL) {
133
        calc_instexst(buf); output("%s",buf);
134
      }
135
      else {
136
        instex_usedynamic=1; exec_instex(buf); instex_usedynamic=0;
137
      }
138
      instex_style="";
139
      if(alignbak[0]) setvar("ins_align",alignbak);
140
      return;
10 reyssat 141
    }
5465 bpr 142
    /* end of the only for images*/
10 reyssat 143
    for(pp=find_mathvar_start(buf); *pp; pp=find_mathvar_start(pe)) {
5504 bpr 144
      pe=find_mathvar_end(pp); n=pe-pp;
145
      if(!isalpha(*pp) || n<3 || n>16) continue;
146
      memmove(nbuf,pp,n); nbuf[n]=0;
147
      if(wordchr(tnames,nbuf)!=NULL) goto tex;
10 reyssat 148
    }
5465 bpr 149
 
150
    for(pp=strchr(buf,'/'); pp!=NULL && *find_word_start(pp+1)!='(';
151
    pp=strchr(pp+1,'/'));
10 reyssat 152
    if(pp!=NULL) goto tex;
153
    if(rawmathready) rawmath_easy=1;
154
    for(pp=strchr(buf,'<'); pp!=NULL; pp=strchr(pp+1,'<'))
155
      string_modify(buf,pp,pp+1,"&lt;");
156
    for(pp=strchr(buf,'>'); pp!=NULL; pp=strchr(pp+1,'>'))
157
      string_modify(buf,pp,pp+1,"&gt;");
5465 bpr 158
    htmlmath(buf); output("%s",buf);
159
    rawmath_easy=0;
10 reyssat 160
}
161
 
162
char *andor[]={"and","or","not","is","isnot"};
163
#define andorcnt (sizeof(andor)/sizeof(andor[0]))
164
char *andorlang[andorcnt], andorlangbuf[1024];
165
int andorlangcnt=-1;
166
 
5504 bpr 167
    /* Processing logic statements in math formulas */
10 reyssat 168
void _mathlogic(char *p, void _put(char *pp))
169
{
170
    char *p1, *p2, *ps;
171
    int i;
172
    if(strstr(p,"qzis")==NULL) {
5504 bpr 173
      for(i=0;i<andorcnt && varchr(p,andor[i])==NULL; i++);
174
      if(i>=andorcnt) {
175
        _put(p); return;
176
      }
10 reyssat 177
    }
178
    if(andorlangcnt<0) {
5504 bpr 179
      char buf[MAX_LINELEN+1];
180
      accessfile(buf,"r","bases/sys/andor.%s",lang);
181
      mystrncpy(andorlangbuf,find_word_start(buf),sizeof(andorlangbuf));
182
      for(i=0,p1=andorlangbuf;i<andorcnt;i++,p1=find_word_start(p2)) {
183
        p2=strchr(p1,',');
184
        if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
185
        strip_trailing_spaces(p1);
186
        if(*p1) andorlang[i]=p1; else break;
10 reyssat 187
    }
5504 bpr 188
    andorlangcnt=i;
189
    }
10 reyssat 190
    for(ps=p, p1=find_mathvar_start(p); *p1; p1=find_mathvar_start(p2)) {
5504 bpr 191
      p2=find_mathvar_end(p1);
192
      if(!isalpha(*p1)) continue;
193
      if(strncmp(p1,"qzis",4)==0) {
194
        char *p3, *p4, *p5;
195
        int tt;
196
        p4=find_word_start(p2);
197
        if(*p4!='(') continue;
198
        if(strncmp(p1+4,"not",3)==0) {tt=4; p3=p1+7;}
199
        else {tt=3; p3=p1+4;}
200
        if(!isalpha(*p3)) continue;
201
        p4++; p5=find_matching(p4,')');
202
        if(*p5!=')') continue;
203
        *p5=0; *p2=0; p2=p5+1;
204
 
205
 
206
        continue;
207
      }
208
      for(i=0;i<andorlangcnt;i++) if(strncmp(p1,andor[i],p2-p1)==0) break;
209
      if(i<andorlangcnt) {
210
        *p1=0; ps=find_word_start(ps); if(*ps) _put(ps);
211
        output(" %s ",andorlang[i]); ps=p2;
212
      }
10 reyssat 213
    }
214
    ps=find_word_start(ps); if(*ps) _put(ps);
215
}
216
 
5504 bpr 217
    /* Intelligent insertion of math formulas */
10 reyssat 218
void insmath(char *p)
219
{
220
    char *pt;
221
    if(!outputing) goto end;
222
    pt=getvar("insmath_logic");
223
    if(pt==NULL || strstr(pt,"yes")==NULL) {
5504 bpr 224
      __insmath(p);
225
      end: *p=0; return;
10 reyssat 226
    }
227
    _mathlogic(p,__insmath);
228
}