1 /* Fast refactoring of ghosd-0.0.1/examples/text.c.
6 #include <cairo/cairo.h>
7 #include <pango/pangocairo.h>
13 #include <ghosd/ghosd.h>
14 #include <ghosd/ghosd-text.h>
16 struct _ghosd_render_info
{
18 float fgr
, fgg
, fgb
, fga
,
23 render(Ghosd
*ghosd
, cairo_t
*cr
, void* data
) {
25 struct _ghosd_render_info
* ri
= data
;
28 cairo_set_source_rgba(cr
, ri
->bgr
, ri
->bgg
, ri
->bgb
, ri
->bga
);
29 cairo_move_to(cr
, 2, 2);
30 pango_cairo_show_layout(cr
, ri
->layout
);
32 /* and the actual text. */
33 cairo_set_source_rgba(cr
, ri
->fgr
, ri
->fgg
, ri
->fgb
, ri
->fga
);
34 cairo_move_to(cr
, 0, 0);
35 pango_cairo_show_layout(cr
, ri
->layout
);
39 ghosd_flash_text(PyObject
* self
, PyObject
* args
, PyObject
* kw
)
41 static char * kwlist
[] = {"text", "x", "y",
42 "fade_time", "display_time",
43 "fg_color", "bg_color", NULL
};
45 PangoContext
* context
;
46 struct _ghosd_render_info ri
;
47 int x
= -50, y
= -50, fade
= 300, dtime
= 2000;
49 PyObject
* fg_color
= NULL
, * bg_color
= NULL
;
51 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "s|iiiiOO",
54 &x
, &y
, &fade
, &dtime
, &fg_color
, &bg_color
))
57 #define cbcheck(nam, c) ((ri.nam ## c >= 0) && (ri.nam ## c <= 1))
58 #define colcheck(nam) \
59 if (nam ## _color) { \
60 if (!PyTuple_Check(nam ## _color)) { \
61 PyErr_SetString(PyExc_TypeError, #nam "_color should be a tuple"); \
64 if (!PyArg_ParseTuple(nam ## _color, "ffff", \
65 & ri.nam ## r, & ri.nam## g, \
66 & ri.nam ## b, & ri.nam ## a)) { \
67 PyErr_SetString(PyExc_TypeError, \
68 #nam "_color should be a 4-tuple of floats"); \
71 if (!cbcheck(nam, r) || !cbcheck(nam, g) || \
72 !cbcheck(nam, b) || !cbcheck(nam, a)) { \
73 PyErr_SetString(PyExc_ValueError, \
74 "all components in " #nam \
75 "_color should be between 0 and 1"); \
80 ri
.fgr
= ri
.fgg
= ri
.fgb
= ri
.fga
= 1;
81 ri
.bgr
= ri
.bgg
= ri
.bgb
= 0; ri
.bga
= .5;
89 /* Finally, we are there: let's use this ghosd lib!
91 if (!(context
= pango_cairo_font_map_create_context(
92 PANGO_CAIRO_FONT_MAP(pango_cairo_font_map_get_default())))) {
93 PyErr_SetString(PyExc_RuntimeError
,
94 "could not create pango context");
98 if (!(ri
.layout
= pango_layout_new(context
))) {
99 PyErr_SetString(PyExc_RuntimeError
,
100 "could not create pango layout");
101 g_object_unref(context
);
105 pango_layout_set_markup(ri
.layout
, text
, -1);
107 if (!(ghosd
= ghosd_new())) {
108 PyErr_SetString(PyExc_RuntimeError
,
109 "could not create ghosd object");
110 g_object_unref(ri
.layout
);
111 g_object_unref(context
);
115 ghosd_text_set_position(ghosd
, x
, y
, ri
.layout
);
116 ghosd_set_render(ghosd
, render
, &ri
);
118 ghosd_flash(ghosd
, fade
, dtime
);
120 /* Free the various objects */
121 free(ghosd
); /* See ghosd-0.0.1/ghosd/ghosd.c:
122 no API for now, but that's just
123 a calloc'ed struct: no API to close
124 the display unfortunately. */
125 g_object_unref(ri
.layout
);
126 g_object_unref(context
);
133 static PyMethodDef ghosdMethods
[] = {
134 { "flash_text", (PyCFunction
)ghosd_flash_text
, METH_VARARGS
| METH_KEYWORDS
,
135 "flash_text(text, x=-50, y=-50, fade_time=300, display_time=2000, \n\
136 fg_color=(1, 1, 1, 1), bg_color=(0, 0, 0, .5)) => None\n\
138 Flash a given text message onto the screen.\n\
140 - text text to be displayed, in Pango markup (see below)\n\
141 - x, y position onto the screen\n\
142 - fade_time time, in milliseconds, for fading the text in and out\n\
143 - display_time time the text is to be displayed\n\
144 - fg_color RGBA default text color\n\
145 - bg_color RGBA text shadow color\n\
147 A concise description of the Pango Markup Language can be found\n\
148 in PyGTK documentation:\n\
150 http://www.pygtk.org/docs/pygtk/pango-markup-language.html\n\
160 Py_InitModule("_ghosd", ghosdMethods
);