blib/lib/Imager/Font.pm

filter: Statementstmt 35.6% 62 / 174 Branchbran 25.5% 23 / 90 Conditioncond 26.3% 10 / 38 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 42.8% 6 / 14 Podpod 100% 9 / 9 totaltotal 32.7% 114 / 348 CC 62 SCAR 47.0
CC
62
Cov
31%
align
342.0
bounding_box
110.0
new
36.4
CRAP 110.1
« blib/lib/Imager/Preprocess.pm ↑ Summary blib/lib/Imager/Expr/Assem.pm »
1 package Imager::Font;
2 79 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 79 ▶use Imager::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 79 our $VERSION = "1.040";
7  
8 # the aim here is that we can:
9 # - add file based types in one place: here
10 # - make sure we only attempt to create types that exist
11 # - give reasonable defaults
12 # - give the user some control over which types get used
13 79 my %drivers =
14 (
15 tt=>{
16 class=>'Imager::Font::Truetype',
17 module=>'Imager/Font/Truetype.pm',
18 files=>'.*\.ttf$',
19 description => 'FreeType 1.x',
20 checktype => 1,
21 },
22 t1=>{
23 class=>'Imager::Font::T1',
24 module=>'Imager/Font/T1.pm',
25 files=>'.*\.pfb$',
26 description => 'T1Lib',
27 },
28 ft2=>{
29 class=>'Imager::Font::FT2',
30 module=>'Imager/Font/FT2.pm',
31 files=>'.*\.(pfa|pfb|otf|ttf|fon|fnt|dfont|pcf(\.gz)?)$',
32 description => 'FreeType 2.x',
33 },
34 ifs=>{
35 class=>'Imager::Font::Image',
36 module=>'Imager/Font/Image.pm',
37 files=>'.*\.ifs$',
38 },
39 w32=>{
40 class=>'Imager::Font::W32',
41 module=>'Imager/Font/W32.pm',
42 description => 'Win32 GDI Fonts',
43 },
44 );
45  
46 # this currently should only contain file based types, don't add w32
47 79 my @priority = qw(ft2 ifs);
48  
49 2 ▶sub new {
Subroutine new: called CC=19 SCAR=35.9
Pod documented
50 2 my $class = shift;
51 2 my $self = {};
52 2 my ($file, $type, $id);
53 2 my %hsh=(color => Imager::Color->new(255,0,0,255),
54 size => 15,
55 @_);
56  
57 2 bless $self,$class;
58  
59 2 ▶ if ($hsh{'file'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hsh{'file'}) { } 2 0 2
elsif ($hsh{'face'}) { } 0 0 0
60 2 $file = $hsh{'file'};
61  
62 2 $type = $hsh{'type'};
63 2 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 2 2
64 0 ▶ unless ($drivers{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($drivers{$type}) 0 0 0
65 0 Imager->_set_error("Unknown font type $type");
66 0 return;
67 }
68  
69 0 ▶ unless ($Imager::formats{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Imager::formats{$type}) 0 0 0
70 0 Imager->_set_error("The $type {$drivers{$type}) font driver is not installed");
71 0 return;
72 }
73 }
74 else {
75 2 for my $drv (@priority) {
76 4 undef $type;
77 4 ▶ my $re = $drivers{$drv}{files} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $drivers{$drv}{"files"} or next 4 0 4
78 4 ▶ if ($file =~ /$re/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$re/i) 3 1 4
79 3 ▶ if (eval { require $drivers{$drv}{module}; 1 } and !( $drivers{$drv}{checktype} && !$Imager::formats{$drv} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require $drivers{$drv}{"module"}; 1 } } and not $drivers{$drv}{"checktype"} && !$Imager::formats{$drv}) 1 2 3
Condition50%3/6
eval { do { require $drivers{$drv}{"module"}; 1 } } and not $drivers{$drv}{"checktype"} && !$Imager::formats{$drv}
!ll&&!rl&&r
201
$drivers{$drv}{'checktype'} && !$Imager::formats{$drv}
!ll&&!rl&&r
100
MC/DC33%1/3
eval { do { require $drivers{$drv}{"module"}; 1 } } and not $drivers{$drv}{"checktype"} && !$Imager::formats{$drv}
eval { do { require $drivers{$drv}{"module"}; 1 } } $drivers{$drv}{'checktype'} !$Imager::formats{$drv}
Truth table50%2/4
A and not(B && not(C))
A= eval { do { require $drivers{$drv}{"module"}; 1 } }B= $drivers{$drv}{'checktype'}C= !$Imager::formats{$drv}
ABCresult
0XX0
1110
10X1
1101
80 1 $type = $drv;
81 1 last;
82 }
83 }
84 }
85 }
86 2 ▶ if (!defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 1 1 2
87 # some types we can support, but the driver isn't available
88 # work out which drivers support it, so we can provide the user
89 # some useful information on how to get it working
90 1 my @not_here;
91 1 for my $driver_name (keys %drivers) {
92 5 my $driver = $drivers{$driver_name};
93 push @not_here, "$driver_name ($driver->{description})"
94 5 ▶ if $driver->{files} && $file =~ /$driver->{files}/i;
Branch: 1 branch
BranchTrueFalseTotal
if $driver->{"files"} and $file =~ /$driver->{"files"}/i 2 3 5
Condition100%3/3
$driver->{"files"} and $file =~ /$driver->{"files"}/i
!ll&&!rl&&r
122
MC/DC100%2/2
$driver->{"files"} and $file =~ /$driver->{"files"}/i
$driver->{"files"} $file =~ /$driver->{"files"}/i
Truth table100%3/3
A and B
A= $driver->{"files"}B= $file =~ /$driver->{"files"}/i
ABresult
0X0
100
111
95 }
96 1 ▶ if (@not_here) {
Branch: 1 branch
BranchTrueFalseTotal
if (@not_here) { } 1 0 1
97 1 $Imager::ERRSTR = "No font drivers enabled that can support this file, rebuild Imager with any of ".join(", ", @not_here)." to use this font file";
98 }
99 else {
100 0 $Imager::ERRSTR = "No font type found for $hsh{'file'}";
101 }
102 1 return;
103 }
104 } elsif ($hsh{face}) {
105 0 $type = "w32";
106 } else {
107 0 $Imager::ERRSTR="No font file specified";
108 0 return;
109 }
110  
111 1 ▶ if ($drivers{$type}{checktype} && !$Imager::formats{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drivers{$type}{"checktype"} and not $Imager::formats{$type}) 0 1 1
Condition33%1/3
$drivers{$type}{"checktype"} and not $Imager::formats{$type}
!ll&&!rl&&r
100
MC/DC0%0/2
$drivers{$type}{"checktype"} and not $Imager::formats{$type}
$drivers{$type}{"checktype"} not $Imager::formats{$type}
Truth table33%1/3
A and not(B)
A= $drivers{$type}{"checktype"}B= not $Imager::formats{$type}
ABresult
0X0
100
111
112 0 $Imager::ERRSTR = "`$type' not enabled";
113 0 return;
114 }
115  
116 # here we should have the font type or be dead already.
117  
118 1 require $drivers{$type}{module};
119 1 return $drivers{$type}{class}->new(%hsh);
120 }
121  
122 # returns first defined parameter
123 0 ▶sub _first {
Subroutine _first: not called CC=3 SCAR=24.8
124 0 for (@_) {
125 0 ▶ return $_ if defined $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 0 0 0
126 }
127 0 return undef;
128 }
129  
130 0 ▶sub draw {
Subroutine draw: not called CC=5 SCAR=34.0
Pod documented
131 0 my $self = shift;
132 0 my %input = ('x' => 0, 'y' => 0, @_);
133 0 ▶ unless ($input{image}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input{"image"}) 0 0 0
134 0 $Imager::ERRSTR = 'No image supplied to $font->draw()';
135 0 return;
136 }
137 0 my $image = $input{image};
138 0 $input{string} = _first($input{string}, $input{text});
139 0 ▶ unless (defined $input{string}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input{"string"}) 0 0 0
140 0 $image->_set_error("Missing required parameter 'string'");
141 0 return;
142 }
143 0 $input{aa} = _first($input{aa}, $input{antialias}, $self->{aa}, 1);
144 # the original draw code worked this out but didn't use it
145 0 $input{align} = _first($input{align}, $self->{align});
146 0 $input{color} = _first($input{color}, $self->{color});
147 0 $input{color} = Imager::_color($input{'color'});
148  
149 0 $input{size} = _first($input{size}, $self->{size});
150 0 ▶ unless (defined $input{size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input{"size"}) 0 0 0
151 0 $image->_set_error("No font size provided");
152 0 return undef;
153 }
154 0 $input{align} = _first($input{align}, 1);
155 0 $input{utf8} = _first($input{utf8}, $self->{utf8}, 0);
156 0 $input{vlayout} = _first($input{vlayout}, $self->{vlayout}, 0);
157  
158 0 my $result = $self->_draw(%input);
159 0 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
160 0 $image->_set_error($image->_error_as_msg());
161 }
162  
163 0 return $result;
164 }
165  
166 0 ▶sub align {
Subroutine align: not called CC=18 SCAR=58.3
Pod documented
167 0 my $self = shift;
168 0 my %input = ( halign => 'left', valign => 'baseline',
169 'x' => 0, 'y' => 0, @_ );
170  
171 # image needs to be supplied, but can be supplied as undef
172 0 ▶ unless (exists $input{image}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $input{"image"}) 0 0 0
173 0 Imager->_set_error("Missing required parameter 'image'");
174 0 return;
175 }
176  
177 0 ▶ my $errors_to = $input{image} || 'Imager';
Condition0%0/2
$input{'image'} || 'Imager'
l!l
00
MC/DC0%0/1
$input{'image'} || 'Imager'
$input{'image'}
Truth table0%0/2
A
A= $input{'image'}
Aresult
11
00
178  
179 0 my $text = _first($input{string}, $input{text});
180 0 ▶ unless (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
181 0 $errors_to->_set_error("Missing required parameter 'string'");
182 0 return;
183 }
184  
185 0 my $size = _first($input{size}, $self->{size});
186 0 my $utf8 = _first($input{utf8}, 0);
187  
188 0 my $bbox = $self->bounding_box(string=>$text, size=>$size, utf8=>$utf8);
189 0 my $valign = $input{valign};
190 0 ▶ $valign = 'baseline'
Branch: 1 branch
BranchTrueFalseTotal
unless $valign and $valign =~ /^(?:top|center|bottom|baseline)$/ 0 0 0
Condition0%0/3
$valign and $valign =~ /^(?:top|center|bottom|baseline)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$valign and $valign =~ /^(?:top|center|bottom|baseline)$/
$valign $valign =~ /^(?:top|center|bottom|baseline)$/
Truth table0%0/3
A and B
A= $valignB= $valign =~ /^(?:top|center|bottom|baseline)$/
ABresult
0X0
100
111
191 unless $valign && $valign =~ /^(?:top|center|bottom|baseline)$/;
192  
193 0 my $halign = $input{halign};
194 0 ▶ $halign = 'start'
Branch: 1 branch
BranchTrueFalseTotal
unless $halign and $halign =~ /^(?:left|start|center|end|right)$/ 0 0 0
Condition0%0/3
$halign and $halign =~ /^(?:left|start|center|end|right)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$halign and $halign =~ /^(?:left|start|center|end|right)$/
$halign $halign =~ /^(?:left|start|center|end|right)$/
Truth table0%0/3
A and B
A= $halignB= $halign =~ /^(?:left|start|center|end|right)$/
ABresult
0X0
100
111
195 unless $halign && $halign =~ /^(?:left|start|center|end|right)$/;
196  
197 0 my $x = $input{'x'};
198 0 my $y = $input{'y'};
199  
200 0 ▶ if ($valign eq 'top') {
Branch: 3 branches
BranchTrueFalseTotal
if ($valign eq 'top') { } 0 0 0
elsif ($valign eq 'center') { } 0 0 0
elsif ($valign eq 'bottom') { } 0 0 0
201 0 $y += $bbox->ascent;
202 }
203 elsif ($valign eq 'center') {
204 0 $y += $bbox->ascent - $bbox->text_height / 2;
205 }
206 elsif ($valign eq 'bottom') {
207 0 $y += $bbox->descent;
208 }
209 # else baseline is the default
210  
211 0 ▶ if ($halign eq 'left') {
Branch: 5 branches
BranchTrueFalseTotal
if ($halign eq 'left') { } 0 0 0
elsif ($halign eq 'start') { } 0 0 0
elsif ($halign eq 'center') { } 0 0 0
elsif ($halign eq 'end') { } 0 0 0
elsif ($halign eq 'right') { } 0 0 0
212 0 $x -= $bbox->start_offset;
213 }
214 elsif ($halign eq 'start') {
215 # nothing to do
216 }
217 elsif ($halign eq 'center') {
218 0 $x -= $bbox->start_offset + $bbox->total_width / 2;
219 }
220 elsif ($halign eq 'end') {
221 0 $x -= $bbox->advance_width;
222 }
223 elsif ($halign eq 'right') {
224 0 $x -= $bbox->advance_width - $bbox->right_bearing;
225 }
226 0 $x = int($x);
227 0 $y = int($y);
228  
229 0 ▶ if ($input{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input{"image"}) 0 0 0
230 0 delete @input{qw/x y/};
231 0 ▶ $self->draw(%input, 'x' => $x, 'y' => $y, align=>1)
Branch: 1 branch
BranchTrueFalseTotal
$self->draw(%input, "x", $x, "y", $y, "align", 1) or return 0 0 0
232 or return;
233 }
234  
235 0 return ($x+$bbox->start_offset, $y-$bbox->ascent,
236 $x+$bbox->end_offset, $y-$bbox->descent+1);
237 }
238  
239 0 ▶sub bounding_box {
Subroutine bounding_box: not called CC=10 SCAR=47.0
Pod documented
240 0 my $self=shift;
241 0 my %input=@_;
242  
243 0 ▶ if (!exists $input{'string'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $input{"string"}) 0 0 0
244 0 $Imager::ERRSTR='string parameter missing';
245 0 return;
246 }
247 0 ▶ $input{size} ||= $self->{size};
Condition0%0/3
$input{"size"} ||= $self->{"size"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$input{"size"} ||= $self->{"size"}
$input{"size"} $self->{"size"}
Truth table0%0/3
A ||= B
A= $input{"size"}B= $self->{"size"}
ABresult
1X1
011
000
248 0 $input{sizew} = _first($input{sizew}, $self->{sizew}, 0);
249 0 $input{utf8} = _first($input{utf8}, $self->{utf8}, 0);
250  
251 0 ▶ my @box = $self->_bounding_box(%input)
Branch: 1 branch
BranchTrueFalseTotal
my(@box) = $self->_bounding_box(%input) or return 0 0 0
252 or return;
253  
254 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
255 0 ▶ if(@box && exists $input{'x'} and exists $input{'y'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@box and exists $input{'x'} and exists $input{'y'}) { } 0 0 0
elsif (@box and $input{'canon'}) { } 0 0 0
Condition0%0/6
@box and exists $input{"x"} and exists $input{"y"}
!ll&&!rl&&r
000
@box and exists $input{"x"}
!ll&&!rl&&r
000
MC/DC0%0/3
@box and exists $input{"x"} and exists $input{"y"}
@box exists $input{"x"} exists $input{"y"}
Truth table0%0/4
A and B and C
A= @boxB= exists $input{"x"}C= exists $input{"y"}
ABCresult
0XX0
10X0
1100
1111
256 0 my($gdescent, $gascent)=@box[1,3];
257 0 $box[1]=$input{'y'}-$gascent; # top = base - ascent (Y is down)
258 0 $box[3]=$input{'y'}-$gdescent; # bottom = base - descent (Y is down, descent is negative)
259 0 $box[0]+=$input{'x'};
260 0 $box[2]+=$input{'x'};
261 ▶ } elsif (@box && $input{'canon'}) {
Condition0%0/3
@box and $input{"canon"}
!ll&&!rl&&r
000
MC/DC0%0/2
@box and $input{"canon"}
@box $input{"canon"}
Truth table0%0/3
A and B
A= @boxB= $input{"canon"}
ABresult
0X0
100
111
262 0 $box[3]-=$box[1]; # make it canonical (ie (0,0) - (width, height))
263 0 $box[2]-=$box[0];
264 }
265 0 return @box;
266 }
267 else {
268 0 require Imager::Font::BBox;
269  
270 0 return Imager::Font::BBox->new(@box);
271 }
272 }
273  
274 0 ▶sub dpi {
Subroutine dpi: not called CC=2 SCAR=17.9
Pod documented
275 0 my $self = shift;
276  
277 # I'm assuming a default of 72 dpi
278 0 my @old = (72, 72);
279 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
280 0 $Imager::ERRSTR = "Setting dpi not implemented for this font type";
281 0 return;
282 }
283  
284 0 return @old;
285 }
286  
287 0 ▶sub transform {
Subroutine transform: not called CC=2 SCAR=17.9
Pod documented
288 0 my $self = shift;
289  
290 0 my %hsh = @_;
291  
292 # this is split into transform() and _transform() so we can
293 # implement other tags like: degrees=>12, which would build a
294 # 12 degree rotation matrix
295 # but I'll do that later
296 0 ▶ unless ($hsh{matrix}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hsh{"matrix"}) 0 0 0
297 0 $Imager::ERRSTR = "You need to supply a matrix";
298 0 return;
299 }
300  
301 0 return $self->_transform(%hsh);
302 }
303  
304 0 ▶sub _transform {
Subroutine _transform: not called CC=1 SCAR=6.9
305 0 $Imager::ERRSTR = "This type of font cannot be transformed";
306 0 return;
307 }
308  
309 0 ▶sub utf8 {
Subroutine utf8: not called CC=1 SCAR=6.9
Pod documented
310 0 return 0;
311 }
312  
313 1 ▶sub priorities {
Subroutine priorities: called CC=2 SCAR=7.0
Pod documented
314 1 my $self = shift;
315 1 my @old = @priority;
316  
317 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
318 1 @priority = @_;
319 }
320 1 return @old;
321 }
322  
323 5 ▶sub register {
Subroutine register: called CC=9 SCAR=22.9
Pod documented
324 5 my ($self, %opts) = @_;
325  
326 5 my $type = delete $opts{type};
327 5 my $class = delete $opts{class};
328 5 my $files = delete $opts{files};
329 5 ▶ my $description = delete $opts{description} || $class;
Condition66%2/3
delete $opts{'description'} || $class
l!l&&r!l&&!r
032
MC/DC50%1/2
delete $opts{'description'} || $class
delete $opts{'description'} $class
Truth table66%2/3
A || B
A= delete $opts{'description'}B= $class
ABresult
1X1
011
000
330  
331 5 ▶ defined $type
Branch: 1 branch
BranchTrueFalseTotal
defined $type or return "Imager"->_set_error("No type parameter supplied to Imager::Font->regster") 4 1 5
332 or return Imager->_set_error("No type parameter supplied to Imager::Font->regster");
333  
334 4 ▶ defined $class
Branch: 1 branch
BranchTrueFalseTotal
defined $class or return "Imager"->_set_error("No class parameter supplied to Imager::Font->register") 3 1 4
335 or return Imager->_set_error("No class parameter supplied to Imager::Font->register");
336  
337 3 ▶ if ($files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files) 3 0 3
338 3 ▶ eval { qr/$files/ }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { qr/$files/ } } or return "Imager"->_set_error("files isn't a valid regexp") 2 1 3
339 or return Imager->_set_error("files isn't a valid regexp");
340 }
341  
342 2 ▶ if ($drivers{$type} && $drivers{$type}{class} ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drivers{$type} and $drivers{$type}{"class"} ne $class) 0 2 2
Condition33%1/3
$drivers{$type} and $drivers{$type}{"class"} ne $class
!ll&&!rl&&r
200
MC/DC0%0/2
$drivers{$type} and $drivers{$type}{"class"} ne $class
$drivers{$type} $drivers{$type}{"class"} ne $class
Truth table33%1/3
A and B
A= $drivers{$type}B= $drivers{$type}{"class"} ne $class
ABresult
0X0
100
111
343 0 Imager->_set_error("Font type $type already registered as $drivers{$type}{class}");
344 0 return;
345 }
346  
347 2 (my $module = $class . ".pm") =~ s(::)(/)g;
348  
349 2 my $driver =
350 {
351 class => $class,
352 module => $module,
353 description => $description,
354 };
355 2 ▶ $files and $driver->{files} = $files;
Branch: 1 branch
BranchTrueFalseTotal
$files and $driver->{"files"} = $files 2 0 2
356  
357 2 $drivers{$type} = $driver;
358  
359 2 1;
360 }
361  
362 79 1;
363  
364 __END__
« blib/lib/Imager/Preprocess.pm ↑ Summary blib/lib/Imager/Expr/Assem.pm »