API2/Resource/UniFont.pm

filter: Statementstmt 79.8% 79 / 99 Branchbran 57.1% 24 / 42 Conditioncond 50.0% 6 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 5 totaltotal 69.7% 122 / 175 SCAR 42.0
CC
36
Cov
71%
new
78.6
text
9.2
width
8.2
CRAP 66.8
« API2/Lite.pm ↑ Summary API2/Resource/XObject/Image/GD.pm »
1 package PDF::API2::Resource::UniFont;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.044'; # VERSION
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new {
12 1 ▶ my $class = shift();
Subroutine new: called CC=21 SCAR=43.6
Pod undocumented
13 1 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
14 1 my $self = {
15 fonts => [],
16 block => {},
17 code => {},
18 pdf => shift(),
19 };
20 1 bless $self, $class;
21  
22 1 my @fonts;
23 1 ▶ push @fonts, shift() while ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and do { push @fonts, shift() } 2 1 3
24  
25 1 my %options = @_;
26 1 ▶ $self->{'encode'} = $options{'-encode'} if defined $options{'-encode'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"-encode"} 1 0 1
27  
28 1 my $font_number = 0;
29 1 foreach my $font (@fonts) {
30 2 ▶ if (ref($font) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $font eq 'ARRAY') { } 1 1 2
elsif (ref $font eq 'HASH') { } 0 1 1
31 1 push @{$self->{'fonts'}}, shift(@$font);
32  
33 1 ▶ while (defined $font->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $font->[0]) 1 1 2
34 1 my $blockspec = shift @$font;
35 1 ▶ if (ref($blockspec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $blockspec) { } 1 0 1
36 1 foreach my $block ($blockspec->[0] .. $blockspec->[-1]) {
37 1 $self->{'block'}->{$block} = $font_number;
38 }
39 }
40 else {
41 0 $self->{'block'}->{$blockspec} = $font_number;
42 }
43 }
44 }
45 elsif (ref($font) eq 'HASH') {
46 0 push @{$self->{'fonts'}}, $font->{'font'};
47  
48 0 ▶ if (defined($font->{'blocks'}) and ref($font->{'blocks'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $font->{"blocks"} and ref $font->{"blocks"} eq "ARRAY") 0 0 0
Condition0%0/3
defined $font->{"blocks"} and ref $font->{"blocks"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $font->{"blocks"} and ref $font->{"blocks"} eq "ARRAY"
defined $font->{"blocks"} ref $font->{"blocks"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $font->{"blocks"}B= ref $font->{"blocks"} eq "ARRAY"
ABresult
0X0
100
111
49 0 foreach my $blockspec (@{$font->{'blocks'}}) {
50 0 ▶ if (ref($blockspec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $blockspec) { } 0 0 0
51 0 foreach my $block ($blockspec->[0] .. $blockspec->[-1]) {
52 0 $self->{'block'}->{$block} = $font_number;
53 }
54 }
55 else {
56 0 $self->{'block'}->{$blockspec} = $font_number;
57 }
58 }
59 }
60  
61 0 ▶ if (defined($font->{'codes'}) and ref($font->{'codes'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $font->{"codes"} and ref $font->{"codes"} eq "ARRAY") 0 0 0
Condition0%0/3
defined $font->{"codes"} and ref $font->{"codes"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $font->{"codes"} and ref $font->{"codes"} eq "ARRAY"
defined $font->{"codes"} ref $font->{"codes"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $font->{"codes"}B= ref $font->{"codes"} eq "ARRAY"
ABresult
0X0
100
111
62 0 foreach my $codespec (@{$font->{'codes'}}) {
63 0 ▶ if (ref($codespec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codespec) { } 0 0 0
64 0 foreach my $code ($codespec->[0] .. $codespec->[-1]) {
65 0 $self->{'code'}->{$code} = $font_number;
66 }
67 }
68 else {
69 0 $self->{'code'}->{$codespec} = $font_number;
70 }
71 }
72 }
73 }
74 else {
75 1 push @{$self->{'fonts'}}, $font;
76 1 foreach my $block (0 .. 255) {
77 256 $self->{'block'}->{$block} = $font_number;
78 }
79 }
80 2 $font_number++;
81 }
82  
83 1 return $self;
84 }
85  
86 2 ▶sub isvirtual { return 1; }
Subroutine isvirtual: called CC=1 SCAR=0.0
Pod undocumented
87  
88 sub fontlist {
89 11 ▶ my $self = shift();
Subroutine fontlist: called CC=1 SCAR=0.0
Pod undocumented
90 11 return [@{$self->{'fonts'}}];
91 }
92  
93 sub width {
94 4 ▶ my ($self, $text) = @_;
Subroutine width: called CC=8 SCAR=21.0
Pod undocumented
95 4 ▶ $text = decode($self->{'encode'}, $text) unless utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($text) 2 2 4
96 4 my $width = 0;
97  
98 4 my @blocks = ();
99 4 foreach my $u (unpack('U*', $text)) {
100 18 my $font_number = 0;
101 18 ▶ if (defined $self->{'code'}->{$u}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'code'}{$u}) { } 0 18 18
elsif (defined $self->{'block'}{$u >> 8}) { } 18 0 18
102 0 $font_number = $self->{'code'}->{$u};
103 }
104 elsif (defined $self->{'block'}->{$u >> 8}) {
105 18 $font_number = $self->{'block'}->{$u >> 8};
106 }
107 else {
108 0 $font_number = 0;
109 }
110  
111 18 ▶ if (scalar @blocks == 0 or $blocks[-1]->[0] != $font_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @blocks == 0 or $blocks[-1][0] != $font_number) { } 6 12 18
Condition100%3/3
scalar @blocks == 0 or $blocks[-1][0] != $font_number
l!l&&r!l&&!r
4212
MC/DC100%2/2
scalar @blocks == 0 or $blocks[-1][0] != $font_number
scalar @blocks == 0 $blocks[-1][0] != $font_number
Truth table100%3/3
A or B
A= scalar @blocks == 0B= $blocks[-1][0] != $font_number
ABresult
1X1
011
000
112 6 push @blocks, [$font_number, pack('U', $u)];
113 }
114 else {
115 12 $blocks[-1]->[1] .= pack('U', $u);
116 }
117 }
118  
119 4 foreach my $block (@blocks) {
120 6 my ($font_number, $string) = @$block;
121 6 $width += $self->fontlist->[$font_number]->width($string);
122 }
123  
124 4 return $width;
125 }
126  
127 sub text {
128 2 ▶ my ($self, $text, $size, $indent) = @_;
Subroutine text: called CC=9 SCAR=22.2
Pod undocumented
129 2 ▶ $text = decode($self->{'encode'}, $text) unless utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($text) 1 1 2
130 2 ▶ croak 'Font size not specified' unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 2 2
131  
132 2 my $value = '';
133 2 my $last_font_number;
134 my @codes;
135  
136 2 foreach my $u (unpack('U*', $text)) {
137 9 my $font_number = 0;
138 9 ▶ if (defined $self->{'code'}->{$u}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'code'}{$u}) { } 0 9 9
elsif (defined $self->{'block'}{$u >> 8}) { } 9 0 9
139 0 $font_number = $self->{'code'}->{$u};
140 }
141 elsif (defined $self->{'block'}->{$u >> 8}) {
142 9 $font_number = $self->{'block'}->{$u >> 8};
143 }
144  
145 9 ▶ if (defined $last_font_number and $font_number != $last_font_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_font_number and $font_number != $last_font_number) 1 8 9
Condition100%3/3
defined $last_font_number and $font_number != $last_font_number
!ll&&!rl&&r
261
MC/DC100%2/2
defined $last_font_number and $font_number != $last_font_number
defined $last_font_number $font_number != $last_font_number
Truth table100%3/3
A and B
A= defined $last_font_numberB= $font_number != $last_font_number
ABresult
0X0
100
111
146 1 my $font = $self->fontlist->[$last_font_number];
147 1 $value .= '/' . $font->name() . ' ' . $size . ' Tf ';
148 1 $value .= $font->text(pack('U*', @codes), $size, $indent) . ' ';
149 1 $indent = undef;
150 1 @codes = ();
151 }
152  
153 9 push @codes, $u;
154 9 $last_font_number = $font_number;
155 }
156  
157 2 ▶ if (scalar @codes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @codes > 0) 2 0 2
158 2 my $font = $self->fontlist->[$last_font_number];
159 2 $value .= '/' . $font->name() . ' ' . $size . ' Tf ';
160 2 $value .= $font->text(pack('U*', @codes), $size, $indent);
161 }
162  
163 2 return $value;
164 }
165  
166 2 1;
« API2/Lite.pm ↑ Summary API2/Resource/XObject/Image/GD.pm »