2010-11-18 02:35:33 +01:00
|
|
|
/* See LICENSE file for license and copyright information */
|
|
|
|
|
2010-11-18 03:15:32 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-11-18 02:35:33 +01:00
|
|
|
#include "pdf.h"
|
2010-12-26 01:03:04 +01:00
|
|
|
#include "../../zathura.h"
|
2010-11-18 02:35:33 +01:00
|
|
|
|
2011-03-06 09:43:19 +01:00
|
|
|
void
|
|
|
|
plugin_register(zathura_document_plugin_t* plugin)
|
|
|
|
{
|
|
|
|
plugin->file_extension = "pdf";
|
|
|
|
plugin->open_function = pdf_document_open;
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:35:33 +01:00
|
|
|
bool
|
|
|
|
pdf_document_open(zathura_document_t* document)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document) {
|
2011-02-26 04:15:50 +01:00
|
|
|
goto error_ret;
|
2010-12-23 20:26:36 +01:00
|
|
|
}
|
2010-11-18 03:15:32 +01:00
|
|
|
|
|
|
|
document->functions.document_free = pdf_document_free;
|
|
|
|
document->functions.page_get = pdf_page_get;
|
|
|
|
document->functions.page_search_text = pdf_page_search_text;
|
|
|
|
document->functions.page_links_get = pdf_page_links_get;
|
|
|
|
document->functions.page_form_fields_get = pdf_page_form_fields_get;
|
|
|
|
document->functions.page_render = pdf_page_render;
|
|
|
|
document->functions.page_free = pdf_page_free;
|
|
|
|
|
2010-11-18 13:54:35 +01:00
|
|
|
document->data = malloc(sizeof(pdf_document_t));
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document->data) {
|
2011-02-26 04:15:50 +01:00
|
|
|
goto error_ret;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
|
|
|
|
|
|
|
fz_accelerate();
|
|
|
|
pdf_document->glyphcache = fz_newglyphcache();
|
2010-11-18 13:54:35 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (pdf_openxref(&(pdf_document->document), document->file_path, NULL)) {
|
|
|
|
fprintf(stderr, "error: could not open file\n");
|
2010-12-23 20:26:36 +01:00
|
|
|
goto error_free;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (pdf_loadpagetree(pdf_document->document)) {
|
|
|
|
fprintf(stderr, "error: could not open file\n");
|
2010-12-23 20:26:36 +01:00
|
|
|
goto error_free;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
document->number_of_pages = pdf_getpagecount(pdf_document->document);
|
2011-01-06 11:54:31 +01:00
|
|
|
|
2010-11-18 02:35:33 +01:00
|
|
|
return true;
|
2010-12-23 20:26:36 +01:00
|
|
|
|
|
|
|
error_free:
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (pdf_document->document) {
|
|
|
|
pdf_freexref(pdf_document->document);
|
|
|
|
}
|
2010-12-23 20:26:36 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (pdf_document->glyphcache) {
|
|
|
|
fz_freeglyphcache(pdf_document->glyphcache);
|
|
|
|
}
|
2010-12-23 20:26:36 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
free(document->data);
|
|
|
|
document->data = NULL;
|
2010-12-23 20:26:36 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
error_ret:
|
2010-12-23 20:26:36 +01:00
|
|
|
|
|
|
|
return false;
|
2010-11-18 02:35:33 +01:00
|
|
|
}
|
2010-11-18 03:15:32 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
pdf_document_free(zathura_document_t* document)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document) {
|
2010-11-18 13:54:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (document->data) {
|
2010-11-18 14:51:13 +01:00
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
2011-02-26 04:15:50 +01:00
|
|
|
pdf_freexref(pdf_document->document);
|
|
|
|
fz_freeglyphcache(pdf_document->glyphcache);
|
2010-11-18 13:54:35 +01:00
|
|
|
free(document->data);
|
2010-12-23 19:41:07 +01:00
|
|
|
document->data = NULL;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-11-18 03:15:32 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
zathura_page_t*
|
|
|
|
pdf_page_get(zathura_document_t* document, unsigned int page)
|
2010-11-18 03:15:32 +01:00
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document || !document->data) {
|
2010-12-25 00:47:52 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
|
|
|
zathura_page_t* document_page = malloc(sizeof(zathura_page_t));
|
2010-12-26 01:12:20 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (document_page == NULL) {
|
|
|
|
goto error_ret;
|
2010-12-25 00:47:52 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
mupdf_page_t* mupdf_page = malloc(sizeof(mupdf_page_t));
|
2010-12-25 00:47:52 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (mupdf_page == NULL) {
|
|
|
|
goto error_free;
|
2010-11-18 14:51:13 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
document_page->document = document;
|
|
|
|
document_page->data = mupdf_page;
|
2010-11-18 03:15:32 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
mupdf_page->page_object = pdf_getpageobject(pdf_document->document, page + 1);
|
2010-11-18 03:15:32 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
if (pdf_loadpage(&(mupdf_page->page), pdf_document->document, mupdf_page->page_object)) {
|
|
|
|
goto error_free;
|
2010-11-18 14:51:13 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
document_page->width = mupdf_page->page->mediabox.x1 - mupdf_page->page->mediabox.x0;
|
|
|
|
document_page->height = mupdf_page->page->mediabox.y1 - mupdf_page->page->mediabox.y0;
|
2010-11-18 14:51:13 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
return document_page;
|
2010-11-18 14:51:13 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
error_free:
|
2010-11-18 14:51:13 +01:00
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
if (mupdf_page && mupdf_page->page_object) {
|
|
|
|
fz_dropobj(mupdf_page->page_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mupdf_page && mupdf_page->page) {
|
|
|
|
pdf_freepage(mupdf_page->page);
|
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
free(document_page);
|
|
|
|
free(mupdf_page);
|
2010-11-18 14:51:13 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
error_ret:
|
2010-11-18 14:51:13 +01:00
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
return NULL;
|
2010-11-18 14:51:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pdf_page_free(zathura_page_t* page)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!page) {
|
2010-11-18 14:51:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
mupdf_page_t* mupdf_page = (mupdf_page_t*) page->data;
|
|
|
|
pdf_freepage(mupdf_page->page);
|
|
|
|
fz_dropobj(mupdf_page->page_object);
|
|
|
|
free(mupdf_page);
|
2010-11-18 14:51:13 +01:00
|
|
|
free(page);
|
|
|
|
|
|
|
|
return true;
|
2010-11-18 03:15:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
zathura_list_t*
|
|
|
|
pdf_page_search_text(zathura_page_t* page, const char* text)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
zathura_list_t*
|
|
|
|
pdf_page_links_get(zathura_page_t* page)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
zathura_list_t*
|
|
|
|
pdf_page_form_fields_get(zathura_page_t* page)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-18 13:27:21 +01:00
|
|
|
zathura_image_buffer_t*
|
2010-11-18 03:15:32 +01:00
|
|
|
pdf_page_render(zathura_page_t* page)
|
|
|
|
{
|
2011-04-18 18:05:17 +02:00
|
|
|
if (!page || !page->data || !page->document) {
|
2010-12-26 01:03:04 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-26 20:35:26 +01:00
|
|
|
/* calculate sizes */
|
2011-04-18 18:05:17 +02:00
|
|
|
unsigned int page_width = page->document->scale * page->width;
|
|
|
|
unsigned int page_height = page->document->scale * page->height;
|
2010-12-26 20:35:26 +01:00
|
|
|
|
2011-04-18 18:05:17 +02:00
|
|
|
if (page->document->rotate == 90 || page->document->rotate == 270) {
|
2010-12-26 20:35:26 +01:00
|
|
|
unsigned int dim_temp = 0;
|
|
|
|
dim_temp = page_width;
|
|
|
|
page_width = page_height;
|
|
|
|
page_height = dim_temp;
|
|
|
|
}
|
|
|
|
|
2011-03-18 14:10:42 +01:00
|
|
|
/* create image buffer */
|
|
|
|
zathura_image_buffer_t* image_buffer = zathura_image_buffer_create(page_width, page_height);
|
2010-12-26 01:03:04 +01:00
|
|
|
|
2011-03-18 14:10:42 +01:00
|
|
|
if (image_buffer == NULL) {
|
2010-12-26 01:03:04 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-26 04:15:50 +01:00
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) page->document->data;
|
2011-02-27 01:19:26 +01:00
|
|
|
mupdf_page_t* mupdf_page = (mupdf_page_t*) page->data;
|
2011-02-26 04:15:50 +01:00
|
|
|
|
|
|
|
/* render */
|
2011-02-27 01:19:26 +01:00
|
|
|
fz_displaylist* display_list = fz_newdisplaylist();
|
|
|
|
fz_device* device = fz_newlistdevice(display_list);
|
2011-02-26 04:15:50 +01:00
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
if (pdf_runpage(pdf_document->document, mupdf_page->page, device, fz_identity)) {
|
2011-02-26 04:15:50 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
fz_freedevice(device);
|
2011-02-26 04:15:50 +01:00
|
|
|
|
2011-03-06 11:24:30 +01:00
|
|
|
pdf_agestore(pdf_document->document->store, 3);
|
|
|
|
|
|
|
|
fz_matrix ctm = fz_identity;
|
|
|
|
ctm = fz_concat(ctm, fz_translate(0, -mupdf_page->page->mediabox.y1));
|
2011-04-18 18:05:17 +02:00
|
|
|
ctm = fz_concat(ctm, fz_scale(page->document->scale, -page->document->scale));
|
2011-03-06 11:24:30 +01:00
|
|
|
ctm = fz_concat(ctm, fz_rotate(mupdf_page->page->rotate));
|
2011-04-18 18:05:17 +02:00
|
|
|
ctm = fz_concat(ctm, fz_rotate(page->document->rotate));
|
2011-02-27 01:19:26 +01:00
|
|
|
fz_bbox bbox = fz_roundrect(fz_transformrect(ctm, mupdf_page->page->mediabox));
|
2011-02-26 04:15:50 +01:00
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
fz_pixmap* pixmap = fz_newpixmapwithrect(fz_devicergb, bbox);
|
2011-03-06 09:43:19 +01:00
|
|
|
fz_clearpixmapwithcolor(pixmap, 0xFF);
|
2011-02-26 04:15:50 +01:00
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
device = fz_newdrawdevice(pdf_document->glyphcache, pixmap);
|
|
|
|
fz_executedisplaylist(display_list, device, ctm);
|
|
|
|
fz_freedevice(device);
|
2011-02-26 04:15:50 +01:00
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
for (unsigned int y = 0; y < pixmap->h; y++) {
|
|
|
|
for (unsigned int x = 0; x < pixmap->w; x++) {
|
|
|
|
unsigned char *s = pixmap->samples + y * pixmap->w * 4 + x * 4;
|
2011-03-18 14:10:42 +01:00
|
|
|
guchar* p = image_buffer->data + y * image_buffer->width + x;
|
2011-02-26 04:15:50 +01:00
|
|
|
p[0] = s[0];
|
|
|
|
p[1] = s[1];
|
|
|
|
p[2] = s[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-27 01:19:26 +01:00
|
|
|
fz_droppixmap(pixmap);
|
|
|
|
fz_freedisplaylist(display_list);
|
2010-12-26 01:03:04 +01:00
|
|
|
|
2011-03-18 14:10:42 +01:00
|
|
|
return image_buffer;
|
2010-11-18 03:15:32 +01:00
|
|
|
}
|