Revised pdf/djvu open functions

This commit is contained in:
Moritz Lipp 2010-12-23 20:26:36 +01:00
parent 876f1e9ead
commit 7c6b081984
3 changed files with 74 additions and 23 deletions

View file

@ -8,7 +8,7 @@ bool
djvu_document_open(zathura_document_t* document) djvu_document_open(zathura_document_t* document)
{ {
if(!document) { if(!document) {
return false; goto error_out;
} }
document->functions.document_free = djvu_document_free; 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)); document->data = malloc(sizeof(djvu_document_t));
if(!document->data) { if(!document->data) {
return false; goto error_out;
} }
djvu_document_t* djvu_document = (djvu_document_t*) document->data; 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) { /* setup format */
free(document->data); djvu_document->format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 0, NULL);
document->data = NULL;
return false; 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); djvu_document->document = ddjvu_document_create_by_filename(djvu_document->context, document->file_path, FALSE);
if(!djvu_document->document) { if(!djvu_document->document) {
ddjvu_context_release(djvu_document->context); goto error_free;
free(document->data);
document->data = NULL;
return false;
} }
document->number_of_pages = ddjvu_document_get_pagenum(djvu_document->document); document->number_of_pages = ddjvu_document_get_pagenum(djvu_document->document);
return true; 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 bool
@ -61,6 +88,7 @@ djvu_document_free(zathura_document_t* document)
djvu_document_t* djvu_document = (djvu_document_t*) document->data; djvu_document_t* djvu_document = (djvu_document_t*) document->data;
ddjvu_context_release(djvu_document->context); ddjvu_context_release(djvu_document->context);
ddjvu_document_release(djvu_document->document); ddjvu_document_release(djvu_document->document);
ddjvu_format_release(djvu_document->format);
free(document->data); free(document->data);
} }
@ -80,7 +108,17 @@ djvu_document_save_as(zathura_document_t* document, const char* path)
return false; 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* zathura_list_t*

View file

@ -12,6 +12,7 @@ typedef struct djvu_document_s
{ {
ddjvu_context_t* context; ddjvu_context_t* context;
ddjvu_document_t* document; ddjvu_document_t* document;
ddjvu_format_t* format;
} djvu_document_t; } djvu_document_t;
bool djvu_document_open(zathura_document_t* document); bool djvu_document_open(zathura_document_t* document);

View file

@ -7,8 +7,9 @@
bool bool
pdf_document_open(zathura_document_t* document) pdf_document_open(zathura_document_t* document)
{ {
if(!document) if(!document) {
return false; goto error_out;
}
document->functions.document_free = pdf_document_free; document->functions.document_free = pdf_document_free;
document->functions.document_index_generate = pdf_document_index_generate;; 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)); document->data = malloc(sizeof(pdf_document_t));
if(!document->data) { if(!document->data) {
return false; goto error_out;
} }
/* format path */ /* format path */
@ -32,10 +33,7 @@ pdf_document_open(zathura_document_t* document)
if(!file_uri) { if(!file_uri) {
fprintf(stderr, "error: could not open file: %s\n", error->message); fprintf(stderr, "error: could not open file: %s\n", error->message);
g_error_free(error); goto error_free;
free(document->data);
document->data = NULL;
return false;
} }
pdf_document_t* pdf_document = (pdf_document_t*) document->data; 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) { if(!pdf_document->document) {
fprintf(stderr, "error: could not open file: %s\n", error->message); fprintf(stderr, "error: could not open file: %s\n", error->message);
g_error_free(error); goto error_free;
free(document->data);
document->data = NULL;
return false;
} }
document->number_of_pages = poppler_document_get_n_pages(pdf_document->document); document->number_of_pages = poppler_document_get_n_pages(pdf_document->document);
return true; 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 bool