Blender V2.61 - r43446

blf_py_api.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Campbell Barton
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00029 #include <Python.h>
00030 #include "blf_py_api.h"
00031 
00032 #include "../../blenfont/BLF_api.h"
00033 
00034 #include "BLI_utildefines.h"
00035 
00036 PyDoc_STRVAR(py_blf_position_doc,
00037 ".. function:: position(fontid, x, y, z)\n"
00038 "\n"
00039 "   Set the position for drawing text.\n"
00040 "\n"
00041 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00042 "   :type fontid: int\n"
00043 "   :arg x: X axis position to draw the text.\n"
00044 "   :type x: float\n"
00045 "   :arg y: Y axis position to draw the text.\n"
00046 "   :type y: float\n"
00047 "   :arg z: Z axis position to draw the text.\n"
00048 "   :type z: float\n"
00049 );
00050 
00051 static PyObject *py_blf_position(PyObject *UNUSED(self), PyObject *args)
00052 {
00053     int fontid;
00054     float x, y, z;
00055 
00056     if (!PyArg_ParseTuple(args, "ifff:blf.position", &fontid, &x, &y, &z))
00057         return NULL;
00058 
00059     BLF_position(fontid, x, y, z);
00060 
00061     Py_RETURN_NONE;
00062 }
00063 
00064 
00065 PyDoc_STRVAR(py_blf_size_doc,
00066 ".. function:: size(fontid, size, dpi)\n"
00067 "\n"
00068 "   Set the size and dpi for drawing text.\n"
00069 "\n"
00070 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00071 "   :type fontid: int\n"
00072 "   :arg size: Point size of the font.\n"
00073 "   :type size: int\n"
00074 "   :arg dpi: dots per inch value to use for drawing.\n"
00075 "   :type dpi: int\n"
00076 );
00077 static PyObject *py_blf_size(PyObject *UNUSED(self), PyObject *args)
00078 {
00079     int fontid, size, dpi;
00080 
00081     if (!PyArg_ParseTuple(args, "iii:blf.size", &fontid, &size, &dpi))
00082         return NULL;
00083 
00084     BLF_size(fontid, size, dpi);
00085 
00086     Py_RETURN_NONE;
00087 }
00088 
00089 
00090 PyDoc_STRVAR(py_blf_aspect_doc,
00091 ".. function:: aspect(fontid, aspect)\n"
00092 "\n"
00093 "   Set the aspect for drawing text.\n"
00094 "\n"
00095 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00096 "   :type fontid: int\n"
00097 "   :arg aspect: The aspect ratio for text drawing to use.\n"
00098 "   :type aspect: float\n"
00099 );
00100 static PyObject *py_blf_aspect(PyObject *UNUSED(self), PyObject *args)
00101 {
00102     float aspect;
00103     int fontid;
00104 
00105     if (!PyArg_ParseTuple(args, "if:blf.aspect", &fontid, &aspect))
00106         return NULL;
00107 
00108     BLF_aspect(fontid, aspect, aspect, 1.0);
00109 
00110     Py_RETURN_NONE;
00111 }
00112 
00113 
00114 PyDoc_STRVAR(py_blf_blur_doc,
00115 ".. function:: blur(fontid, radius)\n"
00116 "\n"
00117 "   Set the blur radius for drawing text.\n"
00118 "\n"
00119 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00120 "   :type fontid: int\n"
00121 "   :arg radius: The radius for blurring text (in pixels).\n"
00122 "   :type radius: int\n"
00123 );
00124 static PyObject *py_blf_blur(PyObject *UNUSED(self), PyObject *args)
00125 {
00126     int blur, fontid;
00127 
00128     if (!PyArg_ParseTuple(args, "ii:blf.blur", &fontid, &blur))
00129         return NULL;
00130 
00131     BLF_blur(fontid, blur);
00132 
00133     Py_RETURN_NONE;
00134 }
00135 
00136 
00137 PyDoc_STRVAR(py_blf_draw_doc,
00138 ".. function:: draw(fontid, text)\n"
00139 "\n"
00140 "   Draw text in the current context.\n"
00141 "\n"
00142 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00143 "   :type fontid: int\n"
00144 "   :arg text: the text to draw.\n"
00145 "   :type text: string\n"
00146 );
00147 static PyObject *py_blf_draw(PyObject *UNUSED(self), PyObject *args)
00148 {
00149     char *text;
00150     int text_length;
00151     int fontid;
00152 
00153     if (!PyArg_ParseTuple(args, "is#:blf.draw", &fontid, &text, &text_length))
00154         return NULL;
00155 
00156     BLF_draw(fontid, text, (unsigned int)text_length);
00157 
00158     Py_RETURN_NONE;
00159 }
00160 
00161 PyDoc_STRVAR(py_blf_dimensions_doc,
00162 ".. function:: dimensions(fontid, text)\n"
00163 "\n"
00164 "   Return the width and height of the text.\n"
00165 "\n"
00166 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00167 "   :type fontid: int\n"
00168 "   :arg text: the text to draw.\n"
00169 "   :type text: string\n"
00170 "   :return: the width and height of the text.\n"
00171 "   :rtype: tuple of 2 floats\n"
00172 );
00173 static PyObject *py_blf_dimensions(PyObject *UNUSED(self), PyObject *args)
00174 {
00175     char *text;
00176     float r_width, r_height;
00177     PyObject *ret;
00178     int fontid;
00179 
00180     if (!PyArg_ParseTuple(args, "is:blf.dimensions", &fontid, &text))
00181         return NULL;
00182 
00183     BLF_width_and_height(fontid, text, &r_width, &r_height);
00184 
00185     ret= PyTuple_New(2);
00186     PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(r_width));
00187     PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(r_height));
00188     return ret;
00189 }
00190 
00191 PyDoc_STRVAR(py_blf_clipping_doc,
00192 ".. function:: clipping(fontid, xmin, ymin, xmax, ymax)\n"
00193 "\n"
00194 "   Set the clipping, enable/disable using CLIPPING.\n"
00195 "\n"
00196 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00197 "   :type fontid: int\n"
00198 "   :arg xmin: Clip the drawing area by these bounds.\n"
00199 "   :type xmin: float\n"
00200 "   :arg ymin: Clip the drawing area by these bounds.\n"
00201 "   :type ymin: float\n"
00202 "   :arg xmax: Clip the drawing area by these bounds.\n"
00203 "   :type xmax: float\n"
00204 "   :arg ymax: Clip the drawing area by these bounds.\n"
00205 "   :type ymax: float\n"
00206 );
00207 static PyObject *py_blf_clipping(PyObject *UNUSED(self), PyObject *args)
00208 {
00209     float xmin, ymin, xmax, ymax;
00210     int fontid;
00211 
00212     if (!PyArg_ParseTuple(args, "iffff:blf.clipping", &fontid, &xmin, &ymin, &xmax, &ymax))
00213         return NULL;
00214 
00215     BLF_clipping(fontid, xmin, ymin, xmax, ymax);
00216 
00217     Py_RETURN_NONE;
00218 }
00219 
00220 PyDoc_STRVAR(py_blf_disable_doc,
00221 ".. function:: disable(fontid, option)\n"
00222 "\n"
00223 "   Disable option.\n"
00224 "\n"
00225 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00226 "   :type fontid: int\n"
00227 "   :arg option: One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.\n"
00228 "   :type option: int\n"
00229 );
00230 static PyObject *py_blf_disable(PyObject *UNUSED(self), PyObject *args)
00231 {
00232     int option, fontid;
00233 
00234     if (!PyArg_ParseTuple(args, "ii:blf.disable", &fontid, &option))
00235         return NULL;
00236 
00237     BLF_disable(fontid, option);
00238 
00239     Py_RETURN_NONE;
00240 }
00241 
00242 PyDoc_STRVAR(py_blf_enable_doc,
00243 ".. function:: enable(fontid, option)\n"
00244 "\n"
00245 "   Enable option.\n"
00246 "\n"
00247 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00248 "   :type fontid: int\n"
00249 "   :arg option: One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.\n"
00250 "   :type option: int\n"
00251 );
00252 static PyObject *py_blf_enable(PyObject *UNUSED(self), PyObject *args)
00253 {
00254     int option, fontid;
00255 
00256     if (!PyArg_ParseTuple(args, "ii:blf.enable", &fontid, &option))
00257         return NULL;
00258 
00259     BLF_enable(fontid, option);
00260 
00261     Py_RETURN_NONE;
00262 }
00263 
00264 PyDoc_STRVAR(py_blf_rotation_doc,
00265 ".. function:: rotation(fontid, angle)\n"
00266 "\n"
00267 "   Set the text rotation angle, enable/disable using ROTATION.\n"
00268 "\n"
00269 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00270 "   :type fontid: int\n"
00271 "   :arg angle: The angle for text drawing to use.\n"
00272 "   :type angle: float\n"
00273 );
00274 static PyObject *py_blf_rotation(PyObject *UNUSED(self), PyObject *args)
00275 {
00276     float angle;
00277     int fontid;
00278 
00279     if (!PyArg_ParseTuple(args, "if:blf.rotation", &fontid, &angle))
00280         return NULL;
00281         
00282     BLF_rotation(fontid, angle);
00283 
00284     Py_RETURN_NONE;
00285 }
00286 
00287 PyDoc_STRVAR(py_blf_shadow_doc,
00288 ".. function:: shadow(fontid, level, r, g, b, a)\n"
00289 "\n"
00290 "   Shadow options, enable/disable using SHADOW .\n"
00291 "\n"
00292 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00293 "   :type fontid: int\n"
00294 "   :arg level: The blur level, can be 3, 5 or 0.\n"
00295 "   :type level: int\n"
00296 "   :arg r: Shadow color (red channel 0.0 - 1.0).\n"
00297 "   :type r: float\n"
00298 "   :arg g: Shadow color (green channel 0.0 - 1.0).\n"
00299 "   :type g: float\n"
00300 "   :arg b: Shadow color (blue channel 0.0 - 1.0).\n"
00301 "   :type b: float\n"
00302 "   :arg a: Shadow color (alpha channel 0.0 - 1.0).\n"
00303 "   :type a: float\n"
00304 );
00305 static PyObject *py_blf_shadow(PyObject *UNUSED(self), PyObject *args)
00306 {
00307     int level, fontid;
00308     float r, g, b, a;
00309 
00310     if (!PyArg_ParseTuple(args, "iiffff:blf.shadow", &fontid, &level, &r, &g, &b, &a))
00311         return NULL;
00312 
00313     if (level != 0 && level != 3 && level != 5) {
00314         PyErr_SetString(PyExc_TypeError, "blf.shadow expected arg to be in (0, 3, 5)");
00315         return NULL;
00316     }
00317 
00318     BLF_shadow(fontid, level, r, g, b, a);
00319 
00320     Py_RETURN_NONE;
00321 }
00322 
00323 PyDoc_STRVAR(py_blf_shadow_offset_doc,
00324 ".. function:: shadow_offset(fontid, x, y)\n"
00325 "\n"
00326 "   Set the offset for shadow text.\n"
00327 "\n"
00328 "   :arg fontid: The id of the typeface as returned by :func:`blf.load`, for default font use 0.\n"
00329 "   :type fontid: int\n"
00330 "   :arg x: Vertical shadow offset value in pixels.\n"
00331 "   :type x: float\n"
00332 "   :arg y: Horizontal shadow offset value in pixels.\n"
00333 "   :type y: float\n"
00334 );
00335 static PyObject *py_blf_shadow_offset(PyObject *UNUSED(self), PyObject *args)
00336 {
00337     int x, y, fontid;
00338 
00339     if (!PyArg_ParseTuple(args, "iii:blf.shadow_offset", &fontid, &x, &y))
00340         return NULL;
00341 
00342     BLF_shadow_offset(fontid, x, y);
00343 
00344     Py_RETURN_NONE;
00345 }
00346 
00347 PyDoc_STRVAR(py_blf_load_doc,
00348 ".. function:: load(filename)\n"
00349 "\n"
00350 "   Load a new font.\n"
00351 "\n"
00352 "   :arg filename: the filename of the font.\n"
00353 "   :type filename: string\n"
00354 "   :return: the new font's fontid or -1 if there was an error.\n"
00355 "   :rtype: integer\n"
00356 );
00357 static PyObject *py_blf_load(PyObject *UNUSED(self), PyObject *args)
00358 {
00359     char* filename;
00360 
00361     if (!PyArg_ParseTuple(args, "s:blf.load", &filename))
00362         return NULL;
00363 
00364     return PyLong_FromLong(BLF_load(filename));
00365 }
00366 
00367 PyDoc_STRVAR(py_blf_unload_doc,
00368 ".. function:: unload(filename)\n"
00369 "\n"
00370 "   Unload an existing font.\n"
00371 "\n"
00372 "   :arg filename: the filename of the font.\n"
00373 "   :type filename: string\n"
00374 );
00375 static PyObject *py_blf_unload(PyObject *UNUSED(self), PyObject *args)
00376 {
00377     char* filename;
00378 
00379     if (!PyArg_ParseTuple(args, "s:blf.unload", &filename))
00380         return NULL;
00381 
00382     BLF_unload(filename);
00383 
00384     Py_RETURN_NONE;
00385 }
00386 
00387 /*----------------------------MODULE INIT-------------------------*/
00388 static PyMethodDef BLF_methods[] = {
00389     {"aspect", (PyCFunction) py_blf_aspect, METH_VARARGS, py_blf_aspect_doc},
00390     {"blur", (PyCFunction) py_blf_blur, METH_VARARGS, py_blf_blur_doc},
00391     {"clipping", (PyCFunction) py_blf_clipping, METH_VARARGS, py_blf_clipping_doc},
00392     {"disable", (PyCFunction) py_blf_disable, METH_VARARGS, py_blf_disable_doc},
00393     {"dimensions", (PyCFunction) py_blf_dimensions, METH_VARARGS, py_blf_dimensions_doc},
00394     {"draw", (PyCFunction) py_blf_draw, METH_VARARGS, py_blf_draw_doc},
00395     {"enable", (PyCFunction) py_blf_enable, METH_VARARGS, py_blf_enable_doc},
00396     {"position", (PyCFunction) py_blf_position, METH_VARARGS, py_blf_position_doc},
00397     {"rotation", (PyCFunction) py_blf_rotation, METH_VARARGS, py_blf_rotation_doc},
00398     {"shadow", (PyCFunction) py_blf_shadow, METH_VARARGS, py_blf_shadow_doc},
00399     {"shadow_offset", (PyCFunction) py_blf_shadow_offset, METH_VARARGS, py_blf_shadow_offset_doc},
00400     {"size", (PyCFunction) py_blf_size, METH_VARARGS, py_blf_size_doc},
00401     {"load", (PyCFunction) py_blf_load, METH_VARARGS, py_blf_load_doc},
00402     {"unload", (PyCFunction) py_blf_unload, METH_VARARGS, py_blf_unload_doc},
00403     {NULL, NULL, 0, NULL}
00404 };
00405 
00406 PyDoc_STRVAR(BLF_doc,
00407 "This module provides access to blenders text drawing functions."
00408 );
00409 static struct PyModuleDef BLF_module_def = {
00410     PyModuleDef_HEAD_INIT,
00411     "blf",  /* m_name */
00412     BLF_doc,  /* m_doc */
00413     0,  /* m_size */
00414     BLF_methods,  /* m_methods */
00415     NULL,  /* m_reload */
00416     NULL,  /* m_traverse */
00417     NULL,  /* m_clear */
00418     NULL,  /* m_free */
00419 };
00420 
00421 PyObject *BPyInit_blf(void)
00422 {
00423     PyObject *submodule;
00424 
00425     submodule = PyModule_Create(&BLF_module_def);
00426 
00427     PyModule_AddIntConstant(submodule, "ROTATION", BLF_ROTATION);
00428     PyModule_AddIntConstant(submodule, "CLIPPING", BLF_CLIPPING);
00429     PyModule_AddIntConstant(submodule, "SHADOW", BLF_SHADOW);
00430     PyModule_AddIntConstant(submodule, "KERNING_DEFAULT", BLF_KERNING_DEFAULT);
00431 
00432     return submodule;
00433 }