-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistfonts.pl
executable file
·194 lines (160 loc) · 4.65 KB
/
listfonts.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
#!/usr/bin/perl -w
package main;
use warnings;
use strict;
use CAM::PDF;
use Getopt::Long;
use Pod::Usage;
our $VERSION = '1.60';
my %opts = (
sort => 0,
verbose => 0,
help => 0,
version => 0,
);
Getopt::Long::Configure('bundling');
GetOptions('s|sort' => \$opts{sort},
'v|verbose' => \$opts{verbose},
'h|help' => \$opts{help},
'V|version' => \$opts{version},
) or pod2usage(1);
if ($opts{help})
{
pod2usage(-exitstatus => 0, -verbose => 2);
}
if ($opts{version})
{
print "CAM::PDF v$CAM::PDF::VERSION\n";
exit 0;
}
if (@ARGV < 1)
{
pod2usage(1);
}
my $infile = shift;
my $doc = CAM::PDF->new($infile) || die "$CAM::PDF::errstr\n";
my %fonts;
for my $p (1 .. $doc->numPages())
{
if (!$opts{sort})
{
print "Page $p:\n";
}
# Retrieve an examine all page properties to find the fonts
foreach my $fontname (sort $doc->getFontNames($p))
{
my $font = $doc->getFont($p, $fontname);
# Collect a list of all fields, so we can list the unhandled ones at the end
my %fields = map {$_ => 1} keys %{$font};
delete $fields{Type}; # delete the fields as we handle them
# Font name, if present
my $name = $fontname;
if ($font->{Name})
{
delete $fields{Name};
my $othername = $doc->getValue($font->{Name});
if ($othername ne $name)
{
$name .= "(aka $othername)";
}
}
my $desc = " Name: $name\n";
# Font subtype (required)
delete $fields{Subtype};
$desc .= ' Type: '.$doc->getValue($font->{Subtype})."\n";
# Base font
if ($font->{BaseFont})
{
delete $fields{BaseFont};
$desc .= ' BaseFont: '.$doc->getValue($font->{BaseFont})."\n";
}
# Font encoding
delete $fields{Encoding};
if ($font->{Encoding})
{
# complex or simple encoding?
if ($font->{Encoding}->{type} eq 'reference') # Complex
{
# Handle encoding here. If it's not an encoding, no big deal
$desc .= " Encoding:\n";
my $ref = $doc->getValue($font->{Encoding});
my %efields = map {$_ => 1} keys %{$ref};
delete $efields{Type};
if ($ref->{BaseEncoding})
{
delete $efields{BaseEncoding};
$desc .= ' BaseEncoding: '.$doc->getValue($ref->{BaseEncoding})."\n";
}
if ($ref->{Differences})
{
delete $efields{Differences};
my @diffs = @{$doc->getValue($ref->{Differences})};
my @chars = grep {$_->{type} eq 'label'} @diffs;
$desc .= ' Differences: ' . @chars . "\n";
}
my @others = sort keys %efields;
if (@others > 0)
{
my $other = join ', ', @others;
$desc .= " Other fields: $other\n";
}
}
else # Simple encoding
{
$desc .= ' Encoding: '.$doc->getValue($font->{Encoding})."\n";
}
}
# Font widths
delete $fields{Widths};
$desc .= ' Widths: '. ($font->{Widths} ? 'yes' : 'no') . "\n";
if ($font->{Widths})
{
delete $fields{FirstChar};
delete $fields{LastChar};
$desc .= ' Characters: '.$doc->getValue($font->{FirstChar}) . q{-} . $doc->getValue($font->{LastChar}) . "\n";
}
# Embedding info
delete $fields{FontDescriptor};
$desc .= ' Embedded: '. ($font->{FontDescriptor} ? 'yes' : 'no') . "\n";
# Remaining fields
my @others = sort keys %fields;
if (@others > 0)
{
my $other = join ', ', @others;
$desc .= " Other fields: $other\n";
}
# Output, or defer until the end of all PDF pages
if ($opts{sort})
{
$fonts{$fontname} = $desc;
}
else
{
print $desc;
}
}
}
# No-op unless $opts{sort} is set
foreach my $fontname (sort keys %fonts)
{
$fonts{$fontname} =~ s/ ^[ ][ ] //gxms;
print $fonts{$fontname};
}
__END__
=for stopwords listfonts.pl
=head1 NAME
listfonts.pl - Print details of the fonts used in the PDF
=head1 SYNOPSIS
listfonts.pl [options] infile.pdf
Options:
-s --sort sort the fonts by name, not by page
-v --verbose print diagnostic messages
-h --help verbose help message
-V --version print CAM::PDF version
=head1 DESCRIPTION
Outputs to STDOUT all of the fonts in the PDF document.
=head1 SEE ALSO
CAM::PDF
=head1 AUTHOR
See L<CAM::PDF>
=cut