2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Campbell Barton
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/python/generic/blf_py_api.c
26 * This file defines the 'bgl' module, used for drawing text in OpenGL.
30 #include "blf_py_api.h"
32 #include "../../blenfont/BLF_api.h"
34 #include "BLI_utildefines.h"
36 PyDoc_STRVAR(py_blf_position_doc,
37 ".. function:: position(fontid, x, y, z)\n"
39 " Set the position for drawing text.\n"
41 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
42 " :type fontid: int\n"
43 " :arg x: X axis position to draw the text.\n"
45 " :arg y: Y axis position to draw the text.\n"
47 " :arg z: Z axis position to draw the text.\n"
51 static PyObject *py_blf_position(PyObject *UNUSED(self), PyObject *args)
56 if (!PyArg_ParseTuple(args, "ifff:blf.position", &fontid, &x, &y, &z))
59 BLF_position(fontid, x, y, z);
65 PyDoc_STRVAR(py_blf_size_doc,
66 ".. function:: size(fontid, size, dpi)\n"
68 " Set the size and dpi for drawing text.\n"
70 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
71 " :type fontid: int\n"
72 " :arg size: Point size of the font.\n"
74 " :arg dpi: dots per inch value to use for drawing.\n"
77 static PyObject *py_blf_size(PyObject *UNUSED(self), PyObject *args)
79 int fontid, size, dpi;
81 if (!PyArg_ParseTuple(args, "iii:blf.size", &fontid, &size, &dpi))
84 BLF_size(fontid, size, dpi);
90 PyDoc_STRVAR(py_blf_aspect_doc,
91 ".. function:: aspect(fontid, aspect)\n"
93 " Set the aspect for drawing text.\n"
95 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
96 " :type fontid: int\n"
97 " :arg aspect: The aspect ratio for text drawing to use.\n"
98 " :type aspect: float\n"
100 static PyObject *py_blf_aspect(PyObject *UNUSED(self), PyObject *args)
105 if (!PyArg_ParseTuple(args, "if:blf.aspect", &fontid, &aspect))
108 BLF_aspect(fontid, aspect, aspect, 1.0);
114 PyDoc_STRVAR(py_blf_blur_doc,
115 ".. function:: blur(fontid, radius)\n"
117 " Set the blur radius for drawing text.\n"
119 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
120 " :type fontid: int\n"
121 " :arg radius: The radius for blurring text (in pixels).\n"
122 " :type radius: int\n"
124 static PyObject *py_blf_blur(PyObject *UNUSED(self), PyObject *args)
128 if (!PyArg_ParseTuple(args, "ii:blf.blur", &fontid, &blur))
131 BLF_blur(fontid, blur);
137 PyDoc_STRVAR(py_blf_draw_doc,
138 ".. function:: draw(fontid, text)\n"
140 " Draw text in the current context.\n"
142 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
143 " :type fontid: int\n"
144 " :arg text: the text to draw.\n"
145 " :type text: string\n"
147 static PyObject *py_blf_draw(PyObject *UNUSED(self), PyObject *args)
153 if (!PyArg_ParseTuple(args, "is#:blf.draw", &fontid, &text, &text_length))
156 BLF_draw(fontid, text, (unsigned int)text_length);
161 PyDoc_STRVAR(py_blf_dimensions_doc,
162 ".. function:: dimensions(fontid, text)\n"
164 " Return the width and height of the text.\n"
166 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
167 " :type fontid: int\n"
168 " :arg text: the text to draw.\n"
169 " :type text: string\n"
170 " :return: the width and height of the text.\n"
171 " :rtype: tuple of 2 floats\n"
173 static PyObject *py_blf_dimensions(PyObject *UNUSED(self), PyObject *args)
176 float r_width, r_height;
180 if (!PyArg_ParseTuple(args, "is:blf.dimensions", &fontid, &text))
183 BLF_width_and_height(fontid, text, INT_MAX, &r_width, &r_height);
185 ret = PyTuple_New(2);
186 PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(r_width));
187 PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(r_height));
191 PyDoc_STRVAR(py_blf_clipping_doc,
192 ".. function:: clipping(fontid, xmin, ymin, xmax, ymax)\n"
194 " Set the clipping, enable/disable using CLIPPING.\n"
196 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
197 " :type fontid: int\n"
198 " :arg xmin: Clip the drawing area by these bounds.\n"
199 " :type xmin: float\n"
200 " :arg ymin: Clip the drawing area by these bounds.\n"
201 " :type ymin: float\n"
202 " :arg xmax: Clip the drawing area by these bounds.\n"
203 " :type xmax: float\n"
204 " :arg ymax: Clip the drawing area by these bounds.\n"
205 " :type ymax: float\n"
207 static PyObject *py_blf_clipping(PyObject *UNUSED(self), PyObject *args)
209 float xmin, ymin, xmax, ymax;
212 if (!PyArg_ParseTuple(args, "iffff:blf.clipping", &fontid, &xmin, &ymin, &xmax, &ymax))
215 BLF_clipping(fontid, xmin, ymin, xmax, ymax);
220 PyDoc_STRVAR(py_blf_disable_doc,
221 ".. function:: disable(fontid, option)\n"
225 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
226 " :type fontid: int\n"
227 " :arg option: One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.\n"
228 " :type option: int\n"
230 static PyObject *py_blf_disable(PyObject *UNUSED(self), PyObject *args)
234 if (!PyArg_ParseTuple(args, "ii:blf.disable", &fontid, &option))
237 BLF_disable(fontid, option);
242 PyDoc_STRVAR(py_blf_enable_doc,
243 ".. function:: enable(fontid, option)\n"
247 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
248 " :type fontid: int\n"
249 " :arg option: One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.\n"
250 " :type option: int\n"
252 static PyObject *py_blf_enable(PyObject *UNUSED(self), PyObject *args)
256 if (!PyArg_ParseTuple(args, "ii:blf.enable", &fontid, &option))
259 BLF_enable(fontid, option);
264 PyDoc_STRVAR(py_blf_rotation_doc,
265 ".. function:: rotation(fontid, angle)\n"
267 " Set the text rotation angle, enable/disable using ROTATION.\n"
269 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
270 " :type fontid: int\n"
271 " :arg angle: The angle for text drawing to use.\n"
272 " :type angle: float\n"
274 static PyObject *py_blf_rotation(PyObject *UNUSED(self), PyObject *args)
279 if (!PyArg_ParseTuple(args, "if:blf.rotation", &fontid, &angle))
282 BLF_rotation(fontid, angle);
287 PyDoc_STRVAR(py_blf_shadow_doc,
288 ".. function:: shadow(fontid, level, r, g, b, a)\n"
290 " Shadow options, enable/disable using SHADOW .\n"
292 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
293 " :type fontid: int\n"
294 " :arg level: The blur level, can be 3, 5 or 0.\n"
295 " :type level: int\n"
296 " :arg r: Shadow color (red channel 0.0 - 1.0).\n"
298 " :arg g: Shadow color (green channel 0.0 - 1.0).\n"
300 " :arg b: Shadow color (blue channel 0.0 - 1.0).\n"
302 " :arg a: Shadow color (alpha channel 0.0 - 1.0).\n"
305 static PyObject *py_blf_shadow(PyObject *UNUSED(self), PyObject *args)
310 if (!PyArg_ParseTuple(args, "iiffff:blf.shadow", &fontid, &level, &r, &g, &b, &a))
313 if (level != 0 && level != 3 && level != 5) {
314 PyErr_SetString(PyExc_TypeError, "blf.shadow expected arg to be in (0, 3, 5)");
318 BLF_shadow(fontid, level, r, g, b, a);
323 PyDoc_STRVAR(py_blf_shadow_offset_doc,
324 ".. function:: shadow_offset(fontid, x, y)\n"
326 " Set the offset for shadow text.\n"
328 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
329 " :type fontid: int\n"
330 " :arg x: Vertical shadow offset value in pixels.\n"
332 " :arg y: Horizontal shadow offset value in pixels.\n"
335 static PyObject *py_blf_shadow_offset(PyObject *UNUSED(self), PyObject *args)
339 if (!PyArg_ParseTuple(args, "iii:blf.shadow_offset", &fontid, &x, &y))
342 BLF_shadow_offset(fontid, x, y);
347 PyDoc_STRVAR(py_blf_load_doc,
348 ".. function:: load(filename)\n"
350 " Load a new font.\n"
352 " :arg filename: the filename of the font.\n"
353 " :type filename: string\n"
354 " :return: the new font's fontid or -1 if there was an error.\n"
357 static PyObject *py_blf_load(PyObject *UNUSED(self), PyObject *args)
359 const char *filename;
361 if (!PyArg_ParseTuple(args, "s:blf.load", &filename))
364 return PyLong_FromLong(BLF_load(filename));
367 PyDoc_STRVAR(py_blf_unload_doc,
368 ".. function:: unload(filename)\n"
370 " Unload an existing font.\n"
372 " :arg filename: the filename of the font.\n"
373 " :type filename: string\n"
375 static PyObject *py_blf_unload(PyObject *UNUSED(self), PyObject *args)
377 const char *filename;
379 if (!PyArg_ParseTuple(args, "s:blf.unload", &filename))
382 BLF_unload(filename);
387 /*----------------------------MODULE INIT-------------------------*/
388 static PyMethodDef BLF_methods[] = {
389 {"aspect", (PyCFunction) py_blf_aspect, METH_VARARGS, py_blf_aspect_doc},
390 {"blur", (PyCFunction) py_blf_blur, METH_VARARGS, py_blf_blur_doc},
391 {"clipping", (PyCFunction) py_blf_clipping, METH_VARARGS, py_blf_clipping_doc},
392 {"disable", (PyCFunction) py_blf_disable, METH_VARARGS, py_blf_disable_doc},
393 {"dimensions", (PyCFunction) py_blf_dimensions, METH_VARARGS, py_blf_dimensions_doc},
394 {"draw", (PyCFunction) py_blf_draw, METH_VARARGS, py_blf_draw_doc},
395 {"enable", (PyCFunction) py_blf_enable, METH_VARARGS, py_blf_enable_doc},
396 {"position", (PyCFunction) py_blf_position, METH_VARARGS, py_blf_position_doc},
397 {"rotation", (PyCFunction) py_blf_rotation, METH_VARARGS, py_blf_rotation_doc},
398 {"shadow", (PyCFunction) py_blf_shadow, METH_VARARGS, py_blf_shadow_doc},
399 {"shadow_offset", (PyCFunction) py_blf_shadow_offset, METH_VARARGS, py_blf_shadow_offset_doc},
400 {"size", (PyCFunction) py_blf_size, METH_VARARGS, py_blf_size_doc},
401 {"load", (PyCFunction) py_blf_load, METH_VARARGS, py_blf_load_doc},
402 {"unload", (PyCFunction) py_blf_unload, METH_VARARGS, py_blf_unload_doc},
403 {NULL, NULL, 0, NULL}
406 PyDoc_STRVAR(BLF_doc,
407 "This module provides access to blenders text drawing functions."
409 static struct PyModuleDef BLF_module_def = {
410 PyModuleDef_HEAD_INIT,
414 BLF_methods, /* m_methods */
416 NULL, /* m_traverse */
421 PyObject *BPyInit_blf(void)
425 submodule = PyModule_Create(&BLF_module_def);
427 PyModule_AddIntConstant(submodule, "ROTATION", BLF_ROTATION);
428 PyModule_AddIntConstant(submodule, "CLIPPING", BLF_CLIPPING);
429 PyModule_AddIntConstant(submodule, "SHADOW", BLF_SHADOW);
430 PyModule_AddIntConstant(submodule, "KERNING_DEFAULT", BLF_KERNING_DEFAULT);