% Copyright (C) 2001-2023 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., 39 Mesa Street, Suite 108A, San Francisco, % CA 94129, USA, 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 % 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