Subversion Repositories wimsdev

Rev

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

Rev Author Line No. Line
5522 bpr 1
/*    Copyright (C) 2012 WIMSDEV
2
 *    This file is part of the WIMS package.
3
 *
4
 *  This program is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program; if not, write to the Free Software
16
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 */
18
 
19
int mathml(char *p, int option ){
20
          if (strlen(p)==0) return 1 ;
21
    if( mathalign_base <= 1){
22
      internal_error(" why is wims trying mathml()?\n");
23
      return 0; // got to insmath with gifs
24
    }
25
    if(strlen(p) > MAX_LINELEN ){ // too big ? probably too big for gifs as well ; but error signalling is better in gif-methods
26
      mathalign_base = 1;// 0 or 1 position of tex_gifs
27
      return 0; // got to insmath with gifs
28
    }
29
    //singlespace(p); // needed for check unbalanced \left\right in wims_mathml.cc --> insmath.c
30
    singlespace(p);
31
    int my_pipe[2];
32
    pid_t pid;
33
    if(pipe(my_pipe)){// pipe could not be opened...
34
      internal_error("mathml(): pipe() failure.\n");
35
      mathalign_base = 1;
36
      return 0; // got to insmath with gifs
37
    }
38
    else
39
    {
40
      pid = fork();
41
      if (pid == (pid_t) 0){
42
          // this buffer should probably be set to 0.5 * MAX_LINELEN : mathml is very big !
43
          // if output mathml string larger ;ERROR will be signaled by wims_matml.
44
          char mml_buffer[MAX_LINELEN+1];
45
          sprintf(mml_buffer, "%d", MAX_LINELEN);// int --> char
46
          // setting --tex-size argument to wims_mathml.cc
47
          char mathml_tex_size[64];//this should be a string like "250%" 
5556 schaersvoo 48
          int texsize_list[]={20,30,50,70,90,100,120,130,150,180,220,300};
5522 bpr 49
          // analogue to  mathfonts.c but now we talk %  and we could make any amount of changes
50
          // not limited by font-size !! 
5556 schaersvoo 51
          // when math_with_gifs is removed from wims, we can rethink / improve this 
5522 bpr 52
          int idx = 6;
53
          int use_js_zoom = 0; // default js-zoom in mathml if disabled
54
          // enable via adm/light
55
          // next code is stolen from wims.c 
56
          if(getvar("useropts") != NULL || getvar("wims_useropts") != NULL){
57
            char *u;
58
            u = getvar("useropts"); // set via adm/light or via cookie?
59
            if(u == NULL || *u == 0){ u=getvar("wims_useropts");} // wims_user? look into def file
60
            if(u != NULL && *u != 0){
61
                if(myisdigit(u[0])){ //2 digit code : 12,22,32,...,92  [tex_size mathalignbase] 
62
                  idx = u[0] - '0';
63
                  if(idx < 0 || idx > 11){ idx = 6; } // the default value 120%
64
                }
65
                if(myisdigit(u[2]) && u[2]!=0){ // suppose we add 0 or 1 to useropts for using js-zoom on mathml?
66
                // 3 digitcode:  621 == fontsize idx=6 & use mathml & use zoom
67
                  use_js_zoom = u[2] - '0';
68
                  if(use_js_zoom > 1 || use_js_zoom < 0){
69
                      use_js_zoom = 0; //disable js-zooming of mathml
70
                  }
71
                }
72
            }
73
          }
74
          else
75
          { // we take the default setting from adm/management config
76
            idx = atoi(getvar("wims_texbasesize")) ;
77
            if(idx < 0 || idx > 11){ idx = 6; } // the default value 120%
78
          }
79
          // now write the "char" 200% into variable "mathml_tex_size"
80
          snprintf(mathml_tex_size,sizeof(mathml_tex_size),"%d%%",texsize_list[idx]);
81
          // int --> char : added % sign (needed for mathml) [%% = escaped %]
82
          if(mathml_tex_size == NULL || strlen(mathml_tex_size) == 0 ){ // this should not happen
83
            sprintf(mathml_tex_size,"%s","120%");
84
            // if it goes wrong we set 120% 
85
            // default in itex2MML was 110% : but we thought it was too small... ?
86
          }
87
          char zoom[2];
88
          snprintf(zoom, 2 ,"%d",use_js_zoom);// int --> char : "0" or "1"
89
          char *argv[]={"wims_mathml","--use-zoom",zoom,"--tex-size",mathml_tex_size,"--max-mml-size",mml_buffer,"--tex-string",p,NULL};
90
          /* This is the child process. Close other end first. */
91
          close(my_pipe[0]);
92
          dup2(my_pipe[1], 1);  // send stdout to the pipe
93
          dup2(my_pipe[1], 2);  // send stderr to the pipe
94
          close(my_pipe[1]);
95
          execv("../bin/wims_mathml",argv);
96
          internal_error("could not execute wims_mathml\n");
97
          mathalign_base = 1;
98
          return 0; // got to insmath with gifs
99
      }
100
      else
101
      {
102
          if (pid < (pid_t) 0){
103
            close(my_pipe[0]);  // close the read end of the pipe in the parent
104
            close(my_pipe[1]);  // close the write end of the pipe in the parent
105
            internal_error("mathml(): fork() failure.\n");
106
            mathalign_base = 1;
107
            return 0; // got to insmath with gifs
108
          }
109
          else
110
          {
111
            FILE *stream;
112
            close(my_pipe[1]);  // close the write end of the pipe in the parent
113
            stream = fdopen (my_pipe[0], "r");
114
            char buffer[MAX_LINELEN+1];
115
            // make buffer filled with 'zero/null'
116
            memset(buffer,'\0',MAX_LINELEN);//bzero(buffer,maxsize);
117
            /* read output from program line by line*/
118
            if (option == 1) {
119
                *p=0;
120
                while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
5537 schaersvoo 121
                    if(strcmp(buffer,"ERROR") != 0){
122
                        mystrncpy(p, buffer, MAX_LINELEN-1);
123
                    }
124
                    else
125
                    {
126
                        mathalign_base=1;
127
                        return 0;
128
                    }
5522 bpr 129
                }
130
            }
131
            else
132
            {
5537 schaersvoo 133
                while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
134
                    if(strcmp(buffer,"ERROR") != 0){
135
                        output("%s", buffer);
136
                    }
137
                    else
138
                    {
139
                        mathalign_base=1;
140
                        return 0;
5522 bpr 141
                    }
5537 schaersvoo 142
                }
5522 bpr 143
            }
144
            fclose (stream);
145
            close(my_pipe[0]);
146
            int status;
147
            waitpid(pid, &status, 0);
148
          }
149
       }
150
    }
151
    return 1;
152
}