Annotation of loncom/homework/drawimage.pm, revision 1.8

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # programatic image drawing
                      3: #
1.8     ! albertel    4: # $Id: drawimage.pm,v 1.7 2004/09/09 07:24:56 albertel Exp $
1.1       albertel    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: #
                     28: 
                     29: package Apache::drawimage;
                     30: use strict;
                     31: use Apache::loncommon;
                     32: 
                     33: my %args;
                     34: my $cgi_id;
1.7       albertel   35: my @cgi_ids;
1.1       albertel   36: BEGIN {
                     37:     &Apache::lonxml::register('Apache::drawimage',('drawimage'));
                     38: }
                     39: 
                     40: sub start_drawimage {
                     41:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel   42:     &Apache::lonxml::register('Apache::drawimage',('text','line','rectangle','arc','fill','polygon','image'));
1.1       albertel   43:     if ($target eq 'web' || $target eq 'tex') {
1.7       albertel   44: 	my $new_id=&Apache::loncommon::get_cgi_id();
                     45: 	if ($cgi_id) { push(@cgi_ids,$cgi_id); } else { undef(%args); }
                     46: 	$cgi_id=$new_id;
1.1       albertel   47:     }
1.7       albertel   48:     return '';
1.1       albertel   49: }
                     50: 
                     51: sub end_drawimage {
                     52:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     53:     my $result;
                     54:     if ($target eq 'web' || $target eq 'tex') {
1.7       albertel   55: 	my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
                     56: 	my $height =&Apache::lonxml::get_param('height',$parstack,$safeeval);
                     57: 	my $bgcolor =&Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
                     58: 	if (!$width) { $width=300; }
                     59: 	if (!$height) { $height=300; }
                     60: 	$result.="<img width='$width' height='$height'
1.1       albertel   61:                            src='/adm/randomlabel.png?token=$cgi_id' />\n";
1.7       albertel   62: 	$args{"cgi.$cgi_id.SIZE"}=join(':',($width,$height));
                     63: 	$args{"cgi.$cgi_id.BGCOLOR"}=join(':',($bgcolor));
                     64: 	&Apache::lonnet::appenv(%args);
                     65: 	if (@cgi_ids) {
                     66: 	    $cgi_id=pop(@cgi_ids);
                     67: 	} else {
                     68: 	    undef($cgi_id);
1.1       albertel   69: 	}
1.7       albertel   70:     } elsif ($target eq 'edit') {
                     71:     } elsif ($target eq 'modified') {
1.1       albertel   72:     }
1.7       albertel   73:     
                     74:     &Apache::lonxml::register('Apache::drawimage',
                     75: 			      ('text','line','rectangle','arc','fill',
                     76: 			       'polygon'));
1.4       matthew    77:     return $result;
                     78: }
                     79: 
1.5       albertel   80: sub start_text {
1.4       matthew    81:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     82:     my $result;
                     83:     if ($target eq 'web' || $target eq 'tex') {
1.6       albertel   84: 	&Apache::lonxml::startredirection();
                     85:     }
                     86:     return $result;
                     87: }
                     88: 
                     89: sub end_text {
                     90:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     91:     my $result;
                     92:     if ($target eq 'web' || $target eq 'tex') {
1.4       matthew    93:         my $x     = &Apache::lonxml::get_param('x',$parstack,$safeeval);
                     94:         my $y     = &Apache::lonxml::get_param('y',$parstack,$safeeval);
                     95:         my $font  = &Apache::lonxml::get_param('font',$parstack,$safeeval);
                     96:         my $color = &Apache::lonxml::get_param('color',$parstack,$safeeval);
1.5       albertel   97:         my $direction = &Apache::lonxml::get_param('direction',$parstack,$safeeval);
1.6       albertel   98: 	my $text  = &Apache::lonxml::endredirection();
1.4       matthew    99:         $text = &Apache::lonnet::escape($text);
                    100:         $args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
                    101: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
1.5       albertel  102: 	$args{"cgi.$cgi_id.OBJ$i"}=join(':',($x,$y,$text,$font,$color,$direction));
1.4       matthew   103:     }
1.1       albertel  104:     return $result;
                    105: }
                    106: 
                    107: sub start_line {
                    108:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    109:     my $result;
                    110:     if ($target eq 'web' || $target eq 'tex') {
                    111: 	my $x1 = &Apache::lonxml::get_param('x1',$parstack,$safeeval);
                    112: 	my $y1 = &Apache::lonxml::get_param('y1',$parstack,$safeeval);
                    113: 	my $x2 = &Apache::lonxml::get_param('x2',$parstack,$safeeval);
                    114: 	my $y2 = &Apache::lonxml::get_param('y2',$parstack,$safeeval);
                    115: 	my $color = &Apache::lonxml::get_param('color',$parstack,$safeeval);
                    116: 	my $thickness = &Apache::lonxml::get_param('thickness',$parstack,$safeeval);
1.3       albertel  117: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
1.1       albertel  118: 	$args{"cgi.$cgi_id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,$color,$thickness));
                    119: 	$args{"cgi.$cgi_id.OBJTYPE"}.='LINE:';
                    120:     }
                    121:     return $result;
                    122: }
                    123: 
                    124: sub end_line {
                    125:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    126:     my $result;
                    127:     return $result;
                    128: }
                    129: 
                    130: sub start_rectangle {
                    131:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    132:     my $result;
                    133:     if ($target eq 'web' || $target eq 'tex') {
                    134: 	my $x1 = &Apache::lonxml::get_param('x1',$parstack,$safeeval);
                    135: 	my $y1 = &Apache::lonxml::get_param('y1',$parstack,$safeeval);
                    136: 	my $x2 = &Apache::lonxml::get_param('x2',$parstack,$safeeval);
                    137: 	my $y2 = &Apache::lonxml::get_param('y2',$parstack,$safeeval);
                    138: 	my $color = &Apache::lonxml::get_param('color',$parstack,$safeeval);
                    139: 	my $thickness = &Apache::lonxml::get_param('thickness',$parstack,
                    140: 						   $safeeval);
                    141: 	my $filled = &Apache::lonxml::get_param('filled',$parstack,
                    142: 						$safeeval);
1.3       albertel  143: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
1.1       albertel  144: 	$args{"cgi.$cgi_id.OBJ$i"}=
                    145: 	    join(':',($x1,$y1,$x2,$y2,$color,$thickness,$filled));
                    146: 	$args{"cgi.$cgi_id.OBJTYPE"}.='RECTANGLE:';
                    147:     }
                    148:     return $result;
                    149: }
                    150: 
                    151: sub end_rectangle {
                    152:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    153:     my $result;
                    154:     return $result;
                    155: }
                    156: 
                    157: sub start_arc {
                    158:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    159:     my $result;
                    160:     if ($target eq 'web' || $target eq 'tex') {
                    161: 	my $x = &Apache::lonxml::get_param('x',$parstack,$safeeval);
                    162: 	my $y = &Apache::lonxml::get_param('y',$parstack,$safeeval);
                    163: 	my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
                    164: 	my $height = &Apache::lonxml::get_param('height',$parstack,$safeeval);
                    165: 	my $start = &Apache::lonxml::get_param('start',$parstack,$safeeval);
                    166: 	my $end = &Apache::lonxml::get_param('end',$parstack,$safeeval);
                    167: 	my $color = &Apache::lonxml::get_param('color',$parstack,$safeeval);
                    168: 	my $thickness = &Apache::lonxml::get_param('thickness',$parstack,$safeeval);
                    169: 	my $filled = &Apache::lonxml::get_param('filled',$parstack,$safeeval);
1.3       albertel  170: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
1.1       albertel  171: 	$args{"cgi.$cgi_id.OBJ$i"}=
                    172: 	    join(':',($x,$y,$width,$height,$start,$end,$color,$thickness,
                    173: 		      $filled));
                    174: 	$args{"cgi.$cgi_id.OBJTYPE"}.='ARC:';
                    175:     }
                    176:     return $result;
                    177: }
                    178: 
                    179: sub end_arc {
                    180:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    181:     my $result;
                    182:     return $result;
                    183: }
                    184: 
1.2       albertel  185: sub start_fill {
1.1       albertel  186:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    187:     my $result;
                    188:     if ($target eq 'web' || $target eq 'tex') {
                    189: 	my $x = &Apache::lonxml::get_param('x',$parstack,$safeeval);
                    190: 	my $y = &Apache::lonxml::get_param('y',$parstack,$safeeval);
                    191: 	my $color = &Apache::lonxml::get_param('color',$parstack,$safeeval);
1.3       albertel  192: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
1.1       albertel  193: 	$args{"cgi.$cgi_id.OBJ$i"}=join(':',($x,$y,$color));
                    194: 	$args{"cgi.$cgi_id.OBJTYPE"}.='FILL:';
                    195:     }
                    196:     return $result;
                    197: }
                    198: 
1.2       albertel  199: sub end_fill {
1.1       albertel  200:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    201:     my $result;
                    202:     return $result;
                    203: }
                    204: 
1.7       albertel  205: my @polygon;
                    206: sub start_polygon {
                    207:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    208:     my $result;
                    209:     &Apache::lonxml::register('Apache::drawimage',('point'));
                    210:     if ($target eq 'web') {
                    211: 	undef(@polygon);
                    212:     }
                    213:     return $result;
                    214: }
                    215: 
                    216: sub end_polygon {
                    217:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    218:     my $result;    
                    219:     if ($target eq 'web') {
                    220: 	my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
                    221: 	my $filled=&Apache::lonxml::get_param('filled',$parstack,$safeeval);
                    222: 	my $open=&Apache::lonxml::get_param('open',$parstack,$safeeval);
                    223: 	my $thickness = &Apache::lonxml::get_param('thickness',$parstack,
                    224: 						   $safeeval);
                    225: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
                    226: 	$args{"cgi.$cgi_id.OBJTYPE"}.='POLYGON:';
                    227: 	$args{"cgi.$cgi_id.OBJ$i"}=join(':',($color,$thickness,$open,$filled));
                    228: 	$args{"cgi.$cgi_id.OBJEXTRA$i"}=join('-',@polygon);
                    229:     }
                    230:     &Apache::lonxml::deregister('Apache::drawimage',('point'));
                    231:     return $result;
                    232: }
                    233: 
                    234: sub start_point {
                    235:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    236:     my $result;
                    237:     if ($target eq 'web') {
                    238: 	my $x = &Apache::lonxml::get_param('x',$parstack,$safeeval);
                    239:         my $y = &Apache::lonxml::get_param('y',$parstack,$safeeval);
                    240: 	push (@polygon,"($x,$y)");
                    241:     }
                    242:     return $result;
                    243: }
                    244: 
                    245: sub end_point {
                    246:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    247:     my $result;
                    248:     return $result;
                    249: }
                    250: 
                    251: sub start_image {
                    252:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    253:     my $result;
                    254:     if ($target eq 'web' || $target eq 'tex') {
                    255: 	&Apache::lonxml::startredirection();
                    256:     }
                    257:     return $result;
                    258: }
1.1       albertel  259: 
1.7       albertel  260: sub end_image {
                    261:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    262:     my $result;
                    263:     if ($target eq 'web' || $target eq 'tex') {
                    264: 	my $bgimg=&Apache::lonxml::endredirection();
                    265: 	my $x = &Apache::lonxml::get_param('x',$parstack,$safeeval);
                    266:         my $y = &Apache::lonxml::get_param('y',$parstack,$safeeval);
                    267: 	my $clipx = &Apache::lonxml::get_param('clipx',$parstack,$safeeval);
                    268:         my $clipy = &Apache::lonxml::get_param('clipy',$parstack,$safeeval);
                    269: 	my $clipwidth = 
                    270: 	    &Apache::lonxml::get_param('clipwidth',$parstack,$safeeval);
                    271:         my $clipheight = 
                    272: 	    &Apache::lonxml::get_param('clipheight',$parstack,$safeeval);
                    273: 	my $scaledwidth = 
                    274: 	    &Apache::lonxml::get_param('scaledwidth',$parstack,$safeeval);
                    275:         my $scaledheight = 
                    276: 	    &Apache::lonxml::get_param('scaledheight',$parstack,$safeeval);
                    277: 	my $transparent = 
                    278: 	    &Apache::lonxml::get_param('transparent',$parstack,$safeeval);
                    279: 	$bgimg=&Apache::imageresponse::clean_up_image($bgimg);
                    280: 	my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
                    281: 	$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
                    282: 	$args{"cgi.$cgi_id.OBJ$i"} = 
                    283: 	    join(':',($x,$y,&Apache::lonnet::escape($bgimg),$transparent,
                    284: 		      $clipx,$clipy,$scaledwidth,$scaledheight,$clipwidth,$clipheight));
                    285:     }
                    286:     return $result;
                    287: }
1.1       albertel  288: 1;
                    289: __END__

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