-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgranogen.pl
1095 lines (686 loc) · 25.7 KB
/
granogen.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/local/perl
########################################################
# TODO ################################################
#
# X randomize offsets for the fill images (I think I got it; hard to test for though)
# make some alternate panels
# X make alternate layouts
# X an about page
# assembling the pages
# - pdf
# - cbr
# - reveal.js
# X finish title page layout
# X key the text loaded to the videodownloaded's creation date
# X keep used fill images for collaging onto the cover
my $start = time;
use JSON::Parse qw(parse_json json_file_to_perl);
use Date::Parse;
use List::MoreUtils qw(uniq);
use List::Util qw(shuffle);
use Date::Format;
use Data::Dumper;
# some global variables
my (%chapterInfo, @chapters, @chaps, $bookTitle, $pageDate, %usedText);
# for testing
my $verbose = 1;
# 10 chapters of 25 pages each. I don't know why but I needed to spell out each valuelike this.
my %chapters = (
'1' => '',
'2' => '',
'3' => '',
'4' => '',
'5' => '',
'6' => '',
'7' => '',
'8' => '',
'9' => '',
'10' => ''
);
# Plus some front matter, added after the fact based on images & text collected during generation
my $pn = 0; # overall page number incrementer
# make the pages
for ($ch = 1; $ch <= 10; $ch++){ #chapter counter
my $chpTitleN;
my $chpCoverImg;
$chapterInfo{$ch} = {'pn' => '', 'title' => '', 'img' => ''};
for ($chpn = 1; $chpn <= 25; $chpn++){ # the page within the chapter, eventually, 25
$pn += 1; # increment the actual page number
# some chapter-page positions have special roles
if ($chpn == 1){
# generate a chapter title page
print "$pn: title page\n" if $verbose == 1;
# skip it for now, but store some data
$chapterInfo{$ch}->{pn} = $pn;
# placeholder
$ppn = sprintf("%05d", $pn);
$blank = `convert -size 1000x1600 xc:white img/tmp/pages/page-$ppn.png`;
}elsif($chpn == 2){
# leave it blank
print "$pn: blank page\n" if $verbose == 1;
# simple blank page with no visible number
$ppn = sprintf("%05d", $pn);
$blank = `convert -size 1000x1600 xc:white img/tmp/pages/page-$ppn.png`;
}elsif($chpn =~ /3|8|12|16|20/){
# make it one of the alternative layouts
print "$pn: alt layout\n" if $verbose == 1;
makeAltLayoutPage();
open $write, ">>dump.txt";
print $write Dumper(%usedText);
close $write;
}elsif($chpn == 24){ # easier to make this an even number, like 24
# end on a full page panel
if ($chpn % 2 == 1){
# generate the splash page
# generate a blank page
print "$pn: splash page\n" if $verbose == 1;
makeChapterEndPage();
print "$pn: blank page\n" if $verbose == 1;
$ppn = sprintf("%05d", $pn);
$blank = `convert -size 1000x1600 xc:white img/tmp/pages/page-$ppn.png`;
}else{
# generate the splash page
print "$pn: splash page\n" if $verbose == 1;
makeChapterEndPage();
}
}else{
# make a regular page
print "$pn: regular page\n" if $verbose == 1;
makeRegularPage();
open $write, ">>dump.txt";
print $write Dumper(%usedText);
close $write;
if (length($chapters{$ch}) == 0){
print "I should look for a new title.\n";
# check for a new chapter title
@legs = getLegs();
foreach (shuffle @legs){
if (/(\w+?ing)/ & length($1) > 5){
$chapters{$ch} = "THE " . uc($1);
}
}
}
}
}
}
# Now that the pages are generated, figure out the chapter titles
foreach (sort {$a <=> $b} keys %chapters){
print "Chapter $_ is called $chapters{$_}\n";
push(@chaps, $chapters{$_});
}
@chaptitles = themeChapters(@chaps);
# figure out the booktitle and set to $bookTitle
$bookTitle = "$chaps[0], $chaps[1] and $chaps[2]";
for (my $c = 1; $c <= $#chaptitles + 1; $c++){
$chapterInfo{$c}->{title} = $chaptitles[$c-1];
}
foreach (sort {$a <=> $b} keys %chapterInfo){
@info = ($_, $chapterInfo{$_}->{pn}, $chapterInfo{$_}->{title}, $chapterInfo{$_}->{img});
#print "Making title page for Chapter $ch, Page $pn, titled $title, with $img for the cover.\n";
makeChapterTitlePage(@info);
}
# make front matter
makeFrontCover();
makeAboutPage();
makeTitlePage();
makeToc();
makeBackCover();
# assemble
# this is going to be ugly
print "Assembling...\n";
# a general purpose blank page
system("convert -size 1000x1600 xc:white img/tmp/pages/blank.png");
# front matter
my @frontMatter = ("cover", "blank", "titlePage", "about", "toc", "blank");
# makeBackCover();
my @backMatter = ("end", "blank", "backCover");
# make a PDF
print "Making a PDF .. \n";
system("mogrify -format pdf img/tmp/pages/*.png");
$pdffront = join(" ", map {"img/tmp/pages/" . $_ . ".pdf"} @frontMatter);
my @pdfpages = glob "img/tmp/pages/page-*.pdf";
$pdfs = join(" ", @pdfpages);
$pdfback = join(" ", map {"img/tmp/pages/" . $_ . ".pdf"} @backMatter);
system("pdftk $pdffront $pdfs $pdfback cat output output/book.pdf");
#make a CBR
print "Making a CBR...\n";
system("mogrify -format jpg -quality 80 img/tmp/pages/*.png");
@jpgfront = map {"img/tmp/pages/" . $_ . ".jpg"} @frontMatter;
@jpgpages = glob "img/tmp/pages/page-*.jpg";
@jpgback = map {"img/tmp/pages/" . $_ . ".jpg"} @backMatter;
my $cbpn = 0;
foreach(@jpgfront){
$tg = $_;
$tg =~ s/img\/tmp\/pages\///i;
$ppn = sprintf("%03d", $cbpn);
system("mv $_ img/tmp/pages/$ppn-$tg");
$cbpn += 1;
}
foreach(@jpgpages){
$tg = $_;
$tg =~ s/img\/tmp\/pages\///i;
$ppn = sprintf("%03d", $cbpn);
system("mv $_ img/tmp/pages/$ppn-$tg");
$cbpn += 1;
}
foreach(@jpgback){
$tg = $_;
$tg =~ s/img\/tmp\/pages\///i;
$ppn = sprintf("%03d", $cbpn);
system("mv $_ img/tmp/pages/$ppn-$tg");
$cbpn += 1;
}
#$cbrpages = join(" ", @jpgfront) . " " . join(" ", @jpgpages) . " " . join(" ", @jpgback);
@cbrpages = glob "img/tmp/pages/*.jpg";
$cbrs = join (" ", @cbrpages);
system("rar a output/book.cbr $cbrpages");
print "Done!?\n";
system("cp output/book.cbr /home/zach/Dropbox/nagro.cbr");
system("cp output/book.pdf /home/zach/Dropbox/nagro.pdf");
exit;
########################################################
# SUBS #################################################
########################################################
sub themeChapters {
@chaps = @_;
foreach $chap (@chaps){
my @pos;
for (my $b = 0; $b <= $#chaps; $b++){
if ($chap eq $chaps[$b]){
push(@pos, $b);
}
}
#print "$chap @pos\n";
if (scalar (@pos) > 1){
my $part = 1;
foreach (@pos){
$chaps[$_] = $chaps[$_] . ", PART $part";
$part += 1;
}
}
}
return @chaps;
}
sub query {
my $pn = @_[0];
while( length($pn) < 4){
$pn = '0' . $pn;
}
my @templates = (
# This list of options comes from an earlier project I did kind of like this.
# A few more I found on underviewed.com
'SAM_n.MP4',
'VIDEOn.MOV',
'CAM0n.MP4',
'RECn.MOV',
'n.mov',
'IMG_n.MOV',
'GOPRn.MP4',
'Movie n.mov',
'IMGn.MOV',
'MVI_n.MP4',
'DSCNn.MOV',
'VIDEOn.MOV',
'n.mts'
);
# I should be using sprintf, but whatever. Don't judge me.
$template = $templates[int(rand($#templates))];
$template =~ s/n\./$pn\./;
$template =~ s/ 0+/%20/g;
return $template;
}
sub drawRect {
my ($canvas, $fill, $width, $height, $xoffset, $yoffset) = @_;
#make the top edge
#the top left corner is where the image files hit, which is about 5px beyond the visible line edge
#so width doesn't include gutter, basically
$line = "img/sources/line1.png";
$seed = int(rand(2000 - $width - 10));
system("convert $line -crop 10x$width+0+$seed -rotate 90 -alpha set img/tmp/lineT.png");
$seed = int(rand(2000 - $width - 10));
system("convert $line -crop 10x$width+0+$seed -rotate 90 -alpha set img/tmp/lineB.png");
$seed = int(rand(2000 - $height - 10));
system("convert $line -crop 10x$height+0+$seed -alpha set img/tmp/lineL.png");
$seed = int(rand(2000 - $height - 10));
system("convert $line -crop 10x$height+0+$seed -alpha set img/tmp/lineR.png");
my $tx = $xoffset + 5;
my $ty = $yoffset;
my $lx = $xoffset;
my $ly = $yoffset + 5;
my $rx = $xoffset + $width;
my $ry = $yoffset + 5;
my $bx = $xoffset + 5;
my $by = $yoffset + $height;
# fill it in with the image
drawImage($canvas, $fill, $width + 7, $height + 7, $xoffset + 7 , $yoffset + 7);
# apply the borders
system("convert $canvas -page +$tx+$ty img/tmp/lineT.png -page +$lx+$ly img/tmp/lineL.png -page +$rx+$ry img/tmp/lineR.png -page +$bx+$by img/tmp/lineB.png -layers flatten $canvas");
}
sub drawImage {
my ($canvas, $fill, $width, $height, $xoffset, $yoffset) = @_;
@details = split(" ", `identify $fill`);
($imgWidth, $imgHeight) = split("x", $details[2]);
$targetWidth = $width - 10;
$targetHeight = $height - 10;
my $tg = $targetWidth . "x" . $targetHeight . "+" . int(rand($width - $targetWidth)) ."+" . int(rand($height - $targetHeight)) ;
# is it big enough?
if ($imgWidth > $width & $imgHeight > $height){
system("convert $fill -crop $tg -paint 5 img/tmp/fill.png");
}else{
my $tw = $targetWidth * 2 . 'x<';
my $th = $targetHeight * 2;
#scale it up
#system("convert $fill -resize 'x$th' -resize '$tw' -resize 50% -gravity center -crop $tg +repage -colorspace gray -sketch 0x20+120 fill.png");
system("convert $fill -resize 'x$th' -resize '$tw' -resize 50% -gravity center -crop $tg +repage -paint 5 img/tmp/fill.png");
}
# do filtering here
system("convert $canvas -page +$xoffset+$yoffset img/tmp/fill.png -layers flatten $canvas");
}
sub drawPanel {
my ($canvas, $img, $text, $width, $height, $xoffset, $yoffset) = @_;
$maxIntWidth = $width - 20;
$maxIntHeight = $height - 20;
$maxWidthTxt = (($maxIntWidth - 30) * .7) . 'x';
print "Writing my text to an initial image file\n";
$txtImg = `convert -background '#ffffff' -fill \"#555555\" -font DigitalStrip-2.0-BB-Regular -pointsize 24 -size $maxWidthTxt caption:'$text' -bordercolor '#ffffff' -border 12x12 img/tmp/text.png`;
@details = split(" ", `identify img/tmp/text.png`);
($txtWidth, $txtHeight) = split("x", $details[2]);
if ($txtHeight < ($maxIntHeight * .3)){
# interior text
print "Putting text inside the panel\n";
# make a panel first
drawRect($canvas, $img, $width, $height, $xoffset, $yoffset);
# pick a location
$x = int(rand($maxIntWidth - $txtWidth - 10)) + $xoffset;
$y = int(rand($maxIntHeight - $txtHeight - 10)) + $yoffset;
$txtImg = `convert $canvas -page +$x+$y img/tmp/text.png -layers flatten $canvas`;
unlink("img/tmp/text.png");
}else{
# exterior text
print "Putting text outside the panel\n";
# make a new text image
$txtImg = `convert -background '#ffffff' -fill \"#555555\" -font DigitalStrip-2.0-BB-Regular -pointsize 24 -size $width caption:'$text' -bordercolor '#ffffff' -border 5x5 img/tmp/text.png`;
@details = split(" ", `identify img/tmp/text.png`);
($txtWidth, $txtHeight) = split("x", $details[2]);
if ($txtHeight < ($maxIntHeight * .4)){
# stick it on the canvas
$placeText = `convert $canvas -page +$xoffset+$yoffset img/tmp/text.png -layers flatten $canvas`;
# make a textless panel
drawRect($canvas, $img, $width, $height - $txtHeight, $xoffset, $yoffset + $txtHeight - 8);
}else{
print "Actually the text is to big so get rid of the image\n";
$textgeo = $maxIntWidth . "x" . $maxIntHeight;
# just make the text into a panel
$txtImg = `convert -background '#ffffff' -fill \"#555555\" -font DigitalStrip-2.0-BB-Regular -gravity center -size $textgeo caption:'$text' -bordercolor '#ffffff' -border 5x5 img/tmp/text.png`;
$placeText = `convert $canvas -page +$xoffset+$yoffset img/tmp/text.png -layers flatten $canvas`;
}
unlink("img/tmp/text.png");
}
}
sub makeText {
#$length = @_[0] + 1;
$length = @_[0] ? @_[0] > 0 : int(rand(2)) + 1;
my @legs = getLegs();
my @templates = (
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, and %s. ",
"%s. ",
"%s, but %s. ",
"%s, %s, but %s. ",
"Eventually, %s. ",
"But after %s, %s. ".
"%s; %s. ",
"%s, which made us realize %s. ",
"Finally, %s. ",
"Even though %s, %s. ",
"%s -- $s. ",
"Back then, %s. ",
"%s, and then %s. ",
"Meanwhile, %s. ",
"%s, and %s at last. ",
"%s, %s and finally %s. "
);
$text = '';
for ($l = 0; $l < $length; $l++){
$template = $templates[int(rand($#templates))];
my @use = shuffle @legs;
$usedText{"$use[0]"} = 'y';
$usedText{"$use[1]"} = 'y';
$text .= ucfirst ( sprintf($template, @use));
$text =~ s/\s(\.|\,|\;|\:)/$1/ig;
}
# some problems with quote marks I think
$text =~ s/\'|\"|\`//ig;
return $text;
}
sub getFrame {
my @frames = glob "img/tmp/frames/*.png";
$frame = $frames[int(rand($#frames))];
my $f = `cp $frame img/tmp/used/`;
return $frame;
}
sub getLegs {
# later, get this as a parameter
$string = $pageDate;
# $string = str2time("2013-11-30");
print "Getting text from $pageDate\n";
my @tweets;
my @legs;
$txtOffset = 0;
$url = 'http://otter.topsy.com/search.js?';
$runCount = 0;
while (scalar @goodlegs < 30){
$txtOffset += 100 * $runCount;
%params = (
'q' => '%23tbt+when',
'apikey' => '09C43A9B270A470B8EB8F2946A9369F3', # I don't know how long this one will work, but should be able to switch out later if need
'type' => 'tweet',
'offset' => '0',
'perpage' => '100',
'sort' => 'date',
'maxtime' => $string,
'offset' => $txtOffset
);
foreach (keys %params){
$url .= $_ . "=" . $params{$_} . '\&';
}
$result = `curl $url`;
$data = parse_json($result);
#print $result;
foreach (@{$data->{response}->{list}}){
$twt = $_->{title};
# Some filters:
$twt =~ s/#.+?(\s|$)/it /ig; # replace hashtags with "it"
$twt =~ s/http.+?(\s|$)//ig; # remove links
$twt =~ s/\@.+?(\s|$)/you /ig; # replace mentions with "you"
push(@tweets, $twt);
}
print "Tweets: " . scalar(@tweets) . "\n";
if (scalar(@tweets) == 0){
print "Result: $result";
}
foreach (@tweets){
if (/when\s+?(.+?)([\?\!\.\;\,\:\&\-\"]|$)/ig){
my $leg = $1;
$leg =~ s/[^\w\s\d]//ig;
unless($usedText{"$leg"} eq 'y'){
push (@legs, $leg);
}
}
}
@goodlegs = uniq @legs;
}
print "Legs: " . scalar(@goodlegs) . "\n";
return @goodlegs;
}
sub getVideo {
if (defined(@_[0])){
$srcs = $_[0];
}else{
# how many source vids? (up to 3)
$srcs = int(rand(3)) + 1;
}
@kept = ();
while (scalar(@kept) < $srcs){
$query = query($pn);
print "Query: $query\n";
$yturl = 'https://gdata.youtube.com/feeds/api/videos?alt=jsonc&v=2&lclk=video&format=5&duration=short&orderby=viewCount&q=allintitle:"' . $query . '"';
$init_result = `curl '$yturl'`;
$init_data = parse_json($init_result);
if ($init_data->{'data'}->{'totalItems'} > 25){
$offset = abs(int(rand($init_data->{'data'}->{'totalItems'})) - 25);
$ytdataURL = 'https://gdata.youtube.com/feeds/api/videos?alt=jsonc&v=2&lclk=video&format=5&duration=short&orderby=viewCount&q=allintitle:"' .$query . '"&start-index=' . $offset;
$result = `curl '$ytdataURL'`;
$data = parse_json($result);
@items = @{$data->{'data'}->{'items'}};
}else{
@items = @{$init_data->{'data'}->{'items'}};
}
foreach my $item (@items){
my %vid = %{$item};
if ($vid{'viewCount'} < 10 & $vid{'description'} == 0 & scalar(@kept) < $srcs){
push (@kept, $vid{'id'});
}
}
}
my @videoDates;
foreach (@kept){
# download into tmp/mov folder
system("youtube-dl https://www.youtube.com/watch?v=$_ -o \"img/tmp/mov/\%\(id\)s.\%\(ext\)s\"");
# apparently avconv sends its output only to stderr for some reason, so 2>&1 redirects stderr back to stdout
$extract = `avconv -i img/tmp/mov/$_.mp4 -r 1 img/tmp/frames/$_-%05d.png 2>&1`;
my @extracts = split("\n", $extract);
foreach (@extracts){
if (/creation_time\s+?:\s+(\d{4}-\d{2}-\d{2}\s+?\d{2}:\d{2}:\d{2})/m){
print "found a creation date. Maybe? $1\n";
push(@videoDates, str2time($1));
}
}
unlink("img/tmp/mov/$_.mp4") or die "Couldn't unlink mp4 : $!\n\n";
}
# set the operative timestamp for text
@videoDates = sort {$a <=> $b} @videoDates;
print "Candidate: $videoDates[0]\n";
$oldest = str2time("2012-01-01");
if ($videoDates[0] > $oldest){
$pageDate = $videoDates[0];
}else{
print "No in-range dates. Randomly picking a date.";
$pageDate = int(rand(str2time("2014-11-30") - $oldest)) + $oldest;
}
}
sub makeRegularPage {
# make a regular page
# (should already have $pn from context)
getVideo();
print "Making regular page $pn with pageDate $pageDate ...\n";
# generate content area
system("convert -size 1000x1600 xc:white img/tmp/layout.png");
# assuming three rows still
for ($row = 0; $row <= 2; $row++){
my $yoff = $row * 436 + $row * 15;
my $panes = int(rand(3)) + 1;
$width = (1000 - 200) / ($panes);
for ($p = 0; $p < $panes; $p++){
# draw a rectangle (move into panel sub later)
# assume full width area is 1000
# with 100px margin, panels are
$txt = makeText();
$frame = getFrame();
drawPanel("img/tmp/layout.png", $frame, $txt, $width - 10, 436, $width * $p + 100, $yoff + 80);
# destroy it (later)
}
}
addPageNumber($pn);
$ppn = sprintf("%05d", $pn);
system ("mv img/tmp/layout.png img/tmp/pages/page-$ppn.png");
cleanUp();
}
sub makeChapterEndPage {
# (should already have $pn from context)
# also should have $chpTitleN from context, which assumes that I'm making these one full chapter at a time
# this is a full-sized splash page
getVideo(1);
print "Making endpage page $pn with pageDate $pageDate ...\n";
$frame = getFrame();
# also move a copy of this image to the chapter cover image folder
system("cp $frame img/tmp/covers/ch-$ch.png");
$chapterInfo{$ch}->{img} = "img/tmp/covers/ch-$ch.png";
# my ($canvas, $img, $text, $width, $height, $xoffset, $yoffset)
my $layout = `convert -size 1000x1600 xc:white img/tmp/layout.png`;
my $text = makeText(1);
drawPanel("img/tmp/layout.png", $frame, $text, 750, 1300, 125, 125);
addPageNumber($pn);
$ppn = sprintf("%05d", $pn);
system("mv img/tmp/layout.png img/tmp/pages/page-$ppn.png");
cleanUp();
}
sub makeChapterTitlePage {
# get details from %chapterInfo
my ($ch, $pn, $title, $img) = @_;
print "Making title page for Chapter $ch, Page $pn, titled $title, with $img for the cover.\n";
$page = `convert -size 1000x1600 xc:white img/tmp/layout.png`;
$make = `convert img/tmp/layout.png -fill '#222222' -font ManlyMen-BB-Regular -pointsize 60 -gravity north -annotate 0 '\\n\\n\\n\\nChapter $ch:' -pointsize 80 -gravity north -annotate 0 '\\n\\n\\n\\n$title' img/tmp/layout.png`;
my $tw = 500 * 5 . 'x<';
my $th = 400 * 5;
my $sktchgeo = int(rand(15)) + 1 . 'x20+' . int(rand(150)) + 50;
my $cropgeo = "700x700+" . int(rand(300)). "+" . int(rand(300));
$make = `convert $img -resize 'x$th' -resize '$tw' -resize 50% -gravity center -crop $cropgeo +repage -paint 10 -colorspace gray img/tmp/fill.png`;
$make = `convert img/tmp/fill.png -alpha set -virtual-pixel transparent -channel A -blur 10x40 -level 90%,100% +channel -layers flatten img/tmp/fill.png`;
$make = `convert img/tmp/layout.png -page +150+500 img/tmp/fill.png -layers flatten img/tmp/layout.png`;
$ppn = sprintf("%05d", $pn);
system("mv img/tmp/layout.png img/tmp/pages/page-$ppn.png");
cleanUp();
}
sub makeAltLayoutPage {
# (should already have $pn from context)
# Originally I wanted more options, but for now, since regular pages are 3 rows, alt pages can be 2 or 1 row
getVideo(2);
print "Making altlayout page $pn with pageDate $pageDate ...\n";
# generate content area
system("convert -size 1000x1600 xc:white img/tmp/layout.png");
# how many rows?
my $rows = int(rand(2)) + 1;
for ($row = 0; $row < $rows; $row++){
my $rowheight = int(1320 / $rows);
my $yoff = $row * $rowheight + $row * 15;
my $panes = int(rand(3)) + 1;
if ($rows == 1 & $panes == 1){
$panes += int(rand(2)) + 1;
}
$width = (1000 - 200) / ($panes);
for ($p = 0; $p < $panes; $p++){
# draw a rectangle (move into panel sub later)
# assume full width area is 1000
# with 100px margin, panels are
$txt = makeText();
$frame = getFrame();
drawPanel("img/tmp/layout.png", $frame, $txt, $width - 10, $rowheight, $width * $p + 100, $yoff + 80);
# destroy it (later)
}
}
addPageNumber($pn);
$ppn = sprintf("%05d", $pn);
system ("mv img/tmp/layout.png img/tmp/pages/page-$ppn.png");
cleanUp();
}
sub addPageNumber {
unless (defined $pn) { $pn = @_[0]; }
system("convert img/tmp/layout.png -fill '#222222' -font ManlyMen-BB-Regular -pointsize 44 -gravity south -annotate 0 '$pn\\n' img/tmp/layout.png");
}
sub cleanUp {
my @frames = glob "img/tmp/frames/*.png";
foreach (@frames) {
unlink($_);
}
my @mov = glob "img/tmp/mov/*";
foreach (@mov){
unlink($_);
}
# my @used = glob "img/tmp/used/*";
# foreach (@used){
# unlink($_);
# }
}
sub makeTitlePage {
print "Making the title page.\n";
$f = `convert -size 1000x1600 xc:white img/tmp/titlePage.png`;
$f = `convert -fill "#222222" -font ManlyMen-BB-Regular -size 750x400 -gravity center caption:'TBT:\n$bookTitle' img/tmp/title.png`;
$f = `convert img/tmp/titlePage.png -fill "#222222" -font ManlyMen-BB-Italic -pointsize 44 -gravity south -annotate +0+500 "'by' Zach Whalen" -pointsize 40 -gravity south -annotate +0+350 "for\\nNaNoGenMo 2014" img/tmp/titlePage.png`;
# add an hr near the bottom
$line = "img/sources/line1.png";
$width = 800;
$seed = int(rand(2000 - $width - 10));
system("convert $line -crop 10x$width+0+$seed -rotate 90 -alpha set img/tmp/lineT.png");
$f = `convert -page +0+0 img/tmp/titlePage.png -page +125+525 img/tmp/title.png -page +100+900 img/tmp/lineT.png -layers flatten img/tmp/pages/titlePage.png`;
}
sub makeFrontCover {
my @used = shuffle glob "img/tmp/used/*.png";
my @blnk = (
'1,3', '2,3', '3,3', '4,3',
'1,4', '2,4', '3,4', '4,4',
'1,5', '2,5', '3,5', '4,5'
);
my %blanks;
foreach(@blnk){
$blanks{"$_"} = 1;
}
$f = `convert -size 1000x1600 xc:black img/tmp/cover.png`;
for (my $r = 0; $r <= 8; $r++){
$yoff = ($r * 200) - 100;
for (my $c = 0; $c <= 5; $c++){
$xoff = ($c * 200) - 100;
unless($blanks{"$c,$r"} == 1){
my $fill = shift @used;
unless(int(rand(12)) < 5 ){
$f = `convert $fill -colorspace gray img/tmp/fill.png`;
$fill = "img/tmp/fill.png";
}
drawRect("img/tmp/cover.png", $fill, 190, 190, $xoff, $yoff);
}
}
}
# draw the actual title ## What is the title??
#$f = `convert -size 1000x1600 xc:blue over.png`;
$f = `convert -background transparent -fill \"#fafafa\" -font ManlyMen-BB-Regular -size 750x400 -gravity center caption:'TBT:\n$bookTitle' img/tmp/title.png`;
$f = `convert -page +0+0 img/tmp/cover.png -page +125+525 img/tmp/title.png -layers flatten img/tmp/pages/cover.png`;
}
sub makeToc {
my $c = `convert -size 1000x1600 xc:white -fill "#222222" -font ManlyMen-BB-Regular -pointsize 80 -gravity north -annotate +0+200 'CONTENTS' img/tmp/toc.png`;