-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgfprint
executable file
·127 lines (103 loc) · 3.26 KB
/
sgfprint
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
#! /usr/bin/perl
use warnings;
use strict;
# Print an SGF file on a single A4 sheet,
# using sgf2dg
#
# takes one command line parameter: name of input file
#
# 1. count the moves in the file
# 2. pass appropriate parameters to sgf2dg
# 3. do some minor editing on the resulting TeX file
# 4. run the TeX file through tex
# 5. open the dvi file with xdvi
# 6. display the command line for sgf2dg, in case manual tweaking is needed
#
# it will fail if the input file contains variations
# or if a ko or recapture causes sgf2dg to generate extra diagrams
my ($infile, $infilename, $sgf2dgcmd);
my ($texfile, $texfilename, $dvifilename, $namelength);
my ($howmanymoves, $firstbreak, $secondbreak, $thirdbreak);
my $numcols = 2;
my $howmanyargs;
$howmanyargs = $#ARGV + 1;
if ($howmanyargs != 1)
{print "Usage: sgfprint filename\n";
exit(0)};
$infilename=$ARGV[0];
open $infile, $infilename or die "Can't open $infilename";
$howmanymoves = CountMoves();
print "Counted moves: there are ", $howmanymoves, ".\n";
close $infile;
SetColsAndBreaks();
$sgf2dgcmd = ($numcols == 1) ?
"sgf2dg -break " . $firstbreak . " " . $infilename :
"sgf2dg -twoColumn -break " . $firstbreak . "," . $secondbreak . "," . $thirdbreak . " " . $infilename;
$namelength = length($infilename);
$texfilename = $infilename;
substr($texfilename, $namelength-3, 3)="tex";
$dvifilename = $infilename;
substr($dvifilename, $namelength-3, 3)="dvi";
if (stat($texfilename)>0) {die "Aborting: TeX file already exists.\n"}
if (stat($dvifilename)>0) {die "Aborting: dvi file already exists.\n"}
system($sgf2dgcmd);
TidyTexFile();
system 'tex ' . $texfilename;
system 'xdvi ' . $dvifilename . '&';
print "\n\nDone. Used command line: ", $sgf2dgcmd, "\n";
########
sub CountMoves {
my $howmany = 0;
while (<$infile>) {
while (/[BW]\[[a-z][a-z]\]/g) {
$howmany++;
}
} # end while
return $howmany;
} # end CountMoves
sub RoundUp {
# round an integer up to the next multiple of 10
my $thenumber = shift;
my $lastdigit;
$lastdigit = $thenumber % 10;
$thenumber -= $lastdigit;
return $lastdigit==0 ? $thenumber : $thenumber+10;
} # end RoundUp
sub IntHalf {
# half an integer, rounded up
# I'm sure I'm missing an easy way to do this!
my $thenumber = shift;
return ($thenumber % 2 == 0) ? $thenumber/2 : ($thenumber+1)/2;
}
sub SetColsAndBreaks {
my $remaining;
if ($howmanymoves <= 150) {
$numcols = 1;
$firstbreak = RoundUp(IntHalf($howmanymoves));
}
else {
$firstbreak = RoundUp(IntHalf(IntHalf($howmanymoves)));
$secondbreak = $firstbreak * 2;
$remaining = $howmanymoves - $secondbreak;
$thirdbreak = $secondbreak + RoundUp(IntHalf($remaining)-3);
}
} # end SetColsAndBreaks
sub TidyTexFile {
# remove extra \breaks, no page numbers
my ($tempfile, $tempfilename, $whichline);
open $texfile, $texfilename or die "Error: can't open $texfilename";
$tempfilename = $texfilename . "-temp";
open ($tempfile, '>', $tempfilename) or die "Error opening temporary file";
$whichline = 0;
while (<$texfile>) {
$whichline++;
if ($whichline == 18) {
print $tempfile "\\nopagenumbers\n\\vsize=600pt\n"
}
$_ =~ s/break\\break/break/;
print $tempfile $_;
}
close $texfile;
close $tempfile;
system 'mv', $tempfilename, $texfilename;
}