From f068590866ef2add8d31d7a3dbcf290a33b333b4 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 8 Oct 2014 21:28:38 +0200 Subject: [PATCH] Initial version, with gen-ballot.pl --- README.md | 3 + gen-ballot.pl | 147 +++++++++++++++++++++++++++++++++++++++++++++++ names-sample.txt | 5 ++ 3 files changed, 155 insertions(+) create mode 100644 README.md create mode 100755 gen-ballot.pl create mode 100644 names-sample.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fa2b3e --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Example: + +./gen-ballot.pl 15 ) { + chomp; + push @names, $_; +} + +$ballot[1] += @names * ($linespacing[1] + $fontsize[1] + 5); + +our $surface = Cairo::PdfSurface->create ($filename, @paper); + +# Effective surface area +our @surfsize = pairwise { $a - $b * 2 } @paper, @margin; +# Grid layout on effective surface +our @grid = pairwise { int($a / $b) } @surfsize, @ballot; +# Grid surface area + +our @gridsurfsize = pairwise { $a * $b } @grid, @ballot; +# Start of grid surface so that it is centered on the paper +our @gridsurfstart = pairwise { ($a - $b) / 2 } @paper, @gridsurfsize; + +sub gen_token { + my $token = ''; + my @chars = ('a'..'z', '0'..'9'); + for (1..5) { + $token .= $chars[rand @chars]; + } + return($token); +} + +# Produce a context for single ballot, starting at coordinates [0,0] +sub ballot_cr { + my ($surface, $cell) = @_; + my @startM = pairwise { $a * $b } @ballot, @$cell; + my @start = pairwise { $a + $b } @gridsurfstart, @startM; + + my $cr = Cairo::Context->create($surface); + $cr->translate(@start); + + $cr->set_source_rgb(0, 0, 0); + $cr->set_line_width(1); + $cr; +} + +# Centered text with top border at $$y. $size is index in font config above. +sub ballot_text_centered { + my ($cr, $y, $face, $slant, $weight, $size, $text) = @_; + $$y += $linespacing[$size] / 2; + + $cr->select_font_face($face, $slant, $weight); + $cr->set_font_size($fontsize[$size]); + my $textents = $cr->text_extents($text); + my $fextents = $cr->font_extents(); + $$y += $fextents->{height}; + $cr->move_to(($ballot[0] - $textents->{width}) / 2, $$y); + $cr->show_text($text); + + $$y += $linespacing[$size] / 2; +} + +sub ballot_text_plus_box { + my ($cr, $y, $face, $slant, $weight, $size, $text) = @_; + $$y += $linespacing[$size] / 2; + + $cr->select_font_face($face, $slant, $weight); + $cr->set_font_size($fontsize[$size]); + my $textents = $cr->text_extents($text); + my $fextents = $cr->font_extents(); + $$y += $fextents->{height}; + $cr->move_to(45, $$y); + $cr->show_text($text); + + $cr->rectangle(15, $$y - $fontsize[$size], 20, $fontsize[$size] * 3 / 2); + $cr->stroke; + + $$y += $linespacing[$size] / 2; +} + +sub ballot { + my ($cr, $names) = @_; + + $cr->rectangle(0, 0, @ballot); + $cr->stroke; + + + my $ypos = $topmargin + $linespacing[0] / 2; + # ballot_text($cr, \$ypos, $fontface, 'normal', 'bold', 0, $host); + # ballot_text($cr, \$ypos, $ffontface, 'normal', 'normal', 1, $mac); + for my $name (@$names) { + ballot_text_plus_box($cr, \$ypos, $fontface, 'normal', 'normal', 1, $name); + } + my $tok = gen_token(); + print($tok."\n"); + ballot_text_centered($cr, \$ypos, $fontface, 'italic', 'normal', 2, $tok); + ballot_text_centered($cr, \$ypos, $fontface, 'italic', 'normal', 2, $contact); +} + + +my ($x, $y) = (0, 0); + +for my $n (1..$n_ballots) { + ballot(ballot_cr($surface, [$x, $y]), \@names); + + $y++; + if ($y >= $grid[1]) { + $y = 0; $x++; + if ($x >= $grid[0]) { + my $cr = Cairo::Context->create($surface); + $cr->show_page; + ($x, $y) = (0, 0); + } + } +} diff --git a/names-sample.txt b/names-sample.txt new file mode 100644 index 0000000..5152803 --- /dev/null +++ b/names-sample.txt @@ -0,0 +1,5 @@ +Tinky Winky +Dipsy Simmit +Laa-Laa Smedley +Po Pui Fan Lee +Noo-noo