| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # POD docs at end | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Data::Stag qw(:all); | ||
| 8 | use Getopt::Long; | ||
| 9 | |||
| 10 | use FileHandle; | ||
| 11 | use GD; | ||
| 12 | |||
| 13 | my $e = ""; | ||
| 14 | my $fmt = 'png'; | ||
| 15 | my $out; | ||
| 16 | my $parser; | ||
| 17 | my $fontchoice; | ||
| 18 | GetOptions("element|e=s"=>\$e, | ||
| 19 | "out|o=s"=>\$out, | ||
| 20 | "parser|format|p=s" => \$parser, | ||
| 21 | "font=s"=>\$fontchoice, | ||
| 22 | "help|h"=>sub { system("perldoc $0"); exit }, | ||
| 23 | ); | ||
| 24 | |||
| 25 | my $fn = shift @ARGV; | ||
| 26 | my $fh; | ||
| 27 | if ($fn eq '-') { | ||
| 28 | $fh = \*STDIN; | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | $fh = FileHandle->new($fn) || die $fn; | ||
| 32 | } | ||
| 33 | my $tab = 4; | ||
| 34 | my $stag = Data::Stag->parse(-file=>$fn, -format=>$parser); | ||
| 35 | my $box = box($stag, 0, 0); | ||
| 36 | my $FONT = gdLargeFont; | ||
| 37 | if ($fontchoice) { | ||
| 38 | if ($fontchoice eq 'gdMediumBoldFont') { | ||
| 39 | $FONT = gdMediumBoldFont; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | my $CW = $FONT->width; | ||
| 43 | my $CH = $FONT->height; | ||
| 44 | my $width = $CW * $box->{x2}; | ||
| 45 | my $height = $CH * $box->{y2}; | ||
| 46 | my $im = new GD::Image($width, $height); | ||
| 47 | |||
| 48 | my $bgcol = $im->colorAllocate(255, 255, 255); | ||
| 49 | |||
| 50 | my $fgcol = $im->colorAllocate(0, 0, 0); | ||
| 51 | my $linecol = $im->colorAllocate(0, 0, 0); | ||
| 52 | my $namecol = $im->colorAllocate(0, 0, 0); | ||
| 53 | my $datacol = $im->colorAllocate(255, 0, 0); | ||
| 54 | #$im->transparent($bgcol); | ||
| 55 | $im->interlaced('true'); | ||
| 56 | |||
| 57 | |||
| 58 | drawbox($im, $box); | ||
| 59 | if ($out) { | ||
| 60 | open(F, ">$out") || die($out); | ||
| 61 | print F $im->$fmt(); | ||
| 62 | close(F); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | print $im->$fmt(); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub box { | ||
| 69 | my $stag = shift; | ||
| 70 | my ($x, $y)= @_; | ||
| 71 | |||
| 72 | my $y1 = $y; | ||
| 73 | my $x2 = $x; | ||
| 74 | my @kids = $stag->kids; | ||
| 75 | my @subboxes = (); | ||
| 76 | my $data; | ||
| 77 | if ($stag->isterminal) { | ||
| 78 | $data = $stag->data; | ||
| 79 | $x2 += length($data) + length($stag->element) + 1; | ||
| 80 | @kids = (); | ||
| 81 | } | ||
| 82 | $y++; | ||
| 83 | foreach my $kid (@kids) { | ||
| 84 | my $subbox = box($kid, $x + $tab, $y); | ||
| 85 | push(@subboxes, $subbox); | ||
| 86 | $y = $subbox->{y2}; | ||
| 87 | if ($subbox->{x2} > $x2) { | ||
| 88 | $x2 = $subbox->{x2}; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | return | ||
| 92 | {boxes=>\@subboxes, | ||
| 93 | name=>$stag->element, | ||
| 94 | data=>$data, | ||
| 95 | x1=>$x, | ||
| 96 | x2=>$x2, | ||
| 97 | y1=>$y1, | ||
| 98 | y2=>$y, | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub drawbox { | ||
| 103 | my $im = shift; | ||
| 104 | my $box = shift; | ||
| 105 | my $sx1 = $box->{x1} * $CW; | ||
| 106 | my $sy1 = $box->{y1} * $CH; | ||
| 107 | my $name = $box->{name}; | ||
| 108 | my $data = $box->{data}; | ||
| 109 | |||
| 110 | $im->string($FONT, | ||
| 111 | $sx1, | ||
| 112 | $sy1, | ||
| 113 | $name, | ||
| 114 | $namecol); | ||
| 115 | if ($data) { | ||
| 116 | $im->string($FONT, | ||
| 117 | $sx1 + length($name) * $CW + 1 * $CW, | ||
| 118 | $sy1, | ||
| 119 | $data, | ||
| 120 | $datacol); | ||
| 121 | } | ||
| 122 | my $subboxes = $box->{boxes}; | ||
| 123 | my $XOFF = int($CW / 2); | ||
| 124 | my $YOFF = int($CH / 2); | ||
| 125 | foreach (@$subboxes) { | ||
| 126 | drawbox($im, $_); | ||
| 127 | my $ix = $sx1 + $XOFF; | ||
| 128 | my $iy = $_->{y1} * $CH + $YOFF; | ||
| 129 | my $jx = $_->{x1} * $CW; | ||
| 130 | $im->line($ix, $sy1 + $CH, $ix, $iy, $linecol); | ||
| 131 | $im->line($ix, $iy, $jx, $iy, $linecol); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | exit 0; | ||
| 136 | |||
| 137 | __END__ |