Anonymous avatar Anonymous committed 78ffd30

Added Generic Font class to Base module (pgreloaded).

Comments (0)

Files changed (6)

     </attr>
   </class>
 
+
+  <class name="Font">
+    <constructor>Font () -> Font</constructor>
+    <desc>
+      An abstract base class for arbitrary :class:`Font` objects.
+    
+      You should not instantiate this class directly.
+    </desc>
+    <method name="render">
+      <call>render (**kwds) -> object</call>
+      <desc>
+          Renders the specified text using the Font object. The exact input
+          arguments and return value is dependand on each specific Font
+          implementation.
+      </desc>
+    </method>
+    <method name="copy">
+      <call>copy () -> Font</call>
+      <desc>Creates a copy of this :class:`Font`.</desc>
+    </method>
+    <attr name="height">
+      <desc>Gets the standard height of the :class:`Font` typography.</desc>
+    </attr>
+    <attr name="size">
+      <desc>Gets the width and height of the :class:`Font` typography.</desc>
+    </attr>
+    <attr name="name">
+      <desc>Gets the name of the loaded :class:`Font`.</desc>
+    </attr>
+    <attr name="style">
+      <desc>Gets or sets the style used to render the :class:`Font`.</desc>
+    </attr>
+  </class>
+
 </module>
 
             "src/base/color.c",
             "src/base/floatrect.c",
             "src/base/rect.c",
-            "src/base/surface.c" ],
+            "src/base/surface.c",
+            "src/base/font.c"],
 
         instheaders = [
             "src/pgcompat.h",

src/base/basemod.c

     floatrect_export_capi (c_api);
     bufferproxy_export_capi (c_api);
     surface_export_capi (c_api);
+    font_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
+/*
+  pygame - Python Game Library
+  Copyright (C) 2000-2001 Pete Shinners, 2008 Marcus von Appen
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Library General Public
+  License as published by the Free Software Foundation; either
+  version 2 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Library General Public License for more details.
+
+  You should have received a copy of the GNU Library General Public
+  License along with this library; if not, write to the Free
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+*/
+
+#define PYGAME_FONT_INTERNAL
+
+#include "internals.h"
+#include "pgbase.h"
+#include "base_doc.h"
+
+static PyObject* _font_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+static int _font_init(PyObject *cursor, PyObject *args, PyObject *kwds);
+static void _font_dealloc(PyGenericFont *self);
+static PyObject* _font_repr(PyObject *self);
+
+
+/*
+ * Get/set attributes
+ */
+static PyObject* _font_getheight(PyObject *self, void *closure);
+static PyObject* _font_getsize(PyObject *self, void *closure);
+static PyObject* _font_getname(PyObject *self, void *closure);
+static PyObject* _font_getstyle(PyObject *self, void *closure);
+static int       _font_setstyle(PyObject *self, PyObject *arg, void *closure);
+
+/*
+ * Class methods
+ */
+static PyObject* _font_render(PyObject* self, PyObject *args, PyObject *kwds);
+static PyObject* _font_copy(PyObject* self);
+
+/*
+ * Get/set attributes (default fallbacks)
+ */
+static PyObject* _def_f_getheight(PyObject *self, void *closure);
+static PyObject* _def_f_getsize(PyObject *self, void *closure);
+static PyObject* _def_f_getname(PyObject *self, void *closure);
+static PyObject* _def_f_getstyle(PyObject *self, void *closure);
+static int       _def_f_setstyle(PyObject *self, PyObject *arg, void *closure);
+
+/*
+ * Class methods (default fallbacks)
+ */
+static PyObject* _def_f_render (PyObject *self, PyObject *args, PyObject *kwds); 
+static PyObject* _def_f_copy (PyObject *self); 
+
+
+/*
+ * METHODS TABLE
+ */
+static PyMethodDef _font_methods[] = 
+{
+    {
+        "render", 
+        (PyCFunction) _font_render, 
+        METH_VARARGS | METH_KEYWORDS,
+        DOC_BASE_FONT_RENDER 
+    },
+    {
+        "copy", 
+        (PyCFunction) _font_copy, 
+        METH_NOARGS, 
+        DOC_BASE_FONT_COPY
+    },
+    { NULL, NULL, 0, NULL }
+};
+
+/*
+ * GET/SETS TABLE
+ */
+static PyGetSetDef _font_getsets[] = {
+    { "height", _font_getheight,    NULL, DOC_BASE_FONT_HEIGHT, NULL },
+    { "size",   _font_getsize,      NULL, DOC_BASE_FONT_SIZE,   NULL },
+    { "name",   _font_getname,      NULL, DOC_BASE_FONT_NAME,   NULL },
+    { "style",  _font_getstyle,     _font_setstyle, DOC_BASE_FONT_STYLE, NULL },
+    { NULL, NULL, NULL, NULL, NULL }
+};
+
+
+/*
+ * TYPE OBJECT TABLE
+ */
+PyTypeObject PyGenericFont_Type =
+{
+    TYPE_HEAD(NULL, 0)
+    "base.Font",                /* tp_name */
+    sizeof (PyGenericFont),            /* tp_basicsize */
+    0,                          /* tp_itemsize */
+    (destructor) _font_dealloc, /* tp_dealloc */
+    0,                          /* tp_print */
+    0,                          /* tp_getattr */
+    0,                          /* tp_setattr */
+    0,                          /* tp_compare */
+    (reprfunc)_font_repr,       /* tp_repr */
+    0,                          /* tp_as_number */
+    0,                          /* tp_as_sequence */
+    0,                          /* tp_as_mapping */
+    0,                          /* tp_hash */
+    0,                          /* tp_call */
+    0,                          /* tp_str */
+    0,                          /* tp_getattro */
+    0,                          /* tp_setattro */
+    0,                          /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+    DOC_BASE_FONT,              /* docstring */
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    _font_methods,              /* tp_methods */
+    0,                          /* tp_members */
+    _font_getsets,              /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    (initproc) _font_init,      /* tp_init */
+    0,                          /* tp_alloc */
+    _font_new,                  /* tp_new */
+    0,                          /* tp_free */
+    0,                          /* tp_is_gc */
+    0,                          /* tp_bases */
+    0,                          /* tp_mro */
+    0,                          /* tp_cache */
+    0,                          /* tp_subclasses */
+    0,                          /* tp_weaklist */
+    0,                          /* tp_del */
+#if PY_VERSION_HEX >= 0x02060000
+    0                           /* tp_version_tag */
+#endif
+};
+
+static PyObject*
+_font_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+    PyGenericFont *font = (PyGenericFont *)type->tp_alloc(type, 0);
+
+    if (!font)
+        return NULL;
+
+    font->get_height =  _def_f_getheight;
+    font->get_size =    _def_f_getsize;
+    font->render =      _def_f_render;
+    font->copy =        _def_f_copy;
+
+    return (PyObject*) font;
+}
+
+static void
+_font_dealloc(PyGenericFont *self)
+{
+    ((PyObject*)self)->ob_type->tp_free((PyObject *)self);
+}
+
+static int
+_font_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    return 0;
+}
+
+static PyObject*
+_font_repr(PyObject *self)
+{
+    return Text_FromUTF8("Generic Font");
+}
+
+/* Font getters/setters */
+static PyObject*
+_font_getheight(PyObject *self, void *closure)
+{
+    if (((PyGenericFont *)self)->get_height &&
+        ((PyGenericFont *)self)->get_height != _def_f_getheight)
+        return ((PyGenericFont *)self)->get_height(self, closure);
+
+    PyErr_SetString(PyExc_NotImplementedError,
+        "height attribute not implemented");
+
+    return NULL;
+}
+
+static PyObject*
+_font_getsize(PyObject *self, void *closure)
+{
+    if (((PyGenericFont *)self)->get_size &&
+        ((PyGenericFont *)self)->get_size != _def_f_getsize)
+        return ((PyGenericFont *)self)->get_size (self, closure);
+
+    PyErr_SetString (PyExc_NotImplementedError,
+        "size attribute not implemented");
+
+    return NULL;
+}
+
+static PyObject*
+_font_getname(PyObject *self, void *closure)
+{
+    if (((PyGenericFont *)self)->get_name &&
+        ((PyGenericFont *)self)->get_name != _def_f_getname)
+        return ((PyGenericFont *)self)->get_name(self, closure);
+
+    PyErr_SetString (PyExc_NotImplementedError,
+        "name attribute not implemented");
+
+    return NULL;
+}
+
+static PyObject*
+_font_getstyle(PyObject *self, void *closure)
+{
+    if (((PyGenericFont *)self)->get_style &&
+        ((PyGenericFont *)self)->get_style != _def_f_getstyle)
+        return ((PyGenericFont *)self)->get_style(self, closure);
+
+    PyErr_SetString (PyExc_NotImplementedError,
+        "style attribute not implemented");
+
+    return NULL;
+}
+
+static int
+_font_setstyle(PyObject *self, PyObject *args, void *closure)
+{
+    if (((PyGenericFont *)self)->set_style &&
+        ((PyGenericFont *)self)->set_style != _def_f_setstyle)
+        return ((PyGenericFont *)self)->set_style(self, args, closure);
+
+    PyErr_SetString (PyExc_NotImplementedError,
+        "style attribute not implemented");
+
+    return -1;
+}
+
+
+
+/* Font methods */
+static PyObject*
+_font_render(PyObject* self, PyObject *args, PyObject *kwds)
+{
+    if (((PyGenericFont *)self)->render && ((PyGenericFont *)self)->render != _def_f_render)
+        return ((PyGenericFont *)self)->render(self, args, kwds);
+
+    PyErr_SetString(PyExc_NotImplementedError, "render method not implemented");
+
+    return NULL;
+}
+
+static PyObject*
+_font_copy(PyObject* self)
+{
+    if (((PyGenericFont *)self)->copy && ((PyGenericFont *)self)->copy != _def_f_copy)
+        return ((PyGenericFont *)self)->copy (self);
+
+    PyErr_SetString(PyExc_NotImplementedError, "copy method not implemented");
+
+    return NULL;
+}
+
+
+static PyObject*
+_def_f_getheight(PyObject *self, void *closure)
+{
+    return PyObject_GetAttrString(self, "height");
+}
+
+static PyObject*
+_def_f_getsize (PyObject *self, void *closure)
+{
+    return PyObject_GetAttrString(self, "size");
+}
+
+static PyObject*
+_def_f_getname(PyObject *self, void *closure)
+{
+    return PyObject_GetAttrString(self, "name");
+}
+
+static PyObject*
+_def_f_getstyle(PyObject *self, void *closure)
+{
+    return PyObject_GetAttrString(self, "style");
+}
+
+static int
+_def_f_setstyle(PyObject *self, PyObject *arg, void *closure)
+{
+    return PyObject_SetAttrString(self, "style", arg);
+}
+
+static PyObject*
+_def_f_render(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *retval, *method;
+    
+    method = PyObject_GetAttrString(self, "render");
+
+    if (!method)
+        return NULL;
+
+    retval = PyObject_Call(method, args, kwds);
+    Py_DECREF (method);
+    return retval;
+}
+
+static PyObject*
+_def_f_copy (PyObject *self)
+{
+    return PyObject_CallMethod(self, "copy", NULL, NULL);
+}
+
+/* C API */
+PyObject*
+PyGenericFont_New(void)
+{
+    return PyGenericFont_Type.tp_new(&PyGenericFont_Type, NULL, NULL);
+}
+
+void
+font_export_capi(void **capi)
+{
+    capi[PYGAME_FONT_FIRSTSLOT + 0] = &PyGenericFont_Type;
+    capi[PYGAME_FONT_FIRSTSLOT + 1] = PyGenericFont_New;
+}

src/base/pgbase.h

     (*(PyObject*(*)(void))PyGameBase_C_API[PYGAME_SURFACE_FIRSTSLOT+1])
 #endif /* PYGAME_SURFACE_INTERNAL */
 
+typedef struct
+{
+    PyObject_HEAD
+
+    PyObject* (*get_height)(PyObject *self, void *closure);
+    PyObject* (*get_size)(PyObject *self, void *closure);
+    PyObject* (*get_name)(PyObject *self, void *closure);
+    PyObject* (*get_style)(PyObject *self, void *closure);
+    int       (*set_style)(PyObject *self, PyObject *attr, void *closure);
+
+    PyObject* (*render)(PyObject *self, PyObject *args, PyObject *kwds);
+    PyObject* (*copy)(PyObject *self);
+} PyGenericFont;
+
+#define PYGAME_FONT_FIRSTSLOT   \
+    (PYGAME_SURFACE_FIRSTSLOT + PYGAME_SURFACE_NUMSLOTS)
+#define PYGAME_FONT_NUMSLOTS 2
+
+#ifndef PYGAME_FONT_INTERNAL
+#define PyGenericFont_Type \
+    (*(PyTypeObject*)PyGameBase_C_API[PYGAME_FONT_FIRSTSLOT+0])
+#define PyGenericFont_Check(x) \
+    (PyObject_TypeCheck(x, \
+        (PyTypeObject*)PyGameBase_C_API[PYGAME_FONT_FIRSTSLOT+0]))
+#define PyGenericFont_New \
+    (*(PyObject*(*)(void))PyGameBase_C_API[PYGAME_FONT_FIRSTSLOT+1])
+#endif /* PYGAME_FONT_INTERNAL */
+
+
 /**
  * C API export.
  */
 #endif
 
 #define PYGAME_BASE_SLOTS \
-    (PYGAME_SURFACE_FIRSTSLOT + PYGAME_SURFACE_NUMSLOTS)
+    (PYGAME_FONT_FIRSTSLOT + PYGAME_FONT_NUMSLOTS)
 #define PYGAME_BASE_ENTRY "_PYGAME_BASE_CAPI"
 
 static int

src/freetype/ft_mod.c

  *
  * Bindings for initialization/cleanup functions
  *
- * FIXME: Do we really need to export bindings for init() and
- * quit() functions? The init function can be automatically
- * called when importing the module, and the quit function is
- * automatically called when unloading the module. Will the user
- * ever need to manually unload the FT library?
+ * Explicit init/quit functions are required to work around
+ * some issues regarding module caching and multi-threaded apps.
+ * It's always good to let the user choose when to initialize
+ * the module.
+ *
+ * TODO: These bindings can be removed once proper threading
+ * support is in place.
  *
  ***************************************************************/
 static PyObject *
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.