Skip to content

Commit 3bc5632

Browse files
authored
Merge pull request #226 from JorjMcKie/master
Uploading first patch
2 parents f4bca38 + e47f16f commit 3bc5632

15 files changed

+329
-187
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PyMuPDF 1.14.0
1+
# PyMuPDF 1.14.1
22

33
![logo](https://github.com/rk700/PyMuPDF/blob/master/demo/pymupdf.jpg)
44

@@ -14,7 +14,7 @@ On **[PyPI](https://pypi.org/project/PyMuPDF)** since August 2016: [![](https://
1414

1515
# Introduction
1616

17-
This is **version 1.14.0 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.14.0](http://mupdf.com/) - "a lightweight PDF, XPS, and E-book viewer".
17+
This is **version 1.14.1 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.14.x](http://mupdf.com/) - "a lightweight PDF, XPS, and E-book viewer".
1818

1919
MuPDF can access files in PDF, XPS, OpenXPS, CBZ, EPUB and FB2 (e-books) formats, and it is known for its top performance and high rendering quality.
2020

@@ -30,14 +30,15 @@ The platform tag for Mac OSX is `macosx_10_6_intel`.
3030

3131
The platform tag for Linux is `manylinux1_x86_64`, which makes these wheels usable on Debian, Ubuntu and most other variations.
3232

33-
On other operating systems you need to generate PyMuPDF yourself. And of course you can choose to do so for a wheel-supported platform, too. Before you can do this, you must download and generate MuPDF. This process depends very much on your system. For most platforms, the MuPDF source contains prepared procedures for achieving this.
33+
On other operating systems you need to generate PyMuPDF yourself. And of course you can choose to do so for a wheel-supported platform, too. Before you can do this, you must download and generate MuPDF. This process depends very much on your system. For most platforms, the MuPDF source contains prepared procedures for achieving this. Please observe the following general steps:
3434

35-
* Be sure to download the official MuPDF source release from [here](https://mupdf.com/downloads). Do **not use** MuPDF's [GitHub repo](https://github.com/ArtifexSoftware/mupdf). It contains their current **development source**, which is **not compatible** with this PyMuPDF version most of the time.
35+
* Be sure to download the official MuPDF source release from [here](https://mupdf.com/downloads/archive). Do **not use** MuPDF's [GitHub repo](https://github.com/ArtifexSoftware/mupdf). It contains their current **development source**, which is **not compatible** with this PyMuPDF version most of the time.
3636

37-
* The repo's `fitz` folder contains a few files whose names start with an underscore. These files contain configuration data and hotfixes and each must be copy-renamed to its correct target location of the downloaded MuPDF source **before you generate MuPDF**. Currently, these files are:
38-
- fitz configuration file `_mupdf_config.h` copy-replace to `mupdf/include/fitz/config.h`.
39-
- fitz error module `_error.c`, copy-replace to `mupdf/source/fitz/error.c`.
40-
- PDF device module `_pdf-device.c` copy-replace `mupdf/source/pdf/pdf-device.c`.
37+
* The repo's `fitz` folder contains a few files whose names start with an underscore `"_"`. These files contain configuration data and hotfixes. Each one must be copy-renamed to its correct target location of the MuPDF source that you have downloaded, **before you generate MuPDF**. Currently, these files are:
38+
- fitz configuration file `_mupdf_config.h` copy-replace to: `mupdf/include/fitz/config.h`. It contains configuration data like e.g. which fonts to support.
39+
- fitz error module `_error.c`, copy-replace to: `mupdf/source/fitz/error.c`. It redirects MuPDF warnings and errors so they can be intercepted by PyMuPDF.
40+
- PDF device module `_pdf-device.c` copy-replace to: `mupdf/source/pdf/pdf-device.c`. It fixes a bug which caused method `Document.convertToPDF()` to bring down the interpeter.
41+
- Now MuPDF can be generated.
4142

4243
Once this is done, adjust directories in ``setup.py`` and run ``python setup.py install``.
4344

doc/PyMuPDF.pdf

-11 KB
Binary file not shown.

doc/html.zip

1.12 KB
Binary file not shown.

fitz/__init__.py

Lines changed: 3 additions & 0 deletions
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

Lines changed: 49 additions & 21 deletions
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

Lines changed: 39 additions & 13 deletions
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)