1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
26 //------------------------------------------------------------------------
28 //------------------------------------------------------------------------
30 Catalog::Catalog(XRef *xrefA) {
31 Object catDict, pagesDict;
40 numPages = pagesSize = 0;
43 xref->getCatalog(&catDict);
44 if (!catDict.isDict()) {
45 error(-1, "Catalog object is wrong type (%s)", catDict.getTypeName());
50 catDict.dictLookup("Pages", &pagesDict);
51 // This should really be isDict("Pages"), but I've seen at least one
52 // PDF file where the /Type entry is missing.
53 if (!pagesDict.isDict()) {
54 error(-1, "Top-level pages object is wrong type (%s)",
55 pagesDict.getTypeName());
58 pagesDict.dictLookup("Count", &obj);
59 // some PDF files actually use real numbers here ("/Count 9.0")
61 error(-1, "Page count in top-level pages object is wrong type (%s)",
65 pagesSize = numPages0 = (int)obj.getNum();
67 pages = (Page **)gmallocn(pagesSize, sizeof(Page *));
68 pageRefs = (Ref *)gmallocn(pagesSize, sizeof(Ref));
69 for (i = 0; i < pagesSize; ++i) {
74 numPages = readPageTree(pagesDict.getDict(), NULL, 0);
75 if (numPages != numPages0) {
76 error(-1, "Page count in top-level pages object is incorrect");
80 // read named destination dictionary
81 catDict.dictLookup("Dests", &dests);
83 // read root of named destination tree
84 if (catDict.dictLookup("Names", &obj)->isDict())
85 obj.dictLookup("Dests", &nameTree);
91 if (catDict.dictLookup("URI", &obj)->isDict()) {
92 if (obj.dictLookup("Base", &obj2)->isString()) {
93 baseURI = obj2.getString()->copy();
99 // get the metadata stream
100 catDict.dictLookup("Metadata", &metadata);
102 // get the structure tree root
103 catDict.dictLookup("StructTreeRoot", &structTreeRoot);
105 // get the outline dictionary
106 catDict.dictLookup("Outlines", &outline);
108 // get the AcroForm dictionary
109 catDict.dictLookup("AcroForm", &acroForm);
125 Catalog::~Catalog() {
129 for (i = 0; i < pagesSize; ++i) {
143 structTreeRoot.free();
148 GString *Catalog::readMetadata() {
154 if (!metadata.isStream()) {
157 dict = metadata.streamGetDict();
158 if (!dict->lookup("Subtype", &obj)->isName("XML")) {
159 error(-1, "Unknown Metadata type: '%s'",
160 obj.isName() ? obj.getName() : "???");
164 metadata.streamReset();
165 while ((c = metadata.streamGetChar()) != EOF) {
168 metadata.streamClose();
172 int Catalog::readPageTree(Dict *pagesDict, PageAttrs *attrs, int start) {
176 PageAttrs *attrs1, *attrs2;
180 attrs1 = new PageAttrs(attrs, pagesDict);
181 pagesDict->lookup("Kids", &kids);
182 if (!kids.isArray()) {
183 error(-1, "Kids object (page %d) is wrong type (%s)",
184 start+1, kids.getTypeName());
187 for (i = 0; i < kids.arrayGetLength(); ++i) {
188 kids.arrayGet(i, &kid);
189 if (kid.isDict("Page")) {
190 attrs2 = new PageAttrs(attrs1, kid.getDict());
191 page = new Page(xref, start+1, kid.getDict(), attrs2);
196 if (start >= pagesSize) {
198 pages = (Page **)greallocn(pages, pagesSize, sizeof(Page *));
199 pageRefs = (Ref *)greallocn(pageRefs, pagesSize, sizeof(Ref));
200 for (j = pagesSize - 32; j < pagesSize; ++j) {
202 pageRefs[j].num = -1;
203 pageRefs[j].gen = -1;
207 kids.arrayGetNF(i, &kidRef);
208 if (kidRef.isRef()) {
209 pageRefs[start].num = kidRef.getRefNum();
210 pageRefs[start].gen = kidRef.getRefGen();
214 // This should really be isDict("Pages"), but I've seen at least one
215 // PDF file where the /Type entry is missing.
216 } else if (kid.isDict()) {
217 if ((start = readPageTree(kid.getDict(), attrs1, start))
221 error(-1, "Kid object (page %d) is wrong type (%s)",
222 start+1, kid.getTypeName());
241 int Catalog::findPage(int num, int gen) {
244 for (i = 0; i < numPages; ++i) {
245 if (pageRefs[i].num == num && pageRefs[i].gen == gen)
251 LinkDest *Catalog::findDest(GString *name) {
256 // try named destination dictionary then name tree
258 if (dests.isDict()) {
259 if (!dests.dictLookup(name->getCString(), &obj1)->isNull())
264 if (!found && nameTree.isDict()) {
265 if (!findDestInTree(&nameTree, name, &obj1)->isNull())
273 // construct LinkDest
275 if (obj1.isArray()) {
276 dest = new LinkDest(obj1.getArray());
277 } else if (obj1.isDict()) {
278 if (obj1.dictLookup("D", &obj2)->isArray())
279 dest = new LinkDest(obj2.getArray());
281 error(-1, "Bad named destination value");
284 error(-1, "Bad named destination value");
287 if (dest && !dest->isOk()) {
295 Object *Catalog::findDestInTree(Object *tree, GString *name, Object *obj) {
297 Object kids, kid, limits, low, high;
302 if (tree->dictLookup("Names", &names)->isArray()) {
303 done = found = gFalse;
304 for (i = 0; !done && i < names.arrayGetLength(); i += 2) {
305 if (names.arrayGet(i, &name1)->isString()) {
306 cmp = name->cmp(name1.getString());
308 names.arrayGet(i+1, obj);
311 } else if (cmp < 0) {
324 // root or intermediate node
326 if (tree->dictLookup("Kids", &kids)->isArray()) {
327 for (i = 0; !done && i < kids.arrayGetLength(); ++i) {
328 if (kids.arrayGet(i, &kid)->isDict()) {
329 if (kid.dictLookup("Limits", &limits)->isArray()) {
330 if (limits.arrayGet(0, &low)->isString() &&
331 name->cmp(low.getString()) >= 0) {
332 if (limits.arrayGet(1, &high)->isString() &&
333 name->cmp(high.getString()) <= 0) {
334 findDestInTree(&kid, name, obj);
348 // name was outside of ranges of all kids