Subversion Repositories wimsdev

Rev

Rev 9618 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. #include "wims.h"
  19. int disable_mathml;
  20. int mathml(char *p, int option ){
  21.   if (strlen(p)==0) return 1 ;
  22.   if( mathalign_base <= 1){
  23.     internal_error(" why is wims trying mathml()?\n");
  24.     return 0; // go to insmath with gifs
  25.   }
  26.   if( disable_mathml==1 || atoi(getvar("disable_mathml"))==1) return 0;
  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
  29.     return 0; // go to insmath with gifs
  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;
  38.     return 0; // go to insmath with gifs
  39.   }
  40.   else
  41.   {
  42.     pid = fork();
  43.     if (pid == (pid_t) 0){
  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.           */
  48.       char mml_buffer[MAX_LINELEN+1];
  49.       sprintf(mml_buffer, "%d", MAX_LINELEN);// int --> char
  50.           /* setting --tex-size argument to wims_mathml.cc */
  51.       char mathml_tex_size[64];/*this should be a string like "250%"*/
  52.       int texsize_list[]={20,30,50,70,90,100,120,130,150,180,220,300};
  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.           */
  65.       if(getvar("useropts") != NULL || getvar("wims_useropts") != NULL){
  66.         char *u;
  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 */
  70.         if(u != NULL && *u != 0){
  71.           if(myisdigit(u[0])){ /* 2 digit code : 12,22,32,...,92  [tex_size mathalignbase] */
  72.             idx = u[0] - '0';
  73.             if(idx < 0 || idx > 11) idx = 5; /* the default value 100% */
  74.           }
  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.                 */
  80.              use_js_zoom = u[2] - '0';
  81.             if(use_js_zoom > 1 || use_js_zoom < 0){
  82.               use_js_zoom = 0;
  83.                       /* disable js-zooming of mathml */
  84.             }
  85.           }
  86.         }
  87.       }
  88.       else
  89.       { /* we take the default setting from adm/management config */
  90.         idx = atoi(getvar("wims_texbasesize")) ;
  91.         if(idx < 0 || idx > 11){ idx = 5; } /* the default value 100% */
  92.       }
  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.           */
  98.       if( strcmp( getvar("disable_zoom") , "yes" ) == 0 ) use_js_zoom = 0;
  99.           /* now write the "char" 100% into variable "mathml_tex_size" */
  100.       snprintf(mathml_tex_size,sizeof(mathml_tex_size),"%d%%",texsize_list[idx]);
  101.           /* int --> char : added % sign (needed for mathml) [%% = escaped %] */
  102.       if(strlen(mathml_tex_size) == 0 ){ // this should not happen
  103.         sprintf(mathml_tex_size,"%s","100%");
  104.             /* if it goes wrong we set 100% */
  105.       }
  106.       char zoom[2];
  107.       snprintf(zoom, 2 ,"%d",use_js_zoom);/* int --> char : "0" or "1" */
  108. /*
  109. jm.evers 30/9/2015
  110.  
  111. FOR TESTING SYNCHRONISATION FONTSIZE HTML--MATHML
  112. SET DEFAULT tex-size = 100 %
  113. and use a
  114. <span style="fonst-size:1em" ><math .. > ... </math></span>
  115. in wims_mathml.y
  116.  
  117. zooming will be adressed (rewritten) when things are OK !!
  118. since zooming is only interesting in native MathML browsers (FireFox and Gecko family)
  119. the zooming could be prepared in exec.c (where mathjax is included for all other browsers)
  120. by adding a zoom function and mouselistener on span element (wims_mathml.y)
  121.  
  122. see forum post of Eric Reyssat
  123. http://wimsedu.info/?topic=unites-de-mesure-et-mathml/#post-3105
  124.  
  125. */
  126.       char *argv[]={"wims_mathml","--use-zoom",zoom,"--tex-size 100%","--max-mml-size",mml_buffer,"--tex-string",p,NULL};
  127.   /* This is the child process. Close other end first. */
  128.       close(my_pipe[0]);
  129.       dup2(my_pipe[1], 1);  /* send stdout to the pipe */
  130.       dup2(my_pipe[1], 2);  /* send stderr to the pipe */
  131.       close(my_pipe[1]);
  132.       execv("../bin/wims_mathml",argv);
  133.       internal_error("could not execute wims_mathml\n");
  134.       mathalign_base = 1;
  135.       return 0; /* go to insmath with gifs ! */
  136.     }
  137.     else
  138.     {
  139.       if (pid < (pid_t) 0){
  140.         close(my_pipe[0]);  /* close the read end of the pipe in the parent */
  141.         close(my_pipe[1]);  /* close the write end of the pipe in the parent */
  142.         internal_error("mathml(): fork() failure.\n");
  143.         mathalign_base = 1;
  144.         return 0; /* go to insmath with gifs */
  145.       }
  146.       else
  147.       {
  148.         int status;
  149.         FILE *stream;
  150.         close(my_pipe[1]);  /* close the write end of the pipe in the parent */
  151.         stream = fdopen (my_pipe[0], "r");
  152.         char buffer[MAX_LINELEN+1];
  153.             /* make buffer filled with 'zero/null' */
  154.             memset(buffer,'\0',MAX_LINELEN); /* or use bzero(buffer,maxsize); */
  155.             /* read output from program line by line */
  156.         if (option == 1) {
  157.           *p=0;
  158.           while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
  159.             if(strcmp(buffer,"ERROR") != 0)
  160.               mystrncpy(p, buffer, MAX_LINELEN-1);
  161.             else /* ERROR close stream; close pipe; wait for clean exit */
  162.             {
  163.               fclose (stream); /* do not know if this is really needed... but it won't hurt ? */
  164.               close(my_pipe[0]);
  165.               waitpid(pid, &status, 0);
  166.               mathalign_base=1; /* go to insmath with gifs */
  167.               return 0;
  168.             }
  169.           }
  170.         }
  171.         else /* this will probably not used ? remove it ? */
  172.         {
  173.           while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
  174.             if(strcmp(buffer,"ERROR") != 0) output("%s", buffer);
  175.             else
  176.             {
  177.               mathalign_base=1;
  178.               return 0;
  179.             }
  180.           }
  181.         }
  182.         fclose (stream);
  183.         close(my_pipe[0]);
  184.         waitpid(pid, &status, 0);
  185.       }
  186.     }
  187.   }
  188.   return 1;
  189. }
  190.