summaryrefslogtreecommitdiffstats
path: root/bin/scoresheet.pl
blob: 74c7a8a8810d3e17d96486441ce086f1b86ad393 (plain)
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
#!/usr/bin/perl

use Config::Tiny;

($dir, $idfile) = @ARGV;

# Convert LaTeX units to mm
%units = (
    'pt' => (25.4/72.27),
    'pc' => (12*25.4/72.27),
    'bp' => (25.4/72),
    'in' => 25.4,
    'cm' => 10,
    'mm' => 1,
    'dd' => ((1238/1157)*(25.4/72.27)),
    'cc' => (12*(1238/1157)*(25.4/72.27)),
    'sp' => (25.4/(65536*72.27)),
);

if (open(L, '<', $dir.'/questionnaire.log')) {
    while (defined($line = <L>)) {
	if ($line =~ /\/pslatex\.sty\b/) {
	    push(@texhdrs, "\\usepackage{pslatex}\n");
	    last;
	}
    }
    close(L);
}

my $info = Config::Tiny->read($dir.'/info', 'utf8');
my $global_id = $info->{'sdaps'}->{'global_id'};
my $title     = $info->{'sdaps'}->{'title'};
my $author    = $info->{'info'}->{'Author'};
my $date      = $info->{'info'}->{'Date'};
my $survey_id = $info->{'questionnaire'}->{'survey_id'};

open(Q, '<', $dir.'/questionnaire.sdaps') or die;

undef $y0, $y1;

sub uconv($) {
    my($y) = @_;
    $y = $1 * $units{$2} if ($y =~ /^([^a-z]+)([a-z]+)$/);
    return $y;
}

$ytweak = 2.75;

undef $name;
while ($line = <Q>) {
    chomp $line;
    if ($line =~ /^Box=Checkbox/) {
	@d = split(/,\s*/, $line);
	$y = uconv($d[3]) - uconv($d[5])/2 - $ytweak;
	push(@y, $y);
	$name{$y} = $name;
	undef $name;
    } elsif ($line =~ /^QObject-Choice=XAUTO\.\s*(.*)$/) {
	$name = $1;
    }
}

close(Q);

foreach $y (@y) {
    $y0 = $y if (!defined($y0) || $y < $y0);
    $y1 = $y if (!defined($y1) || $y > $y1);
}

$writeinspacing = 6.325;

$y0 -= $writeinspacing;
$writeintxt = $y0;

while ($y0 - $writeinspacing > 20.45+12.5) {
    $y0 -= $writeinspacing;
    push(@wiy, $y0);
}

$yrange = $y1 - $y0;

$cby = (279.4 - 12.5 + 7.25+5+2 + 3 + $y1) / 2;

if (defined($idfile) && open(IDS, '<', $idfile)) {
    @id_list = <IDS>;
    close(IDS);
} else {
    @id_list = ('');		# One copy with no ID
}

print "\\documentclass[letterpaper]{article}\n";
print "\\usepackage[utf8]{inputenc}\n";
print "\\usepackage{array}\n";
print "\\usepackage{calc}\n";
print "\\usepackage{tikz}\n";
print @texhdrs;
print "\\newcommand\\drawboxes[1]{\n";
print "  \\foreach \\y in {0,1} {\n";
print "    \\foreach \\x in {1,...,50}\n";
print "      \\filldraw[black,thin,fill=white](20.45+25+3*\\x-3,#1+3*\\y) rectangle ++(2.75,2.75);\n";
print "    \\foreach \\x in {5,10,...,45}\n";
print "      \\filldraw[black,thin,fill=black!10](20.45+25+3*\\x-3,#1+3*\\y) rectangle ++(2.75,2.75);\n";
print "    \\foreach \\x in {10,20,...,50}\n";
print "      \\filldraw[black,thin,fill=black!20](20.45+25+3*\\x-3,#1+3*\\y) rectangle ++(2.75,2.75);\n";
print "  }\n";
print "}\n";
print "\\newcommand\\scoringline[2]{\n";
print "  \\drawboxes{#1}\n";
print "  \\draw[black!50,line width=0.5mm,->](20.20+25,#1+2.75) -- (5,#1+2.75);\n";
print "  \\node at (10,#1+2.75) [anchor=south west] {\\footnotesize #2};\n";
print "}\n";
print "\\newcommand\\writein[1]{\n";
print "  \\drawboxes{#1}\n";
print "  \\draw[black!50](5,#1) rectangle ++(40,5.75);\n";
print "}\n";
print "\\newcolumntype{C}[1]{>{\\centering\\arraybackslash}m{#1}}\n";
print "\\begin{document}\n";
foreach $page_id ( @id_list ) {
    chomp $page_id;
    print "\\thispagestyle{empty}\n";
    print "\\begin{tikzpicture}[remember picture,overlay,shift={(current page.south west)},x=1mm,y=1mm]\n";
    print "\\foreach \\x in {5,15,...,45}{\n";
    print "  \\draw[black!20,line width=1.5mm](20.45+25+1.25+3*\\x-3,$y0) -- ++(0,$yrange+7.25+2.5);\n";
    print "}\n";
    print "\\foreach \\x in {10,20,...,50}{\n";
    print "  \\draw[black!40,line width=1.5mm](20.45+25+1.25+3*\\x-3,$y0) -- ++(0,$yrange+7.25+5);\n";
    print "  \\node at (20.45+25+1.25+3*\\x-3,$y1+7.25+5+2) {\\x};\n";
    print "}\n";
    print "\\node (info) at (215.9/2,279.4-20.45) [anchor=north,shape=rectangle,text width=170mm,text centered] {\n";
    print "  \\textit{\\Large ${author}}\n\n";
    print "  \\vspace{2.5mm}\n\n";
    print "  \\textit{\\Large ${title}}\n\n";
    print "  \\vspace{2.5mm}\n\n";
    print "  ${date}\n";
    print "};\n";
    print "\\draw (node cs:name=info,anchor=south)\n";
    print "  ++(-215.90/2+20.45,0) rectangle (215.90-20.45,279.4-20.45);\n";
    print "\\draw[black!50] (node cs:name=info,anchor=south)\n";
    print "  ++(-215.90/2+20.45+25,-10-12.5) rectangle +(175-25,12.5)\n";
    print "  ++(-5,0) node [anchor=south east] {Counted by:};\n";
    foreach $y (@y) {
	print "\\scoringline{".$y.'}{'.$name{$y}."}\n";
    }
    print "\\node at (5,$writeintxt) [anchor=south west] {Write-in candidates:};\n";
    foreach $y (@wiy) {
	print "\\writein{$y}\n";
    }
    print "\\node at (20.45,15.0) [anchor=south west] {\n";
    print "  \\ttfamily\\textbf{$page_id}\n";
    print "};\n";
    print "\\node at (215.90/2,15.0) [anchor=south] {\n";
    print "  \\ttfamily\\textbf{$global_id}\n";
    print "};\n";
    print "\\node at (215.90-20.45,15.0) [anchor=south east] {\n";
    # 0001 is the page number
    print "  \\ttfamily\\textbf{$survey_id}\\ 0001\n";
    print "};\n";
    print "\\end{tikzpicture}\n";
    print "\\clearpage\n";
}
print "\\end{document}\n";