font handling fixes
[imager-graph.git] / t / t10pie.t
1 #!perl -w
2 use strict;
3 use Imager::Graph::Pie;
4 use lib 't/lib';
5 use Imager::Font::Test;
6 use Test::More;
7
8 -d 'testout' 
9   or mkdir "testout", 0700 
10   or die "Could not create output directory: $!";
11
12 ++$|;
13
14 use Imager qw(:handy);
15
16 #my $fontfile = 'ImUgly.ttf';
17 #my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
18 #  or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
19 my $font = Imager::Font::Test->new();
20
21 my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
22 my @labels = qw(alpha beta gamma delta epsilon phi gi);
23
24 plan tests => 34;
25
26 my $pie = Imager::Graph::Pie->new;
27 ok($pie, "creating pie chart object");
28
29 # this may change output quality too
30
31 print "# Imager version: $Imager::VERSION\n";
32 print "# Font type: ",ref $font,"\n";
33
34 my $img1 = $pie->draw(data=>\@data, labels=>\@labels, font=>$font, 
35                       title=>{ text=>'Imager::Graph::Pie', size=>32 },
36                       features=>{ outline=>1, labels=>1, pieblur=>0, },
37                       outline=>{ line => '404040' },
38                      );
39
40 ok($img1, "drawing first pie chart")
41   or print "# ",$pie->error,"\n";
42 cmpimg($img1, "testimg/t10_pie1.png", 196880977);
43 $img1->write(file=>'testout/t10_pie1.ppm')
44   or die "Cannot save pie1: ",$img1->errstr,"\n";
45
46 my $img2 = $pie->draw(data=>\@data,
47                       labels=>\@labels,
48                       font=>$font, 
49                       title=>{ text=>'Imager::Graph::Pie', size=>36 },
50                       features=>{ labelspconly=>1, _debugblur=>1,
51                                   legend=>1 },
52                       legend=>{ border=>'000000', fill=>'C0C0C0', },
53                       fills=>[ qw(404040 606060 808080 A0A0A0 C0C0C0 E0E0E0) ],
54                      );
55
56 ok($img2, "drawing second pie chart")
57   or print "# ",$pie->error,"\n";
58 cmpimg($img2, "testimg/t10_pie2.png", 255956289);
59 $img2->write(file=>'testout/t10_pie2.ppm')
60   or die "Cannot save pie2: ",$img2->errstr,"\n";
61
62 my $img3 = $pie->draw(data=>\@data, labels=>\@labels,
63                       font=>$font, style=>'fount_lin', 
64                       features=>[ 'legend', 'labelspconly', ],
65                       legend=>{ valign=>'center' });
66 ok($img3, "third chart")
67   or print "# ",$pie->error,"\n";
68 $img3->write(file=>'testout/t10_lin_fount.ppm')
69   or die "Cannot save pie3: ",$img3->errstr,"\n";
70 cmpimg($img3, "testimg/t10_lin_fount.png", 180_000);
71
72 my $img4 = $pie->draw(data=>\@data, labels=>\@labels,
73                       font=>$font, style=>'fount_rad', 
74                       features=>[ 'legend', 'labelspc', ],
75                       legend=>{ valign=>'bottom', 
76                                 halign=>'left',
77                                 border=>'000080' });
78 ok($img4, "fourth chart")
79   or print "# ",$pie->error,"\n";
80 $img4->write(file=>'testout/t10_rad_fount.ppm')
81   or die "Cannot save pie3: ",$img4->errstr,"\n";
82 cmpimg($img4, "testimg/t10_rad_fount.png", 120_000);
83
84 my $img5 = $pie->draw(data=>\@data, labels=>\@labels,
85                       font=>$font, style=>'mono', 
86                       features=>[ 'allcallouts', 'labelspc' ],
87                       legend=>{ valign=>'bottom', 
88                                 halign=>'right' });
89 ok($img5, "fifth chart")
90   or print "# ",$pie->error,"\n";
91 $img5->write(file=>'testout/t10_mono.ppm')
92   or die "Cannot save pie3: ",$img5->errstr,"\n";
93 cmpimg($img5, "testimg/t10_mono.png", 550_000);
94
95 my $img6 = $pie->draw(data=>\@data, labels=>\@labels,
96                       font=>$font, style=>'fount_lin', 
97                       features=>[ 'allcallouts', 'labelspc', 'legend' ],
98                       legend=>
99                       {
100                        valign=>'top', 
101                        halign=>'center',
102                        orientation => 'horizontal',
103                        fill => { solid => Imager::Color->new(0, 0, 0, 32) },
104                        patchborder => undef,
105                        #size => 30,
106                       });
107 ok($img6, "sixth chart")
108   or print "# ",$pie->error,"\n";
109 $img6->write(file=>'testout/t10_hlegend.ppm')
110   or die "Cannot save pie6: ",$img5->errstr,"\n";
111 cmpimg($img6, "testimg/t10_hlegend.png", 550_000);
112
113 {
114   # RT #34813
115   # zero sized segments were drawn to cover the whole circle
116   my @data = ( 10, 8, 5, 0.000 );
117   my @labels = qw(alpha beta gamma);
118   
119   my $img = $pie->draw
120     (
121      data => \@data, 
122      labels => \@labels, 
123      font => $font,
124      features => [ 'legend', 'labelspc', 'outline' ],
125     );
126   ok($img, "create graph with no 'others'");
127   ok($img->write(file => 'testout/t10_noother.ppm'),
128      "save it");
129   cmpimg($img, 'testimg/t10_noother.png', 500_000);
130 }
131
132 { # RT #535
133   # no font parameter would crash
134   my $im = $pie->draw
135     (
136      data => \@data,
137      title => 'test',
138     );
139   ok(!$im, "should fail to produce titled graph with no font");
140   like($pie->error, qr/title\.font/, "message should mention which font");
141
142   $im = $pie->draw
143     (
144      labels => \@labels,
145      data => \@data,
146      features => [ 'legend' ],
147     );
148   ok(!$im, "should fail to produce legended graph with no font");
149   like($pie->error, qr/legend\.font/, "message should mention which font");
150
151   $im = $pie->draw
152     ( 
153      data => \@data,
154      labels => \@labels,
155      features => [ 'legend' ],
156      legend => { orientation => "horizontal" },
157     );
158   ok(!$im, "should fail to produce horizontal legended graph with no font");
159   like($pie->error, qr/legend\.font/, "message should mention which font");
160
161   $im = $pie->draw
162     (
163      data => \@data,
164      labels => \@labels,
165     );
166   ok(!$im, "should fail to produce labelled graph with no font");
167   like($pie->error, qr/label\.font/, "message should mention which font");
168
169   $im = $pie->draw
170     (
171      data => \@data,
172      labels => \@labels,
173      features => [ 'allcallouts' ],
174      label => { font => $font },
175     );
176   ok(!$im, "should fail to produce callout labelled graph with no font");
177   like($pie->error, qr/callout\.font/, "message should mention which font");
178
179   # shouldn't need to set label font if doing all callouts
180   $im = $pie->draw
181     (
182      data => \@data,
183      labels => \@labels,
184      features => [ 'allcallouts' ],
185      callout => { font => $font },
186     );
187   ok($im, "should produce callout labelled graph with only callout font")
188     or print "# ", $pie->error, "\n";
189
190   # shouldn't need to set callout font if doing all labels
191   $im = $pie->draw
192     (
193      data => [ 1, 1, 1 ],
194      labels => [ qw/a b c/ ],
195      label => { font => $font }
196     );
197   ok($im, "should produce label only graph with only label font");
198 }
199
200 {
201   # draw with an empty data array is bad
202   # problem reported and fixed by Patrick Michaud
203   my $im = $pie->draw(data => []);
204   ok(!$im, "fail to draw with empty data");
205   like($pie->error, qr/No values/, "message appropriate");
206 }
207
208 { # pie charts can't represent negative values
209   # problem reported and fixed by Patrick Michaud
210   my $im = $pie->draw(data => [ 10, -1, 10 ]);
211   ok(!$im, "fail to draw with negative value");
212   is($pie->error, "Data index 1 is less than zero", "check message");
213 }
214
215 { # pie can't represent all zeros
216   # problem reported and fixed by Patrick Michaud
217   my $im = $pie->draw(data => [ 0, 0, 0 ]);
218   ok(!$im, "fail to draw with all zero values");
219   is($pie->error, "Sum of all data values is zero", "check message");
220 }
221
222 sub cmpimg {
223   my ($img, $file, $limit) = @_;
224
225   $limit ||= 10000;
226
227  SKIP:
228   {
229     $Imager::formats{png}
230       or skip("No PNG support", 1);
231
232     my $cmpimg = Imager->new;
233     $cmpimg->read(file=>$file)
234       or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
235     my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
236     cmp_ok($diff, '<', $limit, "Comparison to $file ($diff)");
237   }
238 }