summaryrefslogtreecommitdiff
path: root/xpdf/Outline.cc
blob: e87f61e0db997e767decd94cd7609662fe29ce81 (plain)
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
//========================================================================
//
// Outline.cc
//
// Copyright 2002-2013 Glyph & Cog, LLC
//
//========================================================================

#include <aconf.h>

#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif

#include "gmem.h"
#include "GString.h"
#include "GList.h"
#include "Error.h"
#include "Link.h"
#include "TextString.h"
#include "Outline.h"

//------------------------------------------------------------------------

Outline::Outline(Object *outlineObj, XRef *xref) {
  Object first, last;

  items = NULL;
  if (!outlineObj->isDict()) {
    return;
  }
  outlineObj->dictLookupNF("First", &first);
  outlineObj->dictLookupNF("Last", &last);
  if (first.isRef() && last.isRef()) {
    items = OutlineItem::readItemList(&first, &last, NULL, xref);
  }
  first.free();
  last.free();
}

Outline::~Outline() {
  if (items) {
    deleteGList(items, OutlineItem);
  }
}

//------------------------------------------------------------------------

OutlineItem::OutlineItem(Object *itemRefA, Dict *dict,
			 OutlineItem *parentA, XRef *xrefA) {
  Object obj1;

  xref = xrefA;
  title = NULL;
  action = NULL;
  kids = NULL;
  parent = parentA;

  if (dict->lookup("Title", &obj1)->isString()) {
    title = new TextString(obj1.getString());
  }
  obj1.free();

  if (!dict->lookup("Dest", &obj1)->isNull()) {
    action = LinkAction::parseDest(&obj1);
  } else {
    obj1.free();
    if (!dict->lookup("A", &obj1)->isNull()) {
      action = LinkAction::parseAction(&obj1);
    }
  }
  obj1.free();

  itemRefA->copy(&itemRef);
  dict->lookupNF("First", &firstRef);
  dict->lookupNF("Last", &lastRef);
  dict->lookupNF("Next", &nextRef);

  startsOpen = gFalse;
  if (dict->lookup("Count", &obj1)->isInt()) {
    if (obj1.getInt() > 0) {
      startsOpen = gTrue;
    }
  }
  obj1.free();
}

OutlineItem::~OutlineItem() {
  close();
  if (title) {
    delete title;
  }
  if (action) {
    delete action;
  }
  itemRef.free();
  firstRef.free();
  lastRef.free();
  nextRef.free();
}

GList *OutlineItem::readItemList(Object *firstItemRef, Object *lastItemRef,
				 OutlineItem *parentA, XRef *xrefA) {
  GList *items;
  OutlineItem *item, *sibling;
  Object obj;
  Object *p;
  OutlineItem *ancestor;
  int i;

  items = new GList();
  if (!firstItemRef->isRef() || !lastItemRef->isRef()) {
    return items;
  }
  p = firstItemRef;
  do {
    if (!p->fetch(xrefA, &obj)->isDict()) {
      obj.free();
      break;
    }
    item = new OutlineItem(p, obj.getDict(), parentA, xrefA);
    obj.free();

    // check for loops with parents
    for (ancestor = parentA; ancestor; ancestor = ancestor->parent) {
      if (p->getRefNum() == ancestor->itemRef.getRefNum() &&
	  p->getRefGen() == ancestor->itemRef.getRefGen()) {
	error(errSyntaxError, -1, "Loop detected in outline");
	break;
      }
    }
    if (ancestor) {
      delete item;
      break;
    }

    // check for loops with siblings
    for (i = 0; i < items->getLength(); ++i) {
      sibling = (OutlineItem *)items->get(i);
      if (p->getRefNum() == sibling->itemRef.getRefNum() &&
	  p->getRefGen() == sibling->itemRef.getRefGen()) {
	error(errSyntaxError, -1, "Loop detected in outline");
	break;
      }
    }
    if (i < items->getLength()) {
      delete item;
      break;
    }

    items->append(item);
    if (p->getRefNum() == lastItemRef->getRef().num &&
	p->getRefGen() == lastItemRef->getRef().gen) {
      break;
    }
    p = &item->nextRef;
    if (!p->isRef()) {
      break;
    }
  } while (p);
  return items;
}

void OutlineItem::open() {
  if (!kids) {
    kids = readItemList(&firstRef, &lastRef, this, xref);
  }
}

void OutlineItem::close() {
  if (kids) {
    deleteGList(kids, OutlineItem);
    kids = NULL;
  }
}

Unicode *OutlineItem::getTitle() {
  return title ? title->getUnicode() : (Unicode *)NULL;
}

int OutlineItem::getTitleLength() {
  return title ? title->getLength() : 0;
}