zathura/ft/djvu/djvu.c

323 lines
7.2 KiB
C
Raw Normal View History

2010-12-23 19:41:07 +01:00
/* See LICENSE file for license and copyright information */
#include <stdlib.h>
#include "djvu.h"
2010-12-26 00:47:42 +01:00
#include "../../zathura.h"
2010-12-23 19:41:07 +01:00
bool
plugin_register(void)
{
zathura_document_plugin_register("djvu", djvu_document_open);
zathura_document_plugin_register("DJVU", djvu_document_open);
return true;
}
2010-12-23 19:41:07 +01:00
bool
djvu_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-12-23 19:41:07 +01:00
}
document->functions.document_free = djvu_document_free;
document->functions.document_index_generate = djvu_document_index_generate;;
document->functions.document_save_as = djvu_document_save_as;
document->functions.document_attachments_get = djvu_document_attachments_get;
document->functions.page_get = djvu_page_get;
document->functions.page_search_text = djvu_page_search_text;
document->functions.page_links_get = djvu_page_links_get;
document->functions.page_form_fields_get = djvu_page_form_fields_get;
document->functions.page_render = djvu_page_render;
document->functions.page_free = djvu_page_free;
document->data = malloc(sizeof(djvu_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-12-23 19:41:07 +01:00
}
djvu_document_t* djvu_document = (djvu_document_t*) document->data;
2010-12-23 20:26:36 +01:00
djvu_document->context = NULL;
djvu_document->document = NULL;
djvu_document->format = NULL;
/* setup format */
2010-12-26 00:28:53 +01:00
djvu_document->format = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, NULL);
2010-12-23 20:26:36 +01:00
2011-02-09 12:29:09 +01:00
if (!djvu_document->format) {
2010-12-23 20:26:36 +01:00
goto error_free;
}
ddjvu_format_set_row_order(djvu_document->format, TRUE);
/* setup context */
djvu_document->context = ddjvu_context_create("zathura");
2010-12-23 19:41:07 +01:00
2011-02-09 12:29:09 +01:00
if (!djvu_document->context) {
2010-12-23 20:26:36 +01:00
goto error_free;
2010-12-23 19:41:07 +01:00
}
2010-12-23 20:26:36 +01:00
/* setup document */
2010-12-23 19:41:07 +01:00
djvu_document->document = ddjvu_document_create_by_filename(djvu_document->context, document->file_path, FALSE);
2011-02-09 12:29:09 +01:00
if (!djvu_document->document) {
2010-12-23 20:26:36 +01:00
goto error_free;
2010-12-23 19:41:07 +01:00
}
2010-12-27 09:22:08 +01:00
/* load document info */
ddjvu_message_t* msg;
ddjvu_message_wait(djvu_document->context);
2011-02-09 12:29:09 +01:00
while ((msg = ddjvu_message_peek(djvu_document->context)) && (msg->m_any.tag != DDJVU_DOCINFO)) {
if (msg->m_any.tag == DDJVU_ERROR) {
2010-12-27 09:22:08 +01:00
goto error_free;
}
ddjvu_message_pop(djvu_document->context);
}
/* decoding error */
2011-02-09 12:29:09 +01:00
if (ddjvu_document_decoding_error(djvu_document->document)) {
2010-12-27 09:22:08 +01:00
goto error_free;
}
2010-12-23 19:41:07 +01:00
document->number_of_pages = ddjvu_document_get_pagenum(djvu_document->document);
return true;
2010-12-23 20:26:36 +01:00
error_free:
2011-02-09 12:29:09 +01:00
if (djvu_document->format) {
2010-12-23 20:26:36 +01:00
ddjvu_format_release(djvu_document->format);
}
2011-02-09 12:29:09 +01:00
if (djvu_document->context) {
2010-12-23 20:26:36 +01:00
ddjvu_context_release(djvu_document->context);
}
free(document->data);
document->data = NULL;
error_out:
return false;
2010-12-23 19:41:07 +01:00
}
bool
djvu_document_free(zathura_document_t* document)
{
2011-02-09 12:29:09 +01:00
if (!document) {
2010-12-23 19:41:07 +01:00
return false;
}
2011-02-09 12:29:09 +01:00
if (document->data) {
2010-12-23 19:45:06 +01:00
djvu_document_t* djvu_document = (djvu_document_t*) document->data;
ddjvu_context_release(djvu_document->context);
ddjvu_document_release(djvu_document->document);
2010-12-23 20:26:36 +01:00
ddjvu_format_release(djvu_document->format);
2010-12-23 19:45:06 +01:00
free(document->data);
}
2010-12-23 19:41:07 +01:00
return true;
}
girara_tree_node_t*
2010-12-23 19:41:07 +01:00
djvu_document_index_generate(zathura_document_t* document)
{
return NULL;
}
bool
djvu_document_save_as(zathura_document_t* document, const char* path)
{
2011-02-09 12:29:09 +01:00
if (!document || !document->data || !path) {
2010-12-23 19:41:07 +01:00
return false;
}
2010-12-23 20:26:36 +01:00
djvu_document_t* djvu_document = (djvu_document_t*) document->data;
FILE* fp = fopen(path, "w");
2011-02-09 12:29:09 +01:00
if (!fp) {
2010-12-23 20:26:36 +01:00
return false;
}
ddjvu_document_save(djvu_document->document, fp, 0, NULL);
fclose(fp);
return true;
2010-12-23 19:41:07 +01:00
}
zathura_list_t*
djvu_document_attachments_get(zathura_document_t* document)
{
return NULL;
}
zathura_page_t*
djvu_page_get(zathura_document_t* document, unsigned int page)
{
2011-02-09 12:29:09 +01:00
if (!document || !document->data) {
2010-12-23 19:41:07 +01:00
return NULL;
}
2010-12-26 00:28:53 +01:00
djvu_document_t* djvu_document = (djvu_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-12-26 00:28:53 +01:00
return NULL;
}
document_page->document = document;
2011-01-05 23:37:47 +01:00
document_page->data = NULL;
2010-12-26 00:28:53 +01:00
2011-01-05 23:37:47 +01:00
ddjvu_status_t status;
ddjvu_pageinfo_t page_info;
2011-02-09 12:29:09 +01:00
while ((status = ddjvu_document_get_pageinfo(djvu_document->document, page, &page_info)) < DDJVU_JOB_OK);
2011-01-05 23:37:47 +01:00
2011-02-09 12:29:09 +01:00
if (status >= DDJVU_JOB_FAILED) {
2010-12-26 00:28:53 +01:00
free(document_page);
return NULL;
}
2011-01-05 23:37:47 +01:00
document_page->width = 0.2 * page_info.width;
document_page->height = 0.2 * page_info.height;
2010-12-26 00:28:53 +01:00
return document_page;
2010-12-23 19:41:07 +01:00
}
bool
djvu_page_free(zathura_page_t* page)
{
2011-02-09 12:29:09 +01:00
if (!page) {
2010-12-23 19:41:07 +01:00
return false;
}
2010-12-26 00:28:53 +01:00
free(page);
2010-12-23 19:41:07 +01:00
return true;
}
zathura_list_t*
djvu_page_search_text(zathura_page_t* page, const char* text)
{
return NULL;
}
zathura_list_t*
djvu_page_links_get(zathura_page_t* page)
{
return NULL;
}
zathura_list_t*
djvu_page_form_fields_get(zathura_page_t* page)
{
return NULL;
}
GtkWidget*
2010-12-23 19:41:07 +01:00
djvu_page_render(zathura_page_t* page)
{
2011-02-09 12:29:09 +01:00
if (!Zathura.document || !page || !page->document) {
2010-12-26 00:47:42 +01:00
return NULL;
}
/* calculate sizes */
2010-12-26 01:03:04 +01:00
unsigned int page_width = Zathura.document->scale * page->width;
unsigned int page_height = Zathura.document->scale * page->height;
2010-12-26 00:47:42 +01:00
2011-02-09 12:29:09 +01:00
if (!page_width || !page_height) {
2011-01-05 23:37:47 +01:00
goto error_out;
}
2010-12-26 00:47:42 +01:00
/* init ddjvu render data */
djvu_document_t* djvu_document = (djvu_document_t*) page->document->data;
2011-01-05 23:37:47 +01:00
ddjvu_page_t* djvu_page = ddjvu_page_create_by_pageno(djvu_document->document, page->number);
2011-02-09 12:29:09 +01:00
if (!djvu_page) {
2011-01-05 23:37:47 +01:00
goto error_out;
}
2011-02-09 12:29:09 +01:00
while (!ddjvu_page_decoding_done(djvu_page));
2010-12-26 00:47:42 +01:00
ddjvu_rect_t rrect = { 0, 0, page_width, page_height };
ddjvu_rect_t prect = { 0, 0, page_width, page_height };
guchar* buffer = malloc(sizeof(char) * (page_width * page_height * 3));
2011-02-09 12:29:09 +01:00
if (!buffer) {
2011-01-05 23:37:47 +01:00
goto error_free;
}
2010-12-26 00:47:42 +01:00
2010-12-27 09:49:22 +01:00
/* set rotation */
GdkPixbufRotation gdk_angle = GDK_PIXBUF_ROTATE_NONE;
ddjvu_page_rotation_t ddjvu_angle = DDJVU_ROTATE_0;
switch(Zathura.document->rotate) {
case 90:
gdk_angle = GDK_PIXBUF_ROTATE_CLOCKWISE;
ddjvu_angle = DDJVU_ROTATE_90;
break;
case 180:
gdk_angle = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
ddjvu_angle = DDJVU_ROTATE_180;
break;
case 270:
gdk_angle = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
ddjvu_angle = DDJVU_ROTATE_270;
break;
default:
gdk_angle = GDK_PIXBUF_ROTATE_NONE;
ddjvu_angle = DDJVU_ROTATE_0;
break;
}
2011-01-05 23:37:47 +01:00
ddjvu_page_set_rotation(djvu_page, ddjvu_angle);
2010-12-26 00:54:07 +01:00
/* render page */
2011-01-05 23:37:47 +01:00
ddjvu_page_render(djvu_page, DDJVU_RENDER_COLOR, &prect, &rrect, djvu_document->format,
3 * page_width, (char*) buffer);
2010-12-26 00:54:07 +01:00
/* create pixbuf */
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(buffer, GDK_COLORSPACE_RGB, FALSE, 8,
page_width, page_height, 3 * page_width, NULL, NULL);
2010-12-26 00:54:07 +01:00
2011-02-09 12:29:09 +01:00
if (!pixbuf) {
2010-12-27 09:44:28 +01:00
goto error_free;
}
/* rotate page */
2011-02-09 12:29:09 +01:00
if (Zathura.document->rotate != 0) {
2010-12-27 10:24:12 +01:00
GdkPixbuf* pixbuf_temp = gdk_pixbuf_rotate_simple(pixbuf, gdk_angle);
2011-02-09 12:29:09 +01:00
if (!pixbuf_temp) {
2010-12-27 09:44:28 +01:00
goto error_free;
}
pixbuf = pixbuf_temp;
2010-12-26 00:47:42 +01:00
}
GtkWidget* image = gtk_image_new();
2010-12-26 00:47:42 +01:00
2011-02-09 12:29:09 +01:00
if (!image) {
2010-12-27 09:44:28 +01:00
goto error_free;
2010-12-26 00:47:42 +01:00
}
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
gtk_widget_show(image);
2010-12-26 00:47:42 +01:00
2011-01-05 23:37:47 +01:00
ddjvu_page_release(djvu_page);
return image;
2010-12-26 00:47:42 +01:00
2010-12-27 09:44:28 +01:00
error_free:
2011-01-05 23:37:47 +01:00
ddjvu_page_release(djvu_page);
2010-12-27 09:44:28 +01:00
free(buffer);
g_object_unref(pixbuf);
error_out:
2010-12-26 00:47:42 +01:00
return NULL;
2010-12-23 19:41:07 +01:00
}