Blender V5.3
editfont.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cerrno>
11#include <cstdlib>
12#include <cstring>
13#include <cwchar>
14#include <fcntl.h>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_fileops.hh"
19#include "BLI_listbase.hh"
20#include "BLI_math_geom_c.hh"
21#include "BLI_math_matrix_c.hh"
22#include "BLI_math_vector_c.hh"
23#include "BLI_path_utils.hh"
24#include "BLI_string.hh"
26#include "BLI_utildefines.hh"
27
28#include "DNA_curve_types.h"
29#include "DNA_object_types.h"
30#include "DNA_scene_types.h"
31#include "DNA_text_types.h"
32#include "DNA_vfont_types.h"
33
34#include "BKE_context.hh"
35#include "BKE_curve.hh"
36#include "BKE_global.hh"
37#include "BKE_layer.hh"
38#include "BKE_lib_id.hh"
39#include "BKE_library.hh"
40#include "BKE_main.hh"
41#include "BKE_object.hh"
42#include "BKE_report.hh"
43#include "BKE_vfont.hh"
44
45#include "BLI_string_utf8.hh"
46
47#include "BLT_translation.hh"
48
49#include "DEG_depsgraph.hh"
51
52#include "RNA_access.hh"
53#include "RNA_define.hh"
54
55#include "WM_api.hh"
56#include "WM_types.hh"
57
58#include "ED_curve.hh"
59#include "ED_object.hh"
60#include "ED_outliner.hh"
61#include "ED_screen.hh"
62#include "ED_view3d.hh"
63
64#include "UI_interface.hh"
66
67#include "curve_intern.hh"
68
69namespace blender {
70
71#define MAXTEXT 32766
72
73static int kill_selection(Object *obedit, int ins);
74static char *font_select_to_buffer(Object *obedit);
75
76/* -------------------------------------------------------------------- */
79
80static char32_t findaccent(char32_t char1, const char code)
81{
82 char32_t new_char = 0;
83
84 if (char1 == 'a') {
85 if (code == '`') {
86 new_char = 224;
87 }
88 else if (code == 39) {
89 new_char = 225;
90 }
91 else if (code == '^') {
92 new_char = 226;
93 }
94 else if (code == '~') {
95 new_char = 227;
96 }
97 else if (code == '"') {
98 new_char = 228;
99 }
100 else if (code == 'o') {
101 new_char = 229;
102 }
103 else if (code == 'e') {
104 new_char = 230;
105 }
106 else if (code == '-') {
107 new_char = 170;
108 }
109 }
110 else if (char1 == 'c') {
111 if (code == ',') {
112 new_char = 231;
113 }
114 else if (code == '|') {
115 new_char = 162;
116 }
117 else if (code == 'o') {
118 new_char = 169;
119 }
120 }
121 else if (char1 == 'e') {
122 if (code == '`') {
123 new_char = 232;
124 }
125 else if (code == 39) {
126 new_char = 233;
127 }
128 else if (code == '^') {
129 new_char = 234;
130 }
131 else if (code == '"') {
132 new_char = 235;
133 }
134 }
135 else if (char1 == 'i') {
136 if (code == '`') {
137 new_char = 236;
138 }
139 else if (code == 39) {
140 new_char = 237;
141 }
142 else if (code == '^') {
143 new_char = 238;
144 }
145 else if (code == '"') {
146 new_char = 239;
147 }
148 }
149 else if (char1 == 'n') {
150 if (code == '~') {
151 new_char = 241;
152 }
153 }
154 else if (char1 == 'o') {
155 if (code == '`') {
156 new_char = 242;
157 }
158 else if (code == 39) {
159 new_char = 243;
160 }
161 else if (code == '^') {
162 new_char = 244;
163 }
164 else if (code == '~') {
165 new_char = 245;
166 }
167 else if (code == '"') {
168 new_char = 246;
169 }
170 else if (code == '/') {
171 new_char = 248;
172 }
173 else if (code == '-') {
174 new_char = 186;
175 }
176 else if (code == 'e') {
177 new_char = 339;
178 }
179 else if (code == 'c') {
180 new_char = 169;
181 }
182 else if (code == 'r') {
183 new_char = 174;
184 }
185 }
186 else if (char1 == 'r') {
187 if (code == 'o') {
188 new_char = 174;
189 }
190 }
191 else if (char1 == 's') {
192 if (code == 's') {
193 new_char = 167;
194 }
195 }
196 else if (char1 == 't') {
197 if (code == 'm') {
198 new_char = 8482;
199 }
200 }
201 else if (char1 == 'u') {
202 if (code == '`') {
203 new_char = 249;
204 }
205 else if (code == 39) {
206 new_char = 250;
207 }
208 else if (code == '^') {
209 new_char = 251;
210 }
211 else if (code == '"') {
212 new_char = 252;
213 }
214 }
215 else if (char1 == 'y') {
216 if (code == 39) {
217 new_char = 253;
218 }
219 else if (code == '"') {
220 new_char = 255;
221 }
222 }
223 else if (char1 == 'A') {
224 if (code == '`') {
225 new_char = 192;
226 }
227 else if (code == 39) {
228 new_char = 193;
229 }
230 else if (code == '^') {
231 new_char = 194;
232 }
233 else if (code == '~') {
234 new_char = 195;
235 }
236 else if (code == '"') {
237 new_char = 196;
238 }
239 else if (code == 'o') {
240 new_char = 197;
241 }
242 else if (code == 'e') {
243 new_char = 198;
244 }
245 }
246 else if (char1 == 'C') {
247 if (code == ',') {
248 new_char = 199;
249 }
250 }
251 else if (char1 == 'E') {
252 if (code == '`') {
253 new_char = 200;
254 }
255 else if (code == 39) {
256 new_char = 201;
257 }
258 else if (code == '^') {
259 new_char = 202;
260 }
261 else if (code == '"') {
262 new_char = 203;
263 }
264 }
265 else if (char1 == 'I') {
266 if (code == '`') {
267 new_char = 204;
268 }
269 else if (code == 39) {
270 new_char = 205;
271 }
272 else if (code == '^') {
273 new_char = 206;
274 }
275 else if (code == '"') {
276 new_char = 207;
277 }
278 }
279 else if (char1 == 'N') {
280 if (code == '~') {
281 new_char = 209;
282 }
283 }
284 else if (char1 == 'O') {
285 if (code == '`') {
286 new_char = 210;
287 }
288 else if (code == 39) {
289 new_char = 211;
290 }
291 else if (code == '^') {
292 new_char = 212;
293 }
294 else if (code == '~') {
295 new_char = 213;
296 }
297 else if (code == '"') {
298 new_char = 214;
299 }
300 else if (code == '/') {
301 new_char = 216;
302 }
303 else if (code == 'e') {
304 new_char = 141;
305 }
306 }
307 else if (char1 == 'U') {
308 if (code == '`') {
309 new_char = 217;
310 }
311 else if (code == 39) {
312 new_char = 218;
313 }
314 else if (code == '^') {
315 new_char = 219;
316 }
317 else if (code == '"') {
318 new_char = 220;
319 }
320 }
321 else if (char1 == 'Y') {
322 if (code == 39) {
323 new_char = 221;
324 }
325 }
326 else if (char1 == '1') {
327 if (code == '4') {
328 new_char = 188;
329 }
330 if (code == '2') {
331 new_char = 189;
332 }
333 }
334 else if (char1 == '3') {
335 if (code == '4') {
336 new_char = 190;
337 }
338 }
339 else if (char1 == ':') {
340 if (code == '-') {
341 new_char = 247;
342 }
343 }
344 else if (char1 == '-') {
345 if (code == ':') {
346 new_char = 247;
347 }
348 if (code == '|') {
349 new_char = 8224;
350 }
351 if (code == '+') {
352 new_char = 177;
353 }
354 }
355 else if (char1 == '|') {
356 if (code == '-') {
357 new_char = 8224;
358 }
359 if (code == '=') {
360 new_char = 8225;
361 }
362 }
363 else if (char1 == '=') {
364 if (code == '|') {
365 new_char = 8225;
366 }
367 }
368 else if (char1 == '+') {
369 if (code == '-') {
370 new_char = 177;
371 }
372 }
373
374 if (new_char) {
375 return new_char;
376 }
377 return char1;
378}
379
381{
382 Curve *cu = id_cast<Curve *>(obedit->data);
383 EditFont *ef = cu->editfont;
384
385 if (ef->len < MAXTEXT - 1) {
386 int x;
387
388 for (x = ef->len; x > ef->pos; x--) {
389 ef->textbuf[x] = ef->textbuf[x - 1];
390 }
391 for (x = ef->len; x > ef->pos; x--) {
392 ef->textbufinfo[x] = ef->textbufinfo[x - 1];
393 }
394 ef->textbuf[ef->pos] = c;
395 ef->textbufinfo[ef->pos] = cu->curinfo;
396 ef->textbufinfo[ef->pos].kern = 0.0f;
397 ef->textbufinfo[ef->pos].mat_nr = obedit->actcol - 1;
398
399 ef->pos++;
400 ef->len++;
401 ef->textbuf[ef->len] = '\0';
402
403 return 1;
404 }
405 return 0;
406}
407
408static void text_update_edited(bContext *C, Object *obedit, const eEditFontMode mode)
409{
410 Curve *cu = id_cast<Curve *>(obedit->data);
411 EditFont *ef = cu->editfont;
412
413 BLI_assert(ef->len >= 0);
414
415 /* Run update first since it can move the cursor. */
416 if (mode == FO_EDIT) {
417 /* Re-tessellate. */
419 }
420 else {
421 /* Depsgraph runs above, but since we're not tagging for update, call directly. */
422 /* We need evaluated data here. */
423 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
424 BKE_vfont_to_curve(DEG_get_evaluated(depsgraph, obedit), mode);
425 }
426
427 cu->curinfo = ef->textbufinfo[ef->pos ? ef->pos - 1 : 0];
428
430
433}
434
435static int kill_selection(Object *obedit, int ins) /* ins == new character len */
436{
437 Curve *cu = id_cast<Curve *>(obedit->data);
438 EditFont *ef = cu->editfont;
439 int selend, selstart, direction;
440 int getfrom;
441
442 direction = BKE_vfont_select_get(cu, &selstart, &selend);
443 if (direction) {
444 int size;
445 if (ef->pos >= selstart) {
446 ef->pos = selstart + ins;
447 }
448 if ((direction == -1) && ins) {
449 selstart += ins;
450 selend += ins;
451 }
452 getfrom = selend + 1;
453 size = ef->len - selend; /* This is equivalent to: `(ef->len - getfrom) + 1(null)`. */
454 memmove(ef->textbuf + selstart, ef->textbuf + getfrom, sizeof(*ef->textbuf) * size);
455 memmove(ef->textbufinfo + selstart, ef->textbufinfo + getfrom, sizeof(CharInfo) * size);
456 ef->len -= ((selend - selstart) + 1);
457 ef->selstart = ef->selend = 0;
458 }
459
460 return direction;
461}
462
464{
465 if (G.background) {
466 return;
467 }
468
470 return;
471 }
472 char *buf = font_select_to_buffer(obedit);
473 if (buf == nullptr) {
474 return;
475 }
476 WM_clipboard_text_set(buf, true);
477 MEM_delete(buf);
478}
479
481
482/* -------------------------------------------------------------------- */
485
486static bool font_paste_wchar(Object *obedit,
487 const char32_t *str,
488 const size_t str_len,
489 /* Optional. */
490 const CharInfo *str_info)
491{
492 Curve *cu = id_cast<Curve *>(obedit->data);
493 EditFont *ef = cu->editfont;
494 int selend, selstart;
495
496 if (BKE_vfont_select_get(cu, &selstart, &selend) == 0) {
497 selstart = selend = 0;
498 }
499
500 /* Verify that the copy buffer => [copy buffer len] + ef->len < MAXTEXT */
501 if ((ef->len + str_len) - (selend - selstart) <= MAXTEXT) {
502
503 kill_selection(obedit, 0);
504
505 if (str_len) {
506 int size = (ef->len * sizeof(*ef->textbuf)) - (ef->pos * sizeof(*ef->textbuf)) +
507 sizeof(*ef->textbuf);
508 memmove(ef->textbuf + ef->pos + str_len, ef->textbuf + ef->pos, size);
509 memcpy(ef->textbuf + ef->pos, str, str_len * sizeof(*ef->textbuf));
510
511 memmove(ef->textbufinfo + ef->pos + str_len,
512 ef->textbufinfo + ef->pos,
513 (ef->len - ef->pos + 1) * sizeof(CharInfo));
514 if (str_info) {
515 const short mat_nr_max = std::max(0, obedit->totcol - 1);
516 const CharInfo *info_src = str_info;
517 CharInfo *info_dst = ef->textbufinfo + ef->pos;
518
519 for (int i = 0; i < str_len; i++, info_src++, info_dst++) {
520 *info_dst = *info_src;
521 CLAMP_MAX(info_dst->mat_nr, mat_nr_max);
522 }
523 }
524 else {
525 std::fill_n(ef->textbufinfo + ef->pos, str_len, CharInfo{});
526 }
527
528 ef->len += str_len;
529 ef->pos += str_len;
530 }
531
532 return true;
533 }
534
535 return false;
536}
537
538static bool font_paste_utf8(bContext *C, const char *str, const size_t str_len)
539{
540 Object *obedit = CTX_data_edit_object(C);
541 bool retval;
542
543 int tmplen;
544
545 char32_t *mem = MEM_new_array_uninitialized<char32_t>(str_len + 1, __func__);
546
547 tmplen = BLI_str_utf8_as_utf32(mem, str, str_len + 1);
548
549 retval = font_paste_wchar(obedit, mem, tmplen, nullptr);
550
551 MEM_delete(mem);
552
553 return retval;
554}
555
557
558/* -------------------------------------------------------------------- */
561
562static char *font_select_to_buffer(Object *obedit)
563{
564 Curve *cu = id_cast<Curve *>(obedit->data);
565 int selstart, selend;
566 if (!BKE_vfont_select_get(cu, &selstart, &selend)) {
567 return nullptr;
568 }
569 EditFont *ef = cu->editfont;
570 const char32_t *text_buf = ef->textbuf + selstart;
571 const size_t text_buf_len = selend - selstart;
572
573 const size_t len_utf8 = BLI_str_utf32_as_utf8_len_ex(text_buf, text_buf_len + 1);
574 char *buf = MEM_new_array_uninitialized<char>(len_utf8 + 1, __func__);
575 BLI_str_utf32_as_utf8(buf, text_buf, len_utf8 + 1);
576 return buf;
577}
578
580
581/* -------------------------------------------------------------------- */
584
585static wmOperatorStatus paste_from_file(bContext *C, ReportList *reports, const char *filepath)
586{
587 Object *obedit = CTX_data_edit_object(C);
588 char *strp;
589 size_t filelen;
590 wmOperatorStatus retval;
591
592 strp = BLI_file_read_text_as_mem(filepath, 1, &filelen);
593 if (strp == nullptr) {
594 BKE_reportf(reports, RPT_ERROR, "Failed to open file '%s'", filepath);
595 return OPERATOR_CANCELLED;
596 }
597 strp[filelen] = 0;
598
599 if (font_paste_utf8(C, strp, filelen)) {
600 text_update_edited(C, obedit, FO_EDIT);
601 retval = OPERATOR_FINISHED;
602 }
603 else {
604 BKE_reportf(reports, RPT_ERROR, "File too long %s", filepath);
605 retval = OPERATOR_CANCELLED;
606 }
607
608 MEM_delete(strp);
609
610 return retval;
611}
612
614{
615 if (op->flag & OP_IS_INVOKE) {
617 return OPERATOR_CANCELLED;
618 }
619 }
620
621 std::string filepath = RNA_string_get(op->ptr, "filepath");
622 wmOperatorStatus retval = paste_from_file(C, op->reports, filepath.c_str());
623 return retval;
624}
625
627 wmOperator *op,
628 const wmEvent * /*event*/)
629{
630 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
631 return paste_from_file_exec(C, op);
632 }
633
635
637}
638
640{
641 /* identifiers */
642 ot->name = "Paste File";
643 ot->description = "Paste contents from file";
644 ot->idname = "FONT_OT_text_paste_from_file";
645
646 /* API callbacks. */
650
651 /* flags */
653
654 /* properties */
662}
663
665
666/* -------------------------------------------------------------------- */
669
670static void text_insert_unicode_cancel(bContext *C, void *arg_block, void * /*arg2*/)
671{
672 ui::Block *block = static_cast<ui::Block *>(arg_block);
673 popup_block_close(C, CTX_wm_window(C), block);
674}
675
676static void text_insert_unicode_confirm(bContext *C, void *arg_block, void *arg_string)
677{
678 ui::Block *block = static_cast<ui::Block *>(arg_block);
679 char *edit_string = static_cast<char *>(arg_string);
680
681 if (edit_string[0] == 0) {
682 /* Blank text is probably purposeful closure. */
683 popup_block_close(C, CTX_wm_window(C), block);
684 return;
685 }
686
687 uint val = strtoul(edit_string, nullptr, 16);
688 if (val > 31 && val < 0x10FFFF) {
689 Object *obedit = CTX_data_edit_object(C);
690 if (obedit) {
691 const char32_t utf32[2] = {val, 0};
692 font_paste_wchar(obedit, utf32, 1, nullptr);
693 text_update_edited(C, obedit, FO_EDIT);
694 }
695 popup_block_close(C, CTX_wm_window(C), block);
696 }
697 else {
698 /* Invalid. Clear text and keep dialog open. */
699 edit_string[0] = 0;
700 }
701}
702
703static ui::Block *wm_block_insert_unicode_create(bContext *C, ARegion *region, void *arg_string)
704{
705 ui::Block *block = block_begin(C, region, __func__, ui::EmbossType::Emboss);
706 char *edit_string = static_cast<char *>(arg_string);
707
710 const uiStyle *style = ui::style_get_dpi();
711 ui::Layout &layout = ui::block_layout(block,
714 0,
715 0,
716 200 * UI_SCALE_FAC,
717 UI_UNIT_Y,
718 0,
719 style);
720
721 uiItemL_ex(&layout, IFACE_("Insert Unicode Character"), ICON_NONE, true, false);
722 layout.label(RPT_("Enter a Unicode codepoint hex value"), ICON_NONE);
723
724 ui::Button *text_but = uiDefBut(block,
726 "",
727 0,
728 0,
729 100,
730 UI_UNIT_Y,
731 edit_string,
732 0,
733 7,
734 TIP_("Unicode codepoint hex value"));
735 button_flag_enable(text_but, ui::BUT_ACTIVATE_ON_INIT);
736 /* Hitting Enter in the text input is treated the same as clicking the Confirm button. */
737 button_func_set(text_but, text_insert_unicode_confirm, block, edit_string);
738
739 layout.separator();
740
741 /* Buttons. */
742
743#ifdef _WIN32
744 const bool windows_layout = true;
745#else
746 const bool windows_layout = false;
747#endif
748
749 ui::Button *confirm = nullptr;
750 ui::Button *cancel = nullptr;
751 ui::Layout &split = layout.split(0.0f, true);
752 split.column(false);
753
754 if (windows_layout) {
755 confirm = uiDefIconTextBut(block,
757 0,
758 IFACE_("Insert"),
759 0,
760 0,
761 0,
762 UI_UNIT_Y,
763 nullptr,
764 std::nullopt);
765 split.column(false);
766 }
767
768 cancel = uiDefIconTextBut(
769 block, ui::ButtonType::But, 0, IFACE_("Cancel"), 0, 0, 0, UI_UNIT_Y, nullptr, std::nullopt);
770
771 if (!windows_layout) {
772 split.column(false);
773 confirm = uiDefIconTextBut(block,
775 0,
776 IFACE_("Insert"),
777 0,
778 0,
779 0,
780 UI_UNIT_Y,
781 nullptr,
782 std::nullopt);
783 }
784
785 block_func_set(block, nullptr, nullptr, nullptr);
786 button_func_set(confirm, text_insert_unicode_confirm, block, edit_string);
787 button_func_set(cancel, text_insert_unicode_cancel, block, nullptr);
788 button_drawflag_disable(confirm, ui::BUT_TEXT_LEFT);
789 button_drawflag_disable(cancel, ui::BUT_TEXT_LEFT);
790 button_flag_enable(confirm, ui::BUT_ACTIVE_DEFAULT);
791
792 int bounds_offset[2];
793 bounds_offset[0] = layout.width() * -0.2f;
794 bounds_offset[1] = UI_UNIT_Y * 2.5;
795 block_bounds_set_popup(block, 7 * UI_SCALE_FAC, bounds_offset);
796
797 return block;
798}
799
801 wmOperator * /*op*/,
802 const wmEvent * /*event*/)
803{
804 char *edit_string = MEM_new_array_uninitialized<char>(24, __func__);
805 edit_string[0] = 0;
806 popup_block_invoke_ex(C, wm_block_insert_unicode_create, edit_string, MEM_delete_void, false);
807 return OPERATOR_FINISHED;
808}
809
811{
812 /* identifiers */
813 ot->name = "Insert Unicode";
814 ot->description = "Insert Unicode Character";
815 ot->idname = "FONT_OT_text_insert_unicode";
816
817 /* API callbacks. */
820
821 /* flags */
823}
824
826
827/* -------------------------------------------------------------------- */
830
832 const TextLine *firstline,
833 int totline,
834 const float offset[3])
835{
836 Main *bmain = CTX_data_main(C);
837 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
838 Scene *scene = CTX_data_scene(C);
839 ViewLayer *view_layer = CTX_data_view_layer(C);
840 Curve *cu;
841 Object *obedit;
842 Object *object;
843 const TextLine *tmp;
844 int nchars = 0, nbytes = 0;
845 char *s;
846 int a;
847 const float rot[3] = {0.0f, 0.0f, 0.0f};
848
849 obedit = BKE_object_add(bmain, scene, view_layer, OB_FONT, nullptr);
850 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
851 object = BKE_view_layer_active_object_get(view_layer);
852
853 /* seems to assume view align ? TODO: look into this, could be an operator option. */
854 ed::object::init_transform_on_add(object, nullptr, rot);
855
856 BKE_object_where_is_calc(depsgraph, scene, obedit);
857
858 add_v3_v3(obedit->loc, offset);
859
860 cu = id_cast<Curve *>(obedit->data);
862 id_us_plus(&cu->vfont->id);
863
864 for (tmp = firstline, a = 0; nbytes < MAXTEXT && a < totline; tmp = tmp->next, a++) {
865 size_t nchars_line, nbytes_line;
866 nchars_line = BLI_strlen_utf8_ex(tmp->line, &nbytes_line);
867 nchars += nchars_line + 1;
868 nbytes += nbytes_line + 1;
869 }
870
871 if (cu->str) {
872 MEM_delete(cu->str);
873 }
874 if (cu->strinfo) {
875 MEM_delete(cu->strinfo);
876 }
877
878 cu->str = MEM_new_array_uninitialized<char>(nbytes + 4, "str");
879 cu->strinfo = MEM_new_array<CharInfo>((nchars + 4), "strinfo");
880
881 cu->len = 0;
882 cu->len_char32 = nchars - 1;
883 cu->pos = 0;
884
885 s = cu->str;
886
887 for (tmp = firstline, a = 0; cu->len < MAXTEXT && a < totline; tmp = tmp->next, a++) {
888 size_t nchars_line_dummy, nbytes_line;
889 nchars_line_dummy = BLI_strlen_utf8_ex(tmp->line, &nbytes_line);
890 (void)nchars_line_dummy;
891
892 memcpy(s, tmp->line, nbytes_line);
893 s += nbytes_line;
894 cu->len += nbytes_line;
895
896 if (tmp->next) {
897 *s = '\n';
898 s += 1;
899 cu->len += 1;
900 }
901 }
902
903 cu->pos = cu->len_char32;
904 *s = '\0';
905
907}
908
909void ED_text_to_object(bContext *C, const Text *text, const bool split_lines)
910{
911 Main *bmain = CTX_data_main(C);
913 float offset[3];
914 int linenum = 0;
915
916 if (!text || !text->lines.first_) {
917 return;
918 }
919
920 if (split_lines) {
921 for (const TextLine &line : text->lines) {
922 /* skip lines with no text, but still make space for them */
923 if (line.line[0] == '\0') {
924 linenum++;
925 continue;
926 }
927
928 /* do the translation */
929 offset[0] = 0;
930 offset[1] = -linenum;
931 offset[2] = 0;
932
933 if (rv3d) {
934 mul_mat3_m4_v3(rv3d->viewinv, offset);
935 }
936
937 txt_add_object(C, &line, 1, offset);
938
939 linenum++;
940 }
941 }
942 else {
943 offset[0] = 0.0f;
944 offset[1] = 0.0f;
945 offset[2] = 0.0f;
946
947 txt_add_object(C, text->lines.first(), text->lines.count(), offset);
948 }
949
952}
953
955
956/* -------------------------------------------------------------------- */
959
961 {CU_CHINFO_BOLD, "BOLD", 0, "Bold", ""},
962 {CU_CHINFO_ITALIC, "ITALIC", 0, "Italic", ""},
963 {CU_CHINFO_UNDERLINE, "UNDERLINE", 0, "Underline", ""},
964 {CU_CHINFO_SMALLCAPS, "SMALL_CAPS", 0, "Small Caps", ""},
965 {0, nullptr, 0, nullptr, nullptr},
966};
967
968static wmOperatorStatus set_style(bContext *C, const eCharInfoFlag style, const bool clear)
969{
970 Object *obedit = CTX_data_edit_object(C);
971 Curve *cu = id_cast<Curve *>(obedit->data);
972 EditFont *ef = cu->editfont;
973 int i, selstart, selend;
974
975 if (!BKE_vfont_select_get(cu, &selstart, &selend)) {
976 return OPERATOR_CANCELLED;
977 }
978
979 for (i = selstart; i <= selend; i++) {
980 if (clear) {
981 ef->textbufinfo[i].flag &= ~style;
982 }
983 else {
984 ef->textbufinfo[i].flag |= style;
985 }
986 }
987
990
991 return OPERATOR_FINISHED;
992}
993
995{
996 const eCharInfoFlag style = eCharInfoFlag(RNA_enum_get(op->ptr, "style"));
997 const bool clear = RNA_boolean_get(op->ptr, "clear");
998
999 return set_style(C, style, clear);
1000}
1001
1003{
1004 /* identifiers */
1005 ot->name = "Set Style";
1006 ot->description = "Set font style";
1007 ot->idname = "FONT_OT_style_set";
1008
1009 /* API callbacks. */
1010 ot->exec = set_style_exec;
1012
1013 /* flags */
1015
1016 /* properties */
1018 ot->srna, "style", style_items, CU_CHINFO_BOLD, "Style", "Style to set selection to");
1019 RNA_def_boolean(ot->srna, "clear", false, "Clear", "Clear style rather than setting it");
1020}
1021
1023
1024/* -------------------------------------------------------------------- */
1027
1029{
1030 Object *obedit = CTX_data_edit_object(C);
1031 Curve *cu = id_cast<Curve *>(obedit->data);
1032 int clear, selstart, selend;
1033
1034 const eCharInfoFlag style = eCharInfoFlag(RNA_enum_get(op->ptr, "style"));
1035 cu->curinfo.flag ^= style;
1036 if (BKE_vfont_select_get(cu, &selstart, &selend)) {
1037 clear = (cu->curinfo.flag & style) == eCharInfoFlag{};
1038 return set_style(C, style, clear);
1039 }
1040 return OPERATOR_CANCELLED;
1041}
1042
1044{
1045 /* identifiers */
1046 ot->name = "Toggle Style";
1047 ot->description = "Toggle font style";
1048 ot->idname = "FONT_OT_style_toggle";
1049
1050 /* API callbacks. */
1051 ot->exec = toggle_style_exec;
1053
1054 /* flags */
1056
1057 /* properties */
1059 ot->srna, "style", style_items, CU_CHINFO_BOLD, "Style", "Style to set selection to");
1060}
1061
1063
1064/* -------------------------------------------------------------------- */
1067
1069{
1070 Object *obedit = CTX_data_edit_object(C);
1071 Curve *cu = id_cast<Curve *>(obedit->data);
1072 EditFont *ef = cu->editfont;
1073
1074 if (ef->len) {
1075 ef->selstart = 1;
1076 ef->selend = ef->len;
1077 ef->pos = ef->len;
1078
1081
1082 return OPERATOR_FINISHED;
1083 }
1084 return OPERATOR_CANCELLED;
1085}
1086
1088{
1089 /* identifiers */
1090 ot->name = "Select All";
1091 ot->description = "Select all text";
1092 ot->idname = "FONT_OT_select_all";
1093
1094 /* API callbacks. */
1097
1098 /* flags */
1100}
1101
1103
1104/* -------------------------------------------------------------------- */
1107
1108static void copy_selection(Object *obedit)
1109{
1110 Curve *cu = id_cast<Curve *>(obedit->data);
1111 int selstart, selend;
1112
1113 if (BKE_vfont_select_get(cu, &selstart, &selend)) {
1114 EditFont *ef = cu->editfont;
1115 char *buf = nullptr;
1116 char32_t *text_buf;
1117 size_t len_utf8;
1118
1119 /* internal clipboard (for style) */
1121 ef->textbuf + selstart, ef->textbufinfo + selstart, selend - selstart + 1);
1122 BKE_vfont_clipboard_get(&text_buf, nullptr, &len_utf8, nullptr);
1123
1124 /* system clipboard */
1125 buf = MEM_new_array_uninitialized<char>(len_utf8 + 1, __func__);
1126 if (buf) {
1127 BLI_str_utf32_as_utf8(buf, text_buf, len_utf8 + 1);
1128 WM_clipboard_text_set(buf, false);
1129 MEM_delete(buf);
1130 }
1131 }
1132}
1133
1135{
1136 Object *obedit = CTX_data_edit_object(C);
1137
1138 copy_selection(obedit);
1139
1140 return OPERATOR_FINISHED;
1141}
1142
1144{
1145 /* identifiers */
1146 ot->name = "Copy Text";
1147 ot->description = "Copy selected text to clipboard";
1148 ot->idname = "FONT_OT_text_copy";
1149
1150 /* API callbacks. */
1151 ot->exec = copy_text_exec;
1153}
1154
1156
1157/* -------------------------------------------------------------------- */
1160
1162{
1163 Object *obedit = CTX_data_edit_object(C);
1164 Curve *cu = id_cast<Curve *>(obedit->data);
1165 int selstart, selend;
1166
1167 if (!BKE_vfont_select_get(cu, &selstart, &selend)) {
1168 return OPERATOR_CANCELLED;
1169 }
1170
1171 copy_selection(obedit);
1172 kill_selection(obedit, 0);
1173
1174 text_update_edited(C, obedit, FO_EDIT);
1175
1176 return OPERATOR_FINISHED;
1177}
1178
1180{
1181 /* identifiers */
1182 ot->name = "Cut Text";
1183 ot->description = "Cut selected text to clipboard";
1184 ot->idname = "FONT_OT_text_cut";
1185
1186 /* API callbacks. */
1187 ot->exec = cut_text_exec;
1189
1190 /* flags */
1192}
1193
1195
1196/* -------------------------------------------------------------------- */
1199
1200static bool paste_selection(Object *obedit, ReportList *reports)
1201{
1202 char32_t *text_buf;
1203 CharInfo *info_buf;
1204 size_t len;
1205
1206 BKE_vfont_clipboard_get(&text_buf, &info_buf, nullptr, &len);
1207
1208 if (font_paste_wchar(obedit, text_buf, len, info_buf)) {
1209 return true;
1210 }
1211
1212 BKE_report(reports, RPT_WARNING, "Text too long");
1213 return false;
1214}
1215
1217{
1218 const bool selection = RNA_boolean_get(op->ptr, "selection");
1219 Object *obedit = CTX_data_edit_object(C);
1220 wmOperatorStatus retval;
1221 size_t len_utf8;
1222 char32_t *text_buf;
1223
1224 /* Store both clipboards as UTF8 for comparison,
1225 * Give priority to the internal `vfont` clipboard with its #CharInfo text styles
1226 * as long as its synchronized with the systems clipboard. */
1227 struct {
1228 char *buf;
1229 int len;
1230 } clipboard_system = {nullptr}, clipboard_vfont = {nullptr};
1231
1232 /* No need for UTF8 validation as the conversion handles invalid sequences gracefully. */
1233 clipboard_system.buf = WM_clipboard_text_get(selection, false, &clipboard_system.len);
1234
1235 if (clipboard_system.buf == nullptr) {
1236 return OPERATOR_CANCELLED;
1237 }
1238
1239 BKE_vfont_clipboard_get(&text_buf, nullptr, &len_utf8, nullptr);
1240
1241 if (text_buf) {
1242 clipboard_vfont.buf = MEM_new_array_uninitialized<char>(len_utf8 + 1, __func__);
1243
1244 if (clipboard_vfont.buf == nullptr) {
1245 MEM_delete(clipboard_system.buf);
1246 return OPERATOR_CANCELLED;
1247 }
1248
1249 BLI_str_utf32_as_utf8(clipboard_vfont.buf, text_buf, len_utf8 + 1);
1250 }
1251
1252 if (clipboard_vfont.buf && STREQ(clipboard_vfont.buf, clipboard_system.buf)) {
1254 }
1255 else {
1256 if ((clipboard_system.len <= MAXTEXT) &&
1257 font_paste_utf8(C, clipboard_system.buf, clipboard_system.len))
1258 {
1259 text_update_edited(C, obedit, FO_EDIT);
1260 retval = OPERATOR_FINISHED;
1261 }
1262 else {
1263 BKE_report(op->reports, RPT_ERROR, "Clipboard too long");
1264 retval = OPERATOR_CANCELLED;
1265 }
1266
1267 /* free the existent clipboard buffer */
1269 }
1270
1271 if (retval != OPERATOR_CANCELLED) {
1272 text_update_edited(C, obedit, FO_EDIT);
1273 }
1274
1275 /* cleanup */
1276 if (clipboard_vfont.buf) {
1277 MEM_delete(clipboard_vfont.buf);
1278 }
1279
1280 MEM_delete(clipboard_system.buf);
1281
1282 return retval;
1283}
1284
1286{
1287 /* identifiers */
1288 ot->name = "Paste Text";
1289 ot->description = "Paste text from clipboard";
1290 ot->idname = "FONT_OT_text_paste";
1291
1292 /* API callbacks. */
1293 ot->exec = paste_text_exec;
1295
1296 /* flags */
1298
1299 /* properties */
1300 PropertyRNA *prop;
1301 prop = RNA_def_boolean(ot->srna,
1302 "selection",
1303 false,
1304 "Selection",
1305 "Paste text selected elsewhere rather than copied (X11/Wayland only)");
1307}
1308
1310
1311/* -------------------------------------------------------------------- */
1314
1316 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
1317 {LINE_END, "LINE_END", 0, "Line End", ""},
1318 {TEXT_BEGIN, "TEXT_BEGIN", 0, "Text Begin", ""},
1319 {TEXT_END, "TEXT_END", 0, "Text End", ""},
1320 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
1321 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
1322 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
1323 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
1324 {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""},
1325 {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""},
1326 {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""},
1327 {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""},
1328 {0, nullptr, 0, nullptr, nullptr},
1329};
1330
1336static bool move_cursor_drop_select(Object *obedit, int dir)
1337{
1338 Curve *cu = id_cast<Curve *>(obedit->data);
1339 int selstart, selend;
1340 if (!BKE_vfont_select_get(cu, &selstart, &selend)) {
1341 return false;
1342 }
1343
1344 EditFont *ef = cu->editfont;
1345 if (dir == -1) {
1346 ef->pos = selstart;
1347 }
1348 else if (dir == 1) {
1349 ef->pos = selend + 1;
1350 }
1351 else {
1353 }
1354
1355 /* The caller must clear the selection. */
1356 return true;
1357}
1358
1359static wmOperatorStatus move_cursor(bContext *C, int type, const bool select)
1360{
1361 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1362 Object *obedit = CTX_data_edit_object(C);
1363 Curve *cu = id_cast<Curve *>(obedit->data);
1364 EditFont *ef = cu->editfont;
1365 int cursmove = -1;
1366
1367 if ((select) && (ef->selstart == 0)) {
1368 ef->selstart = ef->selend = ef->pos + 1;
1369 }
1370
1371 switch (type) {
1372 case LINE_BEGIN:
1373 cursmove = FO_LINE_BEGIN;
1374 break;
1375 case LINE_END:
1376 cursmove = FO_LINE_END;
1377 break;
1378
1379 case TEXT_BEGIN:
1380 ef->pos = 0;
1381 cursmove = FO_CURS;
1382 break;
1383
1384 case TEXT_END:
1385 ef->pos = ef->len;
1386 cursmove = FO_CURS;
1387 break;
1388
1389 case PREV_WORD: {
1390 if ((select == false) && move_cursor_drop_select(obedit, -1)) {
1391 cursmove = FO_CURS;
1392 }
1393 else {
1394 int pos = ef->pos;
1396 ef->textbuf, ef->len, &pos, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, true);
1397 ef->pos = pos;
1398 cursmove = FO_CURS;
1399 }
1400 break;
1401 }
1402
1403 case NEXT_WORD: {
1404 if ((select == false) && move_cursor_drop_select(obedit, 1)) {
1405 cursmove = FO_CURS;
1406 }
1407 else {
1408 int pos = ef->pos;
1410 ef->textbuf, ef->len, &pos, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, true);
1411 ef->pos = pos;
1412 cursmove = FO_CURS;
1413 }
1414 break;
1415 }
1416
1417 case PREV_CHAR: {
1418 if ((select == false) && move_cursor_drop_select(obedit, -1)) {
1419 cursmove = FO_CURS;
1420 }
1421 else {
1423 cursmove = FO_CURS;
1424 }
1425 break;
1426 }
1427 case NEXT_CHAR: {
1428 if ((select == false) && move_cursor_drop_select(obedit, 1)) {
1429 cursmove = FO_CURS;
1430 }
1431 else {
1433 cursmove = FO_CURS;
1434 }
1435 break;
1436 }
1437 case PREV_LINE:
1438 cursmove = FO_CURSUP;
1439 break;
1440
1441 case NEXT_LINE:
1442 cursmove = FO_CURSDOWN;
1443 break;
1444
1445 case PREV_PAGE:
1446 cursmove = FO_PAGEUP;
1447 break;
1448
1449 case NEXT_PAGE:
1450 cursmove = FO_PAGEDOWN;
1451 break;
1452 }
1453
1454 if (cursmove == -1) {
1455 return OPERATOR_CANCELLED;
1456 }
1457
1458 if (ef->pos > ef->len) {
1459 ef->pos = ef->len;
1460 }
1461 else if (ef->pos >= MAXTEXT) {
1462 ef->pos = MAXTEXT;
1463 }
1464 else if (ef->pos < 0) {
1465 ef->pos = 0;
1466 }
1467
1468 /* apply vertical cursor motion to position immediately
1469 * otherwise the selection will lag behind */
1470 if (FO_CURS_IS_MOTION(cursmove)) {
1471 BKE_vfont_to_curve(DEG_get_evaluated(depsgraph, obedit), eEditFontMode(cursmove));
1472 cursmove = FO_CURS;
1473 }
1474
1475 if (select == 0) {
1476 if (ef->selstart) {
1477 ef->selstart = ef->selend = 0;
1479 }
1480 }
1481
1482 if (select) {
1483 ef->selend = ef->pos;
1485 }
1486
1487 text_update_edited(C, obedit, eEditFontMode(cursmove));
1488
1489 return OPERATOR_FINISHED;
1490}
1491
1493{
1494 int type = RNA_enum_get(op->ptr, "type");
1495
1496 return move_cursor(C, type, false);
1497}
1498
1500{
1501 /* identifiers */
1502 ot->name = "Move Cursor";
1503 ot->description = "Move cursor to position type";
1504 ot->idname = "FONT_OT_move";
1505
1506 /* API callbacks. */
1507 ot->exec = move_exec;
1509
1510 /* flags */
1512
1513 /* properties */
1514 RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to");
1515}
1516
1518
1519/* -------------------------------------------------------------------- */
1522
1524{
1525 int type = RNA_enum_get(op->ptr, "type");
1526
1527 return move_cursor(C, type, true);
1528}
1529
1531{
1532 /* identifiers */
1533 ot->name = "Move Select";
1534 ot->description = "Move the cursor while selecting";
1535 ot->idname = "FONT_OT_move_select";
1536
1537 /* API callbacks. */
1538 ot->exec = move_select_exec;
1540
1541 /* flags */
1543
1544 /* properties */
1545 RNA_def_enum(ot->srna,
1546 "type",
1548 LINE_BEGIN,
1549 "Type",
1550 "Where to move cursor to, to make a selection");
1551}
1552
1554
1555/* -------------------------------------------------------------------- */
1558
1560{
1561 Object *obedit = CTX_data_edit_object(C);
1562 Curve *cu = id_cast<Curve *>(obedit->data);
1563 EditFont *ef = cu->editfont;
1564 float kern, delta = RNA_float_get(op->ptr, "delta");
1565 int selstart, selend;
1566 bool changed = false;
1567
1568 const bool has_select = BKE_vfont_select_get(cu, &selstart, &selend);
1569 if (has_select) {
1570 selstart -= 1;
1571 }
1572 else {
1573 selstart = selend = ef->pos - 1;
1574 }
1575 selstart = max_ii(0, selstart);
1576
1577 for (int i = selstart; i <= selend; i++) {
1578 kern = ef->textbufinfo[i].kern + delta;
1579
1580 if (ef->textbufinfo[i].kern != kern) {
1581 ef->textbufinfo[i].kern = kern;
1582 changed = true;
1583 }
1584 }
1585
1586 if (changed) {
1587 text_update_edited(C, obedit, FO_EDIT);
1588
1589 return OPERATOR_FINISHED;
1590 }
1591 return OPERATOR_CANCELLED;
1592}
1593
1595{
1596 /* identifiers */
1597 ot->name = "Change Spacing";
1598 ot->description = "Change font spacing";
1599 ot->idname = "FONT_OT_change_spacing";
1600
1601 /* API callbacks. */
1604
1605 /* flags */
1607
1608 /* properties */
1609 RNA_def_float(ot->srna,
1610 "delta",
1611 1.0,
1612 0.0,
1613 0.0,
1614 "Delta",
1615 "Amount to decrease or increase character spacing with",
1616 0.0,
1617 0.0);
1618}
1619
1621
1622/* -------------------------------------------------------------------- */
1625
1627{
1628 Object *obedit = CTX_data_edit_object(C);
1629 Curve *cu = id_cast<Curve *>(obedit->data);
1630 EditFont *ef = cu->editfont;
1631 int character, delta = RNA_int_get(op->ptr, "delta");
1632
1633 if (ef->pos <= 0) {
1634 return OPERATOR_CANCELLED;
1635 }
1636
1637 character = ef->textbuf[ef->pos - 1];
1638 character += delta;
1639 CLAMP(character, 0, 255);
1640
1641 if (ELEM(character, 0, ef->textbuf[ef->pos - 1])) {
1642 return OPERATOR_CANCELLED;
1643 }
1644
1645 ef->textbuf[ef->pos - 1] = character;
1646
1647 text_update_edited(C, obedit, FO_EDIT);
1648
1649 return OPERATOR_FINISHED;
1650}
1651
1653{
1654 /* identifiers */
1655 ot->name = "Change Character";
1656 ot->description = "Change font character code";
1657 ot->idname = "FONT_OT_change_character";
1658
1659 /* API callbacks. */
1662
1663 /* flags */
1665
1666 /* properties */
1667 RNA_def_int(ot->srna,
1668 "delta",
1669 1,
1670 -255,
1671 255,
1672 "Delta",
1673 "Number to increase or decrease character code with",
1674 -255,
1675 255);
1676}
1677
1679
1680/* -------------------------------------------------------------------- */
1683
1685{
1686 Object *obedit = CTX_data_edit_object(C);
1687 Curve *cu = id_cast<Curve *>(obedit->data);
1688 EditFont *ef = cu->editfont;
1689
1690 insert_into_textbuf(obedit, '\n');
1691
1692 ef->selstart = ef->selend = 0;
1693
1694 text_update_edited(C, obedit, FO_EDIT);
1695
1696 return OPERATOR_FINISHED;
1697}
1698
1700{
1701 /* identifiers */
1702 ot->name = "Line Break";
1703 ot->description = "Insert line break at cursor position";
1704 ot->idname = "FONT_OT_line_break";
1705
1706 /* API callbacks. */
1707 ot->exec = line_break_exec;
1709
1710 /* flags */
1712}
1713
1715
1716/* -------------------------------------------------------------------- */
1719
1721 {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
1722 {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
1723 {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
1724 {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
1725 {DEL_SELECTION, "SELECTION", 0, "Selection", ""},
1726 {DEL_NEXT_SEL, "NEXT_OR_SELECTION", 0, "Next or Selection", ""},
1727 {DEL_PREV_SEL, "PREVIOUS_OR_SELECTION", 0, "Previous or Selection", ""},
1728 {0, nullptr, 0, nullptr, nullptr},
1729};
1730
1732{
1733#ifdef WITH_INPUT_IME
1734 if (const std::optional<wmOperatorStatus> status = WM_operator_IME_edit_maybe(C)) {
1735 return *status;
1736 }
1737#endif
1738
1739 Object *obedit = CTX_data_edit_object(C);
1740 Curve *cu = id_cast<Curve *>(obedit->data);
1741 EditFont *ef = cu->editfont;
1742 int selstart, selend, type = RNA_enum_get(op->ptr, "type");
1743 int range[2] = {0, 0};
1744 bool has_select = false;
1745
1746 if (ef->len == 0) {
1747 return OPERATOR_CANCELLED;
1748 }
1749
1750 if (BKE_vfont_select_get(cu, &selstart, &selend)) {
1751 if (type == DEL_NEXT_SEL) {
1752 type = DEL_SELECTION;
1753 }
1754 else if (type == DEL_PREV_SEL) {
1755 type = DEL_SELECTION;
1756 }
1757 has_select = true;
1758 }
1759 else {
1760 if (type == DEL_NEXT_SEL) {
1761 type = DEL_NEXT_CHAR;
1762 }
1763 else if (type == DEL_PREV_SEL) {
1764 type = DEL_PREV_CHAR;
1765 }
1766 }
1767
1768 switch (type) {
1769 case DEL_SELECTION:
1770 if (!kill_selection(obedit, 0)) {
1771 return OPERATOR_CANCELLED;
1772 }
1773 break;
1774 case DEL_PREV_CHAR:
1775 if (ef->pos <= 0) {
1776 return OPERATOR_CANCELLED;
1777 }
1778
1779 range[1] = ef->pos;
1781 range[0] = ef->pos;
1782 break;
1783 case DEL_NEXT_CHAR:
1784 if (ef->pos >= ef->len) {
1785 return OPERATOR_CANCELLED;
1786 }
1787
1788 range[0] = ef->pos;
1789 range[1] = ef->pos;
1790 BLI_str_cursor_step_next_utf32(ef->textbuf, ef->len, &range[1]);
1791 break;
1792 case DEL_NEXT_WORD: {
1793 int pos = ef->pos;
1795 ef->textbuf, ef->len, &pos, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, true);
1796 range[0] = ef->pos;
1797 range[1] = pos;
1798 break;
1799 }
1800
1801 case DEL_PREV_WORD: {
1802 int pos = ef->pos;
1804 ef->textbuf, ef->len, &pos, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, true);
1805 range[0] = pos;
1806 range[1] = ef->pos;
1807 ef->pos = pos;
1808 break;
1809 }
1810 default:
1811 return OPERATOR_CANCELLED;
1812 }
1813
1814 if (range[0] != range[1]) {
1815 BLI_assert(range[0] < range[1]);
1816 int len_remove = range[1] - range[0];
1817 int len_tail = ef->len - range[1];
1818 if (has_select) {
1819 for (int i = 0; i < 2; i++) {
1820 int *sel = i ? &ef->selend : &ef->selstart;
1821 if (*sel <= range[0]) {
1822 /* pass */
1823 }
1824 else if (*sel >= range[1]) {
1825 *sel -= len_remove;
1826 }
1827 else {
1828 BLI_assert(*sel < range[1]);
1829 /* pass */
1830 *sel = range[0];
1831 }
1832 }
1833 }
1834
1835 memmove(&ef->textbuf[range[0]], &ef->textbuf[range[1]], sizeof(*ef->textbuf) * len_tail);
1836 memmove(&ef->textbufinfo[range[0]],
1837 &ef->textbufinfo[range[1]],
1838 sizeof(*ef->textbufinfo) * len_tail);
1839
1840 ef->len -= len_remove;
1841 ef->textbuf[ef->len] = '\0';
1842
1844 }
1845
1846 text_update_edited(C, obedit, FO_EDIT);
1847
1848 return OPERATOR_FINISHED;
1849}
1850
1852{
1853 /* identifiers */
1854 ot->name = "Delete";
1855 ot->description = "Delete text by cursor position";
1856 ot->idname = "FONT_OT_delete";
1857
1858 /* API callbacks. */
1859 ot->exec = delete_exec;
1861
1862 /* flags */
1864
1865 /* properties */
1866 RNA_def_enum(ot->srna,
1867 "type",
1870 "Type",
1871 "Which part of the text to delete");
1872}
1873
1875
1876/* -------------------------------------------------------------------- */
1879
1881{
1882 Object *obedit = CTX_data_edit_object(C);
1883 char32_t *inserted_text;
1884 int a, len;
1885
1886 if (!RNA_struct_property_is_set(op->ptr, "text")) {
1887 return OPERATOR_CANCELLED;
1888 }
1889
1890 std::string inserted_utf8 = RNA_string_get(op->ptr, "text");
1891 len = BLI_strlen_utf8(inserted_utf8.c_str());
1892
1893 inserted_text = MEM_new_array_zeroed<char32_t>((len + 1), "FONT_insert_text");
1894 len = BLI_str_utf8_as_utf32(inserted_text, inserted_utf8.c_str(), len + 1);
1895
1896 int inserted_len = 0;
1897 for (a = 0; a < len; a++) {
1898 inserted_len += insert_into_textbuf(obedit, inserted_text[a]);
1899 }
1900
1901 MEM_delete(inserted_text);
1902
1903 kill_selection(obedit, inserted_len);
1904 text_update_edited(C, obedit, FO_EDIT);
1905
1906 return OPERATOR_FINISHED;
1907}
1908
1910{
1911 Object *obedit = CTX_data_edit_object(C);
1912 Curve *cu = id_cast<Curve *>(obedit->data);
1913 EditFont *ef = cu->editfont;
1914 static bool accentcode = false;
1915 const bool alt = event->modifier & KM_ALT;
1916 const bool shift = event->modifier & KM_SHIFT;
1917 const bool ctrl = event->modifier & KM_CTRL;
1918 char32_t insert_char_override = 0;
1919 char32_t inserted_text[2] = {0};
1920
1921 if (RNA_struct_property_is_set(op->ptr, "text")) {
1922 return insert_text_exec(C, op);
1923 }
1924
1925 if (RNA_struct_property_is_set(op->ptr, "accent")) {
1926 if (ef->len != 0 && ef->pos > 0) {
1927 accentcode = true;
1928 }
1929 return OPERATOR_FINISHED;
1930 }
1931
1932 if (event->type == EVT_BACKSPACEKEY) {
1933 if (alt && ef->len != 0 && ef->pos > 0) {
1934 accentcode = true;
1935 }
1936 return OPERATOR_PASS_THROUGH;
1937 }
1938
1939#ifdef WITH_INPUT_IME
1940 if (const std::optional<wmOperatorStatus> status = WM_operator_IME_insert_maybe(
1941 C, op, event, "text"))
1942 {
1943 return *status;
1944 }
1945#endif
1946
1947 /* Tab typically exit edit-mode, but we allow it to be typed using modifier keys. */
1948 if (event->type == EVT_TABKEY) {
1949 if ((alt || ctrl || shift) == 0) {
1950 return OPERATOR_PASS_THROUGH;
1951 }
1952 insert_char_override = '\t';
1953 }
1954
1955 if (insert_char_override || event->utf8_buf[0]) {
1956 if (insert_char_override) {
1957 /* Handle case like TAB ('\t'). */
1958 inserted_text[0] = insert_char_override;
1959 insert_into_textbuf(obedit, insert_char_override);
1960 text_update_edited(C, obedit, FO_EDIT);
1961 }
1962 else {
1963 BLI_assert(event->utf8_buf[0]);
1964 int inserted_len = 0;
1965 if (accentcode) {
1966 if (ef->pos > 0) {
1967 inserted_text[0] = findaccent(ef->textbuf[ef->pos - 1],
1969 ef->textbuf[ef->pos - 1] = inserted_text[0];
1970 }
1971 accentcode = false;
1972 }
1973 else if (event->utf8_buf[0]) {
1974 inserted_text[0] = BLI_str_utf8_as_unicode_or_error(event->utf8_buf);
1975 inserted_len = insert_into_textbuf(obedit, inserted_text[0]);
1976 accentcode = false;
1977 }
1978 else {
1979 BLI_assert(0);
1980 }
1981
1982 kill_selection(obedit, inserted_len);
1983 text_update_edited(C, obedit, FO_EDIT);
1984 }
1985 }
1986 else {
1987 return OPERATOR_PASS_THROUGH;
1988 }
1989
1990 if (inserted_text[0]) {
1991 /* Store as UTF8 in RNA string. */
1992 char inserted_utf8[8] = {0};
1993
1994 BLI_str_utf32_as_utf8(inserted_utf8, inserted_text, sizeof(inserted_utf8));
1995 RNA_string_set(op->ptr, "text", inserted_utf8);
1996 }
1997
1998 return OPERATOR_FINISHED;
1999}
2000
2002{
2003 /* identifiers */
2004 ot->name = "Insert Text";
2005 ot->description = "Insert text at cursor position";
2006 ot->idname = "FONT_OT_text_insert";
2007
2008 /* API callbacks. */
2009 ot->exec = insert_text_exec;
2012
2013 /* flags */
2014 ot->flag = OPTYPE_UNDO;
2015
2016 /* properties */
2017 RNA_def_string(ot->srna, "text", nullptr, 0, "Text", "Text to insert at the cursor position");
2019 ot->srna,
2020 "accent",
2021 false,
2022 "Accent Mode",
2023 "Next typed character will strike through previous, for special character input");
2024}
2025
2027
2028/* -------------------------------------------------------------------- */
2031
2032static int font_cursor_text_index_from_event(bContext *C, Object *obedit, const wmEvent *event)
2033{
2034 /* Calculate a plane from the text object's orientation. */
2035 float plane[4];
2037 plane, obedit->object_to_world().location(), obedit->object_to_world().ptr()[2]);
2038
2039 /* Convert Mouse location in region to 3D location in world space. */
2040 float mal_fl[2] = {float(event->mval[0]), float(event->mval[1])};
2041 float mouse_loc[3];
2042 ED_view3d_win_to_3d_on_plane(CTX_wm_region(C), plane, mal_fl, true, mouse_loc);
2043
2044 /* Convert to object space and scale by font size. */
2045 mul_m4_v3(obedit->world_to_object().ptr(), mouse_loc);
2046
2047 const float2 cursor_location = {mouse_loc[0], mouse_loc[1]};
2048 return BKE_vfont_cursor_to_text_index(obedit, cursor_location);
2049}
2050
2051static void font_cursor_set_apply(bContext *C, const wmEvent *event)
2052{
2053 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2055 Curve *cu = id_cast<Curve *>(ob->data);
2056 EditFont *ef = cu->editfont;
2057 BLI_assert(ef->len >= 0);
2058
2059 const int string_offset = font_cursor_text_index_from_event(C, ob, event);
2060
2061 if (string_offset > ef->len || string_offset < 0) {
2062 return;
2063 }
2064
2065 cu->curinfo = ef->textbufinfo[ef->pos ? ef->pos - 1 : 0];
2066
2068
2069 if (!ef->selboxes && (ef->selstart == 0)) {
2070 if (ef->pos == 0) {
2071 ef->selstart = ef->selend = 1;
2072 }
2073 else {
2074 ef->selstart = ef->selend = string_offset + 1;
2075 }
2076 }
2077 ef->selend = string_offset;
2078 ef->pos = string_offset;
2079
2082}
2083
2085 wmOperator *op,
2086 const wmEvent *event)
2087{
2088 Object *obedit = CTX_data_active_object(C);
2089 Curve *cu = id_cast<Curve *>(obedit->data);
2090 EditFont *ef = cu->editfont;
2091
2092 font_cursor_set_apply(C, event);
2093 ef->selstart = 0;
2094 ef->selend = 0;
2096
2098}
2099
2101 wmOperator * /*op*/,
2102 const wmEvent *event)
2103{
2104 switch (event->type) {
2105 case LEFTMOUSE:
2106 if (event->val == KM_RELEASE) {
2107 font_cursor_set_apply(C, event);
2108 return OPERATOR_FINISHED;
2109 }
2110 break;
2111 case MIDDLEMOUSE:
2112 case RIGHTMOUSE:
2113 return OPERATOR_FINISHED;
2114 case MOUSEMOVE:
2115 font_cursor_set_apply(C, event);
2116 break;
2117 default: {
2118 break;
2119 }
2120 }
2122}
2123
2125{
2126 /* identifiers */
2127 ot->name = "Set Selection";
2128 ot->idname = "FONT_OT_selection_set";
2129 ot->description = "Set cursor selection";
2130
2131 /* API callbacks. */
2135}
2136
2138
2139/* -------------------------------------------------------------------- */
2142
2144{
2145 Object *obedit = CTX_data_edit_object(C);
2146 Curve *cu = id_cast<Curve *>(obedit->data);
2147 EditFont *ef = cu->editfont;
2148
2150 ef->pos = ef->selend;
2151
2152 /* XXX: Text object selection start is 1-based, unlike text processing elsewhere in Blender. */
2153 ef->selstart += 1;
2154
2156 text_update_edited(C, obedit, FO_CURS);
2157
2158 return OPERATOR_FINISHED;
2159}
2160
2162{
2163 /* identifiers */
2164 ot->name = "Select Word";
2165 ot->idname = "FONT_OT_select_word";
2166 ot->description = "Select word under cursor";
2167
2168 /* API callbacks. */
2171}
2172
2174
2175/* -------------------------------------------------------------------- */
2178
2180{
2182
2183 Curve *cu = id_cast<Curve *>(obedit->data);
2184 int i;
2185
2186 if (cu->totbox < 256) {
2187 for (i = cu->totbox; i > cu->actbox; i--) {
2188 cu->tb[i] = cu->tb[i - 1];
2189 }
2190 cu->tb[cu->actbox] = cu->tb[cu->actbox - 1];
2191 cu->actbox++;
2192 cu->totbox++;
2193 }
2194
2197 return OPERATOR_FINISHED;
2198}
2199
2201{
2202 /* identifiers */
2203 ot->name = "Add Text Box";
2204 ot->description = "Add a new text box";
2205 ot->idname = "FONT_OT_textbox_add";
2206
2207 /* API callbacks. */
2208 ot->exec = textbox_add_exec;
2210
2211 /* flags */
2213}
2214
2216
2217/* -------------------------------------------------------------------- */
2220
2222{
2224 Curve *cu = id_cast<Curve *>(obedit->data);
2225 int i;
2226 int index = RNA_int_get(op->ptr, "index");
2227
2228 if (cu->totbox > 1) {
2229 for (i = index; i < cu->totbox; i++) {
2230 cu->tb[i] = cu->tb[i + 1];
2231 }
2232 cu->totbox--;
2233 if (cu->actbox >= index) {
2234 cu->actbox--;
2235 }
2236 }
2237
2240
2241 return OPERATOR_FINISHED;
2242}
2243
2245{
2246 /* identifiers */
2247 ot->name = "Remove Text Box";
2248 ot->description = "Remove the text box";
2249 ot->idname = "FONT_OT_textbox_remove";
2250
2251 /* API callbacks. */
2254
2255 /* flags */
2257
2258 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "The current text box", 0, INT_MAX);
2259}
2260
2262
2263/* -------------------------------------------------------------------- */
2266
2268{
2269 Curve *cu = id_cast<Curve *>(obedit->data);
2270 EditFont *ef = cu->editfont;
2271
2272 if (ef == nullptr) {
2273 ef = cu->editfont = MEM_new_zeroed<EditFont>("editfont");
2274
2275 ef->textbuf = MEM_new_array_zeroed<char32_t>((MAXTEXT + 4), "texteditbuf");
2276 ef->textbufinfo = MEM_new_array<CharInfo>((MAXTEXT + 4), "texteditbufinfo");
2277 }
2278
2279 /* Convert the original text to chat32_t. */
2280 if (cu->str) {
2281 int len_char32 = BLI_str_utf8_as_utf32(ef->textbuf, cu->str, MAXTEXT + 4);
2282 BLI_assert(len_char32 == cu->len_char32);
2283 ef->len = len_char32;
2284 BLI_assert(ef->len >= 0);
2285 }
2286
2287 /* Old files may not have this initialized (v2.34). Leaving zeroed is OK. */
2288 if (cu->strinfo) {
2289 memcpy(ef->textbufinfo, cu->strinfo, ef->len * sizeof(CharInfo));
2290 }
2291
2292 ef->pos = cu->pos;
2293 ef->pos = std::min(ef->pos, ef->len);
2294
2295 cu->curinfo = ef->textbufinfo[ef->pos ? ef->pos - 1 : 0];
2296
2297 /* Other vars */
2298 ef->selstart = cu->selstart;
2299 ef->selend = cu->selend;
2300
2301 /* text may have been modified by Python */
2303}
2304
2306{
2307 Curve *cu = id_cast<Curve *>(obedit->data);
2308 EditFont *ef = cu->editfont;
2309
2310 /* Free the old curve string */
2311 if (cu->str) {
2312 MEM_delete(cu->str);
2313 }
2314
2315 /* Calculate the actual string length in UTF8 variable characters. */
2316 cu->len_char32 = ef->len;
2318
2319 /* Alloc memory for UTF8 variable char length string. */
2320 cu->str = MEM_new_array_uninitialized<char>(cu->len + sizeof(char32_t), "str");
2321
2322 /* Copy the wchar to UTF8. */
2323 BLI_str_utf32_as_utf8(cu->str, ef->textbuf, cu->len + 1);
2324
2325 if (cu->strinfo) {
2326 MEM_delete(cu->strinfo);
2327 }
2328 cu->strinfo = MEM_new_array<CharInfo>((cu->len_char32 + 4), "texteditinfo");
2329 memcpy(cu->strinfo, ef->textbufinfo, cu->len_char32 * sizeof(CharInfo));
2330
2331 /* Other vars */
2332 cu->pos = ef->pos;
2333 cu->selstart = ef->selstart;
2334 cu->selend = ef->selend;
2335}
2336
2338{
2339 BKE_curve_editfont_free(id_cast<Curve *>(obedit->data));
2340}
2341
2343
2344/* -------------------------------------------------------------------- */
2347
2349 {CASE_LOWER, "LOWER", 0, "Lower", ""},
2350 {CASE_UPPER, "UPPER", 0, "Upper", ""},
2351 {0, nullptr, 0, nullptr, nullptr},
2352};
2353
2355{
2356 Object *obedit = CTX_data_edit_object(C);
2357 Curve *cu = id_cast<Curve *>(obedit->data);
2358 int selstart, selend;
2359
2360 if (BKE_vfont_select_get(cu, &selstart, &selend)) {
2361 EditFont *ef = cu->editfont;
2362 char32_t *str = &ef->textbuf[selstart];
2363
2364 for (int len = (selend - selstart) + 1; len; len--, str++) {
2365 *str = (ccase == CASE_LOWER) ? BLI_str_utf32_char_to_lower(*str) :
2367 }
2368
2369 text_update_edited(C, obedit, FO_EDIT);
2370 }
2371
2372 return OPERATOR_FINISHED;
2373}
2374
2376{
2377 return set_case(C, RNA_enum_get(op->ptr, "case"));
2378}
2379
2381{
2382 PropertyRNA *prop;
2383
2384 /* identifiers */
2385 ot->name = "Set Case";
2386 ot->description = "Set font case";
2387 ot->idname = "FONT_OT_case_set";
2388
2389 /* API callbacks. */
2390 ot->exec = set_case_exec;
2392
2393 /* flags */
2395
2396 /* properties */
2397 prop = RNA_def_enum(ot->srna, "case", case_items, CASE_LOWER, "Case", "Lower or upper case");
2399}
2400
2402
2403/* -------------------------------------------------------------------- */
2406
2408{
2409 Object *obedit = CTX_data_edit_object(C);
2410 Curve *cu = id_cast<Curve *>(obedit->data);
2411 EditFont *ef = cu->editfont;
2412 int ccase = CASE_UPPER;
2413
2414 const char32_t *str = ef->textbuf;
2415 while (*str) {
2416 if (*str >= 'a' && *str <= 'z') {
2417 ccase = CASE_LOWER;
2418 break;
2419 }
2420
2421 str++;
2422 }
2423
2424 return set_case(C, ccase);
2425}
2426
2428{
2429 /* identifiers */
2430 ot->name = "Toggle Case";
2431 ot->description = "Toggle font case";
2432 ot->idname = "FONT_OT_case_toggle";
2433
2434 /* API callbacks. */
2435 ot->exec = toggle_case_exec;
2437
2438 /* flags */
2440}
2441
2443
2444/* -------------------------------------------------------------------- */
2447
2449{
2450 PropertyPointerRNA *pprop;
2451
2452 op->customdata = pprop = MEM_new<PropertyPointerRNA>("OpenPropertyPointerRNA");
2454}
2455
2456static void font_open_cancel(bContext * /*C*/, wmOperator *op)
2457{
2458 MEM_delete(static_cast<PropertyPointerRNA *>(op->customdata));
2459 op->customdata = nullptr;
2460}
2461
2463{
2464 Main *bmain = CTX_data_main(C);
2465 VFont *font;
2466 PropertyPointerRNA *pprop;
2467 char filepath[FILE_MAX];
2468 RNA_string_get(op->ptr, "filepath", filepath);
2469
2470 font = BKE_vfont_load(bmain, filepath);
2471
2472 if (!font) {
2473 if (op->customdata) {
2474 MEM_delete(static_cast<PropertyPointerRNA *>(op->customdata));
2475 }
2476 return OPERATOR_CANCELLED;
2477 }
2478
2479 if (!op->customdata) {
2481 }
2482
2483 /* hook into UI */
2484 pprop = static_cast<PropertyPointerRNA *>(op->customdata);
2485
2486 if (pprop->prop) {
2487 /* when creating new ID blocks, use is already 1, but RNA
2488 * pointer use also increases user, so this compensates it */
2489 id_us_min(&font->id);
2490
2491 PointerRNA idptr = RNA_id_pointer_create(&font->id);
2492 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, nullptr);
2493 RNA_property_update(C, &pprop->ptr, pprop->prop);
2494 }
2495
2496 MEM_delete(static_cast<PropertyPointerRNA *>(op->customdata));
2497
2498 return OPERATOR_FINISHED;
2499}
2500
2502{
2503 VFont *vfont = nullptr;
2504 char filepath[FILE_MAX];
2505
2506 PointerRNA idptr;
2507 PropertyPointerRNA *pprop;
2508
2510
2511 /* hook into UI */
2512 pprop = static_cast<PropertyPointerRNA *>(op->customdata);
2513
2514 if (pprop->prop) {
2515 idptr = RNA_property_pointer_get(reinterpret_cast<PointerRNA *>(pprop), pprop->prop);
2516 vfont = id_cast<VFont *>(idptr.owner_id);
2517 }
2518
2519 PropertyRNA *prop_filepath = RNA_struct_find_property(op->ptr, "filepath");
2520 if (RNA_property_is_set(op->ptr, prop_filepath)) {
2521 return font_open_exec(C, op);
2522 }
2523
2524 if (vfont && !BKE_vfont_is_builtin(vfont)) {
2525 STRNCPY(filepath, vfont->filepath);
2526 BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
2527 }
2528 else {
2529 STRNCPY(filepath, U.fontdir);
2530 BLI_path_slash_ensure(filepath, sizeof(filepath));
2531 /* The file selector will expand the blend-file relative prefix. */
2532 }
2533 RNA_property_string_set(op->ptr, prop_filepath, filepath);
2534
2536
2538}
2539
2541{
2542 /* identifiers */
2543 ot->name = "Open Font";
2544 ot->idname = "FONT_OT_open";
2545 ot->description = "Load a new font from a file";
2546
2547 /* API callbacks. */
2548 ot->exec = font_open_exec;
2549 ot->invoke = open_invoke;
2551
2552 /* flags */
2554
2555 /* properties */
2563}
2564
2566
2567/* -------------------------------------------------------------------- */
2570
2572{
2573 VFont *builtin_font;
2574
2575 PropertyPointerRNA pprop;
2576
2578
2579 if (pprop.prop == nullptr) {
2580 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running font unlink");
2581 return OPERATOR_CANCELLED;
2582 }
2583
2584 builtin_font = BKE_vfont_builtin_ensure();
2585
2586 PointerRNA idptr = RNA_id_pointer_create(&builtin_font->id);
2587 RNA_property_pointer_set(&pprop.ptr, pprop.prop, idptr, nullptr);
2588 RNA_property_update(C, &pprop.ptr, pprop.prop);
2589
2590 return OPERATOR_FINISHED;
2591}
2592
2594{
2595 /* identifiers */
2596 ot->name = "Unlink";
2597 ot->idname = "FONT_OT_unlink";
2598 ot->description = "Unlink active font data-block";
2599
2600 /* API callbacks. */
2601 ot->exec = font_unlink_exec;
2602}
2603
2604
2605/* -------------------------------------------------------------------- */
2608
2610 bContext *C,
2611 const int mval[2],
2612 /* NOTE: `params->deselect_all` is ignored as only one text-box is active at once. */
2613 const SelectPick_Params &params)
2614{
2615 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2616 Object *obedit = CTX_data_edit_object(C);
2617 Curve *cu = id_cast<Curve *>(obedit->data);
2618 /* bias against the active, in pixels, allows cycling */
2619 const float active_bias_px = 4.0f;
2620 const float mval_fl[2] = {float(mval[0]), float(mval[1])};
2621 const int i_actbox = max_ii(0, cu->actbox - 1);
2622 int i_iter, actbox_select = -1;
2623 const float dist = ED_view3d_select_dist_px();
2624 float dist_sq_best = dist * dist;
2625
2626 ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph);
2627
2629
2630 /* currently only select active */
2631 (void)params;
2632
2633 for (i_iter = 0; i_iter < cu->totbox; i_iter++) {
2634 int i = (i_iter + i_actbox) % cu->totbox;
2635 float dist_sq_min;
2636 int j, j_prev;
2637
2638 float obedit_co[4][3];
2639 float screen_co[4][2];
2640 rctf rect;
2641 int project_ok = 0;
2642
2643 BKE_curve_rect_from_textbox(cu, &cu->tb[i], &rect);
2644
2645 copy_v3_fl3(obedit_co[0], rect.xmin, rect.ymin, 0.0f);
2646 copy_v3_fl3(obedit_co[1], rect.xmin, rect.ymax, 0.0f);
2647 copy_v3_fl3(obedit_co[2], rect.xmax, rect.ymax, 0.0f);
2648 copy_v3_fl3(obedit_co[3], rect.xmax, rect.ymin, 0.0f);
2649
2650 for (j = 0; j < 4; j++) {
2652 vc.region, obedit_co[j], screen_co[j], V3D_PROJ_TEST_CLIP_BB) == V3D_PROJ_RET_OK)
2653 {
2654 project_ok |= (1 << j);
2655 }
2656 }
2657
2658 dist_sq_min = dist_sq_best;
2659 for (j = 0, j_prev = 3; j < 4; j_prev = j++) {
2660 if ((project_ok & (1 << j)) && (project_ok & (1 << j_prev))) {
2661 const float dist_test_sq = dist_squared_to_line_segment_v2(
2662 mval_fl, screen_co[j_prev], screen_co[j]);
2663 dist_sq_min = std::min(dist_sq_min, dist_test_sq);
2664 }
2665 }
2666
2667 /* Bias in pixels to cycle selection. */
2668 if (i_iter == 0) {
2669 dist_sq_min += active_bias_px;
2670 }
2671
2672 if (dist_sq_min < dist_sq_best) {
2673 dist_sq_best = dist_sq_min;
2674 actbox_select = i + 1;
2675 }
2676 }
2677
2678 if (actbox_select != -1) {
2679 if (cu->actbox != actbox_select) {
2680 cu->actbox = actbox_select;
2682 /* TODO: support #ID_RECALC_SELECT. */
2684 }
2685 return true;
2686 }
2687 return false;
2688}
2689
2691
2692} // namespace blender
General operations, lookup, etc. for blender objects.
#define FO_CURS_IS_MOTION(mode)
Definition BKE_vfont.hh:96
File and directory operations.
#define FILE_MAX
#define BLT_I18NCONTEXT_ID_TEXT
#define RPT_(msgid)
#define TIP_(msgid)
#define IFACE_(msgid)
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
Object is a sort of wrapper for general info.
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NA_ADDED
Definition WM_types.hh:636
#define NC_OBJECT
Definition WM_types.hh:392
#define CLAMP(a, b, c)
#define CLAMP_MAX(a, c)
#define BLI_assert_unreachable()
#define ELEM(...)
#define STREQ(a, b)
#define MAXTEXT
Definition editfont.cc:71
#define rot(x, k)
#define str(s)
#define INT_MAX
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
Object * context_active_object(const bContext *C)
bool material_active_index_set(Object *ob, int index)
void init_transform_on_add(Object *object, const float loc[3], const float rot[3])
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void block_flag_enable(Block *block, int flag)
const uiStyle * style_get_dpi()
void context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
void block_theme_style_set(Block *block, char theme_style)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
BMesh Iterators.
void ED_text_to_object(bContext *C, const Text *text, const bool split_lines)
Definition editfont.cc:909
UserDef U
Definition blender.cc:58
static wmOperatorStatus line_break_exec(bContext *C, wmOperator *)
Definition editfont.cc:1684
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static int insert_into_textbuf(Object *obedit, uintptr_t c)
Definition editfont.cc:380
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
Object * BKE_object_add(Main *bmain, Scene *scene, ViewLayer *view_layer, ObjectType type, const char *name) ATTR_NONNULL(1
Definition object.cc:2296
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
static void font_select_update_primary_clipboard(Object *obedit)
Definition editfont.cc:463
size_t size_t size_t BLI_str_utf32_as_utf8_len_ex(const char32_t *src, size_t src_maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void font_cursor_set_apply(bContext *C, const wmEvent *event)
Definition editfont.cc:2051
static const EnumPropertyItem case_items[]
Definition editfont.cc:2348
char * STRNCPY(char(&dst)[N], const char *src)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void FONT_OT_change_spacing(wmOperatorType *ot)
Definition editfont.cc:1594
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
static bool font_paste_wchar(Object *obedit, const char32_t *str, const size_t str_len, const CharInfo *str_info)
Definition editfont.cc:486
@ RPT_WARNING
Definition BKE_report.hh:40
static wmOperatorStatus textbox_remove_exec(bContext *C, wmOperator *op)
Definition editfont.cc:2221
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
static wmOperatorStatus textbox_add_exec(bContext *C, wmOperator *)
Definition editfont.cc:2179
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BKE_vfont_is_builtin(const VFont *vfont)
Definition vfont.cc:278
void ED_curve_editfont_load(Object *obedit)
Definition editfont.cc:2305
bool WM_operator_poll_or_report_error(bContext *C, wmOperatorType *ot, ReportList *reports)
static char32_t findaccent(char32_t char1, const char code)
Definition editfont.cc:80
void id_us_plus(ID *id)
Definition lib_id.cc:433
PointerRNA RNA_id_pointer_create(ID *id)
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void FONT_OT_select_word(wmOperatorType *ot)
Definition editfont.cc:2161
bool ED_operator_object_active_editable_font(bContext *C)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void FONT_OT_line_break(wmOperatorType *ot)
Definition editfont.cc:1699
static wmOperatorStatus insert_text_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1880
static bool font_paste_utf8(bContext *C, const char *str, const size_t str_len)
Definition editfont.cc:538
static void copy_selection(Object *obedit)
Definition editfont.cc:1108
void FONT_OT_delete(wmOperatorType *ot)
Definition editfont.cc:1851
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void BKE_vfont_clipboard_set(const char32_t *text_buf, const CharInfo *info_buf, size_t len)
Definition vfont.cc:495
size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w, const char *__restrict src_c, size_t dst_w_maxncpy) ATTR_NONNULL(1
static wmOperatorStatus copy_text_exec(bContext *C, wmOperator *)
Definition editfont.cc:1134
static wmOperatorStatus toggle_style_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1028
int RNA_int_get(PointerRNA *ptr, const char *name)
wmWindow * CTX_wm_window(const bContext *C)
static wmOperatorStatus set_case_exec(bContext *C, wmOperator *op)
Definition editfont.cc:2375
static wmOperatorStatus delete_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1731
MINLINE int max_ii(int a, int b)
static wmOperatorStatus move_select_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1523
void ED_curve_editfont_free(Object *obedit)
Definition editfont.cc:2337
static wmOperatorStatus insert_text_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition editfont.cc:1909
static int font_cursor_text_index_from_event(bContext *C, Object *obedit, const wmEvent *event)
Definition editfont.cc:2032
void MEM_delete_void(void *vmemh)
void FONT_OT_unlink(wmOperatorType *ot)
Definition editfont.cc:2593
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
ViewLayer * CTX_data_view_layer(const bContext *C)
void ED_outliner_select_sync_from_object_tag(bContext *C)
static void text_update_edited(bContext *C, Object *obedit, const eEditFontMode mode)
Definition editfont.cc:408
static wmOperatorStatus font_unlink_exec(bContext *C, wmOperator *op)
Definition editfont.cc:2571
void BKE_vfont_select_clamp(Curve *cu)
Definition vfont.cc:463
static void text_insert_unicode_cancel(bContext *C, void *arg_block, void *)
Definition editfont.cc:670
static int kill_selection(Object *obedit, int ins)
Definition editfont.cc:435
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
static wmOperatorStatus open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition editfont.cc:2501
static wmOperatorStatus set_style_exec(bContext *C, wmOperator *op)
Definition editfont.cc:994
Global G
Definition blender.cc:57
void FONT_OT_move(wmOperatorType *ot)
Definition editfont.cc:1499
bool ED_curve_editfont_select_pick(bContext *C, const int mval[2], const SelectPick_Params &params)
Definition editfont.cc:2609
void mul_m4_v3(const float M[4][4], float r[3])
void FONT_OT_text_cut(wmOperatorType *ot)
Definition editfont.cc:1179
static wmOperatorStatus paste_from_file_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition editfont.cc:626
size_t size_t BLI_str_utf32_as_utf8(char *__restrict dst, const char32_t *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static wmOperatorStatus cut_text_exec(bContext *C, wmOperator *)
Definition editfont.cc:1161
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
void FONT_OT_case_set(wmOperatorType *ot)
Definition editfont.cc:2380
VecBase< float, 2 > float2
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
static wmOperatorStatus text_insert_unicode_invoke(bContext *C, wmOperator *, const wmEvent *)
Definition editfont.cc:800
static const EnumPropertyItem style_items[]
Definition editfont.cc:960
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static ui::Block * wm_block_insert_unicode_create(bContext *C, ARegion *region, void *arg_string)
Definition editfont.cc:703
void FONT_OT_text_copy(wmOperatorType *ot)
Definition editfont.cc:1143
void ED_curve_editfont_make(Object *obedit)
Definition editfont.cc:2267
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool ED_view3d_win_to_3d_on_plane(const ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1329
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
static wmOperatorStatus font_selection_set_modal(bContext *C, wmOperator *, const wmEvent *event)
Definition editfont.cc:2100
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
Object * CTX_data_edit_object(const bContext *C)
static wmOperatorStatus set_style(bContext *C, const eCharInfoFlag style, const bool clear)
Definition editfont.cc:968
bool BKE_vfont_to_curve(Object *ob, eEditFontMode mode)
void BKE_vfont_clipboard_get(char32_t **r_text_buf, CharInfo **r_info_buf, size_t *r_len_utf8, size_t *r_len_utf32)
Definition vfont.cc:525
@ FO_SELCHANGE
Definition BKE_vfont.hh:92
@ FO_PAGEUP
Definition BKE_vfont.hh:88
@ FO_CURSUP
Definition BKE_vfont.hh:85
@ FO_LINE_END
Definition BKE_vfont.hh:91
@ FO_PAGEDOWN
Definition BKE_vfont.hh:89
@ FO_CURSDOWN
Definition BKE_vfont.hh:86
@ FO_LINE_BEGIN
Definition BKE_vfont.hh:90
static const EnumPropertyItem move_type_items[]
Definition editfont.cc:1315
static wmOperatorStatus toggle_case_exec(bContext *C, wmOperator *)
Definition editfont.cc:2407
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void FONT_OT_open(wmOperatorType *ot)
Definition editfont.cc:2540
void FONT_OT_textbox_remove(wmOperatorType *ot)
Definition editfont.cc:2244
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
static wmOperatorStatus paste_from_file(bContext *C, ReportList *reports, const char *filepath)
Definition editfont.cc:585
static wmOperatorStatus font_open_exec(bContext *C, wmOperator *op)
Definition editfont.cc:2462
RegionView3D * CTX_wm_region_view3d(const bContext *C)
void FONT_OT_text_paste(wmOperatorType *ot)
Definition editfont.cc:1285
void FONT_OT_textbox_add(wmOperatorType *ot)
Definition editfont.cc:2200
static wmOperatorStatus move_cursor(bContext *C, int type, const bool select)
Definition editfont.cc:1359
void FONT_OT_style_toggle(wmOperatorType *ot)
Definition editfont.cc:1043
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
static char * font_select_to_buffer(Object *obedit)
Definition editfont.cc:562
int BKE_vfont_select_get(const Curve *cu, int *r_start, int *r_end)
Definition vfont.cc:424
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static wmOperatorStatus change_spacing_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1559
void FONT_OT_change_character(wmOperatorType *ot)
Definition editfont.cc:1652
bool ED_operator_editfont(bContext *C)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void BKE_curve_rect_from_textbox(const Curve *cu, const TextBox *tb, rctf *r_rect)
Definition curve.cc:5455
@ WM_CAPABILITY_CLIPBOARD_PRIMARY
Definition WM_api.hh:227
void BLI_str_cursor_step_utf32(const char32_t *str, int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step)
static void txt_add_object(bContext *C, const TextLine *firstline, int totline, const float offset[3])
Definition editfont.cc:831
static wmOperatorStatus paste_text_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1216
void FONT_OT_case_toggle(wmOperatorType *ot)
Definition editfont.cc:2427
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
static void font_open_cancel(bContext *, wmOperator *op)
Definition editfont.cc:2456
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
@ FILE_OPENFILE
Definition WM_api.hh:1341
void FONT_OT_select_all(wmOperatorType *ot)
Definition editfont.cc:1087
Main * CTX_data_main(const bContext *C)
void FONT_OT_style_set(wmOperatorType *ot)
Definition editfont.cc:1002
char * WM_clipboard_text_get(bool selection, bool ensure_utf8, int *r_len)
void BKE_vfont_clipboard_free()
Definition vfont.cc:487
eWM_CapabilitiesFlag WM_capabilities_flag()
bool BLI_str_cursor_step_next_utf32(const char32_t *str, int str_maxlen, int *pos)
void FONT_OT_selection_set(wmOperatorType *ot)
Definition editfont.cc:2124
void WM_clipboard_text_set(const char *buf, bool selection)
char32_t BLI_str_utf32_char_to_lower(char32_t wc)
void FONT_OT_move_select(wmOperatorType *ot)
Definition editfont.cc:1530
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_GEOMETRY_ALL_MODES
Definition DNA_ID.h:1183
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
VFont * BKE_vfont_builtin_ensure()
Definition vfont.cc:387
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:296
void FONT_OT_text_insert(wmOperatorType *ot)
Definition editfont.cc:2001
VFont * BKE_vfont_load(Main *bmain, const char *filepath)
Definition vfont.cc:304
@ KM_RELEASE
Definition WM_types.hh:325
unsigned int BLI_str_utf8_as_unicode_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes) ATTR_NONNULL(1
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
static bool paste_selection(Object *obedit, ReportList *reports)
Definition editfont.cc:1200
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void BKE_object_where_is_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
Definition object.cc:3577
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
float ED_view3d_select_dist_px()
size_t BLI_str_utf32_as_utf8_len(const char32_t *src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BLI_str_cursor_step_prev_utf32(const char32_t *str, int str_maxlen, int *pos)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
void id_us_min(ID *id)
Definition lib_id.cc:441
void BLI_str_cursor_step_bounds_utf32(const char32_t *str, int str_maxlen, int pos, int *r_start, int *r_end)
static wmOperatorStatus move_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1492
int BKE_vfont_cursor_to_text_index(Object *ob, const float2 &cursor_location)
ARegion * CTX_wm_region(const bContext *C)
void FONT_OT_text_paste_from_file(wmOperatorType *ot)
Definition editfont.cc:639
void FONT_OT_text_insert_unicode(wmOperatorType *ot)
Definition editfont.cc:810
static bool move_cursor_drop_select(Object *obedit, int dir)
Definition editfont.cc:1336
float RNA_float_get(PointerRNA *ptr, const char *name)
static const EnumPropertyItem delete_type_items[]
Definition editfont.cc:1720
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
char32_t BLI_str_utf32_char_to_upper(char32_t wc)
static wmOperatorStatus change_character_exec(bContext *C, wmOperator *op)
Definition editfont.cc:1626
void WM_event_add_fileselect(bContext *C, wmOperator *op)
static wmOperatorStatus font_select_all_exec(bContext *C, wmOperator *)
Definition editfont.cc:1068
void BKE_curve_editfont_free(Curve *cu)
Definition curve.cc:332
static wmOperatorStatus font_select_word_exec(bContext *C, wmOperator *)
Definition editfont.cc:2143
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static wmOperatorStatus set_case(bContext *C, int ccase)
Definition editfont.cc:2354
static wmOperatorStatus paste_from_file_exec(bContext *C, wmOperator *op)
Definition editfont.cc:613
static void text_insert_unicode_confirm(bContext *C, void *arg_block, void *arg_string)
Definition editfont.cc:676
static wmOperatorStatus font_selection_set_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition editfont.cc:2084
static void font_ui_template_init(bContext *C, wmOperator *op)
Definition editfont.cc:2448
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct VFont * vfont
struct TextBox * tb
struct EditFont * editfont
struct CharInfo curinfo
struct CharInfo * strinfo
CharInfo * textbufinfo
Definition BKE_vfont.hh:48
EditFontSelBox * selboxes
Definition BKE_vfont.hh:55
char32_t * textbuf
Definition BKE_vfont.hh:46
struct TextLine * next
char filepath[1024]
RegionView3D * rv3d
Definition ED_view3d.hh:86
void label(StringRef name, int icon)
Layout & split(float percentage, bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type