Debian Codegraph @

src/file/ApvlvPopplerPdf.cc

raw
1 /*
2 * This file is part of the apvlv package
3 * Copyright (C) <2010> <Alf>
4 *
5 * Contact: Alf <[email protected]>
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 of the License, or
10 * (at your option) 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22 /* @CPPFILE ApvlvPdf.cc
23 *
24 * Author: Alf <[email protected]>
25 */
26
27 #include <QInputDialog>
28 #include <QMessageBox>
29 #include <filesystem>
30 #include <fstream>
31 #include <qt6/poppler-qt6.h>
32
33 #include "ApvlvPopplerPdf.h"
34 #include "ApvlvView.h"
35
36 namespace apvlv
37 {
38 FILE_TYPE_DEFINITION ("poppler", ApvlvPopplerPDF, { ".pdf" });
39
40 using namespace std;
41 using namespace Poppler;
42
43 bool
44 ApvlvPopplerPDF::load (const string &filename)
45 {
46 mDoc = Document::load (QString::fromLocal8Bit (filename));
47 if (mDoc == nullptr)
48 {
49 auto text
50 = QInputDialog::getText (nullptr, "password", "input password");
51 auto pass = QByteArray::fromStdString (text.toStdString ());
52 mDoc = Document::load (QString::fromStdString (filename), pass, pass);
53 }
54
55 if (mDoc == nullptr)
56 {
57 return false;
58 }
59
60 generateIndex ();
61 return true;
62 }
63
64 SizeF
65 ApvlvPopplerPDF::pageSizeF (int pn, int rot)
66 {
67 auto page = mDoc->page (pn);
68 auto qsize = page->pageSizeF ();
69 if (rot == 0 || rot == 180)
70 {
71 return { qsize.width (), qsize.height () };
72 }
73 else
74 {
75 return { qsize.height (), qsize.width () };
76 }
77 }
78
79 int
80 ApvlvPopplerPDF::sum ()
81 {
82 return mDoc ? mDoc->numPages () : 0;
83 }
84
85 unique_ptr<WordListRectangle>
86 ApvlvPopplerPDF::pageSearch (int pn, const char *str)
87 {
88 if (mDoc == nullptr)
89 return nullptr;
90
91 auto page = mDoc->page (pn);
92 auto results = page->search (str);
93 if (results.empty ())
94 return nullptr;
95
96 auto poses = make_unique<WordListRectangle> ();
97 for (auto const &res : results)
98 {
99 WordRectangle wr;
100 wr.rect_list.push_back (
101 { res.left (), res.top (), res.right (), res.bottom () });
102 poses->push_back (wr);
103 }
104 return poses;
105 }
106
107 bool
108 ApvlvPopplerPDF::pageIsOnlyImage (int pn)
109 {
110 auto page = mDoc->page (pn);
111 auto list = page->textList ();
112 return list.empty ();
113 }
114
115 bool
116 ApvlvPopplerPDF::pageRenderToImage (int pn, double zm, int rot, QImage *pix)
117 {
118 if (mDoc == nullptr)
119 return false;
120
121 auto xres = 72.0 * zm;
122 auto yres = 72.0 * zm;
123
124 auto prot = Poppler::Page::Rotate0;
125 if (rot == 90)
126 prot = Poppler::Page::Rotate90;
127 if (rot == 180)
128 prot = Poppler::Page::Rotate180;
129 if (rot == 270)
130 prot = Poppler::Page::Rotate270;
131
132 auto page = mDoc->page (pn);
133 auto size = page->pageSizeF ();
134 auto image = page->renderToImage (xres, yres, 0, 0, size.width () * zm,
135 size.height () * zm, prot);
136 *pix = std::move (image);
137 return true;
138 }
139
140 bool
141 ApvlvPopplerPDF::generateIndex ()
142 {
143 auto outlines = mDoc->outline ();
144 if (outlines.empty ())
145 return false;
146
147 mIndex = { "", 0, getFilename (), FileIndexType::FILE };
148 generateChildrenIndex (mIndex, outlines);
149 return true;
150 }
151
152 void
153 ApvlvPopplerPDF::generateChildrenIndex (FileIndex &root_index,
154 const QVector<OutlineItem> &outlines)
155 {
156 for (auto const &outline : outlines)
157 {
158 FileIndex index{ outline.name ().toStdString (),
159 outline.destination ()->pageNumber () - 1, "",
160 FileIndexType::PAGE };
161 auto child_outlines = outline.children ();
162 generateChildrenIndex (index, child_outlines);
163 root_index.mChildrenIndex.emplace_back (index);
164 }
165 }
166
167 }
168
169 // Local Variables:
170 // mode: c++
171 // End: