| 1 |
#!/tools/bin/perl5.005 -w |
| 2 |
use strict; |
| 3 |
use Data::Dumper; |
| 4 |
use Tk; |
| 5 |
|
| 6 |
my $hc = qr/[-\*<>]/; |
| 7 |
my $vc = qr/[\|\^v\*]/; |
| 8 |
|
| 9 |
my $mw = MainWindow->new; |
| 10 |
my $menu = $mw->Menu; |
| 11 |
$mw->configure(-menu => $menu); |
| 12 |
$menu->cascade(-label => '~File', -menuitems => [ |
| 13 |
[Command => 'E~xit', -command => [destroy => $mw]] |
| 14 |
]); |
| 15 |
|
| 16 |
my $txt = text_image(); |
| 17 |
|
| 18 |
my $c = $mw->Scrolled('Canvas',-bg => 'white', |
| 19 |
-width => 10*@{$txt->[0]}, |
| 20 |
-height => 10 * @$txt, |
| 21 |
-scrollbars => 'osow'); |
| 22 |
$c->pack(-expand => 1, -fill => 'both'); |
| 23 |
|
| 24 |
my @v = find_vertical($txt); |
| 25 |
my @h = find_horizontal($txt); |
| 26 |
|
| 27 |
my @box = find_boxes(\@v,\@h); |
| 28 |
|
| 29 |
make_varrows($txt,@v); |
| 30 |
make_harrows($txt,@h); |
| 31 |
|
| 32 |
foreach my $box (@box) |
| 33 |
{ |
| 34 |
$c->createRectangle(map(10*$_,@$box),-width => 3, -outline => 'black', -fill => '#f0f0f0'); |
| 35 |
} |
| 36 |
|
| 37 |
foreach my $line (@v,@h) |
| 38 |
{ |
| 39 |
my @line = @$line; |
| 40 |
$c->createLine(map(10*$_,splice(@line,0,4)),@line,-fill => 'black'); |
| 41 |
} |
| 42 |
|
| 43 |
foreach my $box (@box) |
| 44 |
{ |
| 45 |
my ($x1,$y1,$x2,$y2) = @$box; |
| 46 |
my @s; |
| 47 |
for (my $y = $y1+1; $y < $y2; $y++) |
| 48 |
{ |
| 49 |
my $l = ''; |
| 50 |
for (my $x = $x1+1; $x < $x2; $x++) |
| 51 |
{ |
| 52 |
$l .= $txt->[$y][$x]; |
| 53 |
} |
| 54 |
$l =~ s/^\s+//; |
| 55 |
$l =~ s/\s+$//; |
| 56 |
push(@s,$l) if length($l); |
| 57 |
} |
| 58 |
$c->createText(int(10*($x1+$x2)/2),int(10*($y1+$y2)/2),-text => join("\n",@s), |
| 59 |
-justify => 'center', -anchor => 'center'); |
| 60 |
} |
| 61 |
|
| 62 |
$c->configure(-scrollregion => [$c->bbox('all')]); |
| 63 |
|
| 64 |
$mw->update; |
| 65 |
|
| 66 |
MainLoop; |
| 67 |
|
| 68 |
sub make_varrows |
| 69 |
{ |
| 70 |
my $txt = shift; |
| 71 |
foreach my $line (@_) |
| 72 |
{ |
| 73 |
my $f = 0; |
| 74 |
my ($x1,$y1,$x2,$y2) = @$line; |
| 75 |
die unless $x1 == $x2; |
| 76 |
for my $y ($y1..$y2) |
| 77 |
{ |
| 78 |
my $ch = $txt->[$y][$x1]; |
| 79 |
$f |= 1 if $ch eq '^'; |
| 80 |
$f |= 2 if $ch eq 'v'; |
| 81 |
} |
| 82 |
push(@$line,'-arrow',${['','first','last','both']}[$f]) if ($f); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
sub make_harrows |
| 87 |
{ |
| 88 |
my $txt = shift; |
| 89 |
foreach my $line (@_) |
| 90 |
{ |
| 91 |
my $f = 0; |
| 92 |
my ($x1,$y1,$x2,$y2) = @$line; |
| 93 |
die unless $y1 == $y2; |
| 94 |
for my $x ($x1..$x2) |
| 95 |
{ |
| 96 |
my $ch = $txt->[$y1][$x]; |
| 97 |
$f |= 1 if $ch eq '<'; |
| 98 |
$f |= 2 if $ch eq '>'; |
| 99 |
} |
| 100 |
push(@$line,'-arrow',${['','first','last','both']}[$f]) if ($f); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
sub find_boxes |
| 105 |
{ |
| 106 |
my ($v,$h) = @_; |
| 107 |
my %x; |
| 108 |
my %y; |
| 109 |
foreach my $i (0..@$v-1) |
| 110 |
{ |
| 111 |
my $line = $v->[$i]; # x,y1,x,y2 |
| 112 |
my $x = $line->[0]; |
| 113 |
my $y = $line->[1]; |
| 114 |
my $e = $line->[3]; |
| 115 |
my $key = $y.'-'.$e; |
| 116 |
$y{$key} = [] unless exists $y{$key}; |
| 117 |
push(@{$y{$key}},[$x,$i]); |
| 118 |
} |
| 119 |
foreach my $i (0..@$h-1) |
| 120 |
{ |
| 121 |
my $line = $h->[$i]; # x1,y,x2,y |
| 122 |
my $x = $line->[0]; |
| 123 |
my $y = $line->[1]; |
| 124 |
my $e = $line->[2]; |
| 125 |
my $key = $x.'-'.$e; |
| 126 |
$x{$key} = [] unless exists $x{$key}; |
| 127 |
push(@{$x{$key}},[$y,$i]); |
| 128 |
} |
| 129 |
my @box; |
| 130 |
my @vd; |
| 131 |
my @hd; |
| 132 |
foreach my $xk (keys %x) |
| 133 |
{ |
| 134 |
my ($x1,$x2) = split(/-/,$xk); |
| 135 |
my $xp = $x{$xk}; |
| 136 |
my @junk; |
| 137 |
LOOP: |
| 138 |
while (@$xp) |
| 139 |
{ |
| 140 |
my ($y1,$i1) = @{splice(@$xp,0,1)}; |
| 141 |
for my $xi (0..@$xp-1) |
| 142 |
{ |
| 143 |
my ($y2,$i2) = @{$xp->[$xi]}; |
| 144 |
my $yk = "$y1-$y2"; |
| 145 |
if (exists $y{$yk}) |
| 146 |
{ |
| 147 |
my $yp = $y{$yk}; |
| 148 |
my $yi = 0; |
| 149 |
for my $yi (0..@$yp-1) |
| 150 |
{ |
| 151 |
if ($yp->[$yi][0] == $x1) |
| 152 |
{ |
| 153 |
my $j1 = $yp->[$yi][1]; |
| 154 |
for my $yj ($yi..@$yp-1) |
| 155 |
{ |
| 156 |
if ($yp->[$yj][0] == $x2) |
| 157 |
{ |
| 158 |
my $j2 = $yp->[$yj][1]; |
| 159 |
push(@box,[$x1,$y1,$x2,$y2]); |
| 160 |
splice(@$xp,$xi,1); |
| 161 |
splice(@$yp,$yj,1); |
| 162 |
splice(@$yp,$yi,1); |
| 163 |
push(@hd,$i1,$i2); |
| 164 |
push(@vd,$j1,$j2); |
| 165 |
delete $y{$yk} unless @$yp; |
| 166 |
next LOOP; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
push(@junk,[$y1,$i1]); |
| 174 |
} |
| 175 |
if (@junk) |
| 176 |
{ |
| 177 |
$x{$xk} = \@junk; |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
delete $x{$xk}; |
| 182 |
} |
| 183 |
} |
| 184 |
foreach my $i (sort {$b <=> $a} @vd) |
| 185 |
{ |
| 186 |
splice(@$v,$i,1); |
| 187 |
} |
| 188 |
foreach my $i (sort {$b <=> $a} @hd) |
| 189 |
{ |
| 190 |
splice(@$h,$i,1); |
| 191 |
} |
| 192 |
return @box; |
| 193 |
} |
| 194 |
|
| 195 |
sub find_vertical |
| 196 |
{ |
| 197 |
my $txt = shift; |
| 198 |
my $h = @$txt; |
| 199 |
my $w = @{$txt->[0]}; |
| 200 |
my @vert; |
| 201 |
my @live; |
| 202 |
for (my $y = 0; $y < $h; $y++) |
| 203 |
{ |
| 204 |
for (my $x = 0; $x < $w; $x++) |
| 205 |
{ |
| 206 |
my $s = $live[$x]; |
| 207 |
my $c = $txt->[$y][$x]; |
| 208 |
if (defined $s) |
| 209 |
{ |
| 210 |
unless ($c =~ $vc || ($c eq '+' && $y+1 < $h && $txt->[$y+1][$x] =~ $vc)) |
| 211 |
{ |
| 212 |
my $e = ($c eq '+') ? $y : $y-1; |
| 213 |
if ($e - $s > 0) |
| 214 |
{ |
| 215 |
push(@vert,[$x,$s,$x,$e]); |
| 216 |
} |
| 217 |
$live[$x] = undef; |
| 218 |
} |
| 219 |
} |
| 220 |
elsif ($c eq '+') |
| 221 |
{ |
| 222 |
$live[$x] = $y; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
my $e = $h-1; |
| 227 |
for (my $x = 0; $x < $w; $x++) |
| 228 |
{ |
| 229 |
my $s = $live[$x]; |
| 230 |
if (defined $s) |
| 231 |
{ |
| 232 |
if ($e - $s > 0) |
| 233 |
{ |
| 234 |
push(@vert,[$x,$s,$x,$e]); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
return @vert; |
| 239 |
} |
| 240 |
|
| 241 |
sub find_horizontal |
| 242 |
{ |
| 243 |
my $txt = shift; |
| 244 |
my $h = @$txt; |
| 245 |
my $w = @{$txt->[0]}; |
| 246 |
my @horz; |
| 247 |
my @live; |
| 248 |
for (my $x = 0; $x < $w; $x++) |
| 249 |
{ |
| 250 |
for (my $y = 0; $y < $h; $y++) |
| 251 |
{ |
| 252 |
my $c = $txt->[$y][$x]; |
| 253 |
my $s = $live[$y]; |
| 254 |
if (defined $s) |
| 255 |
{ |
| 256 |
unless ($c =~ $hc || ($c =~ /[\+\|]/ && $x+1 < $w && $txt->[$y][$x+1] =~ $hc)) |
| 257 |
{ |
| 258 |
my $e = ($c eq '+') ? $x : $x-1; |
| 259 |
if ($e - $s > 0) |
| 260 |
{ |
| 261 |
push(@horz,[$s,$y,$e,$y]); |
| 262 |
} |
| 263 |
$live[$y] = undef; |
| 264 |
} |
| 265 |
} |
| 266 |
elsif ($c eq '+') |
| 267 |
{ |
| 268 |
$live[$y] = $x; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
my $e = $w-1; |
| 273 |
for (my $y = 0; $y < $h; $y++) |
| 274 |
{ |
| 275 |
my $s = $live[$y]; |
| 276 |
if (defined $s) |
| 277 |
{ |
| 278 |
if ($e - $s > 0) |
| 279 |
{ |
| 280 |
push(@horz,[$s,$y,$e,$y]); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
return @horz; |
| 285 |
} |
| 286 |
|
| 287 |
sub show_txt |
| 288 |
{ |
| 289 |
my $txt = shift; |
| 290 |
foreach (@$txt) |
| 291 |
{ |
| 292 |
print @$_,"\n"; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
sub text_image |
| 297 |
{ |
| 298 |
my @txt; |
| 299 |
my $max = 0; |
| 300 |
while (<>) |
| 301 |
{ |
| 302 |
next if m#^/#; |
| 303 |
s/\s+$//; |
| 304 |
my $l = length($_); |
| 305 |
$max = $l if $l > $max; |
| 306 |
push(@txt,[split('',$_)]); |
| 307 |
} |
| 308 |
foreach (@txt) |
| 309 |
{ |
| 310 |
if (@$_ < $max) |
| 311 |
{ |
| 312 |
push(@$_,(' ') x ($max - @$_)); |
| 313 |
} |
| 314 |
} |
| 315 |
return \@txt; |
| 316 |
} |
| 317 |
|