Blender V5.3
blf_py_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11/* Future-proof, See https://docs.python.org/3/c-api/arg.html#strings-and-buffers */
12#define PY_SSIZE_T_CLEAN
13
14#include "blf_py_api.hh"
15
16#include "py_capi_utils.hh"
17
18#include <Python.h>
19
21
22#include "BLI_utildefines.hh"
23
27
28#include "python_compat.hh" /* IWYU pragma: keep. */
29
30#include "python_utildefines.hh"
31
32#include "imbuf_py_api.hh"
33
34namespace blender {
35
37 PyObject_HEAD /* Required Python macro. */
38 PyObject *py_imbuf;
39
40 int fontid;
42};
43
45 /* Wrap. */
46 py_blf_position_doc,
47 ".. function:: position(fontid, x, y, z)\n"
48 "\n"
49 " Set the position for drawing text.\n"
50 "\n"
51 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
52 "font use 0.\n"
53 " :type fontid: int\n"
54 " :param x: X axis position to draw the text.\n"
55 " :type x: float\n"
56 " :param y: Y axis position to draw the text.\n"
57 " :type y: float\n"
58 " :param z: Z axis position to draw the text (typically 0).\n"
59 " :type z: float\n");
60static PyObject *py_blf_position(PyObject * /*self*/, PyObject *args)
61{
62 int fontid;
63 float x, y, z;
64
65 if (!PyArg_ParseTuple(args,
66 "i" /* `fontid` */
67 "f" /* `x` */
68 "f" /* `y` */
69 "f" /* `z` */
70 ":blf.position",
71 &fontid,
72 &x,
73 &y,
74 &z))
75 {
76 return nullptr;
77 }
78
79 BLF_position(fontid, x, y, z);
80
81 Py_RETURN_NONE;
82}
83
85 /* Wrap. */
86 py_blf_size_doc,
87 ".. function:: size(fontid, size)\n"
88 "\n"
89 " Set the size for drawing text.\n"
90 "\n"
91 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
92 "font use 0.\n"
93 " :type fontid: int\n"
94 " :param size: Point size of the font.\n"
95 " :type size: float\n");
96static PyObject *py_blf_size(PyObject * /*self*/, PyObject *args)
97{
98 int fontid;
99 float size;
100
101 if (!PyArg_ParseTuple(args,
102 "i" /* `fontid` */
103 "f" /* `size` */
104 ":blf.size",
105 &fontid,
106 &size))
107 {
108 return nullptr;
109 }
110
111 BLF_size(fontid, size);
112
113 Py_RETURN_NONE;
114}
115
117 /* Wrap. */
118 py_blf_aspect_doc,
119 ".. function:: aspect(fontid, aspect)\n"
120 "\n"
121 " Set the aspect for drawing text.\n"
122 "\n"
123 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
124 "font use 0.\n"
125 " :type fontid: int\n"
126 " :param aspect: The aspect ratio for non-uniform scaling of text.\n"
127 " :type aspect: float\n");
128static PyObject *py_blf_aspect(PyObject * /*self*/, PyObject *args)
129{
130 float aspect;
131 int fontid;
132
133 if (!PyArg_ParseTuple(args,
134 "i" /* `fontid` */
135 "f" /* `aspect` */
136 ":blf.aspect",
137 &fontid,
138 &aspect))
139 {
140 return nullptr;
141 }
142
143 BLF_aspect(fontid, aspect, aspect, 1.0);
144
145 Py_RETURN_NONE;
146}
147
149 /* Wrap. */
150 py_blf_color_doc,
151 ".. function:: color(fontid, r, g, b, a)\n"
152 "\n"
153 " Set the color for drawing text.\n"
154 "\n"
155 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
156 "font use 0.\n"
157 " :type fontid: int\n"
158 " :param r: Red channel 0.0 - 1.0.\n"
159 " :type r: float\n"
160 " :param g: Green channel 0.0 - 1.0.\n"
161 " :type g: float\n"
162 " :param b: Blue channel 0.0 - 1.0.\n"
163 " :type b: float\n"
164 " :param a: Alpha channel 0.0 - 1.0.\n"
165 " :type a: float\n");
166static PyObject *py_blf_color(PyObject * /*self*/, PyObject *args)
167{
168 int fontid;
169 float rgba[4];
170
171 if (!PyArg_ParseTuple(args,
172 "i" /* `fontid` */
173 "f" /* `r` */
174 "f" /* `g` */
175 "f" /* `b` */
176 "f" /* `a` */
177 ":blf.color",
178 &fontid,
179 &rgba[0],
180 &rgba[1],
181 &rgba[2],
182 &rgba[3]))
183 {
184 return nullptr;
185 }
186
187 BLF_color4fv(fontid, rgba);
188
189 /* NOTE(@ideasman42): that storing these colors separately looks like something that could
190 * be refactored away if the font's internal color format was changed from `uint8` to `float`. */
191 BLF_buffer_col(fontid, rgba);
192
193 Py_RETURN_NONE;
194}
195
197 /* Wrap. */
198 py_blf_draw_doc,
199 ".. function:: draw(fontid, text)\n"
200 "\n"
201 " Draw text in the current context.\n"
202 "\n"
203 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
204 "font use 0.\n"
205 " :type fontid: int\n"
206 " :param text: The text to draw.\n"
207 " :type text: str\n");
208static PyObject *py_blf_draw(PyObject * /*self*/, PyObject *args)
209{
210 const char *text;
211 Py_ssize_t text_length;
212 int fontid;
213
214 if (!PyArg_ParseTuple(args,
215 "i" /* `fontid` */
216 "s#" /* `text` */
217 ":blf.draw",
218 &fontid,
219 &text,
220 &text_length))
221 {
222 return nullptr;
223 }
224
225 BLF_draw(fontid, text, uint(text_length));
226
227 Py_RETURN_NONE;
228}
229
231 /* Wrap. */
232 py_blf_draw_buffer_doc,
233 ".. function:: draw_buffer(fontid, text)\n"
234 "\n"
235 " Draw text into the image buffer bound via :func:`blf.bind_imbuf`.\n"
236 "\n"
237 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
238 "font use 0.\n"
239 " :type fontid: int\n"
240 " :param text: The text to draw into the bound image buffer.\n"
241 " :type text: str\n");
242static PyObject *py_blf_draw_buffer(PyObject * /*self*/, PyObject *args)
243{
244 const char *text;
245 Py_ssize_t text_length;
246 int fontid;
247
248 if (!PyArg_ParseTuple(args,
249 "i" /* `fontid` */
250 "s#" /* `text` */
251 ":blf.draw_buffer",
252 &fontid,
253 &text,
254 &text_length))
255 {
256 return nullptr;
257 }
258
259 BLF_draw_buffer(fontid, text, uint(text_length));
260
261 Py_RETURN_NONE;
262}
263
265 /* Wrap. */
266 py_blf_dimensions_doc,
267 ".. function:: dimensions(fontid, text)\n"
268 "\n"
269 " Return the width and height of the text.\n"
270 "\n"
271 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
272 "font use 0.\n"
273 " :type fontid: int\n"
274 " :param text: The text to measure.\n"
275 " :type text: str\n"
276 " :return: The width and height of the text.\n"
277 " :rtype: tuple[float, float]\n");
278static PyObject *py_blf_dimensions(PyObject * /*self*/, PyObject *args)
279{
280 const char *text;
281 float width, height;
282 PyObject *ret;
283 int fontid;
284
285 if (!PyArg_ParseTuple(args,
286 "i" /* `fontid` */
287 "s" /* `text` */
288 ":blf.dimensions",
289 &fontid,
290 &text))
291 {
292 return nullptr;
293 }
294
295 BLF_width_and_height(fontid, text, INT_MAX, &width, &height);
296
297 ret = PyTuple_New(2);
298 PyTuple_SET_ITEMS(ret, PyFloat_FromDouble(width), PyFloat_FromDouble(height));
299 return ret;
300}
301
303 /* Wrap. */
304 py_blf_clipping_doc,
305 ".. function:: clipping(fontid, xmin, ymin, xmax, ymax)\n"
306 "\n"
307 " Set the clipping, enable/disable using :data:`CLIPPING`.\n"
308 "\n"
309 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
310 "font use 0.\n"
311 " :type fontid: int\n"
312 " :param xmin: Left edge of the clipping rectangle.\n"
313 " :type xmin: float\n"
314 " :param ymin: Bottom edge of the clipping rectangle.\n"
315 " :type ymin: float\n"
316 " :param xmax: Right edge of the clipping rectangle.\n"
317 " :type xmax: float\n"
318 " :param ymax: Top edge of the clipping rectangle.\n"
319 " :type ymax: float\n");
320static PyObject *py_blf_clipping(PyObject * /*self*/, PyObject *args)
321{
322 float xmin, ymin, xmax, ymax;
323 int fontid;
324
325 if (!PyArg_ParseTuple(args,
326 "i" /* `fontid` */
327 "f" /* `xmin` */
328 "f" /* `ymin` */
329 "f" /* `xmax` */
330 "f" /* `ymax` */
331 ":blf.clipping",
332 &fontid,
333 &xmin,
334 &ymin,
335 &xmax,
336 &ymax))
337 {
338 return nullptr;
339 }
340
341 BLF_clipping(fontid, xmin, ymin, xmax, ymax);
342
343 Py_RETURN_NONE;
344}
345
347 /* Wrap. */
348 py_blf_word_wrap_doc,
349 ".. function:: word_wrap(fontid, wrap_width)\n"
350 "\n"
351 " Set the wrap width, enable/disable using :data:`WORD_WRAP`.\n"
352 "\n"
353 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
354 "font use 0.\n"
355 " :type fontid: int\n"
356 " :param wrap_width: The width (in pixels) to wrap words at.\n"
357 " :type wrap_width: int\n");
358static PyObject *py_blf_word_wrap(PyObject * /*self*/, PyObject *args)
359{
360 int wrap_width;
361 int fontid;
362
363 if (!PyArg_ParseTuple(args,
364 "i" /* `fontid` */
365 "i" /* `wrap_width` */
366 ":blf.word_wrap",
367 &fontid,
368 &wrap_width))
369 {
370 return nullptr;
371 }
372
373 BLF_wordwrap(fontid, wrap_width);
374
375 Py_RETURN_NONE;
376}
377
379 /* Wrap. */
380 py_blf_disable_doc,
381 ".. function:: disable(fontid, option)\n"
382 "\n"
383 " Disable a font drawing option.\n"
384 "\n"
385 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
386 "font use 0.\n"
387 " :type fontid: int\n"
388 " :param option: One of :data:`ROTATION`, :data:`CLIPPING`, "
389 ":data:`SHADOW`, :data:`MONOCHROME` or :data:`WORD_WRAP`.\n"
390 " :type option: int\n");
391static PyObject *py_blf_disable(PyObject * /*self*/, PyObject *args)
392{
393 int option, fontid;
394
395 if (!PyArg_ParseTuple(args,
396 "i" /* `fontid` */
397 "i" /* `option` */
398 ":blf.disable",
399 &fontid,
400 &option))
401 {
402 return nullptr;
403 }
404
405 BLF_disable(fontid, FontFlags(option));
406
407 Py_RETURN_NONE;
408}
409
411 /* Wrap. */
412 py_blf_enable_doc,
413 ".. function:: enable(fontid, option)\n"
414 "\n"
415 " Enable a font drawing option.\n"
416 "\n"
417 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
418 "font use 0.\n"
419 " :type fontid: int\n"
420 " :param option: One of :data:`ROTATION`, :data:`CLIPPING`, "
421 ":data:`SHADOW`, :data:`MONOCHROME` or :data:`WORD_WRAP`.\n"
422 " :type option: int\n");
423static PyObject *py_blf_enable(PyObject * /*self*/, PyObject *args)
424{
425 int option, fontid;
426
427 if (!PyArg_ParseTuple(args,
428 "i" /* `fontid` */
429 "i" /* `option` */
430 ":blf.enable",
431 &fontid,
432 &option))
433 {
434 return nullptr;
435 }
436
437 BLF_enable(fontid, FontFlags(option));
438
439 Py_RETURN_NONE;
440}
441
443 /* Wrap. */
444 py_blf_rotation_doc,
445 ".. function:: rotation(fontid, angle)\n"
446 "\n"
447 " Set the text rotation angle, enable/disable using :data:`ROTATION`.\n"
448 "\n"
449 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
450 "font use 0.\n"
451 " :type fontid: int\n"
452 " :param angle: The angle for text drawing to use (in radians).\n"
453 " :type angle: float\n");
454static PyObject *py_blf_rotation(PyObject * /*self*/, PyObject *args)
455{
456 float angle;
457 int fontid;
458
459 if (!PyArg_ParseTuple(args,
460 "i" /* `fontid` */
461 "f" /* `angle` */
462 ":blf.rotation",
463 &fontid,
464 &angle))
465 {
466 return nullptr;
467 }
468
469 BLF_rotation(fontid, angle);
470
471 Py_RETURN_NONE;
472}
473
475 /* Wrap. */
476 py_blf_shadow_doc,
477 ".. function:: shadow(fontid, level, r, g, b, a)\n"
478 "\n"
479 " Shadow options, enable/disable using :data:`SHADOW`.\n"
480 "\n"
481 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
482 "font use 0.\n"
483 " :type fontid: int\n"
484 " :param level: The shadow type: 0 for none, 3 for 3x3 blur, 5 for 5x5 blur "
485 "or 6 for outline. Other values raise a :exc:`TypeError`.\n"
486 " :type level: int\n"
487 " :param r: Shadow color (red channel 0.0 - 1.0).\n"
488 " :type r: float\n"
489 " :param g: Shadow color (green channel 0.0 - 1.0).\n"
490 " :type g: float\n"
491 " :param b: Shadow color (blue channel 0.0 - 1.0).\n"
492 " :type b: float\n"
493 " :param a: Shadow color (alpha channel 0.0 - 1.0).\n"
494 " :type a: float\n");
495static PyObject *py_blf_shadow(PyObject * /*self*/, PyObject *args)
496{
497 int level, fontid;
498 float rgba[4];
499
500 if (!PyArg_ParseTuple(args,
501 "i" /* `fontid` */
502 "i" /* `level` */
503 "f" /* `r` */
504 "f" /* `g` */
505 "f" /* `b` */
506 "f" /* `a` */
507 ":blf.shadow",
508 &fontid,
509 &level,
510 &rgba[0],
511 &rgba[1],
512 &rgba[2],
513 &rgba[3]))
514 {
515 return nullptr;
516 }
517
518 if (!ELEM(level, 0, 3, 5, 6)) {
519 PyErr_SetString(PyExc_TypeError, "blf.shadow expected arg to be in (0, 3, 5, 6)");
520 return nullptr;
521 }
522
523 BLF_shadow(fontid, FontShadowType(level), rgba);
524
525 Py_RETURN_NONE;
526}
527
529 /* Wrap. */
530 py_blf_shadow_offset_doc,
531 ".. function:: shadow_offset(fontid, x, y)\n"
532 "\n"
533 " Set the offset for shadow text, enable/disable using :data:`SHADOW`.\n"
534 "\n"
535 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
536 "font use 0.\n"
537 " :type fontid: int\n"
538 " :param x: Horizontal shadow offset value in pixels.\n"
539 " :type x: int\n"
540 " :param y: Vertical shadow offset value in pixels.\n"
541 " :type y: int\n");
542static PyObject *py_blf_shadow_offset(PyObject * /*self*/, PyObject *args)
543{
544 int x, y, fontid;
545
546 if (!PyArg_ParseTuple(args,
547 "i" /* `fontid` */
548 "i" /* `x` */
549 "i" /* `y` */
550 ":blf.shadow_offset",
551 &fontid,
552 &x,
553 &y))
554 {
555 return nullptr;
556 }
557
558 BLF_shadow_offset(fontid, x, y);
559
560 Py_RETURN_NONE;
561}
562
564 /* Wrap. */
565 py_blf_load_doc,
566 ".. function:: load(filepath)\n"
567 "\n"
568 " Load a new font.\n"
569 "\n"
570 " :param filepath: The filepath of the font.\n"
571 " :type filepath: str | bytes\n"
572 " :return: The new font's fontid or -1 if there was an error.\n"
573 " :rtype: int\n");
574static PyObject *py_blf_load(PyObject * /*self*/, PyObject *args)
575{
576 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
577 if (!PyArg_ParseTuple(args,
578 "O&" /* `filepath` */
579 ":blf.load",
581 &filepath_data))
582 {
583 return nullptr;
584 }
585 const int font_id = BLF_load(filepath_data.value);
586 Py_XDECREF(filepath_data.value_coerce);
587
588 return PyLong_FromLong(font_id);
589}
590
592 /* Wrap. */
593 py_blf_unload_doc,
594 ".. function:: unload(filepath)\n"
595 "\n"
596 " Unload an existing font.\n"
597 "\n"
598 " :param filepath: The filepath of the font.\n"
599 " :type filepath: str | bytes\n");
600static PyObject *py_blf_unload(PyObject * /*self*/, PyObject *args)
601{
602 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
603 if (!PyArg_ParseTuple(args,
604 "O&" /* `filepath` */
605 ":blf.unload",
607 &filepath_data))
608 {
609 return nullptr;
610 }
611
612 BLF_unload(filepath_data.value);
613 Py_XDECREF(filepath_data.value_coerce);
614
615 Py_RETURN_NONE;
616}
617
618/* -------------------------------------------------------------------- */
623
625{
626 if (self->buffer_state) [[unlikely]] {
627 PyErr_SetString(PyExc_ValueError,
628 "BLFImBufContext.__enter__: unable to enter the same context more than once");
629 return nullptr;
630 }
631
633 if (ibuf == nullptr) {
634 /* The error will have been set. */
635 return nullptr;
636 }
637 BLFBufferState *buffer_state = BLF_buffer_state_push(self->fontid);
638 if (buffer_state == nullptr) {
639 PyErr_Format(PyExc_ValueError, "bind_imbuf: unknown fontid %d", self->fontid);
640 return nullptr;
641 }
642 BLF_buffer(self->fontid,
643 ibuf->float_data_for_write(),
644 ibuf->byte_data_for_write(),
645 ibuf->x,
646 ibuf->y,
647 4,
648 ibuf->byte_buffer.colorspace);
649 self->buffer_state = buffer_state;
650
651 Py_RETURN_NONE;
652}
653
654static PyObject *py_blf_bind_imbuf_exit(BPyBLFImBufContext *self, PyObject * /*args*/)
655{
656 if (self->buffer_state == nullptr) [[unlikely]] {
657 PyErr_SetString(PyExc_ValueError,
658 "BLFImBufContext.__exit__: called without a matching __enter__");
659 return nullptr;
660 }
661
663 self->buffer_state = nullptr;
664
665 Py_RETURN_NONE;
666}
667
669{
670 if (self->buffer_state) {
671 /* This should practically never happen since it implies
672 * `__enter__` is called without a matching `__exit__`.
673 * Do this mainly for correctness:
674 * if the process somehow exits before exiting the context manager. */
676 }
677
678 PyObject_GC_UnTrack(self);
679 Py_CLEAR(self->py_imbuf);
680 PyObject_GC_Del(self);
681}
682
683static int py_blf_bind_imbuf_traverse(BPyBLFImBufContext *self, visitproc visit, void *arg)
684{
685 Py_VISIT(self->py_imbuf);
686 return 0;
687}
688
690{
691 Py_CLEAR(self->py_imbuf);
692 return 0;
693}
694
695#ifdef __GNUC__
696# ifdef __clang__
697# pragma clang diagnostic push
698# pragma clang diagnostic ignored "-Wcast-function-type"
699# else
700# pragma GCC diagnostic push
701# pragma GCC diagnostic ignored "-Wcast-function-type"
702# endif
703#endif
704
705static PyMethodDef py_blf_bind_imbuf_methods[] = {
706 {"__enter__", reinterpret_cast<PyCFunction>(py_blf_bind_imbuf_enter), METH_NOARGS},
707 {"__exit__", reinterpret_cast<PyCFunction>(py_blf_bind_imbuf_exit), METH_VARARGS},
708 {nullptr},
709};
710
711#ifdef __GNUC__
712# ifdef __clang__
713# pragma clang diagnostic pop
714# else
715# pragma GCC diagnostic pop
716# endif
717#endif
718
720 /* Wrap. */
721 BPyBLFImBufContext_Type_doc,
722 "Context manager returned by :func:`blf.bind_imbuf` that binds an image buffer\n"
723 "as the destination for text drawing.");
724
725static PyTypeObject BPyBLFImBufContext_Type = {
726 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
727 /*tp_name*/ "BLFImBufContext",
728 /*tp_basicsize*/ sizeof(BPyBLFImBufContext),
729 /*tp_itemsize*/ 0,
730 /*tp_dealloc*/ reinterpret_cast<destructor>(py_blf_bind_imbuf_dealloc),
731 /*tp_vectorcall_offset*/ 0,
732 /*tp_getattr*/ nullptr,
733 /*tp_setattr*/ nullptr,
734 /*tp_as_async*/ nullptr,
735 /*tp_repr*/ nullptr,
736 /*tp_as_number*/ nullptr,
737 /*tp_as_sequence*/ nullptr,
738 /*tp_as_mapping*/ nullptr,
739 /*tp_hash*/ nullptr,
740 /*tp_call*/ nullptr,
741 /*tp_str*/ nullptr,
742 /*tp_getattro*/ nullptr,
743 /*tp_setattro*/ nullptr,
744 /*tp_as_buffer*/ nullptr,
745 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
746 /*tp_doc*/ BPyBLFImBufContext_Type_doc,
747 /*tp_traverse*/ reinterpret_cast<traverseproc>(py_blf_bind_imbuf_traverse),
748 /*tp_clear*/ reinterpret_cast<inquiry>(py_blf_bind_imbuf_clear),
749 /*tp_richcompare*/ nullptr,
750 /*tp_weaklistoffset*/ 0,
751 /*tp_iter*/ nullptr,
752 /*tp_iternext*/ nullptr,
753 /*tp_methods*/ py_blf_bind_imbuf_methods,
754 /*tp_members*/ nullptr,
755 /*tp_getset*/ nullptr,
756 /*tp_base*/ nullptr,
757 /*tp_dict*/ nullptr,
758 /*tp_descr_get*/ nullptr,
759 /*tp_descr_set*/ nullptr,
760 /*tp_dictoffset*/ 0,
761 /*tp_init*/ nullptr,
762 /*tp_alloc*/ nullptr,
763 /*tp_new*/ nullptr,
764 /*tp_free*/ nullptr,
765 /*tp_is_gc*/ nullptr,
766 /*tp_bases*/ nullptr,
767 /*tp_mro*/ nullptr,
768 /*tp_cache*/ nullptr,
769 /*tp_subclasses*/ nullptr,
770 /*tp_weaklist*/ nullptr,
771 /*tp_del*/ nullptr,
772 /*tp_version_tag*/ 0,
773 /*tp_finalize*/ nullptr,
774 /*tp_vectorcall*/ nullptr,
775};
776
778 /* Wrap. */
779 py_blf_bind_imbuf_doc,
780 ".. function:: bind_imbuf(fontid, imbuf, *, display_name=None)\n"
781 "\n"
782 " Context manager to draw text into an image buffer instead of the GPU's context.\n"
783 "\n"
784 " :param fontid: The id of the typeface as returned by :func:`blf.load`, for default "
785 "font use 0.\n"
786 " :type fontid: int\n"
787 " :param imbuf: The image to draw into.\n"
788 " :type imbuf: :class:`imbuf.types.ImBuf`\n"
789 " :param display_name: Ignored (formerly a color-space transform name), "
790 "kept for backwards compatibility.\n"
791 " :type display_name: str | None\n"
792
793 " :return: The BLF ImBuf context manager.\n"
794 " :rtype: :class:`blf.types.BLFImBufContext`\n");
795static PyObject *py_blf_bind_imbuf(PyObject * /*self*/, PyObject *args, PyObject *kwds)
796{
797 int fontid;
798 PyObject *py_imbuf = nullptr;
799 const char *display_name = nullptr;
800
801 static const char *_keywords[] = {
802 "",
803 "",
804 "display_name",
805 nullptr,
806 };
807
808 static _PyArg_Parser _parser = {
809 "i" /* `fontid` */
810 "O!" /* `image` */
811 "|" /* Optional arguments. */
812 "z" /* `display_name` */
813 ":bind_imbuf",
814 _keywords,
815 nullptr,
816 };
817 if (!_PyArg_ParseTupleAndKeywordsFast(
818 args, kwds, &_parser, &fontid, &Py_ImBuf_Type, &py_imbuf, &display_name))
819 {
820 return nullptr;
821 }
822
823 /* Display name is ignored, it is only kept for backwards compatibility. This should
824 * always have been the image buffer byte colorspace rather than a display. */
825
827
828 ret->py_imbuf = Py_NewRef(py_imbuf);
829
830 ret->fontid = fontid;
831 ret->buffer_state = nullptr;
832
833 PyObject_GC_Track(ret);
834
835 return reinterpret_cast<PyObject *>(ret);
836}
837
839
840#ifdef __GNUC__
841# ifdef __clang__
842# pragma clang diagnostic push
843# pragma clang diagnostic ignored "-Wcast-function-type"
844# else
845# pragma GCC diagnostic push
846# pragma GCC diagnostic ignored "-Wcast-function-type"
847# endif
848#endif
849
850/*----------------------------MODULE INIT-------------------------*/
851static PyMethodDef BLF_methods[] = {
852 {"aspect", static_cast<PyCFunction>(py_blf_aspect), METH_VARARGS, py_blf_aspect_doc},
853 {"clipping", static_cast<PyCFunction>(py_blf_clipping), METH_VARARGS, py_blf_clipping_doc},
854 {"word_wrap", static_cast<PyCFunction>(py_blf_word_wrap), METH_VARARGS, py_blf_word_wrap_doc},
855 {"disable", static_cast<PyCFunction>(py_blf_disable), METH_VARARGS, py_blf_disable_doc},
856 {"dimensions",
857 static_cast<PyCFunction>(py_blf_dimensions),
858 METH_VARARGS,
859 py_blf_dimensions_doc},
860 {"draw", static_cast<PyCFunction>(py_blf_draw), METH_VARARGS, py_blf_draw_doc},
861 {"draw_buffer",
862 static_cast<PyCFunction>(py_blf_draw_buffer),
863 METH_VARARGS,
864 py_blf_draw_buffer_doc},
865 {"enable", static_cast<PyCFunction>(py_blf_enable), METH_VARARGS, py_blf_enable_doc},
866 {"position", static_cast<PyCFunction>(py_blf_position), METH_VARARGS, py_blf_position_doc},
867 {"rotation", static_cast<PyCFunction>(py_blf_rotation), METH_VARARGS, py_blf_rotation_doc},
868 {"shadow", static_cast<PyCFunction>(py_blf_shadow), METH_VARARGS, py_blf_shadow_doc},
869 {"shadow_offset",
870 static_cast<PyCFunction>(py_blf_shadow_offset),
871 METH_VARARGS,
872 py_blf_shadow_offset_doc},
873 {"size", static_cast<PyCFunction>(py_blf_size), METH_VARARGS, py_blf_size_doc},
874 {"color", static_cast<PyCFunction>(py_blf_color), METH_VARARGS, py_blf_color_doc},
875 {"load", static_cast<PyCFunction>(py_blf_load), METH_VARARGS, py_blf_load_doc},
876 {"unload", static_cast<PyCFunction>(py_blf_unload), METH_VARARGS, py_blf_unload_doc},
877
878 {"bind_imbuf",
879 reinterpret_cast<PyCFunction>(py_blf_bind_imbuf),
880 METH_VARARGS | METH_KEYWORDS,
881 py_blf_bind_imbuf_doc},
882
883 {nullptr, nullptr, 0, nullptr},
884};
885
886#ifdef __GNUC__
887# ifdef __clang__
888# pragma clang diagnostic pop
889# else
890# pragma GCC diagnostic pop
891# endif
892#endif
893
895 /* Wrap. */
896 BLF_doc,
897 "This module provides access to Blender's text drawing functions.");
898static PyModuleDef BLF_module_def = {
899 /*m_base*/ PyModuleDef_HEAD_INIT,
900 /*m_name*/ "blf",
901 /*m_doc*/ BLF_doc,
902 /*m_size*/ 0,
903 /*m_methods*/ BLF_methods,
904 /*m_slots*/ nullptr,
905 /*m_traverse*/ nullptr,
906 /*m_clear*/ nullptr,
907 /*m_free*/ nullptr,
908};
909
910/* -------------------------------------------------------------------- */
913
915 /* Wrap. */
916 BLF_types_doc,
917 "This module provides access to font drawing types.");
918static PyModuleDef BLF_types_module_def = {
919 /*m_base*/ PyModuleDef_HEAD_INIT,
920 /*m_name*/ "blf.types",
921 /*m_doc*/ BLF_types_doc,
922 /*m_size*/ 0,
923 /*m_methods*/ nullptr,
924 /*m_slots*/ nullptr,
925 /*m_traverse*/ nullptr,
926 /*m_clear*/ nullptr,
927 /*m_free*/ nullptr,
928};
929
930static PyObject *BPyInit_blf_types()
931{
932 if (PyType_Ready(&BPyBLFImBufContext_Type) < 0) {
933 return nullptr;
934 }
935
936 PyObject *submodule = PyModule_Create(&BLF_types_module_def);
937
938 PyModule_AddType(submodule, &BPyBLFImBufContext_Type);
939
940 return submodule;
941}
942
944
945PyObject *BPyInit_blf()
946{
947 PyObject *mod;
948 PyObject *submodule;
949 PyObject *sys_modules = PyImport_GetModuleDict();
950
951 mod = PyModule_Create(&BLF_module_def);
952
953 PyModule_AddIntConstant(mod, "ROTATION", BLF_ROTATION);
954 PyModule_AddIntConstant(mod, "CLIPPING", BLF_CLIPPING);
955 PyModule_AddIntConstant(mod, "SHADOW", BLF_SHADOW);
956 PyModule_AddIntConstant(mod, "WORD_WRAP", BLF_WORD_WRAP);
957 PyModule_AddIntConstant(mod, "MONOCHROME", BLF_MONOCHROME);
958 PyModule_AddIntConstant(mod, "NO_FALLBACK", BLF_NO_FALLBACK);
959
960 /* `blf.types` */
961 PyModule_AddObject(mod, "types", (submodule = BPyInit_blf_types()));
962 PyC_Module_AddToSysModules(sys_modules, submodule);
963
964 return mod;
965}
966
967} // namespace blender
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define INT_MAX
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
void BLF_shadow_offset(int fontid, int x, int y)
Definition blf.cc:979
void BLF_aspect(int fontid, float x, float y, float z)
Definition blf.cc:379
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
static PyObject * py_blf_dimensions(PyObject *, PyObject *args)
static PyObject * py_blf_draw_buffer(PyObject *, PyObject *args)
static PyObject * py_blf_draw(PyObject *, PyObject *args)
unsigned int uint
Definition BKE_text.h:55
void BLF_shadow(int fontid, FontShadowType type, const float rgba[4]=nullptr)
Definition blf.cc:967
static int py_blf_bind_imbuf_traverse(BPyBLFImBufContext *self, visitproc visit, void *arg)
static PyMethodDef py_blf_bind_imbuf_methods[]
void BLF_wordwrap(int fontid, int wrap_width, BLFWrapMode mode=BLFWrapMode::Minimal)
Definition blf.cc:957
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int channel_count, const ColorSpace *colorspace)
Definition blf.cc:989
static PyObject * py_blf_load(PyObject *, PyObject *args)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * py_blf_unload(PyObject *, PyObject *args)
static PyObject * py_blf_aspect(PyObject *, PyObject *args)
PyTypeObject Py_ImBuf_Type
static PyObject * py_blf_disable(PyObject *, PyObject *args)
static PyModuleDef BLF_types_module_def
static PyObject * py_blf_word_wrap(PyObject *, PyObject *args)
void BLF_disable(int fontid, FontFlags flag)
Definition blf.cc:331
static PyObject * py_blf_position(PyObject *, PyObject *args)
Definition blf_py_api.cc:60
static PyObject * py_blf_rotation(PyObject *, PyObject *args)
static PyModuleDef BLF_module_def
void BLF_size(int fontid, float size)
Definition blf.cc:445
int BLF_load(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:177
static PyObject * py_blf_bind_imbuf_exit(BPyBLFImBufContext *self, PyObject *)
void BLF_buffer_state_pop(BLFBufferState *buffer_state)
Definition blf.cc:1035
static int py_blf_bind_imbuf_clear(BPyBLFImBufContext *self)
static PyObject * py_blf_enable(PyObject *, PyObject *args)
void BLF_clipping(int fontid, int xmin, int ymin, int xmax, int ymax)
Definition blf.cc:945
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
static PyObject * py_blf_bind_imbuf_enter(BPyBLFImBufContext *self)
int void BLF_unload(const char *filepath) ATTR_NONNULL(1)
Definition blf.cc:261
static PyMethodDef BLF_methods[]
static PyObject * py_blf_clipping(PyObject *, PyObject *args)
static PyObject * py_blf_size(PyObject *, PyObject *args)
Definition blf_py_api.cc:96
void BLF_draw(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:587
static PyObject * py_blf_bind_imbuf(PyObject *, PyObject *args, PyObject *kwds)
BLFBufferState * BLF_buffer_state_push(int fontid)
Definition blf.cc:1022
static PyObject * py_blf_color(PyObject *, PyObject *args)
void BLF_buffer_col(int fontid, const float srgb_color[4]) ATTR_NONNULL(2)
Definition blf.cc:1057
PyObject * BPyInit_blf()
static PyObject * py_blf_shadow(PyObject *, PyObject *args)
int PyC_Module_AddToSysModules(PyObject *sys_modules, PyObject *module)
static void py_blf_bind_imbuf_dealloc(BPyBLFImBufContext *self)
void BLF_draw_buffer(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:1087
void BLF_buffer_state_free(BLFBufferState *buffer_state)
Definition blf.cc:1052
static PyObject * BPyInit_blf_types()
static PyObject * py_blf_shadow_offset(PyObject *, PyObject *args)
static PyTypeObject BPyBLFImBufContext_Type
void BLF_width_and_height(int fontid, const char *str, size_t str_len, float *r_width, float *r_height) ATTR_NONNULL()
Definition blf.cc:822
void BLF_rotation(int fontid, float angle)
Definition blf.cc:936
ImBuf * BPy_ImBuf_FromPyObject(PyObject *py_imbuf)
void BLF_enable(int fontid, FontFlags flag)
Definition blf.cc:322
@ BLF_MONOCHROME
Definition BLF_enums.hh:44
@ BLF_SHADOW
Definition BLF_enums.hh:38
@ BLF_CLIPPING
Definition BLF_enums.hh:37
@ BLF_ROTATION
Definition BLF_enums.hh:36
@ BLF_NO_FALLBACK
Definition BLF_enums.hh:66
@ BLF_WORD_WRAP
Definition BLF_enums.hh:42
void BLF_color4fv(int fontid, const float rgba[4])
Definition blf.cc:507
header-only compatibility defines.
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
BLFBufferState * buffer_state
Definition blf_py_api.cc:41
PyObject_HEAD PyObject * py_imbuf
Definition blf_py_api.cc:38
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
float * float_data_for_write()
Definition allocimbuf.cc:79