root/yapcasia2008/misc/nameplate-svg.pl

Revision 2664 (checked in by miyagawa, 12 years ago)

add YAPC nameplates project using SVG and dummy CSV file

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