Subversion Repositories wimsdev

Rev

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

Rev Author Line No. Line
7673 bpr 1
/*    Copyright (C) 2012 WIMSDEV
5522 bpr 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
 */
8185 bpr 18
#include "wims.h"
8490 bpr 19
int disable_mathml;
5522 bpr 20
int mathml(char *p, int option ){
5578 schaersvoo 21
    if (strlen(p)==0) return 1 ;
5522 bpr 22
    if( mathalign_base <= 1){
23
      internal_error(" why is wims trying mathml()?\n");
5595 schaersvoo 24
      return 0; // go to insmath with gifs
5522 bpr 25
    }
8490 bpr 26
    if( disable_mathml==1 || atoi(getvar("disable_mathml"))==1) return 0;
5522 bpr 27
    if(strlen(p) > MAX_LINELEN ){ // too big ? probably too big for gifs as well ; but error signalling is better in gif-methods
28
      mathalign_base = 1;// 0 or 1 position of tex_gifs
5595 schaersvoo 29
      return 0; // go to insmath with gifs
5522 bpr 30
    }
31
    //singlespace(p); // needed for check unbalanced \left\right in wims_mathml.cc --> insmath.c
32
    singlespace(p);
33
    int my_pipe[2];
34
    pid_t pid;
35
    if(pipe(my_pipe)){// pipe could not be opened...
36
      internal_error("mathml(): pipe() failure.\n");
37
      mathalign_base = 1;
5595 schaersvoo 38
      return 0; // go to insmath with gifs
5522 bpr 39
    }
40
    else
41
    {
42
      pid = fork();
43
      if (pid == (pid_t) 0){
9568 schaersvoo 44
          /*
45
           this buffer should probably be set to 0.5 * MAX_LINELEN : mathml is very big !
46
           if output mathml string larger ;ERROR will be signaled by wims_mathml.
47
          */
5522 bpr 48
          char mml_buffer[MAX_LINELEN+1];
49
          sprintf(mml_buffer, "%d", MAX_LINELEN);// int --> char
9568 schaersvoo 50
          /* setting --tex-size argument to wims_mathml.cc */
51
          char mathml_tex_size[64];/*this should be a string like "250%"*/
5556 schaersvoo 52
          int texsize_list[]={20,30,50,70,90,100,120,130,150,180,220,300};
9568 schaersvoo 53
          /*
54
          analogue to  mathfonts.c but now we talk %  and we could make any amount of changes
55
          not limited by font-size !!
56
          when math_with_gifs is removed from wims, we can rethink / improve this
57
          */
58
          int idx = 5;
59
          int use_js_zoom = 0;
60
          /*
61
          default js-zoom in mathml if disabled
62
          enable via adm/light
63
          next code is stolen from wims.c
64
          */
5522 bpr 65
          if(getvar("useropts") != NULL || getvar("wims_useropts") != NULL){
66
            char *u;
9568 schaersvoo 67
            u = getvar("useropts");
68
            /* set via adm/light or via cookie? */
69
            if(u == NULL || *u == 0){ u=getvar("wims_useropts");} /* wims_user? look into def file */
5522 bpr 70
            if(u != NULL && *u != 0){
9568 schaersvoo 71
                if(myisdigit(u[0])){ /* 2 digit code : 12,22,32,...,92  [tex_size mathalignbase] */
5522 bpr 72
                  idx = u[0] - '0';
9568 schaersvoo 73
                  if(idx < 0 || idx > 11){ idx = 5; } /* the default value 100% */
5522 bpr 74
                }
9568 schaersvoo 75
                if(myisdigit(u[2]) && u[2]!=0){
76
                /*
77
                 suppose we add 0 or 1 to useropts for using js-zoom on mathml?
78
                 3 digitcode:  621 == fontsize idx=6 & use mathml & use zoom
79
                */
5522 bpr 80
                  use_js_zoom = u[2] - '0';
81
                  if(use_js_zoom > 1 || use_js_zoom < 0){
9568 schaersvoo 82
                      use_js_zoom = 0;
83
                      /* disable js-zooming of mathml */
5522 bpr 84
                  }
85
                }
86
            }
87
          }
88
          else
9568 schaersvoo 89
          { /* we take the default setting from adm/management config */
5522 bpr 90
            idx = atoi(getvar("wims_texbasesize")) ;
9568 schaersvoo 91
            if(idx < 0 || idx > 11){ idx = 5; } /* the default value 100% */
5522 bpr 92
          }
9568 schaersvoo 93
          /*
94
           check is a module wants to disable zooming (eg drag&drop or others )
95
           !set disable_zoom=yes
96
           if not set: disable_zoom="no"; see config.c
97
          */
5578 schaersvoo 98
          if( strcmp( getvar("disable_zoom") , "yes" ) == 0 ){
7673 bpr 99
             use_js_zoom = 0;
5578 schaersvoo 100
          }
9568 schaersvoo 101
          /* now write the "char" 100% into variable "mathml_tex_size" */
5522 bpr 102
          snprintf(mathml_tex_size,sizeof(mathml_tex_size),"%d%%",texsize_list[idx]);
9568 schaersvoo 103
          /* int --> char : added % sign (needed for mathml) [%% = escaped %] */
8894 bpr 104
          if(strlen(mathml_tex_size) == 0 ){ // this should not happen
9568 schaersvoo 105
            sprintf(mathml_tex_size,"%s","100%");
106
            /* if it goes wrong we set 100% */
5522 bpr 107
          }
108
          char zoom[2];
9568 schaersvoo 109
          snprintf(zoom, 2 ,"%d",use_js_zoom);/* int --> char : "0" or "1" */
110
/*
111
jm.evers 30/9/2015
112
 
113
FOR TESTING SYNCHRONISATION FONTSIZE HTML--MATHML
114
SET DEFAULT tex-size = 100 %
115
and use a
116
<span style="fonst-size:1em" ><math .. > ... </math></span>
117
in wims_mathml.y
118
 
119
zooming will be adressed (rewritten) when things are OK !!
120
since zooming is only interesting in native MathML browsers (FireFox and Gecko family)
121
the zooming could be prepared in exec.c (where mathjax is included for all other browsers)
122
by adding a zoom function and mouselistener on span element (wims_mathml.y)
123
 
124
see forum post of Eric Reyssat
125
http://wimsedu.info/?topic=unites-de-mesure-et-mathml/#post-3105
126
 
127
*/
128
          char *argv[]={"wims_mathml","--use-zoom",zoom,"--tex-size 100%%","--max-mml-size",mml_buffer,"--tex-string",p,NULL};
129
          /* This is the child process. Close other end first. */
5522 bpr 130
          close(my_pipe[0]);
9568 schaersvoo 131
          dup2(my_pipe[1], 1);  /* send stdout to the pipe */
132
          dup2(my_pipe[1], 2);  /* send stderr to the pipe */
5522 bpr 133
          close(my_pipe[1]);
134
          execv("../bin/wims_mathml",argv);
135
          internal_error("could not execute wims_mathml\n");
136
          mathalign_base = 1;
9568 schaersvoo 137
          return 0; /* go to insmath with gifs ! */
5522 bpr 138
      }
139
      else
140
      {
141
          if (pid < (pid_t) 0){
9568 schaersvoo 142
            close(my_pipe[0]);  /* close the read end of the pipe in the parent */
143
            close(my_pipe[1]);  /* close the write end of the pipe in the parent */
5522 bpr 144
            internal_error("mathml(): fork() failure.\n");
145
            mathalign_base = 1;
9568 schaersvoo 146
            return 0; /* go to insmath with gifs */
5522 bpr 147
          }
148
          else
149
          {
7673 bpr 150
             int status;
5522 bpr 151
            FILE *stream;
9568 schaersvoo 152
            close(my_pipe[1]);  /* close the write end of the pipe in the parent */
5522 bpr 153
            stream = fdopen (my_pipe[0], "r");
154
            char buffer[MAX_LINELEN+1];
9568 schaersvoo 155
            /* make buffer filled with 'zero/null' */
156
            memset(buffer,'\0',MAX_LINELEN); /* or use bzero(buffer,maxsize); */
157
            /* read output from program line by line */
5522 bpr 158
            if (option == 1) {
159
                *p=0;
160
                while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
7673 bpr 161
                     if(strcmp(buffer,"ERROR") != 0){
162
                      mystrncpy(p, buffer, MAX_LINELEN-1);
163
                     }
8155 bpr 164
                    else /* ERROR close stream; close pipe; wait for clean exit */
5537 schaersvoo 165
                    {
8155 bpr 166
                  fclose (stream); /* do not know if this is really needed... but it won't hurt ? */
7673 bpr 167
                  close(my_pipe[0]);
168
                  waitpid(pid, &status, 0);
8155 bpr 169
                     mathalign_base=1; /* go to insmath with gifs */
7673 bpr 170
                     return 0;
5537 schaersvoo 171
                    }
5522 bpr 172
                }
173
            }
8155 bpr 174
            else /* this will probably not used ? remove it ? */
5522 bpr 175
            {
7673 bpr 176
             while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
177
                 if(strcmp(buffer,"ERROR") != 0){
178
                  output("%s", buffer);
179
                 }
5537 schaersvoo 180
                    else
181
                    {
7673 bpr 182
                     mathalign_base=1;
183
                     return 0;
5522 bpr 184
                    }
5537 schaersvoo 185
                }
5522 bpr 186
            }
187
            fclose (stream);
188
            close(my_pipe[0]);
189
            waitpid(pid, &status, 0);
190
          }
191
       }
192
    }
193
    return 1;
194
}