summaryrefslogtreecommitdiff
blob: 5234e4989fe26ab9944a61c1d92e11849b97cfaa (plain)
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
% Copyright (C) 2001-2020 Artifex Software, Inc.
% All Rights Reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% Refer to licensing information at http://www.artifex.com or contact
% Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
% CA 94945, U.S.A., +1(415)492-9861, for further information.
%
%
% $Id:$
%
% Extract Fonts from a PDF file
%
%	Font files are written with the same name as in the PDF, which
%       may be of the form ABCDEF+fontname if the font is a subset.
%
% example usage:
%
%	gs -q -dNODISPLAY extractFonts.ps -c "(somefile.pdf) extractFonts quit"
%

% Copy one file to another.
% Close the files when finished
/copyfile		% <infile> <outfile> copyfile -
 { 0 mark 32000 string
    { 4 index 1 index readstring
      exch 5 index 1 index writestring
      length 5 -1 roll add 4 1 roll
      not { exit } if
    } loop
   cleartomark 3 -1 roll closefile
   pop closefile
 } bind def

/extractFonts {	%	(filename) extractFonts
  /FontsFound 100 dict def
  (r) file runpdfbegin
  1 1 pdfpagecount % stack: first_page# last_page#
  {
    pdfgetpage			% get pagedict
    /Resources pget {
      /Font knownoget {
        { exch pop oforce
          dup /DescendantFonts knownoget {
            exch pop 0 get oforce
          } if
          dup /FontDescriptor knownoget {
            dup /FontFile known 1 index /FontFile2 known or exch /FontFile3 known or
            {
              % embedded font found!
              dup /BaseFont oget
              FontsFound 1 index known not {
                1 index /Subtype oget
                FontsFound 2 index 2 index put	% mark the font we found
                % data for different types of fonts is handled separately
                % stack: -dict- BaseFont Subtype
                dup /Type1 eq {
                  2 index /FontDescriptor oget
                  dup /FontFile knownoget {
                    true resolvestream
                    2 index 100 string cvs (.pfb) concatstrings
                    (Extracting Type1 font: ) print dup = flush
                    (w) file copyfile
                  } if
                  /FontFile3 knownoget {
                    dup /Subtype oget /Type1C ne {
                      (Warning: Font ) print 2 index =print ( of type: ) print dup
                      /Subtype oget =print ( is being written with extension .cff) = flush
                    } if
                    true resolvestream
                    2 index 100 string cvs (.cff) concatstrings
                    (Extracting Type1C font: ) print dup = flush
                    (w) file copyfile
                  } if
                } if
                dup /TrueType eq {
                  2 index /FontDescriptor oget
                  dup ===
                  /FontFile2 oget true resolvestream
                  2 index 100 string cvs (.ttf) concatstrings
                  (Extracting TrueType font: ) print dup = flush
                  (w) file copyfile
                } if
                pop pop		% done with BaseFont and Subtype
              } if	% end if font not already in FontsFound
              pop  % done with the font resource dictionary
            } {
              pop	%  not embedded
            } ifelse
          } {
            pop		% no FontDescriptor, not embedded
          } ifelse
        } forall	% traverse the Font resource dictionary
      } if
    } if
  } for
} bind def