From 5d622bb851982d1a50ae9c81242b6ee406b4aa33 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Mon, 16 Mar 2009 08:22:51 +0000 Subject: [PATCH] return missing font errors in from the error method, add tests for the various fonts we look for. --- Graph.pm | 22 +++++++++-------- lib/Imager/Graph/Pie.pm | 18 ++++++++++---- t/t10pie.t | 55 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 16 deletions(-) diff --git a/Graph.pm b/Graph.pm index 32459b6..bde67f9 100644 --- a/Graph.pm +++ b/Graph.pm @@ -1220,10 +1220,10 @@ sub _text_style { %work = %{$self->{_style}{text}}; } $work{font} - or return $self->_error("$name has no font parameter"); + or return $self->_error("$name has no font parameter"); $work{font} = $self->_get_thing("$name.font") - or return $self->_error("invalid font"); + or return $self->_error("No $name.font defined, either set $name.font or font to a font"); UNIVERSAL::isa($work{font}, "Imager::Font") or return $self->_error("$name.font is not a font"); if ($work{color} && !ref $work{color}) { @@ -1240,11 +1240,9 @@ sub _text_style { sub _text_bbox { my ($self, $text, $name) = @_; - my %text_info = $self->_text_style($name); + my %text_info = $self->_text_style($name) + or return; - if (!defined $text_info{font}) { - die "No font or invalid font specified, and we're trying to draw text.\n"; - } my @bbox = $text_info{font}->bounding_box(%text_info, string=>$text, canon=>1); @@ -1366,7 +1364,8 @@ sub _draw_legend_horizontal { my @sizes; my @offsets; for my $label (@$labels) { - my @text_box = $self->_text_bbox($label, 'legend'); + my @text_box = $self->_text_bbox($label, 'legend') + or return; push(@sizes, \@text_box); my $entry_width = $patchsize + $gap + $text_box[2]; if ($pos == 0) { @@ -1462,7 +1461,8 @@ sub _draw_legend_vertical { my ($width, $height) = (0,0); my @sizes; for my $label (@$labels) { - my @box = $self->_text_bbox($label, 'legend'); + my @box = $self->_text_bbox($label, 'legend') + or return; push(@sizes, \@box); $width = $box[2] if $box[2] > $width; if ($minrowsize > $box[3]) { @@ -1544,11 +1544,13 @@ sub _draw_title { my ($self, $img, $chart_box) = @_; my $title = $self->{_style}{title}{text}; - my @box = $self->_text_bbox($title, 'title'); + my @box = $self->_text_bbox($title, 'title') + or return; my $yoff = $box[1]; @box[0,1] = (0,0); $self->_align_box(\@box, $chart_box, 'title'); - my %text_info = $self->_text_style('title'); + my %text_info = $self->_text_style('title') + or return; $img->string(%text_info, x=>$box[0], 'y'=>$box[3] + $yoff, text=>$title); $self->_remove_box($chart_box, \@box); 1; diff --git a/lib/Imager/Graph/Pie.pm b/lib/Imager/Graph/Pie.pm index 4bdb50a..dcba2ae 100644 --- a/lib/Imager/Graph/Pie.pm +++ b/lib/Imager/Graph/Pie.pm @@ -272,7 +272,9 @@ sub draw { elsif ($style->{features}{labels}) { $item->{label} = 1; } - $item->{lbox} = [ $self->_text_bbox($item->{text}, 'label') ]; + my @lbox = $self->_text_bbox($item->{text}, 'label') + or return; + $item->{lbox} = \@lbox; if ($item->{label}) { unless ($self->_fit_text(0, 0, 'label', $item->{text}, $guessradius, $item->{begin}, $item->{end})) { @@ -282,7 +284,9 @@ sub draw { $item->{callout} = 1 if $style->{features}{allcallouts}; if ($item->{callout}) { $item->{label} = 0; - $item->{cbox} = [ $self->_text_bbox($item->{text}, 'callout') ]; + my @cbox = $self->_text_bbox($item->{text}, 'callout') + or return; + $item->{cbox} = \@cbox; $item->{cangle} = ($item->{begin} + $item->{end}) / 2; my $dist = cos($item->{cangle}) * ($guessradius+ $callout_outside); @@ -361,8 +365,10 @@ sub draw { my $callout_inside = $radius - $self->_get_number('callout.inside'); $callout_outside += $radius; - my %callout_text = $self->_text_style('callout'); - my %label_text = $self->_text_style('label'); + my %callout_text = $self->_text_style('callout') + or return; + my %label_text = $self->_text_style('label') + or return; for my $label (@info) { if ($label->{label}) { my @loc = $self->_fit_text($cx, $cy, 'label', $label->{text}, $radius, @@ -378,7 +384,9 @@ sub draw { } else { $label->{callout} = 1; - $label->{cbox} = [ $self->_text_bbox($label->{text}, 'callout') ]; + my @cbox = $self->_text_bbox($label->{text}, 'callout') + or return; + $label->{cbox} = \@cbox; $label->{cangle} = ($label->{begin} + $label->{end}) / 2; } } diff --git a/t/t10pie.t b/t/t10pie.t index e8493c9..7e3d61c 100644 --- a/t/t10pie.t +++ b/t/t10pie.t @@ -21,7 +21,7 @@ my $font = Imager::Font::Test->new(); my @data = ( 100, 180, 80, 20, 2, 1, 0.5 ); my @labels = qw(alpha beta gamma delta epsilon phi gi); -plan tests => 16; +plan tests => 26; my $pie = Imager::Graph::Pie->new; ok($pie, "creating pie chart object"); @@ -129,6 +129,59 @@ cmpimg($img6, "testimg/t10_hlegend.png", 550_000); cmpimg($img, 'testimg/t10_noother.png', 500_000); } +{ # RT #535 + # no font parameter would crash + my $im = $pie->draw + ( + data => \@data, + title => 'test', + ); + ok(!$im, "should fail to produce titled graph with no font"); + like($pie->error, qr/title\.font/, "message should mention which font"); + + $im = $pie->draw + ( + labels => \@labels, + data => \@data, + features => [ 'legend' ], + ); + ok(!$im, "should fail to produce legended graph with no font"); + like($pie->error, qr/legend\.font/, "message should mention which font"); + + $im = $pie->draw + ( + data => \@data, + labels => \@labels, + features => [ 'legend' ], + legend => { orientation => "horizontal" }, + ); + ok(!$im, "should fail to produce horizontal legended graph with no font"); + like($pie->error, qr/legend\.font/, "message should mention which font"); + + $im = $pie->draw + ( + data => \@data, + labels => \@labels, + ); + ok(!$im, "should fail to produce labelled graph with no font"); + like($pie->error, qr/label\.font/, "message should mention which font"); + + SKIP: + { + $font + or skip("No font to setup the callout font", 2); + $im = $pie->draw + ( + data => \@data, + labels => \@labels, + features => [ 'allcallouts' ], + label => { font => $font }, + ); + ok(!$im, "should fail to produce callout labelled graph with no font"); + like($pie->error, qr/callout\.font/, "message should mention which font"); + } +} + sub cmpimg { my ($img, $file, $limit) = @_; -- 2.30.2