[LON-CAPA-cvs] cvs: loncom /homework drawimage.pm randomlylabel.pm
albertel
lon-capa-cvs@mail.lon-capa.org
Thu, 09 Sep 2004 07:24:56 -0000
This is a MIME encoded message
--albertel1094714696
Content-Type: text/plain
albertel Thu Sep 9 03:24:56 2004 EDT
Modified files:
/loncom/homework drawimage.pm randomlylabel.pm
Log:
- and <image> and <polygon> to <drawimage>
- add support for clipping subimages in randomlylabeled images
--albertel1094714696
Content-Type: text/plain
Content-Disposition: attachment; filename="albertel-20040909032456.txt"
Index: loncom/homework/drawimage.pm
diff -u loncom/homework/drawimage.pm:1.6 loncom/homework/drawimage.pm:1.7
--- loncom/homework/drawimage.pm:1.6 Tue Mar 23 15:38:09 2004
+++ loncom/homework/drawimage.pm Thu Sep 9 03:24:56 2004
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# programatic image drawing
#
-# $Id: drawimage.pm,v 1.6 2004/03/23 20:38:09 albertel Exp $
+# $Id: drawimage.pm,v 1.7 2004/09/09 07:24:56 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -32,43 +32,48 @@
my %args;
my $cgi_id;
+my @cgi_ids;
BEGIN {
&Apache::lonxml::register('Apache::drawimage',('drawimage'));
}
sub start_drawimage {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
- &Apache::lonxml::register('Apache::drawimage',('text','line','rectangle','arc','fill'));
+ &Apache::lonxml::register('Apache::drawimage',('text','line','rectangle','arc','fill','polygon','image'));
if ($target eq 'web' || $target eq 'tex') {
- $cgi_id=&Apache::loncommon::get_cgi_id();
- %args=();
+ my $new_id=&Apache::loncommon::get_cgi_id();
+ if ($cgi_id) { push(@cgi_ids,$cgi_id); } else { undef(%args); }
+ $cgi_id=$new_id;
}
+ return '';
}
sub end_drawimage {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result;
if ($target eq 'web' || $target eq 'tex') {
- if ($target eq 'web') {
- my $width =
- &Apache::lonxml::get_param('width',$parstack,$safeeval);
- my $height =
- &Apache::lonxml::get_param('height',$parstack,$safeeval);
- my $bgcolor =
- &Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
- if (!$width) { $width=300; }
- if (!$height) { $height=300; }
- $result.="<img width='$width' height='$height'
+ my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval);
+ my $height =&Apache::lonxml::get_param('height',$parstack,$safeeval);
+ my $bgcolor =&Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
+ if (!$width) { $width=300; }
+ if (!$height) { $height=300; }
+ $result.="<img width='$width' height='$height'
src='/adm/randomlabel.png?token=$cgi_id' />\n";
- $args{"cgi.$cgi_id.SIZE"}=join(':',($width,$height));
- $args{"cgi.$cgi_id.BGCOLOR"}=join(':',($bgcolor));
- &Apache::lonnet::appenv(%args);
- } elsif ($target eq 'tex') {
- #FIXME generate image some how
- #probably Simple::PostScript
+ $args{"cgi.$cgi_id.SIZE"}=join(':',($width,$height));
+ $args{"cgi.$cgi_id.BGCOLOR"}=join(':',($bgcolor));
+ &Apache::lonnet::appenv(%args);
+ if (@cgi_ids) {
+ $cgi_id=pop(@cgi_ids);
+ } else {
+ undef($cgi_id);
}
+ } elsif ($target eq 'edit') {
+ } elsif ($target eq 'modified') {
}
- &Apache::lonxml::deregister('Apache::drawimage',('line','rectangle'));
+
+ &Apache::lonxml::register('Apache::drawimage',
+ ('text','line','rectangle','arc','fill',
+ 'polygon'));
return $result;
}
@@ -197,6 +202,90 @@
return $result;
}
+my @polygon;
+sub start_polygon {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+ my $result;
+ &Apache::lonxml::register('Apache::drawimage',('point'));
+ if ($target eq 'web') {
+ undef(@polygon);
+ }
+ return $result;
+}
+
+sub end_polygon {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+ my $result;
+ if ($target eq 'web') {
+ my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
+ my $filled=&Apache::lonxml::get_param('filled',$parstack,$safeeval);
+ my $open=&Apache::lonxml::get_param('open',$parstack,$safeeval);
+ my $thickness = &Apache::lonxml::get_param('thickness',$parstack,
+ $safeeval);
+ my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
+ $args{"cgi.$cgi_id.OBJTYPE"}.='POLYGON:';
+ $args{"cgi.$cgi_id.OBJ$i"}=join(':',($color,$thickness,$open,$filled));
+ $args{"cgi.$cgi_id.OBJEXTRA$i"}=join('-',@polygon);
+ }
+ &Apache::lonxml::deregister('Apache::drawimage',('point'));
+ return $result;
+}
+
+sub start_point {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+ my $result;
+ if ($target eq 'web') {
+ my $x = &Apache::lonxml::get_param('x',$parstack,$safeeval);
+ my $y = &Apache::lonxml::get_param('y',$parstack,$safeeval);
+ push (@polygon,"($x,$y)");
+ }
+ return $result;
+}
+
+sub end_point {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+ my $result;
+ return $result;
+}
+
+sub start_image {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+ my $result;
+ if ($target eq 'web' || $target eq 'tex') {
+ &Apache::lonxml::startredirection();
+ }
+ return $result;
+}
+sub end_image {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+ my $result;
+ if ($target eq 'web' || $target eq 'tex') {
+ my $bgimg=&Apache::lonxml::endredirection();
+ &Apache::lonnet::logthis("image $bgimg");
+ my $x = &Apache::lonxml::get_param('x',$parstack,$safeeval);
+ my $y = &Apache::lonxml::get_param('y',$parstack,$safeeval);
+ my $clipx = &Apache::lonxml::get_param('clipx',$parstack,$safeeval);
+ my $clipy = &Apache::lonxml::get_param('clipy',$parstack,$safeeval);
+ my $clipwidth =
+ &Apache::lonxml::get_param('clipwidth',$parstack,$safeeval);
+ my $clipheight =
+ &Apache::lonxml::get_param('clipheight',$parstack,$safeeval);
+ my $scaledwidth =
+ &Apache::lonxml::get_param('scaledwidth',$parstack,$safeeval);
+ my $scaledheight =
+ &Apache::lonxml::get_param('scaledheight',$parstack,$safeeval);
+ my $transparent =
+ &Apache::lonxml::get_param('transparent',$parstack,$safeeval);
+ $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
+ &Apache::lonnet::logthis("image af clean $bgimg");
+ my $i=$args{"cgi.$cgi_id.OBJCOUNT"}++;
+ $args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
+ $args{"cgi.$cgi_id.OBJ$i"} =
+ join(':',($x,$y,&Apache::lonnet::escape($bgimg),$transparent,
+ $clipx,$clipy,$scaledwidth,$scaledheight,$clipwidth,$clipheight));
+ }
+ return $result;
+}
1;
__END__
Index: loncom/homework/randomlylabel.pm
diff -u loncom/homework/randomlylabel.pm:1.20 loncom/homework/randomlylabel.pm:1.21
--- loncom/homework/randomlylabel.pm:1.20 Thu Jul 15 14:06:09 2004
+++ loncom/homework/randomlylabel.pm Thu Sep 9 03:24:56 2004
@@ -2,7 +2,7 @@
# The LearningOnline Network with CAPA
# randomlabel.png: composite together text and images into 1 image
#
-# $Id: randomlylabel.pm,v 1.20 2004/07/15 18:06:09 albertel Exp $
+# $Id: randomlylabel.pm,v 1.21 2004/09/09 07:24:56 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -137,8 +137,8 @@
my $imcolor=&get_color_from_hexstring($image,$color);
if (!defined($thickness)) { $thickness=1; }
$image->setThickness($thickness);
- $image->setAntiAliased($imcolor);
- $image->line($x1,$y1,$x2,$y2,gdAntiAliased);
+# $image->setAntiAliased($imcolor);
+ $image->line($x1,$y1,$x2,$y2,$imcolor);
} elsif ($type eq 'RECTANGLE') {
my ($x1,$y1,$x2,$y2,$color,$thickness,$filled)=
split(':',$ENV{"cgi.$id.OBJ$i"});
@@ -154,9 +154,10 @@
$image->rectangle($x1,$y1,$x2,$y2,$imcolor);
}
} elsif ($type eq 'POLYGON') {
- my ($color,$width,$open)=split(':',$ENV{"cgi.$id.OBJ$i"});
+ my ($color,$width,$open,$filled)=split(':',$ENV{"cgi.$id.OBJ$i"});
my $imcolor=&get_color_from_hexstring($image,$color);
- my $polygon = (($open) ? (new GD::Polyline) : (new GD::Polygon));
+ my $polygon = (($open && lc ($open ne 'no')) ?
+ (new GD::Polyline) : (new GD::Polygon));
my $added=0;
foreach my $coord (split('-',$ENV{"cgi.$id.OBJEXTRA$i"})) {
my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
@@ -166,8 +167,10 @@
$image->setThickness($width);
if ($added) {
- if ($open) {
+ if ($open && lc($open) ne 'no') {
$image->polydraw($polygon,$imcolor);
+ } elsif ($filled && lc($filled) ne 'no') {
+ $image->filledPolygon($polygon,$imcolor);
} else {
$image->polygon($polygon,$imcolor);
}
@@ -192,7 +195,8 @@
my $imcolor=&get_color_from_hexstring($image,$color);
$image->fill($x,$y,$imcolor);
} elsif ($type eq 'IMAGE') {
- my ($x,$y,$file,$transparent)=split(':',$ENV{"cgi.$id.OBJ$i"});
+ my ($x,$y,$file,$transparent,$srcX,$srcY,$destW,$destH,$srcW,
+ $srcH)=split(':',$ENV{"cgi.$id.OBJ$i"});
$file=&Apache::lonnet::unescape($file);
if (!defined($transparent)) { $transparent=1; }
my $subimage=&get_image($file,$transparent);
@@ -201,7 +205,12 @@
$file);
next;
}
- $image->copy($subimage,$x,$y,0,0,$subimage->getBounds());
+ if (!defined($srcW) or !$srcW) {$srcW=($subimage->getBounds())[0];}
+ if (!defined($srcH) or !$srcH) {$srcH=($subimage->getBounds())[1];}
+ if (!defined($destW) or !$destW) { $destW=$srcW; }
+ if (!defined($destH) or !$destH) { $destH=$srcH; }
+ $image->copyResized($subimage,$x,$y,$srcX,$srcY,$destW,$destH,
+ $srcW,$srcH);
} elsif ($type eq 'LABEL') {
my ($x,$y,$text,$font,$color,$direction)=
split(':',$ENV{"cgi.$id.OBJ$i"});
@@ -221,7 +230,7 @@
} elsif ($font eq 'large') {
$height=GD::Font->Large->height;
$fontref=GD::gdLargeFont;
- } elsif ($font eq 'giant' || !defined($font)) {
+ } elsif ($font eq 'giant' || !$font) {
$height=GD::Font->Giant->height;
$fontref=GD::gdGiantFont;
} else {
--albertel1094714696--