1 /*
2  * Copyright (C) 2009-2010, Pino Toscano <pino@kde.org>
3  * Copyright (C) 2018, Albert Astals Cid <aacid@kde.org>
4  * Copyright (C) 2019, Oliver Sander <oliver.sander@tu-dresden.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  \file poppler-toc.h
23  */
24 #include "poppler-toc.h"
25 
26 #include "poppler-toc-private.h"
27 #include "poppler-private.h"
28 
29 #include "Outline.h"
30 
31 using namespace poppler;
32 
toc_private()33 toc_private::toc_private() { }
34 
~toc_private()35 toc_private::~toc_private() { }
36 
load_from_outline(Outline * outline)37 toc *toc_private::load_from_outline(Outline *outline)
38 {
39     if (!outline) {
40         return nullptr;
41     }
42 
43     const std::vector<OutlineItem *> *items = outline->getItems();
44     if (!items || items->size() < 1) {
45         return nullptr;
46     }
47 
48     toc *newtoc = new toc();
49     newtoc->d->root.d->is_open = true;
50     newtoc->d->root.d->load_children(items);
51 
52     return newtoc;
53 }
54 
toc_item_private()55 toc_item_private::toc_item_private() : is_open(false) { }
56 
~toc_item_private()57 toc_item_private::~toc_item_private()
58 {
59     delete_all(children);
60 }
61 
load(const OutlineItem * item)62 void toc_item_private::load(const OutlineItem *item)
63 {
64     const Unicode *title_unicode = item->getTitle();
65     const int title_length = item->getTitleLength();
66     title = detail::unicode_to_ustring(title_unicode, title_length);
67     is_open = item->isOpen();
68 }
69 
load_children(const std::vector<OutlineItem * > * items)70 void toc_item_private::load_children(const std::vector<OutlineItem *> *items)
71 {
72     const int num_items = items->size();
73     children.resize(num_items);
74     for (int i = 0; i < num_items; ++i) {
75         OutlineItem *item = (*items)[i];
76 
77         toc_item *new_item = new toc_item();
78         new_item->d->load(item);
79         children[i] = new_item;
80 
81         item->open();
82         const std::vector<OutlineItem *> *item_children = item->getKids();
83         if (item_children) {
84             new_item->d->load_children(item_children);
85         }
86     }
87 }
88 
89 /**
90  \class poppler::toc poppler-toc.h "poppler/cpp/poppler-toc.h"
91 
92  Represents the TOC (Table of Contents) of a PDF %document.
93 
94  The TOC of a PDF %document is represented as a tree of items.
95  */
96 
toc()97 toc::toc() : d(new toc_private()) { }
98 
99 /**
100  Destroys the TOC.
101  */
~toc()102 toc::~toc()
103 {
104     delete d;
105 }
106 
107 /**
108  Returns the "invisible item" representing the root of the TOC.
109 
110  This item is special, it has no title nor actions, it is open and its children
111  are the effective root items of the TOC. This is provided as a convenience
112  when iterating through the TOC.
113 
114  \returns the root "item"
115  */
root() const116 toc_item *toc::root() const
117 {
118     return &d->root;
119 }
120 
121 /**
122  \class poppler::toc_item poppler-toc.h "poppler/cpp/poppler-toc.h"
123 
124  Represents an item of the TOC (Table of Contents) of a PDF %document.
125  */
126 
127 /**
128  \typedef std::vector<toc_item *>::const_iterator poppler::toc_item::iterator
129 
130  An iterator for the children of a TOC item.
131  */
132 
toc_item()133 toc_item::toc_item() : d(new toc_item_private()) { }
134 
135 /**
136  Destroys the TOC item.
137  */
~toc_item()138 toc_item::~toc_item()
139 {
140     delete d;
141 }
142 
143 /**
144  \returns the title of the TOC item
145  */
title() const146 ustring toc_item::title() const
147 {
148     return d->title;
149 }
150 
151 /**
152  Returns whether the TOC item should be represented as open when showing the
153  TOC.
154 
155  This is not a functional behaviour, but a visualisation hint of the item.
156  Regardless of this state, the item can be expanded and collapsed freely when
157  represented in a TOC view of a PDF viewer.
158 
159  \returns whether the TOC item should be open
160  */
is_open() const161 bool toc_item::is_open() const
162 {
163     return d->is_open;
164 }
165 
166 /**
167  \returns the children of the TOC item
168  */
children() const169 std::vector<toc_item *> toc_item::children() const
170 {
171     return d->children;
172 }
173 
174 /**
175  \returns an iterator to the being of the list of children of the TOC item
176  */
children_begin() const177 toc_item::iterator toc_item::children_begin() const
178 {
179     return d->children.begin();
180 }
181 
182 /**
183  \returns an iterator to the end of the list of children of the TOC item
184  */
children_end() const185 toc_item::iterator toc_item::children_end() const
186 {
187     return d->children.end();
188 }
189