| 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 <QMessageBox> |
| 28 |
#include <filesystem> |
| 29 |
#include <fstream> |
| 30 |
#include <mupdf/fitz.h> |
| 31 |
|
| 32 |
#include "ApvlvMuPdf.h" |
| 33 |
|
| 34 |
namespace apvlv |
| 35 |
{ |
| 36 |
using namespace std; |
| 37 |
|
| 38 |
FILE_TYPE_DEFINITION ("MuPDF", ApvlvMuPDF, |
| 39 |
{ ".pdf", ".xps", ".epub", ".mobi", ".fb2", ".cbz", |
| 40 |
".svg", ".txt" }); |
| 41 |
|
| 42 |
ApvlvMuPDF::ApvlvMuPDF () : mDoc{ nullptr } |
| 43 |
{ |
| 44 |
mContext = fz_new_context (nullptr, nullptr, FZ_STORE_UNLIMITED); |
| 45 |
fz_register_document_handlers (mContext); |
| 46 |
} |
| 47 |
|
| 48 |
ApvlvMuPDF::~ApvlvMuPDF () |
| 49 |
{ |
| 50 |
fz_drop_document (mContext, mDoc); |
| 51 |
fz_drop_context (mContext); |
| 52 |
} |
| 53 |
|
| 54 |
bool |
| 55 |
ApvlvMuPDF::load (const string &filename) |
| 56 |
{ |
| 57 |
mDoc = fz_open_document (mContext, filename.c_str ()); |
| 58 |
if (mDoc == nullptr) |
| 59 |
{ |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
generateIndex (); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
SizeF |
| 68 |
ApvlvMuPDF::pageSizeF (int pn, int rot) |
| 69 |
{ |
| 70 |
auto page = fz_load_page (mContext, mDoc, pn); |
| 71 |
auto rect = fz_bound_page (mContext, page); |
| 72 |
fz_drop_page (mContext, page); |
| 73 |
SizeF sizef{ rect.x1 - rect.x0, rect.y1 - rect.y0 }; |
| 74 |
return sizef; |
| 75 |
} |
| 76 |
|
| 77 |
int |
| 78 |
ApvlvMuPDF::sum () |
| 79 |
{ |
| 80 |
auto pages = fz_count_pages (mContext, mDoc); |
| 81 |
return pages; |
| 82 |
} |
| 83 |
|
| 84 |
bool |
| 85 |
ApvlvMuPDF::pageIsOnlyImage (int pn) |
| 86 |
{ |
| 87 |
auto text_page |
| 88 |
= fz_new_stext_page_from_page_number (mContext, mDoc, pn, nullptr); |
| 89 |
if (text_page == nullptr) |
| 90 |
{ |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
auto only_image = (text_page->first_block == nullptr); |
| 95 |
|
| 96 |
fz_drop_stext_page (mContext, text_page); |
| 97 |
return only_image; |
| 98 |
} |
| 99 |
|
| 100 |
bool |
| 101 |
ApvlvMuPDF::pageRenderToImage (int pn, double zm, int rot, QImage *pix) |
| 102 |
{ |
| 103 |
auto scale = fz_scale (static_cast<float> (zm), static_cast<float> (zm)); |
| 104 |
auto mat = fz_pre_rotate (scale, static_cast<float> (rot)); |
| 105 |
auto color = fz_device_rgb (mContext); |
| 106 |
auto pixmap |
| 107 |
= fz_new_pixmap_from_page_number (mContext, mDoc, pn, mat, color, 0); |
| 108 |
if (pixmap == nullptr) |
| 109 |
return false; |
| 110 |
|
| 111 |
auto comments = mNote.getCommentsInPage (pn); |
| 112 |
pageRenderComments (pn, pixmap, comments, mat); |
| 113 |
|
| 114 |
QImage img{ pixmap->w, pixmap->h, QImage::Format_RGB32 }; |
| 115 |
for (auto y = 0; y < pixmap->h; ++y) |
| 116 |
{ |
| 117 |
auto p = pixmap->samples + y * pixmap->stride; |
| 118 |
for (auto x = 0; x < pixmap->w; ++x) |
| 119 |
{ |
| 120 |
QColor c{ int (p[0]), int (p[1]), int (p[2]) }; |
| 121 |
img.setPixelColor (x, y, c); |
| 122 |
p += pixmap->n; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
fz_drop_pixmap (mContext, pixmap); |
| 127 |
|
| 128 |
*pix = img; |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
optional<vector<Rectangle>> |
| 133 |
ApvlvMuPDF::pageHighlight (int pn, const ApvlvPoint &pa, const ApvlvPoint &pb) |
| 134 |
{ |
| 135 |
auto options = fz_stext_options{}; |
| 136 |
auto text_page |
| 137 |
= fz_new_stext_page_from_page_number (mContext, mDoc, pn, &options); |
| 138 |
auto fa = fz_point{ static_cast<float> (pa.x), static_cast<float> (pa.y) }; |
| 139 |
auto fb = fz_point{ static_cast<float> (pb.x), static_cast<float> (pb.y) }; |
| 140 |
std::array<fz_quad, 1024> quad_array; |
| 141 |
auto quads |
| 142 |
= fz_highlight_selection (mContext, text_page, fa, fb, quad_array.data(), quad_array.size()); |
| 143 |
fz_drop_stext_page (mContext, text_page); |
| 144 |
if (quads == 0) |
| 145 |
return nullopt; |
| 146 |
|
| 147 |
auto rect_list = vector<Rectangle>{}; |
| 148 |
for (auto i = 0; i < quads; ++i) |
| 149 |
{ |
| 150 |
auto quad = &quad_array[i]; |
| 151 |
Rectangle r{ quad->ul.x, quad->ul.y, quad->lr.x, quad->lr.y }; |
| 152 |
rect_list.emplace_back (r); |
| 153 |
} |
| 154 |
return rect_list; |
| 155 |
} |
| 156 |
|
| 157 |
bool |
| 158 |
ApvlvMuPDF::pageText (int pn, const Rectangle &rect, string &text) |
| 159 |
{ |
| 160 |
auto text_page |
| 161 |
= fz_new_stext_page_from_page_number (mContext, mDoc, pn, nullptr); |
| 162 |
auto fzrect = fz_rect{ |
| 163 |
.x0 = static_cast<float> (rect.p1x), |
| 164 |
.y0 = static_cast<float> (rect.p1y), |
| 165 |
.x1 = static_cast<float> (rect.p2x), |
| 166 |
.y1 = static_cast<float> (rect.p2y), |
| 167 |
}; |
| 168 |
text = fz_copy_rectangle (mContext, text_page, fzrect, 0); |
| 169 |
fz_drop_stext_page (mContext, text_page); |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
unique_ptr<WordListRectangle> |
| 174 |
ApvlvMuPDF::pageSearch (int pn, const char *str) |
| 175 |
{ |
| 176 |
int hit; |
| 177 |
std::array<fz_quad, 1024> quad_array; |
| 178 |
auto count = fz_search_page_number (mContext, mDoc, pn, str, &hit, |
| 179 |
quad_array.data (), quad_array.size ()); |
| 180 |
if (count == 0) |
| 181 |
return nullptr; |
| 182 |
|
| 183 |
auto list = make_unique<WordListRectangle> (); |
| 184 |
for (auto i = 0; i < count; ++i) |
| 185 |
{ |
| 186 |
WordRectangle rectangle; |
| 187 |
rectangle.word = str; |
| 188 |
auto quad = quad_array[i]; |
| 189 |
Rectangle rect{ quad.ul.x, quad.lr.y, quad.lr.x, quad.ul.y }; |
| 190 |
rectangle.rect_list.push_back (rect); |
| 191 |
list->push_back (rectangle); |
| 192 |
} |
| 193 |
return list; |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
ApvlvMuPDF::pageRenderComments (int pn, fz_pixmap *pixmap, |
| 198 |
const vector<Comment> &comments, |
| 199 |
const fz_matrix &mat) |
| 200 |
{ |
| 201 |
if (comments.empty ()) |
| 202 |
return; |
| 203 |
|
| 204 |
auto dev = fz_new_draw_device (mContext, mat, pixmap); |
| 205 |
auto stroke = fz_new_stroke_state (mContext); |
| 206 |
stroke->linewidth = 0.4; |
| 207 |
fz_path *path = fz_new_path (mContext); |
| 208 |
auto scale = fz_scale (1.0, 1.0); |
| 209 |
std::array<float, 3> color = { 0.0, 0.0, 1.0 }; |
| 210 |
|
| 211 |
for (const auto &comment : comments) |
| 212 |
{ |
| 213 |
ApvlvPoint pa{ comment.begin.x, comment.begin.y }; |
| 214 |
ApvlvPoint pb{ comment.end.x, comment.end.y }; |
| 215 |
auto rect_list = pageHighlight (pn, pa, pb); |
| 216 |
if (rect_list->empty ()) |
| 217 |
continue; |
| 218 |
|
| 219 |
for (auto const &rect : rect_list.value ()) |
| 220 |
{ |
| 221 |
fz_moveto (mContext, path, static_cast<float> (rect.p1x), |
| 222 |
static_cast<float> (rect.p2y)); |
| 223 |
fz_lineto (mContext, path, static_cast<float> (rect.p2x), |
| 224 |
static_cast<float> (rect.p2y)); |
| 225 |
fz_stroke_path (mContext, dev, path, stroke, scale, |
| 226 |
fz_device_rgb (mContext), color.data (), 0.8, |
| 227 |
fz_default_color_params); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
fz_drop_path (mContext, path); |
| 232 |
fz_drop_stroke_state (mContext, stroke); |
| 233 |
fz_drop_device (mContext, dev); |
| 234 |
} |
| 235 |
|
| 236 |
void |
| 237 |
ApvlvMuPDF::generateIndex () |
| 238 |
{ |
| 239 |
mIndex = { "", 0, "", FileIndexType::FILE }; |
| 240 |
|
| 241 |
fz_outline *top_toc; |
| 242 |
fz_try (mContext) top_toc = fz_load_outline (mContext, mDoc); |
| 243 |
fz_catch (mContext) |
| 244 |
{ |
| 245 |
qCritical () << "load " << mFilename << " outline error"; |
| 246 |
fz_report_error (mContext); |
| 247 |
top_toc = nullptr; |
| 248 |
} |
| 249 |
|
| 250 |
if (top_toc == nullptr) |
| 251 |
return; |
| 252 |
|
| 253 |
auto toc = top_toc; |
| 254 |
while (toc != nullptr) |
| 255 |
{ |
| 256 |
auto child_index = FileIndex{}; |
| 257 |
generateIndexRecursively (child_index, toc); |
| 258 |
mIndex.mChildrenIndex.push_back (child_index); |
| 259 |
toc = toc->next; |
| 260 |
} |
| 261 |
|
| 262 |
fz_drop_outline (mContext, top_toc); |
| 263 |
} |
| 264 |
|
| 265 |
void |
| 266 |
ApvlvMuPDF::generateIndexRecursively (FileIndex &index, |
| 267 |
const fz_outline *outline) |
| 268 |
{ |
| 269 |
index.type = FileIndexType::PAGE; |
| 270 |
if (outline->title) |
| 271 |
index.title = outline->title; |
| 272 |
index.page = fz_page_number_from_location (mContext, mDoc, outline->page); |
| 273 |
if (outline->uri != nullptr) |
| 274 |
{ |
| 275 |
index.path = outline->uri; |
| 276 |
auto pos = index.path.find ('#'); |
| 277 |
if (pos != string::npos) |
| 278 |
{ |
| 279 |
index.anchor = index.path.substr (pos); |
| 280 |
index.path = index.path.substr (0, pos); |
| 281 |
} |
| 282 |
if (index.page == -1) |
| 283 |
{ |
| 284 |
auto dest = fz_resolve_link (mContext, mDoc, outline->uri, nullptr, |
| 285 |
nullptr); |
| 286 |
index.page = fz_page_number_from_location (mContext, mDoc, dest); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
auto toc = outline->down; |
| 291 |
while (toc != nullptr) |
| 292 |
{ |
| 293 |
auto child_index = FileIndex{}; |
| 294 |
generateIndexRecursively (child_index, toc); |
| 295 |
index.mChildrenIndex.push_back (child_index); |
| 296 |
toc = toc->next; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// Local Variables: |
| 302 |
// mode: c++ |
| 303 |
// End: |