force FT2 usage, hopefully for better text consistency
[imager-graph.git] / t / t10pie.t
1 #!perl -w
2 use strict;
3 use Imager::Graph::Pie;
4 use Test::More;
5
6 -d 'testout' 
7   or mkdir "testout", 0700 
8   or die "Could not create output directory: $!";
9
10 ++$|;
11
12 my $testnum = 1;
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
20 my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
21 my @labels = qw(alpha beta gamma delta epsilon phi gi);
22
23 plan tests => 11;
24
25 my $pie = Imager::Graph::Pie->new;
26 ok($pie, "creating pie chart object");
27
28 # this may change output quality too
29
30 print "# Imager version: $Imager::VERSION\n";
31 print "# Font type: ",ref $font,"\n";
32
33 my $img1 = $pie->draw(data=>\@data, labels=>\@labels, font=>$font, 
34                       title=>{ text=>'Imager::Graph::Pie', size=>32 },
35                       features=>{ outline=>1, labels=>1, pieblur=>0, },
36                       outline=>{ line => '404040' },
37                      );
38
39 ok($img1, "drawing first pie chart")
40   or print "# ",$pie->error,"\n";
41 cmpimg($img1, "testimg/t10_pie1.png", 196880977);
42 $img1->write(file=>'testout/t10_pie1.ppm')
43   or die "Cannot save pie1: ",$img1->errstr,"\n";
44
45 my $img2 = $pie->draw(data=>\@data,
46                       labels=>\@labels,
47                       font=>$font, 
48                       title=>{ text=>'Imager::Graph::Pie', size=>36 },
49                       features=>{ labelspconly=>1, _debugblur=>1,
50                                   legend=>1 },
51                       legend=>{ border=>'000000', fill=>'FF8080', },
52                       fills=>[ qw(404040 606060 808080 A0A0A0 C0C0C0 E0E0E0) ],
53                      );
54
55 ok($img2, "drawing second pie chart")
56   or print "# ",$pie->error,"\n";
57 cmpimg($img2, "testimg/t10_pie2.png", 255956289);
58 $img2->write(file=>'testout/t10_pie2.ppm')
59   or die "Cannot save pie2: ",$img2->errstr,"\n";
60
61 my ($im_version) = $Imager::VERSION =~ /(\d\.[\d_]+)/;
62 {
63   $im_version > 0.38
64     or skip("very old Imager", 6);
65   my $img3 = $pie->draw(data=>\@data, labels=>\@labels,
66                         font=>$font, style=>'fount_lin', 
67                         features=>[ 'legend', 'labelspconly', ],
68                         legend=>{ valign=>'center' });
69   ok($img3, "third chart")
70     or print "# ",$pie->error,"\n";
71   $img3->write(file=>'testout/t10_lin_fount.ppm')
72     or die "Cannot save pie3: ",$img3->errstr,"\n";
73   cmpimg($img3, "testimg/t10_lin_fount.png", 180_000);
74
75   my $img4 = $pie->draw(data=>\@data, labels=>\@labels,
76                         font=>$font, style=>'fount_rad', 
77                         features=>[ 'legend', 'labelspc', ],
78                         legend=>{ valign=>'bottom', 
79                                   halign=>'left',
80                                   border=>'000080' });
81   ok($img4, "fourth chart")
82     or print "# ",$pie->error,"\n";
83   $img4->write(file=>'testout/t10_rad_fount.ppm')
84     or die "Cannot save pie3: ",$img4->errstr,"\n";
85   cmpimg($img4, "testimg/t10_rad_fount.png", 120_000);
86
87   my $img5 = $pie->draw(data=>\@data, labels=>\@labels,
88                         font=>$font, style=>'mono', 
89                         features=>[ 'allcallouts', 'labelspc' ],
90                         legend=>{ valign=>'bottom', 
91                                   halign=>'right' });
92   ok($img5, "fifth chart")
93     or print "# ",$pie->error,"\n";
94   $img5->write(file=>'testout/t10_mono.ppm')
95     or die "Cannot save pie3: ",$img5->errstr,"\n";
96   cmpimg($img5, "testimg/t10_mono.png", 550_000);
97 }
98
99 sub cmpimg {
100   my ($img, $file, $limit) = @_;
101
102   $limit ||= 10000;
103
104  SKIP:
105   {
106     $Imager::formats{png}
107       or skip("No PNG support", 1);
108
109     my $cmpimg = Imager->new;
110     $cmpimg->read(file=>$file)
111       or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
112     my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
113     cmp_ok($diff, '<', $limit, "Comparison to $file ($diff)");
114   }
115 }