Squashed commit of the following: [standardize_font_handling]
closes #60, closes #5, closes #68 The major functionality added here was proper use of types in the module, by importing after finalization. commit5009fa0fb9Author: John McCardle <mccardle.john@gmail.com> Date: Sun Apr 7 22:44:15 2024 -0400 PyFont - use the new standard method for instancing commita19781b56aAuthor: John McCardle <mccardle.john@gmail.com> Date: Sun Apr 7 15:21:17 2024 -0400 Many hours of pain & research behind this small commit. Safe object building by not messing with types before interpreter is fully initialized commit159658521cAuthor: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 31 21:41:45 2024 -0400 Font mostly working, just a few weird bugs with the types of the default items added to the module
This commit is contained in:
parent
fbf263a038
commit
1a7186f745
8 changed files with 207 additions and 11 deletions
63
src/PyFont.cpp
Normal file
63
src/PyFont.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include "PyFont.h"
|
||||
#include "McRFPy_API.h"
|
||||
|
||||
|
||||
PyFont::PyFont(std::string filename)
|
||||
: source(filename)
|
||||
{
|
||||
font = sf::Font();
|
||||
font.loadFromFile(source);
|
||||
}
|
||||
|
||||
PyObject* PyFont::pyObject()
|
||||
{
|
||||
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();
|
||||
}
|
||||
catch (std::bad_weak_ptr& e)
|
||||
{
|
||||
std::cout << "Bad weak ptr: shared_from_this() failed in PyFont::pyObject(); did you create a PyFont outside of std::make_shared? enjoy your segfault, soon!" << std::endl;
|
||||
}
|
||||
// TODO - shared_from_this will raise an exception if the object does not have a shared pointer. Constructor should be made private; write a factory function
|
||||
return obj;
|
||||
}
|
||||
|
||||
PyObject* PyFont::repr(PyObject* obj)
|
||||
{
|
||||
PyFontObject* self = (PyFontObject*)obj;
|
||||
std::ostringstream ss;
|
||||
if (!self->data)
|
||||
{
|
||||
ss << "<Font [invalid internal object]>";
|
||||
std::string repr_str = ss.str();
|
||||
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
|
||||
}
|
||||
auto& pfont = *(self->data);
|
||||
ss << "<Font (family=" << pfont.font.getInfo().family << ") source=`" << pfont.source << "`>";
|
||||
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;
|
||||
return reinterpret_cast<Py_hash_t>(self->data.get());
|
||||
}
|
||||
|
||||
int PyFont::init(PyFontObject* self, PyObject* args, PyObject* kwds)
|
||||
{
|
||||
static const char* keywords[] = { "filename", nullptr };
|
||||
char* filename;
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", const_cast<char**>(keywords), &filename))
|
||||
return -1;
|
||||
self->data = std::make_shared<PyFont>(filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* PyFont::pynew(PyTypeObject* type, PyObject* args, PyObject* kwds)
|
||||
{
|
||||
return (PyObject*)type->tp_alloc(type, 0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue