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 * Copyright (C) 2024 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22/**
23 \file poppler-toc.h
24 */
25#include "poppler-toc.h"
26
27#include "poppler-toc-private.h"
28#include "poppler-private.h"
29
30#include "Outline.h"
31
32using namespace poppler;
33
34toc_private::toc_private() { }
35
36toc_private::~toc_private() { }
37
38toc *toc_private::load_from_outline(Outline *outline)
39{
40 if (!outline) {
41 return nullptr;
42 }
43
44 const std::vector<OutlineItem *> *items = outline->getItems();
45 if (!items || items->size() < 1) {
46 return nullptr;
47 }
48
49 toc *newtoc = new toc();
50 newtoc->d->root.d->is_open = true;
51 newtoc->d->root.d->load_children(items);
52
53 return newtoc;
54}
55
56toc_item_private::toc_item_private() : is_open(false) { }
57
58toc_item_private::~toc_item_private()
59{
60 delete_all(c: children);
61}
62
63void toc_item_private::load(const OutlineItem *item)
64{
65 const std::vector<Unicode> &title_unicode = item->getTitle();
66 title = detail::unicode_to_ustring(u: title_unicode.data(), length: title_unicode.size());
67 is_open = item->isOpen();
68}
69
70void toc_item_private::load_children(const std::vector<OutlineItem *> *items)
71{
72 const int num_items = items->size();
73 children.resize(new_size: 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(items: 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
97toc::toc() : d(new toc_private()) { }
98
99/**
100 Destroys the TOC.
101 */
102toc::~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 */
116toc_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
133toc_item::toc_item() : d(new toc_item_private()) { }
134
135/**
136 Destroys the TOC item.
137 */
138toc_item::~toc_item()
139{
140 delete d;
141}
142
143/**
144 \returns the title of the TOC item
145 */
146ustring 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 */
161bool toc_item::is_open() const
162{
163 return d->is_open;
164}
165
166/**
167 \returns the children of the TOC item
168 */
169std::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 */
177toc_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 */
185toc_item::iterator toc_item::children_end() const
186{
187 return d->children.end();
188}
189

source code of poppler/cpp/poppler-toc.cpp