-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.pl
executable file
·253 lines (219 loc) · 7.04 KB
/
index.pl
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw/:standard/;
use PDF::API2;
use Data::Dumper;
use Image::Magick;
my $annotations;
read_annotations();
my $action = param("action") // '';
if($action eq "draw"){
print_page();
}else{
print_input();
}
sub imgsize {
my ($img_name) = @_;
my $img = new Image::Magick;
$img->Read($img_name);
return $img->Get("columns", "rows");
}
sub print_input{
print <<EOF;
Content-Type: text/html; charset=utf-8
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Infinity Marker Sheet Creator</title>
<style>
.markers {
display: flex;
flex-wrap: wrap;
}
.group {
margin: 10px;
}
</style>
</head>
<body>
EOF
print "<h1>Infinity Marker Sheet Creator</h1>\n";
print <<EOF;
<p>
Create a custom marker sheet by selecting the number and size of each
kind of marker you want, along with the size of paper.
When you submit, a PDF will be generated and downloaded.
</p>
<p>
For each kind of marker, an appropriate set of size choices are available.
</p>
<p>
This page features redesigned N5 tokens are created by <a href='https://forum.corvusbelli.com/threads/n4-c1-token-design-questions.37936/'>Lawson Deming</a>.
</p>
<form method='post'>
<h2>Layout</h2>
<p>
<b>Paper Size: </b>
<select name='paper'>
<option value='Letter' selected>Letter</option>
<option value='A4'>A4</option>
</select>
</p>
<p>
<b>Padding: </b>
<input type='text' name='pad_mm' value='1'>
mm
</p>
<h2>Markers</h2>
EOF
my $category = "";
my $section = "";
for my $marker (@$annotations){
if($category ne $marker->{category}){
if($category ne ""){
print "</table>\n";
print "</div>\n";
}
}
if ($section ne $marker->{section}) {
if($section ne ""){
print "</div>\n";
print "<hr>\n";
}
$section = $marker->{section};
print "<h3>$section</h3>\n";
print "<div class='markers'>\n";
}
if($category ne $marker->{category}){
$category = $marker->{category};
print "<div class='group'>\n";
print "<h4>$category</h4>\n";
print "<table>\n";
print "<tr><th></th><th>Name</th><th>Size</th><th>Count</th></tr>\n";
}
print "<tr>\n";
my ($width, $height) = imgsize($marker->{thumbfile});
print "<td><img src='$marker->{thumbfile}' height=$height width=$width></td>\n";
print "<td>$marker->{name}</td>\n";
print "<td><select name='$marker->{label}_size'>\n";
for my $size (@{$marker->{sizes}}){
my $selected = "";
if($size == $marker->{default_size}){
$selected = " selected";
}
print "<option value='$size'$selected>$size mm</option>\n";
}
print "<input type='hidden' name='$marker->{label}_default_size' value='$marker->{default_size}'>\n";
print "</select></td>\n";
print "<td> <input type=text name='$marker->{label}' value='0' size=2></td>\n";
print "</tr>\n";
}
print "</table>\n";
print "</div>\n";
print <<EOF;
</div>
<input type='hidden' name='action' value='draw'>
<input type='submit'>
</form>
<h2>Copyright Notice</h2>
<p>This tool was created by <a href='http://ghostlords.com/'>Jonathan Polley</a> to help enhance your enjoyment of Infinity the Game. Please direct any feedback to <a href='mailto:infinity\@ghostlords.com'>infinity\@ghostlords.com</a>. My other Infinity resources may be found <a href='http://infinity.ghostlords.com'>here</a>.</p>
<p><a href='http://infinitythegame.com'>Infinity the Game</a> is © Corvus Belli SLL. The sole intended purpose of this tool is to make play aids for Infinity the Game. </p>
</body>
</html>
EOF
}
sub print_page{
print <<EOF;
Content-Type: application/pdf
EOF
my $pdf = PDF::API2->new();
$pdf->info(Author => "Jonathan Polley",
Title => "Infinty Markers",
Creator => "http://infinity.ghostlords.com/",
CreationDate => [localtime],
);
my $paper_size = param('paper') // 'Letter';
$pdf->mediabox($paper_size);
my @bounds = PDF::API2::Util::page_size($paper_size);
my $min_x = 72/4.0;
my $min_y = 72/4.0;
my $max_x = $bounds[2] - 72/4.0;
my $max_y = $bounds[3] - 72/4.0;
my $x = $min_x;
my $y = $max_y;
my $max_height = 0;
my $pad_mm = param('pad_mm') // 1;
my $pad = $pad_mm / 25.4 * 72.0;
my $page = $pdf->page();
for my $marker (@$annotations){
my $num = param($marker->{label});
if($num){
my $img = $pdf->image_png($marker->{imgfile});
my ($xres, $yres) = imgsize($marker->{imgfile});
# 1 point is 1/72 inch
# Tokens by Lawson are 600 DPI
# without a $scale, renders at 1:1 pixels:points
# scale is units of points/pixel
# Resize images based on the default vs. target size in mm
my $size = param("$marker->{label}_size");
my $default_size = param("$marker->{label}_default_size");
my $scale = 72.0 / 600 * $size / $default_size;
my $scaled_width = $xres * $scale;
my $scaled_height = $yres * $scale;;
for(my $i = 0; $i < $num; $i++){
# if the row is ful, go to next row
if($x + $scaled_width > $max_x){
$y -= $max_height + $pad;
$max_height = $scaled_height;
$x = $min_x;
}
# find the tallest item in the row
if($scaled_height > $max_height){
$max_height = $scaled_height;
}
# if the page is full, go to next page
if($y - $scaled_height < $min_y){
$y = $max_y;
$max_height = $scaled_height;
$x = $min_x;
$page = $pdf->page();
}
my $gfx = $page->gfx();
$gfx->image($img, $x, $y - $scaled_height, $scale);
$x += $scaled_width + $pad;
}
}
}
print $pdf->stringify();
}
sub read_annotations{
$annotations = [];
open(my $data, '<', "annotation.csv");
while(my $line = <$data>){
chomp $line;
next if !$line;
my ($id, $name, $section, $category, $sizes, $default_size) = split /,/, $line;
$id =~ s/ /_/g;
if(!$default_size){
$default_size = 25;
}
my @label = ("marker", $id);
my $label = join("-", @label);
my $imgfile = "png/$label.png";
my $thumbfile = "thumb/$label.png";
my @sizes = split /\//, $sizes;
push @$annotations, {
id => $id,
name => $name,
imgfile => $imgfile,
thumbfile => $thumbfile,
sizes => \@sizes,
default_size => $default_size,
section => $section,
category => $category,
label => $label,
};
}
}