summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/fitz.h30
-rw-r--r--include/fitz/base.h59
-rw-r--r--include/fitz/cmap.h25
-rw-r--r--include/fitz/crypt.h30
-rw-r--r--include/fitz/file.h33
-rw-r--r--include/fitz/filter.h80
-rw-r--r--include/fitz/font.h63
-rw-r--r--include/fitz/geometry.h56
-rw-r--r--include/fitz/hash.h15
-rw-r--r--include/fitz/image.h17
-rw-r--r--include/fitz/math.h30
-rw-r--r--include/fitz/object.h124
-rw-r--r--include/fitz/path.h57
-rw-r--r--include/fitz/pixmap.h22
-rw-r--r--include/fitz/render.h11
-rw-r--r--include/fitz/scanconv.h46
-rw-r--r--include/fitz/sysdep.h48
-rw-r--r--include/fitz/text.h23
-rw-r--r--include/fitz/tree.h156
-rw-r--r--include/mupdf.h253
20 files changed, 1178 insertions, 0 deletions
diff --git a/include/fitz.h b/include/fitz.h
new file mode 100644
index 00000000..20c105db
--- /dev/null
+++ b/include/fitz.h
@@ -0,0 +1,30 @@
+#ifdef _FITZ_H_
+#error "fitz.h must only be included once"
+#endif
+#define _FITZ_H_
+
+#include "fitz/sysdep.h"
+
+#include "fitz/base.h"
+#include "fitz/math.h"
+#include "fitz/geometry.h"
+#include "fitz/hash.h"
+#include "fitz/crypt.h"
+
+#include "fitz/object.h"
+
+#include "fitz/filter.h"
+#include "fitz/file.h"
+
+#include "fitz/cmap.h"
+#include "fitz/font.h"
+
+#include "fitz/tree.h"
+#include "fitz/path.h"
+#include "fitz/text.h"
+#include "fitz/image.h"
+
+#include "fitz/pixmap.h"
+#include "fitz/scanconv.h"
+#include "fitz/render.h"
+
diff --git a/include/fitz/base.h b/include/fitz/base.h
new file mode 100644
index 00000000..36fe0aeb
--- /dev/null
+++ b/include/fitz/base.h
@@ -0,0 +1,59 @@
+#undef nil
+#define nil ((void*)0)
+
+#undef offsetof
+#define offsetof(s, m) (unsigned long)(&(((s*)0)->m))
+
+#undef ABS
+#define ABS(x) ( (x) < 0 ? -(x) : (x) )
+
+#undef MAX
+#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
+
+#undef MIN
+#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
+
+#undef CLAMP
+#define CLAMP(x,a,b) ( (x) > (b) ? (b) : ( (x) < (a) ? (a) : (x) ) )
+
+#define MAX4(a,b,c,d) MAX(MAX(a,b), MAX(c,d))
+#define MIN4(a,b,c,d) MIN(MIN(a,b), MIN(c,d))
+
+#define STRIDE(n, bcp) (((bpc) * (n) + 7) / 8)
+
+typedef struct fz_error_s fz_error;
+
+struct fz_error_s
+{
+ char msg[184];
+ char file[32];
+ char func[32];
+ int line;
+ int frozen;
+};
+
+#define fz_outofmem (&fz_koutofmem)
+extern fz_error fz_koutofmem;
+
+#define fz_throw(fmt, ...) fz_throw0(__func__, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
+fz_error *fz_throw0(const char *func, const char *file, int line, char *fmt, ...);
+void fz_warn(char *fmt, ...);
+void fz_abort(fz_error *eo);
+void fz_freeerror(fz_error *eo);
+
+typedef struct fz_memorycontext_s fz_memorycontext;
+
+struct fz_memorycontext_s
+{
+ void * (*malloc)(fz_memorycontext *, int);
+ void * (*realloc)(fz_memorycontext *, void *, int);
+ void (*free)(fz_memorycontext *, void *);
+};
+
+fz_memorycontext *fz_currentmemorycontext(void);
+void fz_setmemorycontext(fz_memorycontext *memorycontext);
+
+void *fz_malloc(int n);
+void *fz_realloc(void *p, int n);
+void fz_free(void *p);
+
diff --git a/include/fitz/cmap.h b/include/fitz/cmap.h
new file mode 100644
index 00000000..ddd37149
--- /dev/null
+++ b/include/fitz/cmap.h
@@ -0,0 +1,25 @@
+typedef struct fz_cmap_s fz_cmap;
+
+fz_error *fz_newcmap(fz_cmap **cmapp);
+void fz_debugcmap(fz_cmap *cmap);
+void fz_freecmap(fz_cmap *cmap);
+
+char *fz_getcmapname(fz_cmap *cmap);
+void fz_setcmapname(fz_cmap *cmap, char *name);
+char *fz_getusecmapname(fz_cmap *cmap);
+void fz_setusecmapname(fz_cmap *cmap, char *usecmap);
+void fz_setusecmap(fz_cmap *cmap, fz_cmap *usecmap);
+fz_cmap *fz_getusecmap(fz_cmap *cmap);
+void fz_setwmode(fz_cmap *cmap, int wmode);
+int fz_getwmode(fz_cmap *cmap);
+
+fz_error *fz_addcodespacerange(fz_cmap *cmap, unsigned lo, unsigned hi, int n);
+
+fz_error *fz_setcidlookup(fz_cmap *cmap, int map[256]);
+
+fz_error *fz_addcidrange(fz_cmap *cmap, int srclo, int srchi, int dstlo);
+fz_error *fz_endcidrange(fz_cmap *cmap);
+
+int fz_lookupcid(fz_cmap *cmap, int cpt);
+char *fz_decodecpt(fz_cmap *cmap, unsigned char *s, int *cpt);
+
diff --git a/include/fitz/crypt.h b/include/fitz/crypt.h
new file mode 100644
index 00000000..f6365c4e
--- /dev/null
+++ b/include/fitz/crypt.h
@@ -0,0 +1,30 @@
+/* md5 digests */
+
+typedef struct fz_md5_s fz_md5;
+
+struct fz_md5_s
+{
+ unsigned long state[4];
+ unsigned long count[2];
+ unsigned char buffer[64];
+};
+
+void fz_md5init(fz_md5 *state);
+void fz_md5update(fz_md5 *state, unsigned char *input, unsigned inlen);
+void fz_md5final(fz_md5 *state, unsigned char digest[16]);
+
+/* arc4 crypto */
+
+typedef struct fz_arc4_s fz_arc4;
+
+struct fz_arc4_s
+{
+ unsigned x;
+ unsigned y;
+ unsigned char state[256];
+};
+
+void fz_arc4init(fz_arc4 *state, unsigned char *key, unsigned len);
+unsigned char fz_arc4next(fz_arc4 *state);
+void fz_arc4encrypt(fz_arc4 *state, unsigned char *dest, unsigned char *src, unsigned len);
+
diff --git a/include/fitz/file.h b/include/fitz/file.h
new file mode 100644
index 00000000..e687e572
--- /dev/null
+++ b/include/fitz/file.h
@@ -0,0 +1,33 @@
+typedef struct fz_file_s fz_file;
+
+struct fz_file_s
+{
+ int mode; /* O_RDONLY or O_WRONLY */
+ int fd;
+ int depth;
+ fz_filter *filter;
+ fz_buffer *in;
+ fz_buffer *out;
+ fz_error *error;
+};
+
+fz_error *fz_openfile(fz_file **filep, char *path, int mode);
+fz_error *fz_pushfilter(fz_file *file, fz_filter *filter);
+void fz_popfilter(fz_file *file);
+void fz_closefile(fz_file *file);
+
+int fz_seek(fz_file *f, int ofs);
+int fz_tell(fz_file *f);
+
+int fz_readbyte(fz_file *f);
+int fz_peekbyte(fz_file *f);
+int fz_readline(fz_file *f, char *buf, int n);
+int fz_read(fz_file *f, char *buf, int n);
+
+int fz_write(fz_file *f, char *buf, int n);
+int fz_flush(fz_file *f);
+
+fz_error *fz_readfile(unsigned char **bufp, int *lenp, fz_file *file);
+
+fz_error *fz_ferror(fz_file *f);
+
diff --git a/include/fitz/filter.h b/include/fitz/filter.h
new file mode 100644
index 00000000..6f234143
--- /dev/null
+++ b/include/fitz/filter.h
@@ -0,0 +1,80 @@
+typedef struct fz_filter_s fz_filter;
+typedef struct fz_buffer_s fz_buffer;
+
+#define FZ_BUFSIZE (32 * 1024)
+
+#define fz_ioneedin (&fz_kioneedin)
+#define fz_ioneedout (&fz_kioneedout)
+#define fz_iodone (&fz_kiodone)
+
+extern fz_error fz_kioneedin;
+extern fz_error fz_kioneedout;
+extern fz_error fz_kiodone;
+
+#define FZ_NEWFILTER(TYPE,VAR,NAME) \
+ fz_error * fz_process ## NAME (fz_filter*,fz_buffer*,fz_buffer*); \
+ void fz_free ## NAME (fz_filter*); \
+ TYPE *VAR; \
+ *fp = fz_malloc(sizeof(TYPE)); \
+ if (!*fp) return fz_outofmem; \
+ (*fp)->process = fz_process ## NAME ; \
+ (*fp)->free = fz_free ## NAME ; \
+ (*fp)->consumed = 0; \
+ (*fp)->produced = 0; \
+ (*fp)->count = 0; \
+ VAR = (TYPE*) *fp
+
+struct fz_filter_s
+{
+ fz_error* (*process)(fz_filter *filter, fz_buffer *in, fz_buffer *out);
+ void (*free)(fz_filter *filter);
+ int consumed;
+ int produced;
+ int count;
+};
+
+struct fz_buffer_s
+{
+ unsigned char *bp;
+ unsigned char *rp;
+ unsigned char *wp;
+ unsigned char *ep;
+ int eof;
+};
+
+fz_error *fz_process(fz_filter *f, fz_buffer *in, fz_buffer *out);
+void fz_freefilter(fz_filter *f);
+
+fz_error *fz_newnullfilter(fz_filter **fp, int len);
+fz_error *fz_newarc4filter(fz_filter **fp, unsigned char *key, unsigned keylen);
+fz_error *fz_newpipeline(fz_filter **fp, fz_filter *head, fz_filter *tail);
+
+fz_error *fz_chainpipeline(fz_filter **fp, fz_filter *head, fz_filter *tail, fz_buffer *buf);
+void fz_unchainpipeline(fz_filter *pipe, fz_filter **oldfp, fz_buffer **oldbp);
+
+fz_error *fz_newbuffer(fz_buffer **bufp, int size);
+fz_error *fz_rewindbuffer(fz_buffer *buf);
+fz_error *fz_growbuffer(fz_buffer *buf);
+void fz_freebuffer(fz_buffer *buf);
+
+fz_error *fz_newa85d(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newa85e(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newahxd(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newahxe(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newrld(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newrle(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newdctd(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newdcte(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newfaxd(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newfaxe(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newflated(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newflatee(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newlzwd(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newlzwe(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newpredictd(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newpredicte(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newjbig2d(fz_filter **filterp, fz_obj *param);
+fz_error *fz_newjpxd(fz_filter **filterp, fz_obj *param);
+
+void fz_pushbackahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out, int n);
+
diff --git a/include/fitz/font.h b/include/fitz/font.h
new file mode 100644
index 00000000..743d87fa
--- /dev/null
+++ b/include/fitz/font.h
@@ -0,0 +1,63 @@
+typedef struct fz_font_s fz_font;
+typedef struct fz_hmtx_s fz_hmtx;
+typedef struct fz_vmtx_s fz_vmtx;
+typedef struct fz_glyph_s fz_glyph;
+typedef struct fz_glyphcache_s fz_glyphcache;
+
+struct fz_hmtx_s
+{
+ unsigned short c;
+ short w;
+};
+
+struct fz_vmtx_s
+{
+ unsigned short c;
+ short x;
+ short y;
+ short w;
+};
+
+struct fz_font_s
+{
+ char name[32];
+
+ fz_error* (*render)(fz_glyph*, fz_font*, int, fz_matrix);
+ void (*free)(fz_font *);
+
+ int wmode;
+ fz_irect bbox;
+
+ int nhmtx, hmtxcap;
+ fz_hmtx dhmtx;
+ fz_hmtx *hmtx;
+
+ int nvmtx, vmtxcap;
+ fz_vmtx dvmtx;
+ fz_vmtx *vmtx;
+};
+
+struct fz_glyph_s
+{
+ int w, h, lsb, top;
+ unsigned char *bitmap;
+};
+
+void fz_initfont(fz_font *font, char *name);
+void fz_freefont(fz_font *font);
+void fz_debugfont(fz_font *font);
+void fz_setfontwmode(fz_font *font, int wmode);
+void fz_setfontbbox(fz_font *font, int xmin, int ymin, int xmax, int ymax);
+void fz_setdefaulthmtx(fz_font *font, int w);
+void fz_setdefaultvmtx(fz_font *font, int y, int w);
+fz_error *fz_addhmtx(fz_font *font, int gid, int w);
+fz_error *fz_addvmtx(fz_font *font, int gid, int x, int y, int w);
+fz_error *fz_endhmtx(fz_font *font);
+fz_error *fz_endvmtx(fz_font *font);
+fz_hmtx fz_gethmtx(fz_font *font, int gid);
+fz_vmtx fz_getvmtx(fz_font *font, int gid);
+
+fz_error *fz_newglyphcache(fz_glyphcache **arenap, int slots, int size);
+fz_error *fz_renderglyph(fz_glyphcache*, fz_glyph*, fz_font*, int, fz_matrix);
+void fz_freeglyphcache(fz_glyphcache *);
+
diff --git a/include/fitz/geometry.h b/include/fitz/geometry.h
new file mode 100644
index 00000000..588aa562
--- /dev/null
+++ b/include/fitz/geometry.h
@@ -0,0 +1,56 @@
+typedef struct fz_matrix_s fz_matrix;
+typedef struct fz_point_s fz_point;
+typedef struct fz_rect_s fz_rect;
+typedef struct fz_ipoint_s fz_ipoint;
+typedef struct fz_irect_s fz_irect;
+
+/*
+ / a b 0 \
+ | c d 0 |
+ \ e f 1 /
+*/
+struct fz_matrix_s
+{
+ float a, b, c, d, e, f;
+};
+
+struct fz_point_s
+{
+ float x, y;
+};
+
+struct fz_rect_s
+{
+ fz_point min;
+ fz_point max;
+};
+
+struct fz_ipoint_s
+{
+ int x, y;
+};
+
+struct fz_irect_s
+{
+ fz_ipoint min;
+ fz_ipoint max;
+};
+
+#define FZ_INFRECT (fz_rect){{1,1},{-1,-1}}
+
+fz_matrix fz_concat(fz_matrix one, fz_matrix two);
+fz_matrix fz_identity(void);
+fz_matrix fz_scale(float sx, float sy);
+fz_matrix fz_rotate(float theta);
+fz_matrix fz_translate(float tx, float ty);
+fz_matrix fz_invertmatrix(fz_matrix m);
+int fz_isrectilinear(fz_matrix m);
+
+fz_rect fz_intersectrects(fz_rect a, fz_rect b);
+fz_rect fz_mergerects(fz_rect a, fz_rect b);
+
+fz_irect fz_intersectirects(fz_irect a, fz_irect b);
+fz_irect fz_mergeirects(fz_irect a, fz_irect b);
+
+fz_point fz_transformpoint(fz_matrix m, fz_point p);
+
diff --git a/include/fitz/hash.h b/include/fitz/hash.h
new file mode 100644
index 00000000..4229a02c
--- /dev/null
+++ b/include/fitz/hash.h
@@ -0,0 +1,15 @@
+typedef struct fz_hashtable_s fz_hashtable;
+
+fz_error *fz_newhash(fz_hashtable **tablep, int initialsize, int keylen);
+fz_error *fz_resizehash(fz_hashtable *table, int newsize);
+void fz_debughash(fz_hashtable *table);
+void fz_freehash(fz_hashtable *table);
+
+void *fz_hashfind(fz_hashtable *table, void *key);
+fz_error *fz_hashinsert(fz_hashtable *table, void *key, void *val);
+fz_error *fz_hashremove(fz_hashtable *table, void *key);
+
+int fz_hashlen(fz_hashtable *table);
+void *fz_gethashkey(fz_hashtable *table, int idx);
+void *fz_gethashval(fz_hashtable *table, int idx);
+
diff --git a/include/fitz/image.h b/include/fitz/image.h
new file mode 100644
index 00000000..7f6038ce
--- /dev/null
+++ b/include/fitz/image.h
@@ -0,0 +1,17 @@
+enum
+{
+ FZ_CSGRAY,
+ FZ_CSRGB,
+ FZ_CSCMYK
+};
+
+struct fz_image_s
+{
+ fz_node super;
+ int w, h, n, bpc;
+ int cs;
+ unsigned char *data;
+};
+
+fz_error *fz_newimage(fz_node **nodep, int w, int h, int n, int bpc, int cs);
+
diff --git a/include/fitz/math.h b/include/fitz/math.h
new file mode 100644
index 00000000..acefeb50
--- /dev/null
+++ b/include/fitz/math.h
@@ -0,0 +1,30 @@
+/* multiply 8-bit fixpoint (0..1) so that 0*0==0 and 255*255==255 */
+static inline unsigned char fz_mul255(unsigned char a, unsigned char b)
+{
+ return ((a + 1) * b) >> 8;
+}
+
+/* divide and floor towards -inf */
+static inline int fz_idiv(int a, int b)
+{
+ return a < 0 ? (a - b + 1) / b : a / b;
+}
+
+/* from python */
+static inline void fz_idivmod(int x, int y, int *d, int *m)
+{
+ int xdivy = x / y;
+ int xmody = x - xdivy * y;
+ /* If the signs of x and y differ, and the remainder is non-0,
+ * C89 doesn't define whether xdivy is now the floor or the
+ * ceiling of the infinitely precise quotient. We want the floor,
+ * and we have it iff the remainder's sign matches y's.
+ */
+ if (xmody && ((y ^ xmody) < 0)) {
+ xmody += y;
+ xdivy --;
+ }
+ *d = xdivy;
+ *m = xmody;
+}
+
diff --git a/include/fitz/object.h b/include/fitz/object.h
new file mode 100644
index 00000000..8ccdd153
--- /dev/null
+++ b/include/fitz/object.h
@@ -0,0 +1,124 @@
+typedef struct fz_obj_s fz_obj;
+
+typedef enum fz_objkind_e fz_objkind;
+
+enum fz_objkind_e
+{
+ FZ_NULL,
+ FZ_BOOL,
+ FZ_INT,
+ FZ_REAL,
+ FZ_STRING,
+ FZ_NAME,
+ FZ_ARRAY,
+ FZ_DICT,
+ FZ_INDIRECT,
+ FZ_POINTER,
+};
+
+struct fz_keyval_s
+{
+ fz_obj *k;
+ fz_obj *v;
+};
+
+struct fz_obj_s
+{
+ unsigned short kind; /* fz_objkind takes 4 bytes :( */
+ unsigned short refcount;
+ union
+ {
+ int b;
+ int i;
+ float f;
+ struct {
+ unsigned short len;
+ unsigned char buf[];
+ } s;
+ unsigned char n[1];
+ struct {
+ int len;
+ int cap;
+ fz_obj **items;
+ } a;
+ struct {
+ int len;
+ int cap;
+ struct fz_keyval_s *items;
+ } d;
+ struct {
+ int oid;
+ int gid;
+ } r;
+ void *p;
+ } u;
+};
+
+fz_error *fz_newnull(fz_obj **op);
+fz_error *fz_newbool(fz_obj **op, int b);
+fz_error *fz_newint(fz_obj **op, int i);
+fz_error *fz_newreal(fz_obj **op, float f);
+fz_error *fz_newname(fz_obj **op, char *str);
+fz_error *fz_newstring(fz_obj **op, char *str, int len);
+fz_error *fz_newindirect(fz_obj **op, int oid, int gid);
+fz_error *fz_newpointer(fz_obj **op, void *p);
+
+fz_error *fz_newarray(fz_obj **op, int initialcap);
+fz_error *fz_newdict(fz_obj **op, int initialcap);
+fz_error *fz_copyarray(fz_obj **op, fz_obj *array);
+fz_error *fz_copydict(fz_obj **op, fz_obj *dict);
+fz_error *fz_deepcopyarray(fz_obj **op, fz_obj *array);
+fz_error *fz_deepcopydict(fz_obj **op, fz_obj *dict);
+
+fz_obj *fz_keepobj(fz_obj *obj);
+fz_obj *fz_dropobj(fz_obj *obj);
+
+/* type queries */
+int fz_isnull(fz_obj *obj);
+int fz_isbool(fz_obj *obj);
+int fz_isint(fz_obj *obj);
+int fz_isreal(fz_obj *obj);
+int fz_isname(fz_obj *obj);
+int fz_isstring(fz_obj *obj);
+int fz_isarray(fz_obj *obj);
+int fz_isdict(fz_obj *obj);
+int fz_isindirect(fz_obj *obj);
+int fz_ispointer(fz_obj *obj);
+
+/* silent failure, no error reporting */
+int fz_tobool(fz_obj *obj);
+int fz_toint(fz_obj *obj);
+float fz_toreal(fz_obj *obj);
+char *fz_toname(fz_obj *obj);
+char *fz_tostringbuf(fz_obj *obj);
+int fz_tostringlen(fz_obj *obj);
+int fz_toobjid(fz_obj *obj);
+int fz_togenid(fz_obj *obj);
+void *fz_topointer(fz_obj *obj);
+
+int fz_arraylen(fz_obj *array);
+fz_obj *fz_arrayget(fz_obj *array, int i);
+fz_error *fz_arrayput(fz_obj *array, int i, fz_obj *obj);
+fz_error *fz_arraypush(fz_obj *array, fz_obj *obj);
+void fz_freearray(fz_obj *array);
+
+int fz_dictlen(fz_obj *dict);
+fz_obj *fz_dictgetkey(fz_obj *dict, int idx);
+fz_obj *fz_dictgetval(fz_obj *dict, int idx);
+fz_obj *fz_dictget(fz_obj *dict, fz_obj *key);
+fz_obj *fz_dictgets(fz_obj *dict, char *key);
+fz_error *fz_dictput(fz_obj *dict, fz_obj *key, fz_obj *val);
+fz_error *fz_dictputs(fz_obj *dict, char *key, fz_obj *val);
+fz_error *fz_dictdel(fz_obj *dict, fz_obj *key);
+fz_error *fz_dictdels(fz_obj *dict, char *key);
+void fz_freedict(fz_obj *dict);
+
+int fz_sprintobj(char *s, fz_obj *obj);
+int fz_sprintcobj(char *s, fz_obj *obj);
+int fz_fprintobj(FILE *f, fz_obj *obj);
+int fz_fprintcobj(FILE *f, fz_obj *obj);
+
+fz_error *fz_parseobj(fz_obj **objp, char *s);
+fz_error *fz_packobj(fz_obj **objp, char *fmt, ...);
+fz_error *fz_unpackobj(fz_obj *obj, char *fmt, ...);
+
diff --git a/include/fitz/path.h b/include/fitz/path.h
new file mode 100644
index 00000000..536ca281
--- /dev/null
+++ b/include/fitz/path.h
@@ -0,0 +1,57 @@
+typedef enum fz_pathkind_e fz_pathkind;
+typedef enum fz_pathelkind_e fz_pathelkind;
+typedef struct fz_stroke_s fz_stroke;
+typedef struct fz_dash_s fz_dash;
+typedef union fz_pathel_s fz_pathel;
+
+enum fz_pathkind_e { FZ_STROKE, FZ_FILL, FZ_EOFILL };
+enum fz_pathelkind_e { FZ_MOVETO, FZ_LINETO, FZ_CURVETO, FZ_CLOSEPATH };
+
+struct fz_stroke_s
+{
+ int linecap;
+ int linejoin;
+ float linewidth;
+ float miterlimit;
+};
+
+struct fz_dash_s
+{
+ int len;
+ float phase;
+ float array[];
+};
+
+union fz_pathel_s
+{
+ fz_pathelkind k;
+ float v;
+};
+
+struct fz_path_s
+{
+ fz_node super;
+ fz_pathkind paint;
+ fz_stroke *stroke;
+ fz_dash *dash;
+ int len, cap;
+ fz_pathel *els;
+};
+
+fz_error *fz_newpath(fz_path **pathp);
+fz_error *fz_clonepath(fz_path **pathp, fz_path *oldpath);
+fz_error *fz_moveto(fz_path*, float x, float y);
+fz_error *fz_lineto(fz_path*, float x, float y);
+fz_error *fz_curveto(fz_path*, float, float, float, float, float, float);
+fz_error *fz_curvetov(fz_path*, float, float, float, float);
+fz_error *fz_curvetoy(fz_path*, float, float, float, float);
+fz_error *fz_closepath(fz_path*);
+fz_error *fz_endpath(fz_path*, fz_pathkind paint, fz_stroke *stroke, fz_dash *dash);
+void fz_freepath(fz_path *path);
+
+fz_rect fz_boundpath(fz_path *node, fz_matrix ctm);
+void fz_debugpath(fz_path *node);
+
+fz_error *fz_newdash(fz_dash **dashp, float phase, int len, float *array);
+void fz_freedash(fz_dash *dash);
+
diff --git a/include/fitz/pixmap.h b/include/fitz/pixmap.h
new file mode 100644
index 00000000..90071a30
--- /dev/null
+++ b/include/fitz/pixmap.h
@@ -0,0 +1,22 @@
+typedef struct fz_pixmap_s fz_pixmap;
+typedef struct fz_colorspace_s fz_colorspace;
+
+struct fz_pixmap_s
+{
+ int x, y, w, h;
+ int n, a;
+ int stride;
+ fz_colorspace *cs;
+ short *samples;
+};
+
+fz_error *fz_newpixmap(fz_pixmap **mapp, int x, int y, int w, int h, int n, int a);
+void fz_clearpixmap(fz_pixmap *map);
+void fz_freepixmap(fz_pixmap *map);
+
+void fz_blendover(short *C, short *A, short *B, int n);
+void fz_blendin(short *C, short *A, short *B, int n);
+void fz_blendout(short *C, short *A, short *B, int n);
+void fz_blendatop(short *C, short *A, short *B, int n);
+void fz_blendxor(short *C, short *A, short *B, int n);
+
diff --git a/include/fitz/render.h b/include/fitz/render.h
new file mode 100644
index 00000000..d237e872
--- /dev/null
+++ b/include/fitz/render.h
@@ -0,0 +1,11 @@
+typedef struct fz_renderer_s fz_renderer;
+
+fz_error *fz_newrenderer(fz_renderer **gcp);
+void fz_freerenderer(fz_renderer *gc);
+
+fz_error *fz_renderover(fz_renderer *gc, fz_over *over, fz_matrix ctm, fz_pixmap *out);
+fz_error *fz_rendermask(fz_renderer *gc, fz_mask *mask, fz_matrix ctm, fz_pixmap *out);
+fz_error *fz_rendertransform(fz_renderer *gc, fz_transform *xform, fz_matrix ctm, fz_pixmap *out);
+fz_error *fz_rendertext(fz_renderer *gc, fz_text *text, fz_matrix ctm, fz_pixmap *out);
+fz_error *fz_rendernode(fz_renderer *gc, fz_node *node, fz_matrix ctm, fz_pixmap *out);
+
diff --git a/include/fitz/scanconv.h b/include/fitz/scanconv.h
new file mode 100644
index 00000000..f391a387
--- /dev/null
+++ b/include/fitz/scanconv.h
@@ -0,0 +1,46 @@
+typedef struct fz_edge_s fz_edge;
+typedef struct fz_gel_s fz_gel;
+typedef struct fz_ael_s fz_ael;
+
+struct fz_edge_s
+{
+ int x, e, h, y;
+ int adjup, adjdown;
+ int xmove;
+ int xdir, ydir; /* -1 or +1 */
+};
+
+struct fz_gel_s
+{
+ int hs, vs;
+ int xmin, xmax;
+ int ymin, ymax;
+ int cap;
+ int len;
+ fz_edge *edges;
+};
+
+struct fz_ael_s
+{
+ int cap;
+ int len;
+ fz_edge **edges;
+};
+
+fz_error *fz_newgel(fz_gel **gelp);
+fz_error *fz_insertgel(fz_gel *gel, float x0, float y0, float x1, float y1);
+void fz_resetgel(fz_gel *gel, int hs, int vs);
+void fz_sortgel(fz_gel *gel);
+void fz_freegel(fz_gel *gel);
+
+fz_error *fz_newael(fz_ael **aelp);
+fz_error *fz_insertael(fz_ael *ael, fz_gel *gel, int y, int *e);
+void fz_advanceael(fz_ael *ael);
+void fz_freeael(fz_ael *ael);
+
+fz_error *fz_scanconvert(fz_gel *gel, fz_ael *ael, int eofill);
+
+fz_error *fz_fillpath(fz_gel *gel, fz_path *path, fz_matrix ctm, float flatness);
+fz_error *fz_strokepath(fz_gel *gel, fz_path *path, fz_matrix ctm, float flatness);
+fz_error *fz_dashpath(fz_gel *gel, fz_path *path, fz_matrix ctm, float flatness);
+
diff --git a/include/fitz/sysdep.h b/include/fitz/sysdep.h
new file mode 100644
index 00000000..bb78521f
--- /dev/null
+++ b/include/fitz/sysdep.h
@@ -0,0 +1,48 @@
+/*
+ * Include the basic standard libc headers.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdarg.h>
+
+#include <limits.h> /* INT_MIN, MAX ... */
+#include <float.h> /* DBL_EPSILON */
+#include <math.h>
+
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h> /* O_RDONLY & co */
+
+/* not supposed to be here, but printf debugging sorta needs it */
+#include <stdio.h>
+
+typedef unsigned char fz_u8;
+typedef signed char fz_s8;
+typedef unsigned short fz_u16;
+typedef signed short fz_s16;
+typedef unsigned long fz_u32;
+typedef signed long fz_s32;
+typedef unsigned long long fz_u64;
+typedef signed long long fz_s64;
+
+/*
+ * Extras! Extras! Get them while they're hot!
+ */
+
+#ifdef NEED_STRLCPY
+extern int strlcpy(char *dst, const char *src, int n);
+extern int strlcat(char *dst, const char *src, int n);
+#endif
+
+#ifdef NEED_STRSEP
+extern char *strsep(char **stringp, const char *delim);
+#endif
+
+#ifdef NEED_GETOPT
+extern int getopt(int nargc, char * const * nargv, const char *ostr);
+extern int opterr, optind, optopt;
+extern char *optarg;
+#endif
+
diff --git a/include/fitz/text.h b/include/fitz/text.h
new file mode 100644
index 00000000..41a08c45
--- /dev/null
+++ b/include/fitz/text.h
@@ -0,0 +1,23 @@
+typedef struct fz_textbuilder_s fz_textbuilder;
+typedef struct fz_textel_s fz_textel;
+
+struct fz_textel_s
+{
+ float x, y;
+ int g;
+};
+
+struct fz_text_s
+{
+ fz_node super;
+ fz_font *font;
+ fz_matrix trm;
+ int len, cap;
+ fz_textel *els;
+};
+
+fz_error *fz_newtext(fz_text **textp, fz_font *face);
+fz_error *fz_addtext(fz_text *text, int g, float x, float y);
+fz_error *fz_endtext(fz_text *text);
+void fz_freetext(fz_text *text);
+
diff --git a/include/fitz/tree.h b/include/fitz/tree.h
new file mode 100644
index 00000000..080e55bf
--- /dev/null
+++ b/include/fitz/tree.h
@@ -0,0 +1,156 @@
+typedef struct fz_tree_s fz_tree;
+typedef struct fz_node_s fz_node;
+
+typedef enum fz_nodekind_e fz_nodekind;
+typedef enum fz_blendkind_e fz_blendkind;
+
+typedef struct fz_transform_s fz_transform;
+typedef struct fz_over_s fz_over;
+typedef struct fz_mask_s fz_mask;
+typedef struct fz_blend_s fz_blend;
+typedef struct fz_path_s fz_path;
+typedef struct fz_text_s fz_text;
+typedef struct fz_solid_s fz_solid;
+typedef struct fz_image_s fz_image;
+typedef struct fz_shade_s fz_shade;
+typedef struct fz_form_s fz_form;
+typedef struct fz_meta_s fz_meta;
+typedef struct fz_halftone_s fz_halftone;
+
+enum fz_nodekind_e
+{
+ FZ_NTRANSFORM,
+ FZ_NOVER,
+ FZ_NMASK,
+ FZ_NBLEND,
+ FZ_NPATH,
+ FZ_NTEXT,
+ FZ_NSOLID,
+ FZ_NIMAGE,
+ FZ_NSHADE,
+ FZ_NFORM,
+ FZ_NMETA,
+ FZ_NHALFTONE
+};
+
+enum fz_blendkind_e
+{
+ /* PDF 1.4 -- standard separable */
+ FZ_BNORMAL,
+ FZ_BMULTIPLY,
+ FZ_BSCREEN,
+ FZ_BOVERLAY,
+ FZ_BDARKEN,
+ FZ_BLIGHTEN,
+ FZ_BCOLORDODGE,
+ FZ_BCOLORBURN,
+ FZ_BHARDLIGHT,
+ FZ_BSOFTLIGHT,
+ FZ_BDIFFERENCE,
+ FZ_BEXCLUSION,
+
+ /* PDF 1.4 -- standard non-separable */
+ FZ_BHUE,
+ FZ_BSATURATION,
+ FZ_BCOLOR,
+ FZ_BLUMINOSITY,
+
+ FZ_BOVERPRINT,
+};
+
+struct fz_tree_s
+{
+ fz_node *root;
+ fz_node *head;
+};
+
+struct fz_node_s
+{
+ fz_nodekind kind;
+ fz_node *parent;
+ fz_node *next;
+};
+
+struct fz_meta_s
+{
+ fz_node super;
+ fz_node *child;
+ fz_obj *info;
+};
+
+struct fz_over_s
+{
+ fz_node super;
+ fz_node *child;
+};
+
+struct fz_mask_s
+{
+ fz_node super;
+ fz_node *child;
+};
+
+struct fz_blend_s
+{
+ fz_node super;
+ fz_node *child;
+ fz_blendkind mode;
+ int isolated;
+ int knockout;
+};
+
+struct fz_transform_s
+{
+ fz_node super;
+ fz_node *child;
+ fz_matrix m;
+};
+
+struct fz_form_s
+{
+ fz_node super;
+ fz_tree *tree;
+};
+
+struct fz_solid_s
+{
+ fz_node super;
+ float r, g, b;
+};
+
+/* tree operations */
+fz_error *fz_newtree(fz_tree **treep);
+void fz_freetree(fz_tree *tree);
+fz_rect fz_boundtree(fz_tree *tree, fz_matrix ctm);
+
+void fz_debugtree(fz_tree *tree);
+void fz_insertnode(fz_node *node, fz_node *child);
+
+/* common to all nodes */
+void fz_initnode(fz_node *node, fz_nodekind kind);
+fz_rect fz_boundnode(fz_node *node, fz_matrix ctm);
+void fz_freenode(fz_node *node);
+
+/* branch nodes */
+fz_error *fz_newmeta(fz_node **nodep, fz_obj *info);
+fz_error *fz_newover(fz_node **nodep);
+fz_error *fz_newmask(fz_node **nodep);
+fz_error *fz_newblend(fz_node **nodep, fz_blendkind b, int k, int i);
+fz_error *fz_newtransform(fz_node **nodep, fz_matrix m);
+
+int fz_ismeta(fz_node *node);
+int fz_isover(fz_node *node);
+int fz_ismask(fz_node *node);
+int fz_isblend(fz_node *node);
+int fz_istransform(fz_node *node);
+
+/* leaf nodes */
+fz_error *fz_newform(fz_node **nodep, fz_tree *subtree);
+fz_error *fz_newsolid(fz_node **nodep, float r, float g, float b);
+
+int fz_isform(fz_node *node);
+int fz_issolid(fz_node *node);
+int fz_ispath(fz_node *node);
+int fz_istext(fz_node *node);
+int fz_isimage(fz_node *node);
+
diff --git a/include/mupdf.h b/include/mupdf.h
new file mode 100644
index 00000000..ca4a267b
--- /dev/null
+++ b/include/mupdf.h
@@ -0,0 +1,253 @@
+#ifdef _MUPDF_H_
+#error "mupdf.h must only be included once"
+#endif
+#define _MUPDF_H_
+
+#ifndef _FITZ_H_
+#error "fitz.h must be included before mupdf.h"
+#endif
+
+/*
+ * tokenizer and low-level object parser
+ */
+
+enum
+{
+ PDF_TERROR, PDF_TEOF,
+ PDF_TOARRAY, PDF_TCARRAY,
+ PDF_TODICT, PDF_TCDICT,
+ PDF_TNAME, PDF_TINT, PDF_TREAL, PDF_TSTRING, PDF_TKEYWORD,
+ PDF_TR, PDF_TTRUE, PDF_TFALSE, PDF_TNULL,
+ PDF_TOBJ, PDF_TENDOBJ,
+ PDF_TSTREAM, PDF_TENDSTREAM,
+ PDF_TXREF, PDF_TTRAILER, PDF_TSTARTXREF,
+ PDF_NTOKENS
+};
+
+/* lex.c */
+int pdf_lex(fz_file *f, unsigned char *buf, int n, int *len);
+
+/* parse.c */
+fz_error *pdf_parsearray(fz_obj **op, fz_file *f, unsigned char *buf, int cap);
+fz_error *pdf_parsedict(fz_obj **op, fz_file *f, unsigned char *buf, int cap);
+fz_error *pdf_parsestmobj(fz_obj **op, fz_file *f, unsigned char *buf, int cap);
+fz_error *pdf_parseindobj(fz_obj **op, fz_file *f, unsigned char *buf, int cap, int *oid, int *gid, int *stmofsj);
+
+/*
+ * xref and syntax object api
+ */
+
+typedef struct pdf_xref_s pdf_xref;
+typedef struct pdf_xrefentry_s pdf_xrefentry;
+typedef struct pdf_crypt_s pdf_crypt;
+
+struct pdf_xref_s
+{
+ float version;
+ pdf_crypt *crypt;
+ fz_file *file;
+ int size;
+ int capacity;
+ pdf_xrefentry *table;
+ fz_obj *trailer;
+ int startxref;
+ fz_hashtable *store;
+};
+
+struct pdf_xrefentry_s
+{
+ unsigned int ofs; /* file offset / objstm object number */
+ unsigned short gen; /* generation / objstm index */
+ char type; /* 0=unset (f)ree i(n)use (o)bjstm (d)elete (a)dd */
+ char mark; /* for garbage collection etc */
+};
+
+struct pdf_crypt_s
+{
+ unsigned char o[32];
+ unsigned char u[32];
+ unsigned int p;
+ int r;
+ int n;
+
+ fz_obj *id;
+
+ unsigned char key[16];
+ int keylen;
+};
+
+/* stream.c */
+fz_error *pdf_buildfilter(fz_filter**, pdf_xref*, fz_obj *stm, int oid, int gid);
+fz_error *pdf_openstream0(pdf_xref*, fz_obj *stmobj, int oid, int gid, int ofs);
+fz_error *pdf_openstream(pdf_xref*, fz_obj *stmref);
+void pdf_closestream(pdf_xref*);
+fz_error *pdf_readstream(unsigned char **bufp, int *lenp, pdf_xref*, fz_obj *stmref);
+
+/* crypt.c */
+fz_error *pdf_newdecrypt(pdf_crypt **cp, fz_obj *enc, fz_obj *id);
+fz_error *pdf_newencrypt(pdf_crypt **cp, fz_obj **edict, char *userpw, char *ownerpw, int p, int n, fz_obj *id);
+fz_error *pdf_setpassword(pdf_crypt *crypt, char *pw);
+fz_error *pdf_cryptstm(fz_filter **fp, pdf_crypt *crypt, int oid, int gid);
+void pdf_cryptobj(pdf_crypt *crypt, fz_obj *obj, int oid, int gid);
+void pdf_freecrypt(pdf_crypt *crypt);
+
+/* repair.c */
+fz_error *pdf_repairxref(pdf_xref*, char *filename);
+
+/* open.c */
+fz_error *pdf_openxref(pdf_xref*, char *filename);
+fz_error *pdf_readobjstm(pdf_xref *xref, int oid, int gid, unsigned char *buf, int cap);
+
+/* xref.c */
+fz_error *pdf_newxref(pdf_xref **xrefp);
+fz_error *pdf_decryptxref(pdf_xref *xref);
+void pdf_closexref(pdf_xref*);
+void pdf_debugxref(pdf_xref*);
+
+fz_obj *pdf_findstoredobject(fz_hashtable *store, int oid, int gid);
+fz_buffer *pdf_findstoredstream(fz_hashtable *store, int oid, int gid);
+fz_error *pdf_deletestoredobject(fz_hashtable *store, int oid, int gid);
+fz_error *pdf_deletestoredstream(fz_hashtable *store, int oid, int gid);
+fz_error *pdf_storeobject(fz_hashtable *store, int oid, int gid, fz_obj *obj);
+fz_error *pdf_storestream(fz_hashtable *store, int oid, int gid, fz_buffer *buf);
+
+fz_error *pdf_createobject(pdf_xref *xref, int *oidp, int *gidp);
+fz_error *pdf_deleteobject(pdf_xref *xref, int oid, int gid);
+fz_error *pdf_saveobject(pdf_xref *xref, int oid, int gid, fz_obj *obj);
+fz_error *pdf_loadobject0(fz_obj **, pdf_xref*, int oid, int gid, int *stmofs);
+fz_error *pdf_loadobject(fz_obj **, pdf_xref*, fz_obj *ref, int *stmofs);
+fz_error *pdf_resolve(fz_obj **, pdf_xref*);
+
+/* save.c */
+fz_error *pdf_saveincrementalpdf(pdf_xref *xref, char *path);
+fz_error *pdf_savepdf(pdf_xref *xref, char *path);
+
+/*
+ * high-level semantic objects for resources and pages
+ */
+
+typedef struct pdf_pagetree_s pdf_pagetree;
+typedef struct pdf_font_s pdf_font;
+typedef struct pdf_resources_s pdf_resources;
+typedef struct pdf_gstate_s pdf_gstate;
+typedef struct pdf_csi_s pdf_csi;
+
+struct pdf_pagetree_s
+{
+ int count;
+ int cursor;
+ fz_obj **pref;
+ fz_obj **pobj;
+};
+
+struct pdf_font_s
+{
+ fz_font super;
+
+ void *ftface;
+
+ fz_cmap *encoding;
+ int cidtogidlen;
+ int *cidtogidmap;
+
+ char *filename;
+ char *fontdata;
+ int fontlen;
+};
+
+struct pdf_type3_s
+{
+ fz_rect bbox;
+ fz_matrix matrix;
+ int widths[256];
+ fz_tree *charprocs[256];
+ int tounicode[256];
+};
+
+struct pdf_resources_s
+{
+ fz_obj *extgstate;
+ fz_obj *colorspace;
+ fz_obj *font;
+ fz_obj *ximage;
+ fz_obj *xform;
+};
+
+struct pdf_gstate_s
+{
+ /* path stroking */
+ float linewidth;
+ int linecap;
+ int linejoin;
+ float miterlimit;
+ float dashphase;
+ int dashlen;
+ float dashlist[32];
+
+ /* colors and colorspaces */
+ struct { float r, g, b; } stroke, fill;
+
+ /* text state */
+ float charspace;
+ float wordspace;
+ float scale;
+ float leading;
+ pdf_font *font;
+ float size;
+ int render;
+ float rise;
+
+ /* tree construction state */
+ fz_node *head;
+};
+
+struct pdf_csi_s
+{
+ pdf_gstate gstate[32];
+ int gtop;
+ fz_obj *stack[32];
+ int top;
+ int xbalance;
+
+ /* path object state */
+ fz_path *path;
+ fz_path *clip;
+
+ /* text object state */
+ fz_text *text;
+ fz_matrix tlm;
+ fz_matrix tm;
+
+ fz_tree *tree;
+};
+
+/* pagetree.c */
+fz_error *pdf_loadpagetree(pdf_pagetree **pp, pdf_xref *xref);
+void pdf_debugpagetree(pdf_pagetree *pages);
+void pdf_freepagetree(pdf_pagetree *pages);
+
+/* cmap.c */
+fz_error *pdf_parsecmap(fz_cmap **cmapp, fz_file *file);
+fz_error *pdf_loadembeddedcmap(fz_cmap **cmapp, pdf_xref *xref, fz_obj *stmref);
+fz_error *pdf_loadsystemcmap(fz_cmap **cmapp, char *name);
+fz_error *pdf_makeidentitycmap(fz_cmap **cmapp, int wmode);
+
+/* fontfile.c */
+fz_error *pdf_loadbuiltinfont(void **fontp, char *pattern);
+fz_error *pdf_loadsystemfont(void **fontp, char *basefont, char *collection);
+fz_error *pdf_loadembeddedfont(void **fontp, pdf_xref *xref, fz_obj *stmref);
+fz_error *pdf_loadfontdescriptor(void **fontp, pdf_xref *xref, fz_obj *desc, char *collection);
+
+/* font.c */
+fz_error *pdf_loadfont(pdf_font **fontp, pdf_xref *xref, fz_obj *font);
+void pdf_freefont(pdf_font *font);
+
+/* resources.c */
+fz_error *pdf_loadresources(pdf_resources **rdbp, pdf_xref *xref, fz_obj *resdict);
+void pdf_freeresources(pdf_resources *rdb);
+
+/* interpret.c */
+fz_error *pdf_newcsi(pdf_csi **csip);
+fz_error *pdf_runcsi(pdf_csi *, pdf_resources *, fz_file *);
+void pdf_freecsi(pdf_csi *csi);
+