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);
60 error(-1, "Page count in top-level pages object is wrong type (%s)",
64 pagesSize = numPages0 = obj.getInt();
66 pages = (Page **)gmalloc(pagesSize * sizeof(Page *));
67 pageRefs = (Ref *)gmalloc(pagesSize * sizeof(Ref));
68 for (i = 0; i < pagesSize; ++i) {
73 numPages = readPageTree(pagesDict.getDict(), NULL, 0);
74 if (numPages != numPages0) {
75 error(-1, "Page count in top-level pages object is incorrect");
79 // read named destination dictionary
80 catDict.dictLookup("Dests", &dests);
82 // read root of named destination tree
83 if (catDict.dictLookup("Names", &obj)->isDict())
84 obj.dictLookup("Dests", &nameTree);
90 if (catDict.dictLookup("URI", &obj)->isDict()) {
91 if (obj.dictLookup("Base", &obj2)->isString()) {
92 baseURI = obj2.getString()->copy();
98 // get the metadata stream
99 catDict.dictLookup("Metadata", &metadata);
101 // get the structure tree root
102 catDict.dictLookup("StructTreeRoot", &structTreeRoot);
104 // get the outline dictionary
105 catDict.dictLookup("Outlines", &outline);
121 Catalog::~Catalog() {
125 for (i = 0; i < pagesSize; ++i) {
139 structTreeRoot.free();
143 GString *Catalog::readMetadata() {
149 if (!metadata.isStream()) {
152 dict = metadata.streamGetDict();
153 if (!dict->lookup("Subtype", &obj)->isName("XML")) {
154 error(-1, "Unknown Metadata type: '%s'",
155 obj.isName() ? obj.getName() : "???");
159 metadata.streamReset();
160 while ((c = metadata.streamGetChar()) != EOF) {
163 metadata.streamClose();
167 int Catalog::readPageTree(Dict *pagesDict, PageAttrs *attrs, int start) {
171 PageAttrs *attrs1, *attrs2;
175 attrs1 = new PageAttrs(attrs, pagesDict);
176 pagesDict->lookup("Kids", &kids);
177 if (!kids.isArray()) {
178 error(-1, "Kids object (page %d) is wrong type (%s)",
179 start+1, kids.getTypeName());
182 for (i = 0; i < kids.arrayGetLength(); ++i) {
183 kids.arrayGet(i, &kid);
184 if (kid.isDict("Page")) {
185 attrs2 = new PageAttrs(attrs1, kid.getDict());
186 page = new Page(xref, start+1, kid.getDict(), attrs2);
191 if (start >= pagesSize) {
193 pages = (Page **)grealloc(pages, pagesSize * sizeof(Page *));
194 pageRefs = (Ref *)grealloc(pageRefs, pagesSize * sizeof(Ref));
195 for (j = pagesSize - 32; j < pagesSize; ++j) {
197 pageRefs[j].num = -1;
198 pageRefs[j].gen = -1;
202 kids.arrayGetNF(i, &kidRef);
203 if (kidRef.isRef()) {
204 pageRefs[start].num = kidRef.getRefNum();
205 pageRefs[start].gen = kidRef.getRefGen();
209 // This should really be isDict("Pages"), but I've seen at least one
210 // PDF file where the /Type entry is missing.
211 } else if (kid.isDict()) {
212 if ((start = readPageTree(kid.getDict(), attrs1, start))
216 error(-1, "Kid object (page %d) is wrong type (%s)",
217 start+1, kid.getTypeName());
237 int Catalog::findPage(int num, int gen) {
240 for (i = 0; i < numPages; ++i) {
241 if (pageRefs[i].num == num && pageRefs[i].gen == gen)
247 LinkDest *Catalog::findDest(GString *name) {
252 // try named destination dictionary then name tree
254 if (dests.isDict()) {
255 if (!dests.dictLookup(name->getCString(), &obj1)->isNull())
260 if (!found && nameTree.isDict()) {
261 if (!findDestInTree(&nameTree, name, &obj1)->isNull())
269 // construct LinkDest
271 if (obj1.isArray()) {
272 dest = new LinkDest(obj1.getArray());
273 } else if (obj1.isDict()) {
274 if (obj1.dictLookup("D", &obj2)->isArray())
275 dest = new LinkDest(obj2.getArray());
277 error(-1, "Bad named destination value");
280 error(-1, "Bad named destination value");
283 if (dest && !dest->isOk()) {
291 Object *Catalog::findDestInTree(Object *tree, GString *name, Object *obj) {
293 Object kids, kid, limits, low, high;
298 if (tree->dictLookup("Names", &names)->isArray()) {
299 done = found = gFalse;
300 for (i = 0; !done && i < names.arrayGetLength(); i += 2) {
301 if (names.arrayGet(i, &name1)->isString()) {
302 cmp = name->cmp(name1.getString());
304 names.arrayGet(i+1, obj);
307 } else if (cmp < 0) {
320 // root or intermediate node
322 if (tree->dictLookup("Kids", &kids)->isArray()) {
323 for (i = 0; !done && i < kids.arrayGetLength(); ++i) {
324 if (kids.arrayGet(i, &kid)->isDict()) {
325 if (kid.dictLookup("Limits", &limits)->isArray()) {
326 if (limits.arrayGet(0, &low)->isString() &&
327 name->cmp(low.getString()) >= 0) {
328 if (limits.arrayGet(1, &high)->isString() &&
329 name->cmp(high.getString()) <= 0) {
330 findDestInTree(&kid, name, obj);
344 // name was outside of ranges of all kids