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