This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pl
executable file
·531 lines (464 loc) · 12.4 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/usr/bin/env perl
use v5.010;
use strict;
use warnings;
# automate some error handling
use autodie;
## LOCAL MODULES
# make local dir accessible for use statements
use FindBin qw( $RealBin );
use lib $RealBin;
# Wrapper for PDF functions
use Document;
# All 3d generator stuff
use Image;
# All text generator stuff
use Text;
##############################################################################
# CONFIG
use constant {
POVRAY => '/usr/local/bin/povray37',
DPI => 300,
QUALITY => 11,
ANTIALIAS => 0,
OPTIPNG => '/usr/local/bin/optipng',
#OPTIPNG => '',
DEBUG => 1,
};
# layout dimensions etc
use constant {
MARGIN => 18,
PAGE_W => 612,
PAGE_H => 792,
};
##############################################################################
# HELPER FUNCTIONS
# helper: choose one item from a list
sub _pick { $_[int(rand @_)] }
# choose item with probability X
#sub chance { rand() < $_[0] }
# helper: create a single-use scene with our defaults
# and prepopulate it
sub generate_scene
{
my $scene = Image->new(
povray => POVRAY,
dpi => DPI,
quality => QUALITY,
antialias => ANTIALIAS,
optipng => OPTIPNG,
debug => DEBUG
);
$scene->generate(@_);
return $scene;
}
##############################################################################
# LAYOUT
# The functions here do layout of a block on the page
# Global definition of all available layouts (const)
use constant LAYOUTS => (
[
# full page bleed
['2x3'],
], [
# 2x2 square, fill rest with 2x1 or a pair of 1x1
['2x2', 0],
['2x1', 2],
], [
['2x2', 0],
['1x1', 2, 0],
['1x1', 2, 1],
], [
['2x1', 0],
['2x2', 1],
], [
['1x1', 0, 0],
['1x1', 0, 1],
['2x2', 1],
], [
# Full-height columnar layouts
['1x3', 0],
['1x3', 1],
], [
['1x3', 0],
['1x2', 0, 1],
['1x1', 2, 1],
], [
['1x3', 0],
['1x1', 0, 1],
['1x2', 1, 1],
], [
['1x3', 0],
['1x1', 0, 1],
['1x1', 1, 1],
['1x1', 2, 1],
], [
['1x2', 0, 0],
['1x1', 2, 0],
['1x3', 1],
], [
['1x1', 0, 0],
['1x2', 1, 0],
['1x3', 1],
], [
['1x1', 0, 0],
['1x1', 1, 0],
['1x1', 2, 0],
['1x3', 1],
], [
# 1x2 blocks with 1x1 filler
['1x2', 0, 0],
['1x1', 2, 0],
['1x2', 0, 1],
['1x1', 2, 1],
], [
['1x2', 0, 0],
['1x1', 2, 0],
['1x1', 0, 1],
['1x2', 1, 1],
], [
['1x1', 0, 0],
['1x2', 1, 0],
['1x2', 0, 1],
['1x1', 2, 1],
], [
['1x1', 0, 0],
['1x2', 1, 0],
['1x1', 0, 1],
['1x2', 1, 1],
], [
# Single 1x2
['1x2', 0, 0],
['1x1', 2, 0],
['1x1', 0, 1],
['1x1', 1, 1],
['1x1', 2, 1],
], [
['1x1', 0, 0],
['1x2', 1, 0],
['1x1', 0, 1],
['1x1', 1, 1],
['1x1', 2, 1],
], [
['1x1', 0, 0],
['1x1', 1, 0],
['1x1', 2, 0],
['1x2', 0, 1],
['1x1', 2, 1],
], [
['1x1', 0, 0],
['1x1', 1, 0],
['1x1', 2, 0],
['1x1', 0, 1],
['1x2', 1, 1],
], [
# Mixed wide and tall rectangles
['1x2', 0, 0],
['1x2', 0, 1],
['2x1', 2],
], [
['2x1', 0],
['1x2', 1, 0],
['1x2', 1, 1],
], [
# Very broken layouts
['1x2', 0, 0],
['1x1', 0, 1],
['1x1', 1, 1],
['2x1', 2],
], [
['1x1', 0, 0],
['1x1', 1, 0],
['1x2', 0, 1],
['2x1', 2],
], [
['2x1', 0],
['1x2', 1, 0],
['1x1', 1, 1],
['1x1', 2, 1],
], [
['2x1', 0],
['1x1', 1, 0],
['1x1', 2, 0],
['1x2', 1, 1],
], [
# Various combinations of 2x1 blocks
['2x1', 0],
['2x1', 1],
['2x1', 2],
], [
['2x1', 0],
['2x1', 1],
['1x1', 2, 0],
['1x1', 2, 1],
], [
['2x1', 0],
['1x1', 1, 0],
['1x1', 1, 1],
['2x1', 2],
], [
['2x1', 0],
['1x1', 1, 0],
['1x1', 1, 1],
['1x1', 2, 0],
['1x1', 2, 1],
], [
['1x1', 0, 0],
['1x1', 0, 1],
['2x1', 1],
['2x1', 2],
], [
['1x1', 0, 0],
['1x1', 0, 1],
['2x1', 1],
['1x1', 2, 0],
['1x1', 2, 1],
], [
['1x1', 0, 0],
['1x1', 0, 1],
['1x1', 1, 0],
['1x1', 1, 1],
['2x1', 2],
], [
['1x1', 0, 0],
['1x1', 0, 1],
['1x1', 1, 0],
['1x1', 1, 1],
['1x1', 2, 0],
['1x1', 2, 1],
]
);
# Single function for layouts to call
sub layout
{
# dimensions on page in pt
my ($doc, $x, $y, $w, $h, $align, $size) = @_;
my @sizes = (
[ 10, 9 ],
[ 12, 10 ],
[ 16, 14 ]
);
# Add some text to the page
my $text = Text::generate_text();
# Place text on page.
my $scene = generate_scene();
if ($align eq 'top') {
my $block_height = $doc->border_text($x, $y, 'top', $w,
{ string => $text->{title}, param => { font => 'helvetica_bold', size => $sizes[$size][0], w => $w } },
{ string => $text->{description}, param => { size => $sizes[$size][1], w => $w } },
{ string => "Location: " . $text->{location} . " | " . $text->{price}, param => { font => 'helvetica_bold', size => $sizes[$size][1], w => $w }}
);
###
# Render a picture of the object.
my $png = $scene->render($w, $h - $block_height);
$doc->image($png, $x, $y - $h, $w, $h - $block_height);
} elsif ($align eq 'left') {
my $block_width = $doc->border_text($x, $y, $align, $h,
{ string => $text->{title}, param => { font => 'helvetica_bold', size => $sizes[$size][0], w => $w / 3, h => $h } },
{ string => $text->{description}, param => { size => $sizes[$size][1], w => $w / 3, h => $h } },
{ string => "Location: " . $text->{location} . " | " . $text->{price}, param => { font => 'helvetica_bold', size => $sizes[$size][1], w => $w / 3, h => $h }}
);
###
my $png = $scene->render($w - $block_width, $h);
$doc->image($png, $x + $block_width, $y - $h, $w - $block_width, $h);
} else {
my $block_width = $doc->border_text($x + (2 * $w / 3), $y, $align, $h,
{ string => $text->{title}, param => { font => 'helvetica_bold', size => $sizes[$size][0], w => $w / 3, h => $h } },
{ string => $text->{description}, param => { size => $sizes[$size][1], w => $w / 3, h => $h } },
{ string => "Location: " . $text->{location} . " | " . $text->{price}, param => { font => 'helvetica_bold', size => $sizes[$size][1], w => $w / 3, h => $h }}
);
###
my $png = $scene->render($w - $block_width, $h);
$doc->image($png, $x, $y - $h, $w - $block_width, $h);
}
}
# Full page spread (2x3)
sub layout_2x3
{
my ($doc) = @_;
# dimensions on page in pt
my $x = MARGIN;
my $y = PAGE_H - MARGIN;
my $w = PAGE_W - (2 * MARGIN);
my $h = PAGE_H - (2 * MARGIN);
layout($doc, $x, $y, $w, $h, 'top', 2);
}
# 2x2 square layout
sub layout_2x2
{
my ($doc, $row) = @_;
my $x = MARGIN;
my $w = PAGE_W - (2 * MARGIN);
# compute box height, which should be 2/3 of a full page,
# less a margin and a half
my $h = (2 * PAGE_H - (5 * MARGIN)) / 3;
my $y;
if ($row == 0) {
$y = PAGE_H - MARGIN;
} else {
$y = $h + MARGIN;
}
layout($doc, $x, $y, $w, $h, 'top', 2);
}
# 2x1 wide rectangular layout
sub layout_2x1
{
my ($doc, $row) = @_;
my $x = MARGIN;
my $w = PAGE_W - (2 * MARGIN);
# compute box height, which should be 1/3 of a full page, less margins
my $h = (PAGE_H - (4 * MARGIN)) / 3;
my $y;
if ($row == 0) {
$y = PAGE_H - MARGIN;
} elsif ($row == 1) {
$y = (PAGE_H + $h) / 2;
} else {
$y = $h + MARGIN;
}
layout($doc, $x, $y, $w, $h, _pick('left', 'right'), 1);
}
# 1x3 tall whole-column layout
sub layout_1x3
{
my ($doc, $col) = @_;
my $y = PAGE_H - MARGIN;
my $h = PAGE_H - (2 * MARGIN);
# compute box width, which is 1/2 a full page, minus center + edge margins
my $w = (PAGE_W / 2) - (1.5 * MARGIN);
my $x;
if ($col == 0) {
$x = MARGIN;
} else {
$x = (PAGE_W / 2) + (0.5 * MARGIN);
}
layout($doc, $x, $y, $w, $h, 'top', 2);
}
# 1x2 tall column
sub layout_1x2
{
my ($doc, $row, $col) = @_;
# compute box width, which is 1/2 a full page, minus center + edge margins
my $w = (PAGE_W / 2) - (1.5 * MARGIN);
my $x;
if ($col == 0) {
$x = MARGIN;
} else {
$x = (PAGE_W / 2) + (0.5 * MARGIN);
}
# compute box height, which should be 2/3 of a full page,
# less a margin and a half
my $h = (2 * PAGE_H - (5 * MARGIN)) / 3;
my $y;
if ($row == 0) {
$y = PAGE_H - MARGIN;
} else {
$y = $h + MARGIN;
}
layout($doc, $x, $y, $w, $h, 'top', 2);
}
# Single block 1x1 layout
sub layout_1x1
{
my ($doc, $row, $col) = @_;
# compute box height, which should be 1/3 of a full page, less margins
my $h = (PAGE_H - (4 * MARGIN)) / 3;
my $y;
if ($row == 0) {
$y = PAGE_H - MARGIN;
} elsif ($row == 1) {
$y = (PAGE_H + $h) / 2;
} else {
$y = $h + MARGIN;
}
# compute box width, which is 1/2 a full page, less margins
my $w = (PAGE_W / 2) - (1.5 * MARGIN);
my $x;
if ($col == 0) {
$x = MARGIN;
} else {
$x = (PAGE_W / 2) + (0.5 * MARGIN);
}
layout($doc, $x, $y, $w, $h, _pick('left', 'right', 'top'), 0);
}
##############################################################################
##############################################################################
##############################################################################
# MAIN ENTRY POINT
##############################################################################
##############################################################################
##############################################################################
# Create a blank PDF file
my $doc = new Document('out.pdf');
# FRONT COVER
{
say "Front Cover...";
# Add a blank page
$doc->add_page();
# Make a big fancy image for the background.
my $scene = generate_scene( object => 'Image::Object::Ship' );
my $png = $scene->render(PAGE_W, PAGE_H);
$doc->image( $png, 0, 0, PAGE_W, PAGE_H);
# Add some text overlay on the page.
$doc->text('Starship Trader Monthly', PAGE_W / 2, PAGE_H - (2 * MARGIN), { font => 'helvetica_bold', size => 72, align => 'center', color => 'white' });
$doc->text('November 3519 Edition', PAGE_W / 2, 600, { font => 'helvetica', size => 16, align => 'center', color => 'white' });
$doc->text("The galaxy's most trusted source for space vehicles.", 60, 250, { font => 'helvetica_bold', size => 24, align => 'left', color => 'yellow', w => 128 });
$doc->text('New, Used, Salvage: Find It Here!', PAGE_W - 60, 202, { font => 'helvetica_bold', size => 24, align => 'right', color => 'orange', w => 128 });
}
# Add a blank page (inside cover)
$doc->add_page();
# TITLE PAGE
{
say "Title Page...";
$doc->add_page();
# Add some text to the page
$doc->text('Starship Trader Monthly', 306, 700, { font => 'helvetica_bold', size => 20, align => 'center' });
$doc->text('A NaNoGenMo 2019 entry.', 306, 680, { font => 'helvetica', size => 16, align => 'center' });
$doc->text('Written by the open-source "Star and Driver" software', 306, 660, { font => 'helvetica', size => 16, align => 'center' });
$doc->text('(https://github.com/greg-kennedy/StarAndDriver),', 306, 640, { font => 'helvetica', size => 16, align => 'center' });
$doc->text('by Greg Kennedy ([email protected]).', 306, 620, { font => 'helvetica', size => 16, align => 'center' });
$doc->text('Generated on ' . scalar(localtime()) . '.', 306, 100, { font => 'helvetica', size => 16, align => 'center' });
}
# Add a blank page (title back)
$doc->add_page();
# CONTENT PAGES
# Loop N pages
for (my $i = 0; $i < 48; $i ++)
{
say "page $i...";
$doc->add_page();
# Big switch block to fill each layout
my $pattern = (LAYOUTS)[int(rand(scalar(LAYOUTS)))];
for (my $j = 0; $j < scalar @$pattern; $j ++) {
my $block = $pattern->[$j];
print " . block " . ($j+1) . " of " . scalar(@$pattern) . " (type " . $block->[0] . ")\n";
if ($block->[0] eq '2x3') {
layout_2x3($doc);
} elsif ($block->[0] eq '2x2') {
layout_2x2($doc, $block->[1]);
} elsif ($block->[0] eq '2x1') {
layout_2x1($doc, $block->[1]);
} elsif ($block->[0] eq '1x3') {
layout_1x3($doc, $block->[1]);
} elsif ($block->[0] eq '1x2') {
layout_1x2($doc, $block->[1], $block->[2]);
} else {
layout_1x1($doc, $block->[1], $block->[2]);
}
}
}
# Add a blank page (back cover reverse
$doc->add_page();
# BACK COVER
{
say "Back Cover...";
$doc->add_page();
# Back cover is just a starfield
my $scene = generate_scene( object => 'NONE' );
my $png = $scene->render(PAGE_W, PAGE_H);
$doc->image( $png, 0, 0, PAGE_W, PAGE_H);
# ascii "NANO" + 2019 + check digit
$doc->barcode( '7865787920193', 456, 54 );
}
# Save the PDF
$doc->close();