diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2013-02-19 18:45:41 +0100 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2013-02-20 13:50:44 +0000 |
commit | c69a9ace941aa56e815ba9656fb81defdf70d34e (patch) | |
tree | bbcaf31f23ada3926ee491ecca4cdcdd37cf5d95 | |
parent | 62b97d063d4c51307ef4b5f67bd26c2522bca174 (diff) | |
download | mupdf-c69a9ace941aa56e815ba9656fb81defdf70d34e.tar.xz |
Bug 693639: some convenience functions.
Added primarily for use by SumatraPDF.
Thanks to zeniko.
-rw-r--r-- | pdf/mupdf.h | 4 | ||||
-rw-r--r-- | pdf/pdf_object.c | 43 | ||||
-rw-r--r-- | pdf/pdf_parse.c | 29 |
3 files changed, 76 insertions, 0 deletions
diff --git a/pdf/mupdf.h b/pdf/mupdf.h index 16a3053d..83710b84 100644 --- a/pdf/mupdf.h +++ b/pdf/mupdf.h @@ -27,6 +27,8 @@ pdf_obj *pdf_new_matrix(fz_context *ctx, const fz_matrix *mtx); pdf_obj *pdf_copy_array(fz_context *ctx, pdf_obj *array); pdf_obj *pdf_copy_dict(fz_context *ctx, pdf_obj *dict); +pdf_obj *pdf_new_obj_from_str(fz_context *ctx, const char *src); + pdf_obj *pdf_keep_obj(pdf_obj *obj); void pdf_drop_obj(pdf_obj *obj); @@ -64,6 +66,7 @@ int pdf_array_len(pdf_obj *array); pdf_obj *pdf_array_get(pdf_obj *array, int i); void pdf_array_put(pdf_obj *array, int i, pdf_obj *obj); void pdf_array_push(pdf_obj *array, pdf_obj *obj); +void pdf_array_push_drop(pdf_obj *array, pdf_obj *obj); void pdf_array_insert(pdf_obj *array, pdf_obj *obj); int pdf_array_contains(pdf_obj *array, pdf_obj *obj); @@ -94,6 +97,7 @@ char *pdf_to_utf8(pdf_document *xref, pdf_obj *src); unsigned short *pdf_to_ucs2(pdf_document *xref, pdf_obj *src); /* sumatrapdf */ pdf_obj *pdf_to_utf8_name(pdf_document *xref, pdf_obj *src); char *pdf_from_ucs2(pdf_document *xref, unsigned short *str); +void pdf_to_ucs2_buf(unsigned short *buffer, pdf_obj *src); fz_rect *pdf_to_rect(fz_context *ctx, pdf_obj *array, fz_rect *rect); fz_matrix *pdf_to_matrix(fz_context *ctx, pdf_obj *array, fz_matrix *mat); diff --git a/pdf/pdf_object.c b/pdf/pdf_object.c index a2d9d37b..c3092afe 100644 --- a/pdf/pdf_object.c +++ b/pdf/pdf_object.c @@ -544,6 +544,25 @@ pdf_array_push(pdf_obj *obj, pdf_obj *item) } void +pdf_array_push_drop(pdf_obj *obj, pdf_obj *item) +{ + fz_context *ctx = obj->ctx; + + fz_try(ctx) + { + pdf_array_push(obj, item); + } + fz_always(ctx) + { + pdf_drop_obj(item); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } +} + +void pdf_array_insert(pdf_obj *obj, pdf_obj *item) { RESOLVE(obj); @@ -1191,6 +1210,30 @@ pdf_drop_obj(pdf_obj *obj) fz_free(obj->ctx, obj); } +pdf_obj *pdf_new_obj_from_str(fz_context *ctx, const char *src) +{ + pdf_obj *result; + pdf_lexbuf lexbuf; + fz_stream *stream = fz_open_memory(ctx, (unsigned char *)src, strlen(src)); + + pdf_lexbuf_init(ctx, &lexbuf, PDF_LEXBUF_SMALL); + fz_try(ctx) + { + result = pdf_parse_stm_obj(NULL, stream, &lexbuf); + } + fz_always(ctx) + { + pdf_lexbuf_fin(&lexbuf); + fz_close(stream); + } + fz_catch(ctx) + { + return NULL; + } + + return result; +} + /* Pretty printing objects */ struct fmt diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c index 365c9ffd..373ad0e8 100644 --- a/pdf/pdf_parse.c +++ b/pdf/pdf_parse.c @@ -150,6 +150,35 @@ pdf_to_ucs2(pdf_document *xref, pdf_obj *src) return dst; } +/* allow to convert to UCS-2 without the need for an fz_context */ +/* (buffer must be at least (fz_to_str_len(src) + 1) * 2 bytes in size) */ +void +pdf_to_ucs2_buf(unsigned short *buffer, pdf_obj *src) +{ + unsigned char *srcptr = (unsigned char *) pdf_to_str_buf(src); + unsigned short *dstptr = buffer; + int srclen = pdf_to_str_len(src); + int i; + + if (srclen >= 2 && srcptr[0] == 254 && srcptr[1] == 255) + { + for (i = 2; i + 1 < srclen; i += 2) + *dstptr++ = srcptr[i] << 8 | srcptr[i+1]; + } + else if (srclen >= 2 && srcptr[0] == 255 && srcptr[1] == 254) + { + for (i = 2; i + 1 < srclen; i += 2) + *dstptr++ = srcptr[i] | srcptr[i+1] << 8; + } + else + { + for (i = 0; i < srclen; i++) + *dstptr++ = pdf_doc_encoding[srcptr[i]]; + } + + *dstptr = '\0'; +} + /* Convert UCS-2 string into PdfDocEncoding for authentication */ char * pdf_from_ucs2(pdf_document *xref, unsigned short *src) |