diff options
Diffstat (limited to 'src/pdf_bridge.c')
| -rw-r--r-- | src/pdf_bridge.c | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/pdf_bridge.c b/src/pdf_bridge.c index bd72a54a..8e433fde 100644 --- a/src/pdf_bridge.c +++ b/src/pdf_bridge.c @@ -316,15 +316,17 @@ pardes_pdf_grown_capacity(size_t current, size_t needed, size_t maximum) return capacity; } -pardes_pdf_document * -pardes_pdf_open(const char *path, int *page_count) +static pardes_pdf_document * +pardes_pdf_open_source(const char *path, const unsigned char *bytes, size_t length, int *page_count) { pardes_pdf_document *state; fz_context *ctx; fz_document *doc = NULL; + fz_buffer *buffer = NULL; + fz_stream *stream = NULL; int pages = 0; - if (path == NULL || page_count == NULL) + if ((path == NULL && bytes == NULL) || page_count == NULL) return NULL; state = pardes_pdf_allocate_document(); @@ -344,14 +346,27 @@ pardes_pdf_open(const char *path, int *page_count) } fz_var(doc); + fz_var(buffer); + fz_var(stream); fz_try(ctx) { fz_register_document_handlers(ctx); - doc = fz_open_document(ctx, path); + if (path != NULL) { + doc = fz_open_document(ctx, path); + } else { + buffer = fz_new_buffer_from_copied_data(ctx, bytes, length); + stream = fz_open_buffer(ctx, buffer); + doc = fz_open_document_with_stream(ctx, "application/pdf", stream); + } pages = fz_count_pages(ctx, doc); if (pages < 1) fz_throw(ctx, FZ_ERROR_FORMAT, "PDF has no pages"); } + fz_always(ctx) + { + fz_drop_stream(ctx, stream); + fz_drop_buffer(ctx, buffer); + } fz_catch(ctx) { fz_report_error(ctx); @@ -369,6 +384,18 @@ pardes_pdf_open(const char *path, int *page_count) return state; } +pardes_pdf_document * +pardes_pdf_open(const char *path, int *page_count) +{ + return pardes_pdf_open_source(path, NULL, 0, page_count); +} + +pardes_pdf_document * +pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count) +{ + return pardes_pdf_open_source(NULL, bytes, length, page_count); +} + void pardes_pdf_close(pardes_pdf_document *document) { |
