1 |
#!/usr/bin/perl |
---|
2 |
use strict; |
---|
3 |
use warnings; |
---|
4 |
use utf8; |
---|
5 |
use Encode; |
---|
6 |
use SVG; |
---|
7 |
use SVG::Parser; |
---|
8 |
use Text::CSV_XS; |
---|
9 |
use Locale::Country; |
---|
10 |
|
---|
11 |
Locale::Country::rename_country('tw' => 'Taiwan'); |
---|
12 |
Locale::Country::rename_country('kr' => 'Korea'); |
---|
13 |
|
---|
14 |
my $base_file = "namecard_2x5.svg"; |
---|
15 |
my $csv_file = shift @ARGV || "sample.csv"; |
---|
16 |
|
---|
17 |
open my $fh, $csv_file or die $!; |
---|
18 |
|
---|
19 |
my $csv = Text::CSV_XS->new({ binary => 1 }); |
---|
20 |
my $header = $csv->getline($fh); |
---|
21 |
$csv->column_names(@$header); |
---|
22 |
|
---|
23 |
my $out_dir = "$ENV{HOME}/Sites/yapc-nameplates"; |
---|
24 |
|
---|
25 |
#my $font = '"M+ 1c"'; |
---|
26 |
my $font = 'mplus-1c-medium'; |
---|
27 |
|
---|
28 |
my @users; |
---|
29 |
while (!$csv->eof) { |
---|
30 |
my $ref = $csv->getline_hr($fh); |
---|
31 |
next unless $ref->{user_id}; |
---|
32 |
push @users, $ref; |
---|
33 |
} |
---|
34 |
|
---|
35 |
while (my @chunk = splice(@users, 0, 10)) { |
---|
36 |
my $first_id = $chunk[0]->{user_id}; |
---|
37 |
my $ox = 0; |
---|
38 |
my $oy = 0; |
---|
39 |
|
---|
40 |
my $parser = SVG::Parser->new; |
---|
41 |
my $svg = $parser->parse_file($base_file); |
---|
42 |
|
---|
43 |
for my $ref (@chunk) { |
---|
44 |
my $name = get_name($ref); |
---|
45 |
my $size = length($name) > 20 ? 18 |
---|
46 |
: length($name) > 16 ? 21 |
---|
47 |
: 24; |
---|
48 |
$svg->text(x => $ox + 35, y => $oy + 55, style => { 'font-family' => $font, 'font-weight' => 'bold', 'color' => 'black', 'font-size' => $size }, 'font-family' => $font, 'font-size' => $size)->cdata($name); |
---|
49 |
$svg->text(x => $ox + 35, y => $oy + 75, style => { 'font-family' => $font, 'color' => 'black', 'font-size' => 11 }, 'font-family' => $font, 'font-size' => 11)->cdata(($ref->{pm_group} ? "$ref->{pm_group} / " : "") . code2country($ref->{country})); |
---|
50 |
if ($ref->{company}) { |
---|
51 |
$svg->text(x => $ox + 35, y => $oy + 90, style => { 'font-family' => $font, color => 'black', 'font-size' => 11 }, 'font-family' => $font, 'font-size' => 11)->cdata( decode_utf8($ref->{company}) ); |
---|
52 |
} |
---|
53 |
|
---|
54 |
my $role = ($ref->{is_orga} || $ref->{is_staff}) ? 'STAFF' : |
---|
55 |
$ref->{has_talk} ? 'SPEAKER' :''; |
---|
56 |
|
---|
57 |
if ($role) { |
---|
58 |
$svg->text(x => $ox + 35, y => $oy + 126, style => { 'font-family' => $font, 'font-weight' => 'bold', color => 'black', 'font-size' => 16 }, 'font-family' => $font, 'font-size' => 16)->cdata($role); |
---|
59 |
} |
---|
60 |
|
---|
61 |
$svg->text(x => $ox + 266, y => $oy + 16, style => { 'font-family' => $font, color => 'black', 'font-size' => 8 }, 'font-family' => $font, 'font-size' => 8)->cdata($ref->{user_id}); |
---|
62 |
|
---|
63 |
$ox = $ox == 0 ? 296 : 0; |
---|
64 |
$oy+= 168 if $ox == 0; |
---|
65 |
} |
---|
66 |
|
---|
67 |
my $outfile = "$out_dir/$first_id.svg"; |
---|
68 |
open my $out, ">:utf8", $outfile or die "$outfile: $!"; |
---|
69 |
print $out $svg->xmlify; |
---|
70 |
warn $outfile, "\n"; |
---|
71 |
} |
---|
72 |
|
---|
73 |
sub get_name { |
---|
74 |
my $ref = shift; |
---|
75 |
|
---|
76 |
if ($ref->{pseudonymous}) { |
---|
77 |
return decode_utf8( $ref->{nick_name} ); |
---|
78 |
} |
---|
79 |
|
---|
80 |
if (is_cjk($ref)) { |
---|
81 |
return decode_utf8( $ref->{last_name} . $ref->{first_name} ); |
---|
82 |
} else { |
---|
83 |
return decode_utf8( join " ", $ref->{first_name}, $ref->{last_name} ); |
---|
84 |
} |
---|
85 |
} |
---|
86 |
|
---|
87 |
sub is_cjk { |
---|
88 |
my $ref = shift; |
---|
89 |
my $name = decode_utf8($ref->{first_name} . $ref->{last_name}); |
---|
90 |
$name =~ /\p{Han}/; |
---|
91 |
} |
---|