File:  [LON-CAPA] / loncom / homework / convertjme.pl
Revision 1.7: download - view: text, annotated - select for diffs
Mon Oct 20 16:14:08 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-integrating Guy A. changes

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

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