Subversion Repositories wimsdev

Rev

Rev 8499 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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