--- /dev/null
+% Copyright (C) 2009 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 that
+% license. Refer to licensing information at http://www.artifex.com/
+% or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
+% San Rafael, CA 94903, 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
+