Skip to content

Commit

Permalink
update mupdf
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Sep 10, 2024
1 parent 1bd5c1a commit cad932c
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 61 deletions.
4 changes: 3 additions & 1 deletion mupdf/include/mupdf/fitz/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,16 @@ struct fz_font
void *t3resources;
fz_buffer **t3procs; /* has 256 entries if used */
struct fz_display_list **t3lists; /* has 256 entries if used */
float *t3widths; /* has 256 entries if used */
unsigned short *t3flags; /* has 256 entries if used */
void *t3doc; /* a pdf_document for the callback */
void (*t3run)(fz_context *ctx, void *doc, void *resources, fz_buffer *contents, struct fz_device *dev, fz_matrix ctm, void *gstate, fz_default_colorspaces *default_cs);
void (*t3freeres)(fz_context *ctx, void *doc, void *resources);

fz_rect bbox; /* font bbox is used only for t3 fonts */

float ascender;
float descender;

int glyph_count;

/* per glyph bounding box cache. */
Expand Down
2 changes: 2 additions & 0 deletions mupdf/include/mupdf/pdf/annot.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,10 @@ int pdf_annot_has_intent(fz_context *ctx, pdf_annot *annot);
enum pdf_intent pdf_annot_intent(fz_context *ctx, pdf_annot *annot);
void pdf_set_annot_intent(fz_context *ctx, pdf_annot *annot, enum pdf_intent it);

void pdf_parse_default_appearance_unmapped(fz_context *ctx, const char *da, char *font_name, int font_name_len, float *size, int *n, float color[4]);
void pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font, float *size, int *n, float color[4]);
void pdf_print_default_appearance(fz_context *ctx, char *buf, int nbuf, const char *font, float size, int n, const float *color);
void pdf_annot_default_appearance_unmapped(fz_context *ctx, pdf_annot *annot, char *font_name, int font_name_len, float *size, int *n, float color[4]);
void pdf_annot_default_appearance(fz_context *ctx, pdf_annot *annot, const char **font, float *size, int *n, float color[4]);
void pdf_set_annot_default_appearance(fz_context *ctx, pdf_annot *annot, const char *font, float size, int n, const float *color);

Expand Down
4 changes: 4 additions & 0 deletions mupdf/include/mupdf/pdf/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pdf_obj *pdf_new_text_string(fz_context *ctx, const char *s);
pdf_obj *pdf_new_indirect(fz_context *ctx, pdf_document *doc, int num, int gen);
pdf_obj *pdf_new_array(fz_context *ctx, pdf_document *doc, int initialcap);
pdf_obj *pdf_new_dict(fz_context *ctx, pdf_document *doc, int initialcap);
pdf_obj *pdf_new_point(fz_context *ctx, pdf_document *doc, fz_point point);
pdf_obj *pdf_new_rect(fz_context *ctx, pdf_document *doc, fz_rect rect);
pdf_obj *pdf_new_matrix(fz_context *ctx, pdf_document *doc, fz_matrix mtx);
pdf_obj *pdf_new_date(fz_context *ctx, pdf_document *doc, int64_t time);
Expand Down Expand Up @@ -198,6 +199,7 @@ void pdf_dict_put_real(fz_context *ctx, pdf_obj *dict, pdf_obj *key, double x);
void pdf_dict_put_name(fz_context *ctx, pdf_obj *dict, pdf_obj *key, const char *x);
void pdf_dict_put_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, const char *x, size_t n);
void pdf_dict_put_text_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, const char *x);
void pdf_dict_put_point(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_point x);
void pdf_dict_put_rect(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_rect x);
void pdf_dict_put_matrix(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_matrix x);
void pdf_dict_put_date(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int64_t time);
Expand All @@ -213,6 +215,7 @@ const char *pdf_dict_get_name(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
const char *pdf_dict_get_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, size_t *sizep);
const char *pdf_dict_get_text_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
const char *pdf_dict_get_text_string_opt(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
fz_point pdf_dict_get_point(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
fz_rect pdf_dict_get_rect(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
fz_matrix pdf_dict_get_matrix(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
int64_t pdf_dict_get_date(fz_context *ctx, pdf_obj *dict, pdf_obj *key);
Expand Down Expand Up @@ -301,6 +304,7 @@ char *pdf_new_utf8_from_pdf_stream_obj(fz_context *ctx, pdf_obj *src);
char *pdf_load_stream_or_string_as_utf8(fz_context *ctx, pdf_obj *src);

fz_quad pdf_to_quad(fz_context *ctx, pdf_obj *array, int offset);
fz_point pdf_to_point(fz_context *ctx, pdf_obj *array, int offset);
fz_rect pdf_to_rect(fz_context *ctx, pdf_obj *array);
fz_matrix pdf_to_matrix(fz_context *ctx, pdf_obj *array);
int64_t pdf_to_date(fz_context *ctx, pdf_obj *time);
Expand Down
7 changes: 5 additions & 2 deletions mupdf/platform/gl/gl-annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ void do_annotate_panel(void)
int q;
const char *text_lang;
const char *text_font;
char text_font_buf[20];
char lang_buf[8];
static float text_size_f, text_color[4];
static int text_size;
Expand All @@ -1115,10 +1116,10 @@ void do_annotate_panel(void)
pdf_set_annot_language(ctx, ui.selected_annot, fz_text_language_from_string(text_lang));
}

pdf_annot_default_appearance(ctx, ui.selected_annot, &text_font, &text_size_f, &n, text_color);
pdf_annot_default_appearance_unmapped(ctx, ui.selected_annot, text_font_buf, sizeof text_font_buf, &text_size_f, &n, text_color);
text_size = text_size_f;
ui_label("Text Font:");
font_choice = ui_select("DA/Font", text_font, font_names, nelem(font_names));
font_choice = ui_select("DA/Font", text_font_buf, font_names, nelem(font_names));
ui_label("Text Size: %d", text_size);
size_changed = ui_slider(&text_size, 8, 36, 256);
ui_label("Text Color:");
Expand All @@ -1127,6 +1128,8 @@ void do_annotate_panel(void)
{
if (font_choice != -1)
text_font = font_names[font_choice];
else
text_font = text_font_buf;
if (color_choice != -1)
{
n = 3;
Expand Down
15 changes: 15 additions & 0 deletions mupdf/platform/java/jni/pdfdocument.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,21 @@ FUN(PDFDocument_verifyEmbeddedFileChecksum)(JNIEnv *env, jobject self, jobject j
return valid ? JNI_TRUE : JNI_FALSE;
}

JNIEXPORT jboolean JNICALL
FUN(PDFDocument_isEmbeddedFile)(JNIEnv *env, jobject self, jobject jfs)
{
fz_context *ctx = get_context(env);
pdf_obj *fs = from_PDFObject_safe(env, jfs);
int embedded = 0;

fz_try(ctx)
embedded = pdf_is_embedded_file(ctx, fs);
fz_catch(ctx)
jni_rethrow(env, ctx);

return embedded ? JNI_TRUE : JNI_FALSE;
}

JNIEXPORT void JNICALL
FUN(PDFDocument_setPageLabels)(JNIEnv *env, jobject self, jint index, jint style, jstring jprefix, jint start)
{
Expand Down
8 changes: 8 additions & 0 deletions mupdf/platform/java/mupdf_native.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public static String appendDestToURI(String uri, LinkDestination dest) {
public native PDFFilespecParams getFilespecParams(PDFObject fs);
public native Buffer loadEmbeddedFileContents(PDFObject fs);
public native boolean verifyEmbeddedFileChecksum(PDFObject fs);
public native boolean isEmbeddedFile(PDFObject fs);

public PDFObject addEmbeddedFile(String filename, String mimetype, InputStream stream, Date created, Date modified, boolean addChecksum) {
Buffer contents = new Buffer();
Expand Down
70 changes: 22 additions & 48 deletions mupdf/source/fitz/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ fz_new_font(fz_context *ctx, const char *name, int use_glyph_bbox, int glyph_cou
font->t3resources = NULL;
font->t3procs = NULL;
font->t3lists = NULL;
font->t3widths = NULL;
font->t3flags = NULL;
font->t3doc = NULL;
font->t3run = NULL;
Expand Down Expand Up @@ -206,7 +205,6 @@ fz_drop_font(fz_context *ctx, fz_font *font)
fz_drop_display_list(ctx, font->t3lists[i]);
fz_free(ctx, font->t3procs);
fz_free(ctx, font->t3lists);
fz_free(ctx, font->t3widths);
fz_free(ctx, font->t3flags);

if (font->ft_face)
Expand Down Expand Up @@ -274,28 +272,12 @@ fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float x

float fz_font_ascender(fz_context *ctx, fz_font *font)
{
if (font->t3procs)
return font->bbox.y1;
else
{
FT_Face face = font->ft_face;
if (face->ascender == 0)
return 0.8f;
return (float)face->ascender / face->units_per_EM;
}
return font->ascender;
}

float fz_font_descender(fz_context *ctx, fz_font *font)
{
if (font->t3procs)
return font->bbox.y0;
else
{
FT_Face face = font->ft_face;
if (face->descender == 0)
return -0.2f;
return (float)face->descender / face->units_per_EM;
}
return font->descender;
}

/*
Expand Down Expand Up @@ -787,6 +769,16 @@ fz_new_font_from_buffer(fz_context *ctx, const char *name, fz_buffer *buffer, in
(float) face->bbox.xMax / face->units_per_EM,
(float) face->bbox.yMax / face->units_per_EM);

if (face->ascender == 0)
font->ascender = 0.8f;
else
font->ascender = (float)face->ascender / face->units_per_EM;

if (face->descender == 0)
font->descender = -0.2f;
else
font->descender = (float)face->descender / face->units_per_EM;

font->subfont = index;

font->flags.is_mono = !!(face->face_flags & FT_FACE_FLAG_FIXED_WIDTH);
Expand Down Expand Up @@ -1538,7 +1530,6 @@ fz_new_type3_font(fz_context *ctx, const char *name, fz_matrix matrix)
{
font->t3procs = fz_calloc(ctx, 256, sizeof(fz_buffer*));
font->t3lists = fz_calloc(ctx, 256, sizeof(fz_display_list*));
font->t3widths = fz_calloc(ctx, 256, sizeof(float));
font->t3flags = fz_calloc(ctx, 256, sizeof(unsigned short));
}
fz_catch(ctx)
Expand Down Expand Up @@ -1835,17 +1826,6 @@ fz_advance_ft_glyph_aux(fz_context *ctx, fz_font *font, int gid, int wmode, int
FT_Fixed adv = 0;
int mask;

/* PDF and substitute font widths. */
if (font->flags.ft_stretch)
{
if (font->width_table)
{
if (gid < font->width_count)
return font->width_table[gid] / 1000.0f;
return font->width_default / 1000.0f;
}
}

mask = FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_IGNORE_TRANSFORM;
if (wmode)
mask |= FT_LOAD_VERTICAL_LAYOUT;
Expand All @@ -1857,12 +1837,7 @@ fz_advance_ft_glyph_aux(fz_context *ctx, fz_font *font, int gid, int wmode, int
if (fterr && fterr != FT_Err_Invalid_Argument)
{
fz_warn(ctx, "FT_Get_Advance(%s,%d): %s", font->name, gid, ft_error_string(fterr));
if (font->width_table)
{
if (gid < font->width_count)
return font->width_table[gid] / 1000.0f;
return font->width_default / 1000.0f;
}
return 0;
}
return (float) adv / ((FT_Face)font->ft_face)->units_per_EM;
}
Expand All @@ -1873,14 +1848,6 @@ fz_advance_ft_glyph(fz_context *ctx, fz_font *font, int gid, int wmode)
return fz_advance_ft_glyph_aux(ctx, font, gid, wmode, 0);
}

static float
fz_advance_t3_glyph(fz_context *ctx, fz_font *font, int gid)
{
if (gid < 0 || gid > 255)
return 0;
return font->t3widths[gid];
}

void
fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size)
{
Expand Down Expand Up @@ -1908,6 +1875,14 @@ fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size
float
fz_advance_glyph(fz_context *ctx, fz_font *font, int gid, int wmode)
{
/* Use PDF font widths table if available */
if (font->width_table)
{
if (gid < font->width_count)
return font->width_table[gid] / 1000.0f;
return font->width_default / 1000.0f;
}

if (font->ft_face)
{
if (wmode)
Expand Down Expand Up @@ -1953,8 +1928,7 @@ fz_advance_glyph(fz_context *ctx, fz_font *font, int gid, int wmode)

return fz_advance_ft_glyph(ctx, font, gid, 0);
}
if (font->t3procs)
return fz_advance_t3_glyph(ctx, font, gid);

return 0;
}

Expand Down
47 changes: 40 additions & 7 deletions mupdf/source/pdf/pdf-annot.c
Original file line number Diff line number Diff line change
Expand Up @@ -3319,13 +3319,13 @@ pdf_set_annot_intent(fz_context *ctx, pdf_annot *annot, enum pdf_intent it)
}

void
pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font, float *size, int *n, float color[4])
pdf_parse_default_appearance_unmapped(fz_context *ctx, const char *da, char *font_name, int font_name_size, float *size, int *n, float color[4])
{
char buf[100], *p = buf, *tok, *end;
float stack[4] = { 0, 0, 0, 0 };
int top = 0;

*font = "Helv";
fz_strlcpy(font_name, "Helv", font_name_size);
*size = 12;
*n = 0;
color[0] = color[1] = color[2] = color[3] = 0;
Expand All @@ -3337,11 +3337,7 @@ pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font,
;
else if (tok[0] == '/')
{
if (!strcmp(tok+1, "Cour")) *font = "Cour";
if (!strcmp(tok+1, "Helv")) *font = "Helv";
if (!strcmp(tok+1, "TiRo")) *font = "TiRo";
if (!strcmp(tok+1, "Symb")) *font = "Symb";
if (!strcmp(tok+1, "ZaDb")) *font = "ZaDb";
fz_strlcpy(font_name, tok+1, font_name_size);
}
else if (!strcmp(tok, "Tf"))
{
Expand Down Expand Up @@ -3384,6 +3380,21 @@ pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font,
}
}

void
pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font, float *size, int *n, float color[4])
{
char font_name[100];

pdf_parse_default_appearance_unmapped(ctx, da, font_name, sizeof font_name, size, n, color);

if (!strcmp(font_name, "Cour")) *font = "Cour";
else if (!strcmp(font_name, "Helv")) *font = "Helv";
else if (!strcmp(font_name, "TiRo")) *font = "TiRo";
else if (!strcmp(font_name, "Symb")) *font = "Symb";
else if (!strcmp(font_name, "ZaDb")) *font = "ZaDb";
else *font = "Helv";
}

void
pdf_print_default_appearance(fz_context *ctx, char *buf, int nbuf, const char *font, float size, int n, const float *color)
{
Expand All @@ -3397,6 +3408,28 @@ pdf_print_default_appearance(fz_context *ctx, char *buf, int nbuf, const char *f
fz_snprintf(buf, nbuf, "/%s %g Tf", font, size);
}

void
pdf_annot_default_appearance_unmapped(fz_context *ctx, pdf_annot *annot, char *font_name, int font_name_len, float *size, int *n, float color[4])
{
pdf_obj *da;
pdf_annot_push_local_xref(ctx, annot);

fz_try(ctx)
{
da = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(DA));
if (!da)
{
pdf_obj *trailer = pdf_trailer(ctx, annot->page->doc);
da = pdf_dict_getl(ctx, trailer, PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DA), NULL);
}
pdf_parse_default_appearance_unmapped(ctx, pdf_to_str_buf(ctx, da), font_name, font_name_len, size, n, color);
}
fz_always(ctx)
pdf_annot_pop_local_xref(ctx, annot);
fz_catch(ctx)
fz_rethrow(ctx);
}

void
pdf_annot_default_appearance(fz_context *ctx, pdf_annot *annot, const char **font, float *size, int *n, float color[4])
{
Expand Down
6 changes: 6 additions & 0 deletions mupdf/source/pdf/pdf-font.c
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,12 @@ pdf_load_font_descriptor(fz_context *ctx, pdf_document *doc, pdf_font_desc *font
if (fontdesc->descent == 0.0f)
fontdesc->descent = 1000.0f * face->descender / face->units_per_EM;
}

/* Prefer FontDescriptor Ascent/Descent values to embedded font's */
if (fontdesc->ascent)
fontdesc->font->ascender = fontdesc->ascent / 1000.0f;
if (fontdesc->descent)
fontdesc->font->descender = fontdesc->descent / 1000.0f;
}

static void
Expand Down
4 changes: 3 additions & 1 deletion mupdf/source/pdf/pdf-image.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ pdf_load_image_imp(fz_context *ctx, pdf_document *doc, pdf_obj *rdb, pdf_obj *di
decode[i] = i & 1 ? maxval : 0;
}

obj = pdf_dict_geta(ctx, dict, PDF_NAME(SMask), PDF_NAME(Mask));
obj = pdf_dict_get(ctx, dict, PDF_NAME(SMask));
if (!pdf_is_dict(ctx, obj))
obj = pdf_dict_get(ctx, dict, PDF_NAME(Mask));
if (pdf_is_dict(ctx, obj))
{
/* Not allowed for inline images or soft masks */
Expand Down
Loading

0 comments on commit cad932c

Please sign in to comment.