Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JorjMcKie committed Nov 19, 2018
1 parent 7d88a74 commit e47f16f
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 178 deletions.
Binary file modified doc/PyMuPDF.pdf
Binary file not shown.
Binary file modified doc/html.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions fitz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
TOOLS = fitz.Tools()
fitz.TOOLS = TOOLS

if fitz.VersionFitz != fitz.TOOLS.mupdf_version():
raise ValueError("MuPDF library mismatch %s <> %s" % (fitz.VersionFitz, fitz.TOOLS.mupdf_version()))

import fitz.utils

# copy functions to their respective fitz classes
Expand Down
70 changes: 49 additions & 21 deletions fitz/fitz.i
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ fz_set_stderr(gctx, JM_fitz_stderr);
if (JM_fitz_stderr && JM_fitz_stdout)
{;}
else
PySys_WriteStdout("error redirecting stdout/stderr!\n");
PySys_WriteStderr("error redirecting stdout/stderr!\n");

JM_error_log = PyList_New(0);
JM_output_log = PyList_New(0);
JM_error_log = PyByteArray_FromStringAndSize("", 0);
JM_output_log = PyByteArray_FromStringAndSize("", 0);

//-----------------------------------------------------------------------------
// STOP redirect stdout/stderr
Expand All @@ -172,6 +172,7 @@ struct DeviceWrapper {
// include version information and several other helpers
//-----------------------------------------------------------------------------
%pythoncode %{
import os
import weakref
from binascii import hexlify
import math
Expand Down Expand Up @@ -211,18 +212,27 @@ struct fz_document_s
FITZEXCEPTION(fz_document_s, !result)

%pythonprepend fz_document_s %{
if not filename or type(filename) == str:
if not filename or type(filename) is str:
pass
elif type(filename) == unicode:
filename = filename.encode('utf8')
else:
raise TypeError("filename must be string or None")
self.name = filename if filename else ""
if str is bytes: # Python 2
if type(filename) is unicode:
filename = filename.encode("utf8")
else:
filename = str(filename) # should take care of pathlib

self.streamlen = len(stream) if stream else 0
if stream and not (filename or filetype):
raise ValueError("filetype missing with stream specified")
if stream and type(stream) not in (bytes, bytearray):
raise ValueError("stream must be bytes or bytearray")

self.name = ""
if filename and self.streamlen == 0:
self.name = filename

if self.streamlen > 0:
if not (filename or filetype):
raise ValueError("filetype missing with stream specified")
if type(stream) not in (bytes, bytearray):
raise ValueError("stream must be bytes or bytearray")

self.isClosed = False
self.isEncrypted = 0
self.metadata = None
Expand Down Expand Up @@ -2102,18 +2112,18 @@ struct fz_page_s {
inklist = pdf_new_array(gctx, annot->page->doc, n0);
for (j = 0; j < n0; j++)
{
sublist = PySequence_GetItem(list, j);
sublist = PySequence_ITEM(list, j);
n1 = PySequence_Size(sublist);
stroke = pdf_new_array(gctx, annot->page->doc, 2 * n1);
for (i = 0; i < n1; i++)
{
p = PySequence_GetItem(sublist, i);
p = PySequence_ITEM(sublist, i);
if (!PySequence_Check(p) || PySequence_Size(p) != 2)
THROWMSG("3rd level entries must be pairs of floats");
x = PyFloat_AsDouble(PySequence_GetItem(p, 0));
x = PyFloat_AsDouble(PySequence_ITEM(p, 0));
if (PyErr_Occurred())
THROWMSG("invalid point coordinate");
y = PyFloat_AsDouble(PySequence_GetItem(p, 1));
y = PyFloat_AsDouble(PySequence_ITEM(p, 1));
if (PyErr_Occurred())
THROWMSG("invalid point coordinate");
Py_CLEAR(p);
Expand Down Expand Up @@ -4559,7 +4569,7 @@ struct fz_annot_s
if (n>0)
{
for (i=0; i<n; i++)
col[i] = (float) PyFloat_AsDouble(PySequence_GetItem(ccol, i));
col[i] = (float) PyFloat_AsDouble(PySequence_ITEM(ccol, i));
fz_try(gctx)
pdf_set_annot_color(gctx, annot, n, col);
fz_catch(gctx)
Expand All @@ -4577,7 +4587,7 @@ struct fz_annot_s
return;
}
for (i=0; i<n; i++)
col[i] = (float) PyFloat_AsDouble(PySequence_GetItem(icol, i));
col[i] = (float) PyFloat_AsDouble(PySequence_ITEM(icol, i));
fz_try(gctx)
pdf_set_annot_interior_color(gctx, annot, n, col);
fz_catch(gctx)
Expand Down Expand Up @@ -5998,16 +6008,34 @@ struct Tools
}
%pythoncode%{@property%}
PyObject *fitz_stderr()
char *fitz_stdout()
{
return PyUnicode_Join(Py_BuildValue("s", ""), JM_error_log);
return PyByteArray_AS_STRING(JM_output_log);
}
%feature("autodoc","Empty fitz output log.") empty_error_log;
void fitz_stdout_reset()
{
Py_CLEAR(JM_output_log);
JM_output_log = PyByteArray_FromStringAndSize("", 0);
}
%pythoncode%{@property%}
char *fitz_stderr()
{
return PyByteArray_AS_STRING(JM_error_log);
}
%feature("autodoc","Empty fitz error log.") empty_error_log;
void fitz_stderr_reset()
{
Py_CLEAR(JM_error_log);
JM_error_log = PyList_New(0);
JM_error_log = PyByteArray_FromStringAndSize("", 0);
}
char *mupdf_version()
{
return FZ_VERSION;
}
PyObject *transform_rect(PyObject *rect, PyObject *matrix)
Expand Down
52 changes: 39 additions & 13 deletions fitz/fitz.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class _object:
_newclass = 0


import os
import weakref
from binascii import hexlify
import math
Expand All @@ -105,9 +106,9 @@ class _object:


VersionFitz = "1.14.0"
VersionBind = "1.14.0"
VersionDate = "2018-11-16 05:14:22"
version = (VersionBind, VersionFitz, "20181116051422")
VersionBind = "1.14.1"
VersionDate = "2018-11-18 17:07:00"
version = (VersionBind, VersionFitz, "20181118170700")


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

if not filename or type(filename) == str:
if not filename or type(filename) is str:
pass
elif type(filename) == unicode:
filename = filename.encode('utf8')
else:
raise TypeError("filename must be string or None")
self.name = filename if filename else ""
if str is bytes: # Python 2
if type(filename) is unicode:
filename = filename.encode("utf8")
else:
filename = str(filename) # should take care of pathlib

self.streamlen = len(stream) if stream else 0
if stream and not (filename or filetype):
raise ValueError("filetype missing with stream specified")
if stream and type(stream) not in (bytes, bytearray):
raise ValueError("stream must be bytes or bytearray")

self.name = ""
if filename and self.streamlen == 0:
self.name = filename

if self.streamlen > 0:
if not (filename or filetype):
raise ValueError("filetype missing with stream specified")
if type(stream) not in (bytes, bytearray):
raise ValueError("stream must be bytes or bytearray")

self.isClosed = False
self.isEncrypted = 0
self.metadata = None
Expand Down Expand Up @@ -4095,8 +4105,19 @@ def glyph_cache_empty(self):

@property

def fitz_stdout(self):
"""fitz_stdout(self) -> char *"""
return _fitz.Tools_fitz_stdout(self)


def fitz_stdout_reset(self):
"""fitz_stdout_reset(self)"""
return _fitz.Tools_fitz_stdout_reset(self)

@property

def fitz_stderr(self):
"""fitz_stderr(self) -> PyObject *"""
"""fitz_stderr(self) -> char *"""
return _fitz.Tools_fitz_stderr(self)


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


def mupdf_version(self):
"""mupdf_version(self) -> char *"""
return _fitz.Tools_mupdf_version(self)


def transform_rect(self, rect, matrix):
"""transform_rect(self, rect, matrix) -> PyObject *"""
return _fitz.Tools_transform_rect(self, rect, matrix)
Expand Down
Loading

0 comments on commit e47f16f

Please sign in to comment.