From ee37f58aafc6f75a1c5eb1e72c507dfcdf53c209 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 5 Dec 2010 21:15:42 +0000 Subject: slightly centered layout --- lib/GridFiller/Result/Pango.pm | 50 ++++++++++++++++++++++++++++++++++++++---- script/qr-color.pl | 8 +------ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/lib/GridFiller/Result/Pango.pm b/lib/GridFiller/Result/Pango.pm index 81eba04..cfb2947 100644 --- a/lib/GridFiller/Result/Pango.pm +++ b/lib/GridFiller/Result/Pango.pm @@ -6,6 +6,7 @@ use MooseX::Types::Moose qw(Int); use Cairo; use Pango; use Carp; +use POSIX 'ceil'; extends 'GridFiller::Result'; @@ -108,15 +109,52 @@ sub _put_squares { return; } -sub layout_for_word { - my ($self,$word) = @_; +sub layout_for_string { + my ($self,$string) = @_; my $p = Pango::Cairo::create_layout($self->_cairo_c); - $p->set_text($word); + $p->set_text($string); return $p; } +sub length_closure { + my ($self) = @_; + + return sub { $self->string_width($_[0]) } +} + +sub string_extents { + my ($self,$string) = @_; + + my $p = $self->layout_for_string($string); + my ($w,$h) = $p->get_pixel_size; + return + ceil($w / $self->cell_size), + ceil($h / $self->cell_size); +} + +sub string_width { + my ($self,$string) = @_; + + return +($self->string_extents($string))[0]; +} + +sub string_height { + my ($self,$string) = @_; + + return +($self->string_extents($string))[1]; +} + +sub _center_adj { + my ($self,$p) = @_; + + my ($w) = $p->get_pixel_size; + my $size = $self->cell_size; + my $rounded = ceil($w/$size)*$size; + return +($rounded-$w)/2; +} + my $PI=3.1415926; sub place_word_at { @@ -124,16 +162,20 @@ sub place_word_at { $self->log->debug("Placing $word at ${x}:${y} ($dir)"); - my $p = $self->layout_for_word($word); + my $p = $self->layout_for_string($word); my $size = $self->cell_size; my $cr = $self->_cairo_c; $x*=$size;$y*=$size; + my $adjustment = $self->_center_adj($p); + $cr->save; if ($dir == $HORIZONTAL) { + $x+=$adjustment; $cr->move_to($x,$y); } else { + $y+=$adjustment; $cr->move_to($x+$size,$y); $cr->rotate($PI/2); } diff --git a/script/qr-color.pl b/script/qr-color.pl index 807ec25..2a4d7f5 100644 --- a/script/qr-color.pl +++ b/script/qr-color.pl @@ -38,13 +38,7 @@ my $result = GridFiller::Result::Pango->new({ $filler->result($result); $filler->status->mode('longest'); -$filler->status->length( - sub { - my $p = $result->layout_for_word(shift); - my ($w,$h) = $p->get_pixel_size; - return int(0.5 + $w / $result->cell_size); - } -); +$filler->status->length($result->length_closure); $filler->fill(); -- cgit v1.2.3