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

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

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