From e423da62532558968e1a66bc04c193ffffd38d43 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 2 Dec 2010 21:57:14 +0000 Subject: proper length measurement also, fix vertical placement off-by-one --- lib/GridFiller/Chooser/Smarter.pm | 2 ++ lib/GridFiller/Result/Pango.pm | 35 ++++++++++++++++++++++++++++++++++- script/qr-color.pl | 26 +++++++++++++++++--------- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/GridFiller/Chooser/Smarter.pm b/lib/GridFiller/Chooser/Smarter.pm index fd21dae..43ad530 100644 --- a/lib/GridFiller/Chooser/Smarter.pm +++ b/lib/GridFiller/Chooser/Smarter.pm @@ -16,6 +16,8 @@ sub find_place_for { my $length = $self->length->($word); + $self->log->debug("looking for $length of space"); + my @candidates; push @candidates, $self->_find_places_horiz($length); diff --git a/lib/GridFiller/Result/Pango.pm b/lib/GridFiller/Result/Pango.pm index 504f2a7..81eba04 100644 --- a/lib/GridFiller/Result/Pango.pm +++ b/lib/GridFiller/Result/Pango.pm @@ -83,6 +83,8 @@ sub _put_squares { my $size = $self->cell_size; + $cr->save; + $cr->rectangle(0,0,$self->_width,$self->_height); $cr->set_source_rgb(1,1,1); $cr->fill; @@ -101,15 +103,46 @@ sub _put_squares { $y+=$size; } + $cr->restore; + return; } +sub layout_for_word { + my ($self,$word) = @_; + + my $p = Pango::Cairo::create_layout($self->_cairo_c); + $p->set_text($word); + + return $p; +} + +my $PI=3.1415926; + sub place_word_at { my ($self, $word, $x, $y, $dir) = @_; $self->log->debug("Placing $word at ${x}:${y} ($dir)"); - $self->_cairo_c; + my $p = $self->layout_for_word($word); + my $size = $self->cell_size; + my $cr = $self->_cairo_c; + $x*=$size;$y*=$size; + + $cr->save; + if ($dir == $HORIZONTAL) { + $cr->move_to($x,$y); + } + else { + $cr->move_to($x+$size,$y); + $cr->rotate($PI/2); + } + Pango::Cairo::update_layout($cr,$p); + $cr->set_source_rgb(1,1,0); + Pango::Cairo::show_layout($cr,$p); + $cr->restore; + + return; } sub as_png { diff --git a/script/qr-color.pl b/script/qr-color.pl index 2c7735f..807ec25 100644 --- a/script/qr-color.pl +++ b/script/qr-color.pl @@ -10,7 +10,10 @@ use open ':std',':locale'; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($INFO); -Log::Log4perl->get_logger('GridFiller::Result::Pango')->level($DEBUG); +#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); binmode DATA,':utf8'; @@ -29,16 +32,21 @@ my $filler=GridFiller->new({ grid=> $arr, }); -$filler->result( - GridFiller::Result::Pango->new({ - source_grid => $filler->grid, - }) -); +my $result = GridFiller::Result::Pango->new({ + source_grid => $filler->grid, +}); + +$filler->result($result); $filler->status->mode('longest'); -$filler->status->length(sub { int(length(shift)/2) }); -$filler->fill(); +$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); + } +); -my $result=$filler->result; +$filler->fill(); $result->save_png('/tmp/qr.png'); -- cgit v1.2.3