2 #include "mupdf-page_p.h"
4 #include "mupdf-document_p.h"
6 #include "mupdf-textbox_p.h"
8 #include <mupdf/fitz.h>
13 static void clear_bgr_samples_with_value(
14 unsigned char *samples,
int size,
15 int b,
int g,
int r,
int a)
27 static void clear_rgb_samples_with_value(
28 unsigned char *samples,
int size,
29 int b,
int g,
int r,
int a)
44 static inline void imageCleanupHandler(
void *data)
46 unsigned char *samples =
static_cast<unsigned char *
>(data);
56 QPointF
mapToOrigin(
const QPointF &pos,
float scaleX,
float scaleY,
float rotation)
58 fz_matrix transform = fz_identity;
63 fz_rotate(&transform, rotation);
64 fz_pre_scale(&transform, scaleX, scaleY);
67 fz_invert_matrix(&inverse, &transform);
72 fz_transform_point(&point, &inverse);
74 return QPointF(point.x, point.y);
77 QSizeF
mapToOrigin(
const QSizeF &size,
float scaleX,
float scaleY,
float rotation)
79 fz_matrix transform = fz_identity;
84 fz_rotate(&transform, rotation);
85 fz_pre_scale(&transform, scaleX, scaleY);
88 fz_invert_matrix(&inverse, &transform);
91 vector.x = size.width();
92 vector.y = size.height();
93 fz_transform_vector(&vector, &inverse);
95 return QSizeF(vector.x, vector.y);
98 QRectF
mapToOrigin(
const QRectF &rect,
float scaleX,
float scaleY,
float rotation)
100 fz_matrix transform = fz_identity;
105 fz_rotate(&transform, rotation);
106 fz_pre_scale(&transform, scaleX, scaleY);
109 fz_invert_matrix(&inverse, &transform);
115 r.y1 = rect.bottom();
116 fz_transform_rect(&r, &inverse);
118 return QRectF(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
121 QPointF
mapFromOrigin(
const QPointF &pos,
float scaleX,
float scaleY,
float rotation)
123 fz_matrix transform = fz_identity;
127 fz_rotate(&transform, rotation);
128 fz_pre_scale(&transform, scaleX, scaleY);
133 fz_transform_point(&point, &transform);
135 return QPointF(point.x, point.y);
138 QSizeF
mapFromOrigin(
const QSizeF &size,
float scaleX,
float scaleY,
float rotation)
140 fz_matrix transform = fz_identity;
144 fz_rotate(&transform, rotation);
145 fz_pre_scale(&transform, scaleX, scaleY);
148 vector.x = size.width();
149 vector.y = size.height();
150 fz_transform_vector(&vector, &transform);
152 return QSizeF(vector.x, vector.y);
155 QRectF
mapFromOrigin(
const QRectF &rect,
float scaleX,
float scaleY,
float rotation)
157 fz_matrix transform = fz_identity;
161 fz_rotate(&transform, rotation);
162 fz_pre_scale(&transform, scaleX, scaleY);
168 r.y1 = rect.bottom();
169 fz_transform_rect(&r, &transform);
171 return QRectF(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0);
180 PagePrivate::PagePrivate(DocumentPrivate *dp,
int index)
182 , context(documentp->context)
183 , document(documentp->document)
188 , transparent(documentp->transparent)
189 , b(documentp->b), g(documentp->g), r(documentp->r), a(documentp->a)
194 fz_device *list_device;
195 fz_device *text_device;
198 page = fz_load_page(document, index);
201 display_list = fz_new_display_list(context);
202 list_device = fz_new_list_device(context, display_list);
203 fz_run_page(document, page, list_device, &fz_identity, NULL);
204 fz_free_device(list_device);
207 text_sheet = fz_new_text_sheet(context);
208 text_page = fz_new_text_page(context);
209 text_device = fz_new_text_device(context, text_sheet, text_page);
210 fz_bound_page(document, page, &bounds);
211 fz_run_display_list(display_list, text_device, &fz_identity, &bounds, NULL);
212 fz_free_device(text_device);
225 return (d && d->page) ?
true :
false;
241 fz_pixmap *pixmap = NULL;
242 unsigned char *samples = NULL;
243 unsigned char *copyed_samples = NULL;
249 fz_matrix transform = fz_identity;
250 fz_rotate(&transform, rotation);
251 fz_pre_scale(&transform, scaleX, scaleY);
256 fz_bound_page(d->document, d->page, &bounds);
257 fz_transform_rect(&bounds, &transform);
258 fz_round_rect(&bbox, &bounds);
261 fz_device *dev = NULL;
265 #if QT_VERSION < 0x050200
266 pixmap = fz_new_pixmap_with_bbox(d->context, fz_device_bgr(d->context), &bbox);
269 pixmap = fz_new_pixmap_with_bbox(d->context, fz_device_rgb(d->context), &bbox);
271 samples = fz_pixmap_samples(d->context, pixmap);
272 width = fz_pixmap_width(d->context, pixmap);
273 height = fz_pixmap_height(d->context, pixmap);
274 size = width * height * 4;
275 if (!d->transparent) {
276 if (d->b >= 0 && d->g >= 0 && d->r >= 0 && d->a >= 0) {
278 #if QT_VERSION < 0x050200
279 clear_bgr_samples_with_value(samples, size, d->b, d->g, d->r, d->a);
282 clear_rgb_samples_with_value(samples, size, d->b, d->g, d->r, d->a);
286 fz_clear_pixmap_with_value(d->context, pixmap, 0xff);
289 dev = fz_new_draw_device(d->context, pixmap);
290 fz_run_display_list(d->display_list, dev, &transform, &bounds, NULL);
292 fz_always(d->context)
302 fz_drop_pixmap(d->context, pixmap);
309 if (NULL == pixmap) {
312 copyed_samples =
new unsigned char[
size];
313 memcpy(copyed_samples, samples, size);
314 fz_drop_pixmap(d->context, pixmap);
315 #if QT_VERSION < 0x050200
318 image = QImage(copyed_samples,
319 width, height, QImage::Format_ARGB32, imageCleanupHandler, copyed_samples);
322 image = QImage(copyed_samples,
323 width, height, QImage::Format_RGBA8888, imageCleanupHandler, copyed_samples);
335 fz_bound_page(d->document, d->page, &rect);
336 return QSizeF(rect.x1 - rect.x0, rect.y1 - rect.y0);
348 d->transparent = enable;
388 r.y1 = rect.bottom();
391 if (!fz_is_infinite_rect(&r)) {
392 str = fz_copy_selection(d->context, d->text_page, r);
393 ret = QString::fromUtf8(str);
408 QList<TextBox *> ret;
410 TextBoxPrivate *boxp;
411 fz_text_block *block;
415 for (
int block_num = 0; block_num < d->text_page->len; ++block_num) {
417 if (d->text_page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT) {
420 block = d->text_page->blocks[block_num].u.text;
422 for (line = block->lines; line < block->lines + block->len; ++line) {
423 for (span = line->first_span; span; span = span->next) {
424 boxp =
new TextBoxPrivate(span);
434 PagePrivate::~PagePrivate()
438 documentp->pages.removeAt(documentp->pages.indexOf(
this));