zathura/ft/pdf-mupdf/pdf.c

251 lines
5.9 KiB
C
Raw Normal View History

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
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;
}
GtkWidget*
2010-11-18 03:15:32 +01:00
pdf_page_render(zathura_page_t* page)
{
2011-02-09 12:29:09 +01:00
if (!Zathura.document || !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 */
unsigned int page_width = Zathura.document->scale * page->width;
unsigned int page_height = Zathura.document->scale * page->height;
2011-02-09 12:29:09 +01:00
if (Zathura.document->rotate == 90 || Zathura.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;
}
/* create pixbuf */
GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
2010-12-26 20:35:26 +01:00
page_width, page_height);
2010-12-26 01:03:04 +01:00
2011-02-09 12:29:09 +01:00
if (!pixbuf) {
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
fz_matrix ctm = fz_translate(0, -mupdf_page->page->mediabox.y1);
2011-02-27 01:19:26 +01:00
ctm = fz_concat(ctm, fz_scale(Zathura.document->scale, -Zathura.document->scale));
fz_bbox bbox = fz_roundrect(fz_transformrect(ctm, mupdf_page->page->mediabox));
2011-02-26 04:15:50 +01:00
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
int n_channels = gdk_pixbuf_get_n_channels(pixbuf);
2011-02-27 01:19:26 +01:00
fz_pixmap* pixmap = fz_newpixmapwithrect(fz_devicergb, bbox);
fz_clearpixmap(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-02-26 04:15:50 +01:00
guchar* p = pixels + y * rowstride + x * n_channels;
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);
2011-02-26 04:15:50 +01:00
pdf_freepage(mupdf_page->page);
pdf_agestore(pdf_document->document->store, 3);
2010-12-26 01:03:04 +01:00
/* write pixbuf */
GtkWidget* image = gtk_image_new();
2010-12-26 01:03:04 +01:00
2011-02-09 12:29:09 +01:00
if (!image) {
g_object_unref(pixbuf);
return NULL;
2010-12-26 01:03:04 +01:00
}
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
gtk_widget_show(image);
2010-12-26 01:03:04 +01:00
return image;
2010-11-18 03:15:32 +01:00
}