Skip to content

Commit e47f16f

Browse files
committed
performance improvements
1 parent 7d88a74 commit e47f16f

13 files changed

+319
-178
lines changed

doc/PyMuPDF.pdf

-11 KB
Binary file not shown.

doc/html.zip

1.12 KB
Binary file not shown.

fitz/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
TOOLS = fitz.Tools()
1414
fitz.TOOLS = TOOLS
1515

16+
if fitz.VersionFitz != fitz.TOOLS.mupdf_version():
17+
raise ValueError("MuPDF library mismatch %s <> %s" % (fitz.VersionFitz, fitz.TOOLS.mupdf_version()))
18+
1619
import fitz.utils
1720

1821
# copy functions to their respective fitz classes

fitz/fitz.i

+49-21
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ fz_set_stderr(gctx, JM_fitz_stderr);
145145
if (JM_fitz_stderr && JM_fitz_stdout)
146146
{;}
147147
else
148-
PySys_WriteStdout("error redirecting stdout/stderr!\n");
148+
PySys_WriteStderr("error redirecting stdout/stderr!\n");
149149

150-
JM_error_log = PyList_New(0);
151-
JM_output_log = PyList_New(0);
150+
JM_error_log = PyByteArray_FromStringAndSize("", 0);
151+
JM_output_log = PyByteArray_FromStringAndSize("", 0);
152152

153153
//-----------------------------------------------------------------------------
154154
// STOP redirect stdout/stderr
@@ -172,6 +172,7 @@ struct DeviceWrapper {
172172
// include version information and several other helpers
173173
//-----------------------------------------------------------------------------
174174
%pythoncode %{
175+
import os
175176
import weakref
176177
from binascii import hexlify
177178
import math
@@ -211,18 +212,27 @@ struct fz_document_s
211212
FITZEXCEPTION(fz_document_s, !result)
212213

213214
%pythonprepend fz_document_s %{
214-
if not filename or type(filename) == str:
215+
if not filename or type(filename) is str:
215216
pass
216-
elif type(filename) == unicode:
217-
filename = filename.encode('utf8')
218217
else:
219-
raise TypeError("filename must be string or None")
220-
self.name = filename if filename else ""
218+
if str is bytes: # Python 2
219+
if type(filename) is unicode:
220+
filename = filename.encode("utf8")
221+
else:
222+
filename = str(filename) # should take care of pathlib
223+
221224
self.streamlen = len(stream) if stream else 0
222-
if stream and not (filename or filetype):
223-
raise ValueError("filetype missing with stream specified")
224-
if stream and type(stream) not in (bytes, bytearray):
225-
raise ValueError("stream must be bytes or bytearray")
225+
226+
self.name = ""
227+
if filename and self.streamlen == 0:
228+
self.name = filename
229+
230+
if self.streamlen > 0:
231+
if not (filename or filetype):
232+
raise ValueError("filetype missing with stream specified")
233+
if type(stream) not in (bytes, bytearray):
234+
raise ValueError("stream must be bytes or bytearray")
235+
226236
self.isClosed = False
227237
self.isEncrypted = 0
228238
self.metadata = None
@@ -2102,18 +2112,18 @@ struct fz_page_s {
21022112
inklist = pdf_new_array(gctx, annot->page->doc, n0);
21032113
for (j = 0; j < n0; j++)
21042114
{
2105-
sublist = PySequence_GetItem(list, j);
2115+
sublist = PySequence_ITEM(list, j);
21062116
n1 = PySequence_Size(sublist);
21072117
stroke = pdf_new_array(gctx, annot->page->doc, 2 * n1);
21082118
for (i = 0; i < n1; i++)
21092119
{
2110-
p = PySequence_GetItem(sublist, i);
2120+
p = PySequence_ITEM(sublist, i);
21112121
if (!PySequence_Check(p) || PySequence_Size(p) != 2)
21122122
THROWMSG("3rd level entries must be pairs of floats");
2113-
x = PyFloat_AsDouble(PySequence_GetItem(p, 0));
2123+
x = PyFloat_AsDouble(PySequence_ITEM(p, 0));
21142124
if (PyErr_Occurred())
21152125
THROWMSG("invalid point coordinate");
2116-
y = PyFloat_AsDouble(PySequence_GetItem(p, 1));
2126+
y = PyFloat_AsDouble(PySequence_ITEM(p, 1));
21172127
if (PyErr_Occurred())
21182128
THROWMSG("invalid point coordinate");
21192129
Py_CLEAR(p);
@@ -4559,7 +4569,7 @@ struct fz_annot_s
45594569
if (n>0)
45604570
{
45614571
for (i=0; i<n; i++)
4562-
col[i] = (float) PyFloat_AsDouble(PySequence_GetItem(ccol, i));
4572+
col[i] = (float) PyFloat_AsDouble(PySequence_ITEM(ccol, i));
45634573
fz_try(gctx)
45644574
pdf_set_annot_color(gctx, annot, n, col);
45654575
fz_catch(gctx)
@@ -4577,7 +4587,7 @@ struct fz_annot_s
45774587
return;
45784588
}
45794589
for (i=0; i<n; i++)
4580-
col[i] = (float) PyFloat_AsDouble(PySequence_GetItem(icol, i));
4590+
col[i] = (float) PyFloat_AsDouble(PySequence_ITEM(icol, i));
45814591
fz_try(gctx)
45824592
pdf_set_annot_interior_color(gctx, annot, n, col);
45834593
fz_catch(gctx)
@@ -5998,16 +6008,34 @@ struct Tools
59986008
}
59996009
60006010
%pythoncode%{@property%}
6001-
PyObject *fitz_stderr()
6011+
char *fitz_stdout()
60026012
{
6003-
return PyUnicode_Join(Py_BuildValue("s", ""), JM_error_log);
6013+
return PyByteArray_AS_STRING(JM_output_log);
6014+
}
6015+
6016+
%feature("autodoc","Empty fitz output log.") empty_error_log;
6017+
void fitz_stdout_reset()
6018+
{
6019+
Py_CLEAR(JM_output_log);
6020+
JM_output_log = PyByteArray_FromStringAndSize("", 0);
6021+
}
6022+
6023+
%pythoncode%{@property%}
6024+
char *fitz_stderr()
6025+
{
6026+
return PyByteArray_AS_STRING(JM_error_log);
60046027
}
60056028
60066029
%feature("autodoc","Empty fitz error log.") empty_error_log;
60076030
void fitz_stderr_reset()
60086031
{
60096032
Py_CLEAR(JM_error_log);
6010-
JM_error_log = PyList_New(0);
6033+
JM_error_log = PyByteArray_FromStringAndSize("", 0);
6034+
}
6035+
6036+
char *mupdf_version()
6037+
{
6038+
return FZ_VERSION;
60116039
}
60126040
60136041
PyObject *transform_rect(PyObject *rect, PyObject *matrix)

fitz/fitz.py

+39-13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class _object:
9696
_newclass = 0
9797

9898

99+
import os
99100
import weakref
100101
from binascii import hexlify
101102
import math
@@ -105,9 +106,9 @@ class _object:
105106

106107

107108
VersionFitz = "1.14.0"
108-
VersionBind = "1.14.0"
109-
VersionDate = "2018-11-16 05:14:22"
110-
version = (VersionBind, VersionFitz, "20181116051422")
109+
VersionBind = "1.14.1"
110+
VersionDate = "2018-11-18 17:07:00"
111+
version = (VersionBind, VersionFitz, "20181118170700")
111112

112113

113114
class Matrix():
@@ -1572,18 +1573,27 @@ class Document(_object):
15721573
def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0, height=0, fontsize=11):
15731574
"""__init__(self, filename=None, stream=None, filetype=None, rect=None, width=0, height=0, fontsize=11) -> Document"""
15741575

1575-
if not filename or type(filename) == str:
1576+
if not filename or type(filename) is str:
15761577
pass
1577-
elif type(filename) == unicode:
1578-
filename = filename.encode('utf8')
15791578
else:
1580-
raise TypeError("filename must be string or None")
1581-
self.name = filename if filename else ""
1579+
if str is bytes: # Python 2
1580+
if type(filename) is unicode:
1581+
filename = filename.encode("utf8")
1582+
else:
1583+
filename = str(filename) # should take care of pathlib
1584+
15821585
self.streamlen = len(stream) if stream else 0
1583-
if stream and not (filename or filetype):
1584-
raise ValueError("filetype missing with stream specified")
1585-
if stream and type(stream) not in (bytes, bytearray):
1586-
raise ValueError("stream must be bytes or bytearray")
1586+
1587+
self.name = ""
1588+
if filename and self.streamlen == 0:
1589+
self.name = filename
1590+
1591+
if self.streamlen > 0:
1592+
if not (filename or filetype):
1593+
raise ValueError("filetype missing with stream specified")
1594+
if type(stream) not in (bytes, bytearray):
1595+
raise ValueError("stream must be bytes or bytearray")
1596+
15871597
self.isClosed = False
15881598
self.isEncrypted = 0
15891599
self.metadata = None
@@ -4095,8 +4105,19 @@ def glyph_cache_empty(self):
40954105

40964106
@property
40974107

4108+
def fitz_stdout(self):
4109+
"""fitz_stdout(self) -> char *"""
4110+
return _fitz.Tools_fitz_stdout(self)
4111+
4112+
4113+
def fitz_stdout_reset(self):
4114+
"""fitz_stdout_reset(self)"""
4115+
return _fitz.Tools_fitz_stdout_reset(self)
4116+
4117+
@property
4118+
40984119
def fitz_stderr(self):
4099-
"""fitz_stderr(self) -> PyObject *"""
4120+
"""fitz_stderr(self) -> char *"""
41004121
return _fitz.Tools_fitz_stderr(self)
41014122

41024123

@@ -4105,6 +4126,11 @@ def fitz_stderr_reset(self):
41054126
return _fitz.Tools_fitz_stderr_reset(self)
41064127

41074128

4129+
def mupdf_version(self):
4130+
"""mupdf_version(self) -> char *"""
4131+
return _fitz.Tools_mupdf_version(self)
4132+
4133+
41084134
def transform_rect(self, rect, matrix):
41094135
"""transform_rect(self, rect, matrix) -> PyObject *"""
41104136
return _fitz.Tools_transform_rect(self, rect, matrix)

0 commit comments

Comments
 (0)