Builder/Resource/CIDFont/CJKFont.pm

filter: Statementstmt 65.5% 76 / 116 Branchbran 33.3% 12 / 36 Conditioncond 20.0% 5 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 75.0% 9 / 12 Podpod 75.0% 3 / 4 totaltotal 50.7% 105 / 207 CC 30 SCAR 35.8
CC
30
Cov
53%
outobjdeep
110.0
_look_for_font
21.4
new
14.5
CRAP 35.7
« Builder/Resource/XObject/Form/BarCode.pm ↑ Summary Builder/NamedDestination.pm »
1 package PDF::Builder::Resource::CIDFont::CJKFont;
2  
3 2 ▶use base 'PDF::Builder::Resource::CIDFont';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '3.028'; # VERSION
9 2 our $LAST_UPDATE = '3.028'; # manually update whenever code is changed
10  
11 2 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $fonts = {};
15 2 our $cmap = {};
16 2 our $alias;
17 2 our $subs;
18  
19 =head1 NAME
20  
21 PDF::Builder::Resource::CIDFont::CJKFont - Base class for CJK fonts
22  
23 Inherits from L<PDF::Builder::Resource::CIDFont>
24  
25 B<Note:> CJK font support is somewhat obsolete. Try directly using an
26 up-to-date TTF or OTF font, with UTF-8 input, instead.
27  
28 =head1 METHODS
29  
30 =head2 new
31  
32 $font = PDF::Builder::Resource::CIDFont::CJKFont->new($pdf, $cjkname, %opts)
33  
34 =over
35  
36 Returns a cjk-font object.
37  
38 =back
39  
40 =over
41  
42 =item *
43  
44 B<Traditional Chinese:> Ming, Ming-Bold, Ming-Italic, Ming-BoldItalic
45  
46 =item *
47  
48 B<Simplified Chinese:> Song, Song-Bold, Song-Italic, Song-BoldItalic
49  
50 =item *
51  
52 B<Korean:> MyungJo, MyungJo-Bold, MyungJo-Italic, MyungJo-BoldItalic
53  
54 =item *
55  
56 B<Japanese (Mincho):> KozMin, KozMin-Bold, KozMin-Italic, KozMin-BoldItalic
57  
58 =item *
59  
60 B<Japanese (Gothic):> KozGo, KozGo-Bold, KozGo-Italic, KozGo-BoldItalic
61  
62 =back
63  
64 Defined Options (%opts):
65  
66 =over
67  
68 =item encode
69  
70 specify fonts encoding for non-utf8 text.
71  
72 =back
73  
74 =cut
75  
76 1 ▶sub _look_for_font {
Subroutine _look_for_font: called CC=8 SCAR=30.6
77 1 my $fname = lc(shift);
78  
79 1 $fname =~ s/[^a-z0-9]+//gi;
80 1 ▶ $fname = $alias->{$fname} if defined $alias->{$fname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::Builder::Resource::CIDFont::CJKFont::alias->{$fname} 1 0 1
81 0 ▶ return {%{$fonts->{$fname}}} if defined $fonts->{$fname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::Builder::Resource::CIDFont::CJKFont::fonts->{$fname} 0 1 1
82  
83 1 ▶ if (defined $subs->{$fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PDF::Builder::Resource::CIDFont::CJKFont::subs->{$fname}) 0 1 1
84 0 my $data = _look_for_font($subs->{$fname}->{'-alias'});
85 0 foreach my $k (keys %{$subs->{$fname}}) {
86 0 ▶ next if $k =~ /^\-/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^\-/ 0 0 0
87 0 ▶ if (substr($k, 0, 1) eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($k, 0, 1) eq '+') { } 0 0 0
88 0 $data->{substr($k, 1)} .= $subs->{$fname}->{$k};
89 } else {
90 0 $data->{$k} = $subs->{$fname}->{$k};
91 }
92 }
93 0 $fonts->{$fname} = $data;
94 0 return {%$data};
95 }
96  
97 1 eval "require 'PDF/Builder/Resource/CIDFont/CJKFont/$fname.data'"; ## no critic
98 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
99 1 return {%{$fonts->{$fname}}};
100 } else {
101 0 die "requested font '$fname' not installed ";
102 }
103 }
104  
105 # identical routine in Resource/CIDFont/TrueType/FontFile.pm
106 1 ▶sub _look_for_cmap {
Subroutine _look_for_cmap: called CC=3 SCAR=11.7
107 1 my $map = shift;
108 1 my $fname = lc($map);
109  
110 1 $fname =~ s/[^a-z0-9]+//gi;
111 0 ▶ return {%{$cmap->{$fname}}} if defined $cmap->{$fname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::Builder::Resource::CIDFont::CJKFont::cmap->{$fname} 0 1 1
112 1 eval "require 'PDF/Builder/Resource/CIDFont/CMap/$fname.cmap'"; ## no critic
113 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
114 1 return {%{$cmap->{$fname}}};
115 } else {
116 0 die "requested cmap '$map' not installed ";
117 }
118 }
119  
120 # compare to TrueType/FontFile.pm: .data and .cmap files are apparently
121 # required when using cjkfont(), so no looking at internal cmap tables
122 1 ▶sub new {
Subroutine new: called CC=12 SCAR=26.7
Pod documented
123 1 my ($class, $pdf, $name, @opts) = @_;
124  
125 1 my %opts = ();
126 1 ▶ %opts = @opts if (scalar @opts)%2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@opts) % 2 == 0 1 0 1
127 # copy dashed option names to preferred undashed names
128 0 ▶ if (defined $opts{'-encode'} && !defined $opts{'encode'}) { $opts{'encode'} = delete($opts{'-encode'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-encode"} and not defined $opts{"encode"}) 0 1 1
Condition33%1/3
defined $opts{"-encode"} and not defined $opts{"encode"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $opts{"-encode"} and not defined $opts{"encode"}
defined $opts{"-encode"} not defined $opts{"encode"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-encode"}B= not defined $opts{"encode"}
ABresult
0X0
100
111
129 1 ▶ $opts{'encode'} ||= 'ident';
Condition50%1/2
$opts{"encode"} ||= "ident"
l!l
01
MC/DC0%0/1
$opts{"encode"} ||= "ident"
$opts{"encode"}
Truth table50%1/2
A
A= $opts{"encode"}
Aresult
11
00
130  
131 1 my $data = _look_for_font($name);
132  
133 1 my $cmap = _look_for_cmap($data->{'cmap'});
134  
135 1 $data->{'u2g'} = { %{$cmap->{'u2g'}} };
136 1 $data->{'g2u'} = [ @{$cmap->{'g2u'}} ];
137  
138 1 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
139 # ensure that apiname is initialized
140 1 ▶ my $key = ($data->{'apiname'} // '') . pdfkey();
Condition50%1/2
$data->{"apiname"} // ""
l!l
01
MC/DC0%0/1
$data->{"apiname"} // ""
$data->{"apiname"}
Truth table50%1/2
A
A= $data->{"apiname"}
Aresult
11
00
141 1 my $self = $class->SUPER::new($pdf, $key);
142 1 ▶ $pdf->new_obj($self) if defined($pdf) && !$self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
if defined $pdf and not $self->is_obj($pdf) 0 1 1
Condition33%1/3
defined $pdf and not $self->is_obj($pdf)
!ll&&!rl&&r
010
MC/DC0%0/2
defined $pdf and not $self->is_obj($pdf)
defined $pdf not $self->is_obj($pdf)
Truth table33%1/3
A and not(B)
A= defined $pdfB= not $self->is_obj($pdf)
ABresult
0X0
100
111
143  
144 1 $self->{' data'} = $data;
145  
146 1 ▶ if (defined $opts{'encode'} && $opts{'encode'} ne 'ident') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"encode"} and $opts{"encode"} ne "ident") 0 1 1
Condition33%1/3
defined $opts{"encode"} and $opts{"encode"} ne "ident"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $opts{"encode"} and $opts{"encode"} ne "ident"
defined $opts{"encode"} $opts{"encode"} ne "ident"
Truth table33%1/3
A and B
A= defined $opts{"encode"}B= $opts{"encode"} ne "ident"
ABresult
0X0
100
111
147 0 $self->data->{'encode'} = $opts{'encode'};
148 }
149  
150 1 my $emap = {
151 'reg' => 'Adobe',
152 'ord' => 'Identity',
153 'sup' => 0,
154 'map' => 'Identity',
155 'dir' => 'H',
156 'dec' => 'ident',
157 };
158  
159 1 ▶ if (defined $cmap->{'ccs'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmap->{"ccs"}) 1 0 1
160 1 $emap->{'reg'} = $cmap->{'ccs'}->[0];
161 1 $emap->{'ord'} = $cmap->{'ccs'}->[1];
162 1 $emap->{'sup'} = $cmap->{'ccs'}->[2];
163 }
164  
165 #if (defined $cmap->{'cmap'} && defined $cmap->{'cmap'}->{$opts{'encode'}} ) {
166 # $emap->{'dec'} = $cmap->{'cmap'}->{$opts{'encode'}}->[0];
167 # $emap->{'map'} = $cmap->{'cmap'}->{$opts{'encode'}}->[1];
168 #} elsif (defined $cmap->{'cmap'} && defined $cmap->{'cmap'}->{'utf8'}) {
169 # $emap->{'dec'} = $cmap->{'cmap'}->{'utf8'}->[0];
170 # $emap->{'map'} = $cmap->{'cmap'}->{'utf8'}->[1];
171 #}
172  
173 1 $self->data()->{'decode'} = $emap->{'dec'};
174  
175 $self->{'BaseFont'} = PDFName(join('-',
176 $self->fontname(),
177 $emap->{'map'},
178 1 $emap->{'dir'}));
179 $self->{'Encoding'} = PDFName(join('-',
180 $emap->{'map'},
181 1 $emap->{'dir'}));
182  
183 1 my $des = $self->descrByData();
184 1 my $de = $self->{' de'};
185  
186 1 $de->{'FontDescriptor'} = $des;
187 1 $de->{'Subtype'} = PDFName('CIDFontType0');
188 1 $de->{'BaseFont'} = PDFName($self->fontname());
189 1 $de->{'DW'} = PDFNum($self->missingwidth());
190 1 $de->{'CIDSystemInfo'}->{'Registry'} = PDFString($emap->{'reg'}, 'x');
191 1 $de->{'CIDSystemInfo'}->{'Ordering'} = PDFString($emap->{'ord'}, 'x');
192 1 $de->{'CIDSystemInfo'}->{'Supplement'} = PDFNum($emap->{'sup'});
193 ## $de->{'CIDToGIDMap'} = PDFName($emap->{'map'}); # ttf only
194  
195 1 return $self;
196 }
197  
198 0 ▶sub tounicodemap {
Subroutine tounicodemap: not called CC=1 SCAR=6.9
Pod undocumented
199 0 my $self = shift;
200  
201 # no-op since PDF knows its char-collection
202 0 return $self;
203 }
204  
205 =head2 glyphByCId
206  
207 $n = $font->glyphByCId($gid)
208  
209 =over
210  
211 Returns a character's glyph name (string), given its glyph ID.
212  
213 =back
214  
215 =cut
216  
217 0 ▶sub glyphByCId {
Subroutine glyphByCId: not called CC=1 SCAR=6.9
Pod documented
218 0 my ($self, $cid) = @_;
219  
220 0 my $uni = $self->uniByCId($cid);
221 0 return nameByUni($uni);
222 }
223  
224 0 ▶sub outobjdeep {
Subroutine outobjdeep: not called CC=10 SCAR=47.0
Pod documented
225 0 my ($self, $fh, $pdf) = @_;
226  
227 0 my $notdefbefore = 1;
228  
229 0 my $wx = PDFArray();
230 0 $self->{' de'}->{'W'} = $wx;
231 0 my $ml;
232  
233 0 foreach my $i (0 .. (scalar @{$self->data()->{'g2u'}} - 1 )) {
234 0 ▶ if (ref($self->data()->{'wx'}) eq 'ARRAY' &&
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->data->{'wx'} eq 'ARRAY' and defined $self->data->{'wx'}[$i] and $self->data->{'wx'}[$i] != $self->missingwidth) { } 0 0 0
elsif (ref $self->data->{'wx'} eq 'HASH' and defined $self->data->{'wx'}{$i} and $self->data->{'wx'}{$i} != $self->missingwidth) { } 0 0 0
Condition0%0/6
ref $self->data->{"wx"} eq "ARRAY" and defined $self->data->{"wx"}[$i] and $self->data->{"wx"}[$i] != $self->missingwidth
!ll&&!rl&&r
000
ref $self->data->{"wx"} eq "ARRAY" and defined $self->data->{"wx"}[$i]
!ll&&!rl&&r
000
MC/DC0%0/3
ref $self->data->{"wx"} eq "ARRAY" and defined $self->data->{"wx"}[$i] and $self->data->{"wx"}[$i] != $self->missingwidth
ref $self->data->{"wx"} eq "ARRAY" defined $self->data->{"wx"}[$i] $self->data->{"wx"}[$i] != $self->missingwidth
Truth table0%0/4
A and B and C
A= ref $self->data->{"wx"} eq "ARRAY"B= defined $self->data->{"wx"}[$i]C= $self->data->{"wx"}[$i] != $self->missingwidth
ABCresult
0XX0
10X0
1100
1111
235 (defined $self->data()->{'wx'}->[$i]) &&
236 ($self->data()->{'wx'}->[$i] != $self->missingwidth()) ) {
237 0 ▶ if ($notdefbefore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notdefbefore) 0 0 0
238 0 $notdefbefore = 0;
239 0 $ml = PDFArray();
240 0 $wx->add_elements(PDFNum($i), $ml);
241 }
242 0 $ml->add_elements(PDFNum($self->data()->{'wx'}->[$i]));
243 ▶ } elsif (ref($self->data()->{'wx'}) eq 'HASH' &&
Condition0%0/6
ref $self->data->{"wx"} eq "HASH" and defined $self->data->{"wx"}{$i} and $self->data->{"wx"}{$i} != $self->missingwidth
!ll&&!rl&&r
000
ref $self->data->{"wx"} eq "HASH" and defined $self->data->{"wx"}{$i}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $self->data->{"wx"} eq "HASH" and defined $self->data->{"wx"}{$i} and $self->data->{"wx"}{$i} != $self->missingwidth
ref $self->data->{"wx"} eq "HASH" defined $self->data->{"wx"}{$i} $self->data->{"wx"}{$i} != $self->missingwidth
Truth table0%0/4
A and B and C
A= ref $self->data->{"wx"} eq "HASH"B= defined $self->data->{"wx"}{$i}C= $self->data->{"wx"}{$i} != $self->missingwidth
ABCresult
0XX0
10X0
1100
1111
244 (defined $self->data()->{'wx'}->{$i}) &&
245 ($self->data()->{'wx'}->{$i} != $self->missingwidth()) ) {
246 0 ▶ if ($notdefbefore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notdefbefore) 0 0 0
247 0 $notdefbefore = 0;
248 0 $ml = PDFArray();
249 0 $wx->add_elements(PDFNum($i), $ml);
250 }
251 0 $ml->add_elements(PDFNum($self->data()->{'wx'}->{$i}));
252 } else {
253 0 $notdefbefore = 1;
254 }
255 }
256  
257 0 return $self->SUPER::outobjdeep($fh, $pdf);
258 }
259  
260 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 2 $alias = {
263 'traditional' => 'adobemingstdlightacro',
264 'traditionalbold' => 'mingbold',
265 'traditionalitalic' => 'mingitalic',
266 'traditionalbolditalic' => 'mingbolditalic',
267 'ming' => 'adobemingstdlightacro',
268  
269 'simplified' => 'adobesongstdlightacro',
270 'simplifiedbold' => 'songbold',
271 'simplifieditalic' => 'songitalic',
272 'simplifiedbolditalic' => 'songbolditalic',
273 'song' => 'adobesongstdlightacro',
274  
275 'korean' => 'adobemyungjostdmediumacro',
276 'koreanbold' => 'myungjobold',
277 'koreanitalic' => 'myungjoitalic',
278 'koreanbolditalic' => 'myungjobolditalic',
279 'myungjo' => 'adobemyungjostdmediumacro',
280  
281 'japanese' => 'kozminproregularacro',
282 'japanesebold' => 'kozminbold',
283 'japaneseitalic' => 'kozminitalic',
284 'japanesebolditalic' => 'kozminbolditalic',
285 'kozmin' => 'kozminproregularacro',
286 'kozgo' => 'kozgopromediumacro',
287  
288 };
289 2 $subs = {
290 # Chinese Traditional (i.e., ROC/Taiwan) Fonts
291 'mingitalic' => {
292 '-alias' => 'adobemingstdlightacro',
293 '+fontname' => ',Italic',
294 },
295 'mingbold' => {
296 '-alias' => 'adobemingstdlightacro',
297 '+fontname' => ',Bold',
298 },
299 'mingbolditalic' => {
300 '-alias' => 'adobemingstdlightacro',
301 '+fontname' => ',BoldItalic',
302 },
303 # Chinese Simplified (i.e., PRC/Mainland China) Fonts
304 'songitalic' => {
305 '-alias' => 'adobesongstdlightacro',
306 '+fontname' => ',Italic',
307 },
308 'songbold' => {
309 '-alias' => 'adobesongstdlightacro',
310 '+fontname' => ',Bold',
311 },
312 'songbolditalic' => {
313 '-alias' => 'adobesongstdlightacro',
314 '+fontname' => ',BoldItalic',
315 },
316 # Japanese Gothic (i.e., sans serif) Fonts
317 'kozgoitalic' => {
318 '-alias' => 'kozgopromediumacro',
319 '+fontname' => ',Italic',
320 },
321 'kozgobold' => {
322 '-alias' => 'kozgopromediumacro',
323 '+fontname' => ',Bold',
324 },
325 'kozgobolditalic' => {
326 '-alias' => 'kozgopromediumacro',
327 '+fontname' => ',BoldItalic',
328 },
329 # Japanese Mincho (i.e., serif) Fonts
330 'kozminitalic' => {
331 '-alias' => 'kozminproregularacro',
332 '+fontname' => ',Italic',
333 },
334 'kozminbold' => {
335 '-alias' => 'kozminproregularacro',
336 '+fontname' => ',Bold',
337 },
338 'kozminbolditalic' => {
339 '-alias' => 'kozminproregularacro',
340 '+fontname' => ',BoldItalic',
341 },
342 # Korean Fonts
343 'myungjoitalic' => {
344 '-alias' => 'adobemyungjostdmediumacro',
345 '+fontname' => ',Italic',
346 },
347 'myungjobold' => {
348 '-alias' => 'adobemyungjostdmediumacro',
349 '+fontname' => ',Bold',
350 },
351 'myungjobolditalic' => {
352 '-alias' => 'adobemyungjostdmediumacro',
353 '+fontname' => ',BoldItalic',
354 },
355 };
356  
357 }
358  
359 2 1;
« Builder/Resource/XObject/Form/BarCode.pm ↑ Summary Builder/NamedDestination.pm »