filters

Outline.cc

00001 //========================================================================
00002 //
00003 // Outline.cc
00004 //
00005 // Copyright 2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include "gmem.h"
00016 #include "GString.h"
00017 #include "GList.h"
00018 #include "Link.h"
00019 #include "PDFDocEncoding.h"
00020 #include "Outline.h"
00021 
00022 //------------------------------------------------------------------------
00023 
00024 Outline::Outline(Object *outlineObj, XRef *xref) {
00025   Object first;
00026 
00027   items = NULL;
00028   if (!outlineObj->isDict()) {
00029     return;
00030   }
00031   items = OutlineItem::readItemList(outlineObj->dictLookupNF("First", &first),
00032                     xref);
00033   first.free();
00034 }
00035 
00036 Outline::~Outline() {
00037   if (items) {
00038     deleteGList(items, OutlineItem);
00039   }
00040 }
00041 
00042 //------------------------------------------------------------------------
00043 
00044 OutlineItem::OutlineItem(Dict *dict, XRef *xrefA) {
00045   Object obj1;
00046   GString *s;
00047   int i;
00048 
00049   xref = xrefA;
00050   title = NULL;
00051   action = NULL;
00052   kids = NULL;
00053 
00054   if (dict->lookup("Title", &obj1)->isString()) {
00055     s = obj1.getString();
00056     if ((s->getChar(0) & 0xff) == 0xfe &&
00057     (s->getChar(1) & 0xff) == 0xff) {
00058       titleLen = (s->getLength() - 2) / 2;
00059       title = (Unicode *)gmalloc(titleLen * sizeof(Unicode));
00060       for (i = 0; i < titleLen; ++i) {
00061     title[i] = ((s->getChar(2 + 2*i) & 0xff) << 8) |
00062                (s->getChar(3 + 2*i) & 0xff);
00063       }
00064     } else {
00065       titleLen = s->getLength();
00066       title = (Unicode *)gmalloc(titleLen * sizeof(Unicode));
00067       for (i = 0; i < titleLen; ++i) {
00068     title[i] = pdfDocEncoding[s->getChar(i) & 0xff];
00069       }
00070     }
00071   }
00072   obj1.free();
00073 
00074   if (!dict->lookup("Dest", &obj1)->isNull()) {
00075     action = LinkAction::parseDest(&obj1);
00076   } else {
00077     obj1.free();
00078     if (dict->lookup("A", &obj1)) {
00079       action = LinkAction::parseAction(&obj1);
00080     }
00081   }
00082   obj1.free();
00083 
00084   dict->lookupNF("First", &firstRef);
00085   dict->lookupNF("Next", &nextRef);
00086 
00087   startsOpen = gFalse;
00088   if (dict->lookup("Count", &obj1)->isInt()) {
00089     if (obj1.getInt() > 0) {
00090       startsOpen = gTrue;
00091     }
00092   }
00093   obj1.free();
00094 }
00095 
00096 OutlineItem::~OutlineItem() {
00097   close();
00098   if (title) {
00099     delete title;
00100   }
00101   if (action) {
00102     delete action;
00103   }
00104   firstRef.free();
00105   nextRef.free();
00106 }
00107 
00108 GList *OutlineItem::readItemList(Object *itemRef, XRef *xrefA) {
00109   GList *items;
00110   OutlineItem *item;
00111   Object obj;
00112   Object *p;
00113 
00114   items = new GList();
00115   p = itemRef;
00116   while (p->isRef()) {
00117     if (!p->fetch(xrefA, &obj)->isDict()) {
00118       obj.free();
00119       break;
00120     }
00121     item = new OutlineItem(obj.getDict(), xrefA);
00122     obj.free();
00123     items->append(item);
00124     p = &item->nextRef;
00125   }
00126   return items;
00127 }
00128 
00129 void OutlineItem::open() {
00130   if (!kids) {
00131     kids = readItemList(&firstRef, xref);
00132   }
00133 }
00134 
00135 void OutlineItem::close() {
00136   if (kids) {
00137     deleteGList(kids, OutlineItem);
00138     kids = NULL;
00139   }
00140 }
KDE Home | KDE Accessibility Home | Description of Access Keys