-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertFigure
executable file
·206 lines (159 loc) · 4.86 KB
/
VertFigure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/local/bin/perl -w
use strict;
use warnings;
use Getopt::Std;
use Data::Dumper;
use SVG;
use SVG::VCD;
my %options;
if (!getopts('d', \%options) || @ARGV != 1) {
print STDERR "\
Usage: $0 [options] <VCD-file>
-d Dump information about parsed VCD-file.
";
exit 1;
}
if (@ARGV != 1) {
exit 1;
}
my $vcd = new SVG::VCD($ARGV[0]);
if ($options{d}) {
print Dumper($vcd);
exit;
}
my $n = $vcd->ntaxa();
my $th = $vcd->config("taxon-height");
my $cw = $vcd->config("cervical-width");
my $ch = $vcd->config("cervical-height");
my $dw = $vcd->config("dorsal-width");
my $dh = $vcd->config("dorsal-height");
my $dhg = $vcd->config("dorsal-height-gradient");
my $tyo = $vcd->config("text-y-offset") || 0;
my $cdo = $vcd->config("cervico-dorsal-offset");
my $cdc = $vcd->config("cervico-dorsal-color");
my $width = $vcd->config("width");
my $height = $vcd->config("height");
my $svg = SVG->new(width => $width, height => $height);
my $background = $vcd->config("background");
if ($background) {
$svg->rectangle(x => 0, y => 0, width => $width, height => $height,
style => { 'fill' => $background });
}
my $st = $svg->group(style => { 'font-family' => $vcd->config("font-family"),
'font-size' => $vcd->config("font-size"),
fill => 'black',
});
foreach my $i (1 .. $n) {
my $taxon = $vcd->taxon($i-1);
my $data = $taxon->field("data");
my($cdata, $ddata) = split('\|', $data);
$ddata = "" if !defined $ddata;
my $nc = length($cdata);
my $nd = length($ddata);
my $text = $st->text(x => 10, 'y' => $th * $i + $tyo);
my $tname = $taxon->field("taxon");
my %args = ( style => { 'font-style' => "italic" } );
if ($tname =~ s/^\///) {
%args = ();
}
$text->tspan(%args)->cdata($tname);
my $specimen = $taxon->field("specimen");
# I don't know why, but some versions of InkScape (e.g. v0.48 on
# Ubuntu 12.04.2) do not render an ordinary space in this context
# -- hence use of a non-breaking space.
$text->tspan()->cdata(" $specimen") if defined $specimen;
# Cervicals
foreach (my $j = 0; $j < $nc; $j++) {
vertebra($svg, $vcd, $i, $j-$nc, $cw, $ch, substr($cdata, $j, 1));
}
# Dorsals
my $dheight = $dh;
foreach (my $j = 0; $j < $nd; $j++) {
vertebra($svg, $vcd, $i, $j, $dw, $dheight, substr($ddata, $j, 1));
$dheight += $dhg if defined $dhg;
}
}
if ($cdc) {
my $maxh = $ch > $dh ? $ch : $dh;
my $margin = $th - $maxh;
$svg->line(x1 => $cdo, y1 => $margin/2,
x2 => $cdo, y2 => $n * $th + $margin/2,
style => { stroke => $cdc } );
}
print $svg->xmlify();
sub vertebra {
my($svg, $vcd, $yoff, $xoff, $width, $height, $char) = @_;
$char = lc($char);
my $cdo = $vcd->config("cervico-dorsal-offset");
my $th = $vcd->config("taxon-height");
my $bc = $vcd->config("box-color") || "grey";
my $x = $cdo + $xoff * $width;
my $y = $th * $yoff - $height;
$svg->rectangle(x => $x, 'y' => $y, width => $width, height => $height,
style => { stroke => $bc, fill => 'white' });
if (my $polygon = $vcd->config("state-$char-polygon")) {
$svg->polygon(style => { fill => $vcd->config("state-$char-color") },
points => compile_points($x, $y, $width, $height,
0.2, 0,
split(/[, ]/, $polygon)));
} elsif ($char eq "-") {
# Unknown state (e.g. vertebra missing): nothing to draw
} else {
die "unrecognised bifurcation state '$char'";
}
}
# Returns a string of the form '0,0 0,1 1,1 1,0'
sub compile_points {
my $xoff = shift();
my $yoff = shift();
my $xscale = shift();
my $yscale = shift();
my $pMargin = shift();
my $aMargin = shift();
my $text = "";
while (@_) {
my $x = shift();
my $y = shift();
# Adjust for proportional margin
$x = $x * (1-$pMargin) + $pMargin/2;
$y = $y * (1-$pMargin) + $pMargin/2;
# Adjust for scale
$x *= $xscale;
$y *= $yscale;
### Adjust for absolute margin -- how?
# Adjust for translation
$x += $xoff;
$y += $yoff;
$text .= " " if $text ne "";
$text .= "$x,$y";
}
return $text;
}
=head1 NAME
VertFigure - generate schematic illustrations of vertebral columns
=head1 SYNOPSIS
VertFigure
[
-d
]
I<VCD--file>
=head1 DESCRIPTION
C<VertFigure>
reads a named file in VCD (Vertebral Column Description) format, and
writes an illustration of the described vertebral columns in SVG
(Scalable Vector Graphics) format. This is useful for preparing
illustrations of scientific papers about vertebrates.
=head1 OPTIONS
=over 4
=item -d
Dump information about the parsed VCD file rather than rendering an
illustration. This is useful only for debugging, ensuring that the
input is interpreted as expected.
=back
=head1 SEE ALSO
SVG::VCD::Format - description of the input file format.
=head1 AUTHOR
Copyright (C) 2013-2014 by Mike Taylor E<lt>[email protected]<gt>
=head1 LICENSE
GNU General Public Licence v3.0
=cut