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

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

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