4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): Campbell Barton
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/python/generic/blf_py_api.c
31 #include "blf_py_api.h"
33 #include "../../blenfont/BLF_api.h"
35 #include "BLI_utildefines.h"
39 static char py_blf_position_doc[] =
40 ".. function:: position(fontid, x, y, z)\n"
42 " Set the position for drawing text.\n"
44 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
45 " :type fontid: int\n"
46 " :arg x: X axis position to draw the text.\n"
48 " :arg y: Y axis position to draw the text.\n"
50 " :arg z: Z axis position to draw the text.\n"
53 static PyObject *py_blf_position(PyObject *UNUSED(self), PyObject *args)
58 if (!PyArg_ParseTuple(args, "ifff:blf.position", &fontid, &x, &y, &z))
61 BLF_position(fontid, x, y, z);
67 static char py_blf_size_doc[] =
68 ".. function:: size(fontid, size, dpi)\n"
70 " Set the size and dpi for drawing text.\n"
72 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
73 " :type fontid: int\n"
74 " :arg size: Point size of the font.\n"
76 " :arg dpi: dots per inch value to use for drawing.\n"
79 static PyObject *py_blf_size(PyObject *UNUSED(self), PyObject *args)
81 int fontid, size, dpi;
83 if (!PyArg_ParseTuple(args, "iii:blf.size", &fontid, &size, &dpi))
86 BLF_size(fontid, size, dpi);
92 static char py_blf_aspect_doc[] =
93 ".. function:: aspect(fontid, aspect)\n"
95 " Set the aspect for drawing text.\n"
97 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
98 " :type fontid: int\n"
99 " :arg aspect: The aspect ratio for text drawing to use.\n"
100 " :type aspect: float\n";
102 static PyObject *py_blf_aspect(PyObject *UNUSED(self), PyObject *args)
107 if (!PyArg_ParseTuple(args, "if:blf.aspect", &fontid, &aspect))
110 BLF_aspect(fontid, aspect, aspect, 1.0);
116 static char py_blf_blur_doc[] =
117 ".. function:: blur(fontid, radius)\n"
119 " Set the blur radius for drawing text.\n"
121 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
122 " :type fontid: int\n"
123 " :arg radius: The radius for blurring text (in pixels).\n"
124 " :type radius: int\n";
126 static PyObject *py_blf_blur(PyObject *UNUSED(self), PyObject *args)
130 if (!PyArg_ParseTuple(args, "ii:blf.blur", &fontid, &blur))
133 BLF_blur(fontid, blur);
139 static char py_blf_draw_doc[] =
140 ".. function:: draw(fontid, text)\n"
142 " Draw text in the current context.\n"
144 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
145 " :type fontid: int\n"
146 " :arg text: the text to draw.\n"
147 " :type text: string\n";
149 static PyObject *py_blf_draw(PyObject *UNUSED(self), PyObject *args)
155 if (!PyArg_ParseTuple(args, "is#:blf.draw", &fontid, &text, &text_length))
158 BLF_draw(fontid, text, (unsigned int)text_length);
163 static char py_blf_dimensions_doc[] =
164 ".. function:: dimensions(fontid, text)\n"
166 " Return the width and height of the text.\n"
168 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
169 " :type fontid: int\n"
170 " :arg text: the text to draw.\n"
171 " :type text: string\n"
172 " :return: the width and height of the text.\n"
173 " :rtype: tuple of 2 floats\n";
175 static PyObject *py_blf_dimensions(PyObject *UNUSED(self), PyObject *args)
178 float r_width, r_height;
182 if (!PyArg_ParseTuple(args, "is:blf.dimensions", &fontid, &text))
185 BLF_width_and_height(fontid, text, &r_width, &r_height);
188 PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(r_width));
189 PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(r_height));
193 static char py_blf_clipping_doc[] =
194 ".. function:: clipping(fontid, xmin, ymin, xmax, ymax)\n"
196 " Set the clipping, enable/disable using CLIPPING.\n"
198 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
199 " :type fontid: int\n"
200 " :arg xmin: Clip the drawing area by these bounds.\n"
201 " :type xmin: float\n"
202 " :arg ymin: Clip the drawing area by these bounds.\n"
203 " :type ymin: float\n"
204 " :arg xmax: Clip the drawing area by these bounds.\n"
205 " :type xmax: float\n"
206 " :arg ymax: Clip the drawing area by these bounds.\n"
207 " :type ymax: float\n";
209 static PyObject *py_blf_clipping(PyObject *UNUSED(self), PyObject *args)
211 float xmin, ymin, xmax, ymax;
214 if (!PyArg_ParseTuple(args, "iffff:blf.clipping", &fontid, &xmin, &ymin, &xmax, &ymax))
217 BLF_clipping(fontid, xmin, ymin, xmax, ymax);
222 static char py_blf_disable_doc[] =
223 ".. function:: disable(fontid, option)\n"
227 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
228 " :type fontid: int\n"
229 " :arg option: One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.\n"
230 " :type option: int\n";
232 static PyObject *py_blf_disable(PyObject *UNUSED(self), PyObject *args)
236 if (!PyArg_ParseTuple(args, "ii:blf.disable", &fontid, &option))
239 BLF_disable(fontid, option);
244 static char py_blf_enable_doc[] =
245 ".. function:: enable(fontid, option)\n"
249 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
250 " :type fontid: int\n"
251 " :arg option: One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.\n"
252 " :type option: int\n";
254 static PyObject *py_blf_enable(PyObject *UNUSED(self), PyObject *args)
258 if (!PyArg_ParseTuple(args, "ii:blf.enable", &fontid, &option))
261 BLF_enable(fontid, option);
266 static char py_blf_rotation_doc[] =
267 ".. function:: rotation(fontid, angle)\n"
269 " Set the text rotation angle, enable/disable using ROTATION.\n"
271 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
272 " :type fontid: int\n"
273 " :arg angle: The angle for text drawing to use.\n"
274 " :type angle: float\n";
276 static PyObject *py_blf_rotation(PyObject *UNUSED(self), PyObject *args)
281 if (!PyArg_ParseTuple(args, "if:blf.rotation", &fontid, &angle))
284 BLF_rotation(fontid, angle);
289 static char py_blf_shadow_doc[] =
290 ".. function:: shadow(fontid, level, r, g, b, a)\n"
292 " Shadow options, enable/disable using SHADOW .\n"
294 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
295 " :type fontid: int\n"
296 " :arg level: The blur level, can be 3, 5 or 0.\n"
297 " :type level: int\n"
298 " :arg r: Shadow color (red channel 0.0 - 1.0).\n"
300 " :arg g: Shadow color (green channel 0.0 - 1.0).\n"
302 " :arg b: Shadow color (blue channel 0.0 - 1.0).\n"
304 " :arg a: Shadow color (alpha channel 0.0 - 1.0).\n"
307 static PyObject *py_blf_shadow(PyObject *UNUSED(self), PyObject *args)
312 if (!PyArg_ParseTuple(args, "iiffff:blf.shadow", &fontid, &level, &r, &g, &b, &a))
315 if (level != 0 && level != 3 && level != 5) {
316 PyErr_SetString(PyExc_TypeError, "blf.shadow expected arg to be in (0, 3, 5)");
320 BLF_shadow(fontid, level, r, g, b, a);
325 static char py_blf_shadow_offset_doc[] =
326 ".. function:: shadow_offset(fontid, x, y)\n"
328 " Set the offset for shadow text.\n"
330 " :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
331 " :type fontid: int\n"
332 " :arg x: Vertical shadow offset value in pixels.\n"
334 " :arg y: Horizontal shadow offset value in pixels.\n"
337 static PyObject *py_blf_shadow_offset(PyObject *UNUSED(self), PyObject *args)
341 if (!PyArg_ParseTuple(args, "iii:blf.shadow_offset", &fontid, &x, &y))
344 BLF_shadow_offset(fontid, x, y);
349 static char py_blf_load_doc[] =
350 ".. function:: load(filename)\n"
352 " Load a new font.\n"
354 " :arg filename: the filename of the font.\n"
355 " :type filename: string\n"
356 " :return: the new font's fontid or -1 if there was an error.\n"
357 " :rtype: integer\n";
359 static PyObject *py_blf_load(PyObject *UNUSED(self), PyObject *args)
363 if (!PyArg_ParseTuple(args, "s:blf.load", &filename))
366 return PyLong_FromLong(BLF_load(filename));
369 /*----------------------------MODULE INIT-------------------------*/
370 static PyMethodDef BLF_methods[] = {
371 {"aspect", (PyCFunction) py_blf_aspect, METH_VARARGS, py_blf_aspect_doc},
372 {"blur", (PyCFunction) py_blf_blur, METH_VARARGS, py_blf_blur_doc},
373 {"clipping", (PyCFunction) py_blf_clipping, METH_VARARGS, py_blf_clipping_doc},
374 {"disable", (PyCFunction) py_blf_disable, METH_VARARGS, py_blf_disable_doc},
375 {"dimensions", (PyCFunction) py_blf_dimensions, METH_VARARGS, py_blf_dimensions_doc},
376 {"draw", (PyCFunction) py_blf_draw, METH_VARARGS, py_blf_draw_doc},
377 {"enable", (PyCFunction) py_blf_enable, METH_VARARGS, py_blf_enable_doc},
378 {"position", (PyCFunction) py_blf_position, METH_VARARGS, py_blf_position_doc},
379 {"rotation", (PyCFunction) py_blf_rotation, METH_VARARGS, py_blf_rotation_doc},
380 {"shadow", (PyCFunction) py_blf_shadow, METH_VARARGS, py_blf_shadow_doc},
381 {"shadow_offset", (PyCFunction) py_blf_shadow_offset, METH_VARARGS, py_blf_shadow_offset_doc},
382 {"size", (PyCFunction) py_blf_size, METH_VARARGS, py_blf_size_doc},
383 {"load", (PyCFunction) py_blf_load, METH_VARARGS, py_blf_load_doc},
384 {NULL, NULL, 0, NULL}
387 static char BLF_doc[] =
388 "This module provides access to blenders text drawing functions.\n";
390 static struct PyModuleDef BLF_module_def = {
391 PyModuleDef_HEAD_INIT,
395 BLF_methods, /* m_methods */
397 NULL, /* m_traverse */
402 PyObject *BPyInit_blf(void)
406 submodule = PyModule_Create(&BLF_module_def);
408 PyModule_AddIntConstant(submodule, "ROTATION", BLF_ROTATION);
409 PyModule_AddIntConstant(submodule, "CLIPPING", BLF_CLIPPING);
410 PyModule_AddIntConstant(submodule, "SHADOW", BLF_SHADOW);
411 PyModule_AddIntConstant(submodule, "KERNING_DEFAULT", BLF_KERNING_DEFAULT);