|
1 # Add Font.render_glyph() method to PyGame. |
|
2 # |
|
3 # This is nicer than full string rendering, as Tuikit renders screen |
|
4 # one character at the time and render_glyph is optimized for caching. |
|
5 # See documentation for TTF_RenderGlyph_Shaded and TTF_GlyphMetrics. |
|
6 # |
|
7 # Radek Brich <radek.brich@devl.cz> |
|
8 diff -rupd pygame-1.9.1release/src/font.c pygame-1.9.1release-my/src/font.c |
|
9 --- pygame-1.9.1release/src/font.c 2009-06-17 01:18:34.000000000 +0200 |
|
10 +++ pygame-1.9.1release-my/src/font.c 2011-10-09 23:19:46.000000000 +0200 |
|
11 @@ -371,6 +371,78 @@ font_render (PyObject* self, PyObject* a |
|
12 } |
|
13 |
|
14 static PyObject* |
|
15 +font_render_glyph (PyObject* self, PyObject* args) |
|
16 +{ |
|
17 + TTF_Font* font = PyFont_AsFont (self); |
|
18 + int aa; |
|
19 + PyObject* text, *final; |
|
20 + PyObject* fg_rgba_obj, *bg_rgba_obj = NULL; |
|
21 + Uint8 rgba[4]; |
|
22 + SDL_Surface* surf; |
|
23 + SDL_Color foreg, backg; |
|
24 + |
|
25 + if (!PyArg_ParseTuple (args, "OiO|O", &text, &aa, &fg_rgba_obj, |
|
26 + &bg_rgba_obj)) |
|
27 + return NULL; |
|
28 + |
|
29 + if (!RGBAFromColorObj (fg_rgba_obj, rgba)) |
|
30 + return RAISE (PyExc_TypeError, "Invalid foreground RGBA argument"); |
|
31 + foreg.r = rgba[0]; |
|
32 + foreg.g = rgba[1]; |
|
33 + foreg.b = rgba[2]; |
|
34 + if (bg_rgba_obj) |
|
35 + { |
|
36 + if (!RGBAFromColorObj (bg_rgba_obj, rgba)) |
|
37 + return RAISE (PyExc_TypeError, "Invalid background RGBA argument"); |
|
38 + backg.r = rgba[0]; |
|
39 + backg.g = rgba[1]; |
|
40 + backg.b = rgba[2]; |
|
41 + backg.unused = 0; |
|
42 + } |
|
43 + else |
|
44 + { |
|
45 + backg.r = 0; |
|
46 + backg.g = 0; |
|
47 + backg.b = 0; |
|
48 + backg.unused = 0; |
|
49 + } |
|
50 + |
|
51 + if (PyUnicode_Check (text) && PyObject_Length (text) == 1) |
|
52 + { |
|
53 + Py_UNICODE *buf = PyUnicode_AsUnicode (text); |
|
54 + Uint16 ch = (Uint16) buf[0]; |
|
55 + |
|
56 + if (aa) |
|
57 + { |
|
58 + if (!bg_rgba_obj) |
|
59 + surf = TTF_RenderGlyph_Blended (font, ch, foreg); |
|
60 + else |
|
61 + surf = TTF_RenderGlyph_Shaded (font, ch, foreg, backg); |
|
62 + } |
|
63 + else |
|
64 + surf = TTF_RenderGlyph_Solid (font, ch, foreg); |
|
65 + } |
|
66 + else |
|
67 + return RAISE (PyExc_TypeError, "char must be string of exactly one unicode character"); |
|
68 + |
|
69 + if (!surf) |
|
70 + return RAISE (PyExc_SDLError, TTF_GetError()); |
|
71 + |
|
72 + if (!aa && bg_rgba_obj) /*turn off transparancy*/ |
|
73 + { |
|
74 + SDL_SetColorKey (surf, 0, 0); |
|
75 + surf->format->palette->colors[0].r = backg.r; |
|
76 + surf->format->palette->colors[0].g = backg.g; |
|
77 + surf->format->palette->colors[0].b = backg.b; |
|
78 + } |
|
79 + |
|
80 + final = PySurface_New (surf); |
|
81 + if (!final) |
|
82 + SDL_FreeSurface (surf); |
|
83 + return final; |
|
84 +} |
|
85 + |
|
86 +static PyObject* |
|
87 font_size (PyObject* self, PyObject* args) |
|
88 { |
|
89 TTF_Font* font = PyFont_AsFont (self); |
|
90 @@ -509,6 +581,7 @@ static PyMethodDef font_methods[] = |
|
91 |
|
92 { "metrics", font_metrics, METH_VARARGS, DOC_FONTMETRICS }, |
|
93 { "render", font_render, METH_VARARGS, DOC_FONTRENDER }, |
|
94 + { "render_glyph", font_render_glyph, METH_VARARGS, DOC_FONTRENDERGLYPH }, |
|
95 { "size", font_size, METH_VARARGS, DOC_FONTSIZE }, |
|
96 |
|
97 { NULL, NULL, 0, NULL } |
|
98 diff -rupd pygame-1.9.1release/src/pygamedocs.h pygame-1.9.1release-my/src/pygamedocs.h |
|
99 --- pygame-1.9.1release/src/pygamedocs.h 2009-06-19 08:20:25.000000000 +0200 |
|
100 +++ pygame-1.9.1release-my/src/pygamedocs.h 2011-10-09 23:27:34.000000000 +0200 |
|
101 @@ -299,6 +299,8 @@ |
|
102 |
|
103 #define DOC_FONTRENDER "Font.render(text, antialias, color, background=None): return Surface\ndraw text on a new Surface" |
|
104 |
|
105 +#define DOC_FONTRENDERGLYPH "Font.render_glyph(char, antialias, color, background=None): return Surface\ndraw glyph of char on a new Surface" |
|
106 + |
|
107 #define DOC_FONTSIZE "Font.size(text): return (width, height)\ndetermine the amount of space needed to render text" |
|
108 |
|
109 #define DOC_FONTSETUNDERLINE "Font.set_underline(bool): return None\ncontrol if text is rendered with an underline" |