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

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

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