From 7c6b0819845b1ae4db34981a3fa5fb53b36431c8 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Thu, 23 Dec 2010 20:26:36 +0100 Subject: [PATCH] Revised pdf/djvu open functions --- ft/djvu/djvu.c | 62 ++++++++++++++++++++++++++++++++++++++++---------- ft/djvu/djvu.h | 1 + ft/pdf/pdf.c | 34 ++++++++++++++++++--------- 3 files changed, 74 insertions(+), 23 deletions(-) diff --git a/ft/djvu/djvu.c b/ft/djvu/djvu.c index d323cde..de965ba 100644 --- a/ft/djvu/djvu.c +++ b/ft/djvu/djvu.c @@ -8,7 +8,7 @@ bool djvu_document_open(zathura_document_t* document) { if(!document) { - return false; + goto error_out; } document->functions.document_free = djvu_document_free; @@ -24,30 +24,57 @@ djvu_document_open(zathura_document_t* document) document->data = malloc(sizeof(djvu_document_t)); if(!document->data) { - return false; + goto error_out; } djvu_document_t* djvu_document = (djvu_document_t*) document->data; - djvu_document->context = ddjvu_context_create("zathura"); + djvu_document->context = NULL; + djvu_document->document = NULL; + djvu_document->format = NULL; - if(!djvu_document->context) { - free(document->data); - document->data = NULL; - return false; + /* setup format */ + djvu_document->format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 0, NULL); + + if(!djvu_document->format) { + goto error_free; } + ddjvu_format_set_row_order(djvu_document->format, TRUE); + + /* setup context */ + djvu_document->context = ddjvu_context_create("zathura"); + + if(!djvu_document->context) { + goto error_free; + } + + /* setup document */ djvu_document->document = ddjvu_document_create_by_filename(djvu_document->context, document->file_path, FALSE); if(!djvu_document->document) { - ddjvu_context_release(djvu_document->context); - free(document->data); - document->data = NULL; - return false; + goto error_free; } document->number_of_pages = ddjvu_document_get_pagenum(djvu_document->document); return true; + +error_free: + + if(djvu_document->format) { + ddjvu_format_release(djvu_document->format); + } + + if(djvu_document->context) { + ddjvu_context_release(djvu_document->context); + } + + free(document->data); + document->data = NULL; + +error_out: + + return false; } bool @@ -61,6 +88,7 @@ djvu_document_free(zathura_document_t* document) djvu_document_t* djvu_document = (djvu_document_t*) document->data; ddjvu_context_release(djvu_document->context); ddjvu_document_release(djvu_document->document); + ddjvu_format_release(djvu_document->format); free(document->data); } @@ -80,7 +108,17 @@ djvu_document_save_as(zathura_document_t* document, const char* path) return false; } - return false; + djvu_document_t* djvu_document = (djvu_document_t*) document->data; + + FILE* fp = fopen(path, "w"); + if(!fp) { + return false; + } + + ddjvu_document_save(djvu_document->document, fp, 0, NULL); + fclose(fp); + + return true; } zathura_list_t* diff --git a/ft/djvu/djvu.h b/ft/djvu/djvu.h index 92f94c8..6ece4a6 100644 --- a/ft/djvu/djvu.h +++ b/ft/djvu/djvu.h @@ -12,6 +12,7 @@ typedef struct djvu_document_s { ddjvu_context_t* context; ddjvu_document_t* document; + ddjvu_format_t* format; } djvu_document_t; bool djvu_document_open(zathura_document_t* document); diff --git a/ft/pdf/pdf.c b/ft/pdf/pdf.c index 2928dae..d43ffb9 100644 --- a/ft/pdf/pdf.c +++ b/ft/pdf/pdf.c @@ -7,8 +7,9 @@ bool pdf_document_open(zathura_document_t* document) { - if(!document) - return false; + if(!document) { + goto error_out; + } document->functions.document_free = pdf_document_free; document->functions.document_index_generate = pdf_document_index_generate;; @@ -23,7 +24,7 @@ pdf_document_open(zathura_document_t* document) document->data = malloc(sizeof(pdf_document_t)); if(!document->data) { - return false; + goto error_out; } /* format path */ @@ -32,10 +33,7 @@ pdf_document_open(zathura_document_t* document) if(!file_uri) { fprintf(stderr, "error: could not open file: %s\n", error->message); - g_error_free(error); - free(document->data); - document->data = NULL; - return false; + goto error_free; } pdf_document_t* pdf_document = (pdf_document_t*) document->data; @@ -43,15 +41,29 @@ pdf_document_open(zathura_document_t* document) if(!pdf_document->document) { fprintf(stderr, "error: could not open file: %s\n", error->message); - g_error_free(error); - free(document->data); - document->data = NULL; - return false; + goto error_free; } document->number_of_pages = poppler_document_get_n_pages(pdf_document->document); return true; + +error_free: + + if(error) { + g_error_free(error); + } + + if(file_uri) { + g_free(file_uri); + } + + free(document->data); + document->data = NULL; + +error_out: + + return false; } bool