-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdffonts.cpp
More file actions
203 lines (178 loc) · 4.48 KB
/
Copy pathpdffonts.cpp
File metadata and controls
203 lines (178 loc) · 4.48 KB
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
#include "galapdf.h"
#include "pdfutil.h"
#include <Object.h>
#include <GfxFont.h>
#include <Annot.h>
static void scan_fonts(Dict *resDict, PDFDoc *doc);
static void scan_font(GfxFont *font, PDFDoc *doc);
static Ref* fonts;
static int fontsLen;
static int fontsSize;
// NB: this must match the definition of GfxFontType in GfxFont.h.
static char *fontTypeNames[] =
{
"unknown",
"Type 1",
"Type 1C",
"Type 1C (OT)",
"Type 3",
"TrueType",
"TrueType (OT)",
"CID Type 0",
"CID Type 0C",
"CID Type 0C (OT)",
"CID TrueType",
"CID TrueType (OT)"
};
int print_pdf_fonts(char* filename, char* owner_password, char* user_password)
{
// load config
xpdf_rc xrc;
// open file
xpdf_doc xdoc(filename, owner_password, user_password);
PDFDoc* doc = xdoc.get_doc();
if (!doc->isOk())
return doc->getErrorCode() == errEncrypted ?
gpret_pdf_encrypted :
gpret_cant_open_pdf;
printf("name type emb sub uni object id\n");
printf("------------------------------------ ----------------- --- --- --- ---------\n");
fonts = NULL;
fontsLen = fontsSize = 0;
for (int pg = 1; pg <= doc->getNumPages(); pg++)
{
Page* page = doc->getCatalog()->getPage(pg);
Dict* res_dict = page->getResourceDict();
if (res_dict)
scan_fonts(res_dict, doc);
Object obj1, obj2;
Annots* annots = new Annots(doc->getXRef(), doc->getCatalog(),
page->getAnnots(&obj1));
obj1.free();
for (int i = 0; i < annots->getNumAnnots(); ++i)
{
if (annots->getAnnot(i)->getAppearance(&obj1)->isStream())
{
obj1.streamGetDict()->lookup("Resources", &obj2);
if (obj2.isDict())
scan_fonts(obj2.getDict(), doc);
obj2.free();
}
obj1.free();
}
delete annots;
}
// clean up
gfree(fonts);
return gpret_success;
}
static void scan_fonts(Dict *resDict, PDFDoc *doc)
{
Object obj1, obj2, xObjDict, xObj, resObj;
Ref r;
GfxFontDict *gfxFontDict;
GfxFont *font;
int i;
// scan the fonts in this resource dictionary
gfxFontDict = NULL;
resDict->lookupNF("Font", &obj1);
if (obj1.isRef())
{
obj1.fetch(doc->getXRef(), &obj2);
if (obj2.isDict())
{
r = obj1.getRef();
gfxFontDict = new GfxFontDict(doc->getXRef(), &r, obj2.getDict());
}
obj2.free();
}
else if (obj1.isDict())
{
gfxFontDict = new GfxFontDict(doc->getXRef(), NULL, obj1.getDict());
}
if (gfxFontDict)
{
for (i = 0; i < gfxFontDict->getNumFonts(); ++i)
{
if ((font = gfxFontDict->getFont(i)))
scan_font(font, doc);
}
delete gfxFontDict;
}
obj1.free();
// recursively scan any resource dictionaries in objects in this
// resource dictionary
resDict->lookup("XObject", &xObjDict);
if (xObjDict.isDict())
{
for (i = 0; i < xObjDict.dictGetLength(); ++i)
{
xObjDict.dictGetVal(i, &xObj);
if (xObj.isStream())
{
xObj.streamGetDict()->lookup("Resources", &resObj);
if (resObj.isDict())
scan_fonts(resObj.getDict(), doc);
resObj.free();
}
xObj.free();
}
}
xObjDict.free();
}
static void scan_font(GfxFont *font, PDFDoc *doc)
{
Ref fontRef, embRef;
Object fontObj, toUnicodeObj;
GString *name;
GBool emb, subset, hasToUnicode;
int i;
fontRef = *font->getID();
// check for an already-seen font
for (i = 0; i < fontsLen; ++i)
if (fontRef.num == fonts[i].num && fontRef.gen == fonts[i].gen)
return;
// font name
name = font->getOrigName();
// check for an embedded font
if (font->getType() == fontType3)
emb = gTrue;
else
emb = font->getEmbeddedFontID(&embRef);
// look for a ToUnicode map
hasToUnicode = gFalse;
if (doc->getXRef()->fetch(fontRef.num, fontRef.gen, &fontObj)->isDict())
{
hasToUnicode =
fontObj.dictLookup("ToUnicode", &toUnicodeObj)->isStream();
toUnicodeObj.free();
}
fontObj.free();
// check for a font subset name: capital letters followed by a '+' sign
subset = gFalse;
if (name)
{
for (i = 0; i < name->getLength(); ++i)
if (name->getChar(i) < 'A' || name->getChar(i) > 'Z')
break;
subset = i > 0 && i < name->getLength() && name->getChar(i) == '+';
}
// print the font info
printf("%-36s %-17s %-3s %-3s %-3s",
name ? name->getCString() : "[none]",
fontTypeNames[font->getType()],
emb ? "yes" : "no",
subset ? "yes" : "no",
hasToUnicode ? "yes" : "no");
if (fontRef.gen >= 100000)
printf(" [none]\n");
else
printf(" %6d %2d\n", fontRef.num, fontRef.gen);
// add this font to the list
if (fontsLen == fontsSize)
{
fontsSize += 32;
fonts = (Ref *)greallocn(fonts, fontsSize, sizeof(Ref));
}
fonts[fontsLen++] = *font->getID();
}