-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdf_fonts_tounicode.pl
executable file
·284 lines (242 loc) · 7.87 KB
/
pdf_fonts_tounicode.pl
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
#!/usr/bin/perl
package main;
use strict;
use warnings FATAL => 'all';
use utf8;
use open qw(:std :utf8); # undeclared streams in UTF-8
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use CAM::PDF;
our $VERSION = '0.1';
use constant mm => 25.4 / 72;
my %opts = (
help => 0,
version => 0,
);
Getopt::Long::Configure('bundling');
GetOptions(
'h|help' => \$opts{help},
'V|version' => \$opts{version},
) or pod2usage(1);
if ($opts{help})
{
pod2usage(-exitstatus => 0, -verbose => 2);
}
if ($opts{version})
{
print "pdf_fonts_tounicode.pl v$VERSION\n";
exit 0;
}
if (@ARGV < 1)
{
pod2usage(1);
}
my $filename = shift;
my $lut_filename = shift || "lut1.pl";
my $outfilename = shift || substr( $filename, 0, -4 )."+.pdf";
sub addPage
{
my $self = shift;
my $pagenum = shift;
my $lastpage = $self->getPage($self->numPages());
# print Data::Dumper->Dump([$lastpage]);
my $objnum = $self->getPageObjnum($pagenum);
my $newobjnum = $self->appendObject($self, $objnum, 0);
my $newdict = $self->getObjValue($newobjnum);
delete $newdict->{Contents};
# set parent from last page
$newdict->{Parent}->{value} = $lastpage->{Parent}->{value};
my $parent = $self->getValue($lastpage->{Parent});
# set fixed page size MediaBox = A4 [0, 0, 595, 842]
# 5 colons with data
$newdict->{Rotate}->{value} = 0;
$newdict->{MediaBox}->{value}[2]->{value} = '650';
$newdict->{MediaBox}->{value}[3]->{value} = '842';
$newdict->{CropBox}->{value}[2]->{value} = '650';
$newdict->{CropBox}->{value}[3]->{value} = '842';
#print Data::Dumper->Dump([$newdict->{MediaBox}->{value}]);
# print Data::Dumper->Dump([$newdict]);
push @{$self->getValue($parent->{Kids})}, CAM::PDF::Node->new('reference', $newobjnum);
while ($parent)
{
$self->{changes}->{$parent->{Count}->{objnum}} = 1;
if ($parent->{Count}->{type} eq 'reference')
{
my $countobj = $self->dereference($parent->{Count}->{value});
$countobj->{value}->{value}++;
$self->{changes}->{$countobj->{objnum}} = 1;
}
else
{
$parent->{Count}->{value}++;
}
$parent = $self->getValue($parent->{Parent});
}
$self->{PageCount}++;
# Caches are now bad for all pages from this one
$self->decachePages($pagenum + 1 .. $self->numPages());
return $self;
}
# Start with a PDF page (new or opened)
my $pdf = CAM::PDF->new($filename) || die "$CAM::PDF::errstr\n";
my $num_pages = $pdf->numPages();
my $helv_font_name = "HELVETICA+H0";
my $lut = <<END;
use strict;
use warnings FATAL => 'all';
use utf8;
# http://www.fileformat.info/info/unicode/index.htm
# http://unicode-table.com/ru/
#
# may by single symbol or hex code <0000>
#
# used symbols:
# COPYRIGHT SIGN <00a9>
# WHITE SMILING FACE <263a>
#
# additional symbols:
# LEFT-POINTING DOUBLE ANGLE QUOTATION MARK <00ab>
# RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK <00bb>
# LEFT SINGLE QUOTATION MARK <2018>
# RIGHT SINGLE QUOTATION MARK <2019>
# LEFT DOUBLE QUOTATION MARK <201c>
# RIGHT DOUBLE QUOTATION MARK <201d>
#
# EN DASH <2013>
# EM DASH <2014>
#
# ONE DOT LEADER <2024>
# TWO DOT LEADER <2025>
# HORIZONTAL ELLIPSIS <2026>
#
# Greek Small Letter Pi <03c0>
#
# may replace all quotation mark with simple ' and "
END
our %out_trans = (
"\n" => 'n',
"\r" => 'r',
"\t" => 't',
"\b" => 'b',
"\f" => 'f',
"\\" => "\\",
'(' => '(',
')' => ')',
);
# обход всех страниц
my %fontsdone;
print "NumPages: $num_pages\n";
for my $pagenum (1 .. $num_pages)
{
foreach my $fontname (sort $pdf->getFontNames( $pagenum ))
{
next if $fontname eq $helv_font_name;
my $ctx = "";
my $font = $pdf->getFont( $pagenum, $fontname );
next if $fontsdone{$font};
my $base_font_name = $pdf->getValue($font->{BaseFont});
print "Font name: $fontname\nBase font: $base_font_name\n";
$fontsdone{$font} = 1;
$lut .= "\# BaseFont: $base_font_name\n";
$lut .= "use vars qw (%$fontname);\n";
$lut .= "%$fontname = (\n";
my $indent = 28;
# copy current page with all fonts
addPage($pdf, $pagenum);
# ToUnicode from embedded font
my $U = $pdf->getValue( $font->{ToUnicode} );
print "ToUnicode not exists\n" unless $U;
my $us = "";
$us = $pdf->decodeOne( CAM::PDF::Node->new( 'dictionary', $U )) if $U;
# print $us;
my %uni = ();
my @L1 = split( "\n", $us );
for (my $i = 0; $i < @L1; $i++)
{
if (index( $L1[$i], "beginbfrange" ) != -1)
{
my $n = (split / /, $L1[$i])[0];
for (my $j = $i + 1; $j <= $i + $n; $j++)
{
# print "$L1[$j]\n";
(my $a, my $b, my $c) = split / /, join('> <', split('><', $L1[$j]));
# print "$a\n";
my $m = 0;
for (my $k = hex substr $a, 1, 2; $k <= hex substr $b, 1, 2; $k++)
{
$uni{hex( substr( $a, 1, 2 ) ) + $m} = hex( substr( $c, 1, 4 ) ) + $m;
$m++
}
}
};
if (index( $L1[$i], "beginbfchar" ) != -1)
{
my $n = (split / /, $L1[$i])[0];
for (my $j = $i + 1; $j <= $i + $n; $j++)
{
(my $a, my $c) = split( " ", $L1[$j] );
$uni{hex( substr( $a, 1, 2 ) )} = hex( substr( $c, 1, 4 ) );
}
}
};
my $page_n = $pdf->numPages();
#print "Page_n: $page_n\n";
#print Data::Dumper->Dump([$font->{ToUnicode}]);
$ctx .= "BT\n1 0 0 1 $indent 800 Tm\n";
my $lc = $pdf->getValue( $font->{LastChar} );
my $fc = $pdf->getValue( $font->{FirstChar} );
$ctx .= "/$helv_font_name 10 Tf\n";
$ctx .= "[ ($fontname $fc $lc) ] TJ\n";
$ctx .= "0 -12 Td\n";
# print font gliph table
for (my $i = $fc, my $no = 0; $i <= $lc; $i++, $no++) {
$ctx .= "/$helv_font_name 10 Tf\n";
my $uc = exists($uni{$i}) ? $uni{$i} : 0;
$ctx .= sprintf(" (%i: <%x> <%04x> ) Tj\n", $i, $i, $uc);
$ctx .= "/$fontname 10 Tf\n";
# problem with ( and ) in the string need slash
my $str = sprintf("%s", chr($i));
$str =~ s/([\n\r\t\b\f\\()])/\\$out_trans{$1}/ogi;
$ctx .= "($str) Tj\n 0 -12 Td\n";
$lut .= sprintf("\t%i => '',\n", $i);
# next column
if ($no > 60){
$indent += 120;
$ctx .= "ET\n";
$ctx .= "BT\n1 0 0 1 $indent 800 Tm\n";
$ctx .= "0 -12 Td\n";
$no = 0;
}
}
$ctx .= "ET\n";
$lut .= ");\n\n";
$pdf->setPageContent($page_n, $ctx);
$pdf->addFont($page_n, "Helvetica", $helv_font_name);
# update resources object
$pdf->{changes}->{$pdf->getPage($page_n)->{Resources}->{value}} = 1;
}
}
# Save the PDF
$pdf->output($outfilename) || die "$CAM::PDF::errstr\n";
open(my $fh, '>:encoding(UTF-8)', $lut_filename)
or die "Could not open file '$lut_filename'";
print $fh $lut;
close($fh);
print "OK\n";
__END__
=head1 NAME
pdf_fonts_tounicode.pl - add to the end of PDF file pages with font gliphs
=head1 SYNOPSIS
pdf_fonts_tounicode.pl [options] infile.pdf [lutfile.pl] [outfile.pdf]
Options:
-h --help verbose help message
-V --version print CAM::PDF version
=head1 DESCRIPTION
Add to the end of file pages with embedded font gliphs. Create file with templates
for the ToUnicode encoding.
Default F<lutfile.pl> - lut1.pl.
Default F<outfile.pdf> - {infile}+.pdf.
=head1 AUTHOR
S.Volkov, [email protected]
=cut