diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/GridFiller/Result/Pango.pm | 50 |
1 files changed, 46 insertions, 4 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); } |