Builder/Resource/UniFont.pm

filter: Statementstmt 79.8% 83 / 104 Branchbran 56.2% 27 / 48 Conditioncond 46.6% 7 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 70.6% 135 / 191 CC 40 SCAR 39.0
CC
40
Cov
70%
new
94.5
text
10.3
width
9.3
CRAP 49.3
« Builder/Resource/XObject/Form.pm ↑ Summary Builder/Basic/PDF/Filter.pm »
1 package PDF::Builder::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 = '3.028'; # VERSION
7 2 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 PDF::Builder::Resource::UniFont - Unicode Font Support
15  
16 =head1 METHODS
17  
18 =head2 new
19  
20 $font = PDF::Builder::Resource::UniFont->new($pdf, @fontspecs, %opts)
21  
22 =over
23  
24 Returns a uni-font object.
25  
26 B<FONTSPECS:> fonts can be registered using the following hash-ref:
27  
28 {
29 font => $fontobj, # the font to be registered
30 blocks => $blockspec, # the unicode blocks the font is being registered for
31 codes => $codespec, # the unicode codepoints, -"-
32 }
33  
34 B<BLOCKSPECS:>
35  
36 [
37 $block1, $block3, # register font for block 1 + 3
38 [$blockA, $blockZ], # register font for blocks A .. Z
39 ]
40  
41 B<CODESPECS:>
42  
43 [
44 $cp1, $cp3, # register font for codepoint 1 + 3
45 [$cpA, $cpZ], # register font for codepoints A .. Z
46 ]
47  
48 B<NOTE:> if you want to register a font for the entire unicode space
49 (ie. U+0000 .. U+FFFF), then simply specify a font-object without the hash-ref.
50  
51 =back
52  
53 =head3 Valid options (%opts)
54  
55 =over
56  
57 =item encode
58  
59 Changes the encoding of the font from its default.
60 See "perldoc Encode" for a list of valid tags.
61  
62 =back
63  
64 =cut
65  
66 1 ▶sub new {
Subroutine new: called CC=23 SCAR=45.5
Pod documented
67 1 my $class = shift();
68 1 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
69  
70 1 my $self = {
71 'fonts' => [],
72 'block' => {},
73 'code' => {},
74 'pdf' => shift(),
75 };
76 1 bless $self, $class;
77  
78 1 my @fonts;
79 1 ▶ push @fonts, shift() while ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and do { push @fonts, shift() } 2 1 3
80  
81 1 my %options = @_;
82 # copy dashed option names to preferred undashed names
83 0 ▶ if (defined $options{'-encode'} && !defined $options{'encode'}) { $options{'encode'} = delete($options{'-encode'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-encode"} and not defined $options{"encode"}) 0 1 1
Condition33%1/3
defined $options{"-encode"} and not defined $options{"encode"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $options{"-encode"} and not defined $options{"encode"}
defined $options{"-encode"} not defined $options{"encode"}
Truth table33%1/3
A and not(B)
A= defined $options{"-encode"}B= not defined $options{"encode"}
ABresult
0X0
100
111
84  
85 1 ▶ $self->{'encode'} = $options{'encode'} if defined $options{'encode'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"encode"} 1 0 1
86 # note that self->encode is undefined if encode not given!
87  
88 1 my $font_number = 0;
89 1 foreach my $font (@fonts) {
90 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
91 1 push @{$self->{'fonts'}}, shift(@$font);
92
93 1 ▶ while (defined $font->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $font->[0]) 1 1 2
94 1 my $blockspec = shift @$font;
95 1 ▶ if (ref($blockspec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $blockspec) { } 1 0 1
96 1 foreach my $block ($blockspec->[0] .. $blockspec->[-1]) {
97 1 $self->{'block'}->{$block} = $font_number;
98 }
99 } else {
100 0 $self->{'block'}->{$blockspec} = $font_number;
101 }
102 }
103 } elsif (ref($font) eq 'HASH') {
104 0 push @{$self->{'fonts'}}, $font->{'font'};
105  
106 0 ▶ if (defined $font->{'blocks'} and
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
107 ref($font->{'blocks'}) eq 'ARRAY') {
108 0 foreach my $blockspec (@{$font->{'blocks'}}) {
109 0 ▶ if (ref($blockspec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $blockspec) { } 0 0 0
110 0 foreach my $block ($blockspec->[0] .. $blockspec->[-1]) {
111 0 $self->{'block'}->{$block} = $font_number;
112 }
113 } else {
114 0 $self->{'block'}->{$blockspec} = $font_number;
115 }
116 }
117 }
118  
119 0 ▶ if (defined $font->{'codes'} and
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
120 ref($font->{'codes'}) eq 'ARRAY') {
121 0 foreach my $codespec (@{$font->{'codes'}}) {
122 0 ▶ if (ref($codespec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codespec) { } 0 0 0
123 0 foreach my $code ($codespec->[0] .. $codespec->[-1]) {
124 0 $self->{'code'}->{$code} = $font_number;
125 }
126 } else {
127 0 $self->{'code'}->{$codespec} = $font_number;
128 }
129 }
130 }
131 } else {
132 1 push @{$self->{'fonts'}}, $font;
133 1 foreach my $block (0 .. 255) {
134 256 $self->{'block'}->{$block} = $font_number;
135 }
136 }
137 2 $font_number++;
138 }
139  
140 1 return $self;
141 }
142  
143 =head2 isvirtual
144  
145 $flag = $font->isvirtual()
146  
147 =over
148  
149 (No Information)
150  
151 =back
152  
153 =cut
154  
155 2 ▶sub isvirtual {
Subroutine isvirtual: called CC=1 SCAR=0.0
Pod documented
156 2 return 1;
157 }
158  
159 =head2 fontlist
160  
161 $font->fontlist()
162  
163 =over
164  
165 (No Information)
166  
167 =back
168  
169 =cut
170  
171 8 ▶sub fontlist {
Subroutine fontlist: called CC=1 SCAR=0.0
Pod documented
172 8 my $self = shift;
173  
174 8 return [@{ $self->{'fonts'} }];
175 }
176  
177 =head2 width
178  
179 $w = $font->width($string)
180  
181 =over
182  
183 (No Information)
184  
185 =back
186  
187 =cut
188  
189 2 ▶sub width {
Subroutine width: called CC=9 SCAR=22.3
Pod documented
190 2 my ($self, $text) = @_;
191  
192 2 ▶ if (defined $self->{'encode'}) { # is self->encode guaranteed set?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"encode"}) 2 0 2
193 2 ▶ $text = decode($self->{'encode'}, $text) unless utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($text) 1 1 2
194 }
195 2 my $width = 0;
196 2 my @blocks = ();
197  
198 2 foreach my $u (unpack('U*', $text)) {
199 9 my $font_number = 0;
200 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
201 0 $font_number = $self->{'code'}->{$u};
202 } elsif (defined $self->{'block'}->{($u >> 8)}) {
203 9 $font_number = $self->{'block'}->{($u >> 8)};
204 } else {
205 0 $font_number = 0;
206 }
207 9 ▶ if (scalar @blocks == 0 or $blocks[-1]->[0] != $font_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @blocks == 0 or $blocks[-1][0] != $font_number) { } 3 6 9
Condition100%3/3
scalar @blocks == 0 or $blocks[-1][0] != $font_number
l!l&&r!l&&!r
216
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
208 3 push @blocks, [$font_number, pack('U', $u)];
209 } else {
210 6 $blocks[-1]->[1] .= pack('U', $u);
211 }
212 }
213 2 foreach my $block (@blocks) {
214 3 my ($font_number, $string) = @$block;
215 3 $width += $self->fontlist()->[$font_number]->width($string);
216 }
217  
218 2 return $width;
219 }
220  
221 =head2 text
222  
223 $font->text($string, $size, $indent)
224  
225 =over
226  
227 (No Information)
228  
229 =back
230  
231 =cut
232  
233 2 ▶sub text {
Subroutine text: called CC=10 SCAR=23.3
Pod documented
234 2 my ($self, $text, $size, $indent) = @_;
235  
236 2 ▶ if (defined $self->{'encode'}) { # is self->encode guaranteed to be defined?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"encode"}) 2 0 2
237 2 ▶ $text = decode($self->{'encode'}, $text) unless utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($text) 1 1 2
238 }
239 2 ▶ croak 'Font size not specified' unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 2 2
240  
241 2 my $newtext = '';
242 2 my $last_font_number;
243 my @codes;
244  
245 2 foreach my $u (unpack('U*', $text)) {
246 9 my $font_number = 0;
247 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
248 0 $font_number = $self->{'code'}->{$u};
249 } elsif (defined $self->{'block'}->{($u >> 8)}) {
250 9 $font_number = $self->{'block'}->{($u >> 8)};
251 }
252  
253 9 ▶ if (defined $last_font_number and
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
254 $font_number != $last_font_number) {
255 1 my $font = $self->fontlist()->[$last_font_number];
256 1 $newtext .= '/' . $font->name() . ' ' . $size. ' Tf ';
257 1 $newtext .= $font->text(pack('U*', @codes), $size, $indent) . ' ';
258 1 $indent = undef;
259 1 @codes = ();
260 }
261  
262 9 push @codes, $u;
263 9 $last_font_number = $font_number;
264 }
265  
266 2 ▶ if (scalar @codes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @codes > 0) 2 0 2
267 2 my $font = $self->fontlist()->[$last_font_number];
268 2 $newtext .= '/' . $font->name() . ' ' . $size . ' Tf ';
269 2 $newtext .= $font->text(pack('U*', @codes), $size, $indent);
270 }
271  
272 2 return $newtext;
273 }
274  
275 2 1;
« Builder/Resource/XObject/Form.pm ↑ Summary Builder/Basic/PDF/Filter.pm »