File: CJKFont.pm

package info (click to toggle)
libpdf-api2-perl 2.047-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,228 kB
  • sloc: perl: 42,227; makefile: 11
file content (371 lines) | stat: -rw-r--r-- 10,588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package PDF::API2::Resource::CIDFont::CJKFont;

use base 'PDF::API2::Resource::CIDFont';

use strict;
use warnings;

our $VERSION = '2.047'; # VERSION

use PDF::API2::Util;
use PDF::API2::Basic::PDF::Utils;

our $fonts = {};
our $cmap  = {};
our $alias;
our $subs;

=encoding UTF-8

=head1 NAME

PDF::API2::Resource::CIDFont::CJKFont - Deprecated base class for CJK fonts

=head1 DESCRIPTION

This is not the CJK font support you are looking for.  It dates back to the days
when Unicode was young and poorly supported.  PDFs created using this class are
not portable.

Instead, use a regular TrueType or OpenType font that includes Unicode support
and create your PDF normally:

    use PDF::API2;
    use utf8;

    my $pdf = PDF::API2->new();
    my $font = $pdf->font('/path/to/font.ttf');
    my $page = $pdf->page();
    my $content = $page->text();
    $content->font($font, 24);

    # Chinese
    $content->translate(72, 72 * 9);
    $content->text('你好');

    # Japanese
    $content->distance(0, -72);
    $content->text('こんにちは');

    # Korean
    $content->distance(0, -72);
    $content->text('안녕하세요');

    $pdf->save('hello.pdf');

Note: The maintainer is not familiar with CJK languages and has deprecated this
class based on his current understanding of Unicode and from reading many bug
reports.  If you are successfully using the CJK support from this class and
think it should not be deprecated, please contact him to discuss.

=head1 DEPRECATED METHODS

=over

=item $font = $class->new($pdf, $cjk_font_name, %options)

Returns a CJK font object.  The requested font will not be embedded in the PDF,
so it will only be readable on computers that have the font installed.

Available fonts:

=over

=item Chinese (Traditional)

Ming, Ming-Bold, Ming-Italic, and Ming-BoldItalic

=item Chinese (Simplified)

Song, Song-Bold, Song-Italic, and Song-BoldItalic

=item Korean

MyungJo, MyungJo-Bold, MyungJo-Italic, and MyungJo-BoldItalic

=item Japanese (Mincho Serif)

KozMin, KozMin-Bold, KozMin-Italic, and KozMin-BoldItalic

=item Japanese (Gothic Sans Serif)

KozGo, KozGo-Bold, KozGo-Italic, KozGo-BoldItalic

=back

If the text isn't UTF-8, include an C<-encode> option with the encoding to be
used.

=cut

sub _look_for_font {
    my $fname = lc(shift);
    $fname =~ s/[^a-z0-9]+//gi;
    $fname = $alias->{$fname} if defined $alias->{$fname};
    return { %{$fonts->{$fname}} } if defined $fonts->{$fname};

    if (defined $subs->{$fname}) {
        my $data = _look_for_font($subs->{$fname}->{'-alias'});
        foreach my $k (keys %{$subs->{$fname}}) {
            next if $k =~ /^\-/;
            if (substr($k, 0, 1) eq '+') {
                $data->{substr($k, 1)} .= $subs->{$fname}->{$k};
            }
            else {
                $data->{$k} = $subs->{$fname}->{$k};
            }
        }
        $fonts->{$fname} = $data;
        return { %$data };
    }

    eval "require 'PDF/API2/Resource/CIDFont/CJKFont/$fname.data'";
    unless ($@) {
        return { %{$fonts->{$fname}} };
    }
    else {
        die "requested font '$fname' not installed";
    }
}

sub _look_for_cmap {
    my $map = shift();
    my $filename = lc($map);
    $filename =~ s/[^a-z0-9]+//gi;
    return { %{$cmap->{$filename}} } if defined $cmap->{$filename};
    eval "require 'PDF/API2/Resource/CIDFont/CMap/$filename.cmap'";
    unless ($@) {
        return { %{$cmap->{$filename}} };
    }
    else {
        die "requested cmap '$map' not installed";
    }
}

sub new {
    my ($class, $pdf, $name, %opts) = @_;
    $opts{'-encode'} //= 'ident';

    my $data = _look_for_font($name);

    my $cmap = _look_for_cmap($data->{'cmap'});

    $data->{'u2g'} = { %{$cmap->{'u2g'}} };
    $data->{'g2u'} = [ @{$cmap->{'g2u'}} ];

    $class = ref($class) if ref($class);
    my $key = ($data->{'apiname'} // '') . pdfkey();
    my $self = $class->SUPER::new($pdf, $key);
    $pdf->new_obj($self) if defined($pdf) and not $self->is_obj($pdf);

    $self->{' data'} = $data;

    if (defined $opts{'-encode'} and $opts{'-encode'} ne 'ident') {
        $self->data->{'encode'} = $opts{'-encode'};
    }

    my $emap = {
        'reg' => 'Adobe',
        'ord' => 'Identity',
        'sup' =>  0,
        'map' => 'Identity',
        'dir' => 'H',
        'dec' => 'ident',
    };

    if (defined $cmap->{'ccs'}) {
        $emap->{'reg'} = $cmap->{'ccs'}->[0];
        $emap->{'ord'} = $cmap->{'ccs'}->[1];
        $emap->{'sup'} = $cmap->{'ccs'}->[2];
    }

    # if (defined $cmap->{'cmap'} and defined $cmap->{'cmap'}->{$opts{'-encode'}} ) {
    #     $emap->{'dec'} = $cmap->{'cmap'}->{$opts{'-encode'}}->[0];
    #     $emap->{'map'} = $cmap->{'cmap'}->{$opts{'-encode'}}->[1];
    # }
    # elsif (defined $cmap->{'cmap'} and defined $cmap->{'cmap'}->{'utf8'} ) {
    #     $emap->{'dec'} = $cmap->{'cmap'}->{'utf8'}->[0];
    #     $emap->{'map'} = $cmap->{'cmap'}->{'utf8'}->[1];
    # }

    $self->data->{'decode'} = $emap->{'dec'};

    $self->{'BaseFont'} = PDFName(join('-',
                                       $self->fontname(),
                                       $emap->{'map'},
                                       $emap->{'dir'}));
    $self->{'Encoding'} = PDFName($emap->{'map'} . '-' . $emap->{'dir'});

    my $des = $self->descrByData();
    my $de = $self->{' de'};

    $de->{'FontDescriptor'} = $des;
    $de->{'Subtype'} = PDFName('CIDFontType0');
    $de->{'BaseFont'} = PDFName($self->fontname());
    $de->{'DW'} = PDFNum($self->missingwidth());
    $de->{'CIDSystemInfo'}->{'Registry'} = PDFStr($emap->{'reg'});
    $de->{'CIDSystemInfo'}->{'Ordering'} = PDFStr($emap->{'ord'});
    $de->{'CIDSystemInfo'}->{'Supplement'} = PDFNum($emap->{'sup'});
    # $de->{'CIDToGIDMap'} = PDFName($emap->{'map'}); # ttf only

    return $self;
}

sub tounicodemap {
    my $self = shift();
    # noop since pdf knows its char-collection
    return $self;
}

sub glyphByCId {
    my ($self, $cid) = @_;
    my $uni = $self->uniByCId($cid);
    return nameByUni($uni);
}

sub outobjdeep {
    my ($self, $fh, $pdf) = @_;

    my $notdefbefore = 1;

    my $wx = PDFArray();
    $self->{' de'}->{'W'} = $wx;
    my $ml;

    foreach my $i (0 .. (scalar @{$self->data->{'g2u'}} - 1)) {
        if (ref($self->data->{'wx'}) eq 'ARRAY'
            and defined $self->data->{'wx'}->[$i]
            and $self->data->{'wx'}->[$i] != $self->missingwidth())
        {
            if ($notdefbefore) {
                $notdefbefore = 0;
                $ml = PDFArray();
                $wx->add_elements(PDFNum($i), $ml);
            }
            $ml->add_elements(PDFNum($self->data->{'wx'}->[$i]));
        }
        elsif (ref($self->data->{'wx'}) eq 'HASH'
               and defined $self->data->{'wx'}->{$i}
               and $self->data->{'wx'}->{$i} != $self->missingwidth())
        {
            if ($notdefbefore) {
                $notdefbefore = 0;
                $ml = PDFArray();
                $wx->add_elements(PDFNum($i), $ml);
            }
            $ml->add_elements(PDFNum($self->data->{'wx'}->{$i}));
        }
        else {
            $notdefbefore = 1;
        }
    }

    $self->SUPER::outobjdeep($fh, $pdf);
}

BEGIN {
    $alias = {
        'traditional'           => 'adobemingstdlightacro',
        'traditionalbold'       => 'mingbold',
        'traditionalitalic'     => 'mingitalic',
        'traditionalbolditalic' => 'mingbolditalic',
        'ming'                  => 'adobemingstdlightacro',

        'simplified'            => 'adobesongstdlightacro',
        'simplifiedbold'        => 'songbold',
        'simplifieditalic'      => 'songitalic',
        'simplifiedbolditalic'  => 'songbolditalic',
        'song'                  => 'adobesongstdlightacro',

        'korean'                => 'adobemyungjostdmediumacro',
        'koreanbold'            => 'myungjobold',
        'koreanitalic'          => 'myungjoitalic',
        'koreanbolditalic'      => 'myungjobolditalic',
        'myungjo'               => 'adobemyungjostdmediumacro',

        'japanese'              => 'kozminproregularacro',
        'japanesebold'          => 'kozminbold',
        'japaneseitalic'        => 'kozminitalic',
        'japanesebolditalic'    => 'kozminbolditalic',
        'kozmin'                => 'kozminproregularacro',
        'kozgo'                 => 'kozgopromediumacro',
    };

    $subs = {
        # Chinese Traditional (Taiwan) Fonts
        'mingitalic' => {
            '-alias'    => 'adobemingstdlightacro',
            '+fontname' => ',Italic',
        },
        'mingbold' => {
            '-alias'    => 'adobemingstdlightacro',
            '+fontname' => ',Bold',
        },
        'mingbolditalic' => {
            '-alias'    => 'adobemingstdlightacro',
            '+fontname' => ',BoldItalic',
        },

        # Chinese Simplified (Mainland China) Fonts
        'songitalic' => {
            '-alias'    => 'adobesongstdlightacro',
            '+fontname' => ',Italic',
        },
        'songbold' => {
            '-alias'    => 'adobesongstdlightacro',
            '+fontname' => ',Bold',
        },
        'songbolditalic' => {
            '-alias'    => 'adobesongstdlightacro',
            '+fontname' => ',BoldItalic',
        },

        # Japanese Gothic (sans serif) Fonts
        'kozgoitalic' => {
            '-alias'    => 'kozgopromediumacro',
            '+fontname' => ',Italic',
        },
        'kozgobold' => {
            '-alias'    => 'kozgopromediumacro',
            '+fontname' => ',Bold',
        },
        'kozgobolditalic' => {
            '-alias'    => 'kozgopromediumacro',
            '+fontname' => ',BoldItalic',
        },

        # Japanese Mincho (serif) Fonts
        'kozminitalic' => {
            '-alias'    => 'kozminproregularacro',
            '+fontname' => ',Italic',
        },
        'kozminbold' => {
            '-alias'    => 'kozminproregularacro',
            '+fontname' => ',Bold',
        },
        'kozminbolditalic' => {
            '-alias'    => 'kozminproregularacro',
            '+fontname' => ',BoldItalic',
        },

        # Korean Fonts
        'myungjoitalic' => {
            '-alias'    => 'adobemyungjostdmediumacro',
            '+fontname' => ',Italic',
        },
        'myungjobold' => {
            '-alias'    => 'adobemyungjostdmediumacro',
            '+fontname' => ',Bold',
        },
        'myungjobolditalic' => {
            '-alias'    => 'adobemyungjostdmediumacro',
            '+fontname' => ',BoldItalic',
        },
    };
}

=back

=cut

1;