Skip to content

Commit

Permalink
update mupdf
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Feb 28, 2023
1 parent 7b925f9 commit c4e78d0
Show file tree
Hide file tree
Showing 18 changed files with 268 additions and 172 deletions.
12 changes: 12 additions & 0 deletions mupdf/Makerules
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ ifeq "$(OS)" "wasm"
HAVE_LIBCRYPTO=no
endif

ifeq "$(OS)" "wasm-mt"
build_prefix += wasm-mt/
CC = emcc
CXX = em++
AR = emar
HAVE_GLUT=no
HAVE_X11=no
HAVE_OBJCOPY=no
HAVE_LIBCRYPTO=no
CFLAGS += -pthread
endif

ifeq "$(OS)" "mingw32-cross"
build_prefix += $(OS)/
SO := dll
Expand Down
20 changes: 0 additions & 20 deletions mupdf/include/mupdf/fitz/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,24 +819,4 @@ fz_drop_imp16(fz_context *ctx, void *p, int16_t *refs)
return 0;
}


#if WASM_SKIP_TRY_CATCH

/**
Exception macro definitions for WASM_SKIP_TRY_CATCH. In this mode, we
throw JS exceptions directly, and we skip fz_catch and fz_always.
Useful for producing cleaner stack traces when debugging.
Should *never* be used in production.
*/
#undef fz_var
#define fz_var(var) (void)(var)
#undef fz_try
#define fz_try(ctx) do
#undef fz_always
#define fz_always(ctx) while (0); if (0) do
#undef fz_catch
#define fz_catch(ctx) while (0); if (0)

#endif

#endif
2 changes: 1 addition & 1 deletion mupdf/include/mupdf/pdf/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,10 @@ struct pdf_document
int last_xref_was_old_style;
int has_linearization_object;

int page_map_nesting;
int map_page_count;
pdf_rev_page_map *rev_page_map;
int *fwd_page_map;
int page_tree_broken;

int repair_attempted;
int repair_in_progress;
Expand Down
15 changes: 7 additions & 8 deletions mupdf/include/mupdf/pdf/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ pdf_obj *pdf_lookup_page_obj(fz_context *ctx, pdf_document *doc, int needle);
/*
Cache the page tree for fast forward/reverse page lookups.
It is the caller's responsibility to ensure that nothing alters
the page tree between this call and the matching pdf_drop_page_tree
or undefined behaviour may occur.
Any successful call to pdf_load_page_tree MUST be matched with
a call to pdf_drop_page_tree.
No longer required. This is a No Op, now as page tree
maps are loaded automatically 'just in time'.
*/
void pdf_load_page_tree(fz_context *ctx, pdf_document *doc);

/*
Discard the page tree maps.
Calls to this should exactly match successful calls to
pdf_load_page_tree.
No longer required. This is a No Op, now as page tree
maps are discarded automatically 'just in time'.
*/
void pdf_drop_page_tree(fz_context *ctx, pdf_document *doc);

void pdf_drop_page_tree_internal(fz_context *ctx, pdf_document *doc);


/*
Find the page number of a named destination.
Expand Down
4 changes: 1 addition & 3 deletions mupdf/platform/java/jni/pdfobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,8 @@ FUN(PDFObject_equals)(JNIEnv *env, jobject self, jobject jother)
pdf_obj *other = NULL;
int result = 0;

if (!ctx || !obj) return JNI_FALSE;
if (!ctx) return JNI_FALSE;

if (jother == NULL)
return JNI_FALSE;
if (!(*env)->IsInstanceOf(env, jother, cls_PDFObject))
return JNI_FALSE;

Expand Down
16 changes: 0 additions & 16 deletions mupdf/source/fitz/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
#endif
#endif

#if WASM_SKIP_TRY_CATCH
#include "emscripten.h"
#endif

#ifdef __ANDROID__
#define USE_ANDROID_LOG
#include <android/log.h>
Expand Down Expand Up @@ -178,8 +174,6 @@ fz_error_cb *fz_error_callback(fz_context *ctx, void **user)

FZ_NORETURN static void throw(fz_context *ctx, int code)
{
#if !WASM_SKIP_TRY_CATCH

if (ctx->error.top > ctx->error.stack_base)
{
ctx->error.top->state += 2;
Expand All @@ -204,16 +198,6 @@ FZ_NORETURN static void throw(fz_context *ctx, int code)
*p = 0;
exit(EXIT_FAILURE);
}

#else
EM_ASM({
let message = UTF8ToString($0);
console.error("mupdf:", message);
throw new libmupdf.MupdfError(message);
}, ctx->error.message);
// Unreachable
exit(EXIT_FAILURE);
#endif
}

fz_jmp_buf *fz_push_try(fz_context *ctx)
Expand Down
6 changes: 6 additions & 0 deletions mupdf/source/fitz/filter-dct.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ static void error_exit_dct(j_common_ptr cinfo)
fz_throw(ctx, FZ_ERROR_GENERIC, "jpeg error: %s", msg);
}

static void output_message_dct(j_common_ptr cinfo)
{
/* swallow message */
}

static void init_source_dct(j_decompress_ptr cinfo)
{
/* nothing to do */
Expand Down Expand Up @@ -369,6 +374,7 @@ fz_open_dctd(fz_context *ctx, fz_stream *chain, int color_transform, int l2facto
cinfo->src = NULL;
cinfo->err = &state->errmgr;
jpeg_std_error(cinfo->err);
cinfo->err->output_message = output_message_dct;
cinfo->err->error_exit = error_exit_dct;

return fz_new_stream(ctx, state, next_dctd, close_dctd);
Expand Down
6 changes: 6 additions & 0 deletions mupdf/source/fitz/load-jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ fz_jpg_mem_term(j_common_ptr cinfo)

#endif /* SHARE_JPEG */

static void output_message(j_common_ptr cinfo)
{
/* swallow message */
}

static void error_exit(j_common_ptr cinfo)
{
char msg[JMSG_LENGTH_MAX];
Expand Down Expand Up @@ -374,6 +379,7 @@ fz_load_jpeg(fz_context *ctx, const unsigned char *rbuf, size_t rlen)
cinfo.mem = NULL;
cinfo.global_state = 0;
cinfo.err = jpeg_std_error(&err);
err.output_message = output_message;
err.error_exit = error_exit;

cinfo.client_data = NULL;
Expand Down
6 changes: 5 additions & 1 deletion mupdf/source/fitz/outline.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,17 @@ load_outline_sub(fz_context *ctx, fz_outline_iterator *iter, fz_outline **tail,
node->refs = 1;
node->title = *t;
node->uri = *u;
node->page.chapter = -1;
node->page.page = -1;
*t = NULL;
*u = NULL;
node->page = fz_resolve_link(ctx, iter->doc, node->uri, &node->x, &node->y);
*tail = node;
tail = &node->next;
onode = node;
node = NULL;

onode->page = fz_resolve_link(ctx, iter->doc, onode->uri, &onode->x, &onode->y);

res = fz_outline_iterator_down(ctx, iter);
if (res == 0)
load_outline_sub(ctx, iter, &onode->down, t, u);
Expand All @@ -140,6 +143,7 @@ fz_load_outline_from_iterator(fz_context *ctx, fz_outline_iterator *iter)
fz_drop_outline_iterator(ctx, iter);
fz_catch(ctx)
{
fz_drop_outline(ctx, head);
fz_free(ctx, title);
fz_free(ctx, uri);
fz_rethrow(ctx);
Expand Down
72 changes: 72 additions & 0 deletions mupdf/source/fitz/stext-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,59 @@ vec_dot(const fz_point *a, const fz_point *b)
return a->x * b->x + a->y * b->y;
}

static void
prepend_line_if_possible(fz_context *ctx, fz_stext_block *cur_block, fz_point q)
{
fz_stext_line *cur_line;
fz_stext_line *line;
fz_point ndir;
float size;
fz_point p;
fz_point delta;
float spacing;

if (cur_block == NULL || cur_block->type != FZ_STEXT_BLOCK_TEXT)
return;

cur_line = cur_block->u.t.last_line;
if (cur_line == NULL)
return;

line = cur_line->prev;
if (line == NULL)
return;

if (line->wmode != cur_line->wmode)
return;

ndir = cur_line->dir;
size = cur_line->last_char->size;
p = line->first_char->origin;
delta.x = p.x - q.x;
delta.y = p.y - q.y;

spacing = ndir.x * delta.x + ndir.y * delta.y;

if (fabsf(spacing) >= size * SPACE_MAX_DIST)
return;

/* cur_line plausibly finishes at the start of line. */
/* Move all the chars from cur_line onto the start of line */
cur_line->last_char->next = line->first_char;
line->first_char = cur_line->first_char;
cur_line->first_char = NULL;
cur_line->last_char = NULL;

/* Merge the bboxes */
line->bbox = fz_union_rect(line->bbox, cur_line->bbox);

/* Unlink cur_line from the block. */
cur_block->u.t.last_line = cur_block->u.t.last_line->prev;
cur_block->u.t.last_line->next = NULL;

/* Can't bin the line storage as it's from a pool. */
}

static void
fz_add_stext_char_imp(fz_context *ctx, fz_stext_device *dev, fz_font *font, int c, int glyph, fz_matrix trm, float adv, int wmode, int force_new_line)
{
Expand Down Expand Up @@ -497,6 +550,14 @@ fz_add_stext_char_imp(fz_context *ctx, fz_stext_device *dev, fz_font *font, int
new_line = 0;
}

if (new_line)
{
/* We are about to start a new line. This means we've finished with this
* one. Can this be prepended to a previous line in this block? */
/* dev->pen records the previous stopping point - so where cur_line ends. */
prepend_line_if_possible(ctx, cur_block, dev->pen);
}

/* Start a new line */
if (new_line || !cur_line || force_new_line)
{
Expand All @@ -516,6 +577,15 @@ fz_add_stext_char_imp(fz_context *ctx, fz_stext_device *dev, fz_font *font, int
dev->trm = trm;
}

static void
flush_text(fz_context *ctx, fz_stext_device *dev)
{
fz_stext_page *page = dev->page;

/* Find current position to enter new text. */
prepend_line_if_possible(ctx, page->last_block, dev->pen);
}

static void
fz_add_stext_char(fz_context *ctx, fz_stext_device *dev, fz_font *font, int c, int glyph, fz_matrix trm, float adv, int wmode, int force_new_line)
{
Expand Down Expand Up @@ -794,6 +864,8 @@ fz_stext_close_device(fz_context *ctx, fz_device *dev)
fz_stext_line *line;
fz_stext_char *ch;

flush_text(ctx, tdev);

for (block = page->first_block; block; block = block->next)
{
if (block->type != FZ_STEXT_BLOCK_TEXT)
Expand Down
Loading

0 comments on commit c4e78d0

Please sign in to comment.