Debian Codegraph @

src/poppler-core/destname.cc

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 // This file is part of Extract PDFmark.
2 //
3 // Copyright (C) 2016, 2018 Masamichi Hosoda
4 //
5 // Extract PDFmark is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Extract PDFmark is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Extract PDFmark. If not, see <http://www.gnu.org/licenses/>.
17
18 #include "config.h"
19
20 #include "poppler-core.hh"
21
22 #include <iostream>
23 #include <map>
24 #include <sstream>
25 #include <string>
26 #include <Link.h>
27 #include <PDFDoc.h>
28
29 #ifndef HAVE_POPPLER_CORE_IF
30 #include "destname-private.hh"
31 #endif
32
33 inline std::string goo_to_string (GooString *goo)
34 {
35 return std::string {
36 #ifdef HAS_GOOSTRING_C_STR
37 goo->c_str (),
38 #else
39 goo->getCString (),
40 #endif
41 static_cast<std::string::size_type>(goo->getLength ())};
42 }
43
44 inline void poppler_core::add_destname (GooString *name,
45 LinkDest *link_dest)
46 {
47 std::string str = goo_to_string (name);
48 add_destname (str, link_dest);
49 }
50
51 inline void poppler_core::add_destname (GooString *name)
52 {
53 std::string str = goo_to_string (name);
54 std::unique_ptr<LinkDest> link_dest {doc->findDest (name)};
55 add_destname (str, link_dest.get ());
56 }
57
58 inline void poppler_core::add_destname (const char *name)
59 {
60 GooString goo {name};
61 std::unique_ptr<LinkDest> link_dest {doc->findDest (&goo)};
62 add_destname (name, link_dest.get ());
63 }
64
65 inline void poppler_core::add_destname (const std::string &name,
66 LinkDest *link_dest)
67 {
68 if (link_dest)
69 {
70 name_map.insert (std::make_pair (name, *link_dest));
71 }
72 }
73
74 std::string poppler_core::build_destname (const std::string &name,
75 LinkDest *link_dest)
76 {
77 std::stringstream ss;
78
79 if (link_dest)
80 {
81 int pagenum;
82 if (link_dest->isPageRef ())
83 {
84 Ref page_ref = link_dest->getPageRef ();
85 pagenum = doc->findPage (page_ref.num, page_ref.gen);
86 }
87 else
88 {
89 pagenum = link_dest->getPageNum ();
90 }
91
92 switch (link_dest->getKind ())
93 {
94 case destXYZ:
95 ss
96 << "[ /Dest " << encode_name (name)
97 << " /Page " << pagenum
98 << " /View [/XYZ " << link_dest->getLeft ()
99 << " " << link_dest->getTop ()
100 << " " << link_dest->getZoom ()
101 << "] /DEST pdfmark" << std::endl;
102 break;
103 case destFit:
104 ss
105 << "[ /Dest " << encode_name (name)
106 << " /Page " << pagenum
107 << " /View [/Fit] /DEST pdfmark" << std::endl;
108 break;
109 case destFitH:
110 ss
111 << "[ /Dest " << encode_name (name)
112 << " /Page " << pagenum
113 << " /View [/FitH " << link_dest->getTop ()
114 << "] /DEST pdfmark" << std::endl;
115 break;
116 case destFitV:
117 ss
118 << "[ /Dest " << encode_name (name)
119 << " /Page " << pagenum
120 << " /View [/FitV " << link_dest->getLeft ()
121 << "] /DEST pdfmark" << std::endl;
122 break;
123 case destFitR:
124 ss
125 << "[ /Dest " << encode_name (name)
126 << " /Page " << pagenum
127 << " /View [/FitR " << link_dest->getLeft ()
128 << " " << link_dest->getBottom ()
129 << " " << link_dest->getRight ()
130 << " " << link_dest->getTop ()
131 << "] /DEST pdfmark" << std::endl;
132 break;
133 case destFitB:
134 ss
135 << "[ /Dest " << encode_name (name)
136 << " /Page " << pagenum
137 << " /View [/FitB] /DEST pdfmark" << std::endl;
138 break;
139 case destFitBH:
140 ss
141 << "[ /Dest " << encode_name (name)
142 << " /Page " << pagenum
143 << " /View [/FitBH " << link_dest->getTop ()
144 << "] /DEST pdfmark" << std::endl;
145 break;
146 case destFitBV:
147 ss
148 << "[ /Dest " << encode_name (name)
149 << " /Page " << pagenum
150 << " /View [/FitBV " << link_dest->getLeft ()
151 << "] /DEST pdfmark" << std::endl;
152 break;
153 default:
154 ss << "% link_dest kind is unknown." << std::endl;
155 break;
156 }
157 }
158 else
159 {
160 ss << "% link_dest is null." << std::endl;
161 }
162
163 return ss.str ();
164 }
165
166 std::string poppler_core::build_destnames (void)
167 {
168 std::string str;
169
170 for (auto &elm: name_map)
171 {
172 str += build_destname (elm.first, &elm.second);
173 }
174
175 return str;
176 }
177
178 #ifdef HAVE_POPPLER_CORE_IF
179
180 // Use poppler-core interface
181 std::string poppler_core::destname (void)
182 {
183 Catalog *catalog = doc->getCatalog ();
184 name_map.clear ();
185
186 if (catalog && catalog->isOk ())
187 {
188 int len = catalog->numDestNameTree ();
189 for (int i=0; i<len; ++i)
190 {
191 add_destname (catalog->getDestNameTreeName (i),
192 catalog->getDestNameTreeDest (i));
193 }
194
195 len = catalog->numDests ();
196 for (int i=0; i<len; ++i)
197 {
198 add_destname (catalog->getDestsName (i),
199 catalog->getDestsDest (i));
200 }
201 }
202 else
203 std::cerr << "Catalog is not OK" << std::endl;
204
205 return build_destnames ();
206 }
207
208 #else // HAVE_POPPLER_CORE_IF
209
210 // Use poppler-core private access
211 std::string poppler_core::destname (void)
212 {
213 Catalog *catalog = doc->getCatalog ();
214 name_map.clear ();
215
216 if (catalog && catalog->isOk ())
217 {
218 NameTree *destnametree = get_destnametree (catalog);
219 if (destnametree)
220 {
221 int len = destnametree->numEntries ();
222 for (int i=0; i<len; ++i)
223 {
224 add_destname (destnametree->getName (i));
225 }
226 }
227
228 Object *obj = catalog->getDests ();
229 if (obj && obj->isDict ())
230 {
231 int len = obj->dictGetLength ();
232 for (int i=0; i<len; ++i)
233 {
234 add_destname (obj->dictGetKey (i));
235 }
236 }
237 }
238 else
239 std::cerr << "Catalog is not OK" << std::endl;
240
241 return build_destnames ();
242 }
243
244 #endif // HAVE_POPPLER_CORE_IF