From 0a927854a10e1e6b9770a81e2e1d9f3093631757 Mon Sep 17 00:00:00 2001
From: Tor Andersson
Date: Wed, 19 Jun 2013 15:29:44 +0200
Subject: Rearrange source files.
---
fitz/base_context.c | 210 -------
fitz/base_error.c | 155 -----
fitz/base_geometry.c | 483 ----------------
fitz/base_getopt.c | 66 ---
fitz/base_hash.c | 357 ------------
fitz/base_memory.c | 402 -------------
fitz/base_string.c | 264 ---------
fitz/base_time.c | 144 -----
fitz/base_trans.c | 165 ------
fitz/base_xml.c | 460 ---------------
fitz/crypt_aes.c | 569 ------------------
fitz/crypt_arc4.c | 98 ----
fitz/crypt_md5.c | 272 ---------
fitz/crypt_pkcs7.c | 400 -------------
fitz/crypt_sha2.c | 393 -------------
fitz/dev_bbox.c | 231 --------
fitz/dev_list.c | 851 ---------------------------
fitz/dev_null.c | 388 -------------
fitz/dev_svg.c | 619 --------------------
fitz/dev_trace.c | 339 -----------
fitz/doc_document.c | 274 ---------
fitz/doc_link.c | 65 ---
fitz/doc_outline.c | 62 --
fitz/filt_basic.c | 662 ---------------------
fitz/filt_dctd.c | 256 ---------
fitz/filt_faxd.c | 776 -------------------------
fitz/filt_flate.c | 117 ----
fitz/filt_jbig2d.c | 108 ----
fitz/filt_lzwd.c | 224 --------
fitz/filt_predict.c | 256 ---------
fitz/image_jpeg.c | 111 ----
fitz/image_jpx.c | 253 --------
fitz/image_png.c | 599 -------------------
fitz/image_tiff.c | 867 ----------------------------
fitz/memento.c | 1535 -------------------------------------------------
fitz/res_bitmap.c | 123 ----
fitz/res_colorspace.c | 1277 ----------------------------------------
fitz/res_font.c | 1094 -----------------------------------
fitz/res_func.c | 48 --
fitz/res_halftone.c | 202 -------
fitz/res_image.c | 493 ----------------
fitz/res_path.c | 507 ----------------
fitz/res_pcl.c | 856 ---------------------------
fitz/res_pixmap.c | 1062 ----------------------------------
fitz/res_pwg.c | 318 ----------
fitz/res_shade.c | 1096 -----------------------------------
fitz/res_store.c | 638 --------------------
fitz/res_text.c | 154 -----
fitz/stm_buffer.c | 390 -------------
fitz/stm_comp_buf.c | 75 ---
fitz/stm_open.c | 210 -------
fitz/stm_output.c | 100 ----
fitz/stm_read.c | 219 -------
fitz/text_extract.c | 1027 ---------------------------------
fitz/text_output.c | 400 -------------
fitz/text_paragraph.c | 1500 -----------------------------------------------
fitz/text_search.c | 279 ---------
57 files changed, 25099 deletions(-)
delete mode 100644 fitz/base_context.c
delete mode 100644 fitz/base_error.c
delete mode 100644 fitz/base_geometry.c
delete mode 100644 fitz/base_getopt.c
delete mode 100644 fitz/base_hash.c
delete mode 100644 fitz/base_memory.c
delete mode 100644 fitz/base_string.c
delete mode 100644 fitz/base_time.c
delete mode 100644 fitz/base_trans.c
delete mode 100644 fitz/base_xml.c
delete mode 100644 fitz/crypt_aes.c
delete mode 100644 fitz/crypt_arc4.c
delete mode 100644 fitz/crypt_md5.c
delete mode 100644 fitz/crypt_pkcs7.c
delete mode 100644 fitz/crypt_sha2.c
delete mode 100644 fitz/dev_bbox.c
delete mode 100644 fitz/dev_list.c
delete mode 100644 fitz/dev_null.c
delete mode 100644 fitz/dev_svg.c
delete mode 100644 fitz/dev_trace.c
delete mode 100644 fitz/doc_document.c
delete mode 100644 fitz/doc_link.c
delete mode 100644 fitz/doc_outline.c
delete mode 100644 fitz/filt_basic.c
delete mode 100644 fitz/filt_dctd.c
delete mode 100644 fitz/filt_faxd.c
delete mode 100644 fitz/filt_flate.c
delete mode 100644 fitz/filt_jbig2d.c
delete mode 100644 fitz/filt_lzwd.c
delete mode 100644 fitz/filt_predict.c
delete mode 100644 fitz/image_jpeg.c
delete mode 100644 fitz/image_jpx.c
delete mode 100644 fitz/image_png.c
delete mode 100644 fitz/image_tiff.c
delete mode 100644 fitz/memento.c
delete mode 100644 fitz/res_bitmap.c
delete mode 100644 fitz/res_colorspace.c
delete mode 100644 fitz/res_font.c
delete mode 100644 fitz/res_func.c
delete mode 100644 fitz/res_halftone.c
delete mode 100644 fitz/res_image.c
delete mode 100644 fitz/res_path.c
delete mode 100644 fitz/res_pcl.c
delete mode 100644 fitz/res_pixmap.c
delete mode 100644 fitz/res_pwg.c
delete mode 100644 fitz/res_shade.c
delete mode 100644 fitz/res_store.c
delete mode 100644 fitz/res_text.c
delete mode 100644 fitz/stm_buffer.c
delete mode 100644 fitz/stm_comp_buf.c
delete mode 100644 fitz/stm_open.c
delete mode 100644 fitz/stm_output.c
delete mode 100644 fitz/stm_read.c
delete mode 100644 fitz/text_extract.c
delete mode 100644 fitz/text_output.c
delete mode 100644 fitz/text_paragraph.c
delete mode 100644 fitz/text_search.c
(limited to 'fitz')
diff --git a/fitz/base_context.c b/fitz/base_context.c
deleted file mode 100644
index c65377a8..00000000
--- a/fitz/base_context.c
+++ /dev/null
@@ -1,210 +0,0 @@
-#include "mupdf/fitz.h"
-
-struct fz_id_context_s
-{
- int refs;
- int id;
-};
-
-static void
-fz_drop_id_context(fz_context *ctx)
-{
- int refs;
- fz_id_context *id = ctx->id;
-
- if (id == NULL)
- return;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- refs = --id->refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- if (refs == 0)
- fz_free(ctx, id);
-}
-
-static void
-fz_new_id_context(fz_context *ctx)
-{
- ctx->id = fz_malloc_struct(ctx, fz_id_context);
- ctx->id->refs = 1;
- ctx->id->id = 0;
-}
-
-static fz_id_context *
-fz_keep_id_context(fz_context *ctx)
-{
- fz_id_context *id = ctx->id;
-
- if (id == NULL)
- return NULL;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- ++id->refs;
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return id;
-}
-
-void
-fz_free_context(fz_context *ctx)
-{
- if (!ctx)
- return;
-
- /* Other finalisation calls go here (in reverse order) */
- fz_drop_glyph_cache_context(ctx);
- fz_drop_store_context(ctx);
- fz_free_aa_context(ctx);
- fz_drop_colorspace_context(ctx);
- fz_drop_font_context(ctx);
- fz_drop_id_context(ctx);
-
- if (ctx->warn)
- {
- fz_flush_warnings(ctx);
- fz_free(ctx, ctx->warn);
- }
-
- if (ctx->error)
- {
- assert(ctx->error->top == -1);
- fz_free(ctx, ctx->error);
- }
-
- /* Free the context itself */
- ctx->alloc->free(ctx->alloc->user, ctx);
-}
-
-/* Allocate new context structure, and initialise allocator, and sections
- * that aren't shared between contexts.
- */
-static fz_context *
-new_context_phase1(fz_alloc_context *alloc, fz_locks_context *locks)
-{
- fz_context *ctx;
-
- ctx = alloc->malloc(alloc->user, sizeof(fz_context));
- if (!ctx)
- return NULL;
- memset(ctx, 0, sizeof *ctx);
- ctx->alloc = alloc;
- ctx->locks = locks;
-
- ctx->glyph_cache = NULL;
-
- ctx->error = fz_malloc_no_throw(ctx, sizeof(fz_error_context));
- if (!ctx->error)
- goto cleanup;
- ctx->error->top = -1;
- ctx->error->errcode = FZ_ERROR_NONE;
- ctx->error->message[0] = 0;
-
- ctx->warn = fz_malloc_no_throw(ctx, sizeof(fz_warn_context));
- if (!ctx->warn)
- goto cleanup;
- ctx->warn->message[0] = 0;
- ctx->warn->count = 0;
-
- /* New initialisation calls for context entries go here */
- fz_try(ctx)
- {
- fz_new_aa_context(ctx);
- }
- fz_catch(ctx)
- {
- goto cleanup;
- }
-
- return ctx;
-
-cleanup:
- fprintf(stderr, "cannot create context (phase 1)\n");
- fz_free_context(ctx);
- return NULL;
-}
-
-fz_context *
-fz_new_context(fz_alloc_context *alloc, fz_locks_context *locks, unsigned int max_store)
-{
- fz_context *ctx;
-
- if (!alloc)
- alloc = &fz_alloc_default;
-
- if (!locks)
- locks = &fz_locks_default;
-
- ctx = new_context_phase1(alloc, locks);
- if (!ctx)
- return NULL;
-
- /* Now initialise sections that are shared */
- fz_try(ctx)
- {
- fz_new_store_context(ctx, max_store);
- fz_new_glyph_cache_context(ctx);
- fz_new_colorspace_context(ctx);
- fz_new_font_context(ctx);
- fz_new_id_context(ctx);
- }
- fz_catch(ctx)
- {
- fprintf(stderr, "cannot create context (phase 2)\n");
- fz_free_context(ctx);
- return NULL;
- }
- return ctx;
-}
-
-fz_context *
-fz_clone_context(fz_context *ctx)
-{
- /* We cannot safely clone the context without having locking/
- * unlocking functions. */
- if (ctx == NULL || ctx->locks == &fz_locks_default)
- return NULL;
- return fz_clone_context_internal(ctx);
-}
-
-fz_context *
-fz_clone_context_internal(fz_context *ctx)
-{
- fz_context *new_ctx;
-
- if (ctx == NULL || ctx->alloc == NULL)
- return NULL;
-
- new_ctx = new_context_phase1(ctx->alloc, ctx->locks);
- if (!new_ctx)
- return NULL;
-
- /* Inherit AA defaults from old context. */
- fz_copy_aa_context(new_ctx, ctx);
-
- /* Keep thread lock checking happy by copying pointers first and locking under new context */
- new_ctx->store = ctx->store;
- new_ctx->store = fz_keep_store_context(new_ctx);
- new_ctx->glyph_cache = ctx->glyph_cache;
- new_ctx->glyph_cache = fz_keep_glyph_cache(new_ctx);
- new_ctx->colorspace = ctx->colorspace;
- new_ctx->colorspace = fz_keep_colorspace_context(new_ctx);
- new_ctx->font = ctx->font;
- new_ctx->font = fz_keep_font_context(new_ctx);
- new_ctx->id = ctx->id;
- new_ctx->id = fz_keep_id_context(new_ctx);
-
- return new_ctx;
-}
-
-int
-fz_gen_id(fz_context *ctx)
-{
- int id;
- fz_lock(ctx, FZ_LOCK_ALLOC);
- /* We'll never wrap around in normal use, but if we *do*, then avoid
- * 0. */
- do
- {
- id = ++ctx->id->id;
- }
- while (id == 0);
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return id;
-}
diff --git a/fitz/base_error.c b/fitz/base_error.c
deleted file mode 100644
index 50b3c5aa..00000000
--- a/fitz/base_error.c
+++ /dev/null
@@ -1,155 +0,0 @@
-#include "mupdf/fitz.h"
-
-/* Warning context */
-
-void fz_var_imp(void *var)
-{
- UNUSED(var); /* Do nothing */
-}
-
-void fz_flush_warnings(fz_context *ctx)
-{
- if (ctx->warn->count > 1)
- {
- fprintf(stderr, "warning: ... repeated %d times ...\n", ctx->warn->count);
- LOGE("warning: ... repeated %d times ...\n", ctx->warn->count);
- }
- ctx->warn->message[0] = 0;
- ctx->warn->count = 0;
-}
-
-void fz_warn(fz_context *ctx, const char *fmt, ...)
-{
- va_list ap;
- char buf[sizeof ctx->warn->message];
-
- va_start(ap, fmt);
- vsnprintf(buf, sizeof buf, fmt, ap);
- va_end(ap);
-
- if (!strcmp(buf, ctx->warn->message))
- {
- ctx->warn->count++;
- }
- else
- {
- fz_flush_warnings(ctx);
- fprintf(stderr, "warning: %s\n", buf);
- LOGE("warning: %s\n", buf);
- fz_strlcpy(ctx->warn->message, buf, sizeof ctx->warn->message);
- ctx->warn->count = 1;
- }
-}
-
-/* Error context */
-
-/* When we first setjmp, code is set to 0. Whenever we throw, we add 2 to
- * this code. Whenever we enter the always block, we add 1.
- *
- * fz_push_try sets code to 0.
- * If (fz_throw called within fz_try)
- * fz_throw makes code = 2.
- * If (no always block present)
- * enter catch region with code = 2. OK.
- * else
- * fz_always entered as code < 3; Makes code = 3;
- * if (fz_throw called within fz_always)
- * fz_throw makes code = 5
- * fz_always is not reentered.
- * catch region entered with code = 5. OK.
- * else
- * catch region entered with code = 3. OK
- * else
- * if (no always block present)
- * catch region not entered as code = 0. OK.
- * else
- * fz_always entered as code < 3. makes code = 1
- * if (fz_throw called within fz_always)
- * fz_throw makes code = 3;
- * fz_always NOT entered as code >= 3
- * catch region entered with code = 3. OK.
- * else
- * catch region entered with code = 1.
- */
-
-static void throw(fz_error_context *ex) FZ_NORETURN;
-
-static void throw(fz_error_context *ex)
-{
- if (ex->top >= 0) {
- fz_longjmp(ex->stack[ex->top].buffer, ex->stack[ex->top].code + 2);
- } else {
- fprintf(stderr, "uncaught exception: %s\n", ex->message);
- LOGE("uncaught exception: %s\n", ex->message);
- exit(EXIT_FAILURE);
- }
-}
-
-int fz_push_try(fz_error_context *ex)
-{
- assert(ex);
- ex->top++;
- /* Normal case, get out of here quick */
- if (ex->top < nelem(ex->stack)-1)
- return 1; /* We exit here, and the setjmp sets the code to 0 */
- /* We reserve the top slot on the exception stack purely to cope with
- * the case when we overflow. If we DO hit this, then we 'throw'
- * immediately - returning 0 stops the setjmp happening and takes us
- * direct to the always/catch clauses. */
- assert(ex->top == nelem(ex->stack)-1);
- strcpy(ex->message, "exception stack overflow!");
- ex->stack[ex->top].code = 2;
- fprintf(stderr, "error: %s\n", ex->message);
- LOGE("error: %s\n", ex->message);
- return 0;
-}
-
-int fz_caught(fz_context *ctx)
-{
- assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE);
- return ctx->error->errcode;
-}
-
-const char *fz_caught_message(fz_context *ctx)
-{
- assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE);
- return ctx->error->message;
-}
-
-void fz_throw(fz_context *ctx, int code, const char *fmt, ...)
-{
- va_list args;
- ctx->error->errcode = code;
- va_start(args, fmt);
- vsnprintf(ctx->error->message, sizeof ctx->error->message, fmt, args);
- va_end(args);
-
- fz_flush_warnings(ctx);
- fprintf(stderr, "error: %s\n", ctx->error->message);
- LOGE("error: %s\n", ctx->error->message);
-
- throw(ctx->error);
-}
-
-void fz_rethrow(fz_context *ctx)
-{
- assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE);
- throw(ctx->error);
-}
-
-void fz_rethrow_message(fz_context *ctx, const char *fmt, ...)
-{
- va_list args;
-
- assert(ctx && ctx->error && ctx->error->errcode >= FZ_ERROR_NONE);
-
- va_start(args, fmt);
- vsnprintf(ctx->error->message, sizeof ctx->error->message, fmt, args);
- va_end(args);
-
- fz_flush_warnings(ctx);
- fprintf(stderr, "error: %s\n", ctx->error->message);
- LOGE("error: %s\n", ctx->error->message);
-
- throw(ctx->error);
-}
diff --git a/fitz/base_geometry.c b/fitz/base_geometry.c
deleted file mode 100644
index 81450246..00000000
--- a/fitz/base_geometry.c
+++ /dev/null
@@ -1,483 +0,0 @@
-#include "mupdf/fitz.h"
-
-#define MAX4(a,b,c,d) fz_max(fz_max(a,b), fz_max(c,d))
-#define MIN4(a,b,c,d) fz_min(fz_min(a,b), fz_min(c,d))
-
-/* A useful macro to add with overflow detection and clamping.
-
- We want to do "b = a + x", but to allow for overflow. Consider the
- top bits, and the cases in which overflow occurs:
-
- overflow a x b ~a^x a^b (~a^x)&(a^b)
- no 0 0 0 1 0 0
- yes 0 0 1 1 1 1
- no 0 1 0 0 0 0
- no 0 1 1 0 1 0
- no 1 0 0 0 1 0
- no 1 0 1 0 0 0
- yes 1 1 0 1 1 1
- no 1 1 1 1 0 0
-*/
-#define ADD_WITH_SAT(b,a,x) \
- ((b) = (a) + (x), (b) = (((~(a)^(x))&((a)^(b))) < 0 ? ((x) < 0 ? INT_MIN : INT_MAX) : (b)))
-
-/* Matrices, points and affine transformations */
-
-const fz_matrix fz_identity = { 1, 0, 0, 1, 0, 0 };
-
-fz_matrix *
-fz_concat(fz_matrix *dst, const fz_matrix *one, const fz_matrix *two)
-{
- fz_matrix dst2;
- dst2.a = one->a * two->a + one->b * two->c;
- dst2.b = one->a * two->b + one->b * two->d;
- dst2.c = one->c * two->a + one->d * two->c;
- dst2.d = one->c * two->b + one->d * two->d;
- dst2.e = one->e * two->a + one->f * two->c + two->e;
- dst2.f = one->e * two->b + one->f * two->d + two->f;
- *dst = dst2;
- return dst;
-}
-
-fz_matrix *
-fz_scale(fz_matrix *m, float sx, float sy)
-{
- m->a = sx; m->b = 0;
- m->c = 0; m->d = sy;
- m->e = 0; m->f = 0;
- return m;
-}
-
-fz_matrix *
-fz_pre_scale(fz_matrix *mat, float sx, float sy)
-{
- mat->a *= sx;
- mat->b *= sx;
- mat->c *= sy;
- mat->d *= sy;
- return mat;
-}
-
-fz_matrix *
-fz_shear(fz_matrix *mat, float h, float v)
-{
- mat->a = 1; mat->b = v;
- mat->c = h; mat->d = 1;
- mat->e = 0; mat->f = 0;
- return mat;
-}
-
-fz_matrix *
-fz_pre_shear(fz_matrix *mat, float h, float v)
-{
- float a = mat->a;
- float b = mat->b;
- mat->a += v * mat->c;
- mat->b += v * mat->d;
- mat->c += h * a;
- mat->d += h * b;
- return mat;
-}
-
-fz_matrix *
-fz_rotate(fz_matrix *m, float theta)
-{
- float s;
- float c;
-
- while (theta < 0)
- theta += 360;
- while (theta >= 360)
- theta -= 360;
-
- if (fabsf(0 - theta) < FLT_EPSILON)
- {
- s = 0;
- c = 1;
- }
- else if (fabsf(90.0f - theta) < FLT_EPSILON)
- {
- s = 1;
- c = 0;
- }
- else if (fabsf(180.0f - theta) < FLT_EPSILON)
- {
- s = 0;
- c = -1;
- }
- else if (fabsf(270.0f - theta) < FLT_EPSILON)
- {
- s = -1;
- c = 0;
- }
- else
- {
- s = sinf(theta * (float)M_PI / 180);
- c = cosf(theta * (float)M_PI / 180);
- }
-
- m->a = c; m->b = s;
- m->c = -s; m->d = c;
- m->e = 0; m->f = 0;
- return m;
-}
-
-fz_matrix *
-fz_pre_rotate(fz_matrix *m, float theta)
-{
- while (theta < 0)
- theta += 360;
- while (theta >= 360)
- theta -= 360;
-
- if (fabsf(0 - theta) < FLT_EPSILON)
- {
- /* Nothing to do */
- }
- else if (fabsf(90.0f - theta) < FLT_EPSILON)
- {
- float a = m->a;
- float b = m->b;
- m->a = m->c;
- m->b = m->d;
- m->c = -a;
- m->d = -b;
- }
- else if (fabsf(180.0f - theta) < FLT_EPSILON)
- {
- m->a = -m->a;
- m->b = -m->b;
- m->c = -m->c;
- m->d = -m->d;
- }
- else if (fabsf(270.0f - theta) < FLT_EPSILON)
- {
- float a = m->a;
- float b = m->b;
- m->a = -m->c;
- m->b = -m->d;
- m->c = a;
- m->d = b;
- }
- else
- {
- float s = sinf(theta * (float)M_PI / 180);
- float c = cosf(theta * (float)M_PI / 180);
- float a = m->a;
- float b = m->b;
- m->a = c * a + s * m->c;
- m->b = c * b + s * m->d;
- m->c =-s * a + c * m->c;
- m->d =-s * b + c * m->d;
- }
-
- return m;
-}
-
-fz_matrix *
-fz_translate(fz_matrix *m, float tx, float ty)
-{
- m->a = 1; m->b = 0;
- m->c = 0; m->d = 1;
- m->e = tx; m->f = ty;
- return m;
-}
-
-fz_matrix *
-fz_pre_translate(fz_matrix *mat, float tx, float ty)
-{
- mat->e += tx * mat->a + ty * mat->c;
- mat->f += tx * mat->b + ty * mat->d;
- return mat;
-}
-
-fz_matrix *
-fz_invert_matrix(fz_matrix *dst, const fz_matrix *src)
-{
- /* Be careful to cope with dst == src */
- float a = src->a;
- float det = a * src->d - src->b * src->c;
- if (det < -FLT_EPSILON || det > FLT_EPSILON)
- {
- float rdet = 1 / det;
- dst->a = src->d * rdet;
- dst->b = -src->b * rdet;
- dst->c = -src->c * rdet;
- dst->d = a * rdet;
- a = -src->e * dst->a - src->f * dst->c;
- dst->f = -src->e * dst->b - src->f * dst->d;
- dst->e = a;
- }
- else
- *dst = *src;
- return dst;
-}
-
-int
-fz_is_rectilinear(const fz_matrix *m)
-{
- return (fabsf(m->b) < FLT_EPSILON && fabsf(m->c) < FLT_EPSILON) ||
- (fabsf(m->a) < FLT_EPSILON && fabsf(m->d) < FLT_EPSILON);
-}
-
-float
-fz_matrix_expansion(const fz_matrix *m)
-{
- return sqrtf(fabsf(m->a * m->d - m->b * m->c));
-}
-
-float
-fz_matrix_max_expansion(const fz_matrix *m)
-{
- float max = fabsf(m->a);
- float x = fabsf(m->b);
- if (max < x)
- max = x;
- x = fabsf(m->c);
- if (max < x)
- max = x;
- x = fabsf(m->d);
- if (max < x)
- max = x;
- return max;
-}
-
-fz_point *
-fz_transform_point(fz_point *restrict p, const fz_matrix *restrict m)
-{
- float x = p->x;
- p->x = x * m->a + p->y * m->c + m->e;
- p->y = x * m->b + p->y * m->d + m->f;
- return p;
-}
-
-fz_point *
-fz_transform_vector(fz_point *restrict p, const fz_matrix *restrict m)
-{
- float x = p->x;
- p->x = x * m->a + p->y * m->c;
- p->y = x * m->b + p->y * m->d;
- return p;
-}
-
-void
-fz_normalize_vector(fz_point *p)
-{
- float len = p->x * p->x + p->y * p->y;
- if (len != 0)
- {
- len = sqrtf(len);
- p->x /= len;
- p->y /= len;
- }
-}
-
-/* Rectangles and bounding boxes */
-
-/* biggest and smallest integers that a float can represent perfectly (i.e. 24 bits) */
-#define MAX_SAFE_INT 16777216
-#define MIN_SAFE_INT -16777216
-
-const fz_rect fz_infinite_rect = { 1, 1, -1, -1 };
-const fz_rect fz_empty_rect = { 0, 0, 0, 0 };
-const fz_rect fz_unit_rect = { 0, 0, 1, 1 };
-
-const fz_irect fz_infinite_irect = { 1, 1, -1, -1 };
-const fz_irect fz_empty_irect = { 0, 0, 0, 0 };
-const fz_irect fz_unit_bbox = { 0, 0, 1, 1 };
-
-fz_irect *
-fz_irect_from_rect(fz_irect *restrict b, const fz_rect *restrict r)
-{
- b->x0 = fz_clamp(floorf(r->x0), MIN_SAFE_INT, MAX_SAFE_INT);
- b->y0 = fz_clamp(floorf(r->y0), MIN_SAFE_INT, MAX_SAFE_INT);
- b->x1 = fz_clamp(ceilf(r->x1), MIN_SAFE_INT, MAX_SAFE_INT);
- b->y1 = fz_clamp(ceilf(r->y1), MIN_SAFE_INT, MAX_SAFE_INT);
- return b;
-}
-
-fz_rect *
-fz_rect_from_irect(fz_rect *restrict r, const fz_irect *restrict a)
-{
- r->x0 = a->x0;
- r->y0 = a->y0;
- r->x1 = a->x1;
- r->y1 = a->y1;
- return r;
-}
-
-fz_irect *
-fz_round_rect(fz_irect * restrict b, const fz_rect *restrict r)
-{
- int i;
-
- i = floorf(r->x0 + 0.001);
- b->x0 = fz_clamp(i, MIN_SAFE_INT, MAX_SAFE_INT);
- i = floorf(r->y0 + 0.001);
- b->y0 = fz_clamp(i, MIN_SAFE_INT, MAX_SAFE_INT);
- i = ceilf(r->x1 - 0.001);
- b->x1 = fz_clamp(i, MIN_SAFE_INT, MAX_SAFE_INT);
- i = ceilf(r->y1 - 0.001);
- b->y1 = fz_clamp(i, MIN_SAFE_INT, MAX_SAFE_INT);
-
- return b;
-}
-
-fz_rect *
-fz_intersect_rect(fz_rect *restrict a, const fz_rect *restrict b)
-{
- /* Check for empty box before infinite box */
- if (fz_is_empty_rect(a)) return a;
- if (fz_is_empty_rect(b)) {
- *a = fz_empty_rect;
- return a;
- }
- if (fz_is_infinite_rect(b)) return a;
- if (fz_is_infinite_rect(a)) {
- *a = *b;
- return a;
- }
- if (a->x0 < b->x0)
- a->x0 = b->x0;
- if (a->y0 < b->y0)
- a->y0 = b->y0;
- if (a->x1 > b->x1)
- a->x1 = b->x1;
- if (a->y1 > b->y1)
- a->y1 = b->y1;
- if (a->x1 < a->x0 || a->y1 < a->y0)
- *a = fz_empty_rect;
- return a;
-}
-
-fz_irect *
-fz_intersect_irect(fz_irect *restrict a, const fz_irect *restrict b)
-{
- /* Check for empty box before infinite box */
- if (fz_is_empty_irect(a)) return a;
- if (fz_is_empty_irect(b))
- {
- *a = fz_empty_irect;
- return a;
- }
- if (fz_is_infinite_irect(b)) return a;
- if (fz_is_infinite_irect(a))
- {
- *a = *b;
- return a;
- }
- if (a->x0 < b->x0)
- a->x0 = b->x0;
- if (a->y0 < b->y0)
- a->y0 = b->y0;
- if (a->x1 > b->x1)
- a->x1 = b->x1;
- if (a->y1 > b->y1)
- a->y1 = b->y1;
- if (a->x1 < a->x0 || a->y1 < a->y0)
- *a = fz_empty_irect;
- return a;
-}
-
-fz_rect *
-fz_union_rect(fz_rect *restrict a, const fz_rect *restrict b)
-{
- /* Check for empty box before infinite box */
- if (fz_is_empty_rect(b)) return a;
- if (fz_is_empty_rect(a)) {
- *a = *b;
- return a;
- }
- if (fz_is_infinite_rect(a)) return a;
- if (fz_is_infinite_rect(b)) {
- *a = *b;
- return a;
- }
- if (a->x0 > b->x0)
- a->x0 = b->x0;
- if (a->y0 > b->y0)
- a->y0 = b->y0;
- if (a->x1 < b->x1)
- a->x1 = b->x1;
- if (a->y1 < b->y1)
- a->y1 = b->y1;
- return a;
-}
-
-fz_irect *
-fz_translate_irect(fz_irect *a, int xoff, int yoff)
-{
- int t;
-
- if (fz_is_empty_irect(a)) return a;
- if (fz_is_infinite_irect(a)) return a;
- a->x0 = ADD_WITH_SAT(t, a->x0, xoff);
- a->y0 = ADD_WITH_SAT(t, a->y0, yoff);
- a->x1 = ADD_WITH_SAT(t, a->x1, xoff);
- a->y1 = ADD_WITH_SAT(t, a->y1, yoff);
- return a;
-}
-
-fz_rect *
-fz_transform_rect(fz_rect *restrict r, const fz_matrix *restrict m)
-{
- fz_point s, t, u, v;
-
- if (fz_is_infinite_rect(r))
- return r;
-
- if (fabsf(m->b) < FLT_EPSILON && fabsf(m->c) < FLT_EPSILON)
- {
- if (m->a < 0)
- {
- float f = r->x0;
- r->x0 = r->x1;
- r->x1 = f;
- }
- if (m->d < 0)
- {
- float f = r->y0;
- r->y0 = r->y1;
- r->y1 = f;
- }
- fz_transform_point(fz_rect_min(r), m);
- fz_transform_point(fz_rect_max(r), m);
- return r;
- }
-
- s.x = r->x0; s.y = r->y0;
- t.x = r->x0; t.y = r->y1;
- u.x = r->x1; u.y = r->y1;
- v.x = r->x1; v.y = r->y0;
- fz_transform_point(&s, m);
- fz_transform_point(&t, m);
- fz_transform_point(&u, m);
- fz_transform_point(&v, m);
- r->x0 = MIN4(s.x, t.x, u.x, v.x);
- r->y0 = MIN4(s.y, t.y, u.y, v.y);
- r->x1 = MAX4(s.x, t.x, u.x, v.x);
- r->y1 = MAX4(s.y, t.y, u.y, v.y);
- return r;
-}
-
-fz_rect *
-fz_expand_rect(fz_rect *a, float expand)
-{
- if (fz_is_empty_rect(a)) return a;
- if (fz_is_infinite_rect(a)) return a;
- a->x0 -= expand;
- a->y0 -= expand;
- a->x1 += expand;
- a->y1 += expand;
- return a;
-}
-
-fz_rect *fz_include_point_in_rect(fz_rect *r, const fz_point *p)
-{
- if (p->x < r->x0) r->x0 = p->x;
- if (p->x > r->x1) r->x1 = p->x;
- if (p->y < r->y0) r->y0 = p->y;
- if (p->y > r->y1) r->y1 = p->y;
-
- return r;
-}
diff --git a/fitz/base_getopt.c b/fitz/base_getopt.c
deleted file mode 100644
index 2a6e5ac4..00000000
--- a/fitz/base_getopt.c
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * This is a version of the public domain getopt implementation by
- * Henry Spencer originally posted to net.sources.
- *
- * This file is in the public domain.
- */
-
-#include
-#include
-
-#define getopt fz_getopt
-#define optarg fz_optarg
-#define optind fz_optind
-
-char *optarg; /* Global argument pointer. */
-int optind = 0; /* Global argv index. */
-
-static char *scan = NULL; /* Private scan pointer. */
-
-int
-getopt(int argc, char *argv[], char *optstring)
-{
- char c;
- char *place;
-
- optarg = NULL;
-
- if (!scan || *scan == '\0') {
- if (optind == 0)
- optind++;
-
- if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
- return EOF;
- if (argv[optind][1] == '-' && argv[optind][2] == '\0') {
- optind++;
- return EOF;
- }
-
- scan = argv[optind]+1;
- optind++;
- }
-
- c = *scan++;
- place = strchr(optstring, c);
-
- if (!place || c == ':') {
- fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
- return '?';
- }
-
- place++;
- if (*place == ':') {
- if (*scan != '\0') {
- optarg = scan;
- scan = NULL;
- } else if( optind < argc ) {
- optarg = argv[optind];
- optind++;
- } else {
- fprintf(stderr, "%s: option requires argument -%c\n", argv[0], c);
- return ':';
- }
- }
-
- return c;
-}
diff --git a/fitz/base_hash.c b/fitz/base_hash.c
deleted file mode 100644
index 624cc305..00000000
--- a/fitz/base_hash.c
+++ /dev/null
@@ -1,357 +0,0 @@
-#include "mupdf/fitz.h"
-
-/*
-Simple hashtable with open addressing linear probe.
-Unlike text book examples, removing entries works
-correctly in this implementation, so it wont start
-exhibiting bad behaviour if entries are inserted
-and removed frequently.
-*/
-
-enum { MAX_KEY_LEN = 48 };
-typedef struct fz_hash_entry_s fz_hash_entry;
-
-struct fz_hash_entry_s
-{
- unsigned char key[MAX_KEY_LEN];
- void *val;
-};
-
-struct fz_hash_table_s
-{
- int keylen;
- int size;
- int load;
- int lock; /* -1 or the lock used to protect this hash table */
- fz_hash_entry *ents;
-};
-
-static unsigned hash(unsigned char *s, int len)
-{
- unsigned val = 0;
- int i;
- for (i = 0; i < len; i++)
- {
- val += s[i];
- val += (val << 10);
- val ^= (val >> 6);
- }
- val += (val << 3);
- val ^= (val >> 11);
- val += (val << 15);
- return val;
-}
-
-fz_hash_table *
-fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock)
-{
- fz_hash_table *table;
-
- assert(keylen <= MAX_KEY_LEN);
-
- table = fz_malloc_struct(ctx, fz_hash_table);
- table->keylen = keylen;
- table->size = initialsize;
- table->load = 0;
- table->lock = lock;
- fz_try(ctx)
- {
- table->ents = fz_malloc_array(ctx, table->size, sizeof(fz_hash_entry));
- memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
- }
- fz_catch(ctx)
- {
- fz_free(ctx, table);
- fz_rethrow(ctx);
- }
-
- return table;
-}
-
-void
-fz_empty_hash(fz_context *ctx, fz_hash_table *table)
-{
- table->load = 0;
- memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
-}
-
-int
-fz_hash_len(fz_context *ctx, fz_hash_table *table)
-{
- return table->size;
-}
-
-void *
-fz_hash_get_key(fz_context *ctx, fz_hash_table *table, int idx)
-{
- return table->ents[idx].key;
-}
-
-void *
-fz_hash_get_val(fz_context *ctx, fz_hash_table *table, int idx)
-{
- return table->ents[idx].val;
-}
-
-void
-fz_free_hash(fz_context *ctx, fz_hash_table *table)
-{
- fz_free(ctx, table->ents);
- fz_free(ctx, table);
-}
-
-static void *
-do_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val, unsigned *pos_ptr)
-{
- fz_hash_entry *ents;
- unsigned size;
- unsigned pos;
-
- ents = table->ents;
- size = table->size;
- pos = hash(key, table->keylen) % size;
-
- if (table->lock >= 0)
- fz_assert_lock_held(ctx, table->lock);
-
- while (1)
- {
- if (!ents[pos].val)
- {
- memcpy(ents[pos].key, key, table->keylen);
- ents[pos].val = val;
- table->load ++;
- if (pos_ptr)
- *pos_ptr = pos;
- return NULL;
- }
-
- if (memcmp(key, ents[pos].key, table->keylen) == 0)
- {
- /* This is legal, but should happen rarely in the non
- * pos_ptr case. */
- if (pos_ptr)
- *pos_ptr = pos;
- else
- fz_warn(ctx, "assert: overwrite hash slot");
- return ents[pos].val;
- }
-
- pos = (pos + 1) % size;
- }
-}
-
-/* Entered with the lock taken, held throughout and at exit, UNLESS the lock
- * is the alloc lock in which case it may be momentarily dropped. */
-static void
-fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize)
-{
- fz_hash_entry *oldents = table->ents;
- fz_hash_entry *newents;
- int oldsize = table->size;
- int oldload = table->load;
- int i;
-
- if (newsize < oldload * 8 / 10)
- {
- fz_warn(ctx, "assert: resize hash too small");
- return;
- }
-
- if (table->lock == FZ_LOCK_ALLOC)
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- newents = fz_malloc_array_no_throw(ctx, newsize, sizeof(fz_hash_entry));
- if (table->lock == FZ_LOCK_ALLOC)
- fz_lock(ctx, FZ_LOCK_ALLOC);
- if (table->lock >= 0)
- {
- if (table->size >= newsize)
- {
- /* Someone else fixed it before we could lock! */
- if (table->lock == FZ_LOCK_ALLOC)
- fz_unlock(ctx, table->lock);
- fz_free(ctx, newents);
- if (table->lock == FZ_LOCK_ALLOC)
- fz_lock(ctx, table->lock);
- return;
- }
- }
- if (newents == NULL)
- fz_throw(ctx, FZ_ERROR_GENERIC, "hash table resize failed; out of memory (%d entries)", newsize);
- table->ents = newents;
- memset(table->ents, 0, sizeof(fz_hash_entry) * newsize);
- table->size = newsize;
- table->load = 0;
-
- for (i = 0; i < oldsize; i++)
- {
- if (oldents[i].val)
- {
- do_hash_insert(ctx, table, oldents[i].key, oldents[i].val, NULL);
- }
- }
-
- if (table->lock == FZ_LOCK_ALLOC)
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- fz_free(ctx, oldents);
- if (table->lock == FZ_LOCK_ALLOC)
- fz_lock(ctx, FZ_LOCK_ALLOC);
-}
-
-void *
-fz_hash_find(fz_context *ctx, fz_hash_table *table, void *key)
-{
- fz_hash_entry *ents = table->ents;
- unsigned size = table->size;
- unsigned pos = hash(key, table->keylen) % size;
-
- if (table->lock >= 0)
- fz_assert_lock_held(ctx, table->lock);
-
- while (1)
- {
- if (!ents[pos].val)
- return NULL;
-
- if (memcmp(key, ents[pos].key, table->keylen) == 0)
- return ents[pos].val;
-
- pos = (pos + 1) % size;
- }
-}
-
-void *
-fz_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val)
-{
- if (table->load > table->size * 8 / 10)
- {
- fz_resize_hash(ctx, table, table->size * 2);
- }
-
- return do_hash_insert(ctx, table, key, val, NULL);
-}
-
-void *
-fz_hash_insert_with_pos(fz_context *ctx, fz_hash_table *table, void *key, void *val, unsigned *pos)
-{
- if (table->load > table->size * 8 / 10)
- {
- fz_resize_hash(ctx, table, table->size * 2);
- }
-
- return do_hash_insert(ctx, table, key, val, pos);
-}
-
-static void
-do_removal(fz_context *ctx, fz_hash_table *table, void *key, unsigned hole)
-{
- fz_hash_entry *ents = table->ents;
- unsigned size = table->size;
- unsigned look, code;
-
- if (table->lock >= 0)
- fz_assert_lock_held(ctx, table->lock);
-
- ents[hole].val = NULL;
-
- look = hole + 1;
- if (look == size)
- look = 0;
-
- while (ents[look].val)
- {
- code = hash(ents[look].key, table->keylen) % size;
- if ((code <= hole && hole < look) ||
- (look < code && code <= hole) ||
- (hole < look && look < code))
- {
- ents[hole] = ents[look];
- ents[look].val = NULL;
- hole = look;
- }
-
- look++;
- if (look == size)
- look = 0;
- }
-
- table->load --;
-}
-
-void
-fz_hash_remove(fz_context *ctx, fz_hash_table *table, void *key)
-{
- fz_hash_entry *ents = table->ents;
- unsigned size = table->size;
- unsigned pos = hash(key, table->keylen) % size;
-
- if (table->lock >= 0)
- fz_assert_lock_held(ctx, table->lock);
-
- while (1)
- {
- if (!ents[pos].val)
- {
- fz_warn(ctx, "assert: remove non-existent hash entry");
- return;
- }
-
- if (memcmp(key, ents[pos].key, table->keylen) == 0)
- {
- do_removal(ctx, table, key, pos);
- return;
- }
-
- pos++;
- if (pos == size)
- pos = 0;
- }
-}
-
-void
-fz_hash_remove_fast(fz_context *ctx, fz_hash_table *table, void *key, unsigned pos)
-{
- fz_hash_entry *ents = table->ents;
-
- if (ents[pos].val == NULL || memcmp(key, ents[pos].key, table->keylen) != 0)
- {
- /* The value isn't there, or the key didn't match! The table
- * must have been rebuilt (or the contents moved) in the
- * meantime. Do the removal the slow way. */
- fz_hash_remove(ctx, table, key);
- }
- else
- do_removal(ctx, table, key, pos);
-}
-
-#ifndef NDEBUG
-void
-fz_print_hash(fz_context *ctx, FILE *out, fz_hash_table *table)
-{
- fz_print_hash_details(ctx, out, table, NULL);
-}
-
-void
-fz_print_hash_details(fz_context *ctx, FILE *out, fz_hash_table *table, void (*details)(FILE *,void*))
-{
- int i, k;
-
- fprintf(out, "cache load %d / %d\n", table->load, table->size);
-
- for (i = 0; i < table->size; i++)
- {
- if (!table->ents[i].val)
- fprintf(out, "table % 4d: empty\n", i);
- else
- {
- fprintf(out, "table % 4d: key=", i);
- for (k = 0; k < MAX_KEY_LEN; k++)
- fprintf(out, "%02x", ((char*)table->ents[i].key)[k]);
- if (details)
- details(out, table->ents[i].val);
- else
- fprintf(out, " val=$%p\n", table->ents[i].val);
- }
- }
-}
-#endif
diff --git a/fitz/base_memory.c b/fitz/base_memory.c
deleted file mode 100644
index f9e7b4f6..00000000
--- a/fitz/base_memory.c
+++ /dev/null
@@ -1,402 +0,0 @@
-#include "mupdf/fitz.h"
-
-/* Enable FITZ_DEBUG_LOCKING_TIMES below if you want to check the times
- * for which locks are held too. */
-#ifdef FITZ_DEBUG_LOCKING
-#undef FITZ_DEBUG_LOCKING_TIMES
-#endif
-
-static void *
-do_scavenging_malloc(fz_context *ctx, unsigned int size)
-{
- void *p;
- int phase = 0;
-
- fz_lock(ctx, FZ_LOCK_ALLOC);
- do {
- p = ctx->alloc->malloc(ctx->alloc->user, size);
- if (p != NULL)
- {
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return p;
- }
- } while (fz_store_scavenge(ctx, size, &phase));
- fz_unlock(ctx, FZ_LOCK_ALLOC);
-
- return NULL;
-}
-
-static void *
-do_scavenging_realloc(fz_context *ctx, void *p, unsigned int size)
-{
- void *q;
- int phase = 0;
-
- fz_lock(ctx, FZ_LOCK_ALLOC);
- do {
- q = ctx->alloc->realloc(ctx->alloc->user, p, size);
- if (q != NULL)
- {
- fz_unlock(ctx, FZ_LOCK_ALLOC);
- return q;
- }
- } while (fz_store_scavenge(ctx, size, &phase));
- fz_unlock(ctx, FZ_LOCK_ALLOC);
-
- return NULL;
-}
-
-void *
-fz_malloc(fz_context *ctx, unsigned int size)
-{
- void *p;
-
- if (size == 0)
- return NULL;
-
- p = do_scavenging_malloc(ctx, size);
- if (!p)
- fz_throw(ctx, FZ_ERROR_GENERIC, "malloc of %d bytes failed", size);
- return p;
-}
-
-void *
-fz_malloc_no_throw(fz_context *ctx, unsigned int size)
-{
- return do_scavenging_malloc(ctx, size);
-}
-
-void *
-fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size)
-{
- void *p;
-
- if (count == 0 || size == 0)
- return 0;
-
- if (count > UINT_MAX / size)
- fz_throw(ctx, FZ_ERROR_GENERIC, "malloc of array (%d x %d bytes) failed (integer overflow)", count, size);
-
- p = do_scavenging_malloc(ctx, count * size);
- if (!p)
- fz_throw(ctx, FZ_ERROR_GENERIC, "malloc of array (%d x %d bytes) failed", count, size);
- return p;
-}
-
-void *
-fz_malloc_array_no_throw(fz_context *ctx, unsigned int count, unsigned int size)
-{
- if (count == 0 || size == 0)
- return 0;
-
- if (count > UINT_MAX / size)
- {
- fprintf(stderr, "error: malloc of array (%d x %d bytes) failed (integer overflow)", count, size);
- return NULL;
- }
-
- return do_scavenging_malloc(ctx, count * size);
-}
-
-void *
-fz_calloc(fz_context *ctx, unsigned int count, unsigned int size)
-{
- void *p;
-
- if (count == 0 || size == 0)
- return 0;
-
- if (count > UINT_MAX / size)
- {
- fz_throw(ctx, FZ_ERROR_GENERIC, "calloc (%d x %d bytes) failed (integer overflow)", count, size);
- }
-
- p = do_scavenging_malloc(ctx, count * size);
- if (!p)
- {
- fz_throw(ctx, FZ_ERROR_GENERIC, "calloc (%d x %d bytes) failed", count, size);
- }
- memset(p, 0, count*size);
- return p;
-}
-
-void *
-fz_calloc_no_throw(fz_context *ctx, unsigned int count, unsigned int size)
-{
- void *p;
-
- if (count == 0 || size == 0)
- return 0;
-
- if (count > UINT_MAX / size)
- {
- fprintf(stderr, "error: calloc (%d x %d bytes) failed (integer overflow)\n", count, size);
- return NULL;
- }
-
- p = do_scavenging_malloc(ctx, count * size);
- if (p)
- {
- memset(p, 0, count*size);
- }
- return p;
-}
-
-void *
-fz_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size)
-{
- void *np;
-
- if (count == 0 || size == 0)
- {
- fz_free(ctx, p);
- return 0;
- }
-
- if (count > UINT_MAX / size)
- fz_throw(ctx, FZ_ERROR_GENERIC, "resize array (%d x %d bytes) failed (integer overflow)", count, size);
-
- np = do_scavenging_realloc(ctx, p, count * size);
- if (!np)
- fz_throw(ctx, FZ_ERROR_GENERIC, "resize array (%d x %d bytes) failed", count, size);
- return np;
-}
-
-void *
-fz_resize_array_no_throw(fz_context *ctx, void *p, unsigned int count, unsigned int size)
-{
- if (count == 0 || size == 0)
- {
- fz_free(ctx, p);
- return 0;
- }
-
- if (count > UINT_MAX / size)
- {
- fprintf(stderr, "error: resize array (%d x %d bytes) failed (integer overflow)\n", count, size);
- return NULL;
- }
-
- return do_scavenging_realloc(ctx, p, count * size);
-}
-
-void
-fz_free(fz_context *ctx, void *p)
-{
- fz_lock(ctx, FZ_LOCK_ALLOC);
- ctx->alloc->free(ctx->alloc->user, p);
- fz_unlock(ctx, FZ_LOCK_ALLOC);
-}
-
-char *
-fz_strdup(fz_context *ctx, const char *s)
-{
- int len = strlen(s) + 1;
- char *ns = fz_malloc(ctx, len);
- memcpy(ns, s, len);
- return ns;
-}
-
-char *
-fz_strdup_no_throw(fz_context *ctx, const char *s)
-{
- int len = strlen(s) + 1;
- char *ns = fz_malloc_no_throw(ctx, len);
- if (ns)
- memcpy(ns, s, len);
- return ns;
-}
-
-static void *
-fz_malloc_default(void *opaque, unsigned int size)
-{
- return malloc(size);
-}
-
-static void *
-fz_realloc_default(void *opaque, void *old, unsigned int size)
-{
- return realloc(old, size);
-}
-
-static void
-fz_free_default(void *opaque, void *ptr)
-{
- free(ptr);
-}
-
-fz_alloc_context fz_alloc_default =
-{
- NULL,
- fz_malloc_default,
- fz_realloc_default,
- fz_free_default
-};
-
-static void
-fz_lock_default(void *user, int lock)
-{
-}
-
-static void
-fz_unlock_default(void *user, int lock)
-{
-}
-
-fz_locks_context fz_locks_default =
-{
- NULL,
- fz_lock_default,
- fz_unlock_default
-};
-
-#ifdef FITZ_DEBUG_LOCKING
-
-enum
-{
- FZ_LOCK_DEBUG_CONTEXT_MAX = 100
-};
-
-fz_context *fz_lock_debug_contexts[FZ_LOCK_DEBUG_CONTEXT_MAX];
-int fz_locks_debug[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX];
-#ifdef FITZ_DEBUG_LOCKING_TIMES
-int fz_debug_locking_inited = 0;
-int fz_lock_program_start;
-int fz_lock_time[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX] = { { 0 } };
-int fz_lock_taken[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX] = { { 0 } };
-
-/* We implement our own millisecond clock, as clock() cannot be trusted
- * when threads are involved. */
-static int ms_clock(void)
-{
-#ifdef _WIN32
- return (int)GetTickCount();
-#else
- struct timeval tp;
- gettimeofday(&tp, NULL);
- return (tp.tv_sec*1000) + (tp.tv_usec/1000);
-#endif
-}
-
-static void dump_lock_times(void)
-{
- int i, j;
- int prog_time = ms_clock() - fz_lock_program_start;
-
- for (j = 0; j < FZ_LOCK_MAX; j++)
- {
- int total = 0;
- for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++)
- {
- total += fz_lock_time[i][j];
- }
- printf("Lock %d held for %g seconds (%g%%)\n", j, ((double)total)/1000, 100.0*total/prog_time);
- }
- printf("Total program time %g seconds\n", ((double)prog_time)/1000);
-}
-
-#endif
-
-static int find_context(fz_context *ctx)
-{
- int i;
-
- for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++)
- {
- if (fz_lock_debug_contexts[i] == ctx)
- return i;
- if (fz_lock_debug_contexts[i] == NULL)
- {
- int gottit = 0;
- /* We've not locked on this context before, so use
- * this one for this new context. We might have other
- * threads trying here too though so, so claim it
- * atomically. No one has locked on this context
- * before, so we are safe to take the ALLOC lock. */
- ctx->locks->lock(ctx->locks->user, FZ_LOCK_ALLOC);
- /* If it's still free, then claim it as ours,
- * otherwise we'll keep hunting. */
- if (fz_lock_debug_contexts[i] == NULL)
- {
- gottit = 1;
- fz_lock_debug_contexts[i] = ctx;
-#ifdef FITZ_DEBUG_LOCKING_TIMES
- if (fz_debug_locking_inited == 0)
- {
- fz_debug_locking_inited = 1;
- fz_lock_program_start = ms_clock();
- atexit(dump_lock_times);
- }
-#endif
- }
- ctx->locks->unlock(ctx->locks->user, FZ_LOCK_ALLOC);
- if (gottit)
- return i;
- }
- }
- return -1;
-}
-
-void
-fz_assert_lock_held(fz_context *ctx, int lock)
-{
- int idx = find_context(ctx);
- if (idx < 0)
- return;
-
- if (fz_locks_debug[idx][lock] == 0)
- fprintf(stderr, "Lock %d not held when expected\n", lock);
-}
-
-void
-fz_assert_lock_not_held(fz_context *ctx, int lock)
-{
- int idx = find_context(ctx);
- if (idx < 0)
- return;
-
- if (fz_locks_debug[idx][lock] != 0)
- fprintf(stderr, "Lock %d held when not expected\n", lock);
-}
-
-void fz_lock_debug_lock(fz_context *ctx, int lock)
-{
- int i;
- int idx = find_context(ctx);
- if (idx < 0)
- return;
-
- if (fz_locks_debug[idx][lock] != 0)
- {
- fprintf(stderr, "Attempt to take lock %d when held already!\n", lock);
- }
- for (i = lock-1; i >= 0; i--)
- {
- if (fz_locks_debug[idx][i] != 0)
- {
- fprintf(stderr, "Lock ordering violation: Attempt to take lock %d when %d held already!\n", lock, i);
- }
- }
- fz_locks_debug[idx][lock] = 1;
-#ifdef FITZ_DEBUG_LOCKING_TIMES
- fz_lock_taken[idx][lock] = clock();
-#endif
-}
-
-void fz_lock_debug_unlock(fz_context *ctx, int lock)
-{
- int idx = find_context(ctx);
- if (idx < 0)
- return;
-
- if (fz_locks_debug[idx][lock] == 0)
- {
- fprintf(stderr, "Attempt to release lock %d when not held!\n", lock);
- }
- fz_locks_debug[idx][lock] = 0;
-#ifdef FITZ_DEBUG_LOCKING_TIMES
- fz_lock_time[idx][lock] += clock() - fz_lock_taken[idx][lock];
-#endif
-}
-
-#endif
diff --git a/fitz/base_string.c b/fitz/base_string.c
deleted file mode 100644
index b29cdbdc..00000000
--- a/fitz/base_string.c
+++ /dev/null
@@ -1,264 +0,0 @@
-#include "mupdf/fitz.h"
-
-char *
-fz_strsep(char **stringp, const char *delim)
-{
- char *ret = *stringp;
- if (!ret) return NULL;
- if ((*stringp = strpbrk(*stringp, delim)))
- *((*stringp)++) = '\0';
- return ret;
-}
-
-int
-fz_strlcpy(char *dst, const char *src, int siz)
-{
- register char *d = dst;
- register const char *s = src;
- register int n = siz;
-
- /* Copy as many bytes as will fit */
- if (n != 0 && --n != 0) {
- do {
- if ((*d++ = *s++) == 0)
- break;
- } while (--n != 0);
- }
-
- /* Not enough room in dst, add NUL and traverse rest of src */
- if (n == 0) {
- if (siz != 0)
- *d = '\0'; /* NUL-terminate dst */
- while (*s++)
- ;
- }
-
- return(s - src - 1); /* count does not include NUL */
-}
-
-int
-fz_strlcat(char *dst, const char *src, int siz)
-{
- register char *d = dst;
- register const char *s = src;
- register int n = siz;
- int dlen;
-
- /* Find the end of dst and adjust bytes left but don't go past end */
- while (*d != '\0' && n-- != 0)
- d++;
- dlen = d - dst;
- n = siz - dlen;
-
- if (n == 0)
- return dlen + strlen(s);
- while (*s != '\0') {
- if (n != 1) {
- *d++ = *s;
- n--;
- }
- s++;
- }
- *d = '\0';
-
- return dlen + (s - src); /* count does not include NUL */
-}
-
-enum
-{
- UTFmax = 4, /* maximum bytes per rune */
- Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
- Runeself = 0x80, /* rune and UTF sequences are the same (<) */
- Runeerror = 0xFFFD, /* decoding error in UTF */
- Runemax = 0x10FFFF, /* maximum rune value */
-};
-
-enum
-{
- Bit1 = 7,
- Bitx = 6,
- Bit2 = 5,
- Bit3 = 4,
- Bit4 = 3,
- Bit5 = 2,
-
- T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
- Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
- T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
- T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
- T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
- T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
-
- Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0111 1111 */
- Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0111 1111 1111 */
- Rune3 = (1<<(Bit3+2*Bitx))-1, /* 1111 1111 1111 1111 */
- Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0001 1111 1111 1111 1111 1111 */
-
- Maskx = (1< T1
- */
- c = *(const unsigned char*)str;
- if(c < Tx) {
- *rune = c;
- return 1;
- }
-
- /*
- * two character sequence
- * 0080-07FF => T2 Tx
- */
- c1 = *(const unsigned char*)(str+1) ^ Tx;
- if(c1 & Testx)
- goto bad;
- if(c < T3) {
- if(c < T2)
- goto bad;
- l = ((c << Bitx) | c1) & Rune2;
- if(l <= Rune1)
- goto bad;
- *rune = l;
- return 2;
- }
-
- /*
- * three character sequence
- * 0800-FFFF => T3 Tx Tx
- */
- c2 = *(const unsigned char*)(str+2) ^ Tx;
- if(c2 & Testx)
- goto bad;
- if(c < T4) {
- l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
- if(l <= Rune2)
- goto bad;
- *rune = l;
- return 3;
- }
-
- /*
- * four character sequence (21-bit value)
- * 10000-1FFFFF => T4 Tx Tx Tx
- */
- c3 = *(const unsigned char*)(str+3) ^ Tx;
- if (c3 & Testx)
- goto bad;
- if (c < T5) {
- l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
- if (l <= Rune3)
- goto bad;
- *rune = l;
- return 4;
- }
- /*
- * Support for 5-byte or longer UTF-8 would go here, but
- * since we don't have that, we'll just fall through to bad.
- */
-
- /*
- * bad decoding
- */
-bad:
- *rune = Bad;
- return 1;
-}
-
-int
-fz_runetochar(char *str, int rune)
-{
- /* Runes are signed, so convert to unsigned for range check. */
- unsigned long c = (unsigned long)rune;
-
- /*
- * one character sequence
- * 00000-0007F => 00-7F
- */
- if(c <= Rune1) {
- str[0] = c;
- return 1;
- }
-
- /*
- * two character sequence
- * 0080-07FF => T2 Tx
- */
- if(c <= Rune2) {
- str[0] = T2 | (c >> 1*Bitx);
- str[1] = Tx | (c & Maskx);
- return 2;
- }
-
- /*
- * If the Rune is out of range, convert it to the error rune.
- * Do this test here because the error rune encodes to three bytes.
- * Doing it earlier would duplicate work, since an out of range
- * Rune wouldn't have fit in one or two bytes.
- */
- if (c > Runemax)
- c = Runeerror;
-
- /*
- * three character sequence
- * 0800-FFFF => T3 Tx Tx
- */
- if (c <= Rune3) {
- str[0] = T3 | (c >> 2*Bitx);
- str[1] = Tx | ((c >> 1*Bitx) & Maskx);
- str[2] = Tx | (c & Maskx);
- return 3;
- }
-
- /*
- * four character sequence (21-bit value)
- * 10000-1FFFFF => T4 Tx Tx Tx
- */
- str[0] = T4 | (c >> 3*Bitx);
- str[1] = Tx | ((c >> 2*Bitx) & Maskx);
- str[2] = Tx | ((c >> 1*Bitx) & Maskx);
- str[3] = Tx | (c & Maskx);
- return 4;
-}
-
-int
-fz_runelen(int c)
-{
- char str[10];
- return fz_runetochar(str, c);
-}
-
-float fz_atof(const char *s)
-{
- double d;
-
- /* The errno voodoo here checks for us reading numbers that are too
- * big to fit into a double. The checks for FLT_MAX ensure that we
- * don't read a number that's OK as a double and then become invalid
- * as we convert to a float. */
- errno = 0;
- d = strtod(s, NULL);
- if (errno == ERANGE || isnan(d)) {
- /* Return 1.0, as it's a small known value that won't cause a divide by 0. */
- return 1.0;
- }
- d = fz_clampd(d, -FLT_MAX, FLT_MAX);
- return (float)d;
-}
-
-int fz_atoi(const char *s)
-{
- if (s == NULL)
- return 0;
- return atoi(s);
-}
diff --git a/fitz/base_time.c b/fitz/base_time.c
deleted file mode 100644
index 0e3d21b5..00000000
--- a/fitz/base_time.c
+++ /dev/null
@@ -1,144 +0,0 @@
-#ifdef _MSC_VER
-
-#include "mupdf/fitz.h"
-
-#include
-#include
-
-#ifndef _WINRT
-
-#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
-
-struct timeval;
-struct timezone;
-
-int gettimeofday(struct timeval *tv, struct timezone *tz)
-{
- FILETIME ft;
- unsigned __int64 tmpres = 0;
-
- if (tv)
- {
- GetSystemTimeAsFileTime(&ft);
-
- tmpres |= ft.dwHighDateTime;
- tmpres <<= 32;
- tmpres |= ft.dwLowDateTime;
-
- tmpres /= 10; /*convert into microseconds*/
- /*converting file time to unix epoch*/
- tmpres -= DELTA_EPOCH_IN_MICROSECS;
- tv->tv_sec = (long)(tmpres / 1000000UL);
- tv->tv_usec = (long)(tmpres % 1000000UL);
- }
-
- return 0;
-}
-
-#endif /* !_WINRT */
-
-char *
-fz_utf8_from_wchar(const wchar_t *s)
-{
- const wchar_t *src = s;
- char *d;
- char *dst;
- int len = 1;
-
- while (*src)
- {
- len += fz_runelen(*src++);
- }
-
- d = malloc(len);
- if (d != NULL)
- {
- dst = d;
- src = s;
- while (*src)
- {
- dst += fz_runetochar(dst, *src++);
- }
- *dst = 0;
- }
- return d;
-}
-
-wchar_t *
-fz_wchar_from_utf8(const char *s)
-{
- wchar_t *d, *r;
- int c;
- r = d = malloc((strlen(s) + 1) * sizeof(wchar_t));
- if (!r)
- return NULL;
- while (*s) {
- s += fz_chartorune(&c, s);
- *d++ = c;
- }
- *d = 0;
- return r;
-}
-
-FILE *
-fz_fopen_utf8(const char *name, const char *mode)
-{
- wchar_t *wname, *wmode;
- FILE *file;
-
- wname = fz_wchar_from_utf8(name);
- if (wname == NULL)
- {
- return NULL;
- }
-
- wmode = fz_wchar_from_utf8(mode);
- if (wmode == NULL)
- {
- free(wname);
- return NULL;
- }
-
- file = _wfopen(wname, wmode);
-
- free(wname);
- free(wmode);
- return file;
-}
-
-char **
-fz_argv_from_wargv(int argc, wchar_t **wargv)
-{
- char **argv;
- int i;
-
- argv = calloc(argc, sizeof(char *));
- if (argv == NULL)
- {
- fprintf(stderr, "Out of memory while processing command line args!\n");
- exit(1);
- }
-
- for (i = 0; i < argc; i++)
- {
- argv[i] = fz_utf8_from_wchar(wargv[i]);
- if (argv[i] == NULL)
- {
- fprintf(stderr, "Out of memory while processing command line args!\n");
- exit(1);
- }
- }
-
- return argv;
-}
-
-void
-fz_free_argv(int argc, char **argv)
-{
- int i;
- for (i = 0; i < argc; i++)
- free(argv[i]);
- free(argv);
-}
-
-#endif /* _MSC_VER */
diff --git a/fitz/base_trans.c b/fitz/base_trans.c
deleted file mode 100644
index 92582253..00000000
--- a/fitz/base_trans.c
+++ /dev/null
@@ -1,165 +0,0 @@
-#include "mupdf/fitz.h"
-
-static int
-fade(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
-{
- unsigned char *t, *o, *n;
- int size;
-
- if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
- return 0;
- size = tpix->w * tpix->h * tpix->n;
- t = tpix->samples;
- o = opix->samples;
- n = npix->samples;
- while (size-- > 0)
- {
- int op = *o++;
- int np = *n++;
- *t++ = ((op<<8) + ((np-op) * time) + 0x80)>>8;
- }
- return 1;
-}
-
-static int
-blind_horiz(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
-{
- unsigned char *t, *o, *n;
- int blind_height, span, position, y;
-
- if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
- return 0;
- span = tpix->w * tpix->n;
- blind_height = (tpix->h+7) / 8;
- position = blind_height * time / 256;
- t = tpix->samples;
- o = opix->samples;
- n = npix->samples;
- for (y = 0; y < tpix->h; y++)
- {
- memcpy(t, ((y % blind_height) <= position ? n : o), span);
- t += span;
- o += span;
- n += span;
- }
- return 1;
-}
-
-static int
-blind_vertical(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
-{
- unsigned char *t, *o, *n;
- int blind_width, span, position, y;
-
- if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
- return 0;
- span = tpix->w * tpix->n;
- blind_width = (tpix->w+7) / 8;
- position = blind_width * time / 256;
- blind_width *= tpix->n;
- position *= tpix->n;
- t = tpix->samples;
- o = opix->samples;
- n = npix->samples;
- for (y = 0; y < tpix->h; y++)
- {
- int w, x;
- x = 0;
- while ((w = span - x) > 0)
- {
- int p;
- if (w > blind_width)
- w = blind_width;
- p = position;
- if (p > w)
- p = w;
- memcpy(t, n, p);
- memcpy(t+position, o+position, w - p);
- x += blind_width;
- t += w;
- o += w;
- n += w;
- }
- }
- return 1;
-}
-
-static int
-wipe_tb(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
-{
- unsigned char *t, *o, *n;
- int span, position, y;
-
- if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
- return 0;
- span = tpix->w * tpix->n;
- position = tpix->h * time / 256;
- t = tpix->samples;
- o = opix->samples;
- n = npix->samples;
- for (y = 0; y < position; y++)
- {
- memcpy(t, n, span);
- t += span;
- o += span;
- n += span;
- }
- for (; y < tpix->h; y++)
- {
- memcpy(t, o, span);
- t += span;
- o += span;
- n += span;
- }
- return 1;
-}
-
-static int
-wipe_lr(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
-{
- unsigned char *t, *o, *n;
- int span, position, y;
-
- if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
- return 0;
- span = tpix->w * tpix->n;
- position = tpix->w * time / 256;
- position *= tpix->n;
- t = tpix->samples;
- o = opix->samples + position;
- n = npix->samples;
- for (y = 0; y < tpix->h; y++)
- {
- memcpy(t, n, position);
- memcpy(t+position, o, span-position);
- t += span;
- o += span;
- n += span;
- }
- return 1;
-}
-
-int fz_generate_transition(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time, fz_transition *trans)
-{
- switch (trans->type)
- {
- default:
- case FZ_TRANSITION_FADE:
- return fade(tpix, opix, npix, time);
- case FZ_TRANSITION_BLINDS:
- if (trans->vertical)
- return blind_vertical(tpix, opix, npix, time);
- else
- return blind_horiz(tpix, opix, npix, time);
- case FZ_TRANSITION_WIPE:
- switch (((trans->direction + 45 + 360) % 360) / 90)
- {
- default:
- case 0: return wipe_lr(tpix, opix, npix, time);
- case 1: return wipe_tb(tpix, npix, opix, 256-time);
- case 2: return wipe_lr(tpix, npix, opix, 256-time);
- case 3: return wipe_tb(tpix, opix, npix, time);
- }
- }
- return 0;
-}
diff --git a/fitz/base_xml.c b/fitz/base_xml.c
deleted file mode 100644
index 8c97562c..00000000
--- a/fitz/base_xml.c
+++ /dev/null
@@ -1,460 +0,0 @@
-#include "mupdf/fitz.h"
-
-struct parser
-{
- fz_xml *head;
- fz_context *ctx;
-};
-
-struct attribute
-{
- char name[40];
- char *value;
- struct attribute *next;
-};
-
-struct fz_xml_s
-{
- char name[40];
- char *text;
- struct attribute *atts;
- fz_xml *up, *down, *next;
-};
-
-static inline void indent(int n)
-{
- while (n--) putchar(' ');
-}
-
-void fz_debug_xml(fz_xml *item, int level)
-{
- while (item) {
- if (item->text) {
- printf("%s\n", item->text);
- } else {
- struct attribute *att;
- indent(level);
- printf("<%s", item->name);
- for (att = item->atts; att; att = att->next)
- printf(" %s=\"%s\"", att->name, att->value);
- if (item->down) {
- printf(">\n");
- fz_debug_xml(item->down, level + 1);
- indent(level);
- printf("%s>\n", item->name);
- }
- else {
- printf("/>\n");
- }
- }
- item = item->next;
- }
-}
-
-fz_xml *fz_xml_next(fz_xml *item)
-{
- return item->next;
-}
-
-fz_xml *fz_xml_down(fz_xml *item)
-{
- return item->down;
-}
-
-char *fz_xml_text(fz_xml *item)
-{
- return item->text;
-}
-
-char *fz_xml_tag(fz_xml *item)
-{
- return item->name;
-}
-
-char *fz_xml_att(fz_xml *item, const char *name)
-{
- struct attribute *att;
- for (att = item->atts; att; att = att->next)
- if (!strcmp(att->name, name))
- return att->value;
- return NULL;
-}
-
-static void xml_free_attribute(fz_context *ctx, struct attribute *att)
-{
- while (att) {
- struct attribute *next = att->next;
- if (att->value)
- fz_free(ctx, att->value);
- fz_free(ctx, att);
- att = next;
- }
-}
-
-void fz_free_xml(fz_context *ctx, fz_xml *item)
-{
- while (item)
- {
- fz_xml *next = item->next;
- if (item->text)
- fz_free(ctx, item->text);
- if (item->atts)
- xml_free_attribute(ctx, item->atts);
- if (item->down)
- fz_free_xml(ctx, item->down);
- fz_free(ctx, item);
- item = next;
- }
-}
-
-void fz_detach_xml(fz_xml *node)
-{
- if (node->up)
- node->up->down = NULL;
-}
-
-static int xml_parse_entity(int *c, char *a)
-{
- char *b;
- if (a[1] == '#') {
- if (a[2] == 'x')
- *c = strtol(a + 3, &b, 16);
- else
- *c = strtol(a + 2, &b, 10);
- if (*b == ';')
- return b - a + 1;
- }
- else if (a[1] == 'l' && a[2] == 't' && a[3] == ';') {
- *c = '<';
- return 4;
- }
- else if (a[1] == 'g' && a[2] == 't' && a[3] == ';') {
- *c = '>';
- return 4;
- }
- else if (a[1] == 'a' && a[2] == 'm' && a[3] == 'p' && a[4] == ';') {
- *c = '&';
- return 5;
- }
- else if (a[1] == 'a' && a[2] == 'p' && a[3] == 'o' && a[4] == 's' && a[5] == ';') {
- *c = '\'';
- return 6;
- }
- else if (a[1] == 'q' && a[2] == 'u' && a[3] == 'o' && a[4] == 't' && a[5] == ';') {
- *c = '"';
- return 6;
- }
- *c = *a++;
- return 1;
-}
-
-static inline int isname(int c)
-{
- return c == '.' || c == '-' || c == '_' || c == ':' ||
- (c >= '0' && c <= '9') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z');
-}
-
-static inline int iswhite(int c)
-{
- return c == ' ' || c == '\r' || c == '\n' || c == '\t';
-}
-
-static void xml_emit_open_tag(struct parser *parser, char *a, char *b)
-{
- fz_xml *head, *tail;
-
- head = fz_malloc_struct(parser->ctx, fz_xml);
- if (b - a > sizeof(head->name) - 1)
- b = a + sizeof(head->name) - 1;
- memcpy(head->name, a, b - a);
- head->name[b - a] = 0;
-
- head->atts = NULL;
- head->text = NULL;
- head->up = parser->head;
- head->down = NULL;
- head->next = NULL;
-
- if (!parser->head->down) {
- parser->head->down = head;
- }
- else {
- tail = parser->head->down;
- while (tail->next)
- tail = tail->next;
- tail->next = head;
- }
-
- parser->head = head;
-}
-
-static void xml_emit_att_name(struct parser *parser, char *a, char *b)
-{
- fz_xml *head = parser->head;
- struct attribute *att;
-
- att = fz_malloc_struct(parser->ctx, struct attribute);
- if (b - a > sizeof(att->name) - 1)
- b = a + sizeof(att->name) - 1;
- memcpy(att->name, a, b - a);
- att->name[b - a] = 0;
- att->value = NULL;
- att->next = head->atts;
- head->atts = att;
-}
-
-static void xml_emit_att_value(struct parser *parser, char *a, char *b)
-{
- fz_xml *head = parser->head;
- struct attribute *att = head->atts;
- char *s;
- int c;
-
- /* entities are all longer than UTFmax so runetochar is safe */
- s = att->value = fz_malloc(parser->ctx, b - a + 1);
- while (a < b) {
- if (*a == '&') {
- a += xml_parse_entity(&c, a);
- s += fz_runetochar(s, c);
- }
- else {
- *s++ = *a++;
- }
- }
- *s = 0;
-}
-
-static void xml_emit_close_tag(struct parser *parser)
-{
- if (parser->head->up)
- parser->head = parser->head->up;
-}
-
-static void xml_emit_text(struct parser *parser, char *a, char *b)
-{
- static char *empty = "";
- fz_xml *head;
- char *s;
- int c;
-
- /* Skip all-whitespace text nodes */
- for (s = a; s < b; s++)
- if (!iswhite(*s))
- break;
- if (s == b)
- return;
-
- xml_emit_open_tag(parser, empty, empty);
- head = parser->head;
-
- /* entities are all longer than UTFmax so runetochar is safe */
- s = head->text = fz_malloc(parser->ctx, b - a + 1);
- while (a < b) {
- if (*a == '&') {
- a += xml_parse_entity(&c, a);
- s += fz_runetochar(s, c);
- }
- else {
- *s++ = *a++;
- }
- }
- *s = 0;
-
- xml_emit_close_tag(parser);
-}
-
-static char *xml_parse_document_imp(struct parser *x, char *p)
-{
- char *mark;
- int quote;
-
-parse_text:
- mark = p;
- while (*p && *p != '<') ++p;
- xml_emit_text(x, mark, p);
- if (*p == '<') { ++p; goto parse_element; }
- return NULL;
-
-parse_element:
- if (*p == '/') { ++p; goto parse_closing_element; }
- if (*p == '!') { ++p; goto parse_comment; }
- if (*p == '?') { ++p; goto parse_processing_instruction; }
- while (iswhite(*p)) ++p;
- if (isname(*p))
- goto parse_element_name;
- return "syntax error in element";
-
-parse_comment:
- if (*p == '[') goto parse_cdata;
- if (*p++ != '-') return "syntax error in comment (') {
- p += 3;
- goto parse_text;
- }
- ++p;
- }
- return "end of data in comment";
-
-parse_cdata:
- if (p[1] != 'C' || p[2] != 'D' || p[3] != 'A' || p[4] != 'T' || p[5] != 'A' || p[6] != '[')
- return "syntax error in CDATA section";
- p += 7;
- mark = p;
- while (*p) {
- if (p[0] == ']' && p[1] == ']' && p[2] == '>') {
- p += 3;
- goto parse_text;
- }
- ++p;
- }
- return "end of data in CDATA section";
-
-parse_processing_instruction:
- while (*p) {
- if (p[0] == '?' && p[1] == '>') {
- p += 2;
- goto parse_text;
- }
- ++p;
- }
- return "end of data in processing instruction";
-
-parse_closing_element:
- while (iswhite(*p)) ++p;
- mark = p;
- while (isname(*p)) ++p;
- while (iswhite(*p)) ++p;
- if (*p != '>')
- return "syntax error in closing element";
- xml_emit_close_tag(x);
- ++p;
- goto parse_text;
-
-parse_element_name:
- mark = p;
- while (isname(*p)) ++p;
- xml_emit_open_tag(x, mark, p);
- if (*p == '>') { ++p; goto parse_text; }
- if (p[0] == '/' && p[1] == '>') {
- xml_emit_close_tag(x);
- p += 2;
- goto parse_text;
- }
- if (iswhite(*p))
- goto parse_attributes;
- return "syntax error after element name";
-
-parse_attributes:
- while (iswhite(*p)) ++p;
- if (isname(*p))
- goto parse_attribute_name;
- if (*p == '>') { ++p; goto parse_text; }
- if (p[0] == '/' && p[1] == '>') {
- xml_emit_close_tag(x);
- p += 2;
- goto parse_text;
- }
- return "syntax error in attributes";
-
-parse_attribute_name:
- mark = p;
- while (isname(*p)) ++p;
- xml_emit_att_name(x, mark, p);
- while (iswhite(*p)) ++p;
- if (*p == '=') { ++p; goto parse_attribute_value; }
- return "syntax error after attribute name";
-
-parse_attribute_value:
- while (iswhite(*p)) ++p;
- quote = *p++;
- if (quote != '"' && quote != '\'')
- return "missing quote character";
- mark = p;
- while (*p && *p != quote) ++p;
- if (*p == quote) {
- xml_emit_att_value(x, mark, p++);
- goto parse_attributes;
- }
- return "end of data in attribute value";
-}
-
-static char *convert_to_utf8(fz_context *doc, unsigned char *s, int n, int *dofree)
-{
- unsigned char *e = s + n;
- char *dst, *d;
- int c;
-
- if (s[0] == 0xFE && s[1] == 0xFF) {
- s += 2;
- dst = d = fz_malloc(doc, n * 2);
- while (s + 1 < e) {
- c = s[0] << 8 | s[1];
- d += fz_runetochar(d, c);
- s += 2;
- }
- *d = 0;
- *dofree = 1;
- return dst;
- }
-
- if (s[0] == 0xFF && s[1] == 0xFE) {
- s += 2;
- dst = d = fz_malloc(doc, n * 2);
- while (s + 1 < e) {
- c = s[0] | s[1] << 8;
- d += fz_runetochar(d, c);
- s += 2;
- }
- *d = 0;
- *dofree = 1;
- return dst;
- }
-
- *dofree = 0;
-
- if (s[0] == 0xEF && s[1] == 0xBB && s[2] == 0xBF)
- return (char*)s+3;
-
- return (char*)s;
-}
-
-fz_xml *
-fz_parse_xml(fz_context *ctx, unsigned char *s, int n)
-{
- struct parser parser;
- fz_xml root;
- char *p, *error;
- int dofree;
-
- /* s is already null-terminated (see xps_new_part) */
-
- memset(&root, 0, sizeof(root));
- parser.head = &root;
- parser.ctx = ctx;
-
- p = convert_to_utf8(ctx, s, n, &dofree);
-
- fz_try(ctx)
- {
- error = xml_parse_document_imp(&parser, p);
- if (error)
- fz_throw(ctx, FZ_ERROR_GENERIC, "%s", error);
- }
- fz_always(ctx)
- {
- if (dofree)
- fz_free(ctx, p);
- }
- fz_catch(ctx)
- {
- fz_free_xml(ctx, root.down);
- fz_rethrow(ctx);
- }
-
- return root.down;
-}
diff --git a/fitz/crypt_aes.c b/fitz/crypt_aes.c
deleted file mode 100644
index 6ce14903..00000000
--- a/fitz/crypt_aes.c
+++ /dev/null
@@ -1,569 +0,0 @@
-/*
- * FIPS-197 compliant AES implementation
- *
- * Copyright (C) 2006-2007 Christophe Devine
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code _must_ retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form may or may not reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- * * Neither the name of XySSL nor the names of its contributors may be
- * used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-/*
- * The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
- *
- * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
- * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
- */
-
-#include "mupdf/fitz.h"
-
-#define aes_context fz_aes
-
-/* AES block cipher implementation from XYSSL */
-
-/*
- * 32-bit integer manipulation macros (little endian)
- */
-#ifndef GET_ULONG_LE
-#define GET_ULONG_LE(n,b,i) \
-{ \
- (n) = ( (unsigned long) (b)[(i)] ) \
- | ( (unsigned long) (b)[(i) + 1] << 8 ) \
- | ( (unsigned long) (b)[(i) + 2] << 16 ) \
- | ( (unsigned long) (b)[(i) + 3] << 24 ); \
-}
-#endif
-
-#ifndef PUT_ULONG_LE
-#define PUT_ULONG_LE(n,b,i) \
-{ \
- (b)[(i) ] = (unsigned char) ( (n) ); \
- (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
- (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
- (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
-}
-#endif
-
-/*
- * Forward S-box & tables
- */
-static unsigned char FSb[256];
-static unsigned long FT0[256];
-static unsigned long FT1[256];
-static unsigned long FT2[256];
-static unsigned long FT3[256];
-
-/*
- * Reverse S-box & tables
- */
-static unsigned char RSb[256];
-static unsigned long RT0[256];
-static unsigned long RT1[256];
-static unsigned long RT2[256];
-static unsigned long RT3[256];
-
-/*
- * Round constants
- */
-static unsigned long RCON[10];
-
-/*
- * Tables generation code
- */
-#define ROTL8(x) ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
-#define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
-#define MUL(x,y) ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )
-
-static int aes_init_done = 0;
-
-static void aes_gen_tables( void )
-{
- int i, x, y, z;
- int pow[256];
- int log[256];
-
- /*
- * compute pow and log tables over GF(2^8)
- */
- for( i = 0, x = 1; i < 256; i++ )
- {
- pow[i] = x;
- log[x] = i;
- x = ( x ^ XTIME( x ) ) & 0xFF;
- }
-
- /*
- * calculate the round constants
- */
- for( i = 0, x = 1; i < 10; i++ )
- {
- RCON[i] = (unsigned long) x;
- x = XTIME( x ) & 0xFF;
- }
-
- /*
- * generate the forward and reverse S-boxes
- */
- FSb[0x00] = 0x63;
- RSb[0x63] = 0x00;
-
- for( i = 1; i < 256; i++ )
- {
- x = pow[255 - log[i]];
-
- y = x; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
- x ^= y ^ 0x63;
-
- FSb[i] = (unsigned char) x;
- RSb[x] = (unsigned char) i;
- }
-
- /*
- * generate the forward and reverse tables
- */
- for( i = 0; i < 256; i++ )
- {
- x = FSb[i];
- y = XTIME( x ) & 0xFF;
- z = ( y ^ x ) & 0xFF;
-
- FT0[i] = ( (unsigned long) y ) ^
- ( (unsigned long) x << 8 ) ^
- ( (unsigned long) x << 16 ) ^
- ( (unsigned long) z << 24 );
-
- FT1[i] = ROTL8( FT0[i] );
- FT2[i] = ROTL8( FT1[i] );
- FT3[i] = ROTL8( FT2[i] );
-
- x = RSb[i];
-
- RT0[i] = ( (unsigned long) MUL( 0x0E, x ) ) ^
- ( (unsigned long) MUL( 0x09, x ) << 8 ) ^
- ( (unsigned long) MUL( 0x0D, x ) << 16 ) ^
- ( (unsigned long) MUL( 0x0B, x ) << 24 );
-
- RT1[i] = ROTL8( RT0[i] );
- RT2[i] = ROTL8( RT1[i] );
- RT3[i] = ROTL8( RT2[i] );
- }
-}
-
-/*
- * AES key schedule (encryption)
- */
-int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize )
-{
- int i;
- unsigned long *RK;
-
-#if !defined(XYSSL_AES_ROM_TABLES)
- if( aes_init_done == 0 )
- {
- aes_gen_tables();
- aes_init_done = 1;
- }
-#endif
-
- switch( keysize )
- {
- case 128: ctx->nr = 10; break;
- case 192: ctx->nr = 12; break;
- case 256: ctx->nr = 14; break;
- default : return 1;
- }
-
-#if defined(PADLOCK_ALIGN16)
- ctx->rk = RK = PADLOCK_ALIGN16( ctx->buf );
-#else
- ctx->rk = RK = ctx->buf;
-#endif
-
- for( i = 0; i < (keysize >> 5); i++ )
- {
- GET_ULONG_LE( RK[i], key, i << 2 );
- }
-
- switch( ctx->nr )
- {
- case 10:
-
- for( i = 0; i < 10; i++, RK += 4 )
- {
- RK[4] = RK[0] ^ RCON[i] ^
- ( FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^
- ( FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( RK[3] ) & 0xFF ] << 24 );
-
- RK[5] = RK[1] ^ RK[4];
- RK[6] = RK[2] ^ RK[5];
- RK[7] = RK[3] ^ RK[6];
- }
- break;
-
- case 12:
-
- for( i = 0; i < 8; i++, RK += 6 )
- {
- RK[6] = RK[0] ^ RCON[i] ^
- ( FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^
- ( FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( RK[5] ) & 0xFF ] << 24 );
-
- RK[7] = RK[1] ^ RK[6];
- RK[8] = RK[2] ^ RK[7];
- RK[9] = RK[3] ^ RK[8];
- RK[10] = RK[4] ^ RK[9];
- RK[11] = RK[5] ^ RK[10];
- }
- break;
-
- case 14:
-
- for( i = 0; i < 7; i++, RK += 8 )
- {
- RK[8] = RK[0] ^ RCON[i] ^
- ( FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^
- ( FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( RK[7] ) & 0xFF ] << 24 );
-
- RK[9] = RK[1] ^ RK[8];
- RK[10] = RK[2] ^ RK[9];
- RK[11] = RK[3] ^ RK[10];
-
- RK[12] = RK[4] ^
- ( FSb[ ( RK[11] ) & 0xFF ] ) ^
- ( FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );
-
- RK[13] = RK[5] ^ RK[12];
- RK[14] = RK[6] ^ RK[13];
- RK[15] = RK[7] ^ RK[14];
- }
- break;
-
- default:
-
- break;
- }
- return 0;
-}
-
-/*
- * AES key schedule (decryption)
- */
-int aes_setkey_dec(aes_context *ctx, const unsigned char *key, int keysize)
-{
- int i, j;
- aes_context cty;
- unsigned long *RK;
- unsigned long *SK;
-
- switch( keysize )
- {
- case 128: ctx->nr = 10; break;
- case 192: ctx->nr = 12; break;
- case 256: ctx->nr = 14; break;
- default: return 1;
- }
-
-#if defined(PADLOCK_ALIGN16)
- ctx->rk = RK = PADLOCK_ALIGN16( ctx->buf );
-#else
- ctx->rk = RK = ctx->buf;
-#endif
-
- i = aes_setkey_enc( &cty, key, keysize );
- if (i)
- return i;
- SK = cty.rk + cty.nr * 4;
-
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
-
- for( i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8 )
- {
- for( j = 0; j < 4; j++, SK++ )
- {
- *RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
- RT1[ FSb[ ( *SK >> 8 ) & 0xFF ] ] ^
- RT2[ FSb[ ( *SK >> 16 ) & 0xFF ] ] ^
- RT3[ FSb[ ( *SK >> 24 ) & 0xFF ] ];
- }
- }
-
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
- *RK++ = *SK++;
-
- memset( &cty, 0, sizeof( aes_context ) );
- return 0;
-}
-
-#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
-{ \
- X0 = *RK++ ^ FT0[ ( Y0 ) & 0xFF ] ^ \
- FT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y3 >> 24 ) & 0xFF ]; \
- \
- X1 = *RK++ ^ FT0[ ( Y1 ) & 0xFF ] ^ \
- FT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y0 >> 24 ) & 0xFF ]; \
- \
- X2 = *RK++ ^ FT0[ ( Y2 ) & 0xFF ] ^ \
- FT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y1 >> 24 ) & 0xFF ]; \
- \
- X3 = *RK++ ^ FT0[ ( Y3 ) & 0xFF ] ^ \
- FT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
- FT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
- FT3[ ( Y2 >> 24 ) & 0xFF ]; \
-}
-
-#define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
-{ \
- X0 = *RK++ ^ RT0[ ( Y0 ) & 0xFF ] ^ \
- RT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y1 >> 24 ) & 0xFF ]; \
- \
- X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \
- RT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y2 >> 24 ) & 0xFF ]; \
- \
- X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \
- RT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y3 >> 24 ) & 0xFF ]; \
- \
- X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \
- RT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
- RT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
- RT3[ ( Y0 >> 24 ) & 0xFF ]; \
-}
-
-/*
- * AES-ECB block encryption/decryption
- */
-void aes_crypt_ecb( aes_context *ctx,
- int mode,
- const unsigned char input[16],
- unsigned char output[16] )
-{
- int i;
- unsigned long *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
-
-#if defined(XYSSL_PADLOCK_C) && defined(XYSSL_HAVE_X86)
- if( padlock_supports( PADLOCK_ACE ) )
- {
- if( padlock_xcryptecb( ctx, mode, input, output ) == 0 )
- return;
- }
-#endif
-
- RK = ctx->rk;
-
- GET_ULONG_LE( X0, input, 0 ); X0 ^= *RK++;
- GET_ULONG_LE( X1, input, 4 ); X1 ^= *RK++;
- GET_ULONG_LE( X2, input, 8 ); X2 ^= *RK++;
- GET_ULONG_LE( X3, input, 12 ); X3 ^= *RK++;
-
- if( mode == AES_DECRYPT )
- {
- for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
- {
- AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
- AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
- }
-
- AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
-
- X0 = *RK++ ^ ( RSb[ ( Y0 ) & 0xFF ] ) ^
- ( RSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
- ( RSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
- ( RSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
-
- X1 = *RK++ ^ ( RSb[ ( Y1 ) & 0xFF ] ) ^
- ( RSb[ ( Y0 >>8 ) & 0xFF ] << 8 ) ^
- ( RSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
- ( RSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
-
- X2 = *RK++ ^ ( RSb[ ( Y2 ) & 0xFF ] ) ^
- ( RSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
- ( RSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
- ( RSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
-
- X3 = *RK++ ^ ( RSb[ ( Y3 ) & 0xFF ] ) ^
- ( RSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
- ( RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
- ( RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
- }
- else /* AES_ENCRYPT */
- {
- for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
- {
- AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
- AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
- }
-
- AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
-
- X0 = *RK++ ^ ( FSb[ ( Y0 ) & 0xFF ] ) ^
- ( FSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
-
- X1 = *RK++ ^ ( FSb[ ( Y1 ) & 0xFF ] ) ^
- ( FSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
-
- X2 = *RK++ ^ ( FSb[ ( Y2 ) & 0xFF ] ) ^
- ( FSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
-
- X3 = *RK++ ^ ( FSb[ ( Y3 ) & 0xFF ] ) ^
- ( FSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
- ( FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
- ( FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
- }
-
- PUT_ULONG_LE( X0, output, 0 );
- PUT_ULONG_LE( X1, output, 4 );
- PUT_ULONG_LE( X2, output, 8 );
- PUT_ULONG_LE( X3, output, 12 );
-}
-
-/*
- * AES-CBC buffer encryption/decryption
- */
-void aes_crypt_cbc( aes_context *ctx,
- int mode,
- int length,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output )
-{
- int i;
- unsigned char temp[16];
-
-#if defined(XYSSL_PADLOCK_C) && defined(XYSSL_HAVE_X86)
- if( padlock_supports( PADLOCK_ACE ) )
- {
- if( padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
- return;
- }
-#endif
-
- if( mode == AES_DECRYPT )
- {
- while( length > 0 )
- {
- memcpy( temp, input, 16 );
- aes_crypt_ecb( ctx, mode, input, output );
-
- for( i = 0; i < 16; i++ )
- output[i] = (unsigned char)( output[i] ^ iv[i] );
-
- memcpy( iv, temp, 16 );
-
- input += 16;
- output += 16;
- length -= 16;
- }
- }
- else
- {
- while( length > 0 )
- {
- for( i = 0; i < 16; i++ )
- output[i] = (unsigned char)( input[i] ^ iv[i] );
-
- aes_crypt_ecb( ctx, mode, output, output );
- memcpy( iv, output, 16 );
-
- input += 16;
- output += 16;
- length -= 16;
- }
- }
-}
-
-/*
- * AES-CFB buffer encryption/decryption
- */
-void aes_crypt_cfb( aes_context *ctx,
- int mode,
- int length,
- int *iv_off,
- unsigned char iv[16],
- const unsigned char *input,
- unsigned char *output )
-{
- int c, n = *iv_off;
-
- if( mode == AES_DECRYPT )
- {
- while( length-- )
- {
- if( n == 0 )
- aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
-
- c = *input++;
- *output++ = (unsigned char)( c ^ iv[n] );
- iv[n] = (unsigned char) c;
-
- n = (n + 1) & 0x0F;
- }
- }
- else
- {
- while( length-- )
- {
- if( n == 0 )
- aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
-
- iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
-
- n = (n + 1) & 0x0F;
- }
- }
-
- *iv_off = n;
-}
diff --git a/fitz/crypt_arc4.c b/fitz/crypt_arc4.c
deleted file mode 100644
index 9c54fbae..00000000
--- a/fitz/crypt_arc4.c
+++ /dev/null
@@ -1,98 +0,0 @@
-/* This code illustrates a sample implementation
- * of the Arcfour algorithm
- * Copyright (c) April 29, 1997 Kalle Kaukonen.
- * All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that this copyright
- * notice and disclaimer are retained.
- *
- * THIS SOFTWARE IS PROVIDED BY KALLE KAUKONEN AND CONTRIBUTORS ``AS
- * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KALLE
- * KAUKONEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "mupdf/fitz.h"
-
-void
-fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, unsigned keylen)
-{
- unsigned int t, u;
- unsigned int keyindex;
- unsigned int stateindex;
- unsigned char *state;
- unsigned int counter;
-
- state = arc4->state;
-
- arc4->x = 0;
- arc4->y = 0;
-
- for (counter = 0; counter < 256; counter++)
- {
- state[counter] = counter;
- }
-
- keyindex = 0;
- stateindex = 0;
-
- for (counter = 0; counter < 256; counter++)
- {
- t = state[counter];
- stateindex = (stateindex + key[keyindex] + t) & 0xff;
- u = state[stateindex];
-
- state[stateindex] = t;
- state[counter] = u;
-
- if (++keyindex >= keylen)
- {
- keyindex = 0;
- }
- }
-}
-
-static unsigned char
-fz_arc4_next(fz_arc4 *arc4)
-{
- unsigned int x;
- unsigned int y;
- unsigned int sx, sy;
- unsigned char *state;
-
- state = arc4->state;
-
- x = (arc4->x + 1) & 0xff;
- sx = state[x];
- y = (sx + arc4->y) & 0xff;
- sy = state[y];
-
- arc4->x = x;
- arc4->y = y;
-
- state[y] = sx;
- state[x] = sy;
-
- return state[(sx + sy) & 0xff];
-}
-
-void
-fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, unsigned len)
-{
- unsigned int i;
- for (i = 0; i < len; i++)
- {
- unsigned char x;
- x = fz_arc4_next(arc4);
- dest[i] = src[i] ^ x;
- }
-}
diff --git a/fitz/crypt_md5.c b/fitz/crypt_md5.c
deleted file mode 100644
index 7490c0bc..00000000
--- a/fitz/crypt_md5.c
+++ /dev/null
@@ -1,272 +0,0 @@
-/*
-MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
-
-Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991.
-All rights reserved.
-
-License to copy and use this software is granted provided that it
-is identified as the "RSA Data Security, Inc. MD5 Message-Digest
-Algorithm" in all material mentioning or referencing this software
-or this function.
-
-License is also granted to make and use derivative works provided
-that such works are identified as "derived from the RSA Data
-Security, Inc. MD5 Message-Digest Algorithm" in all material
-mentioning or referencing the derived work.
-
-RSA Data Security, Inc. makes no representations concerning either
-the merchantability of this software or the suitability of this
-software for any particular purpose. It is provided "as is"
-without express or implied warranty of any kind.
-
-These notices must be retained in any copies of any part of this
-documentation and/or software.
-*/
-
-#include "mupdf/fitz.h"
-
-/* Constants for MD5Transform routine */
-enum
-{
- S11 = 7, S12 = 12, S13 = 17, S14 = 22,
- S21 = 5, S22 = 9, S23 = 14, S24 = 20,
- S31 = 4, S32 = 11, S33 = 16, S34 = 23,
- S41 = 6, S42 = 10, S43 = 15, S44 = 21
-};
-
-static void encode(unsigned char *, const unsigned int *, const unsigned);
-static void decode(unsigned int *, const unsigned char *, const unsigned);
-static void transform(unsigned int state[4], const unsigned char block[64]);
-
-static unsigned char padding[64] =
-{
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
-/* F, G, H and I are basic MD5 functions */
-#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
-#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
-#define H(x, y, z) ((x) ^ (y) ^ (z))
-#define I(x, y, z) ((y) ^ ((x) | (~z)))
-
-/* ROTATE rotates x left n bits */
-#define ROTATE(x, n) (((x) << (n)) | ((x) >> (32-(n))))
-
-/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- * Rotation is separate from addition to prevent recomputation.
- */
-#define FF(a, b, c, d, x, s, ac) { \
- (a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); \
- (a) = ROTATE ((a), (s)); \
- (a) += (b); \
- }
-#define GG(a, b, c, d, x, s, ac) { \
- (a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); \
- (a) = ROTATE ((a), (s)); \
- (a) += (b); \
- }
-#define HH(a, b, c, d, x, s, ac) { \
- (a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); \
- (a) = ROTATE ((a), (s)); \
- (a) += (b); \
- }
-#define II(a, b, c, d, x, s, ac) { \
- (a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); \
- (a) = ROTATE ((a), (s)); \
- (a) += (b); \
- }
-
-static void encode(unsigned char *output, const unsigned int *input, const unsigned len)
-{
- unsigned i, j;
-
- for (i = 0, j = 0; j < len; i++, j += 4)
- {
- output[j] = (unsigned char)(input[i] & 0xff);
- output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
- output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
- output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
- }
-}
-
-static void decode(unsigned int *output, const unsigned char *input, const unsigned len)
-{
- unsigned i, j;
-
- for (i = 0, j = 0; j < len; i++, j += 4)
- {
- output[i] = ((unsigned int)input[j]) |
- (((unsigned int)input[j+1]) << 8) |
- (((unsigned int)input[j+2]) << 16) |
- (((unsigned int)input[j+3]) << 24);
- }
-}
-
-static void transform(unsigned int state[4], const unsigned char block[64])
-{
- unsigned int a = state[0];
- unsigned int b = state[1];
- unsigned int c = state[2];
- unsigned int d = state[3];
- unsigned int x[16];
-
- decode(x, block, 64);
-
- /* Round 1 */
- FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
- FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
- FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
- FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
- FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
- FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
- FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
- FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
- FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
- FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
- FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
- FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
- FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
- FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
- FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
- FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
-
- /* Round 2 */
- GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
- GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
- GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
- GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
- GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
- GG (d, a, b, c, x[10], S22, 0x02441453); /* 22 */
- GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
- GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
- GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
- GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
- GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
- GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
- GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
- GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
- GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
- GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
-
- /* Round 3 */
- HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
- HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
- HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
- HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
- HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
- HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
- HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
- HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
- HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
- HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
- HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
- HH (b, c, d, a, x[ 6], S34, 0x04881d05); /* 44 */
- HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
- HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
- HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
- HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
-
- /* Round 4 */
- II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
- II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
- II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
- II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
- II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
- II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
- II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
- II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
- II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
- II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
- II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
- II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
- II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
- II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
- II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
- II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
-
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
-
- /* Zeroize sensitive information */
- memset(x, 0, sizeof (x));
-}
-
-/* MD5 initialization. Begins an MD5 operation, writing a new context. */
-void fz_md5_init(fz_md5 *context)
-{
- context->count[0] = context->count[1] = 0;
-
- /* Load magic initialization constants */
- context->state[0] = 0x67452301;
- context->state[1] = 0xefcdab89;
- context->state[2] = 0x98badcfe;
- context->state[3] = 0x10325476;
-}
-
-/* MD5 block update operation. Continues an MD5 message-digest operation,
- * processing another message block, and updating the context.
- */
-void fz_md5_update(fz_md5 *context, const unsigned char *input, unsigned inlen)
-{
- unsigned i, index, partlen;
-
- /* Compute number of bytes mod 64 */
- index = (unsigned)((context->count[0] >> 3) & 0x3F);
-
- /* Update number of bits */
- context->count[0] += (unsigned int) inlen << 3;
- if (context->count[0] < (unsigned int) inlen << 3)
- context->count[1] ++;
- context->count[1] += (unsigned int) inlen >> 29;
-
- partlen = 64 - index;
-
- /* Transform as many times as possible. */
- if (inlen >= partlen)
- {
- memcpy(context->buffer + index, input, partlen);
- transform(context->state, context->buffer);
-
- for (i = partlen; i + 63 < inlen; i += 64)
- transform(context->state, input + i);
-
- index = 0;
- }
- else
- {
- i = 0;
- }
-
- /* Buffer remaining input */
- memcpy(context->buffer + index, input + i, inlen - i);
-}
-
-/* MD5 finalization. Ends an MD5 message-digest operation, writing the
- * the message digest and zeroizing the context.
- */
-void fz_md5_final(fz_md5 *context, unsigned char digest[16])
-{
- unsigned char bits[8];
- unsigned index, padlen;
-
- /* Save number of bits */
- encode(bits, context->count, 8);
-
- /* Pad out to 56 mod 64 */
- index = (unsigned)((context->count[0] >> 3) & 0x3f);
- padlen = index < 56 ? 56 - index : 120 - index;
- fz_md5_update(context, padding, padlen);
-
- /* Append length (before padding) */
- fz_md5_update(context, bits, 8);
-
- /* Store state in digest */
- encode(digest, context->state, 16);
-
- /* Zeroize sensitive information */
- memset(context, 0, sizeof(fz_md5));
-}
diff --git a/fitz/crypt_pkcs7.c b/fitz/crypt_pkcs7.c
deleted file mode 100644
index 1bc50b6e..00000000
--- a/fitz/crypt_pkcs7.c
+++ /dev/null
@@ -1,400 +0,0 @@
-#include "mupdf/pdf.h" // TODO: move this file to pdf module
-
-#ifdef HAVE_OPENSSL
-
-#include "openssl/err.h"
-#include "openssl/bio.h"
-#include "openssl/asn1.h"
-#include "openssl/x509.h"
-#include "openssl/err.h"
-#include "openssl/objects.h"
-#include "openssl/pem.h"
-#include "openssl/pkcs7.h"
-
-enum
-{
- SEG_START = 0,
- SEG_SIZE = 1
-};
-
-typedef struct bsegs_struct
-{
- int (*seg)[2];
- int nsegs;
- int current_seg;
- int seg_pos;
-} BIO_SEGS_CTX;
-
-static int bsegs_read(BIO *b, char *buf, int size)
-{
- BIO_SEGS_CTX *ctx = (BIO_SEGS_CTX *)b->ptr;
- int read = 0;
-
- while (size > 0 && ctx->current_seg < ctx->nsegs)
- {
- int nb = ctx->seg[ctx->current_seg][SEG_SIZE] - ctx->seg_pos;
-
- if (nb > size)
- nb = size;
-
- if (nb > 0)
- {
- if (ctx->seg_pos == 0)
- (void)BIO_seek(b->next_bio, ctx->seg[ctx->current_seg][SEG_START]);
-
- (void)BIO_read(b->next_bio, buf, nb);
- ctx->seg_pos += nb;
- read += nb;
- buf += nb;
- size -= nb;
- }
- else
- {
- ctx->current_seg++;
-
- if (ctx->current_seg < ctx->nsegs)
- ctx->seg_pos = 0;
- }
- }
-
- return read;
-}
-
-static long bsegs_ctrl(BIO *b, int cmd, long arg1, void *arg2)
-{
- return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
-}
-
-static int bsegs_new(BIO *b)
-{
- BIO_SEGS_CTX *ctx;
-
- ctx = (BIO_SEGS_CTX *)malloc(sizeof(BIO_SEGS_CTX));
- if (ctx == NULL)
- return 0;
-
- ctx->current_seg = 0;
- ctx->seg_pos = 0;
- ctx->seg = NULL;
- ctx->nsegs = 0;
-
- b->init = 1;
- b->ptr = (char *)ctx;
- b->flags = 0;
- b->num = 0;
-
- return 1;
-}
-
-static int bsegs_free(BIO *b)
-{
- if (b == NULL)
- return 0;
-
- free(b->ptr);
- b->ptr = NULL;
- b->init = 0;
- b->flags = 0;
-
- return 1;
-}
-
-static long bsegs_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
-{
- return BIO_callback_ctrl(b->next_bio, cmd, fp);
-}
-
-static BIO_METHOD methods_bsegs =
-{
- 0,"segment reader",
- NULL,
- bsegs_read,
- NULL,
- NULL,
- bsegs_ctrl,
- bsegs_new,
- bsegs_free,
- bsegs_callback_ctrl,
-};
-
-static BIO_METHOD *BIO_f_segments(void)
-{
- return &methods_bsegs;
-}
-
-static void BIO_set_segments(BIO *b, int (*seg)[2], int nsegs)
-{
- BIO_SEGS_CTX *ctx = (BIO_SEGS_CTX *)b->ptr;
-
- ctx->seg = seg;
- ctx->nsegs = nsegs;
-}
-
-typedef struct verify_context_s
-{
- X509_STORE_CTX x509_ctx;
- char certdesc[256];
- int err;
-} verify_context;
-
-static int verify_callback(int ok, X509_STORE_CTX *ctx)
-{
- verify_context *vctx;
- X509 *err_cert;
- int err, depth;
-
- vctx = (verify_context *)ctx;
-
- err_cert = X509_STORE_CTX_get_current_cert(ctx);
- err = X509_STORE_CTX_get_error(ctx);
- depth = X509_STORE_CTX_get_error_depth(ctx);
-
- X509_NAME_oneline(X509_get_subject_name(err_cert), vctx->certdesc, sizeof(vctx->certdesc));
-
- if (!ok && depth >= 6)
- {
- X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG);
- }
-
- switch (ctx->error)
- {
- case X509_V_ERR_INVALID_PURPOSE:
- case X509_V_ERR_CERT_HAS_EXPIRED:
- case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
- err = X509_V_OK;
- X509_STORE_CTX_set_error(ctx, X509_V_OK);
- ok = 1;
- break;
-
- case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
- /*
- In this case, don't reset err to X509_V_OK, so that it can be reported,
- although we do return 1, so that the digest will still be checked
- */
- ok = 1;
- break;
-
- default:
- break;
- }
-
- if (ok && vctx->err == X509_V_OK)
- vctx->err = err;
- return ok;
-}
-
-static int pk7_verify(X509_STORE *cert_store, PKCS7 *p7, BIO *detached, char *ebuf, int ebufsize)
-{
- PKCS7_SIGNER_INFO *si;
- verify_context vctx;
- BIO *p7bio=NULL;
- char readbuf[1024*4];
- int res = 1;
- int i;
- STACK_OF(PKCS7_SIGNER_INFO) *sk;
-
- vctx.err = X509_V_OK;
- ebuf[0] = 0;
-
- OpenSSL_add_all_algorithms();
-
- EVP_add_digest(EVP_md5());
- EVP_add_digest(EVP_sha1());
-
- ERR_load_crypto_strings();
-
- ERR_clear_error();
-
- X509_VERIFY_PARAM_set_flags(cert_store->param, X509_V_FLAG_CB_ISSUER_CHECK);
- X509_STORE_set_verify_cb_func(cert_store, verify_callback);
-
- p7bio = PKCS7_dataInit(p7, detached);
-
- /* We now have to 'read' from p7bio to calculate digests etc. */
- while (BIO_read(p7bio, readbuf, sizeof(readbuf)) > 0)
- ;
-
- /* We can now verify signatures */
- sk = PKCS7_get_signer_info(p7);
- if (sk == NULL)
- {
- /* there are no signatures on this data */
- res = 0;
- strncpy(ebuf, "No signatures", sizeof(ebuf));
- goto exit;
- }
-
- for (i=0; inext_bio = bdata;
- BIO_set_segments(bsegs, byte_range, byte_range_len);
-
- /* Find the certificates in the pk7 file */
- bcert = BIO_new_mem_buf(adobe_ca, sizeof(adobe_ca));
- pk7cert = d2i_PKCS7_bio(bcert, NULL);
- if (pk7cert == NULL)
- goto exit;
-
- t = OBJ_obj2nid(pk7cert->type);
- switch (t)
- {
- case NID_pkcs7_signed:
- certs = pk7cert->d.sign->cert;
- break;
-
- case NID_pkcs7_signedAndEnveloped:
- certs = pk7cert->d.sign->cert;
- break;
-
- default:
- break;
- }
-
- st = X509_STORE_new();
- if (st == NULL)
- goto exit;
-
- /* Add the certificates to the store */
- if (certs != NULL)
- {
- int i, n = sk_X509_num(certs);
-
- for (i = 0; i < n; i++)
- {
- X509 *c = sk_X509_value(certs, i);
- X509_STORE_add_cert(st, c);
- }
- }
-
- res = pk7_verify(st, pk7sig, bsegs, ebuf, ebufsize);
-
-exit:
- BIO_free(bsig);
- BIO_free(bdata);
- BIO_free(bsegs);
- BIO_free(bcert);
- PKCS7_free(pk7sig);
- PKCS7_free(pk7cert);
- X509_STORE_free(st);
-
- return res;
-}
-
-int pdf_check_signature(fz_context *ctx, pdf_document *doc, pdf_widget *widget, char *file, char *ebuf, int ebufsize)
-{
- int (*byte_range)[2] = NULL;
- int byte_range_len;
- char *contents = NULL;
- int contents_len;
- int res = 0;
-
- fz_var(byte_range);
- fz_var(res);
- fz_try(ctx);
- {
- byte_range_len = pdf_signature_widget_byte_range(doc, widget, NULL);
- if (byte_range_len)
- {
- byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
- pdf_signature_widget_byte_range(doc, widget, byte_range);
- }
-
- contents_len = pdf_signature_widget_contents(doc, widget, &contents);
- if (byte_range && contents)
- {
- res = verify_sig(contents, contents_len, file, byte_range, byte_range_len, ebuf, ebufsize);
- }
- else
- {
- res = 0;
- strncpy(ebuf, "Not signed", ebufsize);
- }
-
- }
- fz_always(ctx)
- {
- fz_free(ctx, byte_range);
- }
- fz_catch(ctx)
- {
- res = 0;
- strncpy(ebuf, fz_caught_message(ctx), ebufsize);
- }
-
- if (ebufsize > 0)
- ebuf[ebufsize-1] = 0;
-
- return res;
-}
-
-#else /* HAVE_OPENSSL */
-
-int pdf_check_signature(fz_context *ctx, pdf_document *doc, pdf_widget *widget, char *file, char *ebuf, int ebufsize)
-{
- strncpy(ebuf, "This version of MuPDF was built without signature support", ebufsize);
-
- return 0;
-}
-
-#endif /* HAVE_OPENSSL */
diff --git a/fitz/crypt_sha2.c b/fitz/crypt_sha2.c
deleted file mode 100644
index ffedfc95..00000000
--- a/fitz/crypt_sha2.c
+++ /dev/null
@@ -1,393 +0,0 @@
-/*
-This code is based on the code found from 7-Zip, which has a modified
-version of the SHA-256 found from Crypto++ .
-The code was modified a little to fit into liblzma and fitz.
-
-This file has been put into the public domain.
-You can do whatever you want with this file.
-
-SHA-384 and SHA-512 were also taken from Crypto++ and adapted for fitz.
-*/
-
-#include "mupdf/fitz.h"
-
-static inline int isbigendian(void)
-{
- static const int one = 1;
- return *(char*)&one == 0;
-}
-
-static inline unsigned int bswap32(unsigned int num)
-{
- if (!isbigendian())
- {
- return ( (((num) << 24))
- | (((num) << 8) & 0x00FF0000)
- | (((num) >> 8) & 0x0000FF00)
- | (((num) >> 24)) );
- }
- return num;
-}
-
-static inline uint64_t bswap64(uint64_t num)
-{
- if (!isbigendian())
- {
- return ( (((num) << 56))
- | (((num) << 40) & 0x00FF000000000000ULL)
- | (((num) << 24) & 0x0000FF0000000000ULL)
- | (((num) << 8) & 0x000000FF00000000ULL)
- | (((num) >> 8) & 0x00000000FF000000ULL)
- | (((num) >> 24) & 0x0000000000FF0000ULL)
- | (((num) >> 40) & 0x000000000000FF00ULL)
- | (((num) >> 56)) );
- }
- return num;
-}
-
-/* At least on x86, GCC is able to optimize this to a rotate instruction. */
-#define rotr(num, amount) ((num) >> (amount) | (num) << (8 * sizeof(num) - (amount)))
-
-#define blk0(i) (W[i] = data[i])
-#define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
- + s0(W[(i - 15) & 15]))
-
-#define Ch(x, y, z) (z ^ (x & (y ^ z)))
-#define Maj(x, y, z) ((x & y) | (z & (x | y)))
-
-#define a(i) T[(0 - i) & 7]
-#define b(i) T[(1 - i) & 7]
-#define c(i) T[(2 - i) & 7]
-#define d(i) T[(3 - i) & 7]
-#define e(i) T[(4 - i) & 7]
-#define f(i) T[(5 - i) & 7]
-#define g(i) T[(6 - i) & 7]
-#define h(i) T[(7 - i) & 7]
-
-#define R(i) \
- h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + K[i + j] \
- + (j ? blk2(i) : blk0(i)); \
- d(i) += h(i); \
- h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
-
-/* For SHA256 */
-
-#define S0(x) (rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22))
-#define S1(x) (rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25))
-#define s0(x) (rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3))
-#define s1(x) (rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10))
-
-static const unsigned int SHA256_K[64] = {
- 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
- 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
- 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
- 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
- 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
- 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
- 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
- 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
- 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
- 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
- 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
- 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
- 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
- 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
- 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
- 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
-};
-
-static void
-transform256(unsigned int state[8], const unsigned int data_xe[16])
-{
- const unsigned int *K = SHA256_K;
- unsigned int data[16];
- unsigned int W[16];
- unsigned int T[8];
- unsigned int j;
-
- /* ensure big-endian integers */
- for (j = 0; j < 16; j++)
- data[j] = bswap32(data_xe[j]);
-
- /* Copy state[] to working vars. */
- memcpy(T, state, sizeof(T));
-
- /* 64 operations, partially loop unrolled */
- for (j = 0; j < 64; j += 16) {
- R( 0); R( 1); R( 2); R( 3);
- R( 4); R( 5); R( 6); R( 7);
- R( 8); R( 9); R(10); R(11);
- R(12); R(13); R(14); R(15);
- }
-
- /* Add the working vars back into state[]. */
- state[0] += a(0);
- state[1] += b(0);
- state[2] += c(0);
- state[3] += d(0);
- state[4] += e(0);
- state[5] += f(0);
- state[6] += g(0);
- state[7] += h(0);
-}
-
-#undef S0
-#undef S1
-#undef s0
-#undef s1
-
-void fz_sha256_init(fz_sha256 *context)
-{
- context->count[0] = context->count[1] = 0;
-
- context->state[0] = 0x6A09E667;
- context->state[1] = 0xBB67AE85;
- context->state[2] = 0x3C6EF372;
- context->state[3] = 0xA54FF53A;
- context->state[4] = 0x510E527F;
- context->state[5] = 0x9B05688C;
- context->state[6] = 0x1F83D9AB;
- context->state[7] = 0x5BE0CD19;
-}
-
-void fz_sha256_update(fz_sha256 *context, const unsigned char *input, unsigned int inlen)
-{
- /* Copy the input data into a properly aligned temporary buffer.
- * This way we can be called with arbitrarily sized buffers
- * (no need to be multiple of 64 bytes), and the code works also
- * on architectures that don't allow unaligned memory access. */
- while (inlen > 0)
- {
- const unsigned int copy_start = context->count[0] & 0x3F;
- unsigned int copy_size = 64 - copy_start;
- if (copy_size > inlen)
- copy_size = inlen;
-
- memcpy(context->buffer.u8 + copy_start, input, copy_size);
-
- input += copy_size;
- inlen -= copy_size;
- context->count[0] += copy_size;
- /* carry overflow from low to high */
- if (context->count[0] < copy_size)
- context->count[1]++;
-
- if ((context->count[0] & 0x3F) == 0)
- transform256(context->state, context->buffer.u32);
- }
-}
-
-void fz_sha256_final(fz_sha256 *context, unsigned char digest[32])
-{
- /* Add padding as described in RFC 3174 (it describes SHA-1 but
- * the same padding style is used for SHA-256 too). */
- unsigned int j = context->count[0] & 0x3F;
- context->buffer.u8[j++] = 0x80;
-
- while (j != 56)
- {
- if (j == 64)
- {
- transform256(context->state, context->buffer.u32);
- j = 0;
- }
- context->buffer.u8[j++] = 0x00;
- }
-
- /* Convert the message size from bytes to bits. */
- context->count[1] = (context->count[1] << 3) + (context->count[0] >> 29);
- context->count[0] = context->count[0] << 3;
-
- context->buffer.u32[14] = bswap32(context->count[1]);
- context->buffer.u32[15] = bswap32(context->count[0]);
- transform256(context->state, context->buffer.u32);
-
- for (j = 0; j < 8; j++)
- ((unsigned int *)digest)[j] = bswap32(context->state[j]);
- memset(context, 0, sizeof(fz_sha256));
-}
-
-/* For SHA512 */
-
-#define S0(x) (rotr(x, 28) ^ rotr(x, 34) ^ rotr(x, 39))
-#define S1(x) (rotr(x, 14) ^ rotr(x, 18) ^ rotr(x, 41))
-#define s0(x) (rotr(x, 1) ^ rotr(x, 8) ^ (x >> 7))
-#define s1(x) (rotr(x, 19) ^ rotr(x, 61) ^ (x >> 6))
-
-static const uint64_t SHA512_K[80] = {
- 0x428A2F98D728AE22ULL, 0x7137449123EF65CDULL,
- 0xB5C0FBCFEC4D3B2FULL, 0xE9B5DBA58189DBBCULL,
- 0x3956C25BF348B538ULL, 0x59F111F1B605D019ULL,
- 0x923F82A4AF194F9BULL, 0xAB1C5ED5DA6D8118ULL,
- 0xD807AA98A3030242ULL, 0x12835B0145706FBEULL,
- 0x243185BE4EE4B28CULL, 0x550C7DC3D5FFB4E2ULL,
- 0x72BE5D74F27B896FULL, 0x80DEB1FE3B1696B1ULL,
- 0x9BDC06A725C71235ULL, 0xC19BF174CF692694ULL,
- 0xE49B69C19EF14AD2ULL, 0xEFBE4786384F25E3ULL,
- 0x0FC19DC68B8CD5B5ULL, 0x240CA1CC77AC9C65ULL,
- 0x2DE92C6F592B0275ULL, 0x4A7484AA6EA6E483ULL,
- 0x5CB0A9DCBD41FBD4ULL, 0x76F988DA831153B5ULL,
- 0x983E5152EE66DFABULL, 0xA831C66D2DB43210ULL,
- 0xB00327C898FB213FULL, 0xBF597FC7BEEF0EE4ULL,
- 0xC6E00BF33DA88FC2ULL, 0xD5A79147930AA725ULL,
- 0x06CA6351E003826FULL, 0x142929670A0E6E70ULL,
- 0x27B70A8546D22FFCULL, 0x2E1B21385C26C926ULL,
- 0x4D2C6DFC5AC42AEDULL, 0x53380D139D95B3DFULL,
- 0x650A73548BAF63DEULL, 0x766A0ABB3C77B2A8ULL,
- 0x81C2C92E47EDAEE6ULL, 0x92722C851482353BULL,
- 0xA2BFE8A14CF10364ULL, 0xA81A664BBC423001ULL,
- 0xC24B8B70D0F89791ULL, 0xC76C51A30654BE30ULL,
- 0xD192E819D6EF5218ULL, 0xD69906245565A910ULL,
- 0xF40E35855771202AULL, 0x106AA07032BBD1B8ULL,
- 0x19A4C116B8D2D0C8ULL, 0x1E376C085141AB53ULL,
- 0x2748774CDF8EEB99ULL, 0x34B0BCB5E19B48A8ULL,
- 0x391C0CB3C5C95A63ULL, 0x4ED8AA4AE3418ACBULL,
- 0x5B9CCA4F7763E373ULL, 0x682E6FF3D6B2B8A3ULL,
- 0x748F82EE5DEFB2FCULL, 0x78A5636F43172F60ULL,
- 0x84C87814A1F0AB72ULL, 0x8CC702081A6439ECULL,
- 0x90BEFFFA23631E28ULL, 0xA4506CEBDE82BDE9ULL,
- 0xBEF9A3F7B2C67915ULL, 0xC67178F2E372532BULL,
- 0xCA273ECEEA26619CULL, 0xD186B8C721C0C207ULL,
- 0xEADA7DD6CDE0EB1EULL, 0xF57D4F7FEE6ED178ULL,
- 0x06F067AA72176FBAULL, 0x0A637DC5A2C898A6ULL,
- 0x113F9804BEF90DAEULL, 0x1B710B35131C471BULL,
- 0x28DB77F523047D84ULL, 0x32CAAB7B40C72493ULL,
- 0x3C9EBE0A15C9BEBCULL, 0x431D67C49C100D4CULL,
- 0x4CC5D4BECB3E42B6ULL, 0x597F299CFC657E2AULL,
- 0x5FCB6FAB3AD6FAECULL, 0x6C44198C4A475817ULL,
-};
-
-static void
-transform512(uint64_t state[8], const uint64_t data_xe[16])
-{
- const uint64_t *K = SHA512_K;
- uint64_t data[16];
- uint64_t W[16];
- uint64_t T[8];
- unsigned int j;
-
- /* ensure big-endian integers */
- for (j = 0; j < 16; j++)
- data[j] = bswap64(data_xe[j]);
-
- /* Copy state[] to working vars. */
- memcpy(T, state, sizeof(T));
-
- /* 80 operations, partially loop unrolled */
- for (j = 0; j < 80; j+= 16) {
- R( 0); R( 1); R( 2); R( 3);
- R( 4); R( 5); R( 6); R( 7);
- R( 8); R( 9); R(10); R(11);
- R(12); R(13); R(14); R(15);
- }
-
- /* Add the working vars back into state[]. */
- state[0] += a(0);
- state[1] += b(0);
- state[2] += c(0);
- state[3] += d(0);
- state[4] += e(0);
- state[5] += f(0);
- state[6] += g(0);
- state[7] += h(0);
-}
-
-#undef S0
-#undef S1
-#undef s0
-#undef s1
-
-void fz_sha512_init(fz_sha512 *context)
-{
- context->count[0] = context->count[1] = 0;
-
- context->state[0] = 0x6A09E667F3BCC908ull;
- context->state[1] = 0xBB67AE8584CAA73Bull;
- context->state[2] = 0x3C6EF372FE94F82Bull;
- context->state[3] = 0xA54FF53A5F1D36F1ull;
- context->state[4] = 0x510E527FADE682D1ull;
- context->state[5] = 0x9B05688C2B3E6C1Full;
- context->state[6] = 0x1F83D9ABFB41BD6Bull;
- context->state[7] = 0x5BE0CD19137E2179ull;
-}
-
-void fz_sha512_update(fz_sha512 *context, const unsigned char *input, unsigned int inlen)
-{
- /* Copy the input data into a properly aligned temporary buffer.
- * This way we can be called with arbitrarily sized buffers
- * (no need to be multiple of 128 bytes), and the code works also
- * on architectures that don't allow unaligned memory access. */
- while (inlen > 0)
- {
- const unsigned int copy_start = context->count[0] & 0x7F;
- unsigned int copy_size = 128 - copy_start;
- if (copy_size > inlen)
- copy_size = inlen;
-
- memcpy(context->buffer.u8 + copy_start, input, copy_size);
-
- input += copy_size;
- inlen -= copy_size;
- context->count[0] += copy_size;
- /* carry overflow from low to high */
- if (context->count[0] < copy_size)
- context->count[1]++;
-
- if ((context->count[0] & 0x7F) == 0)
- transform512(context->state, context->buffer.u64);
- }
-}
-
-void fz_sha512_final(fz_sha512 *context, unsigned char digest[64])
-{
- /* Add padding as described in RFC 3174 (it describes SHA-1 but
- * the same padding style is used for SHA-512 too). */
- unsigned int j = context->count[0] & 0x7F;
- context->buffer.u8[j++] = 0x80;
-
- while (j != 112)
- {
- if (j == 128)
- {
- transform512(context->state, context->buffer.u64);
- j = 0;
- }
- context->buffer.u8[j++] = 0x00;
- }
-
- /* Convert the message size from bytes to bits. */
- context->count[1] = (context->count[1] << 3) + (context->count[0] >> 29);
- context->count[0] = context->count[0] << 3;
-
- context->buffer.u64[14] = bswap64(context->count[1]);
- context->buffer.u64[15] = bswap64(context->count[0]);
- transform512(context->state, context->buffer.u64);
-
- for (j = 0; j < 8; j++)
- ((uint64_t *)digest)[j] = bswap64(context->state[j]);
- memset(context, 0, sizeof(fz_sha512));
-}
-
-void fz_sha384_init(fz_sha384 *context)
-{
- context->count[0] = context->count[1] = 0;
-
- context->state[0] = 0xCBBB9D5DC1059ED8ull;
- context->state[1] = 0x629A292A367CD507ull;
- context->state[2] = 0x9159015A3070DD17ull;
- context->state[3] = 0x152FECD8F70E5939ull;
- context->state[4] = 0x67332667FFC00B31ull;
- context->state[5] = 0x8EB44A8768581511ull;
- context->state[6] = 0xDB0C2E0D64F98FA7ull;
- context->state[7] = 0x47B5481DBEFA4FA4ull;
-}
-
-void fz_sha384_update(fz_sha384 *context, const unsigned char *input, unsigned int inlen)
-{
- fz_sha512_update(context, input, inlen);
-}
-
-void fz_sha384_final(fz_sha384 *context, unsigned char digest[64])
-{
- fz_sha512_final(context, digest);
-}
diff --git a/fitz/dev_bbox.c b/fitz/dev_bbox.c
deleted file mode 100644
index 9cb2a27e..00000000
--- a/fitz/dev_bbox.c
+++ /dev/null
@@ -1,231 +0,0 @@
-#include "mupdf/fitz.h"
-
-#define STACK_SIZE 96
-
-typedef struct fz_bbox_data_s
-{
- fz_rect *result;
- int top;
- fz_rect stack[STACK_SIZE];
- /* mask content and tiles are ignored */
- int ignore;
-} fz_bbox_data;
-
-static void
-fz_bbox_add_rect(fz_device *dev, const fz_rect *rect, int clip)
-{
- fz_bbox_data *data = dev->user;
- fz_rect r = *rect;
-
- if (0 < data->top && data->top <= STACK_SIZE)
- {
- fz_intersect_rect(&r, &data->stack[data->top-1]);
- }
- if (!clip && data->top <= STACK_SIZE && !data->ignore)
- {
- fz_union_rect(data->result, &r);
- }
- if (clip && ++data->top <= STACK_SIZE)
- {
- data->stack[data->top-1] = r;
- }
-}
-
-static void
-fz_bbox_fill_path(fz_device *dev, fz_path *path, int even_odd, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_path(dev->ctx, path, NULL, ctm, &r), 0);
-}
-
-static void
-fz_bbox_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke,
- const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_path(dev->ctx, path, stroke, ctm, &r), 0);
-}
-
-static void
-fz_bbox_fill_text(fz_device *dev, fz_text *text, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_text(dev->ctx, text, NULL, ctm, &r), 0);
-}
-
-static void
-fz_bbox_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke,
- const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_text(dev->ctx, text, stroke, ctm, &r), 0);
-}
-
-static void
-fz_bbox_fill_shade(fz_device *dev, fz_shade *shade, const fz_matrix *ctm, float alpha)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_shade(dev->ctx, shade, ctm, &r), 0);
-}
-
-static void
-fz_bbox_fill_image(fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha)
-{
- fz_rect r = fz_unit_rect;
- fz_bbox_add_rect(dev, fz_transform_rect(&r, ctm), 0);
-}
-
-static void
-fz_bbox_fill_image_mask(fz_device *dev, fz_image *image, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_rect r = fz_unit_rect;
- fz_bbox_add_rect(dev, fz_transform_rect(&r, ctm), 0);
-}
-
-static void
-fz_bbox_clip_path(fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_path(dev->ctx, path, NULL, ctm, &r), 1);
-}
-
-static void
-fz_bbox_clip_stroke_path(fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_path(dev->ctx, path, stroke, ctm, &r), 1);
-}
-
-static void
-fz_bbox_clip_text(fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate)
-{
- fz_rect r = fz_infinite_rect;
- if (accumulate)
- fz_bbox_add_rect(dev, &r, accumulate != 2);
- else
- fz_bbox_add_rect(dev, fz_bound_text(dev->ctx, text, NULL, ctm, &r), 1);
-}
-
-static void
-fz_bbox_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- fz_rect r;
- fz_bbox_add_rect(dev, fz_bound_text(dev->ctx, text, stroke, ctm, &r), 1);
-}
-
-static void
-fz_bbox_clip_image_mask(fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm)
-{
- fz_rect r = *rect;
- fz_bbox_add_rect(dev, fz_transform_rect(&r, ctm), 1);
-}
-
-static void
-fz_bbox_pop_clip(fz_device *dev)
-{
- fz_bbox_data *data = dev->user;
- if (data->top > 0)
- data->top--;
- else
- fz_warn(dev->ctx, "unexpected pop clip");
-}
-
-static void
-fz_bbox_begin_mask(fz_device *dev, const fz_rect *rect, int luminosity, fz_colorspace *colorspace, float *color)
-{
- fz_bbox_data *data = dev->user;
- fz_bbox_add_rect(dev, rect, 1);
- data->ignore++;
-}
-
-static void
-fz_bbox_end_mask(fz_device *dev)
-{
- fz_bbox_data *data = dev->user;
- assert(data->ignore > 0);
- data->ignore--;
-}
-
-static void
-fz_bbox_begin_group(fz_device *dev, const fz_rect *rect, int isolated, int knockout, int blendmode, float alpha)
-{
- fz_bbox_add_rect(dev, rect, 1);
-}
-
-static void
-fz_bbox_end_group(fz_device *dev)
-{
- fz_bbox_pop_clip(dev);
-}
-
-static int
-fz_bbox_begin_tile(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id)
-{
- fz_bbox_data *data = dev->user;
- fz_rect r = *area;
- fz_bbox_add_rect(dev, fz_transform_rect(&r, ctm), 0);
- data->ignore++;
- return 0;
-}
-
-static void
-fz_bbox_end_tile(fz_device *dev)
-{
- fz_bbox_data *data = dev->user;
- assert(data->ignore > 0);
- data->ignore--;
-}
-
-static void
-fz_bbox_free_user(fz_device *dev)
-{
- fz_bbox_data *data = dev->user;
- if (data->top > 0)
- fz_warn(dev->ctx, "items left on stack in bbox device: %d", data->top);
- fz_free(dev->ctx, dev->user);
-}
-
-fz_device *
-fz_new_bbox_device(fz_context *ctx, fz_rect *result)
-{
- fz_device *dev;
-
- fz_bbox_data *user = fz_malloc_struct(ctx, fz_bbox_data);
- user->result = result;
- user->top = 0;
- user->ignore = 0;
- dev = fz_new_device(ctx, user);
- dev->free_user = fz_bbox_free_user;
-
- dev->fill_path = fz_bbox_fill_path;
- dev->stroke_path = fz_bbox_stroke_path;
- dev->clip_path = fz_bbox_clip_path;
- dev->clip_stroke_path = fz_bbox_clip_stroke_path;
-
- dev->fill_text = fz_bbox_fill_text;
- dev->stroke_text = fz_bbox_stroke_text;
- dev->clip_text = fz_bbox_clip_text;
- dev->clip_stroke_text = fz_bbox_clip_stroke_text;
-
- dev->fill_shade = fz_bbox_fill_shade;
- dev->fill_image = fz_bbox_fill_image;
- dev->fill_image_mask = fz_bbox_fill_image_mask;
- dev->clip_image_mask = fz_bbox_clip_image_mask;
-
- dev->pop_clip = fz_bbox_pop_clip;
-
- dev->begin_mask = fz_bbox_begin_mask;
- dev->end_mask = fz_bbox_end_mask;
- dev->begin_group = fz_bbox_begin_group;
- dev->end_group = fz_bbox_end_group;
-
- dev->begin_tile = fz_bbox_begin_tile;
- dev->end_tile = fz_bbox_end_tile;
-
- *result = fz_empty_rect;
-
- return dev;
-}
diff --git a/fitz/dev_list.c b/fitz/dev_list.c
deleted file mode 100644
index 6ffe165f..00000000
--- a/fitz/dev_list.c
+++ /dev/null
@@ -1,851 +0,0 @@
-#include "mupdf/fitz.h"
-
-typedef struct fz_display_node_s fz_display_node;
-
-#define STACK_SIZE 96
-
-typedef enum fz_display_command_e
-{
- FZ_CMD_BEGIN_PAGE,
- FZ_CMD_END_PAGE,
- FZ_CMD_FILL_PATH,
- FZ_CMD_STROKE_PATH,
- FZ_CMD_CLIP_PATH,
- FZ_CMD_CLIP_STROKE_PATH,
- FZ_CMD_FILL_TEXT,
- FZ_CMD_STROKE_TEXT,
- FZ_CMD_CLIP_TEXT,
- FZ_CMD_CLIP_STROKE_TEXT,
- FZ_CMD_IGNORE_TEXT,
- FZ_CMD_FILL_SHADE,
- FZ_CMD_FILL_IMAGE,
- FZ_CMD_FILL_IMAGE_MASK,
- FZ_CMD_CLIP_IMAGE_MASK,
- FZ_CMD_POP_CLIP,
- FZ_CMD_BEGIN_MASK,
- FZ_CMD_END_MASK,
- FZ_CMD_BEGIN_GROUP,
- FZ_CMD_END_GROUP,
- FZ_CMD_BEGIN_TILE,
- FZ_CMD_END_TILE
-} fz_display_command;
-
-struct fz_display_node_s
-{
- fz_display_command cmd;
- fz_display_node *next;
- fz_rect rect;
- union {
- fz_path *path;
- fz_text *text;
- fz_shade *shade;
- fz_image *image;
- int blendmode;
- } item;
- fz_stroke_state *stroke;
- int flag; /* even_odd, accumulate, isolated/knockout... */
- fz_matrix ctm;
- fz_colorspace *colorspace;
- float alpha;
- float color[FZ_MAX_COLORS];
-};
-
-struct fz_display_list_s
-{
- fz_storable storable;
- fz_display_node *first;
- fz_display_node *last;
- int len;
-
- int top;
- struct {
- fz_rect *update;
- fz_rect rect;
- } stack[STACK_SIZE];
- int tiled;
-};
-
-enum { ISOLATED = 1, KNOCKOUT = 2 };
-
-static fz_display_node *
-fz_new_display_node(fz_context *ctx, fz_display_command cmd, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_display_node *node;
- int i;
-
- node = fz_malloc_struct(ctx, fz_display_node);
- node->cmd = cmd;
- node->next = NULL;
- node->rect = fz_empty_rect;
- node->item.path = NULL;
- node->stroke = NULL;
- node->flag = (cmd == FZ_CMD_BEGIN_TILE ? fz_gen_id(ctx) : 0);
- node->ctm = *ctm;
- if (colorspace)
- {
- node->colorspace = fz_keep_colorspace(ctx, colorspace);
- if (color)
- {
- for (i = 0; i < node->colorspace->n; i++)
- node->color[i] = color[i];
- }
- }
- else
- {
- node->colorspace = NULL;
- }
- node->alpha = alpha;
-
- return node;
-}
-
-static void
-fz_append_display_node(fz_display_list *list, fz_display_node *node)
-{
- switch (node->cmd)
- {
- case FZ_CMD_CLIP_PATH:
- case FZ_CMD_CLIP_STROKE_PATH:
- case FZ_CMD_CLIP_IMAGE_MASK:
- if (list->top < STACK_SIZE)
- {
- list->stack[list->top].update = &node->rect;
- list->stack[list->top].rect = fz_empty_rect;
- }
- list->top++;
- break;
- case FZ_CMD_END_MASK:
- case FZ_CMD_CLIP_TEXT:
- case FZ_CMD_CLIP_STROKE_TEXT:
- if (list->top < STACK_SIZE)
- {
- list->stack[list->top].update = NULL;
- list->stack[list->top].rect = fz_empty_rect;
- }
- list->top++;
- break;
- case FZ_CMD_BEGIN_TILE:
- list->tiled++;
- if (list->top > 0 && list->top <= STACK_SIZE)
- {
- list->stack[list->top-1].rect = fz_infinite_rect;
- }
- break;
- case FZ_CMD_END_TILE:
- list->tiled--;
- break;
- case FZ_CMD_END_GROUP:
- break;
- case FZ_CMD_POP_CLIP:
- if (list->top > STACK_SIZE)
- {
- list->top--;
- node->rect = fz_infinite_rect;
- }
- else if (list->top > 0)
- {
- fz_rect *update;
- list->top--;
- update = list->stack[list->top].update;
- if (list->tiled == 0)
- {
- if (update)
- {
- fz_intersect_rect(update, &list->stack[list->top].rect);
- node->rect = *update;
- }
- else
- node->rect = list->stack[list->top].rect;
- }
- else
- node->rect = fz_infinite_rect;
- }
- /* fallthrough */
- default:
- if (list->top > 0 && list->tiled == 0 && list->top <= STACK_SIZE)
- fz_union_rect(&list->stack[list->top-1].rect, &node->rect);
- break;
- }
- if (!list->first)
- {
- list->first = node;
- list->last = node;
- }
- else
- {
- list->last->next = node;
- list->last = node;
- }
- list->len++;
-}
-
-static void
-fz_free_display_node(fz_context *ctx, fz_display_node *node)
-{
- switch (node->cmd)
- {
- case FZ_CMD_FILL_PATH:
- case FZ_CMD_STROKE_PATH:
- case FZ_CMD_CLIP_PATH:
- case FZ_CMD_CLIP_STROKE_PATH:
- fz_free_path(ctx, node->item.path);
- break;
- case FZ_CMD_FILL_TEXT:
- case FZ_CMD_STROKE_TEXT:
- case FZ_CMD_CLIP_TEXT:
- case FZ_CMD_CLIP_STROKE_TEXT:
- case FZ_CMD_IGNORE_TEXT:
- fz_free_text(ctx, node->item.text);
- break;
- case FZ_CMD_FILL_SHADE:
- fz_drop_shade(ctx, node->item.shade);
- break;
- case FZ_CMD_FILL_IMAGE:
- case FZ_CMD_FILL_IMAGE_MASK:
- case FZ_CMD_CLIP_IMAGE_MASK:
- fz_drop_image(ctx, node->item.image);
- break;
- case FZ_CMD_POP_CLIP:
- case FZ_CMD_BEGIN_MASK:
- case FZ_CMD_END_MASK:
- case FZ_CMD_BEGIN_GROUP:
- case FZ_CMD_END_GROUP:
- case FZ_CMD_BEGIN_TILE:
- case FZ_CMD_END_TILE:
- case FZ_CMD_BEGIN_PAGE:
- case FZ_CMD_END_PAGE:
- break;
- }
- if (node->stroke)
- fz_drop_stroke_state(ctx, node->stroke);
- if (node->colorspace)
- fz_drop_colorspace(ctx, node->colorspace);
- fz_free(ctx, node);
-}
-
-static void
-fz_list_begin_page(fz_device *dev, const fz_rect *mediabox, const fz_matrix *ctm)
-{
- fz_context *ctx = dev->ctx;
- fz_display_node *node = fz_new_display_node(ctx, FZ_CMD_BEGIN_PAGE, ctm, NULL, NULL, 0);
- node->rect = *mediabox;
- fz_transform_rect(&node->rect, ctm);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_end_page(fz_device *dev)
-{
- fz_context *ctx = dev->ctx;
- fz_display_node *node = fz_new_display_node(ctx, FZ_CMD_END_PAGE, &fz_identity, NULL, NULL, 0);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_fill_path(fz_device *dev, fz_path *path, int even_odd, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_FILL_PATH, ctm, colorspace, color, alpha);
- fz_try(ctx)
- {
- fz_bound_path(dev->ctx, path, NULL, ctm, &node->rect);
- node->item.path = fz_clone_path(dev->ctx, path);
- node->flag = even_odd;
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke,
- const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_STROKE_PATH, ctm, colorspace, color, alpha);
- fz_try(ctx)
- {
- fz_bound_path(dev->ctx, path, stroke, ctm, &node->rect);
- node->item.path = fz_clone_path(dev->ctx, path);
- node->stroke = fz_keep_stroke_state(dev->ctx, stroke);
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_clip_path(fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_CLIP_PATH, ctm, NULL, NULL, 0);
- fz_try(ctx)
- {
- fz_bound_path(dev->ctx, path, NULL, ctm, &node->rect);
- if (rect)
- fz_intersect_rect(&node->rect, rect);
- node->item.path = fz_clone_path(dev->ctx, path);
- node->flag = even_odd;
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_clip_stroke_path(fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_CLIP_STROKE_PATH, ctm, NULL, NULL, 0);
- fz_try(ctx)
- {
- fz_bound_path(dev->ctx, path, stroke, ctm, &node->rect);
- if (rect)
- fz_intersect_rect(&node->rect, rect);
- node->item.path = fz_clone_path(dev->ctx, path);
- node->stroke = fz_keep_stroke_state(dev->ctx, stroke);
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_fill_text(fz_device *dev, fz_text *text, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_FILL_TEXT, ctm, colorspace, color, alpha);
- fz_try(ctx)
- {
- fz_bound_text(dev->ctx, text, NULL, ctm, &node->rect);
- node->item.text = fz_clone_text(dev->ctx, text);
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_STROKE_TEXT, ctm, colorspace, color, alpha);
- node->item.text = NULL;
- fz_try(ctx)
- {
- fz_bound_text(dev->ctx, text, stroke, ctm, &node->rect);
- node->item.text = fz_clone_text(dev->ctx, text);
- node->stroke = fz_keep_stroke_state(dev->ctx, stroke);
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_clip_text(fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_CLIP_TEXT, ctm, NULL, NULL, 0);
- fz_try(ctx)
- {
- fz_bound_text(dev->ctx, text, NULL, ctm, &node->rect);
- node->item.text = fz_clone_text(dev->ctx, text);
- node->flag = accumulate;
- /* when accumulating, be conservative about culling */
- if (accumulate)
- node->rect = fz_infinite_rect;
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_CLIP_STROKE_TEXT, ctm, NULL, NULL, 0);
- fz_try(ctx)
- {
- fz_bound_text(dev->ctx, text, stroke, ctm, &node->rect);
- node->item.text = fz_clone_text(dev->ctx, text);
- node->stroke = fz_keep_stroke_state(dev->ctx, stroke);
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_ignore_text(fz_device *dev, fz_text *text, const fz_matrix *ctm)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_IGNORE_TEXT, ctm, NULL, NULL, 0);
- fz_try(ctx)
- {
- fz_bound_text(dev->ctx, text, NULL, ctm, &node->rect);
- node->item.text = fz_clone_text(dev->ctx, text);
- }
- fz_catch(ctx)
- {
- fz_free_display_node(ctx, node);
- fz_rethrow(ctx);
- }
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_pop_clip(fz_device *dev)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_POP_CLIP, &fz_identity, NULL, NULL, 0);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_fill_shade(fz_device *dev, fz_shade *shade, const fz_matrix *ctm, float alpha)
-{
- fz_display_node *node;
- fz_context *ctx = dev->ctx;
- node = fz_new_display_node(ctx, FZ_CMD_FILL_SHADE, ctm, NULL, NULL, alpha);
- fz_bound_shade(ctx, shade, ctm, &node->rect);
- node->item.shade = fz_keep_shade(ctx, shade);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_fill_image(fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_IMAGE, ctm, NULL, NULL, alpha);
- node->rect = fz_unit_rect;
- fz_transform_rect(&node->rect, ctm);
- node->item.image = fz_keep_image(dev->ctx, image);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_fill_image_mask(fz_device *dev, fz_image *image, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_IMAGE_MASK, ctm, colorspace, color, alpha);
- node->rect = fz_unit_rect;
- fz_transform_rect(&node->rect, ctm);
- node->item.image = fz_keep_image(dev->ctx, image);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_clip_image_mask(fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_IMAGE_MASK, ctm, NULL, NULL, 0);
- node->rect = fz_unit_rect;
- fz_transform_rect(&node->rect, ctm);
- if (rect)
- fz_intersect_rect(&node->rect, rect);
- node->item.image = fz_keep_image(dev->ctx, image);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_begin_mask(fz_device *dev, const fz_rect *rect, int luminosity, fz_colorspace *colorspace, float *color)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_MASK, &fz_identity, colorspace, color, 0);
- node->rect = *rect;
- node->flag = luminosity;
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_end_mask(fz_device *dev)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_END_MASK, &fz_identity, NULL, NULL, 0);
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_begin_group(fz_device *dev, const fz_rect *rect, int isolated, int knockout, int blendmode, float alpha)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_GROUP, &fz_identity, NULL, NULL, alpha);
- node->rect = *rect;
- node->item.blendmode = blendmode;
- node->flag |= isolated ? ISOLATED : 0;
- node->flag |= knockout ? KNOCKOUT : 0;
- fz_append_display_node(dev->user, node);
-}
-
-static void
-fz_list_end_group(fz_device *dev)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_END_GROUP, &fz_identity, NULL, NULL, 0);
- fz_append_display_node(dev->user, node);
-}
-
-static int
-fz_list_begin_tile(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id)
-{
- /* We ignore id here, as we will pass on our own id */
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_TILE, ctm, NULL, NULL, 0);
- node->rect = *area;
- node->color[0] = xstep;
- node->color[1] = ystep;
- node->color[2] = view->x0;
- node->color[3] = view->y0;
- node->color[4] = view->x1;
- node->color[5] = view->y1;
- fz_append_display_node(dev->user, node);
- return 0;
-}
-
-static void
-fz_list_end_tile(fz_device *dev)
-{
- fz_display_node *node;
- node = fz_new_display_node(dev->ctx, FZ_CMD_END_TILE, &fz_identity, NULL, NULL, 0);
- fz_append_display_node(dev->user, node);
-}
-
-fz_device *
-fz_new_list_device(fz_context *ctx, fz_display_list *list)
-{
- fz_device *dev = fz_new_device(ctx, list);
-
- dev->begin_page = fz_list_begin_page;
- dev->end_page = fz_list_end_page;
-
- dev->fill_path = fz_list_fill_path;
- dev->stroke_path = fz_list_stroke_path;
- dev->clip_path = fz_list_clip_path;
- dev->clip_stroke_path = fz_list_clip_stroke_path;
-
- dev->fill_text = fz_list_fill_text;
- dev->stroke_text = fz_list_stroke_text;
- dev->clip_text = fz_list_clip_text;
- dev->clip_stroke_text = fz_list_clip_stroke_text;
- dev->ignore_text = fz_list_ignore_text;
-
- dev->fill_shade = fz_list_fill_shade;
- dev->fill_image = fz_list_fill_image;
- dev->fill_image_mask = fz_list_fill_image_mask;
- dev->clip_image_mask = fz_list_clip_image_mask;
-
- dev->pop_clip = fz_list_pop_clip;
-
- dev->begin_mask = fz_list_begin_mask;
- dev->end_mask = fz_list_end_mask;
- dev->begin_group = fz_list_begin_group;
- dev->end_group = fz_list_end_group;
-
- dev->begin_tile = fz_list_begin_tile;
- dev->end_tile = fz_list_end_tile;
-
- return dev;
-}
-
-static void
-fz_free_display_list(fz_context *ctx, fz_storable *list_)
-{
- fz_display_list *list = (fz_display_list *)list_;
- fz_display_node *node;
-
- if (list == NULL)
- return;
- node = list->first;
- while (node)
- {
- fz_display_node *next = node->next;
- fz_free_display_node(ctx, node);
- node = next;
- }
- fz_free(ctx, list);
-}
-
-fz_display_list *
-fz_new_display_list(fz_context *ctx)
-{
- fz_display_list *list = fz_malloc_struct(ctx, fz_display_list);
- FZ_INIT_STORABLE(list, 1, fz_free_display_list);
- list->first = NULL;
- list->last = NULL;
- list->len = 0;
- list->top = 0;
- list->tiled = 0;
- return list;
-}
-
-fz_display_list *
-fz_keep_display_list(fz_context *ctx, fz_display_list *list)
-{
- return (fz_display_list *)fz_keep_storable(ctx, &list->storable);
-}
-
-void
-fz_drop_display_list(fz_context *ctx, fz_display_list *list)
-{
- fz_drop_storable(ctx, &list->storable);
-}
-
-static fz_display_node *
-skip_to_end_tile(fz_display_node *node, int *progress)
-{
- fz_display_node *next;
- int depth = 1;
-
- /* Skip through until we find the matching end_tile. Note that
- * (somewhat nastily) we return the PREVIOUS node to this to help
- * the calling routine. */
- do
- {
- next = node->next;
- if (next == NULL)
- break;
- if (next->cmd == FZ_CMD_BEGIN_TILE)
- depth++;
- else if (next->cmd == FZ_CMD_END_TILE)
- {
- depth--;
- if (depth == 0)
- return node;
- }
- (*progress)++;
- node = next;
- }
- while (1);
-
- return NULL;
-}
-
-void
-fz_run_display_list(fz_display_list *list, fz_device *dev, const fz_matrix *top_ctm, const fz_rect *scissor, fz_cookie *cookie)
-{
- fz_display_node *node;
- fz_matrix ctm;
- int clipped = 0;
- int tiled = 0;
- int progress = 0;
- fz_context *ctx = dev->ctx;
-
- if (!scissor)
- scissor = &fz_infinite_rect;
-
- if (cookie)
- {
- cookie->progress_max = list->len;
- cookie->progress = 0;
- }
-
- for (node = list->first; node; node = node->next)
- {
- int empty;
-
- fz_rect node_rect = node->rect;
- fz_transform_rect(&node_rect, top_ctm);
-
- /* Check the cookie for aborting */
- if (cookie)
- {
- if (cookie->abort)
- break;
- cookie->progress = progress++;
- }
-
- /* cull objects to draw using a quick visibility test */
-
- if (tiled ||
- node->cmd == FZ_CMD_BEGIN_TILE || node->cmd == FZ_CMD_END_TILE ||
- node->cmd == FZ_CMD_BEGIN_PAGE || node->cmd == FZ_CMD_END_PAGE)
- {
- empty = 0;
- }
- else
- {
- fz_rect rect = node_rect;
- fz_intersect_rect(&rect, scissor);
- empty = fz_is_empty_rect(&rect);
- }
-
- if (clipped || empty)
- {
- switch (node->cmd)
- {
- case FZ_CMD_CLIP_PATH:
- case FZ_CMD_CLIP_STROKE_PATH:
- case FZ_CMD_CLIP_STROKE_TEXT:
- case FZ_CMD_CLIP_IMAGE_MASK:
- case FZ_CMD_BEGIN_MASK:
- case FZ_CMD_BEGIN_GROUP:
- clipped++;
- continue;
- case FZ_CMD_CLIP_TEXT:
- /* Accumulated text has no extra pops */
- if (node->flag != 2)
- clipped++;
- continue;
- case FZ_CMD_POP_CLIP:
- case FZ_CMD_END_GROUP:
- if (!clipped)
- goto visible;
- clipped--;
- continue;
- case FZ_CMD_END_MASK:
- if (!clipped)
- goto visible;
- continue;
- default:
- continue;
- }
- }
-
-visible:
- fz_concat(&ctm, &node->ctm, top_ctm);
-
-
- fz_try(ctx)
- {
- switch (node->cmd)
- {
- case FZ_CMD_BEGIN_PAGE:
- fz_begin_page(dev, &node_rect, &ctm);
- break;
- case FZ_CMD_END_PAGE:
- fz_end_page(dev);
- break;
- case FZ_CMD_FILL_PATH:
- fz_fill_path(dev, node->item.path, node->flag, &ctm,
- node->colorspace, node->color, node->alpha);
- break;
- case FZ_CMD_STROKE_PATH:
- fz_stroke_path(dev, node->item.path, node->stroke, &ctm,
- node->colorspace, node->color, node->alpha);
- break;
- case FZ_CMD_CLIP_PATH:
- fz_clip_path(dev, node->item.path, &node_rect, node->flag, &ctm);
- break;
- case FZ_CMD_CLIP_STROKE_PATH:
- fz_clip_stroke_path(dev, node->item.path, &node_rect, node->stroke, &ctm);
- break;
- case FZ_CMD_FILL_TEXT:
- fz_fill_text(dev, node->item.text, &ctm,
- node->colorspace, node->color, node->alpha);
- break;
- case FZ_CMD_STROKE_TEXT:
- fz_stroke_text(dev, node->item.text, node->stroke, &ctm,
- node->colorspace, node->color, node->alpha);
- break;
- case FZ_CMD_CLIP_TEXT:
- fz_clip_text(dev, node->item.text, &ctm, node->flag);
- break;
- case FZ_CMD_CLIP_STROKE_TEXT:
- fz_clip_stroke_text(dev, node->item.text, node->stroke, &ctm);
- break;
- case FZ_CMD_IGNORE_TEXT:
- fz_ignore_text(dev, node->item.text, &ctm);
- break;
- case FZ_CMD_FILL_SHADE:
- if ((dev->hints & FZ_IGNORE_SHADE) == 0)
- fz_fill_shade(dev, node->item.shade, &ctm, node->alpha);
- break;
- case FZ_CMD_FILL_IMAGE:
- if ((dev->hints & FZ_IGNORE_IMAGE) == 0)
- fz_fill_image(dev, node->item.image, &ctm, node->alpha);
- break;
- case FZ_CMD_FILL_IMAGE_MASK:
- if ((dev->hints & FZ_IGNORE_IMAGE) == 0)
- fz_fill_image_mask(dev, node->item.image, &ctm,
- node->colorspace, node->color, node->alpha);
- break;
- case FZ_CMD_CLIP_IMAGE_MASK:
- if ((dev->hints & FZ_IGNORE_IMAGE) == 0)
- fz_clip_image_mask(dev, node->item.image, &node_rect, &ctm);
- break;
- case FZ_CMD_POP_CLIP:
- fz_pop_clip(dev);
- break;
- case FZ_CMD_BEGIN_MASK:
- fz_begin_mask(dev, &node_rect, node->flag, node->colorspace, node->color);
- break;
- case FZ_CMD_END_MASK:
- fz_end_mask(dev);
- break;
- case FZ_CMD_BEGIN_GROUP:
- fz_begin_group(dev, &node_rect,
- (node->flag & ISOLATED) != 0, (node->flag & KNOCKOUT) != 0,
- node->item.blendmode, node->alpha);
- break;
- case FZ_CMD_END_GROUP:
- fz_end_group(dev);
- break;
- case FZ_CMD_BEGIN_TILE:
- {
- int cached;
- fz_rect tile_rect;
- tiled++;
- tile_rect.x0 = node->color[2];
- tile_rect.y0 = node->color[3];
- tile_rect.x1 = node->color[4];
- tile_rect.y1 = node->color[5];
- cached = fz_begin_tile_id(dev, &node->rect, &tile_rect, node->color[0], node->color[1], &ctm, node->flag);
- if (cached)
- node = skip_to_end_tile(node, &progress);
- break;
- }
- case FZ_CMD_END_TILE:
- tiled--;
- fz_end_tile(dev);
- break;
- }
- }
- fz_catch(ctx)
- {
- /* Swallow the error */
- if (cookie)
- cookie->errors++;
- fz_warn(ctx, "Ignoring error during interpretation");
- }
- }
-}
diff --git a/fitz/dev_null.c b/fitz/dev_null.c
deleted file mode 100644
index 175b00db..00000000
--- a/fitz/dev_null.c
+++ /dev/null
@@ -1,388 +0,0 @@
-#include "mupdf/fitz.h"
-
-fz_device *
-fz_new_device(fz_context *ctx, void *user)
-{
- fz_device *dev = fz_malloc_struct(ctx, fz_device);
- dev->hints = 0;
- dev->flags = 0;
- dev->user = user;
- dev->ctx = ctx;
- dev->error_depth = 0;
- return dev;
-}
-
-void
-fz_free_device(fz_device *dev)
-{
- if (dev == NULL)
- return;
- if (dev->free_user)
- dev->free_user(dev);
- fz_free(dev->ctx, dev);
-}
-
-void
-fz_enable_device_hints(fz_device *dev, int hints)
-{
- dev->hints |= hints;
-}
-
-void
-fz_disable_device_hints(fz_device *dev, int hints)
-{
- dev->hints &= ~hints;
-}
-
-void
-fz_begin_page(fz_device *dev, const fz_rect *rect, const fz_matrix *ctm)
-{
- if (dev->begin_page)
- dev->begin_page(dev, rect, ctm);
-}
-
-void
-fz_end_page(fz_device *dev)
-{
- if (dev->end_page)
- dev->end_page(dev);
-}
-
-void
-fz_fill_path(fz_device *dev, fz_path *path, int even_odd, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->fill_path)
- dev->fill_path(dev, path, even_odd, ctm, colorspace, color, alpha);
-}
-
-void
-fz_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->stroke_path)
- dev->stroke_path(dev, path, stroke, ctm, colorspace, color, alpha);
-}
-
-void
-fz_clip_path(fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->clip_path)
- dev->clip_path(dev, path, rect, even_odd, ctm);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_clip_stroke_path(fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->clip_stroke_path)
- dev->clip_stroke_path(dev, path, rect, stroke, ctm);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_fill_text(fz_device *dev, fz_text *text, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->fill_text)
- dev->fill_text(dev, text, ctm, colorspace, color, alpha);
-}
-
-void
-fz_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->stroke_text)
- dev->stroke_text(dev, text, stroke, ctm, colorspace, color, alpha);
-}
-
-void
-fz_clip_text(fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- if (accumulate == 0 || accumulate == 1)
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->clip_text)
- dev->clip_text(dev, text, ctm, accumulate);
- }
- fz_catch(ctx)
- {
- if (accumulate == 2)
- fz_rethrow(ctx);
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->clip_stroke_text)
- dev->clip_stroke_text(dev, text, stroke, ctm);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_ignore_text(fz_device *dev, fz_text *text, const fz_matrix *ctm)
-{
- if (dev->error_depth)
- return;
- if (dev->ignore_text)
- dev->ignore_text(dev, text, ctm);
-}
-
-void
-fz_pop_clip(fz_device *dev)
-{
- if (dev->error_depth)
- {
- dev->error_depth--;
- if (dev->error_depth == 0)
- fz_throw(dev->ctx, FZ_ERROR_GENERIC, "%s", dev->errmess);
- return;
- }
- if (dev->pop_clip)
- dev->pop_clip(dev);
-}
-
-void
-fz_fill_shade(fz_device *dev, fz_shade *shade, const fz_matrix *ctm, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->fill_shade)
- dev->fill_shade(dev, shade, ctm, alpha);
-}
-
-void
-fz_fill_image(fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->fill_image)
- dev->fill_image(dev, image, ctm, alpha);
-}
-
-void
-fz_fill_image_mask(fz_device *dev, fz_image *image, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- if (dev->error_depth)
- return;
- if (dev->fill_image_mask)
- dev->fill_image_mask(dev, image, ctm, colorspace, color, alpha);
-}
-
-void
-fz_clip_image_mask(fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->clip_image_mask)
- dev->clip_image_mask(dev, image, rect, ctm);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_begin_mask(fz_device *dev, const fz_rect *area, int luminosity, fz_colorspace *colorspace, float *bc)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->begin_mask)
- dev->begin_mask(dev, area, luminosity, colorspace, bc);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_end_mask(fz_device *dev)
-{
- if (dev->error_depth)
- {
- /* Converts from mask to clip, so no change in stack depth */
- return;
- }
- if (dev->end_mask)
- dev->end_mask(dev);
-}
-
-void
-fz_begin_group(fz_device *dev, const fz_rect *area, int isolated, int knockout, int blendmode, float alpha)
-{
- fz_context *ctx = dev->ctx;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return;
- }
-
- fz_try(ctx)
- {
- if (dev->begin_group)
- dev->begin_group(dev, area, isolated, knockout, blendmode, alpha);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
-}
-
-void
-fz_end_group(fz_device *dev)
-{
- if (dev->error_depth)
- {
- dev->error_depth--;
- if (dev->error_depth == 0)
- fz_throw(dev->ctx, FZ_ERROR_GENERIC, "%s", dev->errmess);
- return;
- }
- if (dev->end_group)
- dev->end_group(dev);
-}
-
-void
-fz_begin_tile(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm)
-{
- (void)fz_begin_tile_id(dev, area, view, xstep, ystep, ctm, 0);
-}
-
-int
-fz_begin_tile_id(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id)
-{
- fz_context *ctx = dev->ctx;
- int ret = 0;
-
- if (dev->error_depth)
- {
- dev->error_depth++;
- return 0;
- }
-
- if (xstep < 0)
- xstep = -xstep;
- if (ystep < 0)
- ystep = -ystep;
-
- fz_try(ctx)
- {
- if (dev->begin_tile)
- ret = dev->begin_tile(dev, area, view, xstep, ystep, ctm, id);
- }
- fz_catch(ctx)
- {
- dev->error_depth = 1;
- strcpy(dev->errmess, fz_caught_message(ctx));
- /* Error swallowed */
- }
- return ret;
-}
-
-void
-fz_end_tile(fz_device *dev)
-{
- if (dev->error_depth)
- {
- dev->error_depth--;
- if (dev->error_depth == 0)
- fz_throw(dev->ctx, FZ_ERROR_GENERIC, "%s", dev->errmess);
- return;
- }
- if (dev->end_tile)
- dev->end_tile(dev);
-}
diff --git a/fitz/dev_svg.c b/fitz/dev_svg.c
deleted file mode 100644
index 4dd82120..00000000
--- a/fitz/dev_svg.c
+++ /dev/null
@@ -1,619 +0,0 @@
-#include "mupdf/fitz.h"
-
-typedef struct svg_device_s svg_device;
-
-typedef struct tile_s tile;
-
-struct tile_s
-{
- int pattern;
- fz_matrix ctm;
- fz_rect view;
- fz_rect area;
- fz_point step;
-};
-
-struct svg_device_s
-{
- fz_context *ctx;
- fz_output *out;
-
- int id;
-
- int num_tiles;
- int max_tiles;
- tile *tiles;
-};
-
-/* Helper functions */
-
-static void
-svg_dev_path(svg_device *sdev, fz_path *path)
-{
- fz_output *out = sdev->out;
- float x, y;
- int i = 0;
- fz_printf(out, " d=\"");
- while (i < path->len)
- {
- switch (path->items[i++].k)
- {
- case FZ_MOVETO:
- x = path->items[i++].v;
- y = path->items[i++].v;
- fz_printf(out, "M %g %g ", x, y);
- break;
- case FZ_LINETO:
- x = path->items[i++].v;
- y = path->items[i++].v;
- fz_printf(out, "L %g %g ", x, y);
- break;
- case FZ_CURVETO:
- x = path->items[i++].v;
- y = path->items[i++].v;
- fz_printf(out, "C %g %g ", x, y);
- x = path->items[i++].v;
- y = path->items[i++].v;
- fz_printf(out, "%g %g ", x, y);
- x = path->items[i++].v;
- y = path->items[i++].v;
- fz_printf(out, "%g %g ", x, y);
- break;
- case FZ_CLOSE_PATH:
- fz_printf(out, "Z ");
- break;
- }
- }
- fz_printf(out, "\"");
-}
-
-static void
-svg_dev_ctm(svg_device *sdev, const fz_matrix *ctm)
-{
- fz_output *out = sdev->out;
-
- if (ctm->a != 1.0 || ctm->b != 0 || ctm->c != 0 || ctm->d != 1.0 || ctm->e != 0 || ctm->f != 0)
- {
- fz_printf(out, " transform=\"matrix(%g,%g,%g,%g,%g,%g)\"",
- ctm->a, ctm->b, ctm->c, ctm->d, ctm->e, ctm->f);
- }
-}
-
-static void
-svg_dev_stroke_state(svg_device *sdev, fz_stroke_state *stroke_state)
-{
- fz_output *out = sdev->out;
-
- fz_printf(out, " stroke-width=\"%g\"", stroke_state->linewidth);
- fz_printf(out, " stroke-linecap=\"%s\"",
- (stroke_state->start_cap == FZ_LINECAP_SQUARE ? "square" :
- (stroke_state->start_cap == FZ_LINECAP_ROUND ? "round" : "butt")));
- if (stroke_state->dash_len != 0)
- {
- int i;
- fz_printf(out, " stroke-dasharray=");
- for (i = 0; i < stroke_state->dash_len; i++)
- fz_printf(out, "%c%g", (i == 0 ? '\"' : ','), stroke_state->dash_list[i]);
- fz_printf(out, "\"");
- if (stroke_state->dash_phase != 0)
- fz_printf(out, " stroke-dashoffset=\"%g\"", stroke_state->dash_phase);
- }
- if (stroke_state->linejoin == FZ_LINEJOIN_MITER || stroke_state->linejoin == FZ_LINEJOIN_MITER_XPS)
- fz_printf(out, " stroke-miterlimit=\"%g\"", stroke_state->miterlimit);
- fz_printf(out, " stroke-linejoin=\"%s\"",
- (stroke_state->linejoin == FZ_LINEJOIN_BEVEL ? "bevel" :
- (stroke_state->linejoin == FZ_LINEJOIN_ROUND ? "round" : "miter")));
-}
-
-static void
-svg_dev_fill_color(svg_device *sdev, fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_context *ctx = sdev->ctx;
- fz_output *out = sdev->out;
- float rgb[FZ_MAX_COLORS];
-
- if (colorspace != fz_device_rgb(ctx))
- {
- /* If it's not rgb, make it rgb */
- colorspace->to_rgb(ctx, colorspace, color, rgb);
- color = rgb;
- }
-
- if (color[0] == 0 && color[1] == 0 && color[2] == 0)
- {
- /* don't send a fill, as it will be assumed to be black */
- }
- else
- fz_printf(out, " fill=\"rgb(%d,%d,%d)\"", (int)(255*color[0] + 0.5), (int)(255*color[1] + 0.5), (int)(255*color[2]+0.5));
- if (alpha != 1)
- fz_printf(out, " fill-opacity=\"%g\"", alpha);
-}
-
-static void
-svg_dev_stroke_color(svg_device *sdev, fz_colorspace *colorspace, float *color, float alpha)
-{
- fz_context *ctx = sdev->ctx;
- fz_output *out = sdev->out;
- float rgb[FZ_MAX_COLORS];
-
- if (colorspace != fz_device_rgb(ctx))
- {
- /* If it's not rgb, make it rgb */
- colorspace->to_rgb(ctx, colorspace, color, rgb);
- color = rgb;
- }
-
- fz_printf(out, " fill=\"none\" stroke=\"rgb(%d,%d,%d)\"", (int)(255*color[0] + 0.5), (int)(255*color[1] + 0.5), (int)(255*color[2]+0.5));
- if (alpha != 1)
- fz_printf(out, " stroke-opacity=\"%g\"", alpha);
-}
-
-static void
-svg_dev_text(svg_device *sdev, const fz_matrix *ctm, fz_text *text)
-{
- fz_output *out = sdev->out;
- int i;
- fz_matrix inverse;
- fz_matrix local_trm;
- float size;
-
- /* Rely on the fact that trm.{e,f} == 0 */
- size = fz_matrix_expansion(&text->trm);
- local_trm.a = text->trm.a / size;
- local_trm.b = text->trm.b / size;
- local_trm.c = -text->trm.c / size;
- local_trm.d = -text->trm.d / size;
- local_trm.e = 0;
- local_trm.f = 0;
- fz_invert_matrix(&inverse, &local_trm);
- fz_concat(&local_trm, &local_trm, ctm);
-
- fz_printf(out, " transform=\"matrix(%g,%g,%g,%g,%g,%g)\"",
- local_trm.a, local_trm.b, local_trm.c, local_trm.d, local_trm.e, local_trm.f);
- fz_printf(out, " font-size=\"%g\"", size);
- fz_printf(out, " font-family=\"%s\"", text->font->name);
-
- fz_printf(out, " x=");
- for (i=0; i < text->len; i++)
- {
- fz_text_item *it = &text->items[i];
- fz_point p;
- p.x = it->x;
- p.y = it->y;
- fz_transform_point(&p, &inverse);
- fz_printf(out, "%c%g", i == 0 ? '\"' : ' ', p.x);
- }
- fz_printf(out, "\" y=");
- for (i=0; i < text->len; i++)
- {
- fz_text_item *it = &text->items[i];
- fz_point p;
- p.x = it->x;
- p.y = it->y;
- fz_transform_point(&p, &inverse);
- fz_printf(out, "%c%g", i == 0 ? '\"' : ' ', p.y);
- }
- fz_printf(out, "\">\n");
- for (i=0; i < text->len; i++)
- {
- fz_text_item *it = &text->items[i];
- int c = it->ucs;
- if (c >= 32 && c <= 127 && c != '<' && c != '&')
- fz_printf(out, "%c", c);
- else
- fz_printf(out, "%04x;", c);
- }
- fz_printf(out, "\n\n");
-}
-
-/* Entry points */
-
-static void
-svg_dev_fill_path(fz_device *dev, fz_path *path, int even_odd, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
-
- fz_printf(out, "\n");
-}
-
-static void
-svg_dev_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
-
- fz_printf(out, "\n");
-}
-
-static void
-svg_dev_clip_path(fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
- int num = sdev->id++;
-
- fz_printf(out, "\n", num);
- fz_printf(out, "\n\n\n", num);
-}
-
-static void
-svg_dev_clip_stroke_path(fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
- fz_context *ctx = dev->ctx;
- fz_rect bounds;
- int num = sdev->id++;
- float white[3] = { 255, 255, 255 };
-
- fz_bound_path(ctx, path, stroke, ctm, &bounds);
-
- fz_printf(out, "\n",
- num, bounds.x0, bounds.y0, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
- fz_printf(out, "\n\n\n", num);
-}
-
-static void
-svg_dev_fill_text(fz_device *dev, fz_text *text, const fz_matrix *ctm,
- fz_colorspace *colorspace, float *color, float alpha)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
-
- fz_printf(out, "user;
- fz_output *out = sdev->out;
-
- fz_printf(out, "user;
- fz_output *out = sdev->out;
- fz_context *ctx = dev->ctx;
- fz_rect bounds;
- int num = sdev->id++;
- float white[3] = { 255, 255, 255 };
-
- fz_bound_text(ctx, text, NULL, ctm, &bounds);
-
- fz_printf(out, "\n",
- num, bounds.x0, bounds.y0, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
- fz_printf(out, "\n\n", num);
-}
-
-static void
-svg_dev_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
- fz_context *ctx = dev->ctx;
- fz_rect bounds;
- int num = sdev->id++;
- float white[3] = { 255, 255, 255 };
-
- fz_bound_text(ctx, text, NULL, ctm, &bounds);
-
- fz_printf(out, "\n",
- num, bounds.x0, bounds.y0, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
- fz_printf(out, "\n\n", num);
-}
-
-static void
-svg_dev_ignore_text(fz_device *dev, fz_text *text, const fz_matrix *ctm)
-{
-}
-
-static void
-send_data_base64(fz_output *out, fz_buffer *buffer)
-{
- int i, len;
- static const char set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- len = buffer->len/3;
- for (i = 0; i < len; i++)
- {
- int c = buffer->data[3*i];
- int d = buffer->data[3*i+1];
- int e = buffer->data[3*i+2];
- if ((i & 15) == 0)
- fz_printf(out, "\n");
- fz_printf(out, "%c%c%c%c", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)|(e>>6)], set[e & 63]);
- }
- i *= 3;
- switch (buffer->len-i)
- {
- case 2:
- {
- int c = buffer->data[i];
- int d = buffer->data[i+1];
- fz_printf(out, "%c%c%c=", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)]);
- break;
- }
- case 1:
- {
- int c = buffer->data[i];
- fz_printf(out, "%c%c==", set[c>>2], set[(c&3)<<4]);
- break;
- }
- default:
- case 0:
- break;
- }
-}
-
-static void
-svg_dev_fill_image(fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha)
-{
- svg_device *sdev = (svg_device *)dev->user;
- fz_context *ctx = dev->ctx;
- fz_output *out = sdev->out;
- fz_matrix local_ctm = *ctm;
- fz_matrix scale = { 1.0f/image->w, 0, 0, 1.0f/image->h, 0, 0};
-
- fz_concat(&local_ctm, &scale, ctm);
- fz_printf(out, "w, image->h);
- switch (image->buffer == NULL ? FZ_IMAGE_JPX : image->buffer->params.type)
- {
- case FZ_IMAGE_JPEG:
- fz_printf(out, "image/jpeg;base64,");
- send_data_base64(out, image->buffer->buffer);
- break;
- case FZ_IMAGE_PNG:
- fz_printf(out, "image/png;base64,");
- send_data_base64(out, image->buffer->buffer);
- break;
- default:
- {
- fz_buffer *buf = fz_image_as_png(ctx, image, image->w, image->h);
- fz_printf(out, "image/png;base64,");
- send_data_base64(out, buf);
- fz_drop_buffer(ctx, buf);
- break;
- }
- }
- fz_printf(out, "\"/>\n");
-}
-
-static void
-svg_dev_fill_shade(fz_device *dev, fz_shade *shade, const fz_matrix *ctm, float alpha)
-{
-}
-
-static void
-svg_dev_fill_image_mask(fz_device *dev, fz_image *image, const fz_matrix *ctm,
-fz_colorspace *colorspace, float *color, float alpha)
-{
-}
-
-static void
-svg_dev_clip_image_mask(fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm)
-{
- svg_device *sdev = dev->user;
- fz_output *out = sdev->out;
-
- fz_printf(out, "\n");
-}
-
-static void
-svg_dev_pop_clip(fz_device *dev)
-{
- svg_device *sdev = (svg_device *)dev->user;
- fz_output *out = sdev->out;
-
- /* FIXME */
- fz_printf(out, "\n");
-}
-
-static void
-svg_dev_begin_mask(fz_device *dev, const fz_rect *bbox, int luminosity, fz_colorspace *colorspace, float *color)
-{
-}
-
-static void
-svg_dev_end_mask(fz_device *dev)
-{
-
-}
-
-static void
-svg_dev_begin_group(fz_device *dev, const fz_rect *bbox, int isolated, int knockout, int blendmode, float alpha)
-{
-
-}
-
-static void
-svg_dev_end_group(fz_device *dev)
-{
-
-}
-
-static int
-svg_dev_begin_tile(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id)
-{
- svg_device *sdev = (svg_device *)dev->user;
- fz_output *out = sdev->out;
- fz_context *ctx = dev->ctx;
- fz_matrix inverse;
- int num;
- tile *t;
-
- if (sdev->num_tiles == sdev->max_tiles)
- {
- int n = (sdev->num_tiles == 0 ? 4 : sdev->num_tiles * 2);
-
- sdev->tiles = fz_resize_array(ctx, sdev->tiles, n, sizeof(tile));
- sdev->max_tiles = n;
- }
- num = sdev->num_tiles++;
- t = &sdev->tiles[num];
- t->area = *area;
- t->view = *view;
- t->ctm = *ctm;
- t->pattern = sdev->id++;
- t->step.x = xstep;
- t->step.y = ystep;
-
- /* view = area of our reference tile in pattern space.
- * area = area to tile into in pattern space.
- * xstep/ystep = pattern repeat step in pattern space.
- * All of these need to be transformed by ctm to get to device space.
- * SVG only allows us to specify pattern tiles as axis aligned
- * rectangles, so we send these through as is, and ensure that the
- * correct matrix is used on the fill.
- */
-
- /* In svg, the reference tile is taken from (x,y) to (x+width,y+height)
- * and is repeated at (x+n*width,y+m*height) for all integer n and m.
- * This means that width and height correspond to xstep and ystep. */
- fz_printf(out, "pattern);
- fz_printf(out, " x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\">\n",
- view->x0, view->y0, xstep, ystep);
- /* All the pattern contents will have their own ctm applied. Let's
- * undo the current one to allow for this */
- fz_invert_matrix(&inverse, ctm);
- fz_printf(out, "\n");
-
- return 0;
-}
-
-static void
-svg_dev_end_tile(fz_device *dev)
-{
- svg_device *sdev = (svg_device *)dev->user;
- fz_output *out = sdev->out;
- int num;
- tile *t;
-
- if (sdev->num_tiles == 0)
- return;
- num = --sdev->num_tiles;
- t = &sdev->tiles[num];
-
- fz_printf(out, "\n\n");
- fz_printf(out, "ctm);
- fz_printf(out, " fill=\"url(#pa%d)\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"/>\n",
- t->pattern, t->area.x0, t->area.y0, t->area.x1 - t->area.x0, t->area.y1 - t->area.y0);
-}
-
-static void
-svg_dev_free_user(fz_device *dev)
-{
- svg_device *sdev = dev->user;
- fz_context *ctx = sdev->ctx;
- fz_output *out = sdev->out;
-
- fz_free(ctx, sdev->tiles);
-
- fz_printf(out, "\n");
-
- fz_free(ctx, sdev);
-}
-
-fz_device *fz_new_svg_device(fz_context *ctx, fz_output *out, float page_width, float page_height)
-{
- svg_device *sdev = fz_malloc_struct(ctx, svg_device);
- fz_device *dev;
-
- fz_try(ctx)
- {
- sdev->ctx = ctx;
- sdev->out = out;
- sdev->id = 0;
-
- dev = fz_new_device(ctx, sdev);
- }
- fz_catch(ctx)
- {
- fz_free(ctx, sdev);
- fz_rethrow(ctx);
- }
-
- dev->free_user = svg_dev_free_user;
-
- dev->fill_path = svg_dev_fill_path;
- dev->stroke_path = svg_dev_stroke_path;
- dev->clip_path = svg_dev_clip_path;
- dev->clip_stroke_path = svg_dev_clip_stroke_path;
-
- dev->fill_text = svg_dev_fill_text;
- dev->stroke_text = svg_dev_stroke_text;
- dev->clip_text = svg_dev_clip_text;
- dev->clip_stroke_text = svg_dev_clip_stroke_text;
- dev->ignore_text = svg_dev_ignore_text;
-
- dev->fill_shade = svg_dev_fill_shade;
- dev->fill_image = svg_dev_fill_image;
- dev->fill_image_mask = svg_dev_fill_image_mask;
- dev->clip_image_mask = svg_dev_clip_image_mask;
-
- dev->pop_clip = svg_dev_pop_clip;
-
- dev->begin_mask = svg_dev_begin_mask;
- dev->end_mask = svg_dev_end_mask;
- dev->begin_group = svg_dev_begin_group;
- dev->end_group = svg_dev_end_group;
-
- dev->begin_tile = svg_dev_begin_tile;
- dev->end_tile = svg_dev_end_tile;
-
- fz_printf(out, "\n");
- fz_printf(out, "\n");
- fz_printf(out, "
\n");
- break;
- }
- case FZ_PAGE_BLOCK_IMAGE:
- {
- fz_image_block *image = page->blocks[block_n].u.image;
- fz_printf(out, "image->w, image->image->h);
- switch (image->image->buffer == NULL ? FZ_IMAGE_JPX : image->image->buffer->params.type)
- {
- case FZ_IMAGE_JPEG:
- fz_printf(out, "image/jpeg;base64,");
- send_data_base64(out, image->image->buffer->buffer);
- break;
- case FZ_IMAGE_PNG:
- fz_printf(out, "image/png;base64,");
- send_data_base64(out, image->image->buffer->buffer);
- break;
- default:
- {
- fz_buffer *buf = fz_image_as_png(ctx, image->image, image->image->w, image->image->h);
- fz_printf(out, "image/png;base64,");
- send_data_base64(out, buf);
- fz_drop_buffer(ctx, buf);
- break;
- }
- }
- fz_printf(out, "\">\n");
- break;
- }
- }
- }
-
- fz_printf(out, "\n");
-}
-
-void
-fz_print_text_page_xml(fz_context *ctx, fz_output *out, fz_text_page *page)
-{
- int block_n;
-
- fz_printf(out, "\n",
- page->mediabox.x1 - page->mediabox.x0,
- page->mediabox.y1 - page->mediabox.y0);
-
- for (block_n = 0; block_n < page->len; block_n++)
- {
- switch (page->blocks[block_n].type)
- {
- case FZ_PAGE_BLOCK_TEXT:
- {
- fz_text_block *block = page->blocks[block_n].u.text;
- fz_text_line *line;
- char *s;
-
- fz_printf(out, "\n",
- block->bbox.x0, block->bbox.y0, block->bbox.x1, block->bbox.y1);
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- fz_text_span *span;
- fz_printf(out, "\n",
- line->bbox.x0, line->bbox.y0, line->bbox.x1, line->bbox.y1);
- for (span = line->first_span; span; span = span->next)
- {
- fz_text_style *style = NULL;
- int char_num;
- for (char_num = 0; char_num < span->len; char_num++)
- {
- fz_text_char *ch = &span->text[char_num];
- if (ch->style != style)
- {
- if (style)
- {
- fz_printf(out, "\n");
- }
- style = ch->style;
- s = strchr(style->font->name, '+');
- s = s ? s + 1 : style->font->name;
- fz_printf(out, "\n",
- span->bbox.x0, span->bbox.y0, span->bbox.x1, span->bbox.y1,
- s, style->size);
- }
- {
- fz_rect rect;
- fz_text_char_bbox(&rect, span, char_num);
- fz_printf(out, "p.x, ch->p.y);
- }
- switch (ch->c)
- {
- case '<': fz_printf(out, "<"); break;
- case '>': fz_printf(out, ">"); break;
- case '&': fz_printf(out, "&"); break;
- case '"': fz_printf(out, """); break;
- case '\'': fz_printf(out, "'"); break;
- default:
- if (ch->c >= 32 && ch->c <= 127)
- fz_printf(out, "%c", ch->c);
- else
- fz_printf(out, "%x;", ch->c);
- break;
- }
- fz_printf(out, "\"/>\n");
- }
- if (style)
- fz_printf(out, "\n");
- }
- fz_printf(out, "\n");
- }
- fz_printf(out, "\n");
- break;
- }
- case FZ_PAGE_BLOCK_IMAGE:
- {
- break;
- }
- }
- }
- fz_printf(out, "\n");
-}
-
-void
-fz_print_text_page(fz_context *ctx, fz_output *out, fz_text_page *page)
-{
- int block_n;
-
- for (block_n = 0; block_n < page->len; block_n++)
- {
- switch (page->blocks[block_n].type)
- {
- case FZ_PAGE_BLOCK_TEXT:
- {
- fz_text_block *block = page->blocks[block_n].u.text;
- fz_text_line *line;
- fz_text_char *ch;
- char utf[10];
- int i, n;
-
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- fz_text_span *span;
- for (span = line->first_span; span; span = span->next)
- {
- for (ch = span->text; ch < span->text + span->len; ch++)
- {
- n = fz_runetochar(utf, ch->c);
- for (i = 0; i < n; i++)
- fz_printf(out, "%c", utf[i]);
- }
- }
- fz_printf(out, "\n");
- }
- fz_printf(out, "\n");
- break;
- }
- case FZ_PAGE_BLOCK_IMAGE:
- break;
- }
- }
-}
diff --git a/fitz/text_paragraph.c b/fitz/text_paragraph.c
deleted file mode 100644
index 51062938..00000000
--- a/fitz/text_paragraph.c
+++ /dev/null
@@ -1,1500 +0,0 @@
-#include "mupdf/fitz.h"
-
-/* Assemble span soup into blocks and lines. */
-
-#define MY_EPSILON 0.001f
-
-#undef DEBUG_LINE_HEIGHTS
-#undef DEBUG_MASKS
-#undef DEBUG_ALIGN
-#undef DEBUG_INDENTS
-
-#undef SPOT_LINE_NUMBERS
-
-typedef struct line_height_s
-{
- float height;
- int count;
- fz_text_style *style;
-} line_height;
-
-typedef struct line_heights_s
-{
- fz_context *ctx;
- int cap;
- int len;
- line_height *lh;
-} line_heights;
-
-static line_heights *
-new_line_heights(fz_context *ctx)
-{
- line_heights *lh = fz_malloc_struct(ctx, line_heights);
- lh->ctx = ctx;
- return lh;
-}
-
-static void
-free_line_heights(line_heights *lh)
-{
- if (!lh)
- return;
- fz_free(lh->ctx, lh->lh);
- fz_free(lh->ctx, lh);
-}
-
-static void
-insert_line_height(line_heights *lh, fz_text_style *style, float height)
-{
- int i;
-
-#ifdef DEBUG_LINE_HEIGHTS
- printf("style=%x height=%g\n", style, height);
-#endif
-
- /* If we have one already, add it in */
- for (i=0; i < lh->len; i++)
- {
- /* Match if we are within 5% */
- if (lh->lh[i].style == style && lh->lh[i].height * 0.95 <= height && lh->lh[i].height * 1.05 >= height)
- {
- /* Ensure that the average height is correct */
- lh->lh[i].height = (lh->lh[i].height * lh->lh[i].count + height) / (lh->lh[i].count+1);
- lh->lh[i].count++;
- return;
- }
- }
-
- /* Otherwise extend (if required) and add it */
- if (lh->cap == lh->len)
- {
- int newcap = (lh->cap ? lh->cap * 2 : 4);
- lh->lh = fz_resize_array(lh->ctx, lh->lh, newcap, sizeof(line_height));
- lh->cap = newcap;
- }
-
- lh->lh[lh->len].count = 1;
- lh->lh[lh->len].height = height;
- lh->lh[lh->len].style = style;
- lh->len++;
-}
-
-static void
-cull_line_heights(line_heights *lh)
-{
- int i, j, k;
-
-#ifdef DEBUG_LINE_HEIGHTS
- printf("Before culling:\n");
- for (i = 0; i < lh->len; i++)
- {
- fz_text_style *style = lh->lh[i].style;
- printf("style=%x height=%g count=%d\n", style, lh->lh[i].height, lh->lh[i].count);
- }
-#endif
- for (i = 0; i < lh->len; i++)
- {
- fz_text_style *style = lh->lh[i].style;
- int count = lh->lh[i].count;
- int max = i;
-
- /* Find the max for this style */
- for (j = i+1; j < lh->len; j++)
- {
- if (lh->lh[j].style == style && lh->lh[j].count > count)
- {
- max = j;
- count = lh->lh[j].count;
- }
- }
-
- /* Destroy all the ones other than the max */
- if (max != i)
- {
- lh->lh[i].count = count;
- lh->lh[i].height = lh->lh[max].height;
- lh->lh[max].count = 0;
- }
- j = i+1;
- for (k = j; k < lh->len; k++)
- {
- if (lh->lh[k].style != style)
- lh->lh[j++] = lh->lh[k];
- }
- lh->len = j;
- }
-#ifdef DEBUG_LINE_HEIGHTS
- printf("After culling:\n");
- for (i = 0; i < lh->len; i++)
- {
- fz_text_style *style = lh->lh[i].style;
- printf("style=%x height=%g count=%d\n", style, lh->lh[i].height, lh->lh[i].count);
- }
-#endif
-}
-
-static float
-line_height_for_style(line_heights *lh, fz_text_style *style)
-{
- int i;
-
- for (i=0; i < lh->len; i++)
- {
- if (lh->lh[i].style == style)
- return lh->lh[i].height;
- }
- return 0.0; /* Never reached */
-}
-
-static void
-split_block(fz_context *ctx, fz_text_page *page, int block_num, int linenum)
-{
- int split_len;
- fz_text_block *block, *block2;
-
- if (page->len == page->cap)
- {
- int new_cap = fz_maxi(16, page->cap * 2);
- page->blocks = fz_resize_array(ctx, page->blocks, new_cap, sizeof(*page->blocks));
- page->cap = new_cap;
- }
-
- memmove(page->blocks+block_num+1, page->blocks+block_num, (page->len - block_num)*sizeof(*page->blocks));
- page->len++;
-
- block2 = fz_malloc_struct(ctx, fz_text_block);
- block = page->blocks[block_num].u.text;
-
- page->blocks[block_num+1].type = FZ_PAGE_BLOCK_TEXT;
- page->blocks[block_num+1].u.text = block2;
- split_len = block->len - linenum;
- block2->bbox = block->bbox; /* FIXME! */
- block2->cap = 0;
- block2->len = 0;
- block2->lines = NULL;
- block2->lines = fz_malloc_array(ctx, split_len, sizeof(fz_text_line));
- block2->cap = block2->len;
- block2->len = split_len;
- block->len = linenum;
- memcpy(block2->lines, block->lines + linenum, split_len * sizeof(fz_text_line));
- block2->lines[0].distance = 0;
-}
-
-static inline int
-is_unicode_wspace(int c)
-{
- return (c == 9 || /* TAB */
- c == 0x0a || /* HT */
- c == 0x0b || /* LF */
- c == 0x0c || /* VT */
- c == 0x0d || /* FF */
- c == 0x20 || /* CR */
- c == 0x85 || /* NEL */
- c == 0xA0 || /* No break space */
- c == 0x1680 || /* Ogham space mark */
- c == 0x180E || /* Mongolian Vowel Separator */
- c == 0x2000 || /* En quad */
- c == 0x2001 || /* Em quad */
- c == 0x2002 || /* En space */
- c == 0x2003 || /* Em space */
- c == 0x2004 || /* Three-per-Em space */
- c == 0x2005 || /* Four-per-Em space */
- c == 0x2006 || /* Five-per-Em space */
- c == 0x2007 || /* Figure space */
- c == 0x2008 || /* Punctuation space */
- c == 0x2009 || /* Thin space */
- c == 0x200A || /* Hair space */
- c == 0x2028 || /* Line separator */
- c == 0x2029 || /* Paragraph separator */
- c == 0x202F || /* Narrow no-break space */
- c == 0x205F || /* Medium mathematical space */
- c == 0x3000); /* Ideographic space */
-}
-
-static inline int
-is_unicode_bullet(int c)
-{
- /* The last 2 aren't strictly bullets, but will do for our usage here */
- return (c == 0x2022 || /* Bullet */
- c == 0x2023 || /* Triangular bullet */
- c == 0x25e6 || /* White bullet */
- c == 0x2043 || /* Hyphen bullet */
- c == 0x2219 || /* Bullet operator */
- c == 149 || /* Ascii bullet */
- c == '*');
-}
-
-static inline int
-is_number(int c)
-{
- return ((c >= '0' && c <= '9') ||
- (c == '.'));
-}
-
-static inline int
-is_latin_char(int c)
-{
- return ((c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z'));
-}
-
-static inline int
-is_roman(int c)
-{
- return (c == 'i' || c == 'I' ||
- c == 'v' || c == 'V' ||
- c == 'x' || c == 'X' ||
- c == 'l' || c == 'L' ||
- c == 'c' || c == 'C' ||
- c == 'm' || c == 'M');
-}
-
-static int
-is_list_entry(fz_text_line *line, fz_text_span *span, int *char_num_ptr)
-{
- int char_num;
- fz_text_char *chr;
-
- /* First, skip over any whitespace */
- for (char_num = 0; char_num < span->len; char_num++)
- {
- chr = &span->text[char_num];
- if (!is_unicode_wspace(chr->c))
- break;
- }
- *char_num_ptr = char_num;
-
- if (span != line->first_span || char_num >= span->len)
- return 0;
-
- /* Now we check for various special cases, which we consider to mean
- * that this is probably a list entry and therefore should always count
- * as a separate paragraph (and hence not be entered in the line height
- * table). */
- chr = &span->text[char_num];
-
- /* Is the first char on the line, a bullet point? */
- if (is_unicode_bullet(chr->c))
- return 1;
-
-#ifdef SPOT_LINE_NUMBERS
- /* Is the entire first span a number? Or does it start with a number
- * followed by ) or : ? Allow to involve single latin chars too. */
- if (is_number(chr->c) || is_latin_char(chr->c))
- {
- int cn = char_num;
- int met_char = is_latin_char(chr->c);
- for (cn = char_num+1; cn < span->len; cn++)
- {
- fz_text_char *chr2 = &span->text[cn];
-
- if (is_latin_char(chr2->c) && !met_char)
- {
- met_char = 1;
- continue;
- }
- met_char = 0;
- if (!is_number(chr2->c) && !is_unicode_wspace(chr2->c))
- break;
- else if (chr2->c == ')' || chr2->c == ':')
- {
- cn = span->len;
- break;
- }
- }
- if (cn == span->len)
- return 1;
- }
-
- /* Is the entire first span a roman numeral? Or does it start with
- * a roman numeral followed by ) or : ? */
- if (is_roman(chr->c))
- {
- int cn = char_num;
- for (cn = char_num+1; cn < span->len; cn++)
- {
- fz_text_char *chr2 = &span->text[cn];
-
- if (!is_roman(chr2->c) && !is_unicode_wspace(chr2->c))
- break;
- else if (chr2->c == ')' || chr2->c == ':')
- {
- cn = span->len;
- break;
- }
- }
- if (cn == span->len)
- return 1;
- }
-#endif
- return 0;
-}
-
-typedef struct region_masks_s region_masks;
-
-typedef struct region_mask_s region_mask;
-
-typedef struct region_s region;
-
-struct region_s
-{
- float start;
- float stop;
- float ave_start;
- float ave_stop;
- int align;
- float colw;
-};
-
-struct region_mask_s
-{
- fz_context *ctx;
- int freq;
- fz_point blv;
- int cap;
- int len;
- float size;
- region *mask;
-};
-
-struct region_masks_s
-{
- fz_context *ctx;
- int cap;
- int len;
- region_mask **mask;
-};
-
-static region_masks *
-new_region_masks(fz_context *ctx)
-{
- region_masks *rms = fz_malloc_struct(ctx, region_masks);
- rms->ctx = ctx;
- rms->cap = 0;
- rms->len = 0;
- rms->mask = NULL;
- return rms;
-}
-
-static void
-free_region_mask(region_mask *rm)
-{
- if (!rm)
- return;
- fz_free(rm->ctx, rm->mask);
- fz_free(rm->ctx, rm);
-}
-
-static void
-free_region_masks(region_masks *rms)
-{
- int i;
-
- if (!rms)
- return;
- for (i=0; i < rms->len; i++)
- {
- free_region_mask(rms->mask[i]);
- }
- fz_free(rms->ctx, rms->mask);
- fz_free(rms->ctx, rms);
-}
-
-static int region_masks_mergeable(const region_mask *rm1, const region_mask *rm2, float *score)
-{
- int i1, i2;
- int count = 0;
-
- *score = 0;
- if (fabsf(rm1->blv.x-rm2->blv.x) >= MY_EPSILON || fabsf(rm1->blv.y-rm2->blv.y) >= MY_EPSILON)
- return 0;
-
- for (i1 = 0, i2 = 0; i1 < rm1->len && i2 < rm2->len; )
- {
- if (rm1->mask[i1].stop < rm2->mask[i2].start)
- {
- /* rm1's region is entirely before rm2's */
- *score += rm1->mask[i1].stop - rm1->mask[i1].start;
- i1++;
- }
- else if (rm1->mask[i1].start > rm2->mask[i2].stop)
- {
- /* rm2's region is entirely before rm1's */
- *score += rm2->mask[i2].stop - rm2->mask[i2].start;
- i2++;
- }
- else
- {
- float lscore, rscore;
- if (rm1->mask[i1].start < rm2->mask[i2].start)
- {
- if (i2 > 0 && rm2->mask[i2-1].stop >= rm1->mask[i1].start)
- return 0; /* Not compatible */
- lscore = rm2->mask[i2].start - rm1->mask[i1].start;
- }
- else
- {
- if (i1 > 0 && rm1->mask[i1-1].stop >= rm2->mask[i2].start)
- return 0; /* Not compatible */
- lscore = rm1->mask[i1].start - rm2->mask[i2].start;
- }
- if (rm1->mask[i1].stop > rm2->mask[i2].stop)
- {
- if (i2+1 < rm2->len && rm2->mask[i2+1].start <= rm1->mask[i1].stop)
- return 0; /* Not compatible */
- rscore = rm1->mask[i1].stop - rm2->mask[i2].stop;
- }
- else
- {
- if (i1+1 < rm1->len && rm1->mask[i1+1].start <= rm2->mask[i2].stop)
- return 0; /* Not compatible */
- rscore = rm2->mask[i2].stop - rm1->mask[i1].stop;
- }
- /* In order to allow a region to merge, either the
- * left, the right, or the centre must agree */
- if (lscore < 1)
- {
- if (rscore < 1)
- {
- rscore = 0;
- }
- lscore = 0;
- }
- else if (rscore < 1)
- {
- rscore = 0;
- }
- else
- {
- /* Neither Left or right agree. Does the centre? */
- float ave1 = rm1->mask[i1].start + rm1->mask[i1].stop;
- float ave2 = rm2->mask[i2].start + rm2->mask[i2].stop;
- if (fabsf(ave1-ave2) > 1)
- {
- /* Nothing agrees, so don't merge */
- return 0;
- }
- lscore = 0;
- rscore = 0;
- }
- *score += lscore + rscore;
- /* These two regions could be merged */
- i1++;
- i2++;
- }
- count++;
- }
- count += rm1->len-i1 + rm2->len-i2;
- return count;
-}
-
-static int region_mask_matches(const region_mask *rm1, const region_mask *rm2, float *score)
-{
- int i1, i2;
- int close = 1;
-
- *score = 0;
- if (fabsf(rm1->blv.x-rm2->blv.x) >= MY_EPSILON || fabsf(rm1->blv.y-rm2->blv.y) >= MY_EPSILON)
- return 0;
-
- for (i1 = 0, i2 = 0; i1 < rm1->len && i2 < rm2->len; )
- {
- if (rm1->mask[i1].stop < rm2->mask[i2].start)
- {
- /* rm1's region is entirely before rm2's */
- *score += rm1->mask[i1].stop - rm1->mask[i1].start;
- i1++;
- }
- else if (rm1->mask[i1].start > rm2->mask[i2].stop)
- {
- /* Not compatible */
- return 0;
- }
- else
- {
- float lscore, rscore;
- if (rm1->mask[i1].start > rm2->mask[i2].start)
- {
- /* Not compatible */
- return 0;
- }
- if (rm1->mask[i1].stop < rm2->mask[i2].stop)
- {
- /* Not compatible */
- return 0;
- }
- lscore = rm2->mask[i2].start - rm1->mask[i1].start;
- rscore = rm1->mask[i1].stop - rm2->mask[i2].stop;
- if (lscore < 1)
- {
- if (rscore < 1)
- close++;
- close++;
- }
- else if (rscore < 1)
- close++;
- else if (fabsf(lscore - rscore) < 1)
- {
- lscore = fabsf(lscore-rscore);
- rscore = 0;
- close++;
- }
- *score += lscore + rscore;
- i1++;
- i2++;
- }
- }
- if (i1 < rm1->len)
- {
- /* Still more to go in rm1 */
- if (rm1->mask[i1].start < rm2->mask[rm2->len-1].stop)
- return 0;
- }
- else if (i2 < rm2->len)
- {
- /* Still more to go in rm2 */
- if (rm2->mask[i2].start < rm1->mask[rm1->len-1].stop)
- return 0;
- }
-
- return close;
-}
-
-static void region_mask_merge(region_mask *rm1, const region_mask *rm2, int newlen)
-{
- int o, i1, i2;
-
- /* First, ensure that rm1 is long enough */
- if (rm1->cap < newlen)
- {
- int newcap = rm1->cap ? rm1->cap : 2;
- do
- {
- newcap *= 2;
- }
- while (newcap < newlen);
- rm1->mask = fz_resize_array(rm1->ctx, rm1->mask, newcap, sizeof(*rm1->mask));
- rm1->cap = newcap;
- }
-
- /* Now run backwards along rm1, filling it out with the merged regions */
- for (o = newlen-1, i1 = rm1->len-1, i2 = rm2->len-1; o >= 0; o--)
- {
- /* So we read from i1 and i2 and store in o */
- if (i1 < 0)
- {
- /* Just copy i2 */
- rm1->mask[o] = rm2->mask[i2];
- i2--;
- }
- else if (i2 < 0)
- {
- /* Just copy i1 */
- rm1->mask[o] = rm1->mask[i1];
- i1--;
- }
- else if (rm1->mask[i1].stop < rm2->mask[i2].start)
- {
- /* rm1's region is entirely before rm2's - copy rm2's */
- rm1->mask[o] = rm2->mask[i2];
- i2--;
- }
- else if (rm2->mask[i2].stop < rm1->mask[i1].start)
- {
- /* rm2's region is entirely before rm1's - copy rm1's */
- rm1->mask[o] = rm1->mask[i1];
- i1--;
- }
- else
- {
- /* We must be merging */
- rm1->mask[o].ave_start = (rm1->mask[i1].start * rm1->freq + rm2->mask[i2].start * rm2->freq)/(rm1->freq + rm2->freq);
- rm1->mask[o].ave_stop = (rm1->mask[i1].stop * rm1->freq + rm2->mask[i2].stop * rm2->freq)/(rm1->freq + rm2->freq);
- rm1->mask[o].start = fz_min(rm1->mask[i1].start, rm2->mask[i2].start);
- rm1->mask[o].stop = fz_max(rm1->mask[i1].stop, rm2->mask[i2].stop);
- i1--;
- i2--;
- }
- }
- rm1->freq += rm2->freq;
- rm1->len = newlen;
-}
-
-static region_mask *region_masks_match(const region_masks *rms, const region_mask *rm, fz_text_line *line, region_mask *prev_match)
-{
- int i;
- float best_score = 9999999;
- float score;
- int best = -1;
- int best_count = 0;
-
- /* If the 'previous match' matches, use it regardless. */
- if (prev_match && region_mask_matches(prev_match, rm, &score))
- {
- return prev_match;
- }
-
- /* Run through and find the 'most compatible' region mask. We are
- * guaranteed that there will always be at least one compatible one!
- */
- for (i=0; i < rms->len; i++)
- {
- int count = region_mask_matches(rms->mask[i], rm, &score);
- if (count > best_count || (count == best_count && (score < best_score || best == -1)))
- {
- best = i;
- best_score = score;
- best_count = count;
- }
- }
- assert(best >= 0 && best < rms->len);
-
- /* So we have the matching mask. */
- return rms->mask[best];
-}
-
-#ifdef DEBUG_MASKS
-static void
-dump_region_mask(const region_mask *rm)
-{
- int j;
- for (j = 0; j < rm->len; j++)
- {
- printf("%g->%g ", rm->mask[j].start, rm->mask[j].stop);
- }
- printf("* %d\n", rm->freq);
-}
-
-static void
-dump_region_masks(const region_masks *rms)
-{
- int i;
-
- for (i = 0; i < rms->len; i++)
- {
- region_mask *rm = rms->mask[i];
- dump_region_mask(rm);
- }
-}
-#endif
-
-static void region_masks_add(region_masks *rms, region_mask *rm)
-{
- /* Add rm to rms */
- if (rms->len == rms->cap)
- {
- int newcap = (rms->cap ? rms->cap * 2 : 4);
- rms->mask = fz_resize_array(rms->ctx, rms->mask, newcap, sizeof(*rms->mask));
- rms->cap = newcap;
- }
- rms->mask[rms->len] = rm;
- rms->len++;
-}
-
-static void region_masks_sort(region_masks *rms)
-{
- int i, j;
-
- /* First calculate sizes */
- for (i=0; i < rms->len; i++)
- {
- region_mask *rm = rms->mask[i];
- float size = 0;
- for (j=0; j < rm->len; j++)
- {
- size += rm->mask[j].stop - rm->mask[j].start;
- }
- rm->size = size;
- }
-
- /* Now, sort on size */
- /* FIXME: bubble sort - use heapsort for efficiency */
- for (i=0; i < rms->len-1; i++)
- {
- for (j=i+1; j < rms->len; j++)
- {
- if (rms->mask[i]->size < rms->mask[j]->size)
- {
- region_mask *tmp = rms->mask[i];
- rms->mask[i] = rms->mask[j];
- rms->mask[j] = tmp;
- }
- }
- }
-}
-
-static void region_masks_merge(region_masks *rms, region_mask *rm)
-{
- int i;
- float best_score = 9999999;
- float score;
- int best = -1;
- int best_count = 0;
-
-#ifdef DEBUG_MASKS
- printf("\nAdding:\n");
- dump_region_mask(rm);
- printf("To:\n");
- dump_region_masks(rms);
-#endif
- for (i=0; i < rms->len; i++)
- {
- int count = region_masks_mergeable(rms->mask[i], rm, &score);
- if (count && (score < best_score || best == -1))
- {
- best = i;
- best_count = count;
- best_score = score;
- }
- }
- if (best != -1)
- {
- region_mask_merge(rms->mask[best], rm, best_count);
-#ifdef DEBUG_MASKS
- printf("Merges to give:\n");
- dump_region_masks(rms);
-#endif
- free_region_mask(rm);
- return;
- }
- region_masks_add(rms, rm);
-#ifdef DEBUG_MASKS
- printf("Adding new one to give:\n");
- dump_region_masks(rms);
-#endif
-}
-
-static region_mask *
-new_region_mask(fz_context *ctx, const fz_point *blv)
-{
- region_mask *rm = fz_malloc_struct(ctx, region_mask);
- rm->ctx = ctx;
- rm->freq = 1;
- rm->blv = *blv;
- rm->cap = 0;
- rm->len = 0;
- rm->mask = NULL;
- return rm;
-}
-
-static void
-region_mask_project(const region_mask *rm, const fz_point *min, const fz_point *max, float *start, float *end)
-{
- /* We project min and max down onto the blv */
- float s = min->x * rm->blv.x + min->y * rm->blv.y;
- float e = max->x * rm->blv.x + max->y * rm->blv.y;
- if (s > e)
- {
- *start = e;
- *end = s;
- }
- else
- {
- *start = s;
- *end = e;
- }
-}
-
-static void
-region_mask_add(region_mask *rm, const fz_point *min, const fz_point *max)
-{
- float start, end;
- int i, j;
-
- region_mask_project(rm, min, max, &start, &end);
-
- /* Now add start/end into our region list. Typically we will be adding
- * to the end of the region list, so search from there backwards. */
- for (i = rm->len; i > 0;)
- {
- if (start > rm->mask[i-1].stop)
- break;
- i--;
- }
- /* So we know that our interval can only affect list items >= i.
- * We know that start is after our previous end. */
- if (i == rm->len || end < rm->mask[i].start)
- {
- /* Insert new one. No overlap. No merging */
- if (rm->len == rm->cap)
- {
- int newcap = (rm->cap ? rm->cap * 2 : 4);
- rm->mask = fz_resize_array(rm->ctx, rm->mask, newcap, sizeof(*rm->mask));
- rm->cap = newcap;
- }
- if (rm->len > i)
- memmove(&rm->mask[i+1], &rm->mask[i], (rm->len - i) * sizeof(*rm->mask));
- rm->mask[i].ave_start = start;
- rm->mask[i].ave_stop = end;
- rm->mask[i].start = start;
- rm->mask[i].stop = end;
- rm->len++;
- }
- else
- {
- /* Extend current one down. */
- rm->mask[i].ave_start = start;
- rm->mask[i].start = start;
- if (rm->mask[i].stop < end)
- {
- rm->mask[i].stop = end;
- rm->mask[i].ave_stop = end;
- /* Our region may now extend upwards too far */
- i++;
- j = i;
- while (j < rm->len && rm->mask[j].start <= end)
- {
- rm->mask[i-1].stop = end = rm->mask[j].stop;
- j++;
- }
- if (i != j)
- {
- /* Move everything from j down to i */
- while (j < rm->len)
- {
- rm->mask[i++] = rm->mask[j++];
- }
- }
- rm->len -= j-i;
- }
- }
-}
-
-static int
-region_mask_column(region_mask *rm, const fz_point *min, const fz_point *max, int *align, float *colw, float *left_)
-{
- float start, end, left, right;
- int i;
-
- region_mask_project(rm, min, max, &start, &end);
-
- for (i = 0; i < rm->len; i++)
- {
- /* The use of MY_EPSILON here is because we might be matching
- * start/end values calculated with slightly different blv's */
- if (rm->mask[i].start - MY_EPSILON <= start && rm->mask[i].stop + MY_EPSILON >= end)
- break;
- }
- if (i >= rm->len)
- {
- *align = 0;
- *colw = 0;
- return 0;
- }
- left = start - rm->mask[i].start;
- right = rm->mask[i].stop - end;
- if (left < 1 && right < 1)
- *align = rm->mask[i].align;
- else if (left*2 <= right)
- *align = 0; /* Left */
- else if (right * 2 < left)
- *align = 2; /* Right */
- else
- *align = 1;
- *left_ = left;
- *colw = rm->mask[i].colw;
- return i;
-}
-
-static void
-region_mask_alignment(region_mask *rm)
-{
- int i;
- float width = 0;
-
- for (i = 0; i < rm->len; i++)
- {
- width += rm->mask[i].stop - rm->mask[i].start;
- }
- for (i = 0; i < rm->len; i++)
- {
- region *r = &rm->mask[i];
- float left = r->ave_start - r->start;
- float right = r->stop - r->ave_stop;
- if (left*2 <= right)
- r->align = 0; /* Left */
- else if (right * 2 < left)
- r->align = 2; /* Right */
- else
- r->align = 1;
- r->colw = 100 * (rm->mask[i].stop - rm->mask[i].start) / width;
- }
-}
-
-static void
-region_masks_alignment(region_masks *rms)
-{
- int i;
-
- for (i = 0; i < rms->len; i++)
- {
- region_mask_alignment(rms->mask[i]);
- }
-}
-
-static int
-is_unicode_hyphen(int c)
-{
- /* We omit 0x2011 (Non breaking hyphen) and 0x2043 (Hyphen Bullet)
- * from this list. */
- return (c == '-' ||
- c == 0x2010 || /* Hyphen */
- c == 0x002d || /* Hyphen-Minus */
- c == 0x00ad || /* Soft hyphen */
- c == 0x058a || /* Armenian Hyphen */
- c == 0x1400 || /* Canadian Syllabive Hyphen */
- c == 0x1806); /* Mongolian Todo soft hyphen */
-}
-
-static int
-is_unicode_hyphenatable(int c)
-{
- /* This is a pretty ad-hoc collection. It may need tuning. */
- return ((c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z') ||
- (c >= 0x00c0 && c <= 0x00d6) ||
- (c >= 0x00d8 && c <= 0x00f6) ||
- (c >= 0x00f8 && c <= 0x02af) ||
- (c >= 0x1d00 && c <= 0x1dbf) ||
- (c >= 0x1e00 && c <= 0x1eff) ||
- (c >= 0x2c60 && c <= 0x2c7f) ||
- (c >= 0xa722 && c <= 0xa78e) ||
- (c >= 0xa790 && c <= 0xa793) ||
- (c >= 0xa7a8 && c <= 0xa7af) ||
- (c >= 0xfb00 && c <= 0xfb07) ||
- (c >= 0xff21 && c <= 0xff3a) ||
- (c >= 0xff41 && c <= 0xff5a));
-}
-
-static void
-dehyphenate(fz_text_span *s1, fz_text_span *s2)
-{
- int i;
-
- for (i = s1->len-1; i > 0; i--)
- if (!is_unicode_wspace(s1->text[i].c))
- break;
- /* Can't leave an empty span. */
- if (i == 0)
- return;
-
- if (!is_unicode_hyphen(s1->text[i].c))
- return;
- if (!is_unicode_hyphenatable(s1->text[i-1].c))
- return;
- if (!is_unicode_hyphenatable(s2->text[0].c))
- return;
- s1->len = i;
- s2->spacing = 0;
-}
-
-void
-fz_analyze_text(fz_context *ctx, fz_text_sheet *sheet, fz_text_page *page)
-{
- fz_text_line *line;
- fz_text_span *span;
- line_heights *lh;
- region_masks *rms;
- int block_num;
-
- /* Simple paragraph analysis; look for the most common 'inter line'
- * spacing. This will be assumed to be our line spacing. Anything
- * more than 25% wider than this will be assumed to be a paragraph
- * space. */
-
- /* Step 1: Gather the line height information */
- lh = new_line_heights(ctx);
- for (block_num = 0; block_num < page->len; block_num++)
- {
- fz_text_block *block;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
-
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- /* For every style in the line, add lineheight to the
- * record for that style. FIXME: This is a nasty n^2
- * algorithm at the moment. */
- fz_text_style *style = NULL;
-
- if (line->distance == 0)
- continue;
-
- for (span = line->first_span; span; span = span->next)
- {
- int char_num;
-
- if (is_list_entry(line, span, &char_num))
- goto list_entry;
-
- for (; char_num < span->len; char_num++)
- {
- fz_text_char *chr = &span->text[char_num];
-
- /* Ignore any whitespace chars */
- if (is_unicode_wspace(chr->c))
- continue;
-
- if (chr->style != style)
- {
- /* Have we had this style before? */
- int match = 0;
- fz_text_span *span2;
- for (span2 = line->first_span; span2; span2 = span2->next)
- {
- int char_num2;
- for (char_num2 = 0; char_num2 < span2->len; char_num2++)
- {
- fz_text_char *chr2 = &span2->text[char_num2];
- if (chr2->style == chr->style)
- {
- match = 1;
- break;
- }
- }
- }
- if (char_num > 0 && match == 0)
- {
- fz_text_span *span2 = span;
- int char_num2;
- for (char_num2 = 0; char_num2 < char_num; char_num2++)
- {
- fz_text_char *chr2 = &span2->text[char_num2];
- if (chr2->style == chr->style)
- {
- match = 1;
- break;
- }
- }
- }
- if (match == 0)
- insert_line_height(lh, chr->style, line->distance);
- style = chr->style;
- }
- }
-list_entry:
- {}
- }
- }
- }
-
- /* Step 2: Find the most popular line height for each style */
- cull_line_heights(lh);
-
- /* Step 3: Run through the blocks, breaking each block into two if
- * the line height isn't right. */
- for (block_num = 0; block_num < page->len; block_num++)
- {
- int line_num;
- fz_text_block *block;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
-
- for (line_num = 0; line_num < block->len; line_num++)
- {
- /* For every style in the line, check to see if lineheight
- * is correct for that style. FIXME: We check each style
- * more than once, currently. */
- int ok = 0; /* -1 = early exit, split now. 0 = split. 1 = don't split. */
- fz_text_style *style = NULL;
- line = &block->lines[line_num];
-
- if (line->distance == 0)
- continue;
-
-#ifdef DEBUG_LINE_HEIGHTS
- printf("line height=%g nspans=%d\n", line->distance, line->len);
-#endif
- for (span = line->first_span; span; span = span->next)
- {
- int char_num;
-
- if (is_list_entry(line, span, &char_num))
- goto force_paragraph;
-
- /* Now we do the rest of the line */
- for (; char_num < span->len; char_num++)
- {
- fz_text_char *chr = &span->text[char_num];
-
- /* Ignore any whitespace chars */
- if (is_unicode_wspace(chr->c))
- continue;
-
- if (chr->style != style)
- {
- float proper_step = line_height_for_style(lh, chr->style);
- if (proper_step * 0.95 <= line->distance && line->distance <= proper_step * 1.05)
- {
- ok = 1;
- break;
- }
- style = chr->style;
- }
- }
- if (ok)
- break;
- }
- if (!ok)
- {
-force_paragraph:
- split_block(ctx, page, block_num, line_num);
- break;
- }
- }
- }
- free_line_heights(lh);
-
- /* Simple line region analysis:
- * For each line:
- * form a list of 'start/stop' points (henceforth a 'region mask')
- * find the normalised baseline vector for the line.
- * Store the region mask and baseline vector.
- * Collate lines that have compatible region masks and identical
- * baseline vectors.
- * If the collated masks are column-like, then split into columns.
- * Otherwise split into tables.
- */
- rms = new_region_masks(ctx);
-
- /* Step 1: Form the region masks and store them into a list with the
- * normalised baseline vectors. */
- for (block_num = 0; block_num < page->len; block_num++)
- {
- fz_text_block *block;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
-
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- fz_point blv;
- region_mask *rm;
-
-#ifdef DEBUG_MASKS
- printf("Line: ");
- dump_line(line);
-#endif
- blv = line->first_span->max;
- blv.x -= line->first_span->min.x;
- blv.y -= line->first_span->min.y;
- fz_normalize_vector(&blv);
-
- rm = new_region_mask(ctx, &blv);
- for (span = line->first_span; span; span = span->next)
- {
- fz_point *region_min = &span->min;
- fz_point *region_max = &span->max;
-
- /* Treat adjacent spans as one big region */
- while (span->next && span->next->spacing < 1.5)
- {
- span = span->next;
- region_max = &span->max;
- }
-
- region_mask_add(rm, region_min, region_max);
- }
-#ifdef DEBUG_MASKS
- dump_region_mask(rm);
-#endif
- region_masks_add(rms, rm);
- }
- }
-
- /* Step 2: Sort the region_masks by size of masked region */
- region_masks_sort(rms);
-
-#ifdef DEBUG_MASKS
- printf("Sorted list of regions:\n");
- dump_region_masks(rms);
-#endif
- /* Step 3: Merge the region masks where possible (large ones first) */
- {
- int i;
- region_masks *rms2;
- rms2 = new_region_masks(ctx);
- for (i=0; i < rms->len; i++)
- {
- region_mask *rm = rms->mask[i];
- rms->mask[i] = NULL;
- region_masks_merge(rms2, rm);
- }
- free_region_masks(rms);
- rms = rms2;
- }
-
-#ifdef DEBUG_MASKS
- printf("Merged list of regions:\n");
- dump_region_masks(rms);
-#endif
-
- /* Step 4: Figure out alignment */
- region_masks_alignment(rms);
-
- /* Step 5: At this point, we should probably look at the region masks
- * to try to guess which ones represent columns on the page. With our
- * current code, we could only get blocks of lines that span 2 or more
- * columns if the PDF producer wrote text out horizontally across 2
- * or more columns, and we've never seen that (yet!). So we skip this
- * step for now. */
-
- /* Step 6: Run through the lines again, deciding which ones fit into
- * which region mask. */
- {
- region_mask *prev_match = NULL;
- for (block_num = 0; block_num < page->len; block_num++)
- {
- fz_text_block *block;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
-
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- fz_point blv;
- region_mask *rm;
- region_mask *match;
-
- blv = line->first_span->max;
- blv.x -= line->first_span->min.x;
- blv.y -= line->first_span->min.y;
- fz_normalize_vector(&blv);
-
-#ifdef DEBUG_MASKS
- dump_line(line);
-#endif
- rm = new_region_mask(ctx, &blv);
- for (span = line->first_span; span; span = span->next)
- {
- fz_point *region_min = &span->min;
- fz_point *region_max = &span->max;
-
- /* Treat adjacent spans as one big region */
- while (span->next && span->next->spacing < 1.5)
- {
- span = span->next;
- region_max = &span->max;
- }
-
- region_mask_add(rm, region_min, region_max);
- }
-#ifdef DEBUG_MASKS
- printf("Mask: ");
- dump_region_mask(rm);
-#endif
- match = region_masks_match(rms, rm, line, prev_match);
- prev_match = match;
-#ifdef DEBUG_MASKS
- printf("Matches: ");
- dump_region_mask(match);
-#endif
- free_region_mask(rm);
- span = line->first_span;
- while (span)
- {
- fz_point *region_min = &span->min;
- fz_point *region_max = &span->max;
- fz_text_span *sn;
- int col, align;
- float colw, left;
-
- /* Treat adjacent spans as one big region */
-#ifdef DEBUG_ALIGN
- dump_span(span);
-#endif
- for (sn = span->next; sn && sn->spacing < 1.5; sn = sn->next)
- {
- region_max = &sn->max;
-#ifdef DEBUG_ALIGN
- dump_span(sn);
-#endif
- }
- col = region_mask_column(match, region_min, region_max, &align, &colw, &left);
-#ifdef DEBUG_ALIGN
- printf(" = col%d colw=%g align=%d\n", col, colw, align);
-#endif
- do
- {
- span->column = col;
- span->align = align;
- span->indent = left;
- span->column_width = colw;
- span = span->next;
- }
- while (span != sn);
-
- if (span)
- span = span->next;
- }
- line->region = match;
- }
- }
- free_region_masks(rms);
- }
-
- /* Step 7: Collate lines within a block that share the same region
- * mask. */
- for (block_num = 0; block_num < page->len; block_num++)
- {
- int line_num;
- int prev_line_num;
-
- fz_text_block *block;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
-
- /* First merge lines. This may leave empty lines behind. */
- for (prev_line_num = 0, line_num = 1; line_num < block->len; line_num++)
- {
- fz_text_line *prev_line;
- line = &block->lines[line_num];
- if (!line->first_span)
- continue;
- prev_line = &block->lines[prev_line_num];
- if (prev_line->region == line->region)
- {
- /* We only merge lines if the second line
- * only uses 1 of the columns. */
- int col = line->first_span->column;
- /* Copy the left value for the first span
- * in the first column in this line forward
- * for all the rest of the spans in the same
- * column. */
- float indent = line->first_span->indent;
- for (span = line->first_span->next; span; span = span->next)
- {
- if (col != span->column)
- break;
- span->indent = indent;
- }
- if (span)
- {
- prev_line_num = line_num;
- continue;
- }
-
- /* Merge line into prev_line */
- {
- fz_text_span **prev_line_span = &prev_line->first_span;
- int try_dehyphen = -1;
- fz_text_span *prev_span = NULL;
- span = line->first_span;
- while (span)
- {
- /* Skip forwards through the original
- * line, until we find a place where
- * span should go. */
- if ((*prev_line_span)->column <= span->column)
- {
- /* The current span we are considering
- * in prev_line is earlier than span.
- * Just skip forwards in prev_line. */
- prev_span = (*prev_line_span);
- prev_line_span = &prev_span->next;
- try_dehyphen = span->column;
- }
- else
- {
- /* We want to copy span into prev_line. */
- fz_text_span *next = (*prev_line_span)->next;
-
- if (prev_line_span == &prev_line->first_span)
- prev_line->first_span = span;
- if (next == NULL)
- prev_line->last_span = span;
- if (try_dehyphen == span->column)
- dehyphenate(prev_span, span);
- try_dehyphen = -1;
- prev_span = *prev_line_span = span;
- span = span->next;
- (*prev_line_span)->next = next;
- prev_line_span = &span->next;
- }
- }
- while (span || *prev_line_span);
- line->first_span = NULL;
- line->last_span = NULL;
- }
- }
- else
- prev_line_num = line_num;
- }
-
- /* Now get rid of the empty lines */
- for (prev_line_num = 0, line_num = 0; line_num < block->len; line_num++)
- {
- line = &block->lines[line_num];
- if (line->first_span)
- block->lines[prev_line_num++] = *line;
- }
- block->len = prev_line_num;
-
- /* Now try to spot indents */
- for (line_num = 0; line_num < block->len; line_num++)
- {
- fz_text_span *span_num, *sn;
- int col, count;
- line = &block->lines[line_num];
-
- /* Run through the spans... */
- span_num = line->first_span;
- {
- float indent = 0;
- /* For each set of spans that share the same
- * column... */
- col = span_num->column;
-#ifdef DEBUG_INDENTS
- printf("Indent %g: ", span_num->indent);
- dump_span(span_num);
- printf("\n");
-#endif
-
- /* find the average indent of all but the first.. */
- for (sn = span_num->next, count = 0; sn && sn->column == col; sn = sn->next, count++)
- {
-#ifdef DEBUG_INDENTS
- printf("Indent %g: ", sn->indent);
- dump_span(sn);
- printf("\n");
-#endif
- indent += sn->indent;
- sn->indent = 0;
- }
- if (sn != span_num->next)
- indent /= count;
-
- /* And compare this indent with the first one... */
-#ifdef DEBUG_INDENTS
- printf("Average indent %g ", indent);
-#endif
- indent -= span_num->indent;
-#ifdef DEBUG_INDENTS
- printf("delta %g ", indent);
-#endif
- if (fabsf(indent) < 1)
- {
- /* No indent worth speaking of */
- indent = 0;
- }
-#ifdef DEBUG_INDENTS
- printf("recorded %g\n", indent);
-#endif
- span_num->indent = indent;
- span_num = sn;
- }
- for (; span_num; span_num = span_num->next)
- {
- span_num->indent = 0;
- }
- }
- }
-}
diff --git a/fitz/text_search.c b/fitz/text_search.c
deleted file mode 100644
index f1f0d203..00000000
--- a/fitz/text_search.c
+++ /dev/null
@@ -1,279 +0,0 @@
-#include "mupdf/fitz.h"
-
-static inline int fz_tolower(int c)
-{
- /* TODO: proper unicode case folding */
- if (c >= 'A' && c <= 'Z')
- return c - 'A' + 'a';
- return c;
-}
-
-static inline int iswhite(int c)
-{
- return c == ' ' || c == '\r' || c == '\n' || c == '\t';
-}
-
-fz_char_and_box *fz_text_char_at(fz_char_and_box *cab, fz_text_page *page, int idx)
-{
- int block_num;
- int ofs = 0;
-
- for (block_num = 0; block_num < page->len; block_num++)
- {
- fz_text_block *block;
- fz_text_line *line;
- fz_text_span *span;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- for (span = line->first_span; span; span = span->next)
- {
- if (idx < ofs + span->len)
- {
- cab->c = span->text[idx - ofs].c;
- fz_text_char_bbox(&cab->bbox, span, idx - ofs);
- return cab;
- }
- ofs += span->len;
- }
- /* pseudo-newline */
- if (idx == ofs)
- {
- cab->bbox = fz_empty_rect;
- cab->c = ' ';
- return cab;
- }
- ofs++;
- }
- }
- cab->bbox = fz_empty_rect;
- cab->c = 0;
- return cab;
-}
-
-static int charat(fz_text_page *page, int idx)
-{
- fz_char_and_box cab;
- return fz_text_char_at(&cab, page, idx)->c;
-}
-
-static fz_rect *bboxat(fz_text_page *page, int idx, fz_rect *bbox)
-{
- fz_char_and_box cab;
- /* FIXME: Nasty extra copy */
- *bbox = fz_text_char_at(&cab, page, idx)->bbox;
- return bbox;
-}
-
-static int textlen(fz_text_page *page)
-{
- int len = 0;
- int block_num;
-
- for (block_num = 0; block_num < page->len; block_num++)
- {
- fz_text_block *block;
- fz_text_line *line;
- fz_text_span *span;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- for (span = line->first_span; span; span = span->next)
- {
- len += span->len;
- }
- len++; /* pseudo-newline */
- }
- }
- return len;
-}
-
-static int match(fz_text_page *page, const char *s, int n)
-{
- int orig = n;
- int c;
- while (*s)
- {
- s += fz_chartorune(&c, (char *)s);
- if (iswhite(c) && iswhite(charat(page, n)))
- {
- const char *s_next;
-
- /* Skip over whitespace in the document */
- do
- n++;
- while (iswhite(charat(page, n)));
-
- /* Skip over multiple whitespace in the search string */
- while (s_next = s + fz_chartorune(&c, (char *)s), iswhite(c))
- s = s_next;
- }
- else
- {
- if (fz_tolower(c) != fz_tolower(charat(page, n)))
- return 0;
- n++;
- }
- }
- return n - orig;
-}
-
-int
-fz_search_text_page(fz_context *ctx, fz_text_page *text, const char *needle, fz_rect *hit_bbox, int hit_max)
-{
- int pos, len, i, n, hit_count;
-
- if (strlen(needle) == 0)
- return 0;
-
- hit_count = 0;
- len = textlen(text);
- for (pos = 0; pos < len; pos++)
- {
- n = match(text, needle, pos);
- if (n)
- {
- fz_rect linebox = fz_empty_rect;
- for (i = 0; i < n; i++)
- {
- fz_rect charbox;
- bboxat(text, pos + i, &charbox);
- if (!fz_is_empty_rect(&charbox))
- {
- if (charbox.y0 != linebox.y0 || fz_abs(charbox.x0 - linebox.x1) > 5)
- {
- if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
- hit_bbox[hit_count++] = linebox;
- linebox = charbox;
- }
- else
- {
- fz_union_rect(&linebox, &charbox);
- }
- }
- }
- if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
- hit_bbox[hit_count++] = linebox;
- }
- }
-
- return hit_count;
-}
-
-int
-fz_highlight_selection(fz_context *ctx, fz_text_page *page, fz_rect rect, fz_rect *hit_bbox, int hit_max)
-{
- fz_rect linebox, charbox;
- fz_text_block *block;
- fz_text_line *line;
- fz_text_span *span;
- int i, block_num, hit_count;
-
- float x0 = rect.x0;
- float x1 = rect.x1;
- float y0 = rect.y0;
- float y1 = rect.y1;
-
- hit_count = 0;
-
- for (block_num = 0; block_num < page->len; block_num++)
- {
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- linebox = fz_empty_rect;
- for (span = line->first_span; span; span = span->next)
- {
- for (i = 0; i < span->len; i++)
- {
- fz_text_char_bbox(&charbox, span, i);
- if (charbox.x1 >= x0 && charbox.x0 <= x1 && charbox.y1 >= y0 && charbox.y0 <= y1)
- {
- if (charbox.y0 != linebox.y0 || fz_abs(charbox.x0 - linebox.x1) > 5)
- {
- if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
- hit_bbox[hit_count++] = linebox;
- linebox = charbox;
- }
- else
- {
- fz_union_rect(&linebox, &charbox);
- }
- }
- }
- }
- if (!fz_is_empty_rect(&linebox) && hit_count < hit_max)
- hit_bbox[hit_count++] = linebox;
- }
- }
-
- return hit_count;
-}
-
-char *
-fz_copy_selection(fz_context *ctx, fz_text_page *page, fz_rect rect)
-{
- fz_buffer *buffer;
- fz_rect hitbox;
- int c, i, block_num, seen = 0;
- char *s;
-
- float x0 = rect.x0;
- float x1 = rect.x1;
- float y0 = rect.y0;
- float y1 = rect.y1;
-
- buffer = fz_new_buffer(ctx, 1024);
-
- for (block_num = 0; block_num < page->len; block_num++)
- {
- fz_text_block *block;
- fz_text_line *line;
- fz_text_span *span;
-
- if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
- continue;
- block = page->blocks[block_num].u.text;
- for (line = block->lines; line < block->lines + block->len; line++)
- {
- for (span = line->first_span; span; span = span->next)
- {
- if (seen)
- {
- fz_write_buffer_byte(ctx, buffer, '\n');
- }
-
- seen = 0;
-
- for (i = 0; i < span->len; i++)
- {
- fz_text_char_bbox(&hitbox, span, i);
- c = span->text[i].c;
- if (c < 32)
- c = '?';
- if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1)
- {
- fz_write_buffer_rune(ctx, buffer, c);
- seen = 1;
- }
- }
-
- seen = (seen && span == line->last_span);
- }
- }
- }
-
- fz_write_buffer_byte(ctx, buffer, 0);
-
- s = (char*)buffer->data;
- fz_free(ctx, buffer);
- return s;
-}
--
cgit v1.2.3