Annotation of loncom/xml/lonplot.pm, revision 1.156

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.156   ! foxr        4: # $Id: lonplot.pm,v 1.155 2012/02/22 10:16:39 foxr Exp $
1.1       matthew     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.35      matthew    28: 
1.149     jms        29: 
                     30: 
                     31: 
1.1       matthew    32: package Apache::lonplot;
1.10      matthew    33: 
1.1       matthew    34: use strict;
1.89      matthew    35: use warnings FATAL=>'all';
                     36: no warnings 'uninitialized';
1.10      matthew    37: use Apache::File;
1.1       matthew    38: use Apache::response;
1.2       matthew    39: use Apache::lonxml;
1.20      matthew    40: use Apache::edit;
1.106     albertel   41: use Apache::lonnet;
1.113     www        42: use LONCAPA;
                     43:  
1.10      matthew    44: 
1.129     albertel   45: use vars qw/$weboutputformat $version/;
1.97      matthew    46: 
1.109     foxr       47: 
1.108     foxr       48: 
1.33      harris41   49: BEGIN {
1.97      matthew    50:     &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
                     51:     #
                     52:     # Determine the version of GNUPLOT
                     53:     $weboutputformat = 'gif';
1.129     albertel   54:     my $versionstring = `gnuplot --version 2>/dev/null`;
                     55:     ($version) = ($versionstring =~ /^gnuplot ([\d.]+)/);
                     56:     if ($version >= 4) {
1.97      matthew    57:         $weboutputformat = 'png';
                     58:     }
1.108     foxr       59:     
                     60: }
                     61: 
1.1       matthew    62: 
1.149     jms        63: =pod
                     64: 
1.10      matthew    65: ## 
                     66: ## Description of data structures:
                     67: ##
                     68: ##  %plot       %key    %axis
                     69: ## --------------------------
                     70: ##  height      title   color
                     71: ##  width       box     xmin
                     72: ##  bgcolor     pos     xmax
                     73: ##  fgcolor             ymin
                     74: ##  transparent         ymax
                     75: ##  grid
                     76: ##  border
                     77: ##  font
1.19      matthew    78: ##  align
1.10      matthew    79: ##
                     80: ##  @labels: $labels[$i] = \%label
                     81: ##           %label: text, xpos, ypos, justify
1.14      matthew    82: ##
1.10      matthew    83: ##  @curves: $curves[$i] = \%curve
1.14      matthew    84: ##           %curve: name, linestyle, ( function | data )
1.10      matthew    85: ##
                     86: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
                     87: ##                            [y1,y2,y3,y4] ]
                     88: ##
1.21      matthew    89: 
                     90: ###################################################################
                     91: ##                                                               ##
                     92: ##        Tests used in checking the validitity of input         ##
                     93: ##                                                               ##
                     94: ###################################################################
1.29      matthew    95: 
1.149     jms        96: =cut
                     97: 
1.32      matthew    98: my $max_str_len = 50;    # if a label, title, xlabel, or ylabel text
                     99:                          # is longer than this, it will be truncated.
                    100: 
1.135     faziophi  101: my %linetypes =
                    102:     (
                    103:      solid          => 1,
                    104:      dashed         => 0
                    105:     );
                    106: 
1.29      matthew   107: my %linestyles = 
                    108:     (
                    109:      lines          => 2,     # Maybe this will be used in the future
                    110:      linespoints    => 2,     # to check on whether or not they have 
                    111:      dots	    => 2,     # supplied enough <data></data> fields
                    112:      points         => 2,     # to use the given line style.  But for
                    113:      steps	    => 2,     # now there are more important things 
                    114:      fsteps	    => 2,     # for me to deal with.
                    115:      histeps        => 2,
1.34      matthew   116:      errorbars	    => 3,
                    117:      xerrorbars	    => [3,4],
                    118:      yerrorbars	    => [3,4],
1.35      matthew   119:      xyerrorbars    => [4,6],
1.34      matthew   120:      boxes          => 3,
1.110     albertel  121:      filledcurves   => 2,
1.35      matthew   122:      vector	    => 4
1.29      matthew   123:     );		    
                    124: 
1.11      matthew   125: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19      matthew   126: my $real_test      = 
                    127:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.62      matthew   128: my $pos_real_test  =
                    129:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
1.79      matthew   130: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-fA-F]{6}$/};
1.1       matthew   131: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
1.15      matthew   132: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.119     albertel  133: my $sml_test       = sub {$_[0]=~/^(\d+|small|medium|large)$/};
1.29      matthew   134: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.67      matthew   135: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
1.21      matthew   136: 
                    137: ###################################################################
                    138: ##                                                               ##
                    139: ##                      Attribute metadata                       ##
                    140: ##                                                               ##
                    141: ###################################################################
1.47      matthew   142: my @gnuplot_edit_order = 
1.123     albertel  143:     qw/alttag bgcolor fgcolor height width texwidth fontface font texfont
                    144:     transparent grid samples 
                    145:     border align plotcolor plottype gridtype lmargin rmargin
1.115     albertel  146:     tmargin bmargin major_ticscale minor_ticscale boxwidth gridlayer fillstyle
1.110     albertel  147:     pattern solid/;
1.101     matthew   148: 
1.105     matthew   149: my $margin_choices = ['default',0..20];
1.48      matthew   150: 
1.47      matthew   151: my %gnuplot_defaults = 
1.1       matthew   152:     (
1.64      matthew   153:      alttag       => {
                    154: 	 default     => 'dynamically generated plot',
                    155: 	 test        => $words_test,
1.120     albertel  156: 	 description => 'Brief description of the plot',
1.64      matthew   157:       	 edit_type   => 'entry',
                    158: 	 size        => '40'
                    159: 	 },
1.20      matthew   160:      height       => {
1.65      matthew   161: 	 default     => 300,
1.20      matthew   162: 	 test        => $int_test,
1.120     albertel  163: 	 description => 'Height of image (pixels)',
1.38      matthew   164:       	 edit_type   => 'entry',
                    165: 	 size        => '10'
1.20      matthew   166: 	 },
                    167:      width        => {
1.65      matthew   168: 	 default     => 400,
1.20      matthew   169: 	 test        => $int_test,
1.120     albertel  170: 	 description => 'Width of image (pixels)',
1.38      matthew   171: 	 edit_type   => 'entry',
                    172: 	 size        => '10'
1.20      matthew   173: 	 },
                    174:      bgcolor      => {
                    175: 	 default     => 'xffffff',
                    176: 	 test        => $color_test, 
1.120     albertel  177: 	 description => 'Background color of image (xffffff)',
1.38      matthew   178: 	 edit_type   => 'entry',
1.154     foxr      179: 	 size        => '10',
                    180:          class       => 'colorchooser'
1.20      matthew   181: 	 },
                    182:      fgcolor      => {
                    183: 	 default     => 'x000000',
                    184: 	 test        => $color_test,
1.120     albertel  185: 	 description => 'Foreground color of image (x000000)',
1.38      matthew   186: 	 edit_type   => 'entry',
1.154     foxr      187: 	 size        => '10',
                    188:          class       => 'colorchooser'
1.20      matthew   189: 	 },
                    190:      transparent  => {
                    191: 	 default     => 'off',
                    192: 	 test        => $onoff_test, 
1.34      matthew   193: 	 description => 'Transparent image',
1.45      matthew   194: 	 edit_type   => 'onoff'
1.20      matthew   195: 	 },
                    196:      grid         => {
1.65      matthew   197: 	 default     => 'on',
1.20      matthew   198: 	 test        => $onoff_test, 
1.34      matthew   199: 	 description => 'Display grid',
1.45      matthew   200: 	 edit_type   => 'onoff'
1.20      matthew   201: 	 },
1.110     albertel  202:      gridlayer    => {
                    203: 	 default     => 'off',
                    204: 	 test        => $onoff_test, 
                    205: 	 description => 'Display grid front layer over filled boxes or filled curves',
                    206: 	 edit_type   => 'onoff'
                    207: 	 },
                    208:      box_border   => {
                    209: 	 default     => 'noborder',
                    210: 	 test        => sub {$_[0]=~/^(noborder|border)$/},
                    211: 	 description => 'Draw border for boxes',
                    212: 	 edit_type   => 'choice',
                    213: 	 choices     => ['border','noborder']
                    214: 	 },
1.20      matthew   215:      border       => {
                    216: 	 default     => 'on',
                    217: 	 test        => $onoff_test, 
1.34      matthew   218: 	 description => 'Draw border around plot',
1.45      matthew   219: 	 edit_type   => 'onoff'
1.20      matthew   220: 	 },
                    221:      font         => {
1.119     albertel  222: 	 default     => '9',
1.20      matthew   223: 	 test        => $sml_test,
1.125     albertel  224: 	 description => 'Font size to use in web output (pts)',
1.20      matthew   225: 	 edit_type   => 'choice',
1.126     albertel  226: 	 choices     => [['5','5 (small)'],'6','7','8',['9','9 (medium)'],'10',['11','11 (large)'],'12','15']
1.20      matthew   227: 	 },
1.120     albertel  228:      fontface     => {
                    229:         default     => 'sans-serif',
                    230:         test        => sub {$_[0]=~/^(sans-serif|serif|classic)$/},
                    231:         description => 'Type of font to use',
                    232:         edit_type   => 'choice',
                    233:         choices     => ['sans-serif','serif', 'classic']
                    234:         },
1.110     albertel  235:      samples      => {
1.77      matthew   236: 	 default     => '100',
                    237: 	 test        => $int_test,
                    238: 	 description => 'Number of samples for non-data plots',
                    239: 	 edit_type   => 'choice',
                    240: 	 choices     => ['100','200','500','1000','2000','5000']
                    241: 	 },
1.20      matthew   242:      align        => {
1.116     albertel  243: 	 default     => 'middle',
                    244: 	 test        => sub {$_[0]=~/^(left|right|middle|center)$/},
1.120     albertel  245: 	 description => 'Alignment for image in HTML',
1.20      matthew   246: 	 edit_type   => 'choice',
1.116     albertel  247: 	 choices     => ['left','right','middle']
1.82      matthew   248: 	 },
                    249:      texwidth     => {
                    250:          default     => '93',
                    251:          test        => $int_test,
                    252:          description => 'Width of plot when printed (mm)',
                    253:          edit_type   => 'entry',
                    254:          size        => '5'
                    255:          },
1.110     albertel  256:      texfont      => {
1.92      matthew   257:          default     => '22',
                    258:          test        => $int_test,
                    259:          description => 'Font size to use in TeX output (pts):',
                    260:          edit_type   => 'choice',
1.96      matthew   261:          choices     => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
1.92      matthew   262:          },
1.110     albertel  263:      plotcolor    => {
1.105     matthew   264:          default     => 'monochrome',
                    265:          test        => sub {$_[0]=~/^(monochrome|color|colour)$/},
                    266:          description => 'Color setting for printing:',
                    267:          edit_type   => 'choice',
                    268:          choices     => [qw/monochrome color colour/],
                    269:          },
1.110     albertel  270:      pattern      => {
                    271: 	 default     => '',
                    272: 	 test        => $int_test,
1.120     albertel  273: 	 description => 'Pattern value for boxes:',
1.110     albertel  274: 	 edit_type   => 'choice',
                    275:          choices     => [0,1,2,3,4,5,6]
                    276:          },
                    277:      solid        => {
                    278:          default     => 0,
                    279:          test        => $real_test,
                    280:          description => 'The density of fill style for boxes',
                    281:          edit_type   => 'entry',
                    282:          size        => '5'
                    283:          },
                    284:      fillstyle    => {
                    285: 	 default     => 'empty',
                    286: 	 test        => sub {$_[0]=~/^(empty|solid|pattern)$/},
                    287: 	 description => 'Filled style for boxes:',
                    288: 	 edit_type   => 'choice',
                    289:          choices     => ['empty','solid','pattern']
                    290:          },
                    291:      plottype     => {
1.87      matthew   292: 	 default     => 'Cartesian',
                    293: 	 test        => sub {$_[0]=~/^(Polar|Cartesian)$/},
                    294: 	 description => 'Plot type:',
                    295: 	 edit_type   => 'choice',
1.94      matthew   296:          choices     => ['Cartesian','Polar']
1.87      matthew   297:          },
1.115     albertel  298:      gridtype     => {
                    299: 	 default     => 'Cartesian',
1.118     albertel  300: 	 test        => sub {$_[0]=~/^(Polar|Cartesian|Linear-Log|Log-Linear|Log-Log)$/},
1.115     albertel  301: 	 description => 'Grid type:',
                    302: 	 edit_type   => 'choice',
1.118     albertel  303:          choices     => ['Cartesian','Polar','Linear-Log','Log-Linear','Log-Log']
1.115     albertel  304:          },
1.110     albertel  305:      lmargin      => {
1.101     matthew   306: 	 default     => 'default',
                    307: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
                    308: 	 description => 'Left margin width (pts):',
                    309: 	 edit_type   => 'choice',
                    310:          choices     => $margin_choices,
                    311:          },
1.110     albertel  312:      rmargin      => {
1.101     matthew   313: 	 default     => 'default',
                    314: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
                    315: 	 description => 'Right margin width (pts):',
                    316: 	 edit_type   => 'choice',
                    317:          choices     => $margin_choices,
                    318:          },
1.110     albertel  319:      tmargin      => {
1.101     matthew   320: 	 default     => 'default',
                    321: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
                    322: 	 description => 'Top margin width (pts):',
                    323: 	 edit_type   => 'choice',
                    324:          choices     => $margin_choices,
                    325:          },
1.110     albertel  326:      bmargin      => {
1.101     matthew   327: 	 default     => 'default',
                    328: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
1.104     www       329: 	 description => 'Bottom margin width (pts):',
1.101     matthew   330: 	 edit_type   => 'choice',
                    331:          choices     => $margin_choices,
                    332:          },
1.110     albertel  333:      boxwidth     => {
                    334: 	 default     => '',
                    335: 	 test        => $real_test, 
1.120     albertel  336: 	 description => 'Width of boxes, default is auto',
1.110     albertel  337: 	 edit_type   => 'entry',
                    338:          size        => '5'
                    339:          },
1.101     matthew   340:      major_ticscale  => {
                    341:          default     => '1',
                    342:          test        => $real_test,
                    343:          description => 'Size of major tic marks (plot coordinates)',
                    344:          edit_type   => 'entry',
                    345:          size        => '5'
                    346:          },
                    347:      minor_ticscale  => {
                    348:          default     => '0.5',
                    349:          test        => $real_test,
                    350:          description => 'Size of minor tic mark (plot coordinates)',
                    351:          edit_type   => 'entry',
                    352:          size        => '5'
                    353:          },
1.1       matthew   354:      );
                    355: 
                    356: my %key_defaults = 
                    357:     (
1.20      matthew   358:      title => { 
                    359: 	 default => '',
                    360: 	 test => $words_test,
                    361: 	 description => 'Title of key',
1.38      matthew   362: 	 edit_type   => 'entry',
                    363: 	 size        => '40'
1.20      matthew   364: 	 },
                    365:      box   => { 
                    366: 	 default => 'off',
                    367: 	 test => $onoff_test,
                    368: 	 description => 'Draw a box around the key?',
1.45      matthew   369: 	 edit_type   => 'onoff'
1.20      matthew   370: 	 },
                    371:      pos   => { 
                    372: 	 default => 'top right', 
                    373: 	 test => $key_pos_test, 
1.120     albertel  374: 	 description => 'Position of the key on the plot',
1.20      matthew   375: 	 edit_type   => 'choice',
                    376: 	 choices     => ['top left','top right','bottom left','bottom right',
                    377: 			 'outside','below']
                    378: 	 }
1.1       matthew   379:      );
                    380: 
                    381: my %label_defaults = 
                    382:     (
1.20      matthew   383:      xpos    => {
                    384: 	 default => 0,
                    385: 	 test => $real_test,
1.120     albertel  386: 	 description => 'X position of label (graph coordinates)',
1.38      matthew   387: 	 edit_type   => 'entry',
                    388: 	 size        => '10'
1.20      matthew   389: 	 },
                    390:      ypos    => {
                    391: 	 default => 0, 
                    392: 	 test => $real_test,
1.120     albertel  393: 	 description => 'Y position of label (graph coordinates)',
1.38      matthew   394: 	 edit_type   => 'entry',
                    395: 	 size        => '10'
1.20      matthew   396: 	 },
                    397:      justify => {
                    398: 	 default => 'left',    
                    399: 	 test => sub {$_[0]=~/^(left|right|center)$/},
                    400: 	 description => 'justification of the label text on the plot',
                    401: 	 edit_type   => 'choice',
                    402: 	 choices     => ['left','right','center']
1.134     raeburn   403:      },
                    404:      rotate => {
                    405:          default => 0,
                    406:          test => $real_test,
                    407:          description => 'Rotation of label (degrees)',
                    408:          edit_type   => 'entry',
                    409:          size        => '10',
1.20      matthew   410:      }
1.1       matthew   411:      );
                    412: 
1.89      matthew   413: my @tic_edit_order = ('location','mirror','start','increment','end',
                    414:                       'minorfreq');
1.45      matthew   415: my %tic_defaults =
                    416:     (
                    417:      location => {
                    418: 	 default => 'border', 
                    419: 	 test => sub {$_[0]=~/^(border|axis)$/},
1.90      matthew   420: 	 description => 'Location of major tic marks',
1.45      matthew   421: 	 edit_type   => 'choice',
                    422: 	 choices     => ['border','axis']
                    423: 	 },
                    424:      mirror => {
                    425: 	 default => 'on', 
                    426: 	 test => $onoff_test,
1.120     albertel  427: 	 description => 'Mirror tics on opposite axis?',
1.45      matthew   428: 	 edit_type   => 'onoff'
                    429: 	 },
                    430:      start => {
                    431: 	 default => '-10.0',
                    432: 	 test => $real_test,
1.90      matthew   433: 	 description => 'Start major tics at',
1.45      matthew   434: 	 edit_type   => 'entry',
                    435: 	 size        => '10'
                    436: 	 },
                    437:      increment => {
                    438: 	 default => '1.0',
                    439: 	 test => $real_test,
1.90      matthew   440: 	 description => 'Place a major tic every',
1.45      matthew   441: 	 edit_type   => 'entry',
                    442: 	 size        => '10'
                    443: 	 },
                    444:      end => {
                    445: 	 default => ' 10.0',
                    446: 	 test => $real_test,
1.90      matthew   447: 	 description => 'Stop major tics at ',
1.45      matthew   448: 	 edit_type   => 'entry',
                    449: 	 size        => '10'
                    450: 	 },
1.89      matthew   451:      minorfreq => {
                    452: 	 default => '0',
                    453: 	 test => $int_test,
1.98      matthew   454: 	 description => 'Number of minor tics per major tic mark',
1.89      matthew   455: 	 edit_type   => 'entry',
                    456: 	 size        => '10'
                    457: 	 },         
1.45      matthew   458:      );
                    459: 
1.152     faziophi  460: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax','xformat', 'yformat', 'xzero', 'yzero');
1.1       matthew   461: my %axis_defaults = 
                    462:     (
1.28      matthew   463:      color   => {
1.20      matthew   464: 	 default => 'x000000', 
                    465: 	 test => $color_test,
1.120     albertel  466: 	 description => 'Color of grid lines (x000000)',
1.38      matthew   467: 	 edit_type   => 'entry',
1.154     foxr      468: 	 size        => '10',
                    469:          class       => 'colorchooser'
1.20      matthew   470: 	 },
                    471:      xmin      => {
                    472: 	 default => '-10.0',
                    473: 	 test => $real_test,
1.120     albertel  474: 	 description => 'Minimum x-value shown in plot',
1.38      matthew   475: 	 edit_type   => 'entry',
                    476: 	 size        => '10'
1.20      matthew   477: 	 },
                    478:      xmax      => {
                    479: 	 default => ' 10.0',
                    480: 	 test => $real_test,
1.120     albertel  481: 	 description => 'Maximum x-value shown in plot',	 
1.38      matthew   482: 	 edit_type   => 'entry',
                    483: 	 size        => '10'
1.20      matthew   484: 	 },
                    485:      ymin      => {
                    486: 	 default => '-10.0',
                    487: 	 test => $real_test,
1.120     albertel  488: 	 description => 'Minimum y-value shown in plot',	 
1.38      matthew   489: 	 edit_type   => 'entry',
                    490: 	 size        => '10'
1.20      matthew   491: 	 },
                    492:      ymax      => {
                    493: 	 default => ' 10.0',
                    494: 	 test => $real_test,
1.120     albertel  495: 	 description => 'Maximum y-value shown in plot',	 
1.38      matthew   496: 	 edit_type   => 'entry',
                    497: 	 size        => '10'
1.132     albertel  498:         },
                    499:      xformat      => {
                    500:          default     => 'on',
                    501:          test        => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
                    502:          description => 'X-axis number formatting',
                    503:          edit_type   => 'choice',
                    504:          choices     => ['on', 'off', '2e', '2f'],
                    505:          },
                    506:      yformat      => {
                    507:          default     => 'on',
                    508:          test        => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
1.151     raeburn   509:          description => 'Y-axis number formatting',
1.132     albertel  510:          edit_type   => 'choice',
                    511:          choices     => ['on', 'off', '2e', '2f'],
                    512:          },
1.152     faziophi  513:      
                    514:      xzero => {
                    515:      	default => 'off',
                    516:      	test	=> sub {$_[0]=~/^(off|line|thick-line|dotted)$/},
                    517:      	description => 'Show x-zero (y=0) axis',
                    518:      	edit_type  => 'choice',
                    519:      	choices	=> ['off', 'line', 'thick-line', 'dotted'],
                    520:      	},
                    521:      
                    522:      yzero => {
                    523:      	default => 'off',
                    524:      	test	=> sub {$_[0]=~/^(off|line|thick-line|dotted)$/},
                    525:      	description => 'Show y-zero (x=0) axis',
                    526:      	edit_type  => 'choice',
                    527:      	choices	=> ['off', 'line', 'thick-line', 'dotted'],
                    528:      	},
1.1       matthew   529:      );
                    530: 
1.135     faziophi  531: my @curve_edit_order = ('color','name','linestyle','linewidth','linetype','pointtype','pointsize','limit');
1.60      matthew   532: 
1.1       matthew   533: my %curve_defaults = 
                    534:     (
1.20      matthew   535:      color     => {
                    536: 	 default => 'x000000',
                    537: 	 test => $color_test,
1.120     albertel  538: 	 description => 'Color of curve (x000000)',
1.38      matthew   539: 	 edit_type   => 'entry',
1.154     foxr      540: 	 size        => '10',
                    541: 	 class       => 'colorchooser'
1.20      matthew   542: 	 },
                    543:      name      => {
                    544: 	 default => '',
                    545: 	 test => $words_test,
1.120     albertel  546: 	 description => 'Name of curve to appear in key',
1.38      matthew   547: 	 edit_type   => 'entry',
                    548: 	 size        => '20'
1.20      matthew   549: 	 },
                    550:      linestyle => {
                    551: 	 default => 'lines',
                    552: 	 test => $linestyle_test,
1.135     faziophi  553: 	 description => 'Plot with:',
1.20      matthew   554: 	 edit_type   => 'choice',
1.38      matthew   555: 	 choices     => [keys(%linestyles)]
1.60      matthew   556: 	 },
1.119     albertel  557:      linewidth => {
1.130     albertel  558:          default     => 1,
1.119     albertel  559:          test        => $int_test,
1.135     faziophi  560:          description => 'Line width (may not apply to all plot styles)',
1.119     albertel  561:          edit_type   => 'choice',
                    562:          choices     => [1,2,3,4,5,6,7,8,9,10]
                    563:          },
1.135     faziophi  564:      linetype => {
                    565:          default     => 'solid',
                    566:          test        => sub {$_[0]=~/^(solid|dashed)$/},
                    567:          description => 'Line type (may not apply to all plot styles)',
                    568:          edit_type   => 'choice',
                    569:          choices     => ['solid', 'dashed']
                    570:          }, 
1.60      matthew   571:      pointsize => {
                    572:          default     => 1,
1.62      matthew   573:          test        => $pos_real_test,
1.135     faziophi  574:          description => 'Point size (may not apply to all plot styles)',
1.62      matthew   575:          edit_type   => 'entry',
                    576:          size        => '5'
                    577:          },
                    578:      pointtype => {
                    579:          default     => 1,
1.60      matthew   580:          test        => $int_test,
1.135     faziophi  581:          description => 'Point type (may not apply to all plot styles)',
1.60      matthew   582:          edit_type   => 'choice',
1.62      matthew   583:          choices     => [0,1,2,3,4,5,6]
1.110     albertel  584:          },
                    585:      limit     => {
                    586:          default     => 'closed',
1.135     faziophi  587: 	 test        => sub {$_[0]=~/^(above|below|closed|x1|x2|y1|y2)$/},
1.120     albertel  588:          description => 'Point to fill -- for filledcurves',
1.110     albertel  589:          edit_type   => 'choice',
1.135     faziophi  590:          choices     => ['above', 'below', 'closed','x1','x2','y1','y2']
1.110     albertel  591:          },
1.1       matthew   592:      );
                    593: 
1.21      matthew   594: ###################################################################
                    595: ##                                                               ##
                    596: ##                    parsing and edit rendering                 ##
                    597: ##                                                               ##
                    598: ###################################################################
1.107     foxr      599: 
                    600: undef %Apache::lonplot::plot;
                    601: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
1.1       matthew   602: 
1.47      matthew   603: sub start_gnuplot {
1.107     foxr      604:     undef(%Apache::lonplot::plot);   undef(%key);    undef(%axis);
1.89      matthew   605:     undef($title);  undef($xlabel); undef($ylabel);
                    606:     undef(@labels); undef(@curves);
                    607:     undef(%xtics);  undef(%ytics);
1.6       matthew   608:     #
1.1       matthew   609:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    610:     my $result='';
1.25      matthew   611:     &Apache::lonxml::register('Apache::lonplot',
1.45      matthew   612: 	     ('title','xlabel','ylabel','key','axis','label','curve',
                    613: 	      'xtics','ytics'));
1.29      matthew   614:     push (@Apache::lonxml::namespace,'lonplot');
1.51      matthew   615:     if ($target eq 'web' || $target eq 'tex') {
1.107     foxr      616: 	&get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
1.17      matthew   617: 			$tagstack->[-1]);
1.20      matthew   618:     } elsif ($target eq 'edit') {
1.47      matthew   619: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
                    620: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
1.116     albertel  621: 				    \@gnuplot_edit_order)
                    622: 	    .&Apache::edit::end_row()
                    623: 	    .&Apache::edit::start_spanning_row();
1.20      matthew   624:     } elsif ($target eq 'modified') {
                    625: 	my $constructtag=&Apache::edit::get_new_args
1.47      matthew   626: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
1.155     foxr      627: 
1.20      matthew   628: 	if ($constructtag) {
1.155     foxr      629: 	    #
                    630: 	    # The color chooser does not prepent x to the color values
                    631: 	    # Do that here:
                    632: 	    #
                    633: 	    foreach my $attribute ('bgcolor', 'fgcolor') {
                    634: 		my $value = $token->[2]{$attribute};
                    635: 		if (defined $value && !($value =~ /^x/)) {
                    636: 		    $token->[2]{$attribute} = 'x' . $value;
                    637: 		}
                    638: 	    }
1.20      matthew   639: 	    $result = &Apache::edit::rebuild_tag($token);
                    640: 	}
1.4       matthew   641:     }
1.21      matthew   642:     return $result;
1.1       matthew   643: }
                    644: 
1.47      matthew   645: sub end_gnuplot {
1.1       matthew   646:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    647:     pop @Apache::lonxml::namespace;
1.4       matthew   648:     &Apache::lonxml::deregister('Apache::lonplot',
                    649: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    650:     my $result = '';
1.56      albertel  651:     my $randnumber;
1.153     foxr      652:     my $tmpdir =LONCAPA::tempdir(); # Where temporary files live:
                    653: 
1.56      albertel  654:     # need to call rand everytime start_script would evaluate, as the
                    655:     # safe space rand number generator and the global rand generator 
1.95      www       656:     # are not separate
1.56      albertel  657:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    658: 	$target eq 'answer') {
                    659:       $randnumber=int(rand(1000));
                    660:     }
1.51      matthew   661:     if ($target eq 'web' || $target eq 'tex') {
1.21      matthew   662: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   663: 	##
                    664: 	## Determine filename
1.106     albertel  665: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.69      matthew   666: 	    '_'.time.'_'.$$.$randnumber.'_plot';
1.4       matthew   667: 	## Write the plot description to the file
1.51      matthew   668: 	&write_gnuplot_file($tmpdir,$filename,$target);
1.113     www       669: 	$filename = &escape($filename);
1.4       matthew   670: 	## return image tag for the plot
1.51      matthew   671: 	if ($target eq 'web') {
                    672: 	    $result .= <<"ENDIMAGE";
1.114     albertel  673: <img src    = "/cgi-bin/plot.$weboutputformat?file=$filename.data" 
1.107     foxr      674:      width  = "$Apache::lonplot::plot{'width'}"
                    675:      height = "$Apache::lonplot::plot{'height'}"
                    676:      align  = "$Apache::lonplot::plot{'align'}"
                    677:      alt    = "$Apache::lonplot::plot{'alttag'}" />
1.12      matthew   678: ENDIMAGE
1.51      matthew   679:         } elsif ($target eq 'tex') {
1.107     foxr      680: 	    &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
                    681: 	    &Apache::lonxml::debug(" gnuplot ht  = $Apache::lonplot::plot{'height'}");
1.91      albertel  682: 	    #might be inside the safe space, register the URL for later
                    683: 	    &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
1.111     albertel  684: 	    $result  = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'}\n";
1.153     foxr      685: 	    $result .= '\graphicspath{{'.$tmpdir.'}}'."\n";
1.113     www       686: 	    $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&unescape($filename).'.eps}';
1.51      matthew   687: 	}
1.20      matthew   688:     } elsif ($target eq 'edit') {
1.21      matthew   689: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   690:     }
1.1       matthew   691:     return $result;
                    692: }
1.2       matthew   693: 
1.45      matthew   694: 
                    695: ##--------------------------------------------------------------- xtics
                    696: sub start_xtics {
                    697:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    698:     my $result='';
1.51      matthew   699:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   700: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
                    701: 		    $tagstack->[-1]);
                    702:     } elsif ($target eq 'edit') {
                    703: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
                    704: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    705: 				    \@tic_edit_order);
                    706:     } elsif ($target eq 'modified') {
                    707: 	my $constructtag=&Apache::edit::get_new_args
                    708: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    709: 	if ($constructtag) {
                    710: 	    $result = &Apache::edit::rebuild_tag($token);
                    711: 	}
                    712:     }
                    713:     return $result;
                    714: }
                    715: 
                    716: sub end_xtics {
                    717:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    718:     my $result = '';
1.51      matthew   719:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   720:     } elsif ($target eq 'edit') {
                    721: 	$result.=&Apache::edit::tag_end($target,$token);
                    722:     }
                    723:     return $result;
                    724: }
                    725: 
                    726: ##--------------------------------------------------------------- ytics
                    727: sub start_ytics {
                    728:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    729:     my $result='';
1.51      matthew   730:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   731: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
                    732: 		    $tagstack->[-1]);
                    733:     } elsif ($target eq 'edit') {
                    734: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
                    735: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    736: 				    \@tic_edit_order);
                    737:     } elsif ($target eq 'modified') {
                    738: 	my $constructtag=&Apache::edit::get_new_args
                    739: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    740: 	if ($constructtag) {
                    741: 	    $result = &Apache::edit::rebuild_tag($token);
                    742: 	}
                    743:     }
                    744:     return $result;
                    745: }
                    746: 
                    747: sub end_ytics {
                    748:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    749:     my $result = '';
1.51      matthew   750:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   751:     } elsif ($target eq 'edit') {
                    752: 	$result.=&Apache::edit::tag_end($target,$token);
                    753:     }
                    754:     return $result;
                    755: }
                    756: 
1.119     albertel  757: ##-----------------------------------------------------------------font
1.120     albertel  758: my %font_properties =
                    759:     (
                    760:      'classic'    => {
                    761: 	 face       => 'classic',
                    762: 	 file       => 'DejaVuSansMono-Bold',
                    763: 	 printname  => 'Helvetica',
1.121     albertel  764: 	 tex_no_file => 1,
1.120     albertel  765:      },
                    766:      'sans-serif' => {
                    767: 	 face       => 'sans-serif',
                    768: 	 file       => 'DejaVuSans',
                    769: 	 printname  => 'DejaVuSans',
                    770:      },
                    771:      'serif'      => {
                    772: 	 face       => 'serif',
                    773: 	 file       => 'DejaVuSerif',
                    774: 	 printname  => 'DejaVuSerif',
                    775:      },
                    776:      );
                    777: 
1.119     albertel  778: sub get_font {
1.124     albertel  779:     my ($target) = @_;
1.120     albertel  780:     my ($size, $selected_font);
                    781: 
1.119     albertel  782:     if ( $Apache::lonplot::plot{'font'} =~ /^(small|medium|large)/) {
1.120     albertel  783: 	$selected_font = $font_properties{'classic'};
1.119     albertel  784: 	if ( $Apache::lonplot::plot{'font'} eq 'small') {
                    785: 	    $size = '5';
                    786: 	} elsif ( $Apache::lonplot::plot{'font'} eq 'medium') {
                    787: 	    $size = '9';
                    788: 	} elsif ( $Apache::lonplot::plot{'font'} eq 'large') {
1.126     albertel  789: 	    $size = '11';
1.119     albertel  790: 	} else {
                    791: 	    $size = '9';
                    792: 	}
                    793:     } else {
1.122     albertel  794: 	$size = $Apache::lonplot::plot{'font'};
1.120     albertel  795: 	$selected_font = $font_properties{$Apache::lonplot::plot{'fontface'}};
1.119     albertel  796:     }
1.124     albertel  797:     if ($target eq 'tex' && defined($Apache::lonplot::plot{'texfont'})) {
1.146     foxr      798: #	$selected_font = $font_properties{'classic'};
1.124     albertel  799: 	$size = $Apache::lonplot::plot{'texfont'};
                    800:     }
1.120     albertel  801:     return ($size, $selected_font);
1.119     albertel  802: }
1.45      matthew   803: 
1.1       matthew   804: ##----------------------------------------------------------------- key
                    805: sub start_key {
                    806:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    807:     my $result='';
1.51      matthew   808:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   809: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   810: 		    $tagstack->[-1]);
1.20      matthew   811:     } elsif ($target eq 'edit') {
1.25      matthew   812: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21      matthew   813: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   814:     } elsif ($target eq 'modified') {
                    815: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   816: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
1.20      matthew   817: 	if ($constructtag) {
                    818: 	    $result = &Apache::edit::rebuild_tag($token);
                    819: 	}
1.4       matthew   820:     }
1.1       matthew   821:     return $result;
                    822: }
                    823: 
                    824: sub end_key {
                    825:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    826:     my $result = '';
1.51      matthew   827:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   828:     } elsif ($target eq 'edit') {
1.21      matthew   829: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   830:     }
1.1       matthew   831:     return $result;
                    832: }
1.21      matthew   833: 
1.128     albertel  834: sub parse_label {
                    835:     my ($target,$text) = @_;
                    836:     my $parser=HTML::LCParser->new(\$text);
                    837:     my $result;
                    838:     while (my $token=$parser->get_token) {
                    839: 	if ($token->[0] eq 'S') {
                    840: 	    if ($token->[1] eq 'sub') {
                    841: 		$result .= '_{';
                    842: 	    } elsif ($token->[1] eq 'sup') {
                    843: 		$result .= '^{';
                    844: 	    } else {
                    845: 		$result .= $token->[4];
                    846: 	    }
                    847: 	} elsif ($token->[0] eq 'E') {
                    848: 	    if ($token->[1] eq 'sub'
                    849: 		|| $token->[1] eq 'sup') {
                    850: 		$result .= '}';
                    851: 	    } else {
                    852: 		$result .= $token->[2];
                    853: 	    }
                    854: 	} elsif ($token->[0] eq 'T') {
                    855: 	    $result .= &replace_entities($target,$token->[1]);
                    856: 	}
                    857:     }
                    858:     return $result;
                    859: }
                    860: 
1.143     foxr      861: #
                    862: #  Note that there are severe restrictions on font selection in the
                    863: # ps driver now.  later in life Gnuplot is supposed to support
                    864: # utf-8 fonts in the posts script driver.  When this happens,
                    865: # the tex entries with comments that include the word <FIX>
                    866: # should be changed to print the correct glyphs rather than some
                    867: # approximation or fallback of what is intended.
1.128     albertel  868: 
                    869: my %lookup = 
1.137     foxr      870:    (  # Greek alphabet:
1.139     foxr      871:       
                    872:       '(Alpha|#913)'    => {'tex' => '{/Symbol A}', 'web' => "\x{391}"},
                    873:       '(Beta|#914)'    => {'tex' => '{/Symbol B}', 'web' => "\x{392}"},
                    874:       '(Chi|#935)'     => {'tex' => '{/Symbol C}', 'web' => "\x{3A7}"},
                    875:       '(Delta|#916)'   => {'tex' => '{/Symbol D}', 'web' => "\x{394}"},
                    876:       '(Epsilon|#917)' => {'tex' => '{/Symbol E}', 'web' => "\x{395}"},
                    877:       '(Phi|#934)'     => {'tex' => '{/Symbol F}', 'web' => "\x{3A6}"},
                    878:       '(Gamma|#915)'   => {'tex' => '{/Symbol G}', 'web' => "\x{393}"},
                    879:       '(Eta|#919)'     => {'tex' => '{/Symbol H}', 'web' => "\x{397}"},
                    880:       '(Iota|#921)'    => {'tex' => '{/Symbol I}', 'web' => "\x{399}"},
                    881:       '(Kappa|#922)'   => {'tex' => '{/Symbol K}', 'web' => "\x{39A}"},
                    882:       '(Lambda|#923)'  => {'tex' => '{/Symbol L}', 'web' => "\x{39B}"},
                    883:       '(Mu|#924)'      => {'tex' => '{/Symbol M}', 'web' => "\x{39C}"},
                    884:       '(Nu|#925)'      => {'tex' => '{/Symbol N}', 'web' => "\x{39D}"},
                    885:       '(Omicron|#927)' => {'tex' => '{/Symbol O}', 'web' => "\x{39F}"},
                    886:       '(Pi|#928)'      => {'tex' => '{/Symbol P}', 'web' => "\x{3A0}"},
                    887:       '(Theta|#920)'   => {'tex' => '{/Symbol Q}', 'web' => "\x{398}"},
                    888:       '(Rho|#929)'     => {'tex' => '{/Symbol R}', 'web' => "\x{3A1}"},
                    889:       '(Sigma|#931)'   => {'tex' => '{/Symbol S}', 'web' => "\x{3A3}"},
                    890:       '(Tau|#932)'     => {'tex' => '{/Symbol T}', 'web' => "\x{3A4}"},
                    891:       '(Upsilon|#933)' => {'tex' => '{/Symbol U}', 'web' => "\x{3A5}"},
                    892:       '(Omega|#937)'   => {'tex' => '{/Symbol W}', 'web' => "\x{3A9}"},
                    893:       '(Xi|#926)'      => {'tex' => '{/Symbol X}', 'web' => "\x{39E}"},
                    894:       '(Psi|#936)'     => {'tex' => '{/Symbol Y}', 'web' => "\x{3A8}"},
                    895:       '(Zeta|#918)'    => {'tex' => '{/Symbol Z}', 'web' => "\x{396}"},
                    896:       '(alpha|#945)'   => {'tex' => '{/Symbol a}', 'web' => "\x{3B1}"},
                    897:       '(beta|#946)'    => {'tex' => '{/Symbol b}', 'web' => "\x{3B2}"},
                    898:       '(chi|#967)'     => {'tex' => '{/Symbol c}', 'web' => "\x{3C7}"},
                    899:       '(delta|#948)'   => {'tex' => '{/Symbol d}', 'web' => "\x{3B4}"},
                    900:       '(epsilon|#949)' => {'tex' => '{/Symbol e}', 'web' => "\x{3B5}"},
                    901:       '(phi|#966)'     => {'tex' => '{/Symbol f}', 'web' => "\x{3C6}"},
                    902:       '(gamma|#947)'   => {'tex' => '{/Symbol g}', 'web' => "\x{3B3}"},
                    903:       '(eta|#951)'     => {'tex' => '{/Symbol h}', 'web' => "\x{3B7}"},
                    904:       '(iota|#953)'    => {'tex' => '{/Symbol i}', 'web' => "\x{3B9}"},
                    905:       '(kappa|#954)'   => {'tex' => '{/Symbol k}', 'web' => "\x{3BA}"},
                    906:       '(lambda|#955)'  => {'tex' => '{/Symbol k}', 'web' => "\x{3BB}"},
                    907:       '(mu|#956)'      => {'tex' => '{/Symbol m}', 'web' => "\x{3BC}"},
                    908:       '(nu|#957)'      => {'tex' => '{/Symbol n}', 'web' => "\x{3BD}"},
                    909:       '(omicron|#959)' => {'tex' => '{/Symbol o}', 'web' => "\x{3BF}"},
                    910:       '(pi|#960)'      => {'tex' => '{/Symbol p}', 'web' => "\x{3C0}"},
                    911:       '(theta|#952)'   => {'tex' => '{/Symbol q}', 'web' => "\x{3B8}"},
                    912:       '(rho|#961)'     => {'tex' => '{/Symbol r}', 'web' => "\x{3C1}"},
                    913:       '(sigma|#963)'   => {'tex' => '{/Symbol s}', 'web' => "\x{3C3}"},
                    914:       '(tau|#964)'     => {'tex' => '{/Symbol t}', 'web' => "\x{3C4}"},
                    915:       '(upsilon|#965)' => {'tex' => '{/Symbol u}', 'web' => "\x{3C5}"},
                    916:       '(omega|#969)'   => {'tex' => '{/Symbol w}', 'web' => "\x{3C9}"},
                    917:       '(xi|#958)'      => {'tex' => '{/Symbol x}', 'web' => "\x{3BE}"},
                    918:       '(psi|#968)'     => {'tex' => '{/Symbol y}', 'web' => "\x{3C8}"},
                    919:       '(zeta|#950)'    => {'tex' => '{/Symbol z}', 'web' => "\x{3B6}"},
                    920:       '(thetasym|#977)' => {'tex' => '{/Symbol \165}', 'web' => "\x{3d1}"},
                    921:       '(upsih|#978)'   => {'tex' => '{/Symbol \241}', 'web' => "\x{3d2}"},
                    922:       '(piv|#982)'     => {'tex' => '{/Symbol \166}', 'web' => "\x{3d6}"},
1.137     foxr      923: 
                    924: 
                    925:       # Punctuation:
                    926:       
1.138     foxr      927:       '(quot|#034)'   => {'tex' =>  '\42',            'web' => '\42'},
                    928:       '(amp|#038)'    => {'tex' =>  '\46',            'web' => '\46'},
                    929:       '(lt|#060)'     => {'tex' =>  '\74',            'web' => '\74'},
                    930:       '(gt|#062)'     => {'tex' =>  '\76',            'web' => '\76'},
1.137     foxr      931:       '#131'          => {'tex' =>  '{/Symbol \246}', 'web' => "\x{192}"},
1.138     foxr      932:       '#132'          => {'tex' => '{/Text \271}',    'web' => "\x{201e}"},
                    933:       '#133'          => {'tex' => '{/Symbol \274}',  'web'=> "\x{2026}"},
                    934:       '#134'          => {'tex' => '{/Text \262}',    'web' => "\x{2020}"},
                    935:       '#135'          => {'tex' => '{/Text \263}',    'web' => "\x{2021}"},
                    936:       '#136'          => {'tex' => '\\\\^',           'web' => '\\\\^'},
1.143     foxr      937:       '#137'          => {'tex' => '%o',              'web' => "\x{2030}"}, # Per Mille <FIX>
                    938:       '#138'          => {'tex' => 'S',               'web' => "\x{160}"}, # S-Caron <FIX>
1.138     foxr      939:       '#139'          => {'tex' => '<',               'web' => '<'},
1.143     foxr      940:       '#140'          => {'tex' => 'AE',              'web' => "\x{152}"}, # AE ligature <FIX>
1.138     foxr      941:       '#145'          => {'tex' => '\140',            'web' => "\x{2018}"},
                    942:       '#146'          => {'tex' => '\47',             'web' => "\x{2019}"},
1.143     foxr      943:       '#147'          => {'tex' => '\140\140',        'web' => "\x{201c}"}, # Left " <FIX>
                    944:       '#148'          => {'tex' => '\47\47',          'web' => '\\"'},      # Right " <FIX>
1.138     foxr      945:       '#149'          => {'tex' => '{/Symbol \267}',  'web' => "\x{2022}"},
1.143     foxr      946:       '#150'          => {'tex' => '{/Text \55}',     'web' => "\x{2013}"},  # en dash
1.138     foxr      947:       '#151'          => {'tex' => '{/Symbol \55}',   'web' => "\x{2014}"},  # em dash
1.143     foxr      948:       '#152'          => {'tex' => '\\\\~',           'web' => '\\\\~'},
                    949:       '#153'          => {'tex' => '{/Symbol \324}',  'web' => "\x{2122}"}, # trademark
1.139     foxr      950: 
                    951:       # Accented letters, and other furreign language glyphs.
                    952: 
1.138     foxr      953:       '#154'          => {'tex' => 's',               'web' => "\x{161}"}, # small s-caron no ps.
                    954:       '#155'          => {'tex' => '>',               'web' => '\76'},     # >
1.143     foxr      955:       '#156'          => {'tex' => '{/Text \366}',    'web' => "\x{153}"}, # oe ligature.<FIX>
                    956:       '#159',         => {'tex' => 'Y',               'web' => "\x{178}"}, # Y-umlaut - can't print <FIX>
1.138     foxr      957:       '(nbsp|#160)'   => {'tex' => ' ',               'web' => ' '},       # non breaking space.
                    958:       '(iexcl|#161)'  => {'tex' => '{/Text \241}',    'web' => "\x{a1}"},  # inverted !
                    959:       '(cent|#162)'   => {'tex' => '{/Text \242}',    'web' => "\x{a2}"},  # Cent currency.
1.142     foxr      960:       '(pound|#163)'  => {'tex' => '{/Text \243}',    'web' => "\x{a3}"},  # GB Pound currency.
1.143     foxr      961:       '(curren|#164)' => {'tex' => '{/ZapfDingbats \161}','web' => "\x{a4}"},  # Generic currency symb. <FIX>
1.138     foxr      962:       '(yen|#165)'    => {'tex' => '{/Text \245}',    'web' => "\x{a5}"},  # Yen currency.
                    963:       '(brvbar|#166)' => {'tex' => '{/Symbol \174}',  'web' => "\x{a6}"},  # Broken vert bar no print.
                    964:       '(sect|#167)'   => {'tex' => '{\247}',          'web' => "\x{a7}"},  # Section symbol.
                    965:       '(uml|#168)'    => {'tex' => '{\250}',          'web' => "\x{a8}"},  # 'naked' umlaut.
                    966:       '(copy|#169)'   => {'tex' => '{/Symbol \343}',  'web' => "\x{a9}"},  # Copyright symbol.
                    967:       '(ordf|#170)'   => {'tex' => '{/Text \343}',    'web' => "\x{aa}"},  # Feminine ordinal.
                    968:       '(laquo|#171)'  => {'tex' => '{/Text \253}',    'web' => "\x{ab}"},  # << quotes.
                    969:       '(not|#172)'    => {'tex' => '\254',            'web' => "\x{ac}"},  # Logical not.
1.143     foxr      970:       '(shy|#173)'    => {'tex' => '\255',               'web' => "\x{ad}"},  # soft hyphen.
1.138     foxr      971:       '(reg|#174)'    => {'tex' => '{/Symbol \342}',  'web' => "\x{ae}"},  # Registered tm.
1.143     foxr      972:       '(macr|#175)'   => {'tex' => '^{\255}',            'web' => "\x{af}"},  # 'naked' macron (overbar).
                    973:       '(deg|#176)'    => {'tex' => '{/Text \260}',    'web' => "\x{b0}"},  # Degree symbo..`
1.138     foxr      974:       '(plusmn|#177)' => {'tex' => '{/Symbol \261}',  'web' => "\x{b1}"},  # +/- symbol.
                    975:       '(sup2|#178)'   => {'tex' => '^2',              'web' => "\x{b2}"},  # Superscript 2.
                    976:       '(sup3|#179)'   => {'tex' => '^3',              'web' => "\x{b3}"},  # Superscript 3.
1.143     foxr      977:       '(acute|#180)'  => {'tex' => '{/Text \222}',    'web' => "\x{b4}"},  # 'naked' acute accent.
1.138     foxr      978:       '(micro|#181)'  => {'tex' => '{/Symbol \155}',  'web' => "\x{b5}"},  # Micro (small mu).
                    979:       '(para|#182)'   => {'tex' => '{/Text \266}',    'web' => "\x{b6}"},  # Paragraph symbol.
1.143     foxr      980:       '(middot|#183)' => {'tex' => '\267',            'web' => "\x{b7}"},  # middle dot
1.138     foxr      981:       '(cedil|#184)'  => {'tex' => '\233',            'web' => "\x{b8}"},  # 'naked' cedilla.
                    982:       '(sup1|#185)'   => {'tex' => '^1',              'web' => "\x{b9}"},  # superscript 1.
1.143     foxr      983:       '(ordm|#186)'   => {'tex' => '{\260}',          'web' => "\x{ba}"},  # masculine ordinal.
1.138     foxr      984:       '(raquo|#187)', => {'tex' => '\273',            'web' => "\x{bb}"},  # Right angle quotes.
                    985:       '(frac14|#188)' => {'tex' => '\274',            'web' => "\x{bc}"},  # 1/4.
                    986:       '(frac12|#189)' => {'tex' => '\275',            'web' => "\x{bd}"},  # 1/2.
                    987:       '(frac34|#190)' => {'tex' => '\276',            'web' => "\x{be}"},  # 3/4
                    988:       '(iquest|#191)' => {'tex' => '{/Text \277}',    'web' => "\x{bf}"},  # Inverted ?
                    989:       '(Agrave|#192)' => {'tex' => '\300',            'web' => "\x{c0}"},  # A Grave.
                    990:       '(Aacute|#193)' => {'tex' => '\301',            'web' => "\x{c1}"},  # A Acute.
                    991:       '(Acirc|#194)'  => {'tex' => '\302',            'web' => "\x{c2}"},  # A Circumflex.
                    992:       '(Atilde|#195)' => {'tex' => '\303',            'web' => "\x{c3}"},  # A tilde.
                    993:       '(Auml|#196)'   => {'tex' => '\304',            'web' => "\x{c4}"},  # A umlaut.
                    994:       '(Aring|#197)'  => {'tex' => '\305',            'web' => "\x{c5}"},  # A ring.
1.139     foxr      995:       '(AElig|#198)'  => {'tex' => '\306',            'web' => "\x{c6}"},  # AE ligature.
                    996:       '(Ccedil|#199)' => {'tex' => '\307',            'web' => "\x{c7}"},  # C cedilla
                    997:       '(Egrave|#200)' => {'tex' => '\310',            'web' => "\x{c8}"},  # E Accent grave.
                    998:       '(Eacute|#201)' => {'tex' => '\311',            'web' => "\x{c9}"},  # E acute accent.
                    999:       '(Ecirc|#202)'  => {'tex' => '\312',            'web' => "\x{ca}"},  # E Circumflex.
                   1000:       '(Euml|#203)'   => {'tex' => '\313',            'web' => "\x{cb}"},  # E umlaut.
                   1001:       '(Igrave|#204)' => {'tex' => '\314',            'web' => "\x{cc}"},  # I grave accent.
                   1002:       '(Iacute|#205)' => {'tex' => '\315',            'web' => "\x{cd}"},  # I acute accent.
                   1003:       '(Icirc|#206)'  => {'tex' => '\316',            'web' => "\x{ce}"},  # I circumflex.
                   1004:       '(Iuml|#207)'   => {'tex' => '\317',            'web' => "\x{cf}"},  # I umlaut.
                   1005:       '(ETH|#208)'    => {'tex' => '\320',            'web' => "\x{d0}"},  # Icelandic Cap eth.
                   1006:       '(Ntilde|#209)' => {'tex' => '\321',            'web' => "\x{d1}"},  # Ntilde (enyan).
                   1007:       '(Ograve|#210)' => {'tex' => '\322',            'web' => "\x{d2}"},  # O accent grave.
                   1008:       '(Oacute|#211)' => {'tex' => '\323',            'web' => "\x{d3}"},  # O accent acute.
                   1009:       '(Ocirc|#212)'  => {'tex' => '\324',            'web' => "\x{d4}"},  # O circumflex.
                   1010:       '(Otilde|#213)' => {'tex' => '\325',            'web' => "\x{d5}"},  # O tilde.
                   1011:       '(Ouml|#214)'   => {'tex' => '\326',            'web' => "\x{d6}"},  # O umlaut.
                   1012:       '(times|#215)'  => {'tex' => '\327',            'web' => "\x{d7}"},  # Times symbol.
                   1013:       '(Oslash|#216)' => {'tex' => '\330',            'web' => "\x{d8}"},  # O slash.
                   1014:       '(Ugrave|#217)' => {'tex' => '\331',            'web' => "\x{d9}"},  # U accent grave.
                   1015:       '(Uacute|#218)' => {'tex' => '\332',            'web' => "\x{da}"},  # U accent acute.
                   1016:       '(Ucirc|#219)'  => {'tex' => '\333',            'web' => "\x{db}"},  # U circumflex.
                   1017:       '(Uuml|#220)'   => {'tex' => '\334',            'web' => "\x{dc}"},  # U umlaut.
                   1018:       '(Yacute|#221)' => {'tex' => '\335',            'web' => "\x{dd}"},  # Y accent acute.
                   1019:       '(THORN|#222)'  => {'tex' => '\336',            'web' => "\x{de}"},  # Icelandic thorn.
                   1020:       '(szlig|#223)'  => {'tex' => '\337',            'web' => "\x{df}"},  # German sharfes s.
                   1021:       '(agrave|#224)' => {'tex' => '\340',            'web' => "\x{e0}"},  # a accent grave.
                   1022:       '(aacute|#225)' => {'tex' => '\341',            'web' => "\x{e1}"},  # a grave.
                   1023:       '(acirc|#226)'  => {'tex' => '\342',            'web' => "\x{e2}"},  # a circumflex.
                   1024:       '(atilde|#227)' => {'tex' => '\343',            'web' => "\x{e3}"},  # a tilde.
                   1025:       '(auml|#228)'   => {'tex' => '\344',            'web' => "\x{e4}"},  # a umlaut
                   1026:       '(aring|#229)'  => {'tex' => '\345',            'web' => "\x{e5}"},  # a ring on top.
                   1027:       '(aelig|#230)'  => {'tex' => '\346',            'web' => "\x{e6}"},  # ae ligature.
1.142     foxr     1028:       '(ccedil|#231)' => {'tex' => '\347',            'web' => "\x{e7}"},  # C cedilla
1.139     foxr     1029:       '(egrave|#232)' => {'tex' => '\350',            'web' => "\x{e8}"},  # e accent grave.
                   1030:       '(eacute|#233)' => {'tex' => '\351',            'web' => "\x{e9}"},  # e accent acute.
                   1031:       '(ecirc|#234)'  => {'tex' => '\352',            'web' => "\x{ea}" }, # e circumflex.
                   1032:       '(euml|#235)'   => {'tex' => '\353',            'web' => "\x{eb}"},  # e umlaut.
                   1033:       '(igrave|#236)' => {'tex' => '\354',            'web' => "\x{ec}"},  # i grave.
1.142     foxr     1034:       '(iacute|#237)' => {'tex' => '\355',            'web' => "\x{ed}"},  # i acute.
                   1035:       '(icirc|#238)'  => {'tex' => '\356',            'web' => "\x{ee}"},  # i circumflex.
1.139     foxr     1036:       '(iuml|#239)'   => {'tex' => '\357',            'web' => "\x{ef}"},  # i umlaut.
                   1037:       '(eth|#240)'    => {'tex' => '\360',            'web' => "\x{f0}"},  # Icelandic eth.
                   1038:       '(ntilde|#241)' => {'tex' => '\361',            'web' => "\x{f1}"},  # n tilde.
                   1039:       '(ograve|#242)' => {'tex' => '\362',            'web' => "\x{f2}"},  # o grave.
                   1040:       '(oacute|#243)' => {'tex' => '\363',            'web' => "\x{f3}"},  # o acute.
1.143     foxr     1041:       '(ocirc|#244)'  => {'tex' => '\364',            'web' => "\x{f4}"},  # o circumflex.
1.139     foxr     1042:       '(otilde|#245)' => {'tex' => '\365',            'web' => "\x{f5}"},  # o tilde.
                   1043:       '(ouml|#246)'   => {'tex' => '\366',            'web' => "\x{f6}"},  # o umlaut.
                   1044:       '(divide|#247)' => {'tex' => '\367',            'web' => "\x{f7}"},  # division symbol
                   1045:       '(oslash|#248)' => {'tex' => '\370',            'web' => "\x{f8}"},  # o slashed.
                   1046:       '(ugrave|#249)' => {'tex' => '\371',            'web' => "\x{f9}"},  # u accent grave.
                   1047:       '(uacute|#250)' => {'tex' => '\372',            'web' => "\x{fa}"},  # u acute.
                   1048:       '(ucirc|#251)'  => {'tex' => '\373',            'web' => "\x{fb}"},  # u circumflex.
                   1049:       '(uuml|#252)'   => {'tex' => '\374',            'web' => "\x{fc}"},  # u umlaut.
                   1050:       '(yacute|#253)' => {'tex' => '\375',            'web' => "\x{fd}"},  # y acute accent.
                   1051:       '(thorn|#254)'  => {'tex' => '\376',            'web' => "\x{fe}"},  # small thorn (icelandic).
                   1052:       '(yuml|#255)'   => {'tex' => '\377',            'web' => "\x{ff}"},  # y umlaut.
                   1053:       
                   1054:       # Latin extended A entities:
                   1055: 
1.143     foxr     1056:       '(OElig|#338)'  => {'tex' => '{/Text \326}',   'web' => "\x{152}"},  # OE ligature.
                   1057:       '(oelig|#339)'  => {'tex' => '{/Text \366}',   'web' => "\x{153}"},  # oe ligature.
1.139     foxr     1058:       '(Scaron|#352)' => {'tex' => 'S',              'web' => "\x{160}"},  # S caron no printable.
                   1059:       '(scaron|#353)' => {'tex' => 's',              'web' => "\x{161}"},  # s caron no printable.
                   1060:       '(Yuml|#376)'   => {'tex' => 'Y',              'web' => "\x{178}"},  # Y umlaut - no printable.
                   1061: 
                   1062:       # Latin extended B.
                   1063: 
1.143     foxr     1064:       '(fnof|#402)'  => {'tex' =>'{/Symbol \246}',    'web' => "\x{192}"},  # f with little hook.
1.139     foxr     1065: 
1.140     foxr     1066:       # Standalone accents:
1.139     foxr     1067: 
                   1068:       '(circ|#710)'  => {'tex' => '^',               'web' => '^'},        # circumflex.
                   1069:       '(tilde|#732)' => {'tex' => '~',               'web' => '~'},        # tilde.
                   1070: 
1.140     foxr     1071:       # General punctuation.  We're not able to make a distinction between
                   1072:       # the various length spacings in the print version. (e.g. en/em/thin).
                   1073:       # the various joiners will be empty strings in the print version too.
                   1074: 
                   1075: 
                   1076:       '(ensp|#8194)'   => {'tex' => ' ',              'web' => "\x{2002}"}, # en space.
                   1077:       '(emsp|#8195)'   => {'tex' => ' ',              'web' => "\x{2003}"}, # em space.
                   1078:       '(thinsp|#8201)' => {'tex' => ' ',              'web' => "\x{2009}"}, # thin space.
1.144     foxr     1079:       '(zwnj|#8204)'   => {'tex' => ' ',               'web' => "\x{200c}"}, # Zero width non joiner.
                   1080:       '(zwj|#8205)'    => {'tex' => ' ',               'web' => "\x{200d}"}, # Zero width joiner.
                   1081:       '(lrm|#8206)'    => {'tex' => ' ',               'web' => "\x{200e}"}, # Left to right mark
                   1082:       '(rlm|#8207)'    => {'tex' => ' ',               'web' => "\x{200f}"}, # right to left mark.
1.140     foxr     1083:       '(ndash|#8211)'  => {'tex' => '{/Text \55}',    'web' => "\x{2013}"}, # en dash.
                   1084:       '(mdash|#8212)'  => {'tex' => '{/Symbol \55}',  'web' => "\x{2014}"}, # em dash.
                   1085:       '(lsquo|#8216)'  => {'tex' => '{/Text \140}',   'web' => "\x{2018}"}, # Left single quote.
1.144     foxr     1086:       '(rsquo|#8217)'  => {'tex' => '\47',            'web' => "\x{2019}"}, # Right single quote.
                   1087:       '(sbquo|#8218)'  => {'tex' => '\54',             'web' => "\x{201a}"}, # Single low-9 quote.
                   1088:       '(ldquo|#8220)'  => {'tex' => '\42',   'web' => "\x{201c}"}, # Left double quote.
                   1089:       '(rdquo|#8221)'  => {'tex' => '\42',   'web' => "\x{201d}"}, # Right double quote.
                   1090:       '(bdquo|#8222)'  => {'tex' => ',',              'web' => "\x{201e}"}, # Double low-9 quote.
                   1091:       '(dagger|#8224)' => {'tex' => '+',   'web' => "\x{2020}"}, # Is this a dagger I see before me now?
1.145     foxr     1092:       '(Dagger|#8225)' => {'tex' => '\261',   'web' => "\x{2021}"}, # it's handle pointing towards my heart?
1.140     foxr     1093:       '(bull|#8226)'   => {'tex' => '\267',           'web' => "\x{2022}"}, # Bullet.
1.144     foxr     1094:       '(hellep|#8230)' => {'tex' => '{/Symbol \274}',   'web' => "\x{2026}"}, # Ellipses.
                   1095:       '(permil|#8240)' => {'tex' => '%_o',            'web' => "\x{2031}"}, # Per mille.
1.140     foxr     1096:       '(prime|#8242)'  => {'tex' => '\264',           'web' => "\x{2032}"}, # Prime.
                   1097:       '(Prime|#8243)'  => {'tex' => '{/Symbol \262}', 'web' => "\x{2033}"}, # double prime.
1.144     foxr     1098:       '(lsaquo|#8249)' => {'tex' => '<',              'web' => "\x{2039}"}, # < quote.
                   1099:       '(rsaquo|#8250)' => {'tex' => '\74',              'web' => "\x{203a}"}, # > quote.
1.140     foxr     1100:       '(oline|#8254)'  => {'tex' => '{/Symbol \140}', 'web' => "\x{203e}"}, # Overline.
                   1101:       '(frasl|#8260)'  => {'tex' => '/',              'web' => "\x{2044}"}, # Fraction slash.
1.144     foxr     1102:       '(euro|#8364)'   => {'tex' => '{/Symbol \240}', 'web' => "\x{20ac}"}, # Euro currency.
1.140     foxr     1103:       
                   1104:       # Letter like symbols.
                   1105: 
                   1106:       '(weierp|#8472)'  => {'tex' => '{/Symbol \303}', 'web' => "\x{2118}"}, # Power set symbol
                   1107:       '(image|#8465)'   => {'tex' => '{/Symbol \301}', 'web' => "\x{2111}"}, # Imaginary part
                   1108:       '(real|#8476)'    => {'tex' => '{/Symbol \302}', 'web' => "\x{211c}"}, # Real part.
                   1109:       '(trade|#8482)'   => {'tex' => '{/Symbol \344}', 'web' => "\x{2122}"}, # trademark symbol.
                   1110:       '(alefsym|#8501)' => {'tex' => '{/Symbol \300}', 'web' => "\x{2135}"}, # Hebrew alef.
                   1111: 
                   1112:       # Arrows  of various types and directions.
                   1113:       '(larr|#8592)'    => {'tex' => '{/Symbol \254}', 'web' => "\x{2190}"}, # <--
1.142     foxr     1114:       '(uarr|#8593)'    => {'tex' => '{/Symbol \255}', 'web' => "\x{2191}"}, # up arrow.
1.140     foxr     1115:       '(rarr|#8594)'    => {'tex' => '{/Symbol \256}', 'web' => "\x{2192}"}, # -->
                   1116:       '(darr|#8595)'    => {'tex' => '{/Symbol \257}', 'web' => "\x{2193}"}, # down arrow.
                   1117:       '(harr|#8596)'    => {'tex' => '{/Symbol \253}', 'web' => "\x{2194}"}, # <-->
                   1118:       '(crarr|#8629)'   => {'tex' => '{/Symbol \277}', 'web' => "\x{21b5}"}, # corner arrow down and right.
                   1119:       '(lArr|#8656)'    => {'tex' => '{/Symbol \334}', 'web' => "\x{21d0}"}, # <==
                   1120:       '(uArr|#8657)'    => {'tex' => '{/Symbol \335}', 'web' => "\x{21d1}"}, # Up double arrow.
                   1121:       '(rArr|#8658)'    => {'tex' => '{/Symbol \336}', 'web' => "\x{21d2}"}, # ==>
                   1122:       '(dArr|#8659)'    => {'tex' => '{/Symbol \337}', 'web' => "\x{21d3}"}, # Down double arrow.
                   1123:       '(hArr|#8660)'    => {'tex' => '{/Symbol \333}', 'web' => "\x{21d4}"}, # <==>
                   1124: 
                   1125:       # Mathematical operators. For some of these we do the best we can in printing.
                   1126: 
                   1127:       '(forall|#8704)'  => {'tex' => '{/Symbol \42}',   'web' => "\x{2200}"}, # For all.
                   1128:       '(part|#8706)'    => {'tex' => '{/Symbol d}',     'web' => "\x{2202}"}, # partial derivative
                   1129:       '(exist|#8707)'   => {'tex' => '{/Symbol \44}',   'web' => "\x{2203}"}, # There exists.
                   1130:       '(empty|#8709)'   => {'tex' => '{/Symbol \306}',  'web' => "\x{2205}"}, # Null set.
                   1131:       '(nabla|#8711)'   => {'tex' => '{/Symbol \321}',  'web' => "\x{2207}"}, # Gradient e.g.
                   1132:       '(isin|#8712)'    => {'tex' => '{/Symbol \316}',  'web' => "\x{2208}"}, # Element of the set.
1.145     foxr     1133:       '(notin|#8713)'   => {'tex' => '{/Symbol \317}',  'web' => "\x{2209}"}, # Not an element of
1.140     foxr     1134:       '(ni|#8715)'      => {'tex' => '{/Symbol \47}',   'web' => "\x{220b}"}, # Contains as a member
                   1135:       '(prod|#8719)'    => {'tex' => '{/Symbol \325}',  'web' => "\x{220f}"}, # Product 
                   1136:       '(sum|#8721)'     => {'tex' => '{/Symbol \345}',  'web' => "\x{2211}"}, # Sum of.
1.145     foxr     1137:       '(minus|#8722)'   => {'tex' => '{/Symbol \55}',   'web' => "\x{2212}"}, # - sign.
1.140     foxr     1138:       '(lowast|#8727)'  => {'tex' => '*',               'web' => "\x{2217}"}, # * 
                   1139:       '(radic|#8730)'   => {'tex' => '{/Symbol \326}',  'web' => "\x{221a}"}, # Square root. 
                   1140:       '(prop|#8733)'    => {'tex' => '{/Symbol \265}',  'web' => "\x{221d}"}, # Proportional to.
                   1141:       '(infin|#8734)'   => {'tex' => '{/Symbol \245}',  'web' => "\x{221e}"}, # Infinity.
                   1142:       '(ang|#8736)'     => {'tex' => '{/Symbol \320}',  'web' => "\x{2220}"}, # Angle .
                   1143:       '(and|#8743)'     => {'tex' => '{/Symbol \331}',  'web' => "\x{2227}"}, # Logical and.
                   1144:       '(or|#8744)'      => {'tex' => '{/Symbol \332}',  'web' => "\x{2228}"}, # Logical or.
                   1145:       '(cap|#8745)'     => {'tex' => '{/Symbol \307}',  'web' => "\x{2229}"}, # Set intersection.
                   1146:       '(cup|#8746)'     => {'tex' => '{/Symbol \310}',  'web' => "\x{222a}"}, # Set union.
                   1147:       '(int|8747)'      => {'tex' => '{/Symbol \362}',  'web' => "\x{222b}"}, # Integral.
1.145     foxr     1148: 
                   1149:       # Some gnuplot guru will have to explain to me why the next three
                   1150:       # require the extra slashes... else they print very funkily.
                   1151: 
                   1152:       '(there4|#8756)'  => {'tex' => '{/Symbol \\\134}',  'web' => "\x{2234}"}, # Therefore triple dots.
                   1153:       '(sim|#8764)'     => {'tex' => '\\\176',               'web' => "\x{223c}"}, # Simlar to.
                   1154:       '(cong|#8773)'    => {'tex' => '{/Symbol \\\100}','web' => "\x{2245}"}, # Congruent to/with.
                   1155: 
                   1156:       '(asymp|#8776)'   => {'tex' => '{/Symbol \273}',  'web' => "\x{2248}"}, # Asymptotic to.
1.141     foxr     1157:       '(ne|#8800)'      => {'tex' => '{/Symbol \271}',  'web' => "\x{2260}"}, # not equal to.
                   1158:       '(equiv|#8801)'   => {'tex' => '{/Symbol \272}',  'web' => "\x{2261}"}, # Equivalent to.
                   1159:       '(le|8804)'       => {'tex' => '{/Symbol \243}',  'web' => "\x{2264}"}, # Less than or equal to.
                   1160:       '(ge|8805)'       => {'tex' => '{/Symbol \263}',  'web' => "\x{2265}"}, # Greater than or equal to
                   1161:       '(sub|8834)'      => {'tex' => '{/Symbol \314}',  'web' => "\x{2282}"}, # Subset of.
                   1162:       '(sup|8835)'      => {'tex' => '{/Symbol \311}',  'web' => "\x{2283}"}, # Super set of.
                   1163:       '(nsub|8836)'     => {'tex' => '{/Symbol \313}',  'web' => "\x{2284}"}, # not subset of.
                   1164:       '(sube|8838)'     => {'tex' => '{/Symbol \315}',  'web' => "\x{2286}"}, # Subset or equal.
                   1165:       '(supe|8839)'     => {'tex' => '{/Symbol \312}',  'web' => "\x{2287}"}, # Superset or equal
                   1166:       '(oplus|8853)'    => {'tex' => '{/Symbol \305}',  'web' => "\x{2295}"}, # O with plus inside
                   1167:       '(otimes|8855)'   => {'tex' => '{/Symbol \304}',  'web' => "\x{2297}"}, # O with times.
                   1168:       '(perp|8869)'     => {'tex' => '{/Symbol \136}',  'web' => "\x{22a5}"}, # Perpendicular.
                   1169:       '(sdot|8901)'     => {'tex' => '{/Symbol \227}',  'web' => "\x{22c5}"}, # Dot operator.
                   1170: 
                   1171:       # Misc. technical symbols:
                   1172: 
                   1173:       '(lceil|8698)'    => {'tex' => '{/Symbol \351}',  'web' => "\x{2308}"}, # Left ceiling.
                   1174:       '(rceil|8969)'    => {'tex' => '{/Symbol \371}',  'web' => "\x{2309}"}, # Right ceiling.
                   1175:       '(lfloor|8970)'   => {'tex' => '{/Symbol \353}',  'web' => "\x{230a}"}, # Left floor.
                   1176:       '(rfloor|8971)'   => {'tex' => '{/Symbol \373}',  'web' => "\x{230b}"}, # Right floor.
1.145     foxr     1177: 
                   1178:       # The gnuplot png font evidently does not have the big angle brackets at
                   1179:       # positions 0x2329, 0x232a so use ordinary brackets.
                   1180: 
                   1181:       '(lang|9001)'     => {'tex' => '{/Symbol \341}',  'web' => '<'}, # Left angle bracket.
                   1182:       '(rang|9002)'     => {'tex' => '{/Symbol \361}',  'web' => '>'}, # Right angle bracket.
1.141     foxr     1183: 
                   1184:       # Gemoetric shapes.
                   1185: 
                   1186:       '(loz|9674)'      => {'tex' => '{/Symbol \340}',  'web' => "\x{25ca}"}, # Lozenge.
                   1187: 
                   1188:       # Misc. symbols
                   1189: 
                   1190:       '(spades|9824)'   => {'tex' => '{/Symbol \252}', 'web' => "\x{2660}"}, 
                   1191:       '(clubs|9827)'    => {'tex' => '{/Symbol \247}', 'web' => "\x{2663}"}, 
                   1192:       '(hearts|9829)'   => {'tex' => '{/Symbol \251}', 'web' => "\x{2665}"}, 
                   1193:       '(diams|9830)'    => {'tex' => '{/Symbol \250}', 'web' => "\x{2666}"}
1.139     foxr     1194: 
1.133     albertel 1195:     );
                   1196: 
1.128     albertel 1197: 
                   1198: sub replace_entities {
                   1199:     my ($target,$text) = @_;
                   1200:     $text =~ s{([_^~\{\}]|\\\\)}{\\\\$1}g;
                   1201:     while (my ($re, $replace) = each(%lookup)) {
1.137     foxr     1202: 	my $repl = $replace->{$target};
1.128     albertel 1203: 	$text =~ s/&$re;/$replace->{$target}/g;
                   1204:     }
                   1205:     $text =~ s{(&)}{\\\\$1}g;
                   1206:     return $text;
1.127     albertel 1207: }
                   1208: 
1.1       matthew  1209: ##------------------------------------------------------------------- title
                   1210: sub start_title {
                   1211:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1212:     my $result='';
1.51      matthew  1213:     if ($target eq 'web' || $target eq 'tex') {
1.112     albertel 1214: 	$title = &Apache::lonxml::get_all_text("/title",$parser,$style);
1.58      matthew  1215: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
1.49      matthew  1216: 	$title =~ s/\n/ /g;
1.32      matthew  1217: 	if (length($title) > $max_str_len) {
                   1218: 	    $title = substr($title,0,$max_str_len);
                   1219: 	}
1.128     albertel 1220: 	$title = &parse_label($target,$title);
1.20      matthew  1221:     } elsif ($target eq 'edit') {
1.25      matthew  1222: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.112     albertel 1223: 	my $text=&Apache::lonxml::get_all_text("/title",$parser,$style);
1.116     albertel 1224: 	$result.=&Apache::edit::editline('',$text,'',60);
1.20      matthew  1225:     } elsif ($target eq 'modified') {
1.42      matthew  1226: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1227: 	$result.=&Apache::edit::modifiedfield("/title",$parser);
1.4       matthew  1228:     }
1.1       matthew  1229:     return $result;
                   1230: }
                   1231: 
                   1232: sub end_title {
                   1233:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1234:     my $result = '';
1.51      matthew  1235:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1236:     } elsif ($target eq 'edit') {
1.27      matthew  1237: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew  1238:     }
1.1       matthew  1239:     return $result;
                   1240: }
                   1241: ##------------------------------------------------------------------- xlabel
                   1242: sub start_xlabel {
                   1243:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1244:     my $result='';
1.51      matthew  1245:     if ($target eq 'web' || $target eq 'tex') {
1.112     albertel 1246: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1.58      matthew  1247: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
1.49      matthew  1248: 	$xlabel =~ s/\n/ /g;
1.32      matthew  1249: 	if (length($xlabel) > $max_str_len) {
                   1250: 	    $xlabel = substr($xlabel,0,$max_str_len);
                   1251: 	}
1.128     albertel 1252: 	$xlabel = &parse_label($target,$xlabel);
1.20      matthew  1253:     } elsif ($target eq 'edit') {
1.25      matthew  1254: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.112     albertel 1255: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1.116     albertel 1256: 	$result.=&Apache::edit::editline('',$text,'',60);
1.20      matthew  1257:     } elsif ($target eq 'modified') {
1.42      matthew  1258: 	$result.=&Apache::edit::rebuild_tag($token);	
1.93      albertel 1259: 	$result.=&Apache::edit::modifiedfield("/xlabel",$parser);
1.4       matthew  1260:     }
1.1       matthew  1261:     return $result;
                   1262: }
                   1263: 
                   1264: sub end_xlabel {
                   1265:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1266:     my $result = '';
1.51      matthew  1267:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1268:     } elsif ($target eq 'edit') {
1.27      matthew  1269: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew  1270:     }
1.1       matthew  1271:     return $result;
                   1272: }
1.21      matthew  1273: 
1.1       matthew  1274: ##------------------------------------------------------------------- ylabel
                   1275: sub start_ylabel {
                   1276:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1277:     my $result='';
1.51      matthew  1278:     if ($target eq 'web' || $target eq 'tex') {
1.112     albertel 1279: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1.58      matthew  1280: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
1.49      matthew  1281: 	$ylabel =~ s/\n/ /g;
1.32      matthew  1282: 	if (length($ylabel) > $max_str_len) {
                   1283: 	    $ylabel = substr($ylabel,0,$max_str_len);
                   1284: 	}
1.128     albertel 1285: 	$ylabel = &parse_label($target,$ylabel);
1.20      matthew  1286:     } elsif ($target eq 'edit') {
1.25      matthew  1287: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.112     albertel 1288: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1.116     albertel 1289: 	$result .= &Apache::edit::editline('',$text,'',60);
1.20      matthew  1290:     } elsif ($target eq 'modified') {
1.42      matthew  1291: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1292: 	$result.=&Apache::edit::modifiedfield("/ylabel",$parser);
1.4       matthew  1293:     }
1.1       matthew  1294:     return $result;
                   1295: }
                   1296: 
                   1297: sub end_ylabel {
                   1298:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1299:     my $result = '';
1.51      matthew  1300:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1301:     } elsif ($target eq 'edit') {
1.27      matthew  1302: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew  1303:     }
1.1       matthew  1304:     return $result;
                   1305: }
1.21      matthew  1306: 
1.1       matthew  1307: ##------------------------------------------------------------------- label
                   1308: sub start_label {
                   1309:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1310:     my $result='';
1.51      matthew  1311:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1312: 	my %label;
                   1313: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew  1314: 		    $tagstack->[-1]);
1.112     albertel 1315: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1.58      matthew  1316: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
1.49      matthew  1317: 	$text =~ s/\n/ /g;
1.32      matthew  1318: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
1.128     albertel 1319: 	$label{'text'} = &parse_label($target,$text);
1.17      matthew  1320: 	push(@labels,\%label);
1.20      matthew  1321:     } elsif ($target eq 'edit') {
1.25      matthew  1322: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21      matthew  1323: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.112     albertel 1324: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1.39      matthew  1325: 	$result .= &Apache::edit::end_row().
                   1326: 	    &Apache::edit::start_spanning_row().
1.63      albertel 1327: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew  1328:     } elsif ($target eq 'modified') {
1.42      matthew  1329: 	&Apache::edit::get_new_args
1.24      matthew  1330: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.42      matthew  1331: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1332: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.4       matthew  1333:     }
1.1       matthew  1334:     return $result;
                   1335: }
                   1336: 
                   1337: sub end_label {
                   1338:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1339:     my $result = '';
1.51      matthew  1340:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1341:     } elsif ($target eq 'edit') {
1.21      matthew  1342: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew  1343:     }
1.1       matthew  1344:     return $result;
                   1345: }
                   1346: 
                   1347: ##------------------------------------------------------------------- curve
                   1348: sub start_curve {
                   1349:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1350:     my $result='';
1.25      matthew  1351:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
                   1352:     push (@Apache::lonxml::namespace,'curve');
1.51      matthew  1353:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1354: 	my %curve;
                   1355: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew  1356: 		    $tagstack->[-1]);
1.17      matthew  1357: 	push (@curves,\%curve);
1.20      matthew  1358:     } elsif ($target eq 'edit') {
1.26      matthew  1359: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
1.60      matthew  1360: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
1.116     albertel 1361:                                     \@curve_edit_order)
                   1362: 	    .&Apache::edit::end_row()
                   1363: 	    .&Apache::edit::start_spanning_row();
                   1364: 
1.20      matthew  1365:     } elsif ($target eq 'modified') {
                   1366: 	my $constructtag=&Apache::edit::get_new_args
1.35      matthew  1367: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
1.20      matthew  1368: 	if ($constructtag) {
1.155     foxr     1369: 	    #
                   1370: 	    # Fix up the color attribute as jcolor does not prepend an x
                   1371: 	    #
                   1372: 	    my $value = $token->[2]{'color'};
                   1373: 	    if (defined $value && !($value =~ /^x/)) {
                   1374: 		$token->[2]{'color'} = 'x' . $value;
                   1375: 	    }
1.20      matthew  1376: 	    $result = &Apache::edit::rebuild_tag($token);
                   1377: 	}
1.4       matthew  1378:     }
1.1       matthew  1379:     return $result;
                   1380: }
                   1381: 
                   1382: sub end_curve {
                   1383:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1384:     my $result = '';
1.25      matthew  1385:     pop @Apache::lonxml::namespace;
                   1386:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.51      matthew  1387:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1388:     } elsif ($target eq 'edit') {
1.21      matthew  1389: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew  1390:     }
1.1       matthew  1391:     return $result;
                   1392: }
1.21      matthew  1393: 
1.1       matthew  1394: ##------------------------------------------------------------ curve function
                   1395: sub start_function {
                   1396:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1397:     my $result='';
1.51      matthew  1398:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1399: 	if (exists($curves[-1]->{'data'})) {
1.85      matthew  1400: 	    &Apache::lonxml::warning
                   1401:                 ('Use of the <b>curve function</b> tag precludes use of '.
                   1402:                  ' the <b>curve data</b> tag.  '.
                   1403:                  'The curve data tag will be omitted in favor of the '.
                   1404:                  'curve function declaration.');
1.17      matthew  1405: 	    delete $curves[-1]->{'data'} ;
                   1406: 	}
1.112     albertel 1407:         my $function = &Apache::lonxml::get_all_text("/function",$parser,
                   1408: 						     $style);
1.58      matthew  1409: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
1.147     www      1410:         $function=~s/\^/\*\*/gs;
1.58      matthew  1411: 	$curves[-1]->{'function'} = $function; 
1.20      matthew  1412:     } elsif ($target eq 'edit') {
1.37      matthew  1413: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1.112     albertel 1414: 	my $text = &Apache::lonxml::get_all_text("/function",$parser,$style);
1.116     albertel 1415: 	$result .= &Apache::edit::editline('',$text,'',60);
1.20      matthew  1416:     } elsif ($target eq 'modified') {
1.42      matthew  1417: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1418: 	$result.=&Apache::edit::modifiedfield("/function",$parser);
1.4       matthew  1419:     }
1.1       matthew  1420:     return $result;
                   1421: }
                   1422: 
                   1423: sub end_function {
                   1424:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1425:     my $result = '';
1.51      matthew  1426:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1427:     } elsif ($target eq 'edit') {
1.26      matthew  1428: 	$result .= &Apache::edit::end_table();
1.4       matthew  1429:     }
1.1       matthew  1430:     return $result;
                   1431: }
1.21      matthew  1432: 
1.1       matthew  1433: ##------------------------------------------------------------ curve  data
                   1434: sub start_data {
                   1435:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1436:     my $result='';
1.51      matthew  1437:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1438: 	if (exists($curves[-1]->{'function'})) {
1.85      matthew  1439: 	    &Apache::lonxml::warning
                   1440:                 ('Use of the <b>curve function</b> tag precludes use of '.
                   1441:                  ' the <b>curve data</b> tag.  '.
                   1442:                  'The curve function tag will be omitted in favor of the '.
                   1443:                  'curve data declaration.');
1.17      matthew  1444: 	    delete($curves[-1]->{'function'});
                   1445: 	}
1.112     albertel 1446: 	my $datatext = &Apache::lonxml::get_all_text("/data",$parser,$style);
1.58      matthew  1447: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
1.40      matthew  1448: 	# Deal with cases where we're given an array...
                   1449: 	if ($datatext =~ /^\@/) {
                   1450: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
                   1451: 					  $safeeval,1);
                   1452: 	}
1.49      matthew  1453: 	$datatext =~ s/\s+/ /g;
1.17      matthew  1454: 	# Need to do some error checking on the @data array - 
                   1455: 	# make sure it's all numbers and make sure each array 
                   1456: 	# is of the same length.
                   1457: 	my @data;
1.35      matthew  1458: 	if ($datatext =~ /,/) { # comma deliminated
1.17      matthew  1459: 	    @data = split /,/,$datatext;
1.95      www      1460: 	} else { # Assume it's space separated.
1.17      matthew  1461: 	    @data = split / /,$datatext;
                   1462: 	}
                   1463: 	for (my $i=0;$i<=$#data;$i++) {
                   1464: 	    # Check that it's non-empty
1.19      matthew  1465: 	    if (! defined($data[$i])) {
                   1466: 		&Apache::lonxml::warning(
1.85      matthew  1467: 		    'undefined curve data value.  Replacing with '.
1.19      matthew  1468: 		    ' pi/e = 1.15572734979092');
                   1469: 		$data[$i] = 1.15572734979092;
                   1470: 	    }
1.17      matthew  1471: 	    # Check that it's a number
1.19      matthew  1472: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                   1473: 		&Apache::lonxml::warning(
1.85      matthew  1474: 		    'Bad curve data value of '.$data[$i].'  Replacing with '.
1.19      matthew  1475: 		    ' pi/e = 1.15572734979092');
                   1476: 		$data[$i] = 1.15572734979092;
                   1477: 	    }
1.17      matthew  1478: 	}
1.35      matthew  1479: 	# complain if the number of data points is not the same as
                   1480: 	# in previous sets of data.
1.150     raeburn  1481: 	if (($curves[-1]->{'data'}) && ($#data != $#{$curves[-1]->{'data'}->[0]})){
1.35      matthew  1482: 	    &Apache::lonxml::warning
                   1483: 		('Number of data points is not consistent with previous '.
                   1484: 		 'number of data points');
                   1485: 	}
1.17      matthew  1486: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew  1487:     } elsif ($target eq 'edit') {
1.37      matthew  1488: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1.112     albertel 1489: 	my $text = &Apache::lonxml::get_all_text("/data",$parser,$style);
1.116     albertel 1490: 	$result .= &Apache::edit::editline('',$text,'',60);
1.20      matthew  1491:     } elsif ($target eq 'modified') {
1.42      matthew  1492: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1493: 	$result.=&Apache::edit::modifiedfield("/data",$parser);
1.4       matthew  1494:     }
1.1       matthew  1495:     return $result;
                   1496: }
                   1497: 
                   1498: sub end_data {
                   1499:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1500:     my $result = '';
1.51      matthew  1501:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1502:     } elsif ($target eq 'edit') {
1.26      matthew  1503: 	$result .= &Apache::edit::end_table();
1.4       matthew  1504:     }
1.1       matthew  1505:     return $result;
                   1506: }
                   1507: 
                   1508: ##------------------------------------------------------------------- axis
                   1509: sub start_axis {
                   1510:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1511:     my $result='';
1.51      matthew  1512:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1513: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                   1514: 			$tagstack->[-1]);
1.20      matthew  1515:     } elsif ($target eq 'edit') {
1.25      matthew  1516: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.65      matthew  1517: 	$result .= &edit_attributes($target,$token,\%axis_defaults,
                   1518: 				    \@axis_edit_order);
1.20      matthew  1519:     } elsif ($target eq 'modified') {
1.29      matthew  1520: 	my $constructtag=&Apache::edit::get_new_args
                   1521: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
1.155     foxr     1522: 
1.29      matthew  1523: 	if ($constructtag) {
1.155     foxr     1524: 	    #
                   1525: 	    #  Fix up the color attribute since jchooser does not
                   1526: 	    #  prepend an x to the color:
                   1527: 	    #
                   1528: 	    my $value = $token->[2]{'color'};
                   1529: 	    if (defined $value && !($value =~ /^x/)) {
                   1530: 		$token->[2]{'color'} = 'x' . $value;
                   1531: 	    }
                   1532: 
1.29      matthew  1533: 	    $result = &Apache::edit::rebuild_tag($token);
                   1534: 	}
1.4       matthew  1535:     }
1.1       matthew  1536:     return $result;
                   1537: }
                   1538: 
                   1539: sub end_axis {
                   1540:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1541:     my $result = '';
1.51      matthew  1542:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1543:     } elsif ($target eq 'edit') {
1.21      matthew  1544: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew  1545:     } elsif ($target eq 'modified') {
1.4       matthew  1546:     }
1.1       matthew  1547:     return $result;
                   1548: }
                   1549: 
1.21      matthew  1550: ###################################################################
                   1551: ##                                                               ##
                   1552: ##        Utility Functions                                      ##
                   1553: ##                                                               ##
                   1554: ###################################################################
                   1555: 
1.13      matthew  1556: ##----------------------------------------------------------- set_defaults
                   1557: sub set_defaults {
1.21      matthew  1558:     my ($var,$defaults) = @_;
1.13      matthew  1559:     my $key;
1.24      matthew  1560:     foreach $key (keys(%$defaults)) {
1.13      matthew  1561: 	$var->{$key} = $defaults->{$key}->{'default'};
                   1562:     }
                   1563: }
                   1564: 
1.1       matthew  1565: ##------------------------------------------------------------------- misc
1.2       matthew  1566: sub get_attributes{
1.21      matthew  1567:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24      matthew  1568:     foreach my $attr (keys(%{$defaults})) {
1.92      matthew  1569: 	if ($attr eq 'texwidth' || $attr eq 'texfont') {
1.86      albertel 1570: 	    $values->{$attr} = 
                   1571: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
                   1572: 	} else {
                   1573: 	    $values->{$attr} = 
                   1574: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval);
                   1575: 	}
1.10      matthew  1576: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew  1577: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew  1578: 	    next;
                   1579: 	}
1.10      matthew  1580: 	my $test = $defaults->{$attr}->{'test'};
                   1581: 	if (! &$test($values->{$attr})) {
1.6       matthew  1582: 	    &Apache::lonxml::warning
                   1583: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew  1584: 		 .$defaults->{$attr}->{'default'} );
                   1585: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew  1586: 	}
1.2       matthew  1587:     }
1.11      matthew  1588:     return ;
1.6       matthew  1589: }
1.40      matthew  1590: 
1.15      matthew  1591: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew  1592: sub write_gnuplot_file {
1.51      matthew  1593:     my ($tmpdir,$filename,$target)= @_;
1.124     albertel 1594:     my ($fontsize, $font_properties) =  &get_font($target);
1.6       matthew  1595:     my $gnuplot_input = '';
1.10      matthew  1596:     my $curve;
1.100     matthew  1597:     #
                   1598:     # Check to be sure we do not have any empty curves
                   1599:     my @curvescopy;
                   1600:     foreach my $curve (@curves) {
                   1601:         if (exists($curve->{'function'})) {
                   1602:             if ($curve->{'function'} !~ /^\s*$/) {
                   1603:                 push(@curvescopy,$curve);
                   1604:             }
                   1605:         } elsif (exists($curve->{'data'})) {
                   1606:             foreach my $data (@{$curve->{'data'}}) {
                   1607:                 if (scalar(@$data) > 0) {
                   1608:                     push(@curvescopy,$curve);
                   1609:                     last;
                   1610:                 }
                   1611:             }
                   1612:         }
                   1613:     }
                   1614:     @curves = @curvescopy;
1.6       matthew  1615:     # Collect all the colors
                   1616:     my @Colors;
1.107     foxr     1617:     push @Colors, $Apache::lonplot::plot{'bgcolor'};
                   1618:     push @Colors, $Apache::lonplot::plot{'fgcolor'}; 
                   1619:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
1.9       matthew  1620:     foreach $curve (@curves) {
                   1621: 	push @Colors, ($curve->{'color'} ne '' ? 
                   1622: 		       $curve->{'color'}       : 
1.107     foxr     1623: 		       $Apache::lonplot::plot{'fgcolor'}        );
1.6       matthew  1624:     }
1.155     foxr     1625:     
1.6       matthew  1626:     # set term
1.51      matthew  1627:     if ($target eq 'web') {
1.120     albertel 1628: 	$gnuplot_input .= 'set terminal png enhanced nocrop ';
1.107     foxr     1629: 	$gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
1.120     albertel 1630: 	$gnuplot_input .= 'font "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
                   1631: 	    '/'.$font_properties->{'file'}.'.ttf" ';
                   1632: 	$gnuplot_input .= $fontsize;
                   1633: 	$gnuplot_input .= ' size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
1.51      matthew  1634: 	$gnuplot_input .= "@Colors\n";
                   1635: 	# set output
                   1636: 	$gnuplot_input .= "set output\n";
                   1637:     } elsif ($target eq 'tex') {
1.156   ! foxr     1638: 	$gnuplot_input .= "set term postscript eps enhanced $Apache::lonplot::plot{'plotcolor'} dash ";
1.121     albertel 1639: 	if (!$font_properties->{'tex_no_file'}) {
                   1640: 	    $gnuplot_input .=
                   1641: 		'fontfile "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
                   1642: 		'/'.$font_properties->{'file'}.'.pfb" ';
                   1643: 	}
1.120     albertel 1644: 	$gnuplot_input .= ' "'.$font_properties->{'printname'}.'" ';
                   1645: 	$gnuplot_input .= $fontsize;
1.153     foxr     1646: 	$gnuplot_input .= "\nset output \"".$tmpdir.
1.113     www      1647: 	    &unescape($filename).".eps\"\n";
1.143     foxr     1648: 	$gnuplot_input .= "set encoding iso_8859_1\n"; # Get access to extended font.
                   1649: 
1.87      matthew  1650:     }
1.115     albertel 1651:     # cartesian or polar plot?
1.107     foxr     1652:     if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
1.87      matthew  1653:         $gnuplot_input .= 'set polar'.$/;
                   1654:     } else {
                   1655:         # Assume Cartesian
1.51      matthew  1656:     }
1.115     albertel 1657:     # cartesian or polar grid?
                   1658:     if (lc($Apache::lonplot::plot{'gridtype'}) eq 'polar') {
                   1659:         $gnuplot_input .= 'set grid polar'.$/;
1.118     albertel 1660:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'linear-log') {
                   1661:         $gnuplot_input .= 'set logscale x'.$/;
                   1662:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-linear') {
                   1663:         $gnuplot_input .= 'set logscale y'.$/;
                   1664:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-log') {
                   1665:         $gnuplot_input .= 'set logscale x'.$/;
                   1666:         $gnuplot_input .= 'set logscale y'.$/;
1.115     albertel 1667:     } else {
                   1668:         # Assume Cartesian
                   1669:     }
1.110     albertel 1670:     # solid or pattern for boxes?
                   1671:     if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
                   1672:         $gnuplot_input .= 'set style fill solid '.
                   1673: 	    $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
                   1674:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
                   1675:         $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
                   1676:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
                   1677:     }
1.101     matthew  1678:     # margin
1.107     foxr     1679:     if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
                   1680:         $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
1.101     matthew  1681:     }
1.107     foxr     1682:     if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
                   1683:         $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
1.101     matthew  1684:     }
1.107     foxr     1685:     if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
                   1686:         $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
1.101     matthew  1687:     }
1.107     foxr     1688:     if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
                   1689:         $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
1.101     matthew  1690:     }
1.129     albertel 1691: 
1.101     matthew  1692:     # tic scales
1.129     albertel 1693:     if ($version > 4) {
                   1694: 	$gnuplot_input .= 'set tics scale '.
                   1695: 	    $Apache::lonplot::plot{'major_ticscale'}.', '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
                   1696:     } else {
                   1697:     	$gnuplot_input .= 'set ticscale '.
                   1698: 	    $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
                   1699:     }
1.110     albertel 1700:     #boxwidth
                   1701:     if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
                   1702: 	$gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
                   1703:     }
                   1704:     # gridlayer
                   1705:     $gnuplot_input .= 'set grid noxtics noytics front '.$/ 
                   1706: 	if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
                   1707: 
1.7       matthew  1708:     # grid
1.107     foxr     1709:     $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
1.7       matthew  1710:     # border
1.107     foxr     1711:     $gnuplot_input .= ($Apache::lonplot::plot{'border'} eq 'on'?
1.9       matthew  1712: 		       'set border'.$/           :
1.67      matthew  1713: 		       'set noborder'.$/         );
1.77      matthew  1714:     # sampling rate for non-data curves
1.107     foxr     1715:     $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
1.67      matthew  1716:     # title, xlabel, ylabel
1.45      matthew  1717:     # titles
1.125     albertel 1718:     my $extra_space_x = ($xtics{'location'} eq 'axis') ? ' 0, -0.5 ' : '';
                   1719:     my $extra_space_y = ($ytics{'location'} eq 'axis') ? ' -0.5, 0 ' : '';
                   1720: 
1.89      matthew  1721:     if ($target eq 'tex') {
1.125     albertel 1722: 	$gnuplot_input .= "set title  \"$title\"          font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($title)) ;
                   1723: 	$gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($xlabel));
                   1724: 	$gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($ylabel));
1.89      matthew  1725:     } else {
1.125     albertel 1726:         $gnuplot_input .= "set title  \"$title\"          \n" if (defined($title)) ;
                   1727:         $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x \n" if (defined($xlabel));
                   1728:         $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y \n" if (defined($ylabel));
1.89      matthew  1729:     }
1.45      matthew  1730:     # tics
                   1731:     if (%xtics) {    
                   1732: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
1.46      matthew  1733: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1734: 	$gnuplot_input .= "$xtics{'start'}, ";
                   1735: 	$gnuplot_input .= "$xtics{'increment'}, ";
1.146     foxr     1736: 	$gnuplot_input .= "$xtics{'end'} ";
                   1737: 	if ($target eq 'tex') {
                   1738: 	    $gnuplot_input .= 'font "Helvetica,22"';     # Needed in iso 8859-1 enc.
                   1739: 	}
                   1740: 	$gnuplot_input .= "\n";
1.89      matthew  1741:         if ($xtics{'minorfreq'} != 0) {
                   1742:             $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
                   1743:         } 
1.146     foxr     1744:     } else {
                   1745: 	if ($target eq 'tex') {
                   1746: 	    $gnuplot_input .= 'set xtics font "Helvetica,22"'."\n"; # needed in iso 8859-1 enc
                   1747: 	}
1.45      matthew  1748:     }
                   1749:     if (%ytics) {    
                   1750: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
1.46      matthew  1751: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1752: 	$gnuplot_input .= "$ytics{'start'}, ";
                   1753: 	$gnuplot_input .= "$ytics{'increment'}, ";
1.146     foxr     1754:         $gnuplot_input .= "$ytics{'end'} ";
1.148     raeburn  1755:         if ($target eq 'tex') {
                   1756:             $gnuplot_input .= 'font "Helvetica,22"'; # Needed in iso-8859-1 encoding.
                   1757:         }
                   1758:         $gnuplot_input .= "\n";
1.89      matthew  1759:         if ($ytics{'minorfreq'} != 0) {
                   1760:             $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
                   1761:         } 
1.146     foxr     1762:     } else {
                   1763: 	if ($target eq 'tex') {
                   1764: 	    $gnuplot_input .= 'set ytics font "Helvetica,22"'."\n"; # Needed for iso 8859-1 enc.
                   1765: 	}
1.45      matthew  1766:     }
                   1767:     # axis
1.23      matthew  1768:     if (%axis) {
1.132     albertel 1769:         if ($axis{'xformat'} ne 'on') {
                   1770:             $gnuplot_input .= "set format x ";
                   1771:             if ($axis{'xformat'} eq 'off') {
                   1772:                 $gnuplot_input .= "\"\"\n";
                   1773:             } else {
                   1774:                 $gnuplot_input .= "\"\%.".$axis{'xformat'}."\"\n";
                   1775:             }
                   1776:         }
                   1777:         if ($axis{'yformat'} ne 'on') {
                   1778:             $gnuplot_input .= "set format y ";
                   1779:             if ($axis{'yformat'} eq 'off') {
                   1780:                 $gnuplot_input .= "\"\"\n";
                   1781:             } else {
                   1782:                 $gnuplot_input .= "\"\%.".$axis{'yformat'}."\"\n";
                   1783:             }
                   1784:         }
1.13      matthew  1785: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                   1786: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.152     faziophi 1787: 		if ($axis{'xzero'} ne 'off') {
                   1788: 			$gnuplot_input .= "set xzeroaxis ";
                   1789: 			if ($axis{'xzero'} eq 'line' || $axis{'xzero'} eq 'thick-line') {
                   1790: 				$gnuplot_input .= "lt -1 ";
                   1791: 				if ($axis{'xzero'} eq 'thick-line') {
                   1792: 					$gnuplot_input .= "lw 3 ";
                   1793: 				}
                   1794: 			}
                   1795: 			$gnuplot_input .= "\n";
                   1796: 		}
                   1797: 		if ($axis{'yzero'} ne 'off') {
                   1798: 			$gnuplot_input .= "set yzeroaxis ";
                   1799: 			if ($axis{'yzero'} eq 'line' || $axis{'yzero'} eq 'thick-line') {
                   1800: 				$gnuplot_input .= "lt -1 ";
                   1801: 				if ($axis{'yzero'} eq 'thick-line') {
                   1802: 					$gnuplot_input .= "lw 3 ";
                   1803: 				}
                   1804: 			}
                   1805: 			$gnuplot_input .= "\n";
                   1806: 		}
1.6       matthew  1807:     }
                   1808:     # Key
1.23      matthew  1809:     if (%key) {
1.9       matthew  1810: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                   1811: 	if ($key{'title'} ne '') {
1.67      matthew  1812: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
1.11      matthew  1813: 	} 
                   1814: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew  1815:     } else {
1.9       matthew  1816: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew  1817:     }
1.6       matthew  1818:     # labels
1.10      matthew  1819:     my $label;
1.6       matthew  1820:     foreach $label (@labels) {
                   1821: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.134     raeburn  1822:                           $label->{'xpos'}.','.$label->{'ypos'};
                   1823:         if ($label->{'rotate'} ne '') {
                   1824:             $gnuplot_input .= ' rotate by '.$label->{'rotate'};
                   1825:         }
                   1826:         $gnuplot_input .= ' '.$label->{'justify'};
                   1827: 
1.103     matthew  1828:         if ($target eq 'tex') {
1.124     albertel 1829: 	    $gnuplot_input .=' font "'.$font_properties->{'printname'}.','.$fontsize.'pt"' ;
1.103     matthew  1830:         }
                   1831:         $gnuplot_input .= $/;
1.6       matthew  1832:     }
1.74      matthew  1833:     if ($target eq 'tex') {
1.107     foxr     1834:         $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
1.74      matthew  1835:         $gnuplot_input .="\n";
1.120     albertel 1836:     }
1.6       matthew  1837:     # curves
1.156   ! foxr     1838:     #
        !          1839:     # Each curve will have its very own linestyle.
        !          1840:     # (This should work just fine in web rendition I think).
        !          1841:     #  The line_xxx variables will hold the elements of the line style.
        !          1842:     #  type (solid/dashed), color, width
        !          1843:     #
        !          1844:     my $linestyle_index = 50;
        !          1845:     my $line_type    = '';     
        !          1846:     my $line_color   = '';
        !          1847:     my $line_width   = '';
        !          1848: 
        !          1849:     my $plot_command;
        !          1850:     my $plot_type;
        !          1851: 
1.9       matthew  1852:     for (my $i = 0;$i<=$#curves;$i++) {
                   1853: 	$curve = $curves[$i];
1.156   ! foxr     1854: 	$plot_command.= ', ' if ($i > 0);
1.119     albertel 1855: 	if ($target eq 'tex') {
                   1856: 	    $curve->{'linewidth'} *= 2;
                   1857: 	}
1.6       matthew  1858: 	if (exists($curve->{'function'})) {
1.156   ! foxr     1859: 	    $plot_type    = 
1.9       matthew  1860: 		$curve->{'function'}.' title "'.
                   1861: 		$curve->{'name'}.'" with '.
1.72      matthew  1862:                 $curve->{'linestyle'};
1.6       matthew  1863: 	} elsif (exists($curve->{'data'})) {
1.40      matthew  1864: 	    # Store data values in $datatext
                   1865: 	    my $datatext = '';
                   1866: 	    #   get new filename
1.70      matthew  1867: 	    my $datafilename = "$tmpdir/$filename.data.$i";
1.40      matthew  1868: 	    my $fh=Apache::File->new(">$datafilename");
                   1869: 	    # Compile data
1.6       matthew  1870: 	    my @Data = @{$curve->{'data'}};
1.9       matthew  1871: 	    my @Data0 = @{$Data[0]};
                   1872: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew  1873: 		my $dataset;
1.6       matthew  1874: 		foreach $dataset (@Data) {
1.9       matthew  1875: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew  1876: 		}
1.9       matthew  1877: 		$datatext .= $/;
1.6       matthew  1878: 	    }
1.40      matthew  1879: 	    #   write file
                   1880: 	    print $fh $datatext;
1.119     albertel 1881: 	    close($fh);
1.40      matthew  1882: 	    #   generate gnuplot text
1.156   ! foxr     1883: 	    $plot_type = '"'.$datafilename.'" title "'.
1.40      matthew  1884: 		$curve->{'name'}.'" with '.
                   1885: 		$curve->{'linestyle'};
1.6       matthew  1886: 	}
1.156   ! foxr     1887: 	if (($curve->{'linestyle'} eq 'points')      ||
        !          1888: 	    ($curve->{'linestyle'} eq 'linespoints') ||
        !          1889: 	    ($curve->{'linestyle'} eq 'errorbars')   ||
        !          1890: 	    ($curve->{'linestyle'} eq 'xerrorbars')  ||
        !          1891: 	    ($curve->{'linestyle'} eq 'yerrorbars')  ||
        !          1892: 	    ($curve->{'linestyle'} eq 'xyerrorbars')) {
        !          1893: 	    $plot_command.=' pointtype '.$curve->{'pointtype'};
        !          1894: 	    $plot_command.=' pointsize '.$curve->{'pointsize'};
        !          1895: 	} elsif ($curve->{'linestyle'} eq 'filledcurves') { 
        !          1896: 	    $plot_command.= ' '.$curve->{'limit'};
        !          1897: 	} elsif ($curve->{'linetype'} ne '' &&
        !          1898: 		 $curve->{'linestyle'} eq 'lines') {
        !          1899: 	    $plot_command.= ' linetype ';
        !          1900: 	    $plot_command.= $linetypes{$curve->{'linetype'}};
        !          1901: 	    $plot_command.= ' linecolor rgb "';
        !          1902: 	    # convert color from xaaaaaa to #aaaaaa
        !          1903: 	    $curve->{'color'} =~ s/^x/#/;
        !          1904: 	    $plot_command.= $curve->{'color'}.'"';
        !          1905: 	}
        !          1906: 	$plot_command.= ' linewidth '.$curve->{'linewidth'};
        !          1907: 	$gnuplot_input .= 'plot ' . $plot_type . ' ' . $plot_command . "\n";
1.6       matthew  1908:     }
1.40      matthew  1909:     # Write the output to a file.
1.128     albertel 1910:     open (my $fh,">$tmpdir$filename.data");
                   1911:     binmode($fh, ":utf8");
1.40      matthew  1912:     print $fh $gnuplot_input;
                   1913:     close($fh);
                   1914:     # That's all folks.
                   1915:     return ;
1.2       matthew  1916: }
1.21      matthew  1917: 
                   1918: #---------------------------------------------- check_inputs
                   1919: sub check_inputs {
                   1920:     ## Note: no inputs, no outputs - this acts only on global variables.
                   1921:     ## Make sure we have all the input we need:
1.107     foxr     1922:     if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
1.23      matthew  1923:     if (! %key ) {} # No key for this plot, thats okay
1.34      matthew  1924: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21      matthew  1925:     if (! defined($title )) {} # No title for this plot, thats okay
                   1926:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
                   1927:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
                   1928:     if ($#labels < 0) { }      # No labels for this plot, thats okay
                   1929:     if ($#curves < 0) { 
                   1930: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
                   1931: 	return '';
                   1932:     }
                   1933:     my $curve;
                   1934:     foreach $curve (@curves) {
                   1935: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
1.85      matthew  1936: 	    &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
1.21      matthew  1937: 	    return '';
                   1938: 	}
                   1939:     }
                   1940: }
                   1941: 
1.20      matthew  1942: #------------------------------------------------ make_edit
                   1943: sub edit_attributes {
1.34      matthew  1944:     my ($target,$token,$defaults,$keys) = @_;
                   1945:     my ($result,@keys);
                   1946:     if ($keys && ref($keys) eq 'ARRAY') {
                   1947:         @keys = @$keys;
                   1948:     } else {
                   1949: 	@keys = sort(keys(%$defaults));
                   1950:     }
                   1951:     foreach my $attr (@keys) {
1.35      matthew  1952: 	# append a ' ' to the description if it doesn't have one already.
                   1953: 	my $description = $defaults->{$attr}->{'description'};
                   1954: 	$description .= ' ' if ($description !~ / $/);
1.20      matthew  1955: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1.35      matthew  1956: 	    $result .= &Apache::edit::text_arg
1.38      matthew  1957: 		($description,$attr,$token,
1.154     foxr     1958: 		 $defaults->{$attr}->{'size'},
                   1959: 		$defaults->{$attr}->{'class'});
1.20      matthew  1960: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1.102     albertel 1961: 	    $result .= &Apache::edit::select_or_text_arg
1.35      matthew  1962: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
1.45      matthew  1963: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1.102     albertel 1964: 	    $result .= &Apache::edit::select_or_text_arg
1.35      matthew  1965: 		($description,$attr,['on','off'],$token);
1.20      matthew  1966: 	}
1.25      matthew  1967: 	$result .= '<br />';
1.20      matthew  1968:     }
                   1969:     return $result;
                   1970: }
1.1       matthew  1971: 
1.21      matthew  1972: 
                   1973: ###################################################################
                   1974: ##                                                               ##
                   1975: ##           Insertion functions for editing plots               ##
                   1976: ##                                                               ##
                   1977: ###################################################################
                   1978: 
1.47      matthew  1979: sub insert_gnuplot {
1.29      matthew  1980:     my $result = '';
1.20      matthew  1981:     #  plot attributes
1.61      matthew  1982:     $result .= "\n<gnuplot ";
1.47      matthew  1983:     foreach my $attr (keys(%gnuplot_defaults)) {
1.61      matthew  1984: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
1.20      matthew  1985:     }
1.61      matthew  1986:     $result .= ">";
1.47      matthew  1987:     # Add the components (most are commented out for simplicity)
1.44      matthew  1988:     # $result .= &insert_key();
                   1989:     # $result .= &insert_axis();
                   1990:     # $result .= &insert_title();    
                   1991:     # $result .= &insert_xlabel();    
                   1992:     # $result .= &insert_ylabel();    
1.20      matthew  1993:     $result .= &insert_curve();
1.50      matthew  1994:     # close up the <gnuplot>
1.61      matthew  1995:     $result .= "\n</gnuplot>";
1.45      matthew  1996:     return $result;
                   1997: }
                   1998: 
1.46      matthew  1999: sub insert_tics {
                   2000:     my $result;
                   2001:     $result .= &insert_xtics() . &insert_ytics;
                   2002:     return $result;
                   2003: }
                   2004: 
1.45      matthew  2005: sub insert_xtics {
                   2006:     my $result;
1.46      matthew  2007:     $result .= "\n    <xtics ";
1.45      matthew  2008:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  2009: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  2010:     }
1.61      matthew  2011:     $result .= "/>";
1.45      matthew  2012:     return $result;
                   2013: }
                   2014: 
                   2015: sub insert_ytics {
                   2016:     my $result;
1.46      matthew  2017:     $result .= "\n    <ytics ";
1.45      matthew  2018:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  2019: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  2020:     }
1.61      matthew  2021:     $result .= "/>";
1.20      matthew  2022:     return $result;
                   2023: }
                   2024: 
                   2025: sub insert_key {
                   2026:     my $result;
1.61      matthew  2027:     $result .= "\n    <key ";
1.30      matthew  2028:     foreach my $attr (keys(%key_defaults)) {
1.61      matthew  2029: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
1.20      matthew  2030:     }
1.61      matthew  2031:     $result .= " />";
1.20      matthew  2032:     return $result;
                   2033: }
                   2034: 
                   2035: sub insert_axis{
                   2036:     my $result;
1.46      matthew  2037:     $result .= "\n    <axis ";
1.30      matthew  2038:    foreach my $attr (keys(%axis_defaults)) {
1.61      matthew  2039: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
1.20      matthew  2040:     }
1.61      matthew  2041:     $result .= " />";
1.20      matthew  2042:     return $result;
                   2043: }
1.28      matthew  2044: 
1.61      matthew  2045: sub insert_title  { return "\n    <title></title>"; }
                   2046: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
                   2047: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
1.20      matthew  2048: 
                   2049: sub insert_label {
                   2050:     my $result;
1.46      matthew  2051:     $result .= "\n    <label ";
1.30      matthew  2052:     foreach my $attr (keys(%label_defaults)) {
1.61      matthew  2053: 	$result .= "\n         $attr=\"".
                   2054:             $label_defaults{$attr}->{'default'}."\"";
1.20      matthew  2055:     }
1.61      matthew  2056:     $result .= "></label>";
1.20      matthew  2057:     return $result;
                   2058: }
                   2059: 
                   2060: sub insert_curve {
                   2061:     my $result;
1.41      matthew  2062:     $result .= "\n    <curve ";
1.30      matthew  2063:     foreach my $attr (keys(%curve_defaults)) {
1.61      matthew  2064: 	$result .= "\n         $attr=\"".
                   2065: 	    $curve_defaults{$attr}->{'default'}."\"";
1.20      matthew  2066:     }
1.61      matthew  2067:     $result .= " >";
                   2068:     $result .= &insert_data().&insert_data()."\n    </curve>";
1.20      matthew  2069: }
1.4       matthew  2070: 
1.20      matthew  2071: sub insert_function {
                   2072:     my $result;
1.61      matthew  2073:     $result .= "\n        <function></function>";
1.20      matthew  2074:     return $result;
                   2075: }
1.4       matthew  2076: 
1.20      matthew  2077: sub insert_data {
                   2078:     my $result;
1.61      matthew  2079:     $result .= "\n        <data></data>";
1.20      matthew  2080:     return $result;
                   2081: }
1.4       matthew  2082: 
1.48      matthew  2083: ##----------------------------------------------------------------------
1.20      matthew  2084: 1;
                   2085: __END__
1.4       matthew  2086: 
                   2087: 
1.149     jms      2088: =head1 NAME
                   2089: 
                   2090: Apache::lonplot.pm
                   2091: 
                   2092: =head1 SYNOPSIS
                   2093: 
                   2094: XML-based plotter of graphs
                   2095: 
                   2096: This is part of the LearningOnline Network with CAPA project
                   2097: described at http://www.lon-capa.org.
                   2098: 
                   2099: 
                   2100: =head1 SUBROUTINES (parsing and edit rendering)
                   2101: 
                   2102: =over
                   2103: 
                   2104: =item start_gnuplot()
                   2105: 
                   2106: =item end_gnuplot()
                   2107: 
                   2108: =item start_xtics()
                   2109: 
                   2110: =item end_xtics()
                   2111: 
                   2112: =item start_ytics()
                   2113: 
                   2114: =item end_ytics()
                   2115: 
                   2116: =item get_font()
                   2117: 
                   2118: =item start_key()
                   2119: 
                   2120: =item end_key()
                   2121: 
                   2122: =item parse_label()
                   2123: 
                   2124: =item replace_entities()
                   2125: 
                   2126: =item start_title()
                   2127: 
                   2128: =item end_title()
                   2129: 
                   2130: =item start_xlabel()
                   2131: 
                   2132: =item end_xlabel()
                   2133: 
                   2134: =item start_ylabel()
                   2135: 
                   2136: =item end_label()
                   2137: 
                   2138: =item start_curve()
                   2139: 
                   2140: =item end_curve()
                   2141: 
                   2142: =item start_function()
                   2143: 
                   2144: =item end_function()
                   2145: 
                   2146: =item start_data()
                   2147: 
                   2148: =item end_data()
                   2149: 
                   2150: =item start_axis()
                   2151: 
                   2152: =item end_axis
                   2153: 
                   2154: =back
                   2155: 
                   2156: =head1 SUBROUTINES (Utility)
                   2157: 
                   2158: =over
                   2159: 
                   2160: =item set_defaults()
                   2161: 
                   2162: =item get_attributes()
                   2163: 
                   2164: =item write_gnuplot_file()
                   2165: 
                   2166: =item check_inputs()
                   2167: 
                   2168: =item edit_attributes()
                   2169: 
                   2170: =back
                   2171: 
                   2172: =head1 SUBROUTINES (Insertion functions for editing plots)
                   2173: 
                   2174: =over
                   2175: 
                   2176: =item insert_gnuplot()
                   2177: 
                   2178: =item insert_tics()
                   2179: 
                   2180: =item insert_xtics()
                   2181: 
                   2182: =item insert_key()
                   2183: 
                   2184: =item insert_axis()
                   2185: 
                   2186: =item insert_title()
                   2187: 
                   2188: =item insert_xlabel()
                   2189: 
                   2190: =item insert_ylabel()
                   2191: 
                   2192: =item insert_label()
                   2193: 
                   2194: =item insert_curve()
                   2195: 
                   2196: =item insert_function()
                   2197: 
                   2198: =item insert_data()
                   2199: 
                   2200: =back
                   2201: 
                   2202: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>