summaryrefslogtreecommitdiff
path: root/script/qr-color.pl
blob: 5717b276ab50f58523ff41d8caf279996b166df6 (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
#!/usr/bin/perl 
use utf8;
use strict;
use warnings;
use Text::QRCode;
use GridFiller;
use GridFiller::Status;
use GridFiller::Chooser::Smarter;
use GridFiller::Result::Pango;
use feature 'say';
use open ':std',':locale';
use Log::Log4perl qw(:easy);
use Getopt::Long;
 
Log::Log4perl->easy_init($INFO);
 
#Log::Log4perl->get_logger('GridFiller')->level($INFO); 
#Log::Log4perl->get_logger('GridFiller::Chooser')->level($DEBUG); 
#Log::Log4perl->get_logger('GridFiller::Status')->level($DEBUG); 
#Log::Log4perl->get_logger('GridFiller::Result')->level($DEBUG); 
 
my ($data_file,$words_file,$level,$size,$output_file,$font_descr)
    =(undef,undef,'M',40,'/tmp/qr.png',undef);
{
my $p = Getopt::Long::Parser->new(
    config => [qw(
                     no_auto_abbrev
                     no_getopt_compat
                     no_gnu_compat
                     require_order
                     no_ignore_case
             )],
);
die unless $p->getoptions(
    'd|data=s' => \$data_file,
    'w|words=s' => \$words_file,
    'l|level=s' => \$level,
    's|size=i' => \$size,
    'o|output=s' => \$output_file,
    'f|font=s' => \$font_descr,
);
}
 
$level='M' unless $level=~m{^[LMH]$}i;
 
my $data = do {open my $fh,'<:utf8',$data_file;local $/;<$fh>};
my @words = do {open my $fh,'<:utf8',$words_file;<$fh>};chomp @words;
 
my $qr=Text::QRCode->new(
    level=>uc($level),
    mode=>'8-bit',
);
 
my $arr=$qr->plot($data);
 
my $result = GridFiller::Result::Pango->new({
    source_grid => $arr,
    cell_size => $size,
    $font_descr ? (font_description => $font_descr) : () ),
});
 
my $status=GridFiller::Status->new({
    words=>\@words,
    grid=> $arr,
    length => $result->length_closure,
    mode => 'longest',
});
 
my $chooser=GridFiller::Chooser::Smarter->new({
    status => $status,
});
 
my $filler=GridFiller->new({
    status => $status,
    chooser => $chooser,
    result => $result,
});
 
$filler->fill();
 
$result->save_png($output_file);
 
say '';
say 'Leftovers:';
say for @{$result->leftover_words};
 
__END__
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2012 by Gianni Ceccarelli.
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
 
=cut