extra/pygame_font_render_glyph.patch
author Radek Brich <radek.brich@devl.cz>
Mon, 17 Dec 2012 23:27:37 +0100
changeset 39 5e5deb1d3945
parent 25 f69a1f0382ce
permissions -rw-r--r--
Fixes.

# Add Font.render_glyph() method to PyGame.
#
# This is nicer than full string rendering, as Tuikit renders screen
# one character at the time and render_glyph is optimized for caching.
# See documentation for TTF_RenderGlyph_Shaded and TTF_GlyphMetrics.
#
# Radek Brich <radek.brich@devl.cz>
diff -rupd pygame-1.9.1release/src/font.c pygame-1.9.1release-my/src/font.c
--- pygame-1.9.1release/src/font.c	2009-06-17 01:18:34.000000000 +0200
+++ pygame-1.9.1release-my/src/font.c	2011-10-09 23:19:46.000000000 +0200
@@ -371,6 +371,78 @@ font_render (PyObject* self, PyObject* a
 }
 
 static PyObject*
+font_render_glyph (PyObject* self, PyObject* args)
+{
+    TTF_Font* font = PyFont_AsFont (self);
+    int aa;
+    PyObject* text, *final;
+    PyObject* fg_rgba_obj, *bg_rgba_obj = NULL;
+    Uint8 rgba[4];
+    SDL_Surface* surf;
+    SDL_Color foreg, backg;
+
+    if (!PyArg_ParseTuple (args, "OiO|O", &text, &aa, &fg_rgba_obj,
+                           &bg_rgba_obj))
+        return NULL;
+
+    if (!RGBAFromColorObj (fg_rgba_obj, rgba))
+        return RAISE (PyExc_TypeError, "Invalid foreground RGBA argument");
+    foreg.r = rgba[0];
+    foreg.g = rgba[1];
+    foreg.b = rgba[2];
+    if (bg_rgba_obj)
+    {
+        if (!RGBAFromColorObj (bg_rgba_obj, rgba))
+            return RAISE (PyExc_TypeError, "Invalid background RGBA argument");
+        backg.r = rgba[0];
+        backg.g = rgba[1];
+        backg.b = rgba[2];
+        backg.unused = 0;
+    }
+    else
+    {
+        backg.r = 0;
+        backg.g = 0;
+        backg.b = 0;
+        backg.unused = 0;
+    }
+
+    if (PyUnicode_Check (text) && PyObject_Length (text) == 1)
+    {
+        Py_UNICODE *buf = PyUnicode_AsUnicode (text);
+        Uint16 ch = (Uint16) buf[0];
+
+        if (aa)
+        {
+            if (!bg_rgba_obj)
+                surf = TTF_RenderGlyph_Blended (font, ch, foreg);
+            else
+                surf = TTF_RenderGlyph_Shaded (font, ch, foreg, backg);
+        }
+        else
+            surf = TTF_RenderGlyph_Solid (font, ch, foreg);
+    }
+    else
+        return RAISE (PyExc_TypeError, "char must be string of exactly one unicode character");
+
+    if (!surf)
+        return RAISE (PyExc_SDLError, TTF_GetError());
+
+    if (!aa && bg_rgba_obj) /*turn off transparancy*/
+    {
+        SDL_SetColorKey (surf, 0, 0);
+        surf->format->palette->colors[0].r = backg.r;
+        surf->format->palette->colors[0].g = backg.g;
+        surf->format->palette->colors[0].b = backg.b;
+    }
+
+    final = PySurface_New (surf);
+    if (!final)
+        SDL_FreeSurface (surf);
+    return final;
+}
+
+static PyObject*
 font_size (PyObject* self, PyObject* args)
 {
     TTF_Font* font = PyFont_AsFont (self);
@@ -509,6 +581,7 @@ static PyMethodDef font_methods[] =
 
     { "metrics", font_metrics, METH_VARARGS, DOC_FONTMETRICS },
     { "render", font_render, METH_VARARGS, DOC_FONTRENDER },
+    { "render_glyph", font_render_glyph, METH_VARARGS, DOC_FONTRENDERGLYPH },
     { "size", font_size, METH_VARARGS, DOC_FONTSIZE },
 
     { NULL, NULL, 0, NULL }
diff -rupd pygame-1.9.1release/src/pygamedocs.h pygame-1.9.1release-my/src/pygamedocs.h
--- pygame-1.9.1release/src/pygamedocs.h	2009-06-19 08:20:25.000000000 +0200
+++ pygame-1.9.1release-my/src/pygamedocs.h	2011-10-09 23:27:34.000000000 +0200
@@ -299,6 +299,8 @@
 
 #define DOC_FONTRENDER "Font.render(text, antialias, color, background=None): return Surface\ndraw text on a new Surface"
 
+#define DOC_FONTRENDERGLYPH "Font.render_glyph(char, antialias, color, background=None): return Surface\ndraw glyph of char on a new Surface"
+
 #define DOC_FONTSIZE "Font.size(text): return (width, height)\ndetermine the amount of space needed to render text"
 
 #define DOC_FONTSETUNDERLINE "Font.set_underline(bool): return None\ncontrol if text is rendered with an underline"