Debian Codegraph @

texmf-dist/scripts/pedigree-perl/pedigree.pl

raw
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2006-2022 Boris Veytsman & Leila Akhmadeeva
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 #
20 #
21 =pod
22
23 =head1 NAME
24
25 pedigree - create a TeX file for pedigree from a csv file
26
27 =head1 SYNOPSIS
28
29 B<pedigree> [-c I<configuration_file>] [-d] [-o I<output_file>] [-s I<start_id>] I<input_file>
30
31 B<pedigree> -v
32
33 =head1 DESCRIPTION
34
35 The program converts a comma separated I<input_file> into a TeX file
36 with pst-pdgr macros.
37
38 =head1 OPTIONS
39
40 =over 4
41
42 =item B<-c> I<configuration_file>
43
44 The configuration file to read along with the system-wide and user's
45 configuration files
46
47 =item B<-d>
48
49 Debug mode on
50
51 =item B<-o> -I<output_file>
52
53 The ouput file instead of I<input_file.tex>
54
55 =item B<-s> -I<start_id>
56
57 If this option is selected, the pedigree is constructed starting from
58 the node with the Id i<start_id>. Otherwise it is started from the
59 proband node.
60
61 This option allows to create pedigrees with multiple probands or absent
62 probands, or show people who are not proband's relatives.
63
64 =item B<-v>
65
66 Print version information
67
68 =back
69
70 =head1 FILES
71
72 =over 4
73
74 =item B</etc/pedigree.cfg>
75
76 Global configuration file
77
78 =item B<$HOME/.pedigreerc>
79
80 User configuration file
81
82 =back
83
84 =head1 SEE ALSO
85
86 The manual distributed with this program describes the format of the
87 configuration file and the input file.
88
89 The library functions are described in Pedigree::Language(3),
90 Pedigree::Parser(3), Pedigree::Node(3), Pedigree::PersonNode(3),
91 Pedigree::MarriageNode(3), Pedigree::Area(3).
92
93 =head1 AUTHOR
94
95 Boris Veytsman, Leila Akhmadeeva, 2006-2022
96
97
98 =cut
99
100
101 #########################################################
102 # Packages and Options #
103 #########################################################
104
105 use strict;
106 use vars qw($opt_c $opt_d $opt_o $opt_s $opt_v);
107
108 ##############################
109 # TeXLive compatibility stuff
110 ##############################
111 my $TLMaster; # Where TeXlive is
112 my $TLCONF; # TL config file
113 my $TLCONFLOCAL; # TL local config file
114 BEGIN {
115 chomp($TLMaster = `kpsewhich -var-value=TEXMFROOT`);
116 if (length($TLMaster)) {
117 unshift @INC, "$TLMaster/texmf-dist/scripts/pedigree-perl";
118 $TLCONF = "$TLMaster/texmf-config/pedigree/pedigree.cfg";
119 chomp($TLCONFLOCAL = `kpsewhich -var-value=TEXMFLOCAL`);
120 $TLCONFLOCAL .= "/pedigree/pedigree.cfg";
121 }
122 }
123
124 use Getopt::Std;
125 use FileHandle;
126 use Pedigree;
127
128 #########################################################
129 # Options Reading and Global Variables #
130 #########################################################
131
132 my $USAGE="Usage: $0 [-c configuration_file] [-d] [-o output_file] [-s start_id] input_file\n";
133 my $COPYRIGHT=<<END;
134 $0 Version 2.1, August 2022
135
136 Copyright (C) 2006-2022 Boris Veytsman & Leila Akhmadeeva
137
138 This program is free software; you can redistribute it and/or modify
139 it under the terms of the GNU General Public License as published by
140 the Free Software Foundation; either version 2 of the License, or
141 (at your option) any later version.
142
143 This program is distributed in the hope that it will be useful,
144 but WITHOUT ANY WARRANTY; without even the implied warranty of
145 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
146 GNU General Public License for more details.
147
148 You should have received a copy of the GNU General Public License
149 along with this program; if not, write to the Free Software
150 Foundation, Inc., 59 Temple Place - Suite 330,
151 END
152
153 my $GLOBALCONF="/etc/pedigree.cfg";
154
155 my $USERCONF="$ENV{HOME}/.pedigreerc";
156
157
158 our $IN = new FileHandle;
159 our $OUT = new FileHandle;
160
161 getopts('c:do:s:v') or die $USAGE;
162
163 if ($opt_v) {
164 die $COPYRIGHT;
165 }
166
167 our $DEBUG = $opt_d ||0;
168
169 if (scalar @ARGV != 1) {
170 die $USAGE;
171 }
172
173 our $start_id = $opt_s;
174
175 #########################################################
176 # Opening Files #
177 #########################################################
178
179
180 if ($ARGV[0] eq '-') {
181 $IN->fdopen(fileno(STDIN),"r");
182 } else {
183 $IN->open($ARGV[0], "r") or die "Cannot read from $ARGV[0]\n";
184 }
185
186 my $outfile=$ARGV[0];
187 if ($opt_o) {
188 $outfile = $opt_o;
189 } else {
190 $outfile =~ s/\.[^\.]*$/.tex/;
191 }
192 if ($outfile eq '-') {
193 $OUT->fdopen(fileno(STDOUT),"w");
194 } else {
195 $OUT->open($outfile, "w") or die "Cannot write to $outfile\n";
196 }
197
198 #########################################################
199 # Configuration #
200 #########################################################
201
202 #
203 # First, the defaults. Even if we do not find any
204 # configuration file, these will work.
205 #
206
207
208 #
209 # Do we want to have a full LaTeX file or just a fragment?
210 #
211 our $fulldoc=1;
212
213 #
214 # What kind of document do we want
215 #
216 # our $documentheader='\documentclass[landscape]{article}';
217 our $documentheader='\documentclass{article}';
218
219 #
220 # Define additional packages here
221 #
222 # our $addtopreamble=<<END;
223 # \\usepackage{graphics}
224 # END
225 our $addtopreamble=<<END;
226 \\psset{descarmA=1}
227 END
228
229
230
231 #
232 # Do we want to print a legend?
233 #
234 our $printlegend=1;
235
236 #
237 # Fields to include in the legend. Delete Name for privacy
238 # protection.
239 #
240 our @fieldsforlegend = qw(Name DoB AgeAtDeath Comment);
241
242 #
243 # Fields to put at the node. Delete Name for privacy
244 # protection.
245 #
246 our @fieldsforchart = qw(Name);
247
248 #
249 # Language
250 #
251 # our $language="russian";
252 our $language="english";
253
254 #
255 # Override the encoding
256 #
257 # our $encoding="koi8-r";
258
259 our $encoding;
260
261 #
262 # descarmA
263 #
264 our $descarmA = 0.8;
265
266 #
267 # Fonts for nodes
268 #
269 our $belowtextfont='\small';
270 our $abovetextfont='\scriptsize';
271
272 #
273 # Distances between nodes (in cm)
274 #
275 our $xdist=2;
276 our $ydist=2;
277
278 #
279 # Maximal width and height of the pedigree in cm.
280 # Set this to 0 to switch off scaling
281 #
282 our $maxW = 15;
283 our $maxH = 19;
284
285 #
286 # Whether to rotate the page. The values are 'yes', 'no' and 'maybe'
287 # If 'maybe' is chosen, the pedigree is rotated if it allows better
288 # scaling
289 #
290 our $rotate = 'maybe';
291
292 #
293 # Read the global configuration file(s)
294 #
295 foreach my $conffile ($GLOBALCONF, $TLCONF, $TLCONFLOCAL) {
296 if (-r $conffile) {
297 if ($DEBUG) {
298 print STDERR "Reading global configuration file $conffile\n";
299 }
300 require "$conffile";
301 } else {
302 if ($DEBUG) {
303 print STDERR "Cannot find global configuration file $conffile; going without it\n";
304 }
305 }
306 }
307
308 #
309 # Read the user configuration file
310 #
311 if (-r $USERCONF) {
312 if ($DEBUG) {
313 print STDERR "Reading user configuration file $USERCONF\n";
314 }
315 require "$USERCONF";
316 } else {
317 if ($DEBUG) {
318 print STDERR "Cannot find user configuration file $USERCONF; going without it\n";
319 }
320 }
321
322 #
323 # Read the option configuration file
324 #
325 if ($opt_c) {
326 if (-r $opt_c) {
327 if ($DEBUG) {
328 print STDERR "Reading optional configuration file $opt_c\n";
329 }
330 require "$opt_c";
331 } else {
332 die "Cannot find $opt_c\n";
333 }
334 }
335
336 #########################################################
337 # Setting up #
338 #########################################################
339
340 my $lang = new Pedigree::Language($language, $encoding);
341 $_=<$IN>;
342 my $parser = new Pedigree::Parser($_,$lang);
343 my $start;
344
345 #########################################################
346 # Reading input #
347 #########################################################
348
349 while (<$IN>) {
350 my $node = Pedigree->MakeNode($parser->Parse($_));
351 if (ref($node)) {
352 if ($start_id) {
353 if ($start_id eq $node->Id()) {
354 $start = $node;
355 if ($DEBUG) {
356 print STDERR "Found start: ", $start->Id(), "\n";
357 }
358 }
359 } else {
360 if ($node->isProband()) {
361 if (ref($start)) {
362 print STDERR "Two probands? I got ", $start->Id(),
363 " and ", $node->Id(), "\n";
364 }
365 $start=$node;
366 if ($DEBUG) {
367 print STDERR "Found proband: ", $start->Id(), "\n";
368 }
369 }
370 }
371 }
372 }
373
374 if (!ref($start)) {
375 die "Cannot find the start!\n";
376 }
377
378 #########################################################
379 # Process Pedigree #
380 #########################################################
381
382 #
383 # Check all parents
384 #
385 $start->CheckAllParents();
386
387
388 #
389 # The root is the root of the tree to which the proband
390 # belongs
391 #
392
393 my ($root, undef)=@{$start->FindRoot(0)};
394 if ($DEBUG) {
395 print STDERR "Root: ", $root->Id(), "\n";
396 }
397
398 #
399 # Calculate relative coordinates
400 #
401 $root->SetRelX(0);
402 $root->SetRelY(0);
403 $root->SetArea();
404
405 #
406 # Calculate the absolute coordinates
407 #
408 $root->CalcAbsCoor(0,0);
409
410 #
411 # Check for consanguinic marriages
412 #
413 $root->AddConsanguinicMarriages();
414
415 #
416 # And twins
417 #
418 $root->AddTwins($ydist);
419
420 #
421 # Get the frame
422 #
423 my ($xmin, $ymin, $xmax, $ymax) = @{$root->SetFrame($xdist, $ydist)};
424
425
426
427 #########################################################
428 # Printing headers #
429 #########################################################
430
431 if ($fulldoc) {
432 printheader($OUT,$lang,$addtopreamble);
433 }
434
435 #########################################################
436 # Calculate scale and check whether to rotate #
437 #########################################################
438
439 my $scale=1;
440 my $scaleRotated = 1;
441
442 if ($maxH && $maxW) {
443 if ($maxH/($ymax-$ymin) < $scale) {
444 $scale = $maxH/($ymax-$ymin);
445 }
446 if ($maxW/($xmax-$xmin) < $scale) {
447 $scale = $maxW/($xmax-$xmin);
448 }
449 if ($maxW/($ymax-$ymin) < $scaleRotated) {
450 $scaleRotated = $maxW/($ymax-$ymin);
451 }
452 if ($maxH/($xmax-$xmin) < $scaleRotated) {
453 $scaleRotated = $maxH/($xmax-$xmin);
454 }
455 }
456
457 my $doRotate = ($rotate =~ /yes/i) || (($rotate =~ /maybe/i) &&
458 ($scaleRotated > $scale));
459
460 #########################################################
461 # Printing pspicture #
462 #########################################################
463
464 my $pre;
465 my $post ='}'."\n";
466
467 if ($doRotate) {
468 $descarmA *= $scaleRotated;
469 $pre="\\rotatebox{90}{%\n\\psset{descarmA=$descarmA}%\n";
470 if ($scaleRotated<1) {
471 $pre .= '\psset{unit='.$scaleRotated.'}%'."\n";
472 }
473 } else {
474 $descarmA *= $scale;
475 $pre="{%\n\\psset{descarmA=$descarmA}%\n";
476 if ($scale<1) {
477 $pre .= '{\psset{unit='.$scale.'}%'."\n";
478 }
479 }
480
481 print $OUT $pre;
482
483 print $OUT '\begin{pspicture}',"($xmin,$ymin)($xmax,$ymax)\n";
484
485 print $OUT $root->DrawAll($xdist, $ydist, $belowtextfont,
486 $abovetextfont, @fieldsforchart);
487
488 print $OUT '\end{pspicture}%',"\n";
489
490 print $OUT $post;
491
492 #########################################################
493 # Printing legend #
494 #########################################################
495
496
497 if ($printlegend) {
498 print $OUT $root->PrintAllLegends($lang, @fieldsforlegend);
499 }
500
501 #########################################################
502 # Printing end #
503 #########################################################
504
505
506 if ($fulldoc) {
507 printend($OUT);
508 }
509
510 #########################################################
511 # Exiting #
512 #########################################################
513
514 exit 0;
515
516 #########################################################
517 # Subroutines #
518 #########################################################
519
520 #
521 # Printing headers & footers
522 #
523
524 sub printheader {
525 my ($OUT,$lang,$addtopreamble)=@_;
526 print $OUT <<END;
527 $documentheader
528 \\usepackage{pst-pdgr}
529 END
530
531 print $OUT $lang->Header;
532 print $OUT <<END;
533 $addtopreamble
534 \\begin{document}
535 END
536 return 0;
537 }
538
539
540 sub printend {
541 my $OUT=shift;
542 print $OUT "\\end{document}\n";
543 return 0;
544 }
545
546