File:  [LON-CAPA] / loncom / homework / drawimage.pm
Revision 1.11: download - view: text, annotated - select for diffs
Fri Apr 5 02:31:23 2024 UTC (8 weeks, 1 day ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add rotation attribute to text tag for drawimage. Rotation value is the
  number of degrees to rotate the text, relative to the horizontal.
 - Only used if font attribute is set to freetype font (e.g., helvetica 12).
 - If set to a valid value will override the value for direction attribute.

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

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