summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2010-01-07 14:19:37 +0100
committerTor Andersson <tor@ghostscript.com>2010-01-07 14:19:37 +0100
commit3417c5d2f429b5166a630fc9eb20c9854283ffcc (patch)
tree5c39f489adf4d45bfbf2a9d0e5455e674277aa96
parentff2530fce783480d985a286bc8da3c3d001ed650 (diff)
downloadmupdf-3417c5d2f429b5166a630fc9eb20c9854283ffcc.tar.xz
Refactor csi creation and running.
-rw-r--r--apps/pdfdraw.c5
-rw-r--r--mupdf/mupdf.h5
-rw-r--r--mupdf/pdf_interpret.c54
3 files changed, 39 insertions, 25 deletions
diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c
index 9cba8472..6c66b9d1 100644
--- a/apps/pdfdraw.c
+++ b/apps/pdfdraw.c
@@ -228,10 +228,7 @@ static void drawpnm(int pagenum, struct benchmark *loadtimes, struct benchmark *
fz_device *dev = fz_newdrawdevice(pdf_devicergb, pix);
drawpage->contents->rp = drawpage->contents->bp;
- fz_stream *file = fz_openrbuffer(drawpage->contents);
- pdf_csi *csi = pdf_newcsi(dev, 0, ctm);
- error = pdf_runcsi(csi, xref, drawpage->resources, file);
- fz_dropstream(file);
+ error = pdf_runcontentstream(dev, ctm, 0, xref, drawpage->resources, drawpage->contents);
if (error)
die(error);
fz_free(dev);
diff --git a/mupdf/mupdf.h b/mupdf/mupdf.h
index ad9a4ff2..080dbe22 100644
--- a/mupdf/mupdf.h
+++ b/mupdf/mupdf.h
@@ -713,9 +713,6 @@ void pdf_showimage(pdf_csi*, pdf_image *img);
void pdf_showshade(pdf_csi*, fz_shade *shd);
/* interpret.c */
-pdf_csi *pdf_newcsi(fz_device *dev, int maskonly, fz_matrix ctm);
-fz_error pdf_runcsi(pdf_csi *, pdf_xref *xref, fz_obj *resources, fz_stream *contents);
-void pdf_dropcsi(pdf_csi *csi);
+fz_error pdf_runcontentstream(fz_device *dev, fz_matrix ctm, int maskonly, pdf_xref *xref, fz_obj *resources, fz_buffer *contents);
#endif
-
diff --git a/mupdf/pdf_interpret.c b/mupdf/pdf_interpret.c
index 2af65552..b9700e35 100644
--- a/mupdf/pdf_interpret.c
+++ b/mupdf/pdf_interpret.c
@@ -1,7 +1,10 @@
#include "fitz.h"
#include "mupdf.h"
-pdf_csi *
+static fz_error
+pdf_runcsibuffer(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_buffer *contents);
+
+static pdf_csi *
pdf_newcsi(fz_device *dev, int maskonly, fz_matrix ctm)
{
pdf_csi *csi;
@@ -113,8 +116,8 @@ grestore(pdf_csi *csi)
csi->gtop --;
}
-void
-pdf_dropcsi(pdf_csi *csi)
+static void
+pdf_freecsi(pdf_csi *csi)
{
while (csi->gtop)
grestore(csi);
@@ -143,7 +146,6 @@ pdf_runxobject(pdf_csi *csi, pdf_xref *xref, fz_obj *resources, pdf_xobject *xob
{
fz_error error;
pdf_gstate *gstate;
- fz_stream *file;
gsave(csi);
@@ -186,15 +188,9 @@ pdf_runxobject(pdf_csi *csi, pdf_xref *xref, fz_obj *resources, pdf_xobject *xob
resources = xobj->resources;
xobj->contents->rp = xobj->contents->bp;
- file = fz_openrbuffer(xobj->contents);
-
- error = pdf_runcsi(csi, xref, resources, file);
+ error = pdf_runcsibuffer(csi, xref, resources, xobj->contents);
if (error)
- {
- fz_dropstream(file);
return fz_rethrow(error, "cannot interpret XObject stream");
- }
- fz_dropstream(file);
grestore(csi);
@@ -1206,11 +1202,10 @@ syntaxerror:
return fz_throw("syntaxerror near '%s'", buf);
}
-fz_error
-pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file)
+static fz_error
+pdf_runcsifile(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file, char *buf, int buflen)
{
fz_error error;
- char buf[65536]; // XXX: EEP! way to eat up the stack!
pdf_token_e tok;
int len;
fz_obj *obj;
@@ -1220,7 +1215,7 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file)
if (csi->top == 31)
return fz_throw("stack overflow");
- error = pdf_lex(&tok, file, buf, sizeof buf, &len);
+ error = pdf_lex(&tok, file, buf, buflen, &len);
if (error)
return fz_rethrow(error, "lexical error in content stream");
@@ -1266,7 +1261,7 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file)
break;
case PDF_TODICT:
- error = pdf_parsedict(&csi->stack[csi->top], xref, file, buf, sizeof buf);
+ error = pdf_parsedict(&csi->stack[csi->top], xref, file, buf, buflen);
if (error)
return fz_rethrow(error, "cannot parse dictionary");
csi->top ++;
@@ -1313,7 +1308,7 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file)
fz_obj *obj;
int ch;
- error = pdf_parsedict(&obj, xref, file, buf, sizeof buf);
+ error = pdf_parsedict(&obj, xref, file, buf, buflen);
if (error)
return fz_rethrow(error, "cannot parse inline image dictionary");
@@ -1346,3 +1341,28 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file)
}
}
}
+
+static fz_error
+pdf_runcsibuffer(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_buffer *contents)
+{
+ char *buf = fz_malloc(65536);
+ fz_stream *file = fz_openrbuffer(contents);
+ fz_error error = pdf_runcsifile(csi, xref, rdb, file, buf, 65536);
+ fz_dropstream(file);
+ fz_free(buf);
+ if (error)
+ return fz_rethrow(error, "cannot parse content stream");
+ return fz_okay;
+}
+
+fz_error
+pdf_runcontentstream(fz_device *dev, fz_matrix ctm, int maskonly,
+ pdf_xref *xref, fz_obj *resources, fz_buffer *contents)
+{
+ pdf_csi *csi = pdf_newcsi(dev, maskonly, ctm);
+ fz_error error = pdf_runcsibuffer(csi, xref, resources, contents);
+ pdf_freecsi(csi);
+ if (error)
+ return fz_rethrow(error, "cannot parse content stream");
+ return fz_okay;
+}