diff --git a/src/PyFont.cpp b/src/PyFont.cpp index f7e04f7..7773d52 100644 --- a/src/PyFont.cpp +++ b/src/PyFont.cpp @@ -1,4 +1,5 @@ #include "PyFont.h" +#include "McRFPy_API.h" PyFont::PyFont(std::string filename) @@ -10,7 +11,9 @@ PyFont::PyFont(std::string filename) PyObject* PyFont::pyObject() { - PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyFontType, 0); + auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Font"); + //PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyFontType, 0); + PyObject* obj = PyFont::pynew(type, Py_None, Py_None); try { ((PyFontObject*)obj)->data = shared_from_this(); } @@ -22,6 +25,22 @@ PyObject* PyFont::pyObject() return obj; } +PyObject* PyFont::repr(PyObject* obj) +{ + PyFontObject* self = (PyFontObject*)obj; + std::ostringstream ss; + if (!self->data) + { + ss << ""; + std::string repr_str = ss.str(); + return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace"); + } + auto& pfont = *(self->data); + ss << ""; + std::string repr_str = ss.str(); + return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace"); +} + Py_hash_t PyFont::hash(PyObject* obj) { auto self = (PyFontObject*)obj; diff --git a/src/PyFont.h b/src/PyFont.h index d3b99f0..99a12b9 100644 --- a/src/PyFont.h +++ b/src/PyFont.h @@ -17,6 +17,7 @@ public: PyFont(std::string filename); sf::Font font; PyObject* pyObject(); + static PyObject* repr(PyObject*); static Py_hash_t hash(PyObject*); static int init(PyFontObject*, PyObject*, PyObject*); static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL); @@ -27,10 +28,12 @@ namespace mcrfpydef { .tp_name = "mcrfpy.Font", .tp_basicsize = sizeof(PyFontObject), .tp_itemsize = 0, + .tp_repr = PyFont::repr, //.tp_hash = PyFont::hash, .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = PyDoc_STR("SFML Font Object"), + //.tp_base = &PyBaseObject_Type, .tp_init = (initproc)PyFont::init, - .tp_new = PyFont::pynew, + .tp_new = PyType_GenericNew, //PyFont::pynew, }; }