File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.93: download - view: text, annotated - select for diffs
Tue Nov 25 13:16:17 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: HEAD
POD documentation changes

    1: # The LearningOnline Network with CAPA
    2: # random labelling tool
    3: #
    4: # $Id: randomlabel.pm,v 1.93 2008/11/25 13:16:17 jms 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: =pod
   29: 
   30: =head1 NAME
   31: 
   32: Apache::randomlable.pm
   33: 
   34: =head1 SYNOPSIS
   35: 
   36: Interface for producing applet code which
   37: randomizes the labelling of an image.
   38: 
   39: This is part of the LearningOnline Network with CAPA project
   40: described at http://www.lon-capa.org.
   41: 
   42: 
   43: =head1 SYNTAX
   44: 
   45:  <randomlabel bgimg="URL" width="12" height="45" texwidth="50">
   46:     <labelgroup name="GroupOne" type="image">
   47:       <location x="123" y="456" />
   48:       <location x="321" y="654" />
   49:       <location x="213" y="546" />
   50:       <label description="TEXT-1">IMG-URL</label>
   51:       <label description="TEXT-2">IMG-URL</label>
   52:       <label description="TEXT-3">IMG-URL</label>
   53:     </labelgroup>
   54:     <labelgroup name="GroupTwo" type="text">
   55:       <location x="12" y="45" />
   56:       <location x="32" y="65" />
   57:       <location x="21" y="54" />
   58:       <label>TEXT-1</label>
   59:       <label>TEXT-2</label>
   60:       <label>TEXT-3</label>
   61:     </labelgroup>
   62:    </randomlabel>
   63:   ===========================================
   64:   side effect:
   65:     location (123,456): $GroupOne[0] = 2   images give out indexes
   66:              (321,654): $GroupOne[1] = 1
   67:              (213,546): $GroupOne[2] = 0
   68:     location (12,45)  : $GroupTwo[0] = "TEXT-3"
   69:              (32,65)  : $GroupTwo[1] = "TEXT-1"
   70:              (21,54)  : $GroupTwo[2] = "TEXT-2"
   71:   ===========================================
   72: 
   73: 
   74: =head1 NOTABLE SUBROUTINES
   75: 
   76: =over
   77: 
   78: =item check_int()
   79: 
   80: 	utility function to do error checking on a integer.
   81: 
   82: =item extract_tag_sizes()
   83: 
   84: Parameters:
   85:       tag         - tag potentially containing height/width attributes.
   86:       def_width   - Default width.
   87:       def_height  - Default height.
   88:   Returns:
   89:       list containing width/height.
   90: 
   91: =item get_label_width()
   92: 
   93: 	 Utility sub to compute the width of a label.
   94: 	 
   95: =item end_labelgroup()
   96: 
   97: begin to assign labels to locations
   98: 
   99: =back
  100: 
  101: =cut
  102: 
  103: 
  104: 
  105: package Apache::randomlabel;
  106: use Apache::lonnet;
  107: use strict;
  108: use Apache::edit;
  109: use Apache::File();
  110: use Apache::Constants qw(:common :http);
  111: use Image::Magick;
  112: use Apache::lonplot;
  113: use LONCAPA;
  114:  
  115: 
  116: my %args;
  117: my $cgi_id;
  118: my $scale_factor;		# image scale factor.
  119: my $label_xscale;                # Label scale factor (needed for gnuplot).
  120: my $label_yscale;
  121: my $dirty_width_adjust = 5;     # Width adjustment for e.g. gnuplot images.
  122: 
  123: BEGIN {
  124:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
  125: }
  126: 
  127: 
  128: 
  129: sub check_int {
  130:     # utility function to do error checking on a integer.
  131:     my ($num,$default) = @_;
  132:     $default = 100 if (! defined($default));
  133:     $num =~ s/\s+//g;  # We dont need no stinkin white space!
  134:     # If it is a real, just grab the integer part.
  135:     ($num,undef) = split (/\./,$num) if ($num =~ /\./); 
  136:     # set to default if what we have left doesn't match anything...
  137:     $num = $default unless ($num =~/^\d+$/);
  138:     return $num;
  139: }
  140: 
  141: 
  142: 
  143: sub extract_tag_sizes {
  144:     my ($tag, $dw, $dh) = @_;
  145:     $tag =~ s/\s+/ /g;         # Collapse whitespace.
  146:     $tag =~ s/\s*=\s*/=/g;     # kill space around ='s.
  147:     $tag =~ s/[<>\"]//g;       # Get rid of the <">'s too.
  148: 
  149:     &Apache::lonxml::debug("Compressed tag: $tag");
  150:     my @taglist = split(/ /,$tag);
  151:     foreach my $attribute (@taglist) {
  152: 	if ($attribute =~ /^width/i) {
  153: 	    my ($e, $s)= split(/=/,$attribute);
  154: 	    $dw = $s;
  155: 	}
  156: 	if ($attribute =~  /^height/i) {
  157: 	    my ($e, $s) = split(/=/,$attribute);
  158: 	    $dh = $s;
  159: 	}
  160:     } 
  161:     return($dw, $dh);
  162: 
  163: }
  164: 
  165: my ($height_param,$width_param);
  166: sub start_randomlabel {
  167: 
  168:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  169:     my $result='';
  170:     push (@Apache::lonxml::namespace,'randomlabel');
  171:     ($height_param,$width_param)=(0,0);
  172:     $label_xscale = 1.0;		# Assume image size not overridden.
  173:     $label_yscale = 1.0;
  174:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  175:     if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
  176: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
  177: 	if (&Apache::lonnet::repcopy($bgimg) ne 'ok') {
  178: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
  179: 	}
  180:     }
  181:     $Apache::randomlabel::obj_cnt=0;
  182:     if ($target eq 'web') {
  183: 	$cgi_id=&Apache::loncommon::get_cgi_id();
  184: 	%args=();
  185: 	$args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
  186: 	$height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
  187: 	$width_param  = &Apache::lonxml::get_param('width', $parstack, $safeeval);
  188:     } elsif ($target eq 'tex' && defined($bgimg)) {
  189: 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
  190:     } elsif ($target eq 'edit') {
  191:         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
  192: 	$result.=&Apache::edit::tag_start($target,$token);
  193: 	$Apache::edit::bgimgsrc=
  194: 	    &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  195: 	$Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
  196: 	$result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
  197: 	$result.=&Apache::edit::browse('bgimg',undef,undef,$only).' ';
  198: 	$result.=&Apache::edit::search('bgimg').'<br />'.
  199: 	    &Apache::edit::text_arg('Width(pixel):' ,'width'   ,$token,6).
  200: 	    &Apache::edit::text_arg('Height(pixel):','height'  ,$token,6).
  201: 	    &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
  202: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();     
  203:     } elsif ($target eq 'modified') {
  204: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  205: 						     $safeeval,'bgimg','width',
  206: 						     'height','texwidth');
  207: 	if ($constructtag) {
  208: 	    $result = &Apache::edit::rebuild_tag($token);
  209: 	}
  210:     }
  211:     return $result;
  212: }
  213: 
  214: sub end_randomlabel {
  215:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  216:     my $result='';
  217:     my $count;
  218:     pop @Apache::lonxml::namespace;
  219:     if ($target eq 'web') {
  220: 	$count = $Apache::randomlabel::obj_cnt;
  221: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
  222: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
  223: 	&Apache::lonnet::appenv(\%args);
  224:     } elsif ($target eq 'tex') {
  225: 	$result='\end{picture}\\\\';
  226: 	$result.= ' \vskip -'.$height_param.' mm }  \\\\ ';
  227:     } elsif ($target eq 'edit') {
  228: 	$result.=&Apache::edit::end_table;
  229:     }
  230:     return $result;
  231: }
  232: 
  233: 
  234: sub start_bgimg {
  235:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  236:     my $result='';
  237:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  238: 	&Apache::lonxml::startredirection(); 
  239:     }
  240:     return $result;
  241: }
  242: 
  243: sub end_bgimg {
  244:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  245:     my $result='';
  246:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  247: 	my $bgimg=&Apache::lonxml::endredirection(); 
  248: 	if ($target eq 'web') {
  249: 	    
  250: 	    # If the  tag produced has sizes, they override ours.
  251: 	    # (for now anyway).
  252: 	    #
  253: 	    &Apache::lonxml::debug("Base sizes: $width_param x $height_param");
  254: 	
  255: 	    my ($plot_x, $plot_y) = &extract_tag_sizes($bgimg, 
  256: 						       $width_param,
  257: 						       $height_param);
  258: 	    &Apache::lonxml::debug("Extracted sizes: $plot_x x $plot_y");
  259: 	    if ($width_param) {
  260: 		$label_xscale     = $plot_x / $width_param;
  261: 	    }
  262: 	    if ($height_param) {
  263: 		$label_yscale     = $plot_y / $height_param;
  264: 	    }
  265: 	    &Apache::lonxml::debug("Scale factors:   $label_xscale $label_yscale");
  266: 
  267: 	    &Apache::lonxml::debug("Image: $bgimg");
  268: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
  269: 	    &Apache::lonxml::debug("Cleaned image: $bgimg");
  270: 	    $args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
  271: 	} elsif ($target eq 'tex') {
  272: 	    #   Some bg images can create latex for us... e.g. gnuplot.
  273: 	    #   If it looks like we have some latex use that, 
  274: 	    #   otherwise, assume this is a resource name that must
  275: 	    #   be converted into the latex to create an eps insertion.
  276: 	    #
  277: 	    my $src = $bgimg;
  278: 	    $src =~ s/\s+$//s;
  279: 	    $src =~ s/^\s+//s;
  280: 
  281: 	    #If this is a dynamically generated image, it will
  282: 	    #be in latex already, with a comment header that
  283: 	    #describes the dimensions:
  284: 
  285: 	    if($src =~ /^%DYNAMICIMAGE:/) {
  286: 		$Apache::lonxml::debug = 0;
  287: 		&Apache::lonxml::debug("Dynamic image");
  288: 		my ($commentline, $junk) = split(/\n/, $src);
  289: 		&Apache::lonxml::debug("Comment line was: $commentline");
  290: 		my $trash;
  291: 		my $initial_width;
  292: 		($trash, $initial_width, $height_param, $width_param) =
  293: 		    split(/:/,$commentline);
  294: 		&Apache::lonxml::debug("internal web Width/height: $initial_width $height_param");
  295: 		&Apache::lonxml::debug("Texwitdh: $width_param");
  296: 		if($initial_width == 0) {
  297: 		    $initial_width = $width_param;
  298: 		}
  299: 		# strip off the comments since output does not always
  300: 		# preserve \n's:
  301:                 #
  302: 		$src =~ s/$commentline//;
  303: 		$scale_factor = $width_param / $initial_width;
  304: 		$height_param = $height_param*$scale_factor;
  305: 
  306: 		$label_xscale = 1.0; #  $scale_factor;
  307: 		$label_yscale = 1.0; #  $scale_factor;
  308: 	   
  309: 		&Apache::lonxml::debug("height $height_param");
  310: 		&Apache::lonxml::debug("Width $width_param");
  311: 		&Apache::lonxml::debug("Scale factors: $label_xscale $label_yscale");
  312: 		my $dirty_width = $width_param + $dirty_width_adjust;
  313: 		my $x_offset    = -$dirty_width_adjust/2.0;
  314: 		#
  315: 		#  Somewhere here it looks like height_param and
  316: 		#  width_param got backwards...
  317: 		#
  318: 		$result .= '\parbox{'.$dirty_width.'mm}{';
  319: 		$result  .= " $src \n";
  320: 		$result  .= '\setlength{\unitlength}{1mm}'."\n";
  321: 		$result  .= '\begin{picture}('."$width_param,$height_param)";
  322: 		$result  .= "($x_offset,-$height_param)";
  323: 		$result  .= "\n";
  324: 		$Apache::lonxml::debug = 0;
  325: 
  326: 	    } else {
  327: 		$result.=&make_eps_image($bgimg,$parstack,$safeeval,-2);
  328: 	    }
  329: 	}
  330:     }
  331:     return $result;
  332: }
  333: sub make_eps_image {
  334:     my ($bgimg,$parstack,$safeeval,$depth)=@_;
  335:     &Apache::lonxml::debug("image prior to get_eps_image: $bgimg");
  336:     my ($path,$file) = &Apache::londefdef::get_eps_image($bgimg);
  337:     &Apache::lonxml::debug("image after:  $bgimg");
  338:     ($height_param,$width_param)=
  339: 	&Apache::londefdef::image_size($bgimg,0.3,$parstack,$safeeval,
  340: 				       $depth,1);
  341: 
  342:     &Apache::lonxml::debug("Image size: $height_param x $width_param");
  343: 
  344:     my $dirtywidth=$width_param+5;
  345: 
  346:     my $result ="\n".'\vspace*{2mm}\noindent'."\n".
  347: 	'\parbox{'.$dirtywidth.
  348: 	' mm}{  \noindent \epsfxsize='.$width_param.
  349: 	' mm \epsffile{'.$path.$file.
  350: 	'}\setlength{\unitlength}{1mm}'."\n".'  \begin{picture}('.
  351: 	$width_param.','.$height_param.')(0,-'.$height_param.')'."\n";
  352:     my $magick = Image::Magick->new;
  353:     $magick->Read($bgimg);
  354:     my $initial_width = $magick->Get('width');
  355:     &Apache::lonxml::debug("ImageMagick thinks width is; $initial_width");
  356:     $scale_factor = $width_param / $initial_width;
  357:     return $result;
  358: }
  359: 
  360: sub start_labelgroup {
  361:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  362:     my $result='';
  363:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  364:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  365:     $type =~tr/A-Z/a-z/;
  366:     if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
  367: 	&Apache::lonxml::error("Only _ a-z A-Z and 0-9 are allowed in the name to a labelgroup, and the first character can not be a number.<br />");
  368:     }
  369:     if ($target eq 'web' || $target eq 'tex' ||
  370: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  371: 	$Apache::randomlabel::groupname=$name;
  372: 	$Apache::randomlabel::type=$type;
  373: 	@Apache::randomlabel::xcoord = ();
  374: 	@Apache::randomlabel::ycoord = ();
  375: 	@Apache::randomlabel::value = ();
  376: 	@Apache::randomlabel::label_arr  = ();
  377: 	@Apache::randomlabel::description  = ();
  378:     } elsif ($target eq 'edit') {
  379: 	$result.=&Apache::edit::tag_start($target,$token);
  380: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
  381: 	    &Apache::edit::select_arg('Type:','type',['text','image'],$token);
  382: 	if (!defined($token->[2]{'TeXsize'})) {
  383: 	    $token->[2]{'TeXsize'}='\normalsize';
  384: 	}
  385: 	$result.=&Apache::edit::select_arg('TeX font size:','TeXsize',
  386: 					   ['\tiny','\scriptsize',
  387: 					    '\footnotesize','\small',
  388: 					    '\normalsize','\large','\Large',
  389: 					    '\LARGE','\huge','\Huge'],
  390: 					   $token);
  391: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  392:     } elsif ($target eq 'modified') {
  393: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  394: 						     $safeeval,'name','type',
  395: 						     'TeXsize');
  396: 	if ($constructtag) {
  397: 	    $result = &Apache::edit::rebuild_tag($token);
  398: 	}
  399:     }
  400:     return $result;
  401: }
  402: 
  403: 
  404: 
  405: sub get_label_width {
  406:     my $label         = shift;
  407:     &Apache::lonxml::debug("image label = $label");
  408:     if (-e $label) {
  409: 	&Apache::lonxml::debug("$label exists");
  410:     } else {
  411: 	&Apache::lonxml::debug("$label does not exist");
  412:     }
  413:     my $magick        = Image::Magick->new;
  414:     $magick->Read($label);
  415:     my $pixel_width   = $magick->Get('width');
  416:     return $pixel_width * $scale_factor;
  417:     
  418: 	
  419: }
  420: 
  421: sub get_label_height {
  422:     my $label         = shift;
  423:     &Apache::lonxml::debug("image label = $label");
  424:     if (-e $label) {
  425: 	&Apache::lonxml::debug("$label exists");
  426:     } else {
  427: 	&Apache::lonxml::debug("$label does not exist");
  428:     }
  429:     my $magick        = Image::Magick->new;
  430:     $magick->Read($label);
  431:     my $pixel_height   = $magick->Get('height');
  432:     return $pixel_height * $scale_factor;
  433: }
  434: 
  435: sub add_vars {
  436:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
  437:     if (!defined($name) || $name eq '') { return; }
  438:     my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
  439:     my $out=Apache::run::run($code,$safeeval);
  440:     if ($value) {
  441: 	$code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
  442: 	$out=Apache::run::run($code,$safeeval);
  443: 	$code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
  444: 	$out=Apache::run::run($code,$safeeval);
  445:     }
  446:     if ($image) {
  447: 	my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
  448: 	my $out=Apache::run::run($code,$safeeval);
  449:     }
  450:     $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
  451:     $out=Apache::run::run($code,$safeeval);
  452: }
  453: 
  454: 
  455: 
  456: sub end_labelgroup {
  457:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  458:     my $gname = $Apache::randomlabel::groupname;
  459:     my $type  = $Apache::randomlabel::type;
  460:     my $result='';
  461:     if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
  462: 	$target eq 'analyze') {
  463: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  464: 	&Apache::structuretags::shuffle(\@idx_arr);
  465: 	for(0 .. $#Apache::randomlabel::label_arr) {
  466: 	    my $str;
  467: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
  468: 	    my $x = $Apache::randomlabel::xcoord[$_];
  469: 	    my $y = $Apache::randomlabel::ycoord[$_];
  470: 	    my $value = $Apache::randomlabel::value[$_];
  471: 	    my $i=$Apache::randomlabel::obj_cnt++;
  472: 	    if( $type eq 'text') {
  473: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
  474: 		$str = join(':',$x,$y,&escape($label));
  475: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
  476: 	    } elsif ( $type eq 'image') {
  477: 		&add_vars($gname,$_,
  478: 			  $Apache::randomlabel::description[$idx_arr[$_]],
  479: 			  $idx_arr[$_],$value,$label,$safeeval);
  480: 		$str = join(':',$x,$y,&escape($label));
  481: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
  482: 	    } else {
  483: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  484: 	    }
  485: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
  486: 	}
  487:     } elsif ($target eq 'tex') {
  488: 	my $WX1=0; #  Web x-coord. of upper left corner (ULC)
  489: 	my $WY1=0; #  Web y-coord. of (ULC)
  490: 	my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
  491: 	my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
  492: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
  493: 	if (!defined($TeXsize)) { $TeXsize='\\normalsize'; }
  494: 	
  495: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  496: 	&Apache::structuretags::shuffle(\@idx_arr);
  497: 
  498: 	&Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
  499: 	$Apache::lonxml::debug = 0;
  500: 	for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
  501: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
  502: 	    my $x = $Apache::randomlabel::xcoord[$i];
  503: 	    my $y = $Apache::randomlabel::ycoord[$i];
  504: 	    if ( $type eq 'text' ) {
  505: 		# FIXME the 3.5 here is the 'height' of the letter in TeX
  506: 		$y=$y-3.5;
  507: 	    }
  508: 	    &Apache::lonxml::debug("initially: x= $x y= $y");
  509: 	    my $value = $Apache::randomlabel::value[$i];
  510: 	    #x latex coordinate
  511: 	    my $tcX=($x)*($width_param/$wwidth);
  512: 	    &Apache::lonxml::debug("wparam = $width_param wwidth = $wwidth, texx = $tcX");
  513: 	    #y latex coordinate
  514:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
  515: 	    my $tcY=$height_param-$y*($height_param/$wheight);
  516: 	    if ( $type eq 'image') {
  517: 		my $label_height = &get_label_height($label);
  518: 		$tcY=$tcY-$label_height;
  519: 	    }
  520: 
  521: 	    &Apache::lonxml::debug("hparam = $height_param wheight = $wheight texy = $tcY");
  522: 	    $tcX=sprintf('%.2f',$tcX);
  523: 	    $tcY=sprintf('%.2f',$tcY);
  524: 	    $result .= '\put('.$tcX.','.$tcY.'){';
  525: 	    if( $type eq 'text') {
  526: 		$result.= $TeXsize.' \bf '.$label."}\n";
  527: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
  528: 	    } elsif ( $type eq 'image') {
  529: 		my ($path,$file) = &Apache::londefdef::get_eps_image($label);
  530: 		my $image_name = $path.$file;
  531: 		#
  532: 		#  Note that spaces in e.. \includegraphics cause problems for Latex
  533: 		# so they get replaced by _'s by lonprintout/printout and us:
  534: 		#
  535: 		my $label_width = &get_label_width($label);
  536: 
  537: 		$result .=  '\includegraphics[width='.$label_width.'mm]{'
  538: 		            .$image_name."}}\n";
  539: 		&add_vars($gname,$i,
  540: 			  $Apache::randomlabel::description[$idx_arr[$i]],
  541: 			  $idx_arr[$i],$value,$label,$safeeval);
  542: 	    } else {
  543: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
  544: 	    }
  545: 	}
  546: 	$Apache::lonxml::debug =0;
  547:     } elsif ($target eq 'edit') {
  548: 	$result.=&Apache::edit::end_table;
  549:     }
  550:     return $result;
  551: }
  552: 
  553: # <location x="123" y="456" value="some value"/>
  554: sub start_location {
  555:     $Apache::lonxml::debug = 0;
  556:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  557:     my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
  558:     my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
  559:     &Apache::lonxml::debug("x = $x y = $y");
  560:     $x = $x*$label_xscale;
  561:     $y = $y*$label_yscale;
  562:     &Apache::lonxml::debug(" H = $height_param W = $width_param");
  563:     &Apache::lonxml::debug(" XS = $label_xscale YS = $label_yscale");
  564:     &Apache::lonxml::debug(" X  = $x Y = $y");
  565:     my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
  566:     my $result='';
  567:     push(@Apache::randomlabel::xcoord,$x);
  568:     push(@Apache::randomlabel::ycoord,$y);
  569:     push(@Apache::randomlabel::value,$value);
  570:     if ($target eq 'edit') {
  571: 	$result.=&Apache::edit::tag_start($target,$token);
  572: 	$result.=&Apache::edit::text_arg('X:','x',$token,4).
  573: 	    &Apache::edit::text_arg('Y:','y',$token,4).
  574: 	    &Apache::edit::entercoords('x','y','attribute','width','height').'&nbsp;&nbsp;&nbsp;'.
  575: 	    &Apache::edit::text_arg('Value:','value',$token).
  576: 	    &Apache::edit::end_row();
  577: 	$result.=&Apache::edit::end_table;
  578:     } elsif ($target eq 'modified') {
  579: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  580: 						    $safeeval,'x','y','value');
  581: 	if ($constructtag) {
  582: 	    $result = &Apache::edit::rebuild_tag($token);
  583: 	}
  584:     }
  585:     $Apache::lonxml::debug = 0;
  586:     return $result;
  587: }
  588: 
  589: sub end_location {
  590:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  591:     my @result;
  592:     if ($target eq 'edit') { @result=('','no') }
  593:     return @result;
  594: }
  595: 
  596: # <label>$var_abc</label>
  597: sub insert_label {
  598:     my ($after) = @_;
  599:     my $depth = scalar(@Apache::lonxml::depthcounter);
  600:     $depth-- if ($after);
  601:     my $inset = "\t"x$depth;
  602:     return "\n$inset<label></label>";
  603: }
  604: 
  605: sub start_label {
  606:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  607:     my $result='';
  608:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  609:     if ($target eq 'web' || $target eq 'tex' ||
  610: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
  611: 	&Apache::lonxml::startredirection; 
  612:     } elsif ($target eq 'edit') {
  613: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
  614: 	my $text=&Apache::lonxml::get_all_text("/label",$parser,$style);
  615: 	if ($type eq 'image') {
  616: 	    $result.=&Apache::edit::end_row().
  617: 		&Apache::edit::start_spanning_row();
  618: 	    $result.=&Apache::edit::text_arg('Description:','description',
  619: 					     $token);
  620: 	}
  621: 	if ($type eq 'text') { $result.="Label Text:"; }
  622: 	if ($type eq 'image') { $result.="Path to image:&nbsp;"; }
  623: 	$result.=&Apache::edit::editline('',$text,'',50).
  624: 	    &Apache::edit::end_table();
  625:     } elsif ($target eq 'modified') {
  626: 	$result = '<label>';
  627: 	if ($type eq 'image') {
  628: 	    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  629: 							 $safeeval,
  630: 							 'description');
  631: 	    if ($constructtag) {
  632: 		$result = &Apache::edit::rebuild_tag($token);
  633: 	    } else {
  634: 		$result = $token->[4];
  635: 	    }
  636: 	}
  637: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  638:     }
  639:     return $result;
  640: }
  641: 
  642: sub end_label {
  643:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  644:     my @result;
  645:     if ($target eq 'edit') {
  646: 	@result=('','no') ;
  647:     } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  648: 	     $target eq 'answer' || $target eq 'analyze') {
  649: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
  650: 	my $ltext=&Apache::lonxml::endredirection; 
  651: 	if ($type eq 'image') {
  652: 	    if ($target eq 'tex') {
  653: 		# For tex targets, our image url has been potentially corrupted
  654: 		# by prepending \'s in front of special latex symbols.
  655: 		# For now we only worry about the _ case (most common?)
  656: 		# There's a whole host of theim in lonxml::latex_special_symbols
  657: 		# that could potentially have to be re-done.
  658: 
  659: 		$ltext =~ s/\\_/_/g;
  660: 	    }
  661: 	    &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
  662: 	    $ltext=&Apache::imageresponse::clean_up_image($ltext);
  663: #	    $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
  664: #						 $ltext);
  665: 	    &Apache::lonxml::debug("into $ltext");
  666: 	    my $description = &Apache::lonxml::get_param('description',
  667: 							 $parstack,$safeeval);
  668: 	    push(@Apache::randomlabel::description,$description);
  669: 	} else {
  670: 	    $ltext=~s/[\r\n]*//gs
  671: 	}
  672: 	push(@Apache::randomlabel::label_arr,$ltext);
  673:     }
  674:     return @result;
  675: }
  676: 
  677: 1;
  678: __END__
  679:  

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