Skip to content

Commit

Permalink
update mupdf
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Sep 19, 2024
1 parent d0d76d2 commit 7a9a444
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 113 deletions.
4 changes: 4 additions & 0 deletions mupdf/include/mupdf/fitz/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <math.h>
#include <assert.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

/**
Multiply scaled two integers in the 0..255 range
*/
Expand Down
1 change: 1 addition & 0 deletions mupdf/include/mupdf/pdf/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,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_indirect(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int num);
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);
Expand Down
2 changes: 2 additions & 0 deletions mupdf/platform/gl/gl-annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ static void save_pdf_options(void)
ui_checkbox("Compress", &save_opts.do_compress);
ui_checkbox("Compress images", &save_opts.do_compress_images);
ui_checkbox("Compress fonts", &save_opts.do_compress_fonts);
ui_checkbox("Object streams", &save_opts.do_use_objstms);
ui_checkbox("Preserve metadata", &save_opts.do_preserve_metadata);

if (save_opts.do_incremental)
{
Expand Down
105 changes: 29 additions & 76 deletions mupdf/scripts/wrap/swig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,32 +1705,6 @@ def fz_install_load_system_font_funcs(f=None, f_cjk=None, f_fallback=None):

text += '%}\n'

text2_code = textwrap.dedent( '''
''')

if text2_code.strip():
text2 = textwrap.dedent( f'''
%{{
#include "mupdf/fitz.h"
#include "mupdf/classes.h"
#include "mupdf/classes2.h"
#include <vector>
{text2_code}
%}}
%include std_vector.i
namespace std
{{
%template(vectori) vector<int>;
}};
{text2_code}
''')
else:
text2 = ''

if 1: # lgtm [py/constant-conditional-expression]
# This is a horrible hack to avoid swig failing because
# include/mupdf/pdf/object.h defines an enum which contains a #include.
Expand Down Expand Up @@ -1768,10 +1742,7 @@ def fz_install_load_system_font_funcs(f=None, f_cjk=None, f_fallback=None):
os.makedirs( f'{build_dirs.dir_mupdf}/platform/{language}', exist_ok=True)
os.makedirs( f'{build_dirs.dir_so}', exist_ok=True)
util.update_file_regress( text, swig_i, check_regress)
if text2:
util.update_file_regress( text2, swig2_i, check_regress)
else:
jlib.fs_update( '', swig2_i)
jlib.fs_update( '', swig2_i)

# Disable some unhelpful SWIG warnings. Must not use -Wall as it overrides
# all warning disables.
Expand Down Expand Up @@ -1815,7 +1786,7 @@ def make_command( module, cpp, swig_i):
# include/mupdf/fitz/heap.h. Otherwise swig's preprocessor seems to
# ignore #undef's in include/mupdf/fitz/heap-imp.h then complains
# about redefinition of macros in include/mupdf/fitz/heap.h.
command = (f'''
command = f'''
"{swig_command}"
{"-D_WIN32" if state_.windows else ""}
-c++
Expand All @@ -1824,7 +1795,7 @@ def make_command( module, cpp, swig_i):
-Wextra
{disable_swig_warnings}
-module {module}
-outdir {os.path.relpath(build_dirs.dir_so)}
-outdir {os.path.relpath(build_dirs.dir_mupdf)}/platform/python
-o {cpp}
-includeall
{os.environ.get('XCXXFLAGS', '')}
Expand All @@ -1834,60 +1805,42 @@ def make_command( module, cpp, swig_i):
-ignoremissing
-DMUPDF_FITZ_HEAP_H
{swig_i}
''')
'''
return command

def modify_py( rebuilt, swig_py, do_enums):
if not rebuilt:
jlib.log(f'Not rebuilding {swig_py=} because {rebuilt=}.')
return
swig_py_leaf = os.path.basename( swig_py)
assert swig_py_leaf.endswith( '.py')
so = f'_{swig_py_leaf[:-3]}.so'
swig_py_tmp = f'{swig_py}-'
jlib.fs_remove( swig_py_tmp)
os.rename( swig_py, swig_py_tmp)
with open( swig_py_tmp) as f:
swig_py_content = f.read()

if do_enums:
# Change all our PDF_ENUM_NAME_* enums so that they are actually
# PdfObj instances so that they can be used like any other PdfObj.
#
#jlib.log('{len(generated.c_enums)=}')
for enum_type, enum_names in generated.c_enums.items():
for enum_name in enum_names:
if enum_name.startswith( 'PDF_ENUM_NAME_'):
swig_py_content += f'{enum_name} = {rename.class_("pdf_obj")}( obj_enum_to_obj( {enum_name}))\n'

with open( swig_py_tmp, 'w') as f:
f.write( swig_py_content)
os.rename( swig_py_tmp, swig_py)

if text2:
# Make mupdf2, for mupdfpy optimisations.
jlib.log( 'Running SWIG to generate mupdf2 .cpp')
command = make_command( 'mupdf2', swig2_cpp, swig2_i)
rebuilt = jlib.build(
(swig2_i, include1, include2),
(swig2_cpp, swig2_py),
command,
force_rebuild,
)
modify_py( rebuilt, swig2_py, do_enums=False)
else:
jlib.fs_update( '', swig2_cpp)
jlib.fs_remove( swig2_py)
def modify_py( path_in, path_out):
with open( path_in) as f:
text = f.read()

# Change all our PDF_ENUM_NAME_* enums so that they are actually
# PdfObj instances so that they can be used like any other PdfObj.
#
#jlib.log('{len(generated.c_enums)=}')
for enum_type, enum_names in generated.c_enums.items():
for enum_name in enum_names:
if enum_name.startswith( 'PDF_ENUM_NAME_'):
text += f'{enum_name} = {rename.class_("pdf_obj")}( obj_enum_to_obj( {enum_name}))\n'

for name in ('NULL', 'TRUE', 'FALSE', 'LIMIT'):
text += f'PDF_{name} = {rename.class_("pdf_obj")}( obj_enum_to_obj( PDF_ENUM_{name}))\n'

jlib.fs_update(text, path_out)

jlib.fs_update( '', swig2_cpp)
jlib.fs_remove( swig2_py)

# Make main mupdf .so.
command = make_command( 'mupdf', swig_cpp, swig_i)
swig_py_ = f'{build_dirs.dir_mupdf}/platform/python/mupdf.py'
rebuilt = jlib.build(
(swig_i, include1, include2),
(swig_cpp, swig_py),
(swig_cpp, swig_py_),
command,
force_rebuild,
)
modify_py( rebuilt, swig_py, do_enums=True)
jlib.log(f'swig => {rebuilt=}.')
updated = modify_py( swig_py_, swig_py)
jlib.log(f'modify_py() => {updated=}.')


elif language == 'csharp':
Expand Down
6 changes: 0 additions & 6 deletions mupdf/source/fitz/deskew.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@

#include "mupdf/fitz.h"

#include <math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

//#define DEBUG_DESKEWER

//int errprintf_nomem(const char *string, ...);
Expand Down
1 change: 0 additions & 1 deletion mupdf/source/fitz/stext-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include "glyphbox.h"

#include <math.h>
#include <float.h>
#include <string.h>

Expand Down
5 changes: 0 additions & 5 deletions mupdf/source/fitz/warp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "pixmap-imp.h"

#include <string.h>
#include <math.h>

/* Define TIMINGS to get timing information dumped to stdout. */
#undef TIMINGS
Expand Down Expand Up @@ -1413,10 +1412,6 @@ clean(fz_context *ctx, fz_pixmap *src)
static int16_t sintable[270];
#define costable (&sintable[90])

#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif

/* We have collected an array of edge data.
* For each pixel, we know whether there is a 'strong' edge
* there, and if so, in which of 4 directions it runs.
Expand Down
1 change: 0 additions & 1 deletion mupdf/source/pdf/pdf-appearance.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include <float.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <time.h>

Expand Down
5 changes: 5 additions & 0 deletions mupdf/source/pdf/pdf-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3828,6 +3828,11 @@ void pdf_dict_put_text_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, cons
pdf_dict_put_drop(ctx, dict, key, pdf_new_text_string(ctx, x));
}

void pdf_dict_put_indirect(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int num)
{
pdf_dict_put_drop(ctx, dict, key, pdf_new_indirect(ctx, pdf_get_bound_document(ctx, dict), num, 0));
}

void pdf_dict_put_point(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_point x)
{
pdf_dict_put_drop(ctx, dict, key, pdf_new_point(ctx, pdf_get_bound_document(ctx, dict), x));
Expand Down
23 changes: 17 additions & 6 deletions mupdf/source/pdf/pdf-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -2278,10 +2278,14 @@ static void writexref(fz_context *ctx, pdf_document *doc, pdf_write_state *opts,
if (obj)
pdf_dict_put(ctx, trailer, PDF_NAME(ID), obj);

/* The encryption dictionary is kept in the writer state to handle
the encryption dictionary object being renumbered during repair.*/
if (opts->crypt_obj)
{
/* If the encryption dictionary used to be an indirect reference from the trailer,
store it the same way in the trailer in the saved file. */
if (pdf_is_indirect(ctx, opts->crypt_obj))
pdf_dict_put_drop(ctx, trailer, PDF_NAME(Encrypt), pdf_new_indirect(ctx, doc, opts->crypt_object_number, 0));
pdf_dict_put_indirect(ctx, trailer, PDF_NAME(Encrypt), opts->crypt_object_number);
else
pdf_dict_put(ctx, trailer, PDF_NAME(Encrypt), opts->crypt_obj);
}
Expand Down Expand Up @@ -2387,11 +2391,16 @@ static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_state
if (obj)
pdf_dict_put(ctx, dict, PDF_NAME(ID), obj);

if (opts->do_incremental)
/* The encryption dictionary is kept in the writer state to handle
the encryption dictionary object being renumbered during repair.*/
if (opts->crypt_obj)
{
obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Encrypt));
if (obj)
pdf_dict_put(ctx, dict, PDF_NAME(Encrypt), obj);
/* If the encryption dictionary used to be an indirect reference from the trailer,
store it the same way in the xref stream in the saved file. */
if (pdf_is_indirect(ctx, opts->crypt_obj))
pdf_dict_put_indirect(ctx, dict, PDF_NAME(Encrypt), opts->crypt_object_number);
else
pdf_dict_put(ctx, dict, PDF_NAME(Encrypt), opts->crypt_obj);
}
}

Expand Down Expand Up @@ -3658,7 +3667,9 @@ objstm_gather(fz_context *ctx, pdf_xref_entry *x, int i, pdf_document *doc, void
pdf_cache_object(ctx, doc, i);

if (x->type != 'n' || x->stm_buf != NULL || x->stm_ofs != 0 || x->gen != 0)
return; /* Ineligible for using an objstm */
return; /* Stream objects, objects with generation number != 0 cannot be put in objstms */
if (i == data->opts->crypt_object_number)
return; /* Encryption dictionaries can also not be put in objstms */

/* FIXME: Can we do a pass through to check for such objects more exactly? */
if (pdf_is_int(ctx, x->obj))
Expand Down
2 changes: 0 additions & 2 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,6 @@ workspace "SumatraPDF"
kind "StaticLib"
language "C"
mixed_dbg_rel_conf()
-- enable M_PI in <math.h>
defines { "_USE_MATH_DEFINES" }
-- for openjpeg, OPJ_STATIC is alrady defined in load-jpx.c
-- so we can't double-define it
defines { "USE_JPIP", "OPJ_EXPORTS", "HAVE_LCMS2MT=1" }
Expand Down
Loading

0 comments on commit 7a9a444

Please sign in to comment.