/src/poppler/cpp/poppler-toc.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2009-2010, Pino Toscano <pino@kde.org> |
3 | | * Copyright (C) 2018, 2024, 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 | | |
32 | | using namespace poppler; |
33 | | |
34 | 0 | toc_private::toc_private() = default; |
35 | | |
36 | 0 | toc_private::~toc_private() = default; |
37 | | |
38 | | toc *toc_private::load_from_outline(Outline *outline) |
39 | 0 | { |
40 | 0 | if (!outline) { |
41 | 0 | return nullptr; |
42 | 0 | } |
43 | | |
44 | 0 | const std::vector<OutlineItem *> *items = outline->getItems(); |
45 | 0 | if (!items || items->empty()) { |
46 | 0 | return nullptr; |
47 | 0 | } |
48 | | |
49 | 0 | toc *newtoc = new toc(); |
50 | 0 | newtoc->d->root.d->is_open = true; |
51 | 0 | newtoc->d->root.d->load_children(items); |
52 | |
|
53 | 0 | return newtoc; |
54 | 0 | } |
55 | | |
56 | 0 | toc_item_private::toc_item_private() : is_open(false) { } |
57 | | |
58 | | toc_item_private::~toc_item_private() |
59 | 0 | { |
60 | 0 | delete_all(children); |
61 | 0 | } |
62 | | |
63 | | void toc_item_private::load(const OutlineItem *item) |
64 | 0 | { |
65 | 0 | const std::vector<Unicode> &title_unicode = item->getTitle(); |
66 | 0 | title = detail::unicode_to_ustring(title_unicode.data(), title_unicode.size()); |
67 | 0 | is_open = item->isOpen(); |
68 | 0 | } |
69 | | |
70 | | void toc_item_private::load_children(const std::vector<OutlineItem *> *items) |
71 | 0 | { |
72 | 0 | const int num_items = items->size(); |
73 | 0 | children.resize(num_items); |
74 | 0 | for (int i = 0; i < num_items; ++i) { |
75 | 0 | OutlineItem *item = (*items)[i]; |
76 | |
|
77 | 0 | toc_item *new_item = new toc_item(); |
78 | 0 | new_item->d->load(item); |
79 | 0 | children[i] = new_item; |
80 | |
|
81 | 0 | item->open(); |
82 | 0 | const std::vector<OutlineItem *> *item_children = item->getKids(); |
83 | 0 | if (item_children) { |
84 | 0 | new_item->d->load_children(item_children); |
85 | 0 | } |
86 | 0 | } |
87 | 0 | } |
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 | | |
97 | 0 | toc::toc() : d(new toc_private()) { } |
98 | | |
99 | | /** |
100 | | Destroys the TOC. |
101 | | */ |
102 | | toc::~toc() |
103 | 0 | { |
104 | 0 | delete d; |
105 | 0 | } |
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 | | */ |
116 | | toc_item *toc::root() const |
117 | 0 | { |
118 | 0 | return &d->root; |
119 | 0 | } |
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 | | |
133 | 0 | toc_item::toc_item() : d(new toc_item_private()) { } |
134 | | |
135 | | /** |
136 | | Destroys the TOC item. |
137 | | */ |
138 | | toc_item::~toc_item() |
139 | 0 | { |
140 | 0 | delete d; |
141 | 0 | } |
142 | | |
143 | | /** |
144 | | \returns the title of the TOC item |
145 | | */ |
146 | | ustring toc_item::title() const |
147 | 0 | { |
148 | 0 | return d->title; |
149 | 0 | } |
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 | | */ |
161 | | bool toc_item::is_open() const |
162 | 0 | { |
163 | 0 | return d->is_open; |
164 | 0 | } |
165 | | |
166 | | /** |
167 | | \returns the children of the TOC item |
168 | | */ |
169 | | std::vector<toc_item *> toc_item::children() const |
170 | 0 | { |
171 | 0 | return d->children; |
172 | 0 | } |
173 | | |
174 | | /** |
175 | | \returns an iterator to the being of the list of children of the TOC item |
176 | | */ |
177 | | toc_item::iterator toc_item::children_begin() const |
178 | 0 | { |
179 | 0 | return d->children.begin(); |
180 | 0 | } |
181 | | |
182 | | /** |
183 | | \returns an iterator to the end of the list of children of the TOC item |
184 | | */ |
185 | | toc_item::iterator toc_item::children_end() const |
186 | 0 | { |
187 | 0 | return d->children.end(); |
188 | 0 | } |