#!/usr/bin/perl
use strict;
my $filename = shift @ARGV;
my $render = `dot -Tplain $filename`;
my @commands = split /\n/, $render;
my ($img_scale, $img_width, $img_height);
my $x_dpi = 72;
my $y_dpi = 60;
print q|
|;
foreach my $command (@commands) {
print "\n";
my @p;
if(@p = ($command =~ /
^graph
\s+
([\d.]+) # scale
\s+
([\d.]+) # width
\s+
([\d.]+) # height
$/x)) {
($img_scale, $img_width, $img_height) = @p;
$img_scale = 1;
print "\n";
} elsif(@p = ($command =~ /
^node
\s+
("[^"]+"|\w+) # name
\s+
([\d.]+) # x
\s+
([\d.]+) # y
\s+
([\d.]+) # width
\s+
([\d.]+) # height
\s+
("[^"]+"|\w+) # label
\s+
(\w+) # style
\s+
(\w+) # shape
\s+
(\w+) # color
\s+
(\w+) # colorfill
$/x)) {
my ($name, $x, $y, $width, $height, $label, $style, $shape, $color, $fillcolor) = @p;
$x = $x * $x_dpi * $img_scale;
$y = ($img_height - $y) * $y_dpi * $img_scale;
$width *= $x_dpi * $img_scale;
$height *= $y_dpi * $img_scale - 10;
$label =~ s/"//g;
$label =~ s/\\n/
/g;
print qq{
$label
};
} elsif(@p = ($command =~ /
^edge
\s+
("[^"]+"|\w+) # tail name
\s+
("[^"]+"|\w+) # head name
\s+
(\d+) # n
\s+
(
(?:
(?:[\d.]+) # x_n
\s+
(?:[\d.]+) # y_n
\s+
)+
)
(?:
("[^"]+"|\w+) # label
\s+
([\d.]+) # label_x
\s+
([\d.]+) # label_y
\s+
)?
(\w+) # style
\s+
(\w+) # color
$/x)) {
# edge tail head n x1 y1 .. xn yn [label xl yl] style color
my ($tail, $head, $count, $points, $label, $label_x, $label_y, $style, $color) = @p;
my @points = split / /, $points;
my $cur_x = shift @points;
my $cur_y = shift @points;
while(@points) {
my $next_x = shift @points;
my $next_y = shift @points;
my ($x1, $y1, $x2, $y2) = ($cur_x, $cur_y, $next_x, $next_y);
$x1 = $x1 * $x_dpi * $img_scale;
$y1 = ($img_height - $y1) * $y_dpi * $img_scale;
$x2 = $x2 * $x_dpi * $img_scale;
$y2 = ($img_height - $y2) * $y_dpi * $img_scale;
print "\n";
$cur_x = $next_x;
$cur_y = $next_y;
}
}
print "\n";
}