From 4bea68faa549c6725c4af9c94a3565d5bd78729a Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Sun, 26 Dec 2010 16:59:19 +0000 Subject: string length may depend on direction --- lib/GridFiller/Chooser/Smarter.pm | 10 +++++++--- lib/GridFiller/Result/Pango.pm | 23 ++++++++++++----------- lib/GridFiller/Status.pm | 8 ++++---- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/GridFiller/Chooser/Smarter.pm b/lib/GridFiller/Chooser/Smarter.pm index 0abd088..16ed0b0 100644 --- a/lib/GridFiller/Chooser/Smarter.pm +++ b/lib/GridFiller/Chooser/Smarter.pm @@ -14,13 +14,17 @@ sub maxfirst { sub find_place_for { my ($self,$word) = @_; - my $length = $self->length->($word); - - $self->log->debug("looking for $length of space"); my @candidates; + my $length = $self->length->($word,$HORIZONTAL); + $self->log->debug("looking for $length of horizontal space"); + push @candidates, $self->_find_places_horiz($length); + + $length = $self->length->($word,$VERTICAL); + $self->log->debug("looking for $length of vertical space"); + push @candidates, $self->_find_places_vert($length); return unless @candidates; diff --git a/lib/GridFiller/Result/Pango.pm b/lib/GridFiller/Result/Pango.pm index 1172848..34b3401 100644 --- a/lib/GridFiller/Result/Pango.pm +++ b/lib/GridFiller/Result/Pango.pm @@ -149,11 +149,14 @@ sub _put_squares { } sub layout_for_string { - my ($self,$string) = @_; + my ($self,$string,$dir) = @_; my $p = Pango::Cairo::create_layout($self->_cairo_c); Pango::Cairo::Context::set_font_options($p->get_context,$self->font_options); Pango::Cairo::update_context($self->_cairo_c,,$p->get_context); + if ($dir == $VERTICAL) { + $p->get_context->set_base_gravity('east'); + } $p->context_changed; $p->set_font_description($self->font_description); $p->set_text($string); @@ -164,13 +167,13 @@ sub layout_for_string { sub length_closure { my ($self) = @_; - return sub { $self->string_width($_[0]) } + return sub { $self->string_width(@_) } } sub string_extents { - my ($self,$string) = @_; + my ($self,$string,$dir) = @_; - my $p = $self->layout_for_string($string); + my $p = $self->layout_for_string($string,$dir); my ($w,$h) = $p->get_pixel_size; return ceil($w / $self->cell_size), @@ -178,15 +181,15 @@ sub string_extents { } sub string_width { - my ($self,$string) = @_; + my ($self,$string,$dir) = @_; - return +($self->string_extents($string))[0]; + return +($self->string_extents($string,$dir))[0]; } sub string_height { - my ($self,$string) = @_; + my ($self,$string,$dir) = @_; - return +($self->string_extents($string))[1]; + return +($self->string_extents($string,$dir))[1]; } sub _center_adj { @@ -220,7 +223,7 @@ sub place_word_at { $self->log->debug("Placing $word at ${x}:${y} ($dir)"); - my $p = $self->layout_for_string($word); + my $p = $self->layout_for_string($word,$dir); my @colour = $self->colour_for_string($word,$x,$y,$dir); my $size = $self->cell_size; my $cr = $self->_cairo_c; @@ -235,8 +238,6 @@ sub place_word_at { $cr->move_to($x,$y); } else { - $p->get_context->set_base_gravity('east'); - $p->context_changed; $y+=$w_adjustment; $x-=$h_adjustment; $cr->move_to($x+$size,$y); diff --git a/lib/GridFiller/Status.pm b/lib/GridFiller/Status.pm index bf5e9e6..bccb20f 100644 --- a/lib/GridFiller/Status.pm +++ b/lib/GridFiller/Status.pm @@ -62,8 +62,8 @@ sub _build_words_to_use { given ($mode) { when ('random') { return [ shuffle uniq @$words ] } when ('given') { return [ uniq @$words ] } - when ('longest') { return [ sort {$l->($b) <=> $l->($a)} uniq @$words ] } - when ('shortest') { return [ sort {$l->($a) <=> $l->($b)} uniq @$words ] } + when ('longest') { return [ sort {$l->($b,$HORIZONTAL) <=> $l->($a,$HORIZONTAL)} uniq @$words ] } + when ('shortest') { return [ sort {$l->($a,$HORIZONTAL) <=> $l->($b,$HORIZONTAL)} uniq @$words ] } default { croak "Unknown mode $mode" } } } @@ -87,12 +87,12 @@ sub place_word_at { $self->log->debug("Marking <$word> occupied at ${x}:${y} ($dir)"); if ($dir == $HORIZONTAL) { - for my $i (0..$self->length->($word)+$space-1) { + for my $i (0..$self->length->($word,$dir)+$space-1) { $self->_mark_occupied($x+$i,$y); } } elsif ($dir == $VERTICAL) { - for my $i (0..$self->length->($word)+$space-1) { + for my $i (0..$self->length->($word,$dir)+$space-1) { $self->_mark_occupied($x,$y+$i); } } -- cgit v1.2.3