Debian Codegraph @

src/ApvlvFile.h

raw
1 /*
2 * This file is part of the apvlv package
3 * Copyright (C) <2008> <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 File.h
23 *
24 * Author: Alf <[email protected]>
25 */
26
27 #ifndef _APVLV_FILE_H_
28 #define _APVLV_FILE_H_
29
30 #include <map>
31 #include <memory>
32 #include <string>
33 #include <vector>
34
35 #include "ApvlvFileIndex.h"
36 #include "ApvlvNote.h"
37 #include "ApvlvParams.h"
38 #include "ApvlvSearch.h"
39
40 namespace apvlv
41 {
42
43 enum class DISPLAY_TYPE
44 {
45 IMAGE,
46 HTML,
47 CUSTOM,
48 };
49
50 //
51 // link to an url, or a page num
52 //
53 struct ApvlvLink
54 {
55 int mPage;
56 };
57
58 struct ApvlvCover
59 {
60 std::string content;
61 std::string mime_type;
62 };
63
64 using ApvlvLinks = std::vector<ApvlvLink>;
65
66 struct ApvlvPoint
67 {
68 double x;
69 double y;
70 };
71
72 struct Size
73 {
74 int width;
75 int height;
76 };
77
78 struct SizeF
79 {
80 double width;
81 double height;
82 };
83
84 /*
85 * position of a search result, or just an area
86 */
87 struct Rectangle
88 {
89 double p1x;
90 double p1y;
91 double p2x;
92 double p2y;
93 };
94
95 using CharRectangle = Rectangle;
96
97 struct WordRectangle
98 {
99 std::string word;
100 std::vector<CharRectangle> rect_list;
101 };
102
103 using WordListRectangle = std::vector<WordRectangle>;
104
105 class WebView;
106 class FileWidget;
107 class File
108 {
109 public:
110 virtual ~File ();
111
112 virtual bool load (const std::string &filename) = 0;
113
114 /* File methods */
115 [[nodiscard]] virtual DISPLAY_TYPE
116 getDisplayType () const
117 {
118 return DISPLAY_TYPE::HTML;
119 }
120
121 virtual FileWidget *
122 getWidget ()
123 {
124 return nullptr;
125 }
126
127 const std::string &
128 getFilename ()
129 {
130 return mFilename;
131 }
132
133 bool
134 setFilename (const std::string &filename)
135 {
136 if (load (filename))
137 {
138 mFilename = filename;
139
140 mNote.load ();
141
142 return true;
143 }
144
145 return false;
146 }
147
148 const ApvlvCover &
149 getCover ()
150 {
151 return mCover;
152 }
153
154 Note *
155 getNote ()
156 {
157 return &mNote;
158 }
159
160 const FileIndex &
161 getIndex ()
162 {
163 return mIndex;
164 }
165
166 virtual std::unique_ptr<SearchFileMatch>
167 grepFile (const std::string &seq, bool is_case, bool is_regex,
168 std::atomic<bool> &is_abort);
169
170 virtual int
171 sum ()
172 {
173 return 1;
174 };
175
176 // Page methods
177 Size
178 pageSize (int page, int rot)
179 {
180 auto sizef = pageSizeF (page, rot);
181 return { static_cast<int> (sizef.width), static_cast<int> (sizef.height) };
182 }
183
184 virtual SizeF
185 pageSizeF (int page, int rot)
186 {
187 return { 0, 0 };
188 }
189
190 virtual int
191 pageNumberWrap (int page)
192 {
193 auto scrdoc = ApvlvParams::instance ()->getBoolOrDefault ("autoscrolldoc");
194 int c = sum ();
195
196 if (page >= 0 && page < c)
197 {
198 return page;
199 }
200 else if (page >= c && scrdoc)
201 {
202 return page % c;
203 }
204 else if (page < 0 && scrdoc)
205 {
206 while (page < 0)
207 page += c;
208 return page;
209 }
210 else
211 {
212 return -1;
213 }
214 }
215
216 virtual bool pageRenderToImage (int pn, double zm, int rot, QImage *img);
217
218 virtual bool pageRenderToWebView (int pn, double zm, int rot,
219 WebView *webview);
220
221 // some ebooks only have image as page
222 virtual bool
223 pageIsOnlyImage (int pn)
224 {
225 return false;
226 }
227
228 virtual std::unique_ptr<ApvlvLinks>
229 pageLinks (int pn)
230 {
231 return nullptr;
232 }
233
234 virtual bool
235 pageText (int pn, const Rectangle &rect, std::string &text)
236 {
237 return false;
238 }
239
240 virtual std::unique_ptr<WordListRectangle>
241 pageSearch (int pn, const char *str)
242 {
243 return nullptr;
244 }
245
246 virtual std::optional<std::vector<Rectangle>>
247 pageHighlight (int pn, const ApvlvPoint &pa, const ApvlvPoint &pb)
248 {
249 return std::nullopt;
250 }
251
252 // path methods
253 virtual std::optional<QByteArray> pathContent (const std::string &path);
254
255 virtual std::string pathMimeType (const std::string &path);
256
257 virtual int pathPageNumber (const std::string &path);
258
259 protected:
260 File () : mNote (this) {}
261
262 std::string mFilename;
263 FileIndex mIndex;
264 std::vector<std::string> mPages;
265 std::map<std::string, int> srcPages;
266 std::map<std::string, std::string> srcMimeTypes;
267 ApvlvCover mCover;
268 Note mNote;
269
270 private:
271 std::optional<QByteArray> pathContentHtml (int, double, int);
272 std::optional<QByteArray> pathContentPng (int, double, int);
273 };
274
275 class FileFactory
276 {
277 public:
278 static int registerClass (const std::string &name,
279 const std::function<File *()> &fun,
280 std::initializer_list<std::string> exts);
281
282 static const std::map<std::string, std::vector<std::string>> &
283 supportMimeTypes ();
284
285 static std::vector<std::string> supportFileExts ();
286
287 static std::ostream &typeEngineDescription (std::ostream &os);
288
289 using ExtClass = std::pair<std::string, std::function<File *()>>;
290 using ExtClassList = std::vector<ExtClass>;
291
292 static std::optional<ExtClass> findMatchClass (const std::string &filename);
293 static std::unique_ptr<File> loadFile (const std::string &filename);
294
295 private:
296 static std::map<std::string, std::vector<std::string>> mSupportMimeTypes;
297 static std::map<std::string, ExtClassList> mSupportClass;
298 };
299
300 #define FILE_TYPE_DECLARATION(cls) \
301 private: \
302 static int class_id_of_##cls
303 #define FILE_TYPE_DEFINITION(name, cls, ...) \
304 int cls::class_id_of_##cls = FileFactory::registerClass ( \
305 name, [] () -> File * { return new cls (); }, __VA_ARGS__)
306
307 };
308
309 #endif
310
311 /* Local Variables: */
312 /* mode: c++ */
313 /* End: */