forked from gustawho/okular-backend-mupdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.cpp
More file actions
190 lines (157 loc) · 5.85 KB
/
page.cpp
File metadata and controls
190 lines (157 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
* Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "page.hpp"
extern "C" {
#include <mupdf/fitz.h>
}
#include <QDebug>
#include <QImage>
#include <QSharedData>
namespace QMuPDF
{
QRectF convert_fz_rect(const fz_rect &rect, const QSizeF &dpi)
{
const float scaleX = dpi.width() / 72.;
const float scaleY = dpi.height() / 72.;
const QPointF topLeft(rect.x0 * scaleX, rect.y0 * scaleY);
const QPointF bottomRight(rect.x1 * scaleX, rect.y1 * scaleY);
return QRectF(topLeft, bottomRight);
}
QImage convert_fz_pixmap(fz_context *ctx, fz_pixmap *image)
{
const int w = fz_pixmap_width(ctx, image);
const int h = fz_pixmap_height(ctx, image);
QImage img(w, h, QImage::Format_RGBA8888);
if (img.bytesPerLine() == fz_pixmap_stride(ctx, image)) {
memcpy(img.bits(), fz_pixmap_samples(ctx, image), img.sizeInBytes());
} else {
qWarning() << "QImage line stride" << img.bytesPerLine() << "doesn't match" << fz_pixmap_stride(ctx, image);
QRgb *data = reinterpret_cast<QRgb *>(fz_pixmap_samples(ctx, image));
for (int i = 0; i < h; ++i) {
QRgb *imgdata = reinterpret_cast<QRgb *>(img.scanLine(h));
for (int j = 0; j < w; ++j) {
*imgdata = *data;
data++;
imgdata++;
}
}
}
return img;
}
struct Page::Data : public QSharedData {
Data(int pageNum, fz_context *ctx, fz_document *doc, fz_page *page) : pageNum{pageNum}, ctx{ctx}, doc{doc}, page{page} {}
int pageNum;
fz_context *ctx;
fz_document *doc;
fz_page *page;
};
Page::~Page()
{
fz_drop_page(d->ctx, d->page);
}
Page::Page(fz_context *ctx, fz_document *doc, int num) :
d(new Page::Data(num, ctx, doc, fz_load_page(ctx, doc, num)))
{
Q_ASSERT(doc && ctx);
}
Page::Page(const Page &other) = default;
int Page::number() const
{
return d->pageNum;
}
QSizeF Page::size(const QSizeF &dpi) const
{
fz_rect rect = fz_bound_page(d->ctx, d->page);
// MuPDF always assumes 72dpi
return QSizeF((rect.x1 - rect.x0) * dpi.width() / 72.,
(rect.y1 - rect.y0) * dpi.height() / 72.);
}
qreal Page::duration() const
{
float val;
(void)fz_page_presentation(d->ctx, d->page, nullptr, &val);
return val < 0.1 ? -1 : val;
}
QImage Page::render(qreal width, qreal height) const
{
const QSizeF s = size(QSizeF(72, 72));
fz_matrix ctm = fz_scale(width / s.width(), height / s.height());
fz_cookie cookie = { 0, 0, 0, 0, 0 };
fz_colorspace *csp = fz_device_rgb(d->ctx);
fz_pixmap *image = fz_new_pixmap(d->ctx, csp, width, height, nullptr, 1);
fz_clear_pixmap_with_value(d->ctx, image, 0xff);
fz_device *device = fz_new_draw_device(d->ctx, fz_identity, image);
fz_run_page(d->ctx, d->page, device, ctm, &cookie);
fz_close_device(d->ctx, device);
fz_drop_device(d->ctx, device);
QImage img;
if (!cookie.errors) {
img = convert_fz_pixmap(d->ctx, image);
}
fz_drop_pixmap(d->ctx, image);
return img;
}
QVector<TextBox *> Page::textBoxes(const QSizeF &dpi) const
{
fz_cookie cookie = {0, 0, 0, 0, 0};
fz_stext_page *page = fz_new_stext_page(d->ctx, fz_empty_rect);
fz_stext_options options{};
fz_device *device = fz_new_stext_device(d->ctx, page, &options);
fz_run_page(d->ctx, d->page, device, fz_identity, &cookie);
fz_close_device(d->ctx, device);
fz_drop_device(d->ctx, device);
if (cookie.errors) {
fz_drop_stext_page(d->ctx, page);
return QVector<TextBox *>();
}
QVector<TextBox *> boxes;
for (fz_stext_block *block = page->first_block; block; block = block->next) {
if (block->type != FZ_STEXT_BLOCK_TEXT) {
continue;
}
for (fz_stext_line *line = block->u.t.first_line; line; line = line->next) {
bool hasText = false;
for (fz_stext_char *ch = line->first_char; ch; ch = ch->next) {
const int text = ch->c;
TextBox *box = new TextBox(text, convert_fz_rect(fz_rect_from_quad(ch->quad), dpi));
boxes.append(box);
hasText = true;
}
if (hasText) {
boxes.back()->markAtEndOfLine();
}
}
}
fz_drop_stext_page(d->ctx, page);
return boxes;
}
QVector<Link> Page::links(const QSizeF &dpi) const
{
QVector<Link> ret;
const auto deleter = [this](fz_link* link) { fz_drop_link(d->ctx, link); };
std::unique_ptr<fz_link, decltype(deleter)> links{fz_load_links(d->ctx, d->page), deleter};
const auto pageSize = size(dpi);
for (fz_link* link = links.get(); link; link = link->next)
{
const auto linkRect = convert_fz_rect(link->rect, dpi);
QRectF normalizedRect{linkRect.left() / pageSize.width(), linkRect.top() / pageSize.height(), linkRect.width() / pageSize.width(), linkRect.height() / pageSize.height()};
if (fz_is_external_link(d->ctx, link->uri))
{
ret.push_back({link->uri, std::move(normalizedRect)});
}
else
{
float xp = 0, yp = 0;
fz_location location = fz_resolve_link(d->ctx, d->doc, link->uri, &xp, &yp);
ret.push_back({location.page, xp / pageSize.width(), yp / pageSize.height(), std::move(normalizedRect)});
}
}
return ret;
}
} // namespace QMuPDF