File:  [LON-CAPA] / loncom / homework / convertjme.pl
Revision 1.8: download - view: text, annotated - select for diffs
Mon Oct 20 16:25:33 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_1_0_99, HEAD
- add header to quickly switch between Lon-CAPA and standalone mode

    1: #!/usr/bin/perl
    2: # The LearningOnline Network with CAPA
    3: # Dynamically converts JME strings into either a png or ps file.
    4: #
    5: # $Id: convertjme.pl,v 1.8 2003/10/20 16:25:33 albertel Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: # Coded by Guy Ashkenazi, guy@fh.huji.ac.il
   29: # Based on the work of Peter Ertl, peter.ertl@pharma.novartis.com
   30: 
   31: use strict;
   32: 
   33: ### FOR LON-CAPA set $loncapa to 1 and uncomment both uses
   34: ### For standalone operation, set $loncapa to 0, and comment out both uses
   35: my $loncapa=1;
   36: use lib '/home/httpd/lib/perl';
   37: use LONCAPA::loncgi();
   38: 
   39: 
   40: use GD;
   41: use PostScript::Simple;
   42: 
   43: if ($loncapa) {
   44:     if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   45: 	print <<END;
   46: Content-type: text/html
   47: 
   48: <html>
   49: <head><title>Bad Cookie</title></head>
   50: <body>
   51: Your cookie information is incorrect. 
   52: </body>
   53: </html>
   54: END
   55:         exit;
   56:     }
   57: }
   58: 
   59: sub unescape {
   60:     my $str=shift;
   61:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   62:     return $str;
   63: }
   64: 
   65: # read the width and the JME string from the cgi query
   66: my ($id,$width,$ps,$png,@JMEstring);
   67: if ($loncapa) {
   68:     $id=$ENV{'QUERY_STRING'};
   69:     $width = $ENV{'cgi.'.$id.'.WIDTH'};
   70:     if (!$width) { $width = 400; }
   71:     $png = $ENV{'cgi.'.$id.'.PNG'};
   72:     $ps = $ENV{'cgi.'.$id.'.PS'};
   73:     @JMEstring=&unescape($ENV{'cgi.'.$id.'.JME'});
   74: } else {
   75:     my @JMEstring = @ARGV;
   76:     $width = shift @JMEstring;
   77:     $png = 1;
   78:     $ps = 1;
   79: }
   80: 
   81: #get objects
   82: my ($reactants,$modulators,$products)=split('>',$JMEstring[0]);
   83: 
   84: my @reactant_structs=split(/\|/,$reactants);
   85: my @modulator_structs=split(/\|/,$modulators);
   86: my @product_structs=split(/\|/,$products);
   87: 
   88: 
   89: 
   90: my @all_structs=(@reactant_structs,@modulator_structs,@product_structs);
   91: 
   92: #get size of image and initialize image and globals
   93: my ($xmin,$xmax,$ymin,$ymax,$maxName,$height,$scale) =
   94:     &determine_size(@all_structs);
   95: 
   96: my $draw_arrow;
   97: if (@modulator_structs || @product_structs) { $draw_arrow=1; }
   98: my ($arrow_x1,$arrow_x2,$arrow_y) = (-1e20,1e20,0);
   99: if ($draw_arrow) {
  100:     foreach my $struct (@reactant_structs) {
  101: 	my @bounds =  &determine_size($struct);
  102: 	if ($arrow_x1 < $bounds[1]) {
  103: 	    $arrow_x1 = $bounds[1];
  104: 	    $arrow_y = ($bounds[2] + $bounds[3]) / 2;
  105: 	}
  106:     }
  107:     foreach my $struct (@product_structs) {
  108: 	my @bounds =  &determine_size($struct);
  109: 	$arrow_x2 = $bounds[0] if ($arrow_x2 > $bounds[0]);
  110:     }
  111: 
  112:     $arrow_x1 += (1.5+$maxName/2-$xmin);
  113:     $arrow_x1 *= $scale;
  114:     $arrow_x2 += (1.5+$maxName/2-$xmin);
  115:     $arrow_x2 *= $scale; 
  116:     $arrow_y += (1.0-$ymin);
  117:     $arrow_y *= $scale;
  118: }
  119: 
  120: # Create a new PostScript object
  121: my ($im,$white,$black,$gray);
  122: my $gdAntiAliased;
  123: if ($png) {
  124:     $im = new GD::Image($width,$height); 
  125:     $white = $im->colorAllocate(255,255,255);
  126:     $black = $im->colorAllocate(0,0,0);
  127:     $gray = $im->colorAllocate(200,200,200);
  128:     $gdAntiAliased = $im->colorAllocate(1,1,1);
  129:     # $im->setAntiAliased($black);
  130: } elsif ($ps) {
  131:     $im = new PostScript::Simple(xsize => $xmax-$xmin+3+$maxName,
  132: 				 ysize => $ymax-$ymin+2,
  133: 				 clip => 1,
  134: 				 eps => 1,
  135: 				 color => 0,
  136: 				 units => "cm");
  137: }
  138: 
  139: my %valence = ("C",4,"N",3,"P",3,"O",2,"S",2);
  140: my %font_width = (" ",250,"+",564,"-",500,"0",500,"1",500,"2",500,"3",500,"4",500,"5",500,"6",500,"7",500,"8",500,"9",500,"A",722,"B",667,"C",667,"D",722,"E",611,"F",556,"G",722,"H",722,"I",333,"J",389,"K",722,"L",611,"M",889,"N",722,"O",722,"P",556,"Q",722,"R",667,"S",556,"T",611,"U",722,"V",722,"W",944,"X",722,"Y",722,"Z",611,"a",444,"b",500,"c",444,"d",500,"e",444,"f",333,"g",500,"h",500,"i",278,"j",278,"k",500,"l",278,"m",778,"n",500,"o",500,"p",500,"q",500,"r",333,"s",389,"t",278,"u",500,"v",500,"w",722,"x",500,"y",500,"z",444);
  141: my $font = '/usr/share/fonts/default/Type1/n021003l.pfb';
  142: my $pointsize = 20;
  143: my ($ptsize,@bounds);
  144: if ($png) {
  145:     @bounds = GD::Image->stringTTF($black,$font,100,0,0,0,"H");
  146:     $ptsize = 100*0.662*$pointsize*(2.54/72)*$scale/(@bounds[3]-@bounds[5]);
  147: }
  148: 
  149: #set bond sizes
  150: my $doubleWidth;
  151: my $tripleWidth;
  152: $doubleWidth = 0.10*$scale;
  153: $tripleWidth = 0.15*$scale;
  154: 
  155: # Draw arrow
  156: 
  157: if ($draw_arrow) {
  158:     my $dx = $arrow_x2 - $arrow_x1;
  159:     if ($png) {
  160: 	$im->line($arrow_x1+0.25*$dx,$height-$arrow_y,
  161: 		  $arrow_x2-0.25*$dx,$height-$arrow_y,
  162: 		  $gdAntiAliased);
  163: 	$im->line($arrow_x2-0.25*$dx,$height-$arrow_y,
  164: 		  $arrow_x2-0.25*$dx-fm2cm(500),$height-$arrow_y-fm2cm(300),
  165: 		  $gdAntiAliased);
  166: 	$im->line($arrow_x2-0.25*$dx,$height-$arrow_y,
  167: 		  $arrow_x2-0.25*$dx-fm2cm(500),$height-$arrow_y+fm2cm(300),
  168: 		  $gdAntiAliased);
  169: 	
  170:     } elsif ($ps) {
  171: 	$im->line($arrow_x1+0.25*$dx,$arrow_y,
  172: 		  $arrow_x2-0.25*$dx,$arrow_y);
  173: 	$im->line($arrow_x2-0.25*$dx,$arrow_y,
  174: 		  $arrow_x2-0.25*$dx-fm2cm(500),$arrow_y-fm2cm(250));
  175: 	$im->line($arrow_x2-0.25*$dx,$arrow_y,
  176: 		  $arrow_x2-0.25*$dx-fm2cm(500),$arrow_y+fm2cm(250));
  177:     }
  178: }
  179: 
  180: 
  181: foreach my $struct (@all_structs) {
  182: 
  183:     my (@name,@x,@y,@atomA,@atomB,@bondType,$natoms,$nbonds);
  184:     &parse_struct($struct,\@name,\@x,\@y,\@atomA,\@atomB,\@bondType);
  185:     $natoms=scalar(@x);
  186:     $nbonds=scalar(@bondType);
  187: 
  188: # Scale and move lower left corner to (1.5,1.0)
  189: 
  190:     for (my $i = 0; $i < $natoms; $i++) {
  191: 	$x[$i] += (1.5+$maxName/2-$xmin);
  192: 	$x[$i] *= $scale; 
  193: 	$y[$i] += (1.0-$ymin);
  194: 	$y[$i] *= $scale;
  195:     }
  196:     
  197: # Count bonds
  198: 
  199:     my @bonds = map {0} 0..$natoms-1;
  200:     my @adjacent = map {0} 0..$natoms-1;
  201:     my @bondsx = map {0} 0..$natoms-1;
  202:     my @bondsy = map {0} 0..$natoms-1;
  203:     for (my $i = 0; $i < $nbonds; $i++) {
  204: 	$bonds[$atomA[$i]] += ($bondType[$i]>0) ? $bondType[$i] : 1;
  205: 	$bonds[$atomB[$i]] += ($bondType[$i]>0) ? $bondType[$i] : 1;
  206: 
  207: 	$adjacent[$atomA[$i]]++;
  208: 	$adjacent[$atomB[$i]]++;
  209:     
  210: 	$bondsx[$atomA[$i]] += $x[$atomB[$i]] - $x[$atomA[$i]];
  211: 	$bondsy[$atomA[$i]] += $y[$atomB[$i]] - $y[$atomA[$i]];
  212: 	$bondsx[$atomB[$i]] += $x[$atomA[$i]] - $x[$atomB[$i]];
  213: 	$bondsy[$atomB[$i]] += $y[$atomA[$i]] - $y[$atomB[$i]];
  214:     }
  215: 
  216: # Draw bonds
  217:     for (my $i = 0; $i < $nbonds; $i++) {
  218: 	my $xa = $x[$atomA[$i]];
  219: 	my $ya = $y[$atomA[$i]];
  220: 	my $xb = $x[$atomB[$i]];
  221: 	my $yb = $y[$atomB[$i]];
  222: 
  223: 	my ($sina,$cosa,$dx,$dy);
  224: 	if ($bondType[$i] != 1) {
  225: 	    $dx = $xb-$xa;
  226: 	    $dy = $yb-$ya;
  227: 	    my $dd = sqrt($dx*$dx + $dy*$dy);
  228: 	    $sina=$dy/$dd;
  229: 	    $cosa=$dx/$dd;
  230: 	}
  231: 	if    ($bondType[$i] == -2) {
  232: 	    for (my $t = 0; $t <= 1; $t += 0.1) {
  233: 		my $xab = $xa + $t*$dx; 
  234: 		my $yab = $ya + $t*$dy; 
  235: 		my $xperp = $tripleWidth*$sina*$t;
  236: 		my $yperp = $tripleWidth*$cosa*$t;
  237: 		if ($png) {
  238: 		    $im->line($xab+$xperp,$height-($yab-$yperp),
  239: 			      $xab-$xperp,$height-($yab+$yperp),
  240: 			      $gdAntiAliased);
  241: 		} elsif ($ps) {
  242: 		    $im->line($xab+$xperp,$yab-$yperp,$xab-$xperp,$yab+$yperp);
  243: 		}
  244: 	    }
  245: 	}
  246: 	elsif ($bondType[$i] == -1) {
  247: 	    my $xperp = $tripleWidth*$sina;
  248: 	    my $yperp = $tripleWidth*$cosa;
  249: 	    if ($png) {
  250: 		my $poly = new GD::Polygon;
  251: 		$poly->addPt($xa,$height-$ya);
  252: 		$poly->addPt($xb+$xperp,$height-($yb-$yperp));
  253: 		$poly->addPt($xb-$xperp,$height-($yb+$yperp));
  254: 		$im->filledPolygon($poly,$black);
  255: 	    } elsif ($ps) {
  256: 		$im->polygon({filled=>1},
  257: 			     $xa,$ya,
  258: 			     $xb+$xperp,$yb-$yperp,
  259: 			     $xb-$xperp,$yb+$yperp);
  260: 	    }
  261: 	}
  262: 	elsif ($bondType[$i] == 1) {
  263: 	    if ($png) {
  264: 		$im->line($xa,$height-$ya,$xb,$height-$yb,$gdAntiAliased);
  265: 	    } elsif ($ps) {
  266: 		$im->line($xa,$ya,$xb,$yb);
  267: 	    }
  268: 	}
  269: 	elsif ($bondType[$i] == 2 &&
  270: 	       (($adjacent[$atomA[$i]] == 1 && $adjacent[$atomB[$i]] > 2)||
  271: 		($adjacent[$atomB[$i]] == 1 && $adjacent[$atomA[$i]] > 2))) {
  272: 	    # centered bond
  273: 	    my $xperp = $doubleWidth*$sina;
  274: 	    my $yperp = $doubleWidth*$cosa;
  275: 	    if ($png) {
  276: 		$im->line($xa+$xperp,$height-($ya-$yperp),
  277: 			  $xb+$xperp,$height-($yb-$yperp),
  278: 			  $gdAntiAliased);
  279: 		$im->line($xa-$xperp,$height-($ya+$yperp),
  280: 			  $xb-$xperp,$height-($yb+$yperp),
  281: 			  $gdAntiAliased);
  282: 	    } elsif ($ps) {
  283: 		$im->line($xa+$xperp,$ya-$yperp,$xb+$xperp,$yb-$yperp);
  284: 		$im->line($xa-$xperp,$ya+$yperp,$xb-$xperp,$yb+$yperp);
  285: 	    }
  286: 	}
  287: 	elsif ($bondType[$i] == 2) {
  288: 	    my $xperp = 2*$doubleWidth*$sina;
  289: 	    my $yperp = 2*$doubleWidth*$cosa;
  290: 	    if ($png) {
  291: 		$im->line($xa,$height-$ya,$xb,$height-$yb,$gdAntiAliased);
  292: 		$im->line($xa+0.1*$dx-$xperp,$height-($ya+0.1*$dy+$yperp),
  293: 			  $xb-0.1*$dx-$xperp,$height-($yb-0.1*$dy+$yperp),
  294: 			  $gdAntiAliased);
  295: 	    } elsif ($ps) {
  296: 		$im->line($xa,$ya,$xb,$yb);
  297: 		$im->line($xa+0.1*$dx-$xperp,$ya+0.1*$dy+$yperp,
  298: 			  $xb-0.1*$dx-$xperp,$yb-0.1*$dy+$yperp);
  299: 	    }
  300: 	}
  301: 	elsif ($bondType[$i] == 3) {
  302: 	    my $xperp = $tripleWidth*$sina;
  303: 	    my $yperp = $tripleWidth*$cosa;
  304: 	    if ($png) {
  305: 		$im->line($xa,$height-$ya,$xb,$height-$yb,$gdAntiAliased);
  306: 		$im->line($xa+$xperp,$height-($ya-$yperp),
  307: 			  $xb+$xperp,$height-($yb-$yperp),
  308: 			  $gdAntiAliased);
  309: 		$im->line($xa-$xperp,$height-($ya+$yperp),
  310: 			  $xb-$xperp,$height-($yb+$yperp),
  311: 			  $gdAntiAliased);
  312: 	    } elsif ($ps) {
  313: 		$im->line($xa,$ya,$xb,$yb);
  314: 		$im->line($xa+$xperp,$ya-$yperp,$xb+$xperp,$yb-$yperp);
  315: 		$im->line($xa-$xperp,$ya+$yperp,$xb-$xperp,$yb+$yperp);
  316: 	    }
  317: 	}   
  318:     }
  319: 
  320: # Write labels
  321: 
  322:     for (my $i = 0; $i < $natoms; $i++) {
  323: 	my ($formula,$sign,$charge) =
  324: 	    ($name[$i] =~ /(\w+)([\+|\-])?(\d)?/);
  325: 	if ($png) {
  326: 	    $sign = "&#8211;" if ($sign eq "-");  # replace by n-dash
  327: 	}
  328: 	if ($formula ne "C" || $sign ne ""||
  329: 	    $adjacent[$i] < 2 || ($adjacent[$i] == 2 && $bonds[$i] == 4)) {
  330: 	    # don't show C, unless charged, terminal, or linear
  331: 	    my $nH = 0;
  332: 	    if (exists $valence{$formula}) {
  333: 		$nH = $valence{$formula} - $bonds[$i];
  334: 		$nH += (($charge eq "")? 1 : $charge) if ($sign eq "+");
  335: 		$nH -= (($charge eq "")? 1 : $charge) if ($sign eq "-");
  336: 	    }
  337: 	    $formula .= "H" if ($nH > 0);
  338: 	    $formula .= $nH if ($nH > 1);
  339: 	    my @formula = $formula=~ /[A-Z][a-z]?\d*/g;
  340: 	    
  341: 	    my $PI = 3.1415;
  342: 	    my $bondAngle;
  343: 	    if (abs($bondsy[$i]) < 0.01 && abs($bondsx[$i]) < 0.01) {
  344: 		$bondAngle = -$PI;
  345: 	    }
  346: 	    else {
  347: 		$bondAngle = atan2($bondsy[$i],$bondsx[$i]);
  348: 	    }
  349: 
  350: 	    my $direction;
  351: 	    if ($adjacent[$i] < 2) {
  352: 		$direction = ($bondsx[$i] < 0.01) ? "r" : "l";
  353: 	    }
  354: 	    else {
  355: 		if  ($bondAngle >= -$PI/4 && $bondAngle <= $PI/4) {
  356: 		    $direction = "l";
  357: 		}
  358: 		elsif ($bondAngle > $PI/4 && $bondAngle < 3*$PI/4) {
  359: 		    $direction = "d";
  360: 		}
  361: 		elsif ($bondAngle < -$PI/4 && $bondAngle > -3*$PI/4) {
  362: 		    $direction = "u";
  363: 		}
  364: 		else {
  365: 		    $direction = "r";
  366: 		}
  367: 	    }
  368: 		
  369: 	    if ($direction eq "r") {  # direction = right
  370: 		$formula[0] =~ /([A-Z][a-z]?)(\d*)/;
  371: 		my $carrige = $x[$i]-stringWidth($1)/2;
  372: 		foreach (@formula) {
  373: 		    $_ =~ /([A-Z][a-z]?)(\d*)/;
  374: 		    $carrige = printElement ($1,$2,$carrige,$y[$i]);
  375: 		}
  376: 		printCharge ($sign,$charge,$carrige,$y[$i]) if ($sign ne ""); 
  377: 	    }
  378: 	    elsif ($direction eq "l") {  # direction = left, reverse hydrogens
  379: 		$formula[0] =~ /([A-Z][a-z]?)(\d*)/;
  380: 		my $carrige = $x[$i]+
  381: 		    stringWidth($1)/2+stringWidth($2)-stringWidth($formula);
  382: 		foreach (reverse @formula) {
  383: 		    $_ =~ /([A-Z][a-z]?)(\d*)/;
  384: 		    $carrige = printElement ($1,$2,$carrige,$y[$i]);
  385: 		}
  386: 		printCharge ($sign,$charge,$carrige,$y[$i]) if ($sign ne ""); 
  387: 	    }
  388: 	    elsif ($direction eq "u") { # direction = up
  389: 		(shift @formula) =~ /([A-Z][a-z]?)(\d*)/;
  390: 		my $carrige = $x[$i]-stringWidth($1)/2;
  391: 		$carrige = printElement ($1,$2,$carrige,$y[$i]);
  392: 		my $y = (@formula > 0) ? $y[$i] + fm2cm(800) : $y[$i];
  393: 		$carrige =
  394: 		    (@formula > 0) ? $x[$i]-stringWidth($1)/2 : $carrige;
  395: 		foreach (@formula) {
  396: 		    $_ =~ /([A-Z][a-z]?)(\d*)/;
  397: 		    $carrige = printElement ($1,$2,$carrige,$y);
  398: 		}
  399: 		printCharge ($sign,$charge,$carrige,$y) if ($sign ne ""); 
  400: 	    }
  401: 	    else { # direction = down
  402: 		(shift @formula) =~ /([A-Z][a-z]?)(\d*)/;
  403: 		my $carrige = $x[$i]-stringWidth($1)/2;
  404: 		$carrige = printElement ($1,$2,$carrige,$y[$i]);
  405: 		my $y = (@formula > 0) ? $y[$i] + fm2cm(-800) : $y[$i];
  406: 		$carrige =
  407: 		    (@formula > 0) ? $x[$i]-stringWidth($1)/2 : $carrige;
  408: 		foreach (@formula) {
  409: 		    $_ =~ /([A-Z][a-z]?)(\d*)/;
  410: 		    $carrige = printElement ($1,$2,$carrige,$y);
  411: 		}
  412: 		printCharge ($sign,$charge,$carrige,$y) if ($sign ne ""); 
  413: 	    }
  414: 	}
  415:     }
  416: }
  417: 
  418: if ($loncapa) {
  419:     if ($png) {
  420: # make sure we are writing to a binary stream
  421: 	binmode STDOUT;
  422: 
  423: # Convert the image to PNG and print it on standard output
  424: 	print "Content-type: image/png\n\n";
  425: 	print $im->png;
  426:     } elsif ($ps) {
  427: 	my $psfile = "/home/httpd/perl/tmp/".$id.'.eps';
  428: 	$im->output($psfile);
  429: 	print "Content-type: text/html\n\n";
  430: 	print (<<HTML)
  431: 	    <html>
  432: 	    <body>
  433: 	    Wrote eps file $psfile
  434: 	    </body>
  435: 	    </html>
  436: HTML
  437:     }
  438: } else {
  439:     if ($png) {
  440: # make sure we are writing to a binary stream
  441: 	binmode STDOUT;
  442: # Convert the image to PNG and print it on standard output
  443: 	print $im->png;
  444:     } elsif ($ps) {
  445: 	$im->output("file.ps");
  446:     }
  447: }
  448: 
  449: sub stringWidth {
  450:     my ($string) = @_;
  451:     my $width = 0;
  452:     while ($string =~ /[A-Za-z]/g) {
  453: 	if ($png) {
  454: 	    my @bounds = GD::Image->stringTTF($black,$font,$ptsize,0,0,0,$&);
  455: 	    $width += $bounds[2]-$bounds[0]+2;
  456: 	} elsif ($ps) {
  457: 	    $width += fm2cm($font_width{$&});
  458: 	}
  459:     }
  460:     while ($string =~ /[\d+-]/g) {
  461: 	if ($png) {
  462: 	    my @bounds=GD::Image->stringTTF($black,$font,0.6*$ptsize,0,0,0,$&);
  463: 	    $width += $bounds[2]-$bounds[0]+2;
  464: 	} elsif ($ps) {
  465: 	    $width += fm2cm(0.6*$font_width{$&});
  466: 	}
  467:     }
  468:     
  469:     return $width;
  470: }
  471: 
  472: sub fm2cm {  #font metrics to cm
  473:     my ($fm) = @_;
  474:     return $scale*(2.54/72)*$pointsize*$fm/1000;
  475: }
  476: 
  477: sub printElement {
  478:     if ($png) {
  479: 	return &printElement_png(@_);
  480:     } elsif ($ps) {
  481: 	return &printElement_ps(@_);
  482:     }
  483: }
  484: 
  485: sub printElement_png {  #element symbol + optional subscript
  486:     my ($element,$subscript,$x,$y) = @_;
  487:     my $yy = 662;
  488: 
  489:     my @bounds = GD::Image->stringTTF($black,$font,$ptsize,0,
  490: 				   $x,$height-($y+fm2cm(-$yy/2)),$element);
  491:     $im->filledRectangle(
  492: 			 $bounds[6]-1,$bounds[7]-fm2cm(135),
  493: 			 $bounds[2]+1,$bounds[3]+fm2cm(135),$white);
  494: 
  495:     $im->stringTTF($black,$font,$ptsize,0,
  496: 		   $x,$height-($y+fm2cm(-$yy/2)),$element);
  497:     $x = $bounds[2] + 1;
  498: 
  499:     if ($subscript ne "") {
  500: 	@bounds = GD::Image->stringTTF($black,$font,0.6*$ptsize,0,
  501: 	   $x,$height-($y+fm2cm(-0.8*$yy)),$subscript);
  502: 	$im->filledRectangle(
  503: 			     $bounds[6]-1,$bounds[7]-fm2cm(45),
  504: 			     $bounds[2]+1,$bounds[3]+fm2cm(45),$white);
  505: 	$im->stringTTF($black,$font,0.6*$ptsize,0,
  506: 				 $x,$height-($y+fm2cm(-0.8*$yy)),$subscript);
  507:     }
  508:     $x = $bounds[2] + 1;
  509: }
  510: 
  511: sub printElement_ps {  #element symbol + optional subscript
  512:     my ($element,$subscript,$x,$y) = @_;
  513:     $height = 662;
  514: 
  515:     $im->setcolour("white");
  516:     $im->box({filled=>1},
  517: 	    $x+fm2cm(-30),$y+fm2cm(-$height/2-150),
  518: 	    $x+stringWidth($element)+fm2cm(50),$y+fm2cm(+$height/2+150));
  519:     $im->setcolour("black");
  520:     $im->setfont("Times-Roman",$pointsize);
  521:     $im->text($x,$y+fm2cm(-$height/2),$element);
  522:     $x += stringWidth($element);
  523: 
  524:     if ($subscript ne "") {
  525: 	$im->setcolour("white");
  526: 	$im->box({filled=>1},
  527: 		$x,$y+fm2cm(-0.8*$height-45),
  528: 		$x+stringWidth($subscript)+fm2cm(50),$y+fm2cm(-0.2*$height+45));
  529: 	$im->setcolour("black");
  530: 	$im->setfont("Times-Roman",0.6*$pointsize);
  531: 	$im->text($x,$y+fm2cm(-0.8*$height),$subscript);
  532:     }
  533:     $x += stringWidth($subscript);
  534: }
  535: 
  536: sub printCharge {
  537:     if ($png) {
  538: 	return &printCharge_png(@_);
  539:     } elsif ($ps) {
  540: 	return &printCharge_ps(@_);
  541:     }
  542: }
  543: 
  544: sub printCharge_png {
  545:     my ($sign,$charge,$x,$y) = @_;
  546:     my $yy = 662;
  547: 
  548:     $charge = "" if ($charge == 1);
  549:     $charge .= $sign;
  550:     
  551:     my @bounds = GD::Image->stringTTF($black,$font,0.6*$ptsize,0,
  552:        $x,$height-($y+fm2cm(0.2*$yy)),$charge);
  553:     $im->filledRectangle(
  554: 			 $bounds[6]-1,$bounds[7]-fm2cm(45),
  555: 			 $bounds[2]+1,$bounds[3]+fm2cm(45),$white);
  556: 
  557:     $im->stringTTF($black,$font,0.6*$ptsize,0,$x,$height-($y+fm2cm(0.2*$yy)),$charge);
  558:     $x = $bounds[2] + 1;
  559: }
  560: 
  561: sub printCharge_ps {
  562:     my ($sign,$charge,$x,$y) = @_;
  563:     $height = 662;
  564: 
  565:     $charge = "" if ($charge == 1);
  566:     $charge .= $sign;
  567:     
  568:     $im->setcolour("white");
  569:     $im->box({filled=>1},
  570: 	    $x,$y+fm2cm(0.2*$height-45),
  571: 	    $x+stringWidth($charge)+fm2cm(50),$y+fm2cm(0.8*$height+45));
  572: 
  573:     if ($sign eq "-") { # replace by n-dash
  574: 	chop $charge;
  575: 	$charge .= "\xb1";
  576:     }
  577:     $im->setcolour("black");
  578:     $im->setfont("Times-Roman",0.6*$pointsize);
  579:     $im->text($x,$y+fm2cm(0.2*$height),$charge);
  580:     $x += stringWidth($charge);
  581: }
  582: 
  583: sub determine_size {
  584: # Find border 
  585:     my (@all_structs)=@_;
  586:     my $xmin = my $ymin = 1e20;
  587:     my $xmax = my $ymax = -1e20;
  588:     my $maxName = 0;
  589:     foreach my $struct (@all_structs) {
  590: 	my (@name,@x,@y,@atomA,@atomB,@bondType,$natoms,$nbonds);
  591: 	&parse_struct($struct,\@name,\@x,\@y,\@atomA,\@atomB,\@bondType);
  592: 	$natoms=scalar(@x);
  593: 	$nbonds=scalar(@bondType);
  594: 	for (my $i = 0; $i < $natoms; $i++) {
  595: 	    $xmax = $x[$i] if ($x[$i] > $xmax);
  596: 	    $xmin = $x[$i] if ($x[$i] < $xmin);
  597: 	    $ymax = $y[$i] if ($y[$i] > $ymax);
  598: 	    $ymin = $y[$i] if ($y[$i] < $ymin);
  599: 	    $name[$i] =~ /(\@{1,2})?(\w+)([\+|\-])?(\d)?/;
  600: 	    $maxName = length $2 if (length $2 > $maxName);
  601: 	}
  602:     }
  603:     $maxName = ($maxName-3 < 0) ? 0 : $maxName-3;
  604: 
  605:     my $scale;
  606:     if ($png) {
  607: 	$scale = $width / ($xmax-$xmin+3+$maxName);
  608:     } elsif ($ps) {
  609: 	$scale = 1;
  610:     }
  611:     my $height = $scale * ($ymax-$ymin+2);
  612: 
  613:     return ($xmin,$xmax,$ymin,$ymax,$maxName,$height,$scale);
  614: 
  615: }
  616: 
  617: sub parse_struct {
  618:     my ($struct,$name,$x,$y,$atomA,$atomB,$bondType)=@_;
  619:     $struct=~s/^\s*//;
  620:     $struct=~s/\s*$//;
  621:     my @JMEstring = split(/ +/,$struct);
  622: # parse JME string
  623:     my $natoms= shift @JMEstring;
  624:     my $nbonds= shift @JMEstring;
  625:     for (my $i = 0; $i < $natoms; $i++) {
  626: 	$$name[$i] = shift @JMEstring;
  627: 	$$x[$i] = shift @JMEstring;
  628: 	$$y[$i] = shift @JMEstring;
  629:     }
  630: 
  631:     for (my $i = 0; $i < $nbonds; $i++) {
  632: 	$$atomA[$i] = (shift @JMEstring)-1;
  633: 	$$atomB[$i] = (shift @JMEstring)-1;
  634: 	$$bondType[$i] = shift @JMEstring;
  635:     }
  636: }
  637: 

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