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-12-26 01:03:04 +01:00
|
|
|
#include <poppler/glib/poppler.h>
|
2010-11-18 03:15:32 +01:00
|
|
|
|
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) {
|
2010-12-23 20:26:36 +01:00
|
|
|
goto error_out;
|
|
|
|
}
|
2010-11-18 03:15:32 +01:00
|
|
|
|
|
|
|
document->functions.document_free = pdf_document_free;
|
|
|
|
document->functions.document_index_generate = pdf_document_index_generate;;
|
|
|
|
document->functions.document_save_as = pdf_document_save_as;
|
|
|
|
document->functions.document_attachments_get = pdf_document_attachments_get;
|
|
|
|
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) {
|
2010-12-23 20:26:36 +01:00
|
|
|
goto error_out;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* format path */
|
|
|
|
GError* error = NULL;
|
|
|
|
char* file_uri = g_filename_to_uri(document->file_path, NULL, &error);
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!file_uri) {
|
2010-11-18 13:54:35 +01:00
|
|
|
fprintf(stderr, "error: could not open file: %s\n", error->message);
|
2010-12-23 20:26:36 +01:00
|
|
|
goto error_free;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
2010-11-18 14:51:13 +01:00
|
|
|
pdf_document->document = poppler_document_new_from_file(file_uri, document->password, &error);
|
2010-11-18 13:54:35 +01:00
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!pdf_document->document) {
|
2010-11-18 13:54:35 +01:00
|
|
|
fprintf(stderr, "error: could not open file: %s\n", error->message);
|
2010-12-23 20:26:36 +01:00
|
|
|
goto error_free;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
2010-11-18 14:51:13 +01:00
|
|
|
document->number_of_pages = poppler_document_get_n_pages(pdf_document->document);
|
|
|
|
|
2011-01-06 11:54:31 +01:00
|
|
|
g_free(file_uri);
|
|
|
|
|
2010-11-18 02:35:33 +01:00
|
|
|
return true;
|
2010-12-23 20:26:36 +01:00
|
|
|
|
|
|
|
error_free:
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (error) {
|
2010-12-23 20:26:36 +01:00
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (file_uri) {
|
2010-12-23 20:26:36 +01:00
|
|
|
g_free(file_uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(document->data);
|
|
|
|
document->data = NULL;
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
|
|
|
|
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;
|
|
|
|
g_object_unref(pdf_document->document);
|
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
|
|
|
}
|
|
|
|
|
2010-12-25 00:47:52 +01:00
|
|
|
static void
|
|
|
|
build_index(pdf_document_t* pdf, girara_tree_node_t* root, PopplerIndexIter* iter)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!root || !iter) {
|
2010-12-25 00:47:52 +01:00
|
|
|
return;
|
2010-12-26 01:12:20 +01:00
|
|
|
}
|
2010-12-25 00:47:52 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
PopplerAction* action = poppler_index_iter_get_action(iter);
|
2010-12-26 01:12:20 +01:00
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!action) {
|
2010-12-25 00:47:52 +01:00
|
|
|
continue;
|
2010-12-26 01:12:20 +01:00
|
|
|
}
|
2010-12-25 00:47:52 +01:00
|
|
|
|
|
|
|
gchar* markup = g_markup_escape_text(action->any.title, -1);
|
2011-02-10 04:33:28 +01:00
|
|
|
zathura_index_element_t* index_element = zathura_index_element_new(markup);
|
2010-12-26 01:12:20 +01:00
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (action->type == POPPLER_ACTION_URI) {
|
2011-02-10 04:33:28 +01:00
|
|
|
index_element->type = ZATHURA_LINK_EXTERNAL;
|
|
|
|
index_element->target.uri = g_strdup(action->uri.uri);
|
2010-12-26 01:12:20 +01:00
|
|
|
} else if (action->type == POPPLER_ACTION_GOTO_DEST) {
|
2011-02-10 04:33:28 +01:00
|
|
|
index_element->type = ZATHURA_LINK_TO_PAGE;
|
2010-12-26 01:12:20 +01:00
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
|
2010-12-25 00:47:52 +01:00
|
|
|
PopplerDest* dest = poppler_document_find_dest(pdf->document, action->goto_dest.dest->named_dest);
|
2011-02-09 12:29:09 +01:00
|
|
|
if (dest) {
|
2011-02-10 04:33:28 +01:00
|
|
|
index_element->target.page_number = dest->page_num - 1;
|
2010-12-25 00:47:52 +01:00
|
|
|
poppler_dest_free(dest);
|
|
|
|
}
|
2010-12-26 01:12:20 +01:00
|
|
|
} else {
|
2011-02-10 04:33:28 +01:00
|
|
|
index_element->target.page_number = action->goto_dest.dest->page_num - 1;
|
2010-12-26 01:12:20 +01:00
|
|
|
}
|
|
|
|
} else {
|
2010-12-25 00:47:52 +01:00
|
|
|
poppler_action_free(action);
|
2011-02-10 04:33:28 +01:00
|
|
|
zathura_index_element_free(index_element);
|
2010-12-25 00:47:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-12-26 01:12:20 +01:00
|
|
|
|
2010-12-25 00:47:52 +01:00
|
|
|
poppler_action_free(action);
|
|
|
|
|
2011-02-10 04:33:28 +01:00
|
|
|
girara_tree_node_t* node = girara_node_append_data(root, index_element);
|
2010-12-26 01:12:20 +01:00
|
|
|
PopplerIndexIter* child = poppler_index_iter_get_child(iter);
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (child) {
|
2010-12-25 00:47:52 +01:00
|
|
|
build_index(pdf, node, child);
|
2010-12-26 01:12:20 +01:00
|
|
|
}
|
|
|
|
|
2010-12-25 00:47:52 +01:00
|
|
|
poppler_index_iter_free(child);
|
2010-12-26 01:12:20 +01:00
|
|
|
|
2010-12-25 00:47:52 +01:00
|
|
|
} while (poppler_index_iter_next(iter));
|
|
|
|
}
|
|
|
|
|
2010-12-24 16:21:54 +01:00
|
|
|
girara_tree_node_t*
|
2010-11-18 03:15:32 +01:00
|
|
|
pdf_document_index_generate(zathura_document_t* document)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document || !document->data) {
|
2010-12-25 00:47:52 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
2010-12-26 01:12:20 +01:00
|
|
|
PopplerIndexIter* iter = poppler_index_iter_new(pdf_document->document);
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!iter) {
|
2010-12-25 00:47:52 +01:00
|
|
|
// XXX: error message?
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
girara_tree_node_t* root = girara_node_new(zathura_index_element_new("ROOT"));
|
|
|
|
girara_node_set_free_function(root, (girara_free_function_t)zathura_index_element_free);
|
|
|
|
build_index(pdf_document, root, iter);
|
|
|
|
|
|
|
|
poppler_index_iter_free(iter);
|
|
|
|
return root;
|
2010-11-18 03:15:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pdf_document_save_as(zathura_document_t* document, const char* path)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document || !document->data || !path) {
|
2010-11-18 14:51:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
|
|
|
|
|
|
|
char* file_path = g_strdup_printf("file://%s", path);
|
|
|
|
poppler_document_save(pdf_document->document, file_path, NULL);
|
|
|
|
g_free(file_path);
|
|
|
|
|
2010-11-18 03:15:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
zathura_list_t*
|
|
|
|
pdf_document_attachments_get(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
zathura_page_t*
|
|
|
|
pdf_page_get(zathura_document_t* document, unsigned int page)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document || !document->data) {
|
2010-11-18 14:51:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
|
|
|
|
zathura_page_t* document_page = malloc(sizeof(zathura_page_t));
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document_page) {
|
2010-11-18 14:51:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
document_page->document = document;
|
|
|
|
document_page->data = poppler_document_get_page(pdf_document->document, page);
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!document_page->data) {
|
2010-11-18 14:51:13 +01:00
|
|
|
free(document_page);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
poppler_page_get_size(document_page->data, &(document_page->width), &(document_page->height));
|
|
|
|
|
|
|
|
return document_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref(page->data);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-12-27 09:07:17 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-12-27 09:07:17 +01:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2010-12-27 09:07:17 +01:00
|
|
|
poppler_page_render_to_pixbuf(page->data, 0, 0, page_width, page_height, Zathura.document->scale,
|
|
|
|
Zathura.document->rotate, pixbuf);
|
2010-12-26 01:03:04 +01:00
|
|
|
|
2010-12-27 09:07:17 +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) {
|
2010-12-27 09:07:17 +01:00
|
|
|
g_object_unref(pixbuf);
|
|
|
|
return NULL;
|
2010-12-26 01:03:04 +01:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:07:17 +01:00
|
|
|
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
|
|
|
|
gtk_widget_show(image);
|
2010-12-26 01:03:04 +01:00
|
|
|
|
2010-12-27 09:07:17 +01:00
|
|
|
return image;
|
2010-11-18 03:15:32 +01:00
|
|
|
}
|