render.in 11.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/perl -w

#
# Convert an outputed neato file into a png image
# (does intelligent mashing/expanding)
#
# takes neato output file on stdin, spits png image out on stdout.
#

use GD;
Chad Barb's avatar
Chad Barb committed
11
use Getopt::Std;
Chad Barb's avatar
   
Chad Barb committed
12
13
14
15
16
#
# Configure variables
#
my $TB		= "@prefix@";
my $ICONDIR     = "$TB/www";
17

Chad Barb's avatar
Chad Barb committed
18
19
20
21
22
23
my  $optlist = "z:";
if (! getopts($optlist, \%options)) {
    die("Usage:\nrender [-z zoomfactor]\n");
    
}

24
25
26
27
28
# if $embiggen == 1, node fonts will be rendered bigger.
# if $embiggen == 2, node and link fonts will be rendered bigger.

my $embiggen = 0; 

Chad Barb's avatar
Chad Barb committed
29
30
31
32
33
34
35
36
37
38
my $zoom = 1;
if ( defined($options{"z"} ) ) {
    my $zf = $options{"z"};
    if ($zf =~ /^([\.0-9]+)/) {
	$zoom = $1;
    } else {
	die("Bad argument to -z.. must be float.");
    }
}

39
40
41
if ($zoom >= 1.5) { $embiggen = 1; }
if ($zoom >= 1.75) { $embiggen = 2; }

42
43
%props = ();
%nodes = ();
Chad Barb's avatar
Chad Barb committed
44
%linklabels = ();
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@links = ();

#parse input file

while (<>) {
  if (/^\s*(\w+)\s\[([^\]]*)\]/) {
    # this line is a property set
    ($cmd, $props) = ($1, $2);
    $props =~ s/[\=\,]/ /g;

    while (($props =~ s/^\s*(\w+)\s+((\"[^\"]*\")|(\w+))\s*//)) {
	# add each property to %props
        ($k, $v) = ($1, $2);
	$v =~ s/\"//g;
	$props{$k} = $v;
#	print "property $k gets value $v\n";
    } 

    if ($cmd =~ /^node$/) {
#	print "node property $props\n";
    } elsif ($cmd =~ /^graph$/) {
#	print "graph thingee (ignored)\n";
    } else {
	# there is a name here, not "node" or "graph"
        # so it terminates the node.. store props away.
	$nodes{$cmd} = {%props}; 
	%props = ();
    }
  } elsif (/^\s*(\w+)\s\-\-\s(\w+)\s/) {
    # a link.
    ($a, $b) = ($1, $2);
Chad Barb's avatar
Chad Barb committed
76
77
78
79
80
81
    push( @links, $a . " " . $b );
    if (/label\s*\=\s*\"([^\"]*)\"/) {
	$linklabels{ "$a $b" } = $1;
    } elsif (/label\s*\=\s*(\w+)/) {
	$linklabels{ "$a $b" } = $1;
    }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  }
}


%nodepos = ();

($fontw, $fonth) = (gdSmallFont->width, gdSmallFont->height);

# load position hash for nodes

foreach $i (keys %nodes) {
    if (exists $nodes{$i}{"pos"}) {
	$s = $nodes{$i}{"pos"};
	$s =~ /^\s*(\d+)\s+(\d+)/;    
	$nodepos{ $i } = [$1, $2]; 	
    }
}

Chad Barb's avatar
   
Chad Barb committed
100
101
102
103
104
105
# "ideal" width and height

$idealw = 500;
$idealh = 500;


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
$maxX = 1000;
$minX = 0;
$maxY = 1000;
$minY = 0;

$xSquishable = 1;
$ySquishable = 1;

# while we are above the ideal width or height,
# and there exist empty rows or columns,
# take out rows or columns of whitespace.

while ((($maxX - $minX) > $idealw && $xSquishable) || 
       (($maxY - $minY) > $idealh && $ySquishable)) {

    if ($maxX - $minX > $idealw) {   
	$totalsub = 0;
	$lastvert = 0;
	foreach $j (sort {$nodepos{ $a }[0] <=> $nodepos{ $b }[0]} keys %nodepos) 
	{
	    $vert = $nodepos{ $j }[0];
	    if ($vert - $lastvert > 36) {
#		$totalsub += ($vert - $lastvert - 36);
		$totalsub += 1;
#		warn " now eating $totalsub vert\n";
	    }
	    $nodepos{ $j }[0] -= $totalsub;
	    $lastvert = $vert;
	}
    
	if ($totalsub > 0) { $xSquishable = 1; } else { $xSquishable = 0; }
    }

    if ($maxY - $minY > $idealh) {
	$totalsub = 0;
	$lastvert = 0;
	foreach $j (sort {$nodepos{ $a }[1] <=> $nodepos{ $b }[1]} keys %nodepos) 
	{
	    $vert = $nodepos{ $j }[1];
	    if ($vert - $lastvert > 36) {
#		$totalsub += ($vert - $lastvert - 36);
		$totalsub += 1;
#		warn "now eating $totalsub horiz\n";
	    }
	    $nodepos{ $j }[1] -= $totalsub;
	    $lastvert = $vert;
	}
	
	if ($totalsub > 0) { $ySquishable = 1; } else { $ySquishable = 0; }
    }

    $maxX = 0;
    $maxY = 0;
    $minX = 1000000;
    $minY = 1000000;
    
    # get min and max X and Y
    foreach $i (keys %nodepos) {
	($x, $y) = ($nodepos{ $i }[0], $nodepos{ $i }[1]);
	$halflabel = ((length( $i ) - 0.5) * $fontw) / 2;
	if ($x + $halflabel > $maxX) { $maxX = $x + $halflabel; }
	if ($y + $fonth> $maxY) { $maxY = $y + $fonth; }
	if ($x - $halflabel < $minX) { $minX = $x - $halflabel; }
	if ($y < $minY) { $minY = $y; }
    }     
}

$xbig = $maxX - $minX;
$ybig = $maxY - $minY;

$xscale = 1.0;
$yscale = 1.0;

Chad Barb's avatar
   
Chad Barb committed
179
180
181
182
183
184
185
186

# recompute new ideal.. minimum size.
$nodecount = (keys %nodepos);
$idealarea = 4200 * $nodecount;
$idealw = sqrt( $idealarea );
if ($idealw > 500) { $idealw = 500; }
$idealh = $idealw;

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# if it is too small, scale everything up so it takes the
# "ideal" amount of space

if ($xbig < $idealw) {
    $xscale = ($idealw / $xbig);
    $xbig = $idealw;
}

if ($ybig < $idealh) {
    $yscale = ($idealh / $ybig);
    $ybig = $idealh;
}

foreach $i (keys %nodepos) {
  ($x, $y) = ($nodepos{ $i }[0], $nodepos{ $i }[1]);
  $x -= $minX;
  $y -= $minY;

  $x *= $xscale;
  $y *= $yscale;

  $x += 20;
  $y += 20;
  $nodepos{ $i } = [$x,$y];	
}

Chad Barb's avatar
Chad Barb committed
213
214
215
216
217
218
219
220
# Uhh, Zoom Zip..
foreach $i (keys %nodepos) {
  ($x, $y) = ($nodepos{ $i }[0], $nodepos{ $i }[1]);
  $x *= $zoom;
  $y *= $zoom;
  $nodepos{ $i } = [$x,$y];	
}

221
222
# start constructing the image

Chad Barb's avatar
Chad Barb committed
223
$im = new GD::Image(($xbig + 60) * $zoom, ($ybig + 70) * $zoom);
224

Chad Barb's avatar
   
Chad Barb committed
225
226
$nodeicon = GD::Image->newFromPng("$ICONDIR/nodeicon.png") || warn "nodeicon.png not found";
$lanicon  = GD::Image->newFromPng("$ICONDIR/lanicon.png") || warn "lanicon.png not found";
227
228
229
230

%colors = ();

$colors{"black"} =  $im->colorAllocate(0,0,0);
Chad Barb's avatar
Chad Barb committed
231
$colors{"darkblue"} =  $im->colorAllocate(0,0,96);
232
233
$colors{"darkred"} =  $im->colorAllocate(128,0,0);
$colors{"red"} =  $im->colorAllocate(192,0,0);
234
235
236
237
238
239
240
241
242
243
244
$colors{"blue"} =  $im->colorAllocate(0,0,192);
$colors{"paleblue"} =  $im->colorAllocate(127,127,192);
$colors{"green"} =  $im->colorAllocate(0,96,0);
$colors{"orange"} =  $im->colorAllocate(255, 128, 0);
$colors{"white"} =  $im->colorAllocate(255,255,255);
$colors{"gray80"} = $im->colorAllocate(210,210,210);
$colors{"gray75"} = $im->colorAllocate(191,191,191);
$colors{"gray50"} = $im->colorAllocate(127,127,127);
$colors{"gray25"} = $im->colorAllocate(63,63,63);

# set clear background
Chad Barb's avatar
   
Chad Barb committed
245
$bgcolor = $im->colorAllocate(254, 254, 254);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

$im->transparent($bgcolor);
#$im->interlaced('true');
$im->fill( 1, 1, $bgcolor );

#$im->rectangle( 0,0,99,99, $gray50 );

# render shadows

foreach $i (keys %nodes) {
  ($x, $y) = ($nodepos{ $i }[0], $nodepos{ $i }[1]);
  $im->filledRectangle( $x - 12, $y - 12, 
 	                $x + 20, $y + 20, $colors{"gray80"});
} 

# render links

foreach $i (@links) {
  ($a, $b) = ($i =~ /(\w+)\s(\w+)/);

  ($x1, $y1) = ($nodepos{ $a }[0], $nodepos{ $a }[1]);
  ($x2, $y2) = ($nodepos{ $b }[0], $nodepos{ $b }[1]);  

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  if (exists $linklabels{ $i }) {
      my $foo   = $linklabels{ $i };
      my $red   = $colors{"red"};
      my $green = $colors{"green"};      
      my $blue  = $colors{"blue"};
      my $black = $colors{"black"};

      if ($foo =~ /^\!rs/) {
	  $im->setStyle($red); 
      } elsif ($foo =~ /^\!bs/) {
	  $im->setStyle($blue); 
      } elsif ($foo =~ /^\!ks/) {
	  $im->setStyle($black); 
      } elsif ($foo =~ /^\!ro/) {
	  $im->setStyle($red, gdTransparent);
      } elsif ($foo =~ /^\!bo/) {
	  $im->setStyle($blue, gdTransparent);
      } elsif ($foo =~ /^\!ko/) {
	  $im->setStyle($black, gdTransparent);
      } elsif ($foo =~ /^\!ra/) {
	  $im->setStyle($red, $red, gdTransparent);
      } elsif ($foo =~ /^\!ba/) {
	  $im->setStyle($blue, $blue, gdTransparent);
      } elsif ($foo =~ /^\!ka/) {
	  $im->setStyle($black, $black, gdTransparent);
      } else {
	  $im->setStyle($colors{"paleblue"}); 
      }
  } else {
      $im->setStyle($colors{"paleblue"}); 
  }
  
  #$im->line( $x1, $y1, $x2, $y2, $colors{"paleblue"} );
  
  $im->line( $x1, $y1, $x2, $y2, gdStyled );
304
305
306
307
308
309
310
311
312
313
314
315
}

# render nodes.
# if colors match certain colors, use icons instead
# (hack)

foreach $i (keys %nodes) {
  ($x, $y) = ($nodepos{ $i }[0], $nodepos{ $i }[1]);

  $cname = $nodes{$i}{"color"};
  $c = $colors{ $cname } || $colors{"blue"};

316
317
318
319
320
321
322
323
  if ($cname eq "red") {
      $im->rectangle( $x - 16, $y - 16, $x + 16, $y + 16, $colors{"darkred"} );
      $im->rectangle( $x - 15, $y - 15, $x + 15, $y + 15, $colors{"darkred"} );
  } else {
      $im->rectangle( $x - 16, $y - 16, $x + 16, $y + 16, $colors{"gray25"} );
      $im->rectangle( $x - 15, $y - 15, $x + 15, $y + 15, $colors{"gray25"} );
  }

324
325
326
  $im->filledRectangle( $x - 14, $y - 14, 
			$x + 14, $y + 14, $colors{"white"} );

327
328
329
330
  if ($nodeicon && ($cname eq "skyblue" || 
		    $cname eq "steelblue" ||
		    $cname eq "blue" ||
		    $cname eq "red")) {
331
332
333
334
335
336
337
338
339
340
341
342
343
    $im->copy($nodeicon, $x-16, $y-16, 0, 0, 32, 32);
  } elsif ($lanicon && $cname eq "green") {
    $im->copy($lanicon, $x-16, $y-16, 0, 0, 32, 32);
  } else {
  $im->filledRectangle( $x - 6, $y - 6, 
			$x + 6, $y + 6, $c );
  }
} 

# render text.
# this is done in a second pass so no text is obscured by
# boxes.

Chad Barb's avatar
Chad Barb committed
344
345
346
347
348
349
foreach $i (@links) {
    if (exists $linklabels{ $i }) {
	($a, $b) = ($i =~ /(\w+)\s(\w+)/);
	($x1, $y1) = ($nodepos{ $a }[0], $nodepos{ $a }[1]);
	($x2, $y2) = ($nodepos{ $b }[0], $nodepos{ $b }[1]);  
	($x, $y) = ( ($x1 + $x2) / 2, ($y1 + $y2) / 2 );
350
351

	$linklabels{ $i } =~ s/^\!..//;
Chad Barb's avatar
Chad Barb committed
352
353
354
355
356
357
358
	
	my @lines = split " ", $linklabels{ $i };

	$y -= (0.5 * (@lines * gdTinyFont->height)); 

	my $linenum = 0;
	foreach $j (@lines) {
359
	    $xpos = $x - ((length($j) - 0.5) * (($embiggen == 2) ? gdSmallFont->width : gdTinyFont->width) / 2); 
Chad Barb's avatar
Chad Barb committed
360
	    
361
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos + 1, $y, 
Chad Barb's avatar
Chad Barb committed
362
			$j, $bgcolor);
363
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos - 1, $y, 
Chad Barb's avatar
Chad Barb committed
364
			$j, $bgcolor);
365
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos, $y - 1, 
Chad Barb's avatar
Chad Barb committed
366
			$j, $bgcolor);
367
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos, $y + 1, 
Chad Barb's avatar
Chad Barb committed
368
			$j, $bgcolor);
369
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos, $y, 
Chad Barb's avatar
Chad Barb committed
370
			$j, $colors{"darkblue"}); 
371
	    $y += ($embiggen == 2) ? gdSmallFont->height : gdTinyFont->height; 
Chad Barb's avatar
Chad Barb committed
372
373
374
375
	}
    }
}
    
376
377
378
foreach $i (keys %nodes) {
  ($x, $y) = ($nodepos{ $i }[0], $nodepos{ $i }[1]);

Chad Barb's avatar
Chad Barb committed
379
380
381
# my $nm = $i;
  my $nm = $nodes{$i}{"label"};

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  @lines = ();

  $nm .= " ";

  $nm =~ s/^(\S+)//;

  push @lines, $1;

  while ( $nm =~ s/^(.{1,12})\s// ) {
    push @lines, $1;
  }

  push @lines, $nm;

  # my @lines = split " ", $nm;
Chad Barb's avatar
Chad Barb committed
397
398
399
400
401

  my $linenum = 0;
  foreach $j (@lines) {
#     warn "$j $x $y!";
      if ($linenum++ == 0) {
402
403
404
405
406
407
408
409
	  $xpos = $x - ((length($j) - 0.5) * ($embiggen ? gdMediumBoldFont->width : gdSmallFont->width) / 2);

	  $im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
		      $xpos + 1, $y + 20, $j, $bgcolor);
	  $im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
		      $xpos - 1, $y + 20, $j, $bgcolor);
	  $im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
		      $xpos, $y + 19, 
Chad Barb's avatar
Chad Barb committed
410
		      $j, $bgcolor);
411
412
	  $im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
		      $xpos, $y + 21, 
Chad Barb's avatar
Chad Barb committed
413
		      $j, $bgcolor);
414
415
	  $im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
		      $xpos, $y + 20, 
Chad Barb's avatar
Chad Barb committed
416
		      $j, $colors{"black"}); 
417
	  $y += $embiggen ? gdMediumBoldFont->height : gdSmallFont->height; 
Chad Barb's avatar
Chad Barb committed
418
      } else {
419
	  $xpos = $x - ((length($j) - 0.5) * ($embiggen ? gdSmallFont->width : gdTinyFont->width) / 2); 
Chad Barb's avatar
Chad Barb committed
420

421
422
	  $im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
		      $xpos + 1, $y + 20, 
Chad Barb's avatar
Chad Barb committed
423
		      $j, $bgcolor);
424
425
	  $im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
		      $xpos - 1, $y + 20, 
Chad Barb's avatar
Chad Barb committed
426
		      $j, $bgcolor);
427
428
	  $im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
		      $xpos, $y + 19, 
Chad Barb's avatar
Chad Barb committed
429
		      $j, $bgcolor);
430
431
	  $im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
		      $xpos, $y + 21, 
Chad Barb's avatar
Chad Barb committed
432
		      $j, $bgcolor);
433
434
	  $im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
		      $xpos, $y + 20, 
Chad Barb's avatar
Chad Barb committed
435
		      $j, $colors{"black"}); 
436
	  $y += ($embiggen) ? gdSmallFont->height : gdTinyFont->height; 
Chad Barb's avatar
Chad Barb committed
437
438
      }
  }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
} 

#write it to stdout

binmode STDOUT;
print $im->png;