Builder/Resource/Font.pm

filter: Statementstmt 60.4% 81 / 134 Branchbran 43.7% 7 / 16 Conditioncond 62.5% 35 / 56 MC/DCmcdc 21.2% 7 / 33 Subroutinesub 77.7% 7 / 9 Podpod 100% 3 / 3 totaltotal 55.7% 140 / 251 CC 42 SCAR 39.5
CC
42
Cov
60%
automap
132.0
encodeByData
36.9
remap
12.0
CRAP 52.0
« Builder/Resource/Font/CoreFont.pm ↑ Summary Builder/Resource/XObject/Form.pm »
1 package PDF::Builder::Resource::Font;
2  
3 8 ▶use base 'PDF::Builder::Resource::BaseFont';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our $VERSION = '3.028'; # VERSION
9 8 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10  
11 8 ▶use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 PDF::Builder::Resource::Font - Some common support routines for font files
19  
20 Inherits from L<PDF::Builder::Resource::BaseFont>
21  
22 =head1 METHODS
23  
24 =head2 encodeByData
25  
26 $font->encodeByData($enc)
27  
28 =over
29  
30 (No Information)
31  
32 =back
33  
34 =cut
35  
36 37 ▶sub encodeByData {
Subroutine encodeByData: called CC=30 SCAR=36.1
Pod documented
37 37 my ($self, $encoding) = @_;
38  
39 37 my $data = $self->data();
40  
41 37 ▶ if ($self->issymbol()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->issymbol) 3 34 37
42 3 $encoding = undef;
43 }
44  
45 37 ▶ if (defined $encoding && $encoding =~ m|^uni(\d+)$|o) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $encoding and $encoding =~ /^uni(\d+)$/o) { } 1 36 37
elsif (defined $encoding) { } 33 3 36
elsif (defined $data->{'uni'}) { } 3 0 3
Condition100%3/3
defined $encoding and $encoding =~ /^uni(\d+)$/o
!ll&&!rl&&r
3331
MC/DC100%2/2
defined $encoding and $encoding =~ /^uni(\d+)$/o
defined $encoding $encoding =~ /^uni(\d+)$/o
Truth table100%3/3
A and B
A= defined $encodingB= $encoding =~ /^uni(\d+)$/o
ABresult
0X0
100
111
46 1 my $blk = $1;
47 1 $data->{'e2u'} = [ map { $blk*256+$_ } (0..255) ];
48 1 ▶ $data->{'e2n'} = [ map { nameByUni($_) || '.notdef' } @{$data->{'e2u'}} ];
Condition50%1/2
nameByUni($_) || '.notdef'
l!l
2560
MC/DC0%0/1
nameByUni($_) || '.notdef'
nameByUni($_)
Truth table50%1/2
A
A= nameByUni($_)
Aresult
11
00
49 1 $data->{'firstchar'} = 0;
50 } elsif (defined $encoding) {
51 33 $data->{'e2u'} = [ unpack('U*', decode($encoding, pack('C*', (0..255)))) ];
52 33 ▶ $data->{'e2n'} = [ map { nameByUni($_) || '.notdef' } @{$data->{'e2u'}} ];
Condition50%1/2
nameByUni($_) || '.notdef'
l!l
84480
MC/DC0%0/1
nameByUni($_) || '.notdef'
nameByUni($_)
Truth table50%1/2
A
A= nameByUni($_)
Aresult
11
00
53 } elsif (defined $data->{'uni'}) {
54 3 $data->{'e2u'} = [ @{$data->{'uni'}} ];
55 3 ▶ $data->{'e2n'} = [ map { $_ || '.notdef' } @{$data->{'char'}} ];
Condition50%1/2
$_ || '.notdef'
l!l
7680
MC/DC0%0/1
$_ || '.notdef'
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
56 } else {
57 0 $data->{'e2u'} = [ map { uniByName($_) } @{$data->{'char'}} ];
58 0 ▶ $data->{'e2n'} = [ map { $_ || '.notdef' } @{$data->{'char'}} ];
Condition0%0/2
$_ || '.notdef'
l!l
00
MC/DC0%0/1
$_ || '.notdef'
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
59 }
60  
61 37 $data->{'u2c'} = {};
62 37 $data->{'u2e'} = {};
63 37 $data->{'u2n'} = {};
64 37 $data->{'n2c'} = {};
65 37 $data->{'n2e'} = {};
66 37 $data->{'n2u'} = {};
67  
68 37 foreach my $n (0..255) {
69 9472 my $xchar = undef;
70 9472 my $xuni = undef;
71 9472 ▶ $xchar = $data->{'char'}->[$n] // '.notdef';
Condition50%1/2
$data->{"char"}[$n] // ".notdef"
l!l
94720
MC/DC0%0/1
$data->{"char"}[$n] // ".notdef"
$data->{"char"}[$n]
Truth table50%1/2
A
A= $data->{"char"}[$n]
Aresult
11
00
72 9472 ▶ $data->{'n2c'}->{$xchar} //= $n;
Condition66%2/3
$data->{"n2c"}{$xchar} //= $n
l!l&&r!l&&!r
149779750
MC/DC0%0/2
$data->{"n2c"}{$xchar} //= $n
$data->{"n2c"}{$xchar} $n
Truth table66%2/3
A //= B
A= $data->{"n2c"}{$xchar}B= $n
ABresult
1X1
011
000
73  
74 9472 ▶ $xchar = $data->{'e2n'}->[$n] // '.notdef';
Condition50%1/2
$data->{"e2n"}[$n] // ".notdef"
l!l
94720
MC/DC0%0/1
$data->{"e2n"}[$n] // ".notdef"
$data->{"e2n"}[$n]
Truth table50%1/2
A
A= $data->{"e2n"}[$n]
Aresult
11
00
75 9472 ▶ $data->{'n2e'}->{$xchar} //= $n;
Condition66%2/3
$data->{"n2e"}{$xchar} //= $n
l!l&&r!l&&!r
63388390
MC/DC0%0/2
$data->{"n2e"}{$xchar} //= $n
$data->{"n2e"}{$xchar} $n
Truth table66%2/3
A //= B
A= $data->{"n2e"}{$xchar}B= $n
ABresult
1X1
011
000
76  
77 9472 ▶ $data->{'n2u'}->{$xchar} //= $data->{'e2u'}->[$n];
Condition100%3/3
$data->{"n2u"}{$xchar} //= $data->{"e2u"}[$n]
l!l&&r!l&&!r
667878025
MC/DC100%2/2
$data->{"n2u"}{$xchar} //= $data->{"e2u"}[$n]
$data->{"n2u"}{$xchar} $data->{"e2u"}[$n]
Truth table100%3/3
A //= B
A= $data->{"n2u"}{$xchar}B= $data->{"e2u"}[$n]
ABresult
1X1
011
000
78  
79 9472 ▶ $xchar = $data->{'char'}->[$n] // '.notdef';
Condition50%1/2
$data->{"char"}[$n] // ".notdef"
l!l
94720
MC/DC0%0/1
$data->{"char"}[$n] // ".notdef"
$data->{"char"}[$n]
Truth table50%1/2
A
A= $data->{"char"}[$n]
Aresult
11
00
80 9472 ▶ $xuni = $data->{'uni'}->[$n] // 0;
Condition100%2/2
$data->{"uni"}[$n] // 0
l!l
81881284
MC/DC100%1/1
$data->{"uni"}[$n] // 0
$data->{"uni"}[$n]
Truth table100%2/2
A
A= $data->{"uni"}[$n]
Aresult
11
00
81 9472 ▶ $data->{'n2u'}->{$xchar} //= $xuni;
Condition66%2/3
$data->{"n2u"}{$xchar} //= $xuni
l!l&&r!l&&!r
92042680
MC/DC0%0/2
$data->{"n2u"}{$xchar} //= $xuni
$data->{"n2u"}{$xchar} $xuni
Truth table66%2/3
A //= B
A= $data->{"n2u"}{$xchar}B= $xuni
ABresult
1X1
011
000
82  
83 9472 ▶ $data->{'u2c'}->{$xuni} //= $n;
Condition66%2/3
$data->{"u2c"}{$xuni} //= $n
l!l&&r!l&&!r
151979530
MC/DC0%0/2
$data->{"u2c"}{$xuni} //= $n
$data->{"u2c"}{$xuni} $n
Truth table66%2/3
A //= B
A= $data->{"u2c"}{$xuni}B= $n
ABresult
1X1
011
000
84  
85 9472 ▶ $xuni = $data->{'e2u'}->[$n] // 0;
Condition100%2/2
$data->{"e2u"}[$n] // 0
l!l
9276196
MC/DC100%1/1
$data->{"e2u"}[$n] // 0
$data->{"e2u"}[$n]
Truth table100%2/2
A
A= $data->{"e2u"}[$n]
Aresult
11
00
86 9472 ▶ $data->{'u2e'}->{$xuni} //= $n;
Condition66%2/3
$data->{"u2e"}{$xuni} //= $n
l!l&&r!l&&!r
19392790
MC/DC0%0/2
$data->{"u2e"}{$xuni} //= $n
$data->{"u2e"}{$xuni} $n
Truth table66%2/3
A //= B
A= $data->{"u2e"}{$xuni}B= $n
ABresult
1X1
011
000
87  
88 9472 ▶ $xchar = $data->{'e2n'}->[$n] // '.notdef';
Condition50%1/2
$data->{"e2n"}[$n] // ".notdef"
l!l
94720
MC/DC0%0/1
$data->{"e2n"}[$n] // ".notdef"
$data->{"e2n"}[$n]
Truth table50%1/2
A
A= $data->{"e2n"}[$n]
Aresult
11
00
89 9472 ▶ $data->{'u2n'}->{$xuni} //= $xchar;
Condition66%2/3
$data->{"u2n"}{$xuni} //= $xchar
l!l&&r!l&&!r
19492780
MC/DC0%0/2
$data->{"u2n"}{$xuni} //= $xchar
$data->{"u2n"}{$xuni} $xchar
Truth table66%2/3
A //= B
A= $data->{"u2n"}{$xuni}B= $xchar
ABresult
1X1
011
000
90  
91 9472 ▶ $xchar = $data->{'char'}->[$n] // '.notdef';
Condition50%1/2
$data->{"char"}[$n] // ".notdef"
l!l
94720
MC/DC0%0/1
$data->{"char"}[$n] // ".notdef"
$data->{"char"}[$n]
Truth table50%1/2
A
A= $data->{"char"}[$n]
Aresult
11
00
92 9472 ▶ $xuni = $data->{'uni'}->[$n] //= 0;
Condition100%2/2
$data->{"uni"}[$n] //= 0
l!l
81881284
MC/DC100%1/1
$data->{"uni"}[$n] //= 0
$data->{"uni"}[$n]
Truth table100%2/2
A
A= $data->{"uni"}[$n]
Aresult
11
00
93 9472 ▶ $data->{'u2n'}->{$xuni} //= $xchar;
Condition66%2/3
$data->{"u2n"}{$xuni} //= $xchar
l!l&&r!l&&!r
837111010
MC/DC0%0/2
$data->{"u2n"}{$xuni} //= $xchar
$data->{"u2n"}{$xuni} $xchar
Truth table66%2/3
A //= B
A= $data->{"u2n"}{$xuni}B= $xchar
ABresult
1X1
011
000
94 }
95  
96 37 my $en = PDFDict();
97 37 $self->{'Encoding'} = $en;
98  
99 37 $en->{'Type'} = PDFName('Encoding');
100 37 $en->{'BaseEncoding'} = PDFName('WinAnsiEncoding');
101  
102 37 $en->{'Differences'} = PDFArray(PDFNum(0));
103 37 foreach my $n (0..255) {
104 9472 ▶ my $element = $self->glyphByEnc($n) || '.notdef';
Condition50%1/2
$self->glyphByEnc($n) || '.notdef'
l!l
94720
MC/DC0%0/1
$self->glyphByEnc($n) || '.notdef'
$self->glyphByEnc($n)
Truth table50%1/2
A
A= $self->glyphByEnc($n)
Aresult
11
00
105 9472 $en->{'Differences'}->add_elements(PDFName($element));
106 }
107  
108 37 $self->{'FirstChar'} = PDFNum($data->{'firstchar'});
109 37 $self->{'LastChar'} = PDFNum($data->{'lastchar'});
110  
111 37 $self->{'Widths'} = PDFArray();
112 37 foreach my $n ($data->{'firstchar'} .. $data->{'lastchar'}) {
113 8320 $self->{'Widths'}->add_elements(PDFNum($self->wxByEnc($n)));
114 }
115  
116 37 return $self;
117 }
118  
119 =head2 automap
120  
121 $font->automap()
122  
123 =over
124  
125 This applies to core fonts (C<< $pdf->corefont() >>) and PostScript fonts
126 (C<< $pdf->psfont() >>). These cannot use UTF-8 (or other multibyte character)
127 encoded text; only single byte characters. This limits a font to a maximum of
128 256 glyphs (the "standard" single-byte encoding being used). Any other glyphs
129 supplied with the font are inaccessible.
130  
131 C<automap> splits a font containing more than 256 glyphs into "planes" of single
132 byte fonts of up to 256 glyphs, so that all glyphs may be accessed in separate
133 "fonts". An array of new fonts will be returned, with [0] being the standard
134 code page (of the selected encoding). If there are any glyphs beyond xFF on the
135 standard encoding page, they will be returned in one or more additional fonts
136 of 223 glyphs each. I<Why 223?> The first 32 are reserved as control characters
137 (although they have no glyphs), and number x20 is a space. This, plus 223,
138 gives 256 in total (the last plane may have fewer than 223 glyphs). These
139 "fonts" are temporary (dynamic), though as usable as any other font.
140  
141 Note that a plane may be B<empty> (only I<space> at x20 and possibly an unusable
142 character at x21) if the previous plane was full. You might want to check if
143 any character in the plane has a Unicode value (if not, it's empty).
144  
145 The I<ordering> of these 223 glyphs in each following plane does I<not> appear
146 to follow any particular official scheme, so be sure to reference something like
147 C<examples/020_corefonts> to see what is available, and what code point a glyph
148 is at (e.g., an 'A' in the text stream will print something different if you're
149 not on plane 0). For a given font B<file>, they should be I<consistent>. For
150 instance, in Times-Roman core font, an \x21 or ! in plane[1] should always give
151 an A+macron. Further note that new editions of font files released in the future
152 may have changes to the glyph list and the ordering (affecting which plane a
153 glyph appears on), so use automap() with caution. It appears that glyphs are
154 sorted by Unicode number, but if a new glyph is inserted, it would bump other
155 glyphs to new positions, and even to the next plane.
156  
157 An example:
158  
159 $fnt = $pdf->corefont('Times-Roman', 'encode' => 'latin1');
160 @planes = ($fnt, $fnt->automap()); # two planes
161 $text->font($planes[0], 15); # or just $fnt will work
162 $text->text('!'); # prints !
163 $text->font($planes[1], 15);
164 $text->text('!'); # prints A+macron
165  
166 If you had used 'latin2' encoding, an \x21 on plane 1 will give an inverted !
167 (&iexcl; HTML entity).
168  
169 Note that C<< $planes[$n]->fontname() >> should always be the desired base
170 font (e.g., I<Times-Roman>), while C<< $planes[$n]->name() >> will be the font
171 ID (e.g., I<TiRoCBC>) for plane 0, while for other planes there will be a
172 unique suffix added (e.g., I<TiRoCBCam0>).
173  
174 If you have just an occasional non-plane 0 character (or run of characters),
175 it may be tolerable to switch back and forth between planes like this, just as
176 typing an HTML entity once in a while when you need a Greek letter on a web page
177 is acceptable to most people. However, if you're typing a lot of Greek text, a
178 dedicated keyboard may be better for you. Like that, switching to a TTF font in
179 order to be able to use UTF-8 may be easier.
180  
181 =back
182  
183 =cut
184  
185 0 ▶sub automap {
Subroutine automap: not called CC=11 SCAR=48.8
Pod documented
186 0 my ($self) = @_;
187 0 my $data = $self->data();
188  
189 0 my %gl = map { $_ => defineName($_) } keys %{$data->{'wx'}};
190  
191 0 foreach my $n (0..255) {
192 0 delete $gl{$data->{'e2n'}->[$n]};
193 }
194  
195 0 ▶ if (defined $data->{'comps'} && !$self->{'-nocomps'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"comps"} and not $self->{"-nocomps"}) 0 0 0
Condition0%0/3
defined $data->{"comps"} and not $self->{"-nocomps"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data->{"comps"} and not $self->{"-nocomps"}
defined $data->{"comps"} not $self->{"-nocomps"}
Truth table0%0/3
A and not(B)
A= defined $data->{"comps"}B= not $self->{"-nocomps"}
ABresult
0X0
100
111
196 0 foreach my $n (keys %{$data->{'comps'}}) {
197 0 delete $gl{$n};
198 }
199 }
200  
201 0 my @nm = sort { $gl{$a} <=> $gl{$b} } keys %gl;
202  
203 0 my @fonts = ();
204 0 my $count = 0;
205 0 ▶ while (my @glyphs = splice(@nm, 0, 223)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@glyphs) = splice(@nm, 0, 223)) 0 0 0
206 0 my $obj = $self->SUPER::new($self->{' apipdf'},
207 $self->name() . 'am' . $count);
208 0 $obj->{' data'} = { %$data };
209 0 $obj->data()->{'firstchar'} = 32;
210 0 $obj->data()->{'lastchar'} = 32 + scalar(@glyphs);
211 0 push(@fonts, $obj);
212 0 foreach my $key (qw( Subtype BaseFont FontDescriptor )) {
213 0 ▶ $obj->{$key} = $self->{$key} if defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$key} 0 0 0
214 }
215 0 $obj->data()->{'char'} = [];
216 0 $obj->data()->{'uni'} = [];
217 0 foreach my $n (0..31) {
218 0 $obj->data()->{'char'}->[$n] = '.notdef';
219 0 $obj->data()->{'uni'}->[$n] = 0;
220 }
221 0 $obj->data()->{'char'}->[32] = 'space';
222 0 $obj->data()->{'uni'}->[32] = 32;
223 0 foreach my $n (33 .. $obj->data()->{'lastchar'}) {
224 0 $obj->data()->{'char'}->[$n] = $glyphs[$n-33];
225 0 $obj->data()->{'uni'}->[$n] = $gl{$glyphs[$n-33]};
226 }
227 0 foreach my $n (($obj->data()->{'lastchar'}+1) .. 255) {
228 0 $obj->data()->{'char'}->[$n] = '.notdef';
229 0 $obj->data()->{'uni'}->[$n] = 0;
230 }
231 0 $obj->encodeByData(undef);
232  
233 0 $count++;
234 }
235  
236 0 return @fonts;
237 }
238  
239 =head2 remap
240  
241 $font->remap($enc)
242  
243 =over
244  
245 (No Information)
246  
247 =back
248  
249 =cut
250  
251 0 ▶sub remap {
Subroutine remap: not called CC=3 SCAR=24.8
Pod documented
252 0 my ($self, $enc) = @_;
253  
254 0 my $obj = $self->SUPER::new($self->{' apipdf'},
255 $self->name() . 'rm' . pdfkey());
256 0 $obj->{' data'}={ %{$self->data()} };
257 0 foreach my $key (qw( Subtype BaseFont FontDescriptor )) {
258 0 ▶ $obj->{$key} = $self->{$key} if defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$key} 0 0 0
259 }
260  
261 0 $obj->encodeByData($enc);
262  
263 0 return $obj;
264 }
265  
266 8 1;
« Builder/Resource/Font/CoreFont.pm ↑ Summary Builder/Resource/XObject/Form.pm »