File Coverage

lib/Bio/Graphics/GDWrapper.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Bio::Graphics::GDWrapper;
2              
3 1     1   4 use base 'GD::Image';
  1         2  
  1         629  
4             use Memoize 'memoize';
5             use Carp 'cluck';
6             memoize('_match_font');
7              
8             my $DefaultFont;
9              
10             #from http://reeddesign.co.uk/test/points-pixels.html
11             my %Pixel2Point = (
12             8 => 6,
13             9 => 7,
14             10 => 7.5,
15             11 => 8,
16             12 => 9,
17             13 => 10,
18             14 => 10.5,
19             15 =>11,
20             16 => 12,
21             17 => 13,
22             18 => 13.5,
23             19 => 14,
24             20 => 14.5,
25             21 => 15,
26             22 => 16,
27             23 => 17,
28             24 => 18,
29             25 => 19,
30             26 => 20
31             );
32             my $GdInit;
33              
34             sub new {
35             my $self = shift;
36             my ($gd,$default_font) = @_;
37             $DefaultFont = $default_font unless $default_font eq '1';
38             $gd->useFontConfig(1);
39             return bless $gd,ref $self || $self;
40             }
41              
42             sub default_font { return $DefaultFont || 'Arial' }
43              
44             # print with a truetype string
45             sub string {
46             my $self = shift;
47             my ($font,$x,$y,$string,$color) = @_;
48             return $self->SUPER::string(@_) if $self->isa('GD::SVG');
49             my $fontface = $self->_match_font($font);
50             # warn "$font => $fontface";
51             my ($fontsize) = $fontface =~ /-(\d+)/;
52             $self->stringFT($color,$fontface,$fontsize,0,$x,$y+$fontsize+1,$string);
53             }
54              
55             sub string_width {
56             my $self = shift;
57             my ($font,$string) = @_;
58             my $fontface = $self->_match_font($font);
59             my ($fontsize) = $fontface =~ /-([\d.]+)/;
60             my @bounds = GD::Image->stringFT(0,$fontface,$fontsize,0,0,0,$string);
61             return abs($bounds[2]-$bounds[0]);
62             }
63              
64             sub string_height {
65             my $self = shift;
66             my ($font,$string) = @_;
67             my $fontface = $self->_match_font($font);
68             my ($fontsize) = $fontface =~ /-(\d+)/;
69             my @bounds = GD::Image->stringFT(0,$fontface,$fontsize,0,0,0,$string);
70             return abs($bounds[5]-$bounds[3]);
71             }
72              
73             # find a truetype match for a built-in font
74             sub _match_font {
75             my $self = shift;
76             my $font = shift;
77             return $font unless ref $font && $font->isa('GD::Font');
78              
79             # work around older versions of GD that require useFontConfig to be called from a GD::Image instance
80             $GdInit++ || eval{GD::Image->useFontConfig(1)} || GD::Image->new(10,10)->useFontConfig(1);
81              
82             my $fh = $font->height-1;
83             my $height = $Pixel2Point{$fh} || $fh;
84             my $style = $font eq GD->gdMediumBoldFont ? 'bold'
85             :$font eq GD->gdGiantFont ? 'bold'
86             :'normal';
87             my $ttfont = $self->default_font;
88             return "$ttfont-$height:$style";
89             }
90              
91             1;