File: Font.pm

package info (click to toggle)
libpdf-api2-perl 0.41.00-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 20,308 kB
  • ctags: 1,214
  • sloc: perl: 265,979; makefile: 37
file content (304 lines) | stat: -rw-r--r-- 8,924 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
#=======================================================================
#    ____  ____  _____              _    ____ ___   ____
#   |  _ \|  _ \|  ___|  _   _     / \  |  _ \_ _| |___ \
#   | |_) | | | | |_    (_) (_)   / _ \ | |_) | |    __) |
#   |  __/| |_| |  _|    _   _   / ___ \|  __/| |   / __/
#   |_|   |____/|_|     (_) (_) /_/   \_\_|  |___| |_____|
#
#   A Perl Module Chain to faciliate the Creation and Modification
#   of High-Quality "Portable Document Format (PDF)" Files.
#
#   Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>.
#
#=======================================================================
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the
#   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.
#
#   $Id: Font.pm,v 1.13 2005/03/14 22:01:06 fredo Exp $
#
#=======================================================================
package PDF::API2::Resource::Font;

BEGIN {

    use utf8;
    use Encode qw(:all);

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

    use POSIX;

    use vars qw(@ISA $VERSION);

    @ISA = qw( PDF::API2::Resource::BaseFont );

    ( $VERSION ) = '$Revision: 1.13 $' =~ /Revision: (\S+)\s/; # $Date: 2005/03/14 22:01:06 $

}
no warnings qw[ deprecated recursion uninitialized ];

=head1 NAME

PDF::API2::Resource::Font - Encodes the font in the specified byte-ordering

=head1 SYNOPSIS

 $font->encodeByData $encoding

Encodes the font in the specified byte-encoding.

=cut

sub encodeByData {
    my ($self,$encoding)=@_;
    my $data=undef;

    my ($firstChar,$lastChar);

    if($self->issymbol || $encoding eq 'asis') {
        $encoding=undef;
    }

    if(defined $encoding && $encoding=~m|^uni(\d+)$|o) 
    {
        my $blk=$1;
        $self->data->{e2u}=[ map { $blk*256+$_ } (0..255) ];
        $self->data->{e2n}=[ map { nameByUni($_) || '.notdef' } @{$self->data->{e2u}} ];
    }
    elsif(defined $encoding) 
    {
        $self->data->{e2u}=[ unpack('U*',decode($encoding,pack('C*',(0..255)))) ];
        $self->data->{e2n}=[ map { nameByUni($_) || '.notdef' } @{$self->data->{e2u}} ];
    } 
    elsif(defined $self->data->{uni}) 
    {
        $self->data->{e2u}=[ @{$self->data->{uni}} ];
        $self->data->{e2n}=[ map { $_ || '.notdef' } @{$self->data->{char}} ];
    } 
    else 
    {
        $self->data->{e2u}=[ map { uniByName($_) } @{$self->data->{char}} ];
        $self->data->{e2n}=[ map { $_ || '.notdef' } @{$self->data->{char}} ];
    }

    $self->data->{u2c}={};
    $self->data->{u2e}={};
    $self->data->{u2n}={};
    $self->data->{n2c}={};
    $self->data->{n2e}={};
    $self->data->{n2u}={};

    foreach my $n (0..255) {
        my $xchar=undef;
        my $xuni=undef;
        if(defined $self->data->{char}->[$n]) {
            $xchar=$self->data->{char}->[$n];
        } else {
            $xchar='.notdef';
        }
        $self->data->{n2c}->{$xchar}=$n unless(defined $self->data->{n2c}->{$xchar});

        if(defined $self->data->{e2n}->[$n]) {
            $xchar=$self->data->{e2n}->[$n];
        } else {
            $xchar='.notdef';
        }
        $self->data->{n2e}->{$xchar}=$n unless(defined $self->data->{n2e}->{$xchar});

        $self->data->{n2u}->{$xchar}=$self->data->{e2u}->[$n] unless(defined $self->data->{n2u}->{$xchar});

        if(defined $self->data->{char}->[$n]) {
            $xchar=$self->data->{char}->[$n];
        } else {
            $xchar='.notdef';
        }
        if(defined $self->data->{uni}->[$n]) {
            $xuni=$self->data->{uni}->[$n];
        } else {
            $xuni=0;
        }
        $self->data->{n2u}->{$xchar}=$xuni unless(defined $self->data->{n2u}->{$xchar});

        $self->data->{u2c}->{$xuni}||=$n unless(defined $self->data->{u2c}->{$xuni});
        
        if(defined $self->data->{e2u}->[$n]) {
            $xuni=$self->data->{e2u}->[$n];
        } else {
            $xuni=0;
        }
        $self->data->{u2e}->{$xuni}||=$n unless(defined $self->data->{u2e}->{$xuni});

        if(defined $self->data->{e2n}->[$n]) {
            $xchar=$self->data->{e2n}->[$n];
        } else {
            $xchar='.notdef';
        }
        $self->data->{u2n}->{$xuni}=$xchar unless(defined $self->data->{u2n}->{$xuni});

        if(defined $self->data->{char}->[$n]) {
            $xchar=$self->data->{char}->[$n];
        } else {
            $xchar='.notdef';
        }
        if(defined $self->data->{uni}->[$n]) {
            $xuni=$self->data->{uni}->[$n];
        } else {
            $xuni=0;
        }
        $self->data->{u2n}->{$xuni}=$xchar unless(defined $self->data->{u2n}->{$xuni});
    }

    my $en = PDFDict();
    $self->{Encoding}=$en;

    $en->{Type}=PDFName('Encoding');
    $en->{BaseEncoding}=PDFName('WinAnsiEncoding');

    $en->{Differences}=PDFArray(PDFNum(0));
    foreach my $n (0..255) {
        $en->{Differences}->add_elements( PDFName($self->glyphByEnc($n) || '.notdef') );
    }

    $self->{'FirstChar'} = PDFNum($self->data->{firstchar});
    $self->{'LastChar'} = PDFNum($self->data->{lastchar});

    $self->{Widths}=PDFArray();
    foreach my $n ($self->data->{firstchar}..$self->data->{lastchar}) {
        $self->{Widths}->add_elements( PDFNum($self->wxByEnc($n)) );
    }

#use Data::Dumper;
#    print Dumper($self->data);

    return($self);
}

sub automap {
    my ($self)=@_;

    my %gl=map { $_=>defineName($_) } keys %{$self->data->{wx}};

    foreach my $n (0..255) {
        delete $gl{$self->data->{e2n}->[$n]};
    }

#    my @nm=sort { lc($a) eq lc($b) ? $a cmp $b : lc($a) cmp lc($b) } keys %gl;
    my @nm=sort { $gl{$a}<=>$gl{$b} } keys %gl;
    #splice(@nm,223);
    my @fnts=();
    my $count=0;
    while(@glyphs=splice(@nm,0,223)) {

        my $obj=$self->SUPER::new($self->{' apipdf'},$self->name.'am'.$count);
        $obj->{' data'}={ %{$self->data} };
        $obj->data->{firstchar}=32;
        $obj->data->{lastchar}=32+scalar(@glyphs);
        push @fnts,$obj;
        foreach my $key (qw( Subtype BaseFont FontDescriptor )) {
            $obj->{$key}=$self->{$key} if(defined $self->{$key});
        }
        $obj->data->{char}=[];
        $obj->data->{uni}=[];
        foreach my $n (0..31) {
            $obj->data->{char}->[$n]='.notdef';
            $obj->data->{uni}->[$n]=0;
        }
        $obj->data->{char}->[32]='space';
        $obj->data->{uni}->[32]=32;
        foreach my $n (33..$obj->data->{lastchar}) {
            $obj->data->{char}->[$n]=$glyphs[$n-33];
            $obj->data->{uni}->[$n]=$gl{$glyphs[$n-33]};
        }
        foreach my $n (($obj->data->{lastchar}+1)..255) {
            $obj->data->{char}->[$n]='.notdef';
            $obj->data->{uni}->[$n]=0;
        }
        $obj->encodeByData(undef);

        $count++;
    }

    return(@fnts);
}

sub remap {
    my ($self,$enc)=@_;

    my $obj=$self->SUPER::new($self->{' apipdf'},$self->name.'rm'.pdfkey());
    $obj->{' data'}={ %{$self->data} };
    foreach my $key (qw( Subtype BaseFont FontDescriptor )) {
        $obj->{$key}=$self->{$key} if(defined $self->{$key});
    }

    $obj->encodeByData($enc);

    return($obj);
}

1;

__END__

=head1 AUTHOR

alfred reibenschuh

=head1 HISTORY

    $Log: Font.pm,v $
    Revision 1.13  2005/03/14 22:01:06  fredo
    upd 2005

    Revision 1.12  2004/12/16 00:30:53  fredo
    added no warn for recursion

    Revision 1.11  2004/11/26 01:25:28  fredo
    added unicode block mapping

    Revision 1.10  2004/11/25 23:50:26  fredo
    fixed unicode maps for unmapped corefonts

    Revision 1.9  2004/11/24 20:11:10  fredo
    added virtual font handling

    Revision 1.8  2004/10/17 03:46:20  fredo
    restructured encoding vs. unicode vs. glyph-name lookup

    Revision 1.7  2004/06/15 09:14:41  fredo
    removed cr+lf

    Revision 1.6  2004/06/07 19:44:36  fredo
    cleaned out cr+lf for lf

    Revision 1.5  2004/05/21 15:10:29  fredo
    worked around some unicode probs

    Revision 1.4  2003/12/08 13:05:33  Administrator
    corrected to proper licencing statement

    Revision 1.3  2003/11/30 17:28:55  Administrator
    merged into default

    Revision 1.2.2.1  2003/11/30 16:56:35  Administrator
    merged into default

    Revision 1.2  2003/11/30 11:44:49  Administrator
    added CVS id/log


=cut