API2/Resource/CIDFont/CJKFont.pm

filter: Statementstmt 64.8% 72 / 111 Branchbran 31.2% 10 / 32 Conditioncond 18.1% 4 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 75.0% 9 / 12 Podpod 50.0% 2 / 4 totaltotal 50.2% 97 / 193 SCAR 46.7
CC
27
Cov
52%
outobjdeep
110.0
_look_for_font
21.4
new
9.8
CRAP 107.0
« API2/Resource/ColorSpace/Separation.pm ↑ Summary API2/Outline.pm »
1 package PDF::API2::Resource::CIDFont::CJKFont;
2  
3 2 ▶use base 'PDF::API2::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 = '2.044'; # VERSION
9  
10 2 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $fonts = {};
14 2 our $cmap = {};
15 2 our $alias;
16 2 our $subs;
17  
18 =encoding UTF-8
19  
20 =head1 NAME
21  
22 PDF::API2::Resource::CIDFont::CJKFont - Deprecated base class for CJK fonts
23  
24 =head1 DESCRIPTION
25  
26 This is not the CJK font support you are looking for. It dates back to the days
27 when Unicode was young and poorly supported. PDFs created using this class are
28 not portable.
29  
30 Instead, use a regular TrueType or OpenType font that includes Unicode support
31 and create your PDF normally:
32  
33 use PDF::API2;
34 use utf8;
35  
36 my $pdf = PDF::API2->new();
37 my $font = $pdf->font('/path/to/font.ttf');
38 my $page = $pdf->page();
39 my $content = $page->text();
40 $content->font($font, 24);
41  
42 # Chinese
43 $content->translate(72, 72 * 9);
44 $content->text('你好');
45  
46 # Japanese
47 $content->distance(0, -72);
48 $content->text('こんにちは');
49  
50 # Korean
51 $content->distance(0, -72);
52 $content->text('안녕하세요');
53  
54 $pdf->save('hello.pdf');
55  
56 Note: The maintainer is not familiar with CJK languages and has deprecated this
57 class based on his current understanding of Unicode and from reading many bug
58 reports. If you are successfully using the CJK support from this class and
59 think it should not be deprecated, please contact him to discuss.
60  
61 =head1 DEPRECATED METHODS
62  
63 =over
64  
65 =item $font = $class->new($pdf, $cjk_font_name, %options)
66  
67 Returns a CJK font object. The requested font will not be embedded in the PDF,
68 so it will only be readable on computers that have the font installed.
69  
70 Available fonts:
71  
72 =over
73  
74 =item Chinese (Traditional)
75  
76 Ming, Ming-Bold, Ming-Italic, and Ming-BoldItalic
77  
78 =item Chinese (Simplified)
79  
80 Song, Song-Bold, Song-Italic, and Song-BoldItalic
81  
82 =item Korean
83  
84 MyungJo, MyungJo-Bold, MyungJo-Italic, and MyungJo-BoldItalic
85  
86 =item Japanese (Mincho Serif)
87  
88 KozMin, KozMin-Bold, KozMin-Italic, and KozMin-BoldItalic
89  
90 =item Japanese (Gothic Sans Serif)
91  
92 KozGo, KozGo-Bold, KozGo-Italic, KozGo-BoldItalic
93  
94 =back
95  
96 If the text isn't UTF-8, include an C<-encode> option with the encoding to be
97 used.
98  
99 =cut
100  
101 sub _look_for_font {
102 1 ▶ my $fname = lc(shift);
Subroutine _look_for_font: called CC=8 SCAR=30.6
103 1 $fname =~ s/[^a-z0-9]+//gi;
104 1 ▶ $fname = $alias->{$fname} if defined $alias->{$fname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::API2::Resource::CIDFont::CJKFont::alias->{$fname} 1 0 1
105 1 ▶ return { %{$fonts->{$fname}} } if defined $fonts->{$fname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::API2::Resource::CIDFont::CJKFont::fonts->{$fname} 0 1 1
106  
107 1 ▶ if (defined $subs->{$fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PDF::API2::Resource::CIDFont::CJKFont::subs->{$fname}) 0 1 1
108 0 my $data = _look_for_font($subs->{$fname}->{'-alias'});
109 0 foreach my $k (keys %{$subs->{$fname}}) {
110 0 ▶ next if $k =~ /^\-/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^\-/ 0 0 0
111 0 ▶ if (substr($k, 0, 1) eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($k, 0, 1) eq '+') { } 0 0 0
112 0 $data->{substr($k, 1)} .= $subs->{$fname}->{$k};
113 }
114 else {
115 0 $data->{$k} = $subs->{$fname}->{$k};
116 }
117 }
118 0 $fonts->{$fname} = $data;
119 0 return { %$data };
120 }
121  
122 1 eval "require 'PDF/API2/Resource/CIDFont/CJKFont/$fname.data'";
123 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
124 1 return { %{$fonts->{$fname}} };
125 }
126 else {
127 0 die "requested font '$fname' not installed";
128 }
129 }
130  
131 sub _look_for_cmap {
132 1 ▶ my $map = shift();
Subroutine _look_for_cmap: called CC=3 SCAR=11.7
133 1 my $filename = lc($map);
134 1 $filename =~ s/[^a-z0-9]+//gi;
135 1 ▶ return { %{$cmap->{$filename}} } if defined $cmap->{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::API2::Resource::CIDFont::CJKFont::cmap->{$filename} 0 1 1
136 1 eval "require 'PDF/API2/Resource/CIDFont/CMap/$filename.cmap'";
137 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
138 1 return { %{$cmap->{$filename}} };
139 }
140 else {
141 0 die "requested cmap '$map' not installed";
142 }
143 }
144  
145 sub new {
146 1 ▶ my ($class, $pdf, $name, %opts) = @_;
Subroutine new: called CC=9 SCAR=22.8
Pod documented
147 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
148  
149 1 my $data = _look_for_font($name);
150  
151 1 my $cmap = _look_for_cmap($data->{'cmap'});
152  
153 1 $data->{'u2g'} = { %{$cmap->{'u2g'}} };
154 1 $data->{'g2u'} = [ @{$cmap->{'g2u'}} ];
155  
156 1 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
157 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
158 1 my $self = $class->SUPER::new($pdf, $key);
159 1 ▶ $pdf->new_obj($self) if defined($pdf) and not $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
160  
161 1 $self->{' data'} = $data;
162  
163 1 ▶ if (defined $opts{'-encode'} and $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
164 0 $self->data->{'encode'} = $opts{'-encode'};
165 }
166  
167 1 my $emap = {
168 'reg' => 'Adobe',
169 'ord' => 'Identity',
170 'sup' => 0,
171 'map' => 'Identity',
172 'dir' => 'H',
173 'dec' => 'ident',
174 };
175  
176 1 ▶ if (defined $cmap->{'ccs'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmap->{"ccs"}) 1 0 1
177 1 $emap->{'reg'} = $cmap->{'ccs'}->[0];
178 1 $emap->{'ord'} = $cmap->{'ccs'}->[1];
179 1 $emap->{'sup'} = $cmap->{'ccs'}->[2];
180 }
181  
182 # if (defined $cmap->{'cmap'} and defined $cmap->{'cmap'}->{$opts{'-encode'}} ) {
183 # $emap->{'dec'} = $cmap->{'cmap'}->{$opts{'-encode'}}->[0];
184 # $emap->{'map'} = $cmap->{'cmap'}->{$opts{'-encode'}}->[1];
185 # }
186 # elsif (defined $cmap->{'cmap'} and defined $cmap->{'cmap'}->{'utf8'} ) {
187 # $emap->{'dec'} = $cmap->{'cmap'}->{'utf8'}->[0];
188 # $emap->{'map'} = $cmap->{'cmap'}->{'utf8'}->[1];
189 # }
190  
191 1 $self->data->{'decode'} = $emap->{'dec'};
192  
193 $self->{'BaseFont'} = PDFName(join('-',
194 $self->fontname(),
195 $emap->{'map'},
196 1 $emap->{'dir'}));
197 1 $self->{'Encoding'} = PDFName($emap->{'map'} . '-' . $emap->{'dir'});
198  
199 1 my $des = $self->descrByData();
200 1 my $de = $self->{' de'};
201  
202 1 $de->{'FontDescriptor'} = $des;
203 1 $de->{'Subtype'} = PDFName('CIDFontType0');
204 1 $de->{'BaseFont'} = PDFName($self->fontname());
205 1 $de->{'DW'} = PDFNum($self->missingwidth());
206 1 $de->{'CIDSystemInfo'}->{'Registry'} = PDFStr($emap->{'reg'});
207 1 $de->{'CIDSystemInfo'}->{'Ordering'} = PDFStr($emap->{'ord'});
208 1 $de->{'CIDSystemInfo'}->{'Supplement'} = PDFNum($emap->{'sup'});
209 # $de->{'CIDToGIDMap'} = PDFName($emap->{'map'}); # ttf only
210  
211 1 return $self;
212 }
213  
214 sub tounicodemap {
215 0 ▶ my $self = shift();
Subroutine tounicodemap: not called CC=1 SCAR=6.9
Pod undocumented
216 # noop since pdf knows its char-collection
217 0 return $self;
218 }
219  
220 sub glyphByCId {
221 0 ▶ my ($self, $cid) = @_;
Subroutine glyphByCId: not called CC=1 SCAR=6.9
Pod undocumented
222 0 my $uni = $self->uniByCId($cid);
223 0 return nameByUni($uni);
224 }
225  
226 sub outobjdeep {
227 0 ▶ my ($self, $fh, $pdf) = @_;
Subroutine outobjdeep: not called CC=10 SCAR=47.0
Pod documented
228  
229 0 my $notdefbefore = 1;
230  
231 0 my $wx = PDFArray();
232 0 $self->{' de'}->{'W'} = $wx;
233 0 my $ml;
234  
235 0 foreach my $i (0 .. (scalar @{$self->data->{'g2u'}} - 1)) {
236 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
237 and defined $self->data->{'wx'}->[$i]
238 and $self->data->{'wx'}->[$i] != $self->missingwidth())
239 {
240 0 ▶ if ($notdefbefore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notdefbefore) 0 0 0
241 0 $notdefbefore = 0;
242 0 $ml = PDFArray();
243 0 $wx->add_elements(PDFNum($i), $ml);
244 }
245 0 $ml->add_elements(PDFNum($self->data->{'wx'}->[$i]));
246 }
247 ▶ 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
248 and defined $self->data->{'wx'}->{$i}
249 and $self->data->{'wx'}->{$i} != $self->missingwidth())
250 {
251 0 ▶ if ($notdefbefore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notdefbefore) 0 0 0
252 0 $notdefbefore = 0;
253 0 $ml = PDFArray();
254 0 $wx->add_elements(PDFNum($i), $ml);
255 }
256 0 $ml->add_elements(PDFNum($self->data->{'wx'}->{$i}));
257 }
258 else {
259 0 $notdefbefore = 1;
260 }
261 }
262  
263 0 $self->SUPER::outobjdeep($fh, $pdf);
264 }
265  
266 BEGIN {
267 2 ▶ $alias = {
Subroutine BEGIN: called CC=1 SCAR=0.0
268 'traditional' => 'adobemingstdlightacro',
269 'traditionalbold' => 'mingbold',
270 'traditionalitalic' => 'mingitalic',
271 'traditionalbolditalic' => 'mingbolditalic',
272 'ming' => 'adobemingstdlightacro',
273  
274 'simplified' => 'adobesongstdlightacro',
275 'simplifiedbold' => 'songbold',
276 'simplifieditalic' => 'songitalic',
277 'simplifiedbolditalic' => 'songbolditalic',
278 'song' => 'adobesongstdlightacro',
279  
280 'korean' => 'adobemyungjostdmediumacro',
281 'koreanbold' => 'myungjobold',
282 'koreanitalic' => 'myungjoitalic',
283 'koreanbolditalic' => 'myungjobolditalic',
284 'myungjo' => 'adobemyungjostdmediumacro',
285  
286 'japanese' => 'kozminproregularacro',
287 'japanesebold' => 'kozminbold',
288 'japaneseitalic' => 'kozminitalic',
289 'japanesebolditalic' => 'kozminbolditalic',
290 'kozmin' => 'kozminproregularacro',
291 'kozgo' => 'kozgopromediumacro',
292 };
293  
294 2 $subs = {
295 # Chinese Traditional (Taiwan) Fonts
296 'mingitalic' => {
297 '-alias' => 'adobemingstdlightacro',
298 '+fontname' => ',Italic',
299 },
300 'mingbold' => {
301 '-alias' => 'adobemingstdlightacro',
302 '+fontname' => ',Bold',
303 },
304 'mingbolditalic' => {
305 '-alias' => 'adobemingstdlightacro',
306 '+fontname' => ',BoldItalic',
307 },
308  
309 # Chinese Simplified (Mainland China) Fonts
310 'songitalic' => {
311 '-alias' => 'adobesongstdlightacro',
312 '+fontname' => ',Italic',
313 },
314 'songbold' => {
315 '-alias' => 'adobesongstdlightacro',
316 '+fontname' => ',Bold',
317 },
318 'songbolditalic' => {
319 '-alias' => 'adobesongstdlightacro',
320 '+fontname' => ',BoldItalic',
321 },
322  
323 # Japanese Gothic (sans serif) Fonts
324 'kozgoitalic' => {
325 '-alias' => 'kozgopromediumacro',
326 '+fontname' => ',Italic',
327 },
328 'kozgobold' => {
329 '-alias' => 'kozgopromediumacro',
330 '+fontname' => ',Bold',
331 },
332 'kozgobolditalic' => {
333 '-alias' => 'kozgopromediumacro',
334 '+fontname' => ',BoldItalic',
335 },
336  
337 # Japanese Mincho (serif) Fonts
338 'kozminitalic' => {
339 '-alias' => 'kozminproregularacro',
340 '+fontname' => ',Italic',
341 },
342 'kozminbold' => {
343 '-alias' => 'kozminproregularacro',
344 '+fontname' => ',Bold',
345 },
346 'kozminbolditalic' => {
347 '-alias' => 'kozminproregularacro',
348 '+fontname' => ',BoldItalic',
349 },
350  
351 # Korean Fonts
352 'myungjoitalic' => {
353 '-alias' => 'adobemyungjostdmediumacro',
354 '+fontname' => ',Italic',
355 },
356 'myungjobold' => {
357 '-alias' => 'adobemyungjostdmediumacro',
358 '+fontname' => ',Bold',
359 },
360 'myungjobolditalic' => {
361 '-alias' => 'adobemyungjostdmediumacro',
362 '+fontname' => ',BoldItalic',
363 },
364 };
365 }
366  
367 =back
368  
369 =cut
370  
371 2 1;
« API2/Resource/ColorSpace/Separation.pm ↑ Summary API2/Outline.pm »