summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/image/pdftoepdf.c
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/image/pdftoepdf.c')
-rw-r--r--Build/source/texk/web2c/luatexdir/image/pdftoepdf.c150
1 files changed, 97 insertions, 53 deletions
diff --git a/Build/source/texk/web2c/luatexdir/image/pdftoepdf.c b/Build/source/texk/web2c/luatexdir/image/pdftoepdf.c
index b29493b073d..664bfef18c9 100644
--- a/Build/source/texk/web2c/luatexdir/image/pdftoepdf.c
+++ b/Build/source/texk/web2c/luatexdir/image/pdftoepdf.c
@@ -23,6 +23,11 @@ with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
#define __STDC_FORMAT_MACROS /* for PRId64 etc. */
#include "image/epdf.h"
+#include "luatexcallbackids.h"
+
+/* to be sorted out, we cannot include */
+
+#define xfree(a) do { free(a); a = NULL; } while (0)
/* Conflict with pdfgen.h */
@@ -270,31 +275,37 @@ PdfDocument *refMemStreamPdfDocument(char *docstream, unsigned long long streams
typedef struct ObjMap ObjMap ;
struct ObjMap {
- ppref * in;
+ /* int version; */ /* not really needed */
+ int objnum;
int out_num;
};
static int CompObjMap(const void *pa, const void *pb, void *p)
{
- const ppref *a = (((const ObjMap *) pa)->in);
- const ppref *b = (((const ObjMap *) pb)->in);
- if (a->number > b->number)
+ const ObjMap *a = (((const ObjMap *) pa));
+ const ObjMap *b = (((const ObjMap *) pb));
+ if (a->objnum > b->objnum)
return 1;
- else if (a->number < b->number)
+ else if (a->objnum < b->objnum)
return -1;
+ /*
else if (a->version == b->version)
return 0;
else if (a->version < b->version)
return -1;
return 1;
+ */
+ return 0;
}
static ObjMap *findObjMap(PdfDocument * pdf_doc, ppref * in)
{
ObjMap *obj_map, tmp;
- if (pdf_doc->ObjMapTree == NULL)
+ if (pdf_doc->ObjMapTree == NULL) {
return NULL;
- tmp.in = in;
+ }
+ tmp.objnum = in->number;
+ /* tmp.version = in->version; */
obj_map = (ObjMap *) avl_find(pdf_doc->ObjMapTree, &tmp);
return obj_map;
}
@@ -302,10 +313,12 @@ static ObjMap *findObjMap(PdfDocument * pdf_doc, ppref * in)
static void addObjMap(PdfDocument * pdf_doc, ppref * in, int out_num)
{
ObjMap *obj_map = NULL;
- if (pdf_doc->ObjMapTree == NULL)
+ if (pdf_doc->ObjMapTree == NULL) {
pdf_doc->ObjMapTree = avl_create(CompObjMap, NULL, &avl_xallocator);
+ }
obj_map = (ObjMap*)xmalloc(sizeof(ObjMap));
- obj_map->in = in;
+ /* obj_map->version = in->version; */
+ obj_map->objnum = in->number;
obj_map->out_num = out_num;
avl_probe(pdf_doc->ObjMapTree, obj_map);
}
@@ -332,7 +345,8 @@ static int addInObj(PDF pdf, PdfDocument * pdf_doc, ppref * ref)
return obj_map->out_num;
}
n = (InObj*)xmalloc(sizeof(InObj));
- n->ref = ref;
+ n->objnum = ref->number;
+ /* n->version = ref->version; */
n->next = NULL;
n->num = pdf_create_obj(pdf, obj_type_others, 0);
addObjMap(pdf_doc, ref, n->num);
@@ -408,7 +422,7 @@ static void copyDict(PDF pdf, PdfDocument * pdf_doc, ppdict *dict)
pdf_end_dict(pdf);
}
-static void copyStreamStream(PDF pdf, ppstream * stream, int decode)
+static void copyStreamStream(PDF pdf, ppstream * stream, int decode, int callback_id)
{
uint8_t *data = NULL;
size_t size = 0;
@@ -419,7 +433,18 @@ static void copyStreamStream(PDF pdf, ppstream * stream, int decode)
} else {
data = ppstream_all(stream,&size,decode);
if (data != NULL) {
- pdf_out_block(pdf, (const char *) data, size);
+ /*tex We only do this when we recompress in which case we fetch the whole stream. */
+ if (callback_id == 1) {
+ callback_id = callback_defined(process_pdf_image_content_callback);
+ }
+ if (callback_id) {
+ char *result = NULL;
+ run_callback(callback_id, "S->S",(char *) data,&result);
+ pdf_out_block(pdf, (const char *) (uint8_t *) result, size);
+ xfree(result);
+ } else {
+ pdf_out_block(pdf, (const char *) data, size);
+ }
}
}
ppstream_done(stream);
@@ -429,38 +454,57 @@ static void copyStream(PDF pdf, PdfDocument * pdf_doc, ppstream * stream)
{
ppdict *dict = stream->dict; /* bug in: stream_dict(stream) */
if (pdf->compress_level == 0 || pdf->recompress) {
- const char *ignoredkeys[] = {
- "Filter", "Decode", "Length", "DL", NULL
- };
- int i;
- int n = dict->size;
- pdf_begin_dict(pdf);
- for (i=0; i<n; ++i) {
- const char *key = ppdict_key(dict,i);
- int okay = 1;
+ ppobj * obj = ppdict_get_obj (dict, "Filter");
+ int known = 0;
+ if (obj != NULL && obj->type == PPNAME) {
+ const char *codecs[] = {
+ "ASCIIHexDecode", "ASCII85Decode", "RunLengthDecode",
+ "FlateDecode", "LZWDecode", NULL
+ };
int k;
- for (k = 0; ignoredkeys[k] != NULL; k++) {
- if (strcmp(key,ignoredkeys[k]) == 0) {
- okay = 0;
+ const char *val = ppobj_get_name(obj);
+ for (k = 0; codecs[k] != NULL; k++) {
+ if (strcmp(val,codecs[k]) == 0) {
+ known = 1;
break;
}
}
- if (okay) {
- pdf_add_name(pdf, key);
- copyObject(pdf, pdf_doc, ppdict_at(dict,i));
+ }
+ if (known) {
+ /*tex recompress or keep uncompressed */
+ const char *ignoredkeys[] = {
+ "Filter", "DecodeParms", "Length", "DL", NULL
+ };
+ int i;
+ pdf_begin_dict(pdf);
+ for (i=0; i<dict->size; ++i) {
+ const char *key = ppdict_key(dict,i);
+ int copy = 1;
+ int k;
+ for (k = 0; ignoredkeys[k] != NULL; k++) {
+ if (strcmp(key,ignoredkeys[k]) == 0) {
+ copy = 0;
+ break;
+ }
+ }
+ if (copy) {
+ pdf_add_name(pdf, key);
+ copyObject(pdf, pdf_doc, ppdict_at(dict,i));
+ }
}
+ pdf_dict_add_streaminfo(pdf);
+ pdf_end_dict(pdf);
+ pdf_begin_stream(pdf);
+ copyStreamStream(pdf, stream, 1, 0);
+ pdf_end_stream(pdf);
+ return ;
}
- pdf_dict_add_streaminfo(pdf);
- pdf_end_dict(pdf);
- pdf_begin_stream(pdf);
- copyStreamStream(pdf, stream, 1);
- pdf_end_stream(pdf);
- } else {
- copyDict(pdf, pdf_doc, dict);
- pdf_begin_stream(pdf);
- copyStreamStream(pdf, stream, 0);
- pdf_end_stream(pdf);
}
+ /* copy as-is */
+ copyDict(pdf, pdf_doc, dict);
+ pdf_begin_stream(pdf);
+ copyStreamStream(pdf, stream, 0, 0);
+ pdf_end_stream(pdf);
}
static void copyObject(PDF pdf, PdfDocument * pdf_doc, ppobj * obj)
@@ -505,12 +549,16 @@ static void writeRefs(PDF pdf, PdfDocument * pdf_doc)
{
InObj *r, *n;
ppobj * obj;
+ ppref * ref ;
+ ppxref * xref = ppdoc_xref (pdf_doc->pdfe);
for (r = pdf_doc->inObjList; r != NULL;) {
- obj = ppref_obj(r->ref);
- if (obj->type == PPSTREAM)
+ ref = ppxref_find (xref, (ppuint) r->objnum);
+ obj = ppref_obj(ref);
+ if (obj->type == PPSTREAM) {
pdf_begin_obj(pdf, r->num, OBJSTM_NEVER);
- else
+ } else {
pdf_begin_obj(pdf, r->num, 2);
+ }
copyObject(pdf, pdf_doc, obj);
pdf_end_obj(pdf);
n = r->next;
@@ -520,6 +568,7 @@ static void writeRefs(PDF pdf, PdfDocument * pdf_doc)
}
}
+
/* get the pagebox coordinates according to the pagebox_spec */
static void somebox(ppdict *page, const char * key, pprect * box)
@@ -578,11 +627,6 @@ static ppdict * get_pdf_page_dict(ppdoc *pdfe, int n)
return NULL;
}
-// static ppdict * get_pdf_page_dict(ppdoc *pdfe, int n)
-// {
-// return ppref_obj(ppdoc_page(pdfe,n))->dict;
-// }
-
void read_pdf_info(image_dict * idict)
{
PdfDocument *pdf_doc = NULL;
@@ -592,7 +636,7 @@ void read_pdf_info(image_dict * idict)
ppint rotate = 0;
int pdf_major_version_found = 1;
int pdf_minor_version_found = 3;
- float xsize, ysize, xorig, yorig;
+ double xsize, ysize, xorig, yorig;
if (img_type(idict) == IMG_TYPE_PDF) {
pdf_doc = refPdfDocument(img_filepath(idict), FE_FAIL, img_userpassword(idict), img_ownerpassword(idict));
} else if (img_type(idict) == IMG_TYPE_PDFMEMSTREAM) {
@@ -851,12 +895,12 @@ void write_epdf(PDF pdf, image_dict * idict, int suppress_optional_info)
Write the Page contents.
*/
content = ppdict_rget_obj(pageDict, "Contents");
- if (content->type == PPSTREAM) {
+ if (content && content->type == PPSTREAM) {
if (pdf->compress_level == 0 || pdf->recompress) {
pdf_dict_add_streaminfo(pdf);
pdf_end_dict(pdf);
pdf_begin_stream(pdf);
- copyStreamStream(pdf, content->stream,1); /* decompress */
+ copyStreamStream(pdf, content->stream, 1, 1); /* decompress */
} else {
/* copies compressed stream */
ppstream * stream = content->stream;
@@ -878,18 +922,18 @@ void write_epdf(PDF pdf, image_dict * idict, int suppress_optional_info)
}
*/
}
- pdf_end_dict(pdf);
+ pdf_end_dict(pdf);
pdf_begin_stream(pdf);
- copyStreamStream(pdf, stream,0);
+ copyStreamStream(pdf, stream, 0, 0);
} else {
pdf_dict_add_streaminfo(pdf);
pdf_end_dict(pdf);
pdf_begin_stream(pdf);
- copyStreamStream(pdf, stream,1);
+ copyStreamStream(pdf, stream, 1, 0);
}
}
pdf_end_stream(pdf);
- } else if (content->type == PPARRAY) {
+ } else if (content && content->type == PPARRAY) {
/* listens to compresslevel */
pdf_dict_add_streaminfo(pdf);
pdf_end_dict(pdf);
@@ -913,7 +957,7 @@ void write_epdf(PDF pdf, image_dict * idict, int suppress_optional_info)
} else {
b = 1;
}
- copyStreamStream(pdf, (ppstream *) o->stream,1);
+ copyStreamStream(pdf, (ppstream *) o->stream, 1, 0);
}
}
}