diff options
author | Vincent Sanders <vince@kyllikki.org> | 2017-12-31 15:21:09 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2020-12-11 16:17:27 +0000 |
commit | b441a51b78b7ef852a4aac586ab233788204760e (patch) | |
tree | 7b0d858da3ff27ab0e15735b79484b2d852b0fed /content/handlers/pdf/pdf.c | |
parent | 399d7189f21ad41bd80e11d5ff8da4037edfc1c2 (diff) | |
download | netsurf-b441a51b78b7ef852a4aac586ab233788204760e.tar.gz netsurf-b441a51b78b7ef852a4aac586ab233788204760e.tar.bz2 |
use nspdf library to parse document
Diffstat (limited to 'content/handlers/pdf/pdf.c')
-rw-r--r-- | content/handlers/pdf/pdf.c | 80 |
1 files changed, 69 insertions, 11 deletions
diff --git a/content/handlers/pdf/pdf.c b/content/handlers/pdf/pdf.c index 23457a26a..657a5e978 100644 --- a/content/handlers/pdf/pdf.c +++ b/content/handlers/pdf/pdf.c @@ -27,12 +27,39 @@ #include <stdbool.h> #include <stdlib.h> +#include <nspdf/document.h> + #include "utils/utils.h" #include "content/llcache.h" #include "content/content_protected.h" #include "pdf.h" +typedef struct pdf_content { + struct content base; + + struct nspdf_doc *doc; +} pdf_content; + +static nserror nspdf2nserr(nspdferror nspdferr) +{ + nserror res; + switch (nspdferr) { + case NSPDFERROR_OK: + res = NSERROR_OK; + break; + + case NSPDFERROR_NOMEM: + res = NSERROR_NOMEM; + break; + + default: + res = NSERROR_UNKNOWN; + break; + } + return res; +} + /** * Content create entry point. */ @@ -45,32 +72,63 @@ pdf_create(const content_handler *handler, bool quirks, struct content **c) { - struct content *jpeg; - nserror error; + struct pdf_content *pdfc; + nserror res; + nspdferror pdfres; - jpeg = calloc(1, sizeof(struct content)); - if (jpeg == NULL) + pdfc = calloc(1, sizeof(struct pdf_content)); + if (pdfc == NULL) { return NSERROR_NOMEM; + } + + res = content__init(&pdfc->base, + handler, + imime_type, + params, + llcache, + fallback_charset, + quirks); + if (res != NSERROR_OK) { + free(pdfc); + return res; + } - error = content__init(jpeg, handler, imime_type, params, - llcache, fallback_charset, quirks); - if (error != NSERROR_OK) { - free(jpeg); - return error; + pdfres = nspdf_document_create(&pdfc->doc); + if (pdfres != NSPDFERROR_OK) { + free(pdfc); + return nspdf2nserr(res); } - *c = jpeg; + *c = (struct content *)pdfc; return NSERROR_OK; } /* exported interface documented in image_cache.h */ -static void pdf_destroy(struct content *content) +static void pdf_destroy(struct content *c) { + struct pdf_content *pdfc = (struct pdf_content *)c; + nspdf_document_destroy(pdfc->doc); } static bool pdf_convert(struct content *c) { + struct pdf_content *pdfc = (struct pdf_content *)c; + nspdferror pdfres; + const uint8_t *content_data; + unsigned long content_length; + + content_data = (const uint8_t *)content__get_source_data(c, + &content_length); + + pdfres = nspdf_document_parse(pdfc->doc, + content_data, + content_length); + if (pdfres != NSPDFERROR_OK) { + content_broadcast_errorcode(c, NSERROR_INVALID); + return false; + } + content_set_ready(c); content_set_done(c); return true; |