From 247e0b557bef931fd43b8fd5a2f504333b55751c Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 25 Feb 2019 02:36:54 -0500 Subject: [PATCH] Port qt_hack plugin to build on python2/python3 --- src/calibre/ebooks/pdf/render/qt_hack.cpp | 9 ++++++++- src/calibre/ebooks/pdf/render/qt_hack.h | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/pdf/render/qt_hack.cpp b/src/calibre/ebooks/pdf/render/qt_hack.cpp index d3a2a7da72..81f54d7591 100644 --- a/src/calibre/ebooks/pdf/render/qt_hack.cpp +++ b/src/calibre/ebooks/pdf/render/qt_hack.cpp @@ -48,7 +48,11 @@ PyObject* get_glyphs(const QPointF &p, const QTextItem &text_item) { indices = PyTuple_New(glyphs.count()); if (indices == NULL) { Py_DECREF(points); return PyErr_NoMemory(); } for (int i = 0; i < glyphs.count(); i++) { +#if PY_MAJOR_VERSION >= 3 + temp = PyLong_FromLong((long)glyphs[i]); +#else temp = PyInt_FromLong((long)glyphs[i]); +#endif if (temp == NULL) { Py_DECREF(indices); Py_DECREF(points); return PyErr_NoMemory(); } PyTuple_SET_ITEM(indices, i, temp); temp = NULL; } @@ -74,10 +78,13 @@ PyObject* get_glyph_map(const QTextItem &text_item) { for (uint uc = 0; uc < 0x10000; ++uc) { QChar ch(uc); ti.fontEngine->stringToCMap(&ch, 1, &glyphs, &nglyphs, QFontEngine::GlyphIndicesOnly); +#if PY_MAJOR_VERSION >= 3 + t = PyLong_FromLong(glyphs.glyphs[0]); +#else t = PyInt_FromLong(glyphs.glyphs[0]); +#endif if (t == NULL) { Py_DECREF(ans); return PyErr_NoMemory(); } PyTuple_SET_ITEM(ans, uc, t); t = NULL; } return ans; } - diff --git a/src/calibre/ebooks/pdf/render/qt_hack.h b/src/calibre/ebooks/pdf/render/qt_hack.h index 43a638bfba..c7c06614fc 100644 --- a/src/calibre/ebooks/pdf/render/qt_hack.h +++ b/src/calibre/ebooks/pdf/render/qt_hack.h @@ -7,14 +7,14 @@ #pragma once +// Per python C-API docs, Python.h must always be the first header +#include #include #include #include -#include PyObject* get_glyphs(const QPointF &p, const QTextItem &text_item); PyObject* get_sfnt_table(const QTextItem &text_item, const char* tag_name); PyObject* get_glyph_map(const QTextItem &text_item); -