forked from bylee20/okular-backend-mupdf
-
Notifications
You must be signed in to change notification settings - Fork 5
/
page.cpp
163 lines (134 loc) · 4.83 KB
/
page.cpp
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
/***************************************************************************
* 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_ARGB32);
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;
}
} // namespace QMuPDF