Blender V5.3
text_draw.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
11#include "MEM_guardedalloc.h"
12
13#include "BLF_api.hh"
14
15#include "BLI_listbase.hh"
16#include "BLI_rect.hh"
17#include "BLI_string.hh"
18#include "BLI_string_utf8.hh"
19
20#include "DNA_screen_types.h"
21#include "DNA_space_types.h"
22#include "DNA_text_types.h"
23
24#include "BKE_context.hh"
25#include "BKE_screen.hh"
26#include "BKE_text.h"
28
29#include "ED_text.hh"
30
31#include "GPU_immediate.hh"
32#include "GPU_state.hh"
33
34#include "UI_interface.hh"
35#include "UI_resources.hh"
36
37#include "text_format.hh"
38#include "text_intern.hh"
39
40#include "WM_api.hh"
41#include "WM_types.hh"
42
43namespace blender {
44
45/* -------------------------------------------------------------------- */
48
50{
51 return st->runtime->viewlines;
52}
53
55
56/* -------------------------------------------------------------------- */
59
66
74
75static void text_font_begin(const TextDrawContext *tdc)
76{
77 BLF_size(tdc->font_id, float(tdc->line_height_px));
78}
79
80static void text_font_end(const TextDrawContext * /*tdc*/) {}
81
82static int text_font_draw(const TextDrawContext *tdc, int x, int y, const char *str)
83{
84 const char tab_columns = 1; /* Tab characters aren't used here. */
85 BLF_position(tdc->font_id, x, y, 0);
86 const int columns = BLF_draw_mono(
87 tdc->font_id, str, BLF_DRAW_STR_DUMMY_MAX, tdc->char_width_px, tab_columns);
88
89 return tdc->char_width_px * columns;
90}
91
92static int text_font_draw_character(const TextDrawContext *tdc, int x, int y, char c)
93{
94 const char tab_columns = 1;
95 BLF_position(tdc->font_id, x, y, 0);
96 BLF_draw_mono(tdc->font_id, &c, 1, tdc->char_width_px, tab_columns);
97
98 return tdc->char_width_px;
99}
100
102 const TextDrawContext *tdc, int x, int y, const char *c, const int c_len)
103{
105 const char tab_columns = 1; /* Tab characters aren't used here. */
106 BLF_position(tdc->font_id, x, y, 0);
107 const int columns = BLF_draw_mono(tdc->font_id, c, c_len, tdc->char_width_px, tab_columns);
108
109 return tdc->char_width_px * columns;
110}
111
112#if 0
114static void txt_format_text(SpaceText *st)
115{
116 TextLine *linep;
117
118 if (!st->text) {
119 return;
120 }
121
122 for (linep = st->text->lines.first; linep; linep = linep->next) {
123 txt_format_line(st, linep, 0);
124 }
125}
126#endif
127
129static void format_draw_color(const TextDrawContext *tdc, char formatchar)
130{
131 switch (formatchar) {
133 break;
134 case FMT_TYPE_SYMBOL:
136 break;
137 case FMT_TYPE_COMMENT:
139 break;
140 case FMT_TYPE_NUMERAL:
142 break;
143 case FMT_TYPE_STRING:
145 break;
148 break;
149 case FMT_TYPE_SPECIAL:
151 break;
154 break;
155 case FMT_TYPE_KEYWORD:
157 break;
158 case FMT_TYPE_DEFAULT:
159 default:
161 break;
162 }
163}
164
166
167/* -------------------------------------------------------------------- */
199
200int space_text_wrap_width(const SpaceText *st, const ARegion *region)
201{
202 int winx = region->winx - TXT_SCROLL_WIDTH;
203 int x, max;
204
205 x = TXT_BODY_LEFT(st);
206 max = st->runtime->char_width_px ? (winx - x) / st->runtime->char_width_px : 0;
207 return max > 8 ? max : 8;
208}
209
211 const SpaceText *st, const ARegion *region, TextLine *linein, int cursin, int *offl, int *offc)
212{
213 Text *text;
214 TextLine *linep;
215 int i, j, start, end, max;
216 bool chop;
217 char ch;
218
219 *offl = *offc = 0;
220
221 if (!st->text) {
222 return;
223 }
224 if (!st->wordwrap) {
225 return;
226 }
227
228 text = st->text;
229
230 /* Move pointer to first visible line (top). */
231 linep = text->lines.first();
232 i = st->top;
233 while (i > 0 && linep) {
234 int lines = space_text_get_visible_lines(st, region, linep->line);
235
236 /* Line before top. */
237 if (linep == linein) {
238 if (lines <= i) {
239 /* No visible part of line. */
240 return;
241 }
242 }
243
244 if (i - lines < 0) {
245 break;
246 }
247
248 linep = linep->next;
249 (*offl) += lines - 1;
250 i -= lines;
251 }
252
253 max = space_text_wrap_width(st, region);
254 cursin = BLI_str_utf8_offset_to_column(linein->line, linein->len, cursin);
255
256 while (linep) {
257 start = 0;
258 end = max;
259 chop = true;
260 *offc = 0;
261 for (i = 0, j = 0; linep->line[j]; j += BLI_str_utf8_size_safe(linep->line + j)) {
262 int chars;
263 const int columns = BLI_str_utf8_char_width_safe(linep->line + j); /* = 1 for tab. */
264
265 /* Mimic replacement of tabs. */
266 ch = linep->line[j];
267 if (ch == '\t') {
268 chars = st->tabnumber - i % st->tabnumber;
269 if (linep == linein && i < cursin) {
270 cursin += chars - 1;
271 }
272 ch = ' ';
273 }
274 else {
275 chars = 1;
276 }
277
278 while (chars--) {
279 if (i + columns - start > max) {
280 end = std::min(end, i);
281
282 if (chop && linep == linein && i >= cursin) {
283 if (i == cursin) {
284 (*offl)++;
285 *offc -= end - start;
286 }
287
288 return;
289 }
290
291 (*offl)++;
292 *offc -= end - start;
293
294 start = end;
295 end += max;
296 chop = true;
297 }
298 else if (ELEM(ch, ' ', '-')) {
299 end = i + 1;
300 chop = false;
301 if (linep == linein && i >= cursin) {
302 return;
303 }
304 }
305 i += columns;
306 }
307 }
308 if (linep == linein) {
309 break;
310 }
311 linep = linep->next;
312 }
313}
314
316 const SpaceText *st, const ARegion *region, TextLine *linein, int cursin, int *offl, int *offc)
317{
318 int i, j, start, end, chars, max;
319 bool chop;
320 char ch;
321
322 *offl = *offc = 0;
323
324 if (!st->text) {
325 return;
326 }
327 if (!st->wordwrap) {
328 return;
329 }
330
331 max = space_text_wrap_width(st, region);
332
333 start = 0;
334 end = max;
335 chop = true;
336 *offc = 0;
337 cursin = BLI_str_utf8_offset_to_column(linein->line, linein->len, cursin);
338
339 for (i = 0, j = 0; linein->line[j]; j += BLI_str_utf8_size_safe(linein->line + j)) {
340 const int columns = BLI_str_utf8_char_width_safe(linein->line + j); /* = 1 for tab. */
341
342 /* Mimic replacement of tabs. */
343 ch = linein->line[j];
344 if (ch == '\t') {
345 chars = st->tabnumber - i % st->tabnumber;
346 if (i < cursin) {
347 cursin += chars - 1;
348 }
349 ch = ' ';
350 }
351 else {
352 chars = 1;
353 }
354
355 while (chars--) {
356 if (i + columns - start > max) {
357 end = std::min(end, i);
358
359 if (chop && i >= cursin) {
360 if (i == cursin) {
361 (*offl)++;
362 *offc -= end - start;
363 }
364
365 return;
366 }
367
368 (*offl)++;
369 *offc -= end - start;
370
371 start = end;
372 end += max;
373 chop = true;
374 }
375 else if (ELEM(ch, ' ', '-')) {
376 end = i + 1;
377 chop = false;
378 if (i >= cursin) {
379 return;
380 }
381 }
382 i += columns;
383 }
384 }
385}
386
387int space_text_get_char_pos(const SpaceText *st, const char *line, int cur)
388{
389 int a = 0, i;
390
391 for (i = 0; i < cur && line[i]; i += BLI_str_utf8_size_safe(line + i)) {
392 if (line[i] == '\t') {
393 a += st->tabnumber - a % st->tabnumber;
394 }
395 else {
396 a += BLI_str_utf8_char_width_safe(line + i);
397 }
398 }
399 return a;
400}
401
410static int space_text_get_format_index(const SpaceText *st, const char *line, int cur)
411{
412 int fc = 0;
413 for (int i = 0; i < cur && line[i]; i += BLI_str_utf8_size_safe(line + i)) {
414 if (line[i] == '\t') {
415 fc += st->tabnumber - (fc % st->tabnumber);
416 }
417 else {
418 fc++;
419 }
420 }
421 return fc;
422}
423
424static const char *txt_utf8_forward_columns(const char *str, int columns, int *padding)
425{
426 const char *p = str;
427 while (*p) {
428 const int col = BLI_str_utf8_char_width_safe(p);
429 if (columns - col < 0) {
430 break;
431 }
432 columns -= col;
434 if (columns == 0) {
435 break;
436 }
437 }
438 if (padding) {
439 *padding = *p ? columns : 0;
440 }
441 return p;
442}
443
445 const TextDrawContext *tdc,
446 const char *str,
447 int x,
448 int y,
449 int w,
450 const char *format,
451 int skip)
452{
453 const bool use_syntax = (tdc->syntax_highlight && format);
454 FlattenString fs;
455 int basex, lines;
456 int i, wrap, end, max, columns, padding; /* Column. */
457 /* Warning, only valid when `use_syntax` is set. */
458 int a, fstart, fpos; /* UTF8 characters. */
459 int mi, ma, mstart, mend;
460 char fmt_prev = 0xff;
461 /* Don't draw lines below this. */
462 const int clip_min_y = -(st->runtime->line_height_px - 1);
463
464 flatten_string(st, &fs, str);
465 str = fs.buf;
466 max = w / st->runtime->char_width_px;
467 max = std::max(max, 8);
468 basex = x;
469 lines = 1;
470
471 fpos = fstart = 0;
472 mstart = 0;
474 end = wrap = max - padding;
475
476 for (i = 0, mi = 0; str[mi]; i += columns, mi += BLI_str_utf8_size_safe(str + mi)) {
477 columns = BLI_str_utf8_char_width_safe(str + mi);
478 if (i + columns > end) {
479 /* Skip hidden part of line. */
480 if (skip) {
481 skip--;
482 if (use_syntax) {
483 /* Currently fpos only used when formatting. */
484 fpos += BLI_strnlen_utf8(str + mstart, mend - mstart);
485 }
486 fstart = fpos;
487 mstart = mend;
488 mend = txt_utf8_forward_columns(str + mend, max, &padding) - str;
489 end = (wrap += max - padding);
490 continue;
491 }
492
493 /* Draw the visible portion of text on the overshot line. */
494 for (a = fstart, ma = mstart; ma < mend; a++) {
495 if (use_syntax) {
496 if (fmt_prev != format[a]) {
497 format_draw_color(tdc, fmt_prev = format[a]);
498 }
499 }
500 const int c_len = BLI_str_utf8_size_safe(str + ma);
501 x += text_font_draw_character_utf8(tdc, x, y, str + ma, c_len);
502 ma += c_len;
503 fpos++;
504 }
505 y -= TXT_LINE_HEIGHT(st);
506 x = basex;
507 lines++;
508 fstart = fpos;
509 mstart = mend;
510 mend = txt_utf8_forward_columns(str + mend, max, &padding) - str;
511 end = (wrap += max - padding);
512
513 if (y <= clip_min_y) {
514 break;
515 }
516 }
517 else if (ELEM(str[mi], ' ', '-')) {
518 wrap = i + 1;
519 mend = mi + 1;
520 }
521 }
522
523 /* Draw the remaining text. */
524 for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; a++) {
525 if (use_syntax) {
526 if (fmt_prev != format[a]) {
527 format_draw_color(tdc, fmt_prev = format[a]);
528 }
529 }
530
531 const int c_len = BLI_str_utf8_size_safe(str + ma);
532 x += text_font_draw_character_utf8(tdc, x, y, str + ma, c_len);
533 ma += c_len;
534 }
535
537
538 return lines;
539}
540
541static void space_text_draw(const SpaceText *st,
542 const TextDrawContext *tdc,
543 char *str,
544 int cshift,
545 int maxwidth,
546 int x,
547 int y,
548 const char *format)
549{
550 const bool use_syntax = (tdc->syntax_highlight && format);
551 FlattenString fs;
552 int n, w = 0, padding, amount = 0;
553 const char *in = nullptr;
554
555 for (n = flatten_string(st, &fs, str), str = fs.buf; n > 0; n--) {
556 const int columns = BLI_str_utf8_char_width_safe(str);
557 const int size = BLI_str_utf8_size_safe(str);
558
559 if (!in) {
560 if (w >= cshift) {
561 padding = w - cshift;
562 in = str;
563 }
564 else if (format) {
565 format++;
566 }
567 }
568 if (in) {
569 if (maxwidth && w + columns > cshift + maxwidth) {
570 break;
571 }
572 amount++;
573 }
574
575 w += columns;
576 str += size;
577 }
578 if (!in) {
580 return; /* String is shorter than shift or ends with a padding. */
581 }
582
583 x += tdc->char_width_px * padding;
584
585 if (use_syntax) {
586 int a, str_shift = 0;
587 char fmt_prev = 0xff;
588
589 for (a = 0; a < amount; a++) {
590 if (format[a] != fmt_prev) {
591 format_draw_color(tdc, fmt_prev = format[a]);
592 }
593 const int c_len = BLI_str_utf8_size_safe(in + str_shift);
594 x += text_font_draw_character_utf8(tdc, x, y, in + str_shift, c_len);
595 str_shift += c_len;
596 }
597 }
598 else {
599 text_font_draw(tdc, x, y, in);
600 }
601
603}
604
606
607/* -------------------------------------------------------------------- */
610
611struct DrawCache {
614
615 /* This is needed to check cache relevance. */
620
622 bool update;
623 int valid_head, valid_tail; /* Amount of unchanged lines. */
624};
625
627{
628 DrawCache *drawcache = MEM_new_zeroed<DrawCache>("text draw cache");
629
630 drawcache->winx = -1;
631 drawcache->nlines = st->text->lines.count();
632 drawcache->text_id[0] = '\0';
633
634 st->runtime->drawcache = drawcache;
635}
636
637static void space_text_update_drawcache(SpaceText *st, const ARegion *region)
638{
639 DrawCache *drawcache;
640 bool full_update = false;
641 int nlines = 0;
642 Text *txt = st->text;
643
644 if (st->runtime->drawcache == nullptr) {
646 }
647
649
650 drawcache = static_cast<DrawCache *>(st->runtime->drawcache);
651 nlines = drawcache->nlines;
652
653 /* Check if full cache update is needed. */
654
655 /* Area was resized. */
656 full_update |= drawcache->winx != region->winx;
657 /* Word-wrapping option was toggled. */
658 full_update |= drawcache->wordwrap != st->wordwrap;
659 /* Word-wrapping option was toggled. */
660 full_update |= drawcache->showlinenrs != st->showlinenrs;
661 /* Word-wrapping option was toggled. */
662 full_update |= drawcache->tabnumber != st->tabnumber;
663 /* Word-wrapping option was toggled. */
664 full_update |= drawcache->line_height != st->runtime->line_height_px;
665 /* Word-wrapping option was toggled. */
666 full_update |= drawcache->char_width_px != st->runtime->char_width_px;
667 /* Text datablock was changed. */
668 full_update |= !STREQLEN(drawcache->text_id, txt->id.name + 2, MAX_ID_NAME - 2);
669
670 if (st->wordwrap) {
671 /* Update line heights. */
672 if (full_update || !drawcache->line_height_table) {
673 drawcache->valid_head = 0;
674 drawcache->valid_tail = 0;
675 drawcache->update = true;
676 }
677
678 if (drawcache->update) {
679 TextLine *line = st->text->lines.first();
680 int lineno = 0, lines_count;
681 int *fp = drawcache->line_height_table, *new_tail, *old_tail;
682
683 nlines = txt->lines.count();
684
685 if (fp) {
686 fp = static_cast<int *>(MEM_realloc_uninitialized(fp, sizeof(int) * nlines));
687 }
688 else {
689 fp = MEM_new_array_zeroed<int>(nlines, "text drawcache line_height");
690 }
691
692 drawcache->valid_tail = drawcache->valid_head = 0;
693 old_tail = fp + drawcache->nlines - drawcache->valid_tail;
694 new_tail = fp + nlines - drawcache->valid_tail;
695 memmove(new_tail, old_tail, drawcache->valid_tail);
696
697 drawcache->total_lines = 0;
698
699 if (st->showlinenrs) {
701 }
702
703 while (line) {
704 if (drawcache->valid_head) { /* We're inside valid head lines. */
705 lines_count = fp[lineno];
706 drawcache->valid_head--;
707 }
708 else if (lineno > new_tail - fp) { /* We-re inside valid tail lines. */
709 lines_count = fp[lineno];
710 }
711 else {
712 lines_count = space_text_get_visible_lines(st, region, line->line);
713 }
714
715 fp[lineno] = lines_count;
716
717 line = line->next;
718 lineno++;
719 drawcache->total_lines += lines_count;
720 }
721
722 drawcache->line_height_table = fp;
723 }
724 }
725 else {
726 MEM_SAFE_DELETE(drawcache->line_height_table);
727
728 if (full_update || drawcache->update) {
729 nlines = txt->lines.count();
730
731 if (st->showlinenrs) {
733 }
734 }
735
736 drawcache->total_lines = nlines;
737 }
738
739 drawcache->nlines = nlines;
740
741 /* Store settings. */
742 drawcache->winx = region->winx;
743 drawcache->wordwrap = st->wordwrap;
744 drawcache->line_height = st->runtime->line_height_px;
745 drawcache->char_width_px = st->runtime->char_width_px;
746 drawcache->showlinenrs = st->showlinenrs;
747 drawcache->tabnumber = st->tabnumber;
748
749 STRNCPY(drawcache->text_id, txt->id.name + 2);
750
751 /* Clear update flag. */
752 drawcache->update = false;
753 drawcache->valid_head = 0;
754 drawcache->valid_tail = 0;
755}
756
758{
759 /* This happens if text editor ops are called from Python. */
760 if (st == nullptr) {
761 return;
762 }
763
764 if (st->runtime->drawcache != nullptr) {
765 DrawCache *drawcache = static_cast<DrawCache *>(st->runtime->drawcache);
766 Text *txt = st->text;
767
768 if (drawcache->update) {
769 /* Happens when tagging update from space listener. */
770 /* Should do nothing to prevent locally tagged cache be fully recalculated. */
771 return;
772 }
773
774 if (!full) {
775 int sellno = BLI_findindex(&txt->lines, txt->sell);
776 int curlno = BLI_findindex(&txt->lines, txt->curl);
777
778 if (curlno < sellno) {
779 drawcache->valid_head = curlno;
780 drawcache->valid_tail = drawcache->nlines - sellno - 1;
781 }
782 else {
783 drawcache->valid_head = sellno;
784 drawcache->valid_tail = drawcache->nlines - curlno - 1;
785 }
786
787 /* Quick cache recalculation is also used in delete operator,
788 * which could merge lines which are adjacent to current selection lines
789 * expand recalculate area to this lines. */
790 if (drawcache->valid_head > 0) {
791 drawcache->valid_head--;
792 }
793 if (drawcache->valid_tail > 0) {
794 drawcache->valid_tail--;
795 }
796 }
797 else {
798 drawcache->valid_head = 0;
799 drawcache->valid_tail = 0;
800 }
801
802 drawcache->update = true;
803 }
804}
805
807{
808 DrawCache *drawcache = static_cast<DrawCache *>(st->runtime->drawcache);
809
810 if (drawcache) {
811 if (drawcache->line_height_table) {
812 MEM_delete(drawcache->line_height_table);
813 }
814
815 MEM_delete(drawcache);
816 }
817}
818
820
821/* -------------------------------------------------------------------- */
824
826static int space_text_get_visible_lines_no(const SpaceText *st, int lineno)
827{
828 const DrawCache *drawcache = static_cast<const DrawCache *>(st->runtime->drawcache);
829
830 return drawcache->line_height_table[lineno];
831}
832
833int space_text_get_visible_lines(const SpaceText *st, const ARegion *region, const char *str)
834{
835 int i, j, start, end, max, lines, chars;
836 char ch;
837
838 max = space_text_wrap_width(st, region);
839 lines = 1;
840 start = 0;
841 end = max;
842 for (i = 0, j = 0; str[j]; j += BLI_str_utf8_size_safe(str + j)) {
843 const int columns = BLI_str_utf8_char_width_safe(str + j); /* = 1 for tab. */
844
845 /* Mimic replacement of tabs. */
846 ch = str[j];
847 if (ch == '\t') {
848 chars = st->tabnumber - i % st->tabnumber;
849 ch = ' ';
850 }
851 else {
852 chars = 1;
853 }
854
855 while (chars--) {
856 if (i + columns - start > max) {
857 lines++;
858 start = std::min(end, i);
859 end += max;
860 }
861 else if (ELEM(ch, ' ', '-')) {
862 end = i + 1;
863 }
864
865 i += columns;
866 }
867 }
868
869 return lines;
870}
871
873 const ARegion *region,
874 const TextLine *from,
875 const TextLine *to)
876{
877 if (st->wordwrap) {
878 int ret = 0;
879 const TextLine *tmp = from;
880
881 /* Look forwards. */
882 while (tmp) {
883 if (tmp == to) {
884 return ret;
885 }
886 ret += space_text_get_visible_lines(st, region, tmp->line);
887 tmp = tmp->next;
888 }
889
890 return ret;
891 }
892 return txt_get_span(from, to);
893}
894
896{
897 DrawCache *drawcache;
898
899 space_text_update_drawcache(st, region);
900 drawcache = static_cast<DrawCache *>(st->runtime->drawcache);
901
902 return drawcache->total_lines;
903}
904
906
907/* -------------------------------------------------------------------- */
910
911static void calc_text_rcts(SpaceText *st, ARegion *region, rcti *r_scroll, rcti *r_back)
912{
913 int lhlstart, lhlend, ltexth, sell_off, curl_off;
914 short barheight, barstart, hlstart, hlend, blank_lines;
915 short pix_available, pix_top_margin, pix_bottom_margin, pix_bardiff;
916
917 pix_top_margin = (0.4 * U.widget_unit);
918 pix_bottom_margin = (0.4 * U.widget_unit);
919 pix_available = region->winy - pix_top_margin - pix_bottom_margin;
920 ltexth = space_text_get_total_lines(st, region);
921 blank_lines = st->runtime->viewlines / 2;
922
923 /* Nicer code: use scroll rect for entire bar. */
924 r_back->xmin = region->winx - (0.6 * U.widget_unit);
925 r_back->xmax = region->winx;
926 r_back->ymin = 0;
927 r_back->ymax = region->winy;
928
929 r_scroll->xmax = region->winx - (0.2 * U.widget_unit);
930 r_scroll->xmin = r_scroll->xmax - (0.4 * U.widget_unit);
931 r_scroll->ymin = pix_top_margin;
932 r_scroll->ymax = pix_available;
933
934 /* When re-sizing a 2D Viewport with the bar at the bottom to a greater height
935 * more blank lines will be added. */
936 if (ltexth + blank_lines < st->top + st->runtime->viewlines) {
937 blank_lines = st->top + st->runtime->viewlines - ltexth;
938 }
939
940 ltexth += blank_lines;
941
942 barheight = (ltexth > 0) ? (st->runtime->viewlines * pix_available) / ltexth : 0;
943 pix_bardiff = 0;
944 if (barheight < 20) {
945 pix_bardiff = 20 - barheight; /* Take into account the now non-linear sizing of the bar. */
946 barheight = 20;
947 }
948 barstart = (ltexth > 0) ? ((pix_available - pix_bardiff) * st->top) / ltexth : 0;
949
950 st->runtime->scroll_region_handle = *r_scroll;
951 st->runtime->scroll_region_handle.ymax -= barstart;
953
954 CLAMP(st->runtime->scroll_region_handle.ymin, pix_bottom_margin, region->winy - pix_top_margin);
955 CLAMP(st->runtime->scroll_region_handle.ymax, pix_bottom_margin, region->winy - pix_top_margin);
956
957 st->runtime->scroll_px_per_line = (pix_available > 0) ? float(ltexth) / pix_available : 0;
958 st->runtime->scroll_px_per_line = std::max(st->runtime->scroll_px_per_line, 0.1f);
959
960 curl_off = space_text_get_span_wrap(st, region, st->text->lines.first(), st->text->curl);
961 sell_off = space_text_get_span_wrap(st, region, st->text->lines.first(), st->text->sell);
962 lhlstart = std::min(curl_off, sell_off);
963 lhlend = std::max(curl_off, sell_off);
964
965 if (ltexth > 0) {
966 hlstart = (lhlstart * pix_available) / ltexth;
967 hlend = (lhlend * pix_available) / ltexth;
968
969 /* The scroll-bar is non-linear sized. */
970 if (pix_bardiff > 0) {
971 /* The start of the highlight is in the current viewport. */
972 if (st->runtime->viewlines && lhlstart >= st->top &&
973 lhlstart <= st->top + st->runtime->viewlines)
974 {
975 /* Speed the progression of the start of the highlight through the scroll-bar. */
976 hlstart = (((pix_available - pix_bardiff) * lhlstart) / ltexth) +
977 (pix_bardiff * (lhlstart - st->top) / st->runtime->viewlines);
978 }
979 else if (lhlstart > st->top + st->runtime->viewlines && hlstart < barstart + barheight &&
980 hlstart > barstart)
981 {
982 /* Push `hlstart` down. */
983 hlstart = barstart + barheight;
984 }
985 else if (lhlend > st->top && lhlstart < st->top && hlstart > barstart) {
986 /* Fill out start. */
987 hlstart = barstart;
988 }
989
990 if (hlend <= hlstart) {
991 hlend = hlstart + 2;
992 }
993
994 /* The end of the highlight is in the current viewport. */
995 if (st->runtime->viewlines && lhlend >= st->top &&
996 lhlend <= st->top + st->runtime->viewlines)
997 {
998 /* Speed the progression of the end of the highlight through the scroll-bar. */
999 hlend = (((pix_available - pix_bardiff) * lhlend) / ltexth) +
1000 (pix_bardiff * (lhlend - st->top) / st->runtime->viewlines);
1001 }
1002 else if (lhlend < st->top && hlend >= barstart - 2 && hlend < barstart + barheight) {
1003 /* Push `hlend` up. */
1004 hlend = barstart;
1005 }
1006 else if (lhlend > st->top + st->runtime->viewlines &&
1007 lhlstart < st->top + st->runtime->viewlines && hlend < barstart + barheight)
1008 {
1009 /* Fill out end. */
1010 hlend = barstart + barheight;
1011 }
1012
1013 if (hlend <= hlstart) {
1014 hlstart = hlend - 2;
1015 }
1016 }
1017 }
1018 else {
1019 hlstart = 0;
1020 hlend = 0;
1021 }
1022
1023 if (hlend - hlstart < 2) {
1024 hlend = hlstart + 2;
1025 }
1026
1027 st->runtime->scroll_region_select = *r_scroll;
1028 st->runtime->scroll_region_select.ymax = region->winy - pix_top_margin - hlstart;
1029 st->runtime->scroll_region_select.ymin = region->winy - pix_top_margin - hlend;
1030
1031 CLAMP(st->runtime->scroll_region_select.ymin, pix_bottom_margin, region->winy - pix_top_margin);
1032 CLAMP(st->runtime->scroll_region_select.ymax, pix_bottom_margin, region->winy - pix_top_margin);
1033}
1034
1035static void draw_textscroll(const SpaceText *st, const rcti *scroll, const rcti *back)
1036{
1037 const bTheme *btheme = ui::theme::theme_get();
1038 uiWidgetColors wcol = btheme->tui.wcol_scroll;
1039 float col[4];
1040 float rad;
1041
1042 /* Background so highlights don't go behind the scroll-bar. */
1043 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1046 immRectf(pos, back->xmin, back->ymin, back->xmax, back->ymax);
1048
1050 scroll,
1053
1054 draw_roundbox_corner_set(ui::CNR_ALL);
1058 col[3] = 0.18f;
1059
1060 rctf rect;
1061 rect.xmin = st->runtime->scroll_region_select.xmin + 1;
1062 rect.xmax = st->runtime->scroll_region_select.xmax - 1;
1065 ui::draw_roundbox_aa(&rect, true, rad, col);
1066}
1067
1069
1070/* -------------------------------------------------------------------- */
1073
1074static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc, ARegion *region)
1075{
1076 SuggItem *item, *first, *last, *sel;
1077 char str[SUGG_LIST_WIDTH * BLI_UTF8_MAX + 1];
1078 int offl, offc, vcurl, vcurc;
1079 int w, boxw = 0, boxh, i, x, y, *top;
1080 const int line_height = TXT_LINE_HEIGHT(st);
1081 const int margin_x = 2;
1082
1083 if (!st->text) {
1084 return;
1085 }
1086 if (!texttool_text_is_active(st->text)) {
1087 return;
1088 }
1089
1090 first = texttool_suggest_first();
1091 last = texttool_suggest_last();
1092
1093 if (!first || !last) {
1094 return;
1095 }
1096
1099 top = texttool_suggest_top();
1100
1101 space_text_wrap_offset(st, region, st->text->curl, st->text->curc, &offl, &offc);
1102 vcurl = txt_get_span(st->text->lines.first(), st->text->curl) - st->top + offl;
1103 vcurc = space_text_get_char_pos(st, st->text->curl->line, st->text->curc) - st->left + offc;
1104
1105 x = TXT_BODY_LEFT(st) + (vcurc * st->runtime->char_width_px);
1106 y = region->winy - (vcurl + 1) * line_height - 2;
1107
1108 /* Offset back so the start of the text lines up with the suggestions,
1109 * not essential but makes suggestions easier to follow. */
1110 x -= st->runtime->char_width_px *
1111 (st->text->curc - text_find_identifier_start(st->text->curl->line, st->text->curc));
1112
1113 boxw = SUGG_LIST_WIDTH * st->runtime->char_width_px + 20;
1114 boxh = SUGG_LIST_SIZE * line_height + 8;
1115
1116 if (x + boxw > region->winx) {
1117 x = std::max(0, region->winx - boxw);
1118 }
1119
1120 /* Not needed but stands out nicer. */
1121 {
1122 rctf rect;
1123 rect.xmin = x;
1124 rect.xmax = x + boxw;
1125 rect.ymin = y - boxh;
1126 rect.ymax = y;
1127 ui::draw_dropshadow(&rect, 0.0f, 8.0f, 1.0f, 0.5f);
1128 }
1129
1130 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1132
1134 immRectf(pos, x - 1, y + 1, x + boxw + 1, y - boxh - 1);
1136 immRectf(pos, x, y, x + boxw, y - boxh);
1137
1139
1140 /* Set the top `item` of the visible list. */
1141 for (i = 0, item = first; i < *top && item->next; i++, item = item->next) {
1142 /* Pass. */
1143 }
1144
1145 for (i = 0; i < SUGG_LIST_SIZE && item; i++, item = item->next) {
1146 int len = txt_utf8_forward_columns(item->name, SUGG_LIST_WIDTH, nullptr) - item->name;
1147
1148 y -= line_height;
1149
1150 BLI_strncpy_utf8(str, item->name, len + 1);
1151
1153
1154 if (item == sel) {
1156 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1158
1160 immRectf(posi, x + margin_x, y - 3, x + margin_x + w, y + line_height - 3);
1161
1163 }
1164
1165 format_draw_color(tdc, item->type);
1166 space_text_draw(st, tdc, str, 0, 0, x + margin_x, y - 1, nullptr);
1167
1168 if (item == last) {
1169 break;
1170 }
1171 }
1172}
1173
1175
1176/* -------------------------------------------------------------------- */
1179
1180static void draw_text_decoration(SpaceText *st, ARegion *region)
1181{
1182 Text *text = st->text;
1183 int vcurl, vcurc, vsell, vselc;
1184 bool hidden = false;
1185 int offl, offc;
1186 const int line_height = TXT_LINE_HEIGHT(st);
1187
1188 /* Convert to view space character coordinates to determine if cursor is hidden. */
1189 space_text_wrap_offset(st, region, text->sell, text->selc, &offl, &offc);
1190 vsell = txt_get_span(text->lines.first(), text->sell) - st->top + offl;
1191 vselc = space_text_get_char_pos(st, text->sell->line, text->selc) - st->left + offc;
1192
1193 if (vselc < 0) {
1194 vselc = 0;
1195 hidden = true;
1196 }
1197
1198 if (text->curl == text->sell && text->curc == text->selc && !st->line_hlight && hidden) {
1199 /* Nothing to draw here. */
1200 return;
1201 }
1202
1203 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1205
1206 /* Draw the selection. */
1207 if (text->curl != text->sell || text->curc != text->selc) {
1208 /* Convert all to view space character coordinates. */
1209 space_text_wrap_offset(st, region, text->curl, text->curc, &offl, &offc);
1210 vcurl = txt_get_span(text->lines.first(), text->curl) - st->top + offl;
1211 vcurc = space_text_get_char_pos(st, text->curl->line, text->curc) - st->left + offc;
1212
1213 vcurc = std::max(vcurc, 0);
1214
1216 /* Translucent theme colors must blend properly with the background rather than writing
1217 * translucency directly to the region, which must remain opaque. See #163821. */
1219
1220 int x = TXT_BODY_LEFT(st);
1221 int y = region->winy;
1222 if (st->flags & ST_SCROLL_SELECT) {
1223 y += st->runtime->scroll_ofs_px[1];
1224 }
1225
1226 if (vcurl == vsell) {
1227 y -= vcurl * line_height;
1228
1229 if (vcurc < vselc) {
1230 immRectf(pos,
1231 x + vcurc * st->runtime->char_width_px,
1232 y,
1233 x + vselc * st->runtime->char_width_px,
1234 y - line_height);
1235 }
1236 else {
1237 immRectf(pos,
1238 x + vselc * st->runtime->char_width_px,
1239 y,
1240 x + vcurc * st->runtime->char_width_px,
1241 y - line_height);
1242 }
1243 }
1244 else {
1245 int froml, fromc, tol, toc;
1246
1247 if (vcurl < vsell) {
1248 froml = vcurl;
1249 tol = vsell;
1250 fromc = vcurc;
1251 toc = vselc;
1252 }
1253 else {
1254 froml = vsell;
1255 tol = vcurl;
1256 fromc = vselc;
1257 toc = vcurc;
1258 }
1259
1260 y -= froml * line_height;
1261
1262 immRectf(pos,
1263 x + fromc * st->runtime->char_width_px - U.pixelsize,
1264 y,
1265 region->winx,
1266 y - line_height);
1267 y -= line_height;
1268
1269 for (int i = froml + 1; i < tol; i++) {
1270 immRectf(pos, x - U.pixelsize, y, region->winx, y - line_height);
1271 y -= line_height;
1272 }
1273
1274 if (x + toc * st->runtime->char_width_px > x) {
1275 immRectf(pos, x - U.pixelsize, y, x + toc * st->runtime->char_width_px, y - line_height);
1276 }
1277 y -= line_height;
1278 }
1280 /* Quiet warnings. */
1281 UNUSED_VARS(x, y);
1282 }
1283
1284 if (st->line_hlight) {
1285 int y1, y2;
1286
1287 if (st->wordwrap) {
1288 int visible_lines = space_text_get_visible_lines(st, region, text->sell->line);
1289
1290 space_text_wrap_offset_in_line(st, region, text->sell, text->selc, &offl, &offc);
1291
1292 y1 = region->winy - (vsell - offl) * line_height;
1293 if (st->flags & ST_SCROLL_SELECT) {
1294 y1 += st->runtime->scroll_ofs_px[1];
1295 }
1296 y2 = y1 - (line_height * visible_lines);
1297 }
1298 else {
1299 y1 = region->winy - vsell * line_height;
1300 if (st->flags & ST_SCROLL_SELECT) {
1301 y1 += st->runtime->scroll_ofs_px[1];
1302 }
1303 y2 = y1 - line_height;
1304 }
1305
1306 if (!(y1 < 0 || y2 > region->winy)) { /* Check we need to draw. */
1307 float highlight_color[4];
1308 ui::theme::get_color_4fv(TH_TEXT, highlight_color);
1309 highlight_color[3] = 0.1f;
1310 immUniformColor4fv(highlight_color);
1312 immRectf(pos, 0, y1, region->winx, y2);
1314 }
1315 }
1316
1317 if (!hidden) {
1318 /* Draw the cursor itself (we draw the sel. cursor as this is the leading edge). */
1319 int x = TXT_BODY_LEFT(st) + (vselc * st->runtime->char_width_px);
1320 int y = region->winy - vsell * line_height;
1321 if (st->flags & ST_SCROLL_SELECT) {
1322 y += st->runtime->scroll_ofs_px[1];
1323 }
1324
1326
1327 if (st->overwrite) {
1328 char ch = text->sell->line[text->selc];
1329
1330 y += TXT_LINE_SPACING(st);
1331 int w = st->runtime->char_width_px;
1332 if (ch == '\t') {
1333 w *= st->tabnumber - (vselc + st->left) % st->tabnumber;
1334 }
1335
1336 immRectf(pos,
1337 x,
1338 y - line_height - U.pixelsize,
1339 x + w + U.pixelsize,
1340 y - line_height - (3 * U.pixelsize));
1341 }
1342 else {
1343 immRectf(pos, x - U.pixelsize, y, x + U.pixelsize, y - line_height);
1344 }
1345 }
1346
1348}
1349
1351
1352/* -------------------------------------------------------------------- */
1355
1356static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegion *region)
1357{
1358 TextLine *startl, *endl, *linep;
1359 Text *text = st->text;
1360 int b, fc, find, stack, viewc, viewl, offl, offc, x, y;
1361 int startc, endc, c;
1362
1363 char ch;
1364
1365 /* Syntax_highlight must be on or else the format string will be null. */
1366 if (!text->curl || !tdc->syntax_highlight) {
1367 return;
1368 }
1369
1370 startl = text->curl;
1371 startc = text->curc;
1372 b = text_check_bracket(startl->line[startc]);
1373 if (b == 0 && startc > 0) {
1374 b = text_check_bracket(startl->line[--startc]);
1375 }
1376 if (b == 0) {
1377 return;
1378 }
1379
1380 linep = startl;
1381 c = startc;
1382 fc = space_text_get_format_index(st, linep->line, startc);
1383 endl = nullptr;
1384 endc = -1;
1385 find = -b;
1386 stack = 0;
1387
1388 /* Don't highlight brackets if syntax HL is off or bracket in string or comment. */
1389 if (!linep->format || linep->format[fc] == FMT_TYPE_STRING ||
1390 linep->format[fc] == FMT_TYPE_COMMENT)
1391 {
1392 return;
1393 }
1394
1395 if (b > 0) {
1396 /* Opening bracket, search forward for close. */
1397 fc++;
1398 c += BLI_str_utf8_size_safe(linep->line + c);
1399 while (linep) {
1400 while (c < linep->len) {
1401 if (linep->format && linep->format[fc] != FMT_TYPE_STRING &&
1402 linep->format[fc] != FMT_TYPE_COMMENT)
1403 {
1404 b = text_check_bracket(linep->line[c]);
1405 if (b == find) {
1406 if (stack == 0) {
1407 endl = linep;
1408 endc = c;
1409 break;
1410 }
1411 stack--;
1412 }
1413 else if (b == -find) {
1414 stack++;
1415 }
1416 }
1417 if (linep->line[c] == '\t') {
1418 fc += st->tabnumber - fc % st->tabnumber;
1419 }
1420 else {
1421 fc++;
1422 }
1423 c += BLI_str_utf8_size_safe(linep->line + c);
1424 }
1425 if (endl) {
1426 break;
1427 }
1428 linep = linep->next;
1429 c = 0;
1430 fc = 0;
1431 }
1432 }
1433 else {
1434 /* Closing bracket, search backward for open. */
1435 if (c > 0) {
1436 c -= linep->line + c - BLI_str_find_prev_char_utf8(linep->line + c, linep->line);
1437 if (linep->line[c] == '\t') {
1438 fc = space_text_get_format_index(st, linep->line, c);
1439 }
1440 else {
1441 fc--;
1442 }
1443 }
1444 else {
1445 /* For balanced brackets: `fc == 0`. */
1446 fc--;
1447 }
1448 while (linep) {
1449 while (fc >= 0) {
1450 if (linep->format && linep->format[fc] != FMT_TYPE_STRING &&
1451 linep->format[fc] != FMT_TYPE_COMMENT)
1452 {
1453 b = text_check_bracket(linep->line[c]);
1454 if (b == find) {
1455 if (stack == 0) {
1456 endl = linep;
1457 endc = c;
1458 break;
1459 }
1460 stack--;
1461 }
1462 else if (b == -find) {
1463 stack++;
1464 }
1465 }
1466 if (c > 0) {
1467 c -= linep->line + c - BLI_str_find_prev_char_utf8(linep->line + c, linep->line);
1468 if (linep->line[c] == '\t') {
1469 fc = space_text_get_format_index(st, linep->line, c);
1470 }
1471 else {
1472 fc--;
1473 }
1474 }
1475 else {
1476 /* For balanced brackets: `fc == 0`. */
1477 fc--;
1478 }
1479 }
1480 if (endl) {
1481 break;
1482 }
1483 linep = linep->prev;
1484 if (linep) {
1485 /* Without a format string, brackets can't be matched on this line;
1486 * the format check skips unnecessary iteration. */
1487 if (linep->format && linep->len) {
1488 c = BLI_str_find_prev_char_utf8(linep->line + linep->len, linep->line) - linep->line;
1489 fc = space_text_get_format_index(st, linep->line, c);
1490 }
1491 else {
1492 /* Empty or unformatted line: nothing to scan, skip to the previous line. */
1493 fc = -1;
1494 }
1495 }
1496 }
1497 }
1498
1499 if (!endl || endc == -1) {
1500 return;
1501 }
1502
1504 x = TXT_BODY_LEFT(st);
1505 y = region->winy - st->runtime->line_height_px;
1506 if (st->flags & ST_SCROLL_SELECT) {
1507 y += st->runtime->scroll_ofs_px[1];
1508 }
1509
1510 /* Draw opening bracket. */
1511 ch = startl->line[startc];
1512 space_text_wrap_offset(st, region, startl, startc, &offl, &offc);
1513 viewc = space_text_get_char_pos(st, startl->line, startc) - st->left + offc;
1514
1515 if (viewc >= 0) {
1516 viewl = txt_get_span(text->lines.first(), startl) - st->top + offl;
1517
1519 tdc, x + viewc * st->runtime->char_width_px, y - viewl * TXT_LINE_HEIGHT(st), ch);
1521 tdc, x + viewc * st->runtime->char_width_px + 1, y - viewl * TXT_LINE_HEIGHT(st), ch);
1522 }
1523
1524 /* Draw closing bracket. */
1525 ch = endl->line[endc];
1526 space_text_wrap_offset(st, region, endl, endc, &offl, &offc);
1527 viewc = space_text_get_char_pos(st, endl->line, endc) - st->left + offc;
1528
1529 if (viewc >= 0) {
1530 viewl = txt_get_span(text->lines.first(), endl) - st->top + offl;
1531
1533 tdc, x + viewc * st->runtime->char_width_px, y - viewl * TXT_LINE_HEIGHT(st), ch);
1535 tdc, x + viewc * st->runtime->char_width_px + 1, y - viewl * TXT_LINE_HEIGHT(st), ch);
1536 }
1537}
1538
1540
1541/* -------------------------------------------------------------------- */
1544
1546{
1547 TextDrawContext tdc = {0};
1548 Text *text = st->text;
1549 TextFormatType *tft;
1550 TextLine *tmp;
1551 rcti scroll, back;
1552 char linenr[12];
1553 int i, x, y, winx, linecount = 0, lineno = 0;
1554 int wraplinecount = 0, wrap_skip = 0;
1555 int margin_column_x;
1556
1557 /* If no text, nothing to do. */
1558 if (!text) {
1559 return;
1560 }
1561
1562 /* DPI controlled line height and font size. */
1563 st->runtime->line_height_px = (U.widget_unit * st->line_height) / 20;
1564
1565 /* Don't draw lines below this. */
1566 const int clip_min_y = -(st->runtime->line_height_px - 1);
1567
1568 st->runtime->viewlines = (st->runtime->line_height_px) ?
1569 int(region->winy - clip_min_y) / TXT_LINE_HEIGHT(st) :
1570 0;
1571
1573
1574 space_text_update_drawcache(st, region);
1575
1576 /* Make sure all the positional pointers exist. */
1577 if (!text->curl || !text->sell || !text->lines.first_ || !text->lines.last()) {
1578 txt_clean_text(text);
1579 }
1580
1581 /* Update rectangles for scroll. */
1582 calc_text_rcts(st, region, &scroll, &back); /* Scroll will hold the entire bar size. */
1583
1584 /* Update syntax formatting if needed. */
1585 tft = ED_text_format_get(text);
1586 tmp = text->lines.first();
1587 lineno = 0;
1588 for (i = 0; i < st->top && tmp; i++) {
1589 if (tdc.syntax_highlight && !tmp->format) {
1590 tft->format_line(st, tmp, false);
1591 }
1592
1593 if (st->wordwrap) {
1594 int lines = space_text_get_visible_lines_no(st, lineno);
1595
1596 if (wraplinecount + lines > st->top) {
1597 wrap_skip = st->top - wraplinecount;
1598 break;
1599 }
1600
1601 wraplinecount += lines;
1602 tmp = tmp->next;
1603 linecount++;
1604 }
1605 else {
1606 tmp = tmp->next;
1607 linecount++;
1608 }
1609
1610 lineno++;
1611 }
1612
1613 text_font_begin(&tdc);
1614
1615 tdc.char_width_px = max_ii(int(BLF_fixed_width(tdc.font_id)), 1);
1618
1619 /* Draw line numbers background. */
1620 if (st->showlinenrs) {
1621 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1624 immRectf(pos, 0, 0, TXT_NUMCOL_WIDTH(st), region->winy);
1626 }
1627 else {
1628 st->runtime->line_number_display_digits = 0; /* Not used. */
1629 }
1630
1631 x = TXT_BODY_LEFT(st);
1632 y = region->winy - st->runtime->line_height_px;
1633 int viewlines = st->runtime->viewlines;
1634 if (st->flags & ST_SCROLL_SELECT) {
1635 y += st->runtime->scroll_ofs_px[1];
1636 viewlines += 1;
1637 }
1638
1639 winx = region->winx - TXT_SCROLL_WIDTH;
1640
1641 /* Draw cursor, margin, selection and highlight. */
1642 draw_text_decoration(st, region);
1643
1644 /* Draw the text. */
1646
1647 for (i = 0; y > clip_min_y && i < viewlines && tmp; i++, tmp = tmp->next) {
1648 if (tdc.syntax_highlight && !tmp->format) {
1649 tft->format_line(st, tmp, false);
1650 }
1651
1652 if (st->showlinenrs && !wrap_skip) {
1653 /* Draw line number. */
1655 (tmp == text->sell) ? TH_HILITE : TH_LINENUMBERS);
1656 SNPRINTF_UTF8(linenr, "%*d", st->runtime->line_number_display_digits, i + linecount + 1);
1657 text_font_draw(&tdc, TXT_NUMCOL_PAD * st->runtime->char_width_px, y, linenr);
1658 /* Change back to text color. */
1660 }
1661
1662 if (st->wordwrap) {
1663 /* Draw word wrapped text. */
1664 int lines = space_text_draw_wrapped(
1665 st, &tdc, tmp->line, x, y, winx - x, tmp->format, wrap_skip);
1666 y -= lines * TXT_LINE_HEIGHT(st);
1667 }
1668 else {
1669 /* Draw unwrapped text. */
1670 space_text_draw(st,
1671 &tdc,
1672 tmp->line,
1673 st->left,
1674 region->winx / st->runtime->char_width_px,
1675 x,
1676 y,
1677 tmp->format);
1678 y -= TXT_LINE_HEIGHT(st);
1679 }
1680
1681 wrap_skip = 0;
1682 }
1683
1684 if (st->flags & ST_SHOW_MARGIN) {
1685 margin_column_x = x + st->runtime->char_width_px * (st->margin_column - st->left);
1686 if (margin_column_x >= x) {
1688 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1690 float margin_color[4];
1691 ui::theme::get_color_4fv(TH_TEXT, margin_color);
1692 margin_color[3] = 0.2f;
1693 immUniformColor4fv(margin_color);
1695 immRectf(pos, margin_column_x, 0, margin_column_x + U.pixelsize, region->winy);
1698 }
1699 }
1700
1701 /* Draw other stuff. */
1702 draw_brackets(st, &tdc, region);
1703 draw_textscroll(st, &scroll, &back);
1704 /* Draw_documentation(st, region); - No longer supported. */
1705 draw_suggestion_list(st, &tdc, region);
1706
1707 text_font_end(&tdc);
1708}
1709
1711
1712/* -------------------------------------------------------------------- */
1715
1717{
1718 TextDrawContext tdc = {0};
1719
1721
1722 text_font_begin(&tdc);
1724 st->runtime->char_width_px = std::max(st->runtime->char_width_px, 1);
1725 text_font_end(&tdc);
1726}
1727
1729{
1731 if (area) {
1732 SpaceText *st = area->spacedata.first_as<SpaceText>();
1734 st->text = text;
1735 if (region) {
1736 ED_space_text_scroll_to_cursor(st, region, true);
1737 }
1739 return true;
1740 }
1741
1742 return false;
1743}
1744
1745void ED_space_text_scroll_to_cursor(SpaceText *st, ARegion *region, const bool center)
1746{
1747 Text *text;
1748 int i, x, winx = region->winx;
1749
1750 if (ELEM(nullptr, st, st->text, st->text->curl)) {
1751 return;
1752 }
1753
1754 text = st->text;
1755
1757
1758 i = txt_get_span(text->lines.first(), text->sell);
1759 if (st->wordwrap) {
1760 int offl, offc;
1761 space_text_wrap_offset(st, region, text->sell, text->selc, &offl, &offc);
1762 i += offl;
1763 }
1764
1765 if (center) {
1766 if (st->top + st->runtime->viewlines <= i || st->top > i) {
1767 st->top = i - st->runtime->viewlines / 2;
1768 }
1769 }
1770 else {
1771 if (st->top + st->runtime->viewlines <= i) {
1772 st->top = i - (st->runtime->viewlines - 1);
1773 }
1774 else if (st->top > i) {
1775 st->top = i;
1776 }
1777 }
1778
1779 if (st->wordwrap) {
1780 st->left = 0;
1781 }
1782 else {
1783 x = st->runtime->char_width_px *
1784 (space_text_get_char_pos(st, text->sell->line, text->selc) - st->left);
1785 winx -= TXT_BODY_LEFT(st) + TXT_SCROLL_WIDTH;
1786
1787 if (center) {
1788 if (x <= 0 || x > winx) {
1789 st->left += (x - winx / 2) / st->runtime->char_width_px;
1790 }
1791 }
1792 else {
1793 if (x <= 0) {
1794 st->left += ((x + 1) / st->runtime->char_width_px) - 1;
1795 }
1796 else if (x > winx) {
1797 st->left += ((x - (winx + 1)) / st->runtime->char_width_px) + 1;
1798 }
1799 }
1800 }
1801
1802 st->top = std::max(st->top, 0);
1803 st->left = std::max(st->left, 0);
1804
1805 st->runtime->scroll_ofs_px[0] = 0;
1806 st->runtime->scroll_ofs_px[1] = 0;
1807}
1808
1810{
1811 ARegion *region;
1812
1813 if (ELEM(nullptr, st, st->text, st->text->curl)) {
1814 return;
1815 }
1816
1818
1819 if (region) {
1820 ED_space_text_scroll_to_cursor(st, region, center);
1821 }
1822}
1823
1831
1838 const ARegion *region,
1839 TextLine *line,
1840 const int line_index,
1841 const int char_ofs)
1842{
1843 int offl, offc;
1844 space_text_wrap_offset(st, region, line, char_ofs, &offl, &offc);
1845 /* Handle tabs as well! */
1846 const int char_pos = space_text_get_char_pos(st, line->line, char_ofs);
1847
1848 const int x = TXT_BODY_LEFT(st) + ((char_pos + offc - st->left) * st->runtime->char_width_px);
1849 const int y = region->winy - ((line_index + offl - st->top) * TXT_LINE_HEIGHT(st));
1850 return int2{x, y};
1851}
1852
1853std::optional<int2> space_text_cursor_region_xy_get(const SpaceText *st, const ARegion *region)
1854{
1855 const Text *text = st->text;
1856 if (text == nullptr) {
1857 return std::nullopt;
1858 }
1859 const int line_index = txt_get_span(text->lines.first(), text->sell);
1860 /* NOTE: `ST_SCROLL_SELECT` doesn't need to be checked (offsetting by `scroll_ofs_px` the way
1861 * #draw_text_decoration does), since the caller treats the position as pending while
1862 * dragging. */
1863 return space_text_region_xy_from_cursor(st, region, text->sell, line_index, text->selc);
1864}
1865
1867 const ARegion *region,
1868 const int cursor_co[2],
1869 int r_pixel_co[2])
1870{
1871 Text *text = st->text;
1872
1873 if (!text) {
1874 return false;
1875 }
1876 TextLine *line = static_cast<TextLine *>(BLI_findlink(&text->lines, cursor_co[0]));
1877 if (!line) {
1878 return false;
1879 }
1880
1881 /* Convert character index to char byte offset. */
1882 const int char_ofs = BLI_str_utf8_offset_from_index(line->line, line->len, cursor_co[1]);
1883 if (char_ofs < 0 || char_ofs > line->len) {
1884 return false;
1885 }
1886
1887 /* All values are in-range, calculate the pixel offset. */
1888 const int2 xy = space_text_region_xy_from_cursor(st, region, line, cursor_co[0], char_ofs);
1889 r_pixel_co[0] = xy[0];
1890 r_pixel_co[1] = xy[1] - TXT_LINE_HEIGHT(st);
1891 return true;
1892}
1893
1895
1896} // namespace blender
#define BLF_DRAW_STR_DUMMY_MAX
Definition BLF_api.hh:456
#define BLI_UTF8_MAX
#define SNPRINTF_UTF8(dst, format,...)
#define MAX_ID_NAME
Definition DNA_ID.h:367
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_CURSOR
Definition WM_types.hh:533
#define NC_TEXT
Definition WM_types.hh:399
#define CLAMP(a, b, c)
#define UNUSED_VARS(...)
#define STREQLEN(a, b, n)
#define ELEM(...)
#define MEM_realloc_uninitialized(vmemh, len)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define str(s)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BLI_INLINE Texture * wrap(MTLTexture *texture)
void font_theme_color_set(int fontid, int colorid)
void get_color_3fv(int colorid, float col[3])
void get_color_4fv(int colorid, float col[4])
const bTheme * theme_get()
void draw_dropshadow(const rctf *rct, float radius, float width, float aspect, float alpha)
void draw_widget_scroll(uiWidgetColors *wcol, const rcti *rect, const rcti *slider, int state)
void draw_roundbox_aa(const rctf *rect, bool filled, float rad, const float color[4])
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
ScrArea * CTX_wm_area(const bContext *C)
int txt_get_span(const struct TextLine *from, const struct TextLine *to)
char * STRNCPY(char(&dst)[N], const char *src)
uint padding(uint offset, uint alignment)
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
static int text_font_draw(const TextDrawContext *tdc, int x, int y, const char *str)
Definition text_draw.cc:82
unsigned int uint
Definition BKE_text.h:55
void space_text_drawcache_tag_update(SpaceText *st, const bool full)
Definition text_draw.cc:757
int ED_space_text_visible_lines_get(const SpaceText *st)
Definition text_draw.cc:49
static int space_text_get_visible_lines_no(const SpaceText *st, int lineno)
Definition text_draw.cc:826
int BLI_str_utf8_char_width_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
GPUVertFormat * immVertexFormat()
static int text_font_draw_character_utf8(const TextDrawContext *tdc, int x, int y, const char *c, const int c_len)
Definition text_draw.cc:101
static int2 space_text_region_xy_from_cursor(const SpaceText *st, const ARegion *region, TextLine *line, const int line_index, const int char_ofs)
static void format_draw_color(const TextDrawContext *tdc, char formatchar)
Definition text_draw.cc:129
static void draw_textscroll(const SpaceText *st, const rcti *scroll, const rcti *back)
MINLINE int integer_digits_i(int i)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
SuggItem * texttool_suggest_first()
void space_text_update_cursor_moved(bContext *C)
SuggItem * texttool_suggest_last()
MINLINE int max_ii(int a, int b)
static int space_text_get_format_index(const SpaceText *st, const char *line, int cur)
Definition text_draw.cc:410
TextFormatType * ED_text_format_get(Text *text)
void immUniformColor4fv(const float rgba[4])
void space_text_wrap_offset(const SpaceText *st, const ARegion *region, TextLine *linein, int cursin, int *offl, int *offc)
Definition text_draw.cc:210
int space_text_get_span_wrap(const SpaceText *st, const ARegion *region, const TextLine *from, const TextLine *to)
Definition text_draw.cc:872
int text_find_identifier_start(const char *str, int i)
Definition text.cc:2386
bool ED_text_activate_in_screen(bContext *C, Text *text)
int space_text_get_total_lines(SpaceText *st, const ARegion *region)
Definition text_draw.cc:895
void immUnbindProgram()
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void ED_space_text_scroll_to_cursor(SpaceText *st, ARegion *region, bool center)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
SpaceText * CTX_wm_space_text(const bContext *C)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
static int space_text_draw_wrapped(const SpaceText *st, const TextDrawContext *tdc, const char *str, int x, int y, int w, const char *format, int skip)
Definition text_draw.cc:444
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
int BLI_str_utf8_offset_to_column(const char *str, size_t str_len, int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void immUniformThemeColor(int color_id)
void BLF_size(int fontid, float size)
Definition blf.cc:445
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void flatten_string_free(FlattenString *fs)
VecBase< int32_t, 2 > int2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static void space_text_update_drawcache(SpaceText *st, const ARegion *region)
Definition text_draw.cc:637
int space_text_get_char_pos(const SpaceText *st, const char *line, int cur)
Definition text_draw.cc:387
short texttool_text_is_active(struct Text *text)
void txt_clean_text(struct Text *text)
Definition text.cc:638
void immUniformThemeColorShade(int color_id, int offset)
static void draw_text_decoration(SpaceText *st, ARegion *region)
int space_text_wrap_width(const SpaceText *st, const ARegion *region)
Definition text_draw.cc:200
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:848
int space_text_get_visible_lines(const SpaceText *st, const ARegion *region, const char *str)
Definition text_draw.cc:833
static const char * txt_utf8_forward_columns(const char *str, int columns, int *padding)
Definition text_draw.cc:424
bool ED_space_text_region_location_from_cursor(const SpaceText *st, const ARegion *region, const int cursor_co[2], int r_pixel_co[2])
void space_text_free_caches(SpaceText *st)
Definition text_draw.cc:806
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void calc_text_rcts(SpaceText *st, ARegion *region, rcti *r_scroll, rcti *r_back)
Definition text_draw.cc:911
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void space_text_drawcache_init(SpaceText *st)
Definition text_draw.cc:626
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
int * texttool_suggest_top()
int BLF_draw_mono(int fontid, const char *str, size_t str_len, int char_width, int tab_columns) ATTR_NONNULL(2)
Definition blf.cc:609
SuggItem * texttool_suggest_selected()
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
int text_check_bracket(char ch)
Definition text.cc:2279
static void text_font_begin(const TextDrawContext *tdc)
Definition text_draw.cc:75
void space_text_wrap_offset_in_line(const SpaceText *st, const ARegion *region, TextLine *linein, int cursin, int *offl, int *offc)
Definition text_draw.cc:315
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:905
ScrArea * BKE_screen_find_big_area(const bScreen *screen, int spacetype, short min)
Definition screen.cc:1189
std::optional< int2 > space_text_cursor_region_xy_get(const SpaceText *st, const ARegion *region)
int blf_mono_font
Definition blf.cc:50
bScreen * CTX_wm_screen(const bContext *C)
static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegion *region)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
bool ED_text_is_syntax_highlight_supported(Text *text)
void draw_text_main(SpaceText *st, ARegion *region)
void space_text_scroll_to_cursor_with_area(SpaceText *st, ScrArea *area, const bool center)
static void space_text_draw(const SpaceText *st, const TextDrawContext *tdc, char *str, int cshift, int maxwidth, int x, int y, const char *format)
Definition text_draw.cc:541
MINLINE int min_ii(int a, int b)
void text_pop_suggest_list()
void space_text_update_character_width(SpaceText *st)
void immRectf(uint pos, float x1, float y1, float x2, float y2)
static int text_font_draw_character(const TextDrawContext *tdc, int x, int y, char c)
Definition text_draw.cc:92
static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc, ARegion *region)
@ FMT_TYPE_KEYWORD
@ FMT_TYPE_RESERVED
@ FMT_TYPE_WHITESPACE
@ FMT_TYPE_COMMENT
@ FMT_TYPE_SPECIAL
@ FMT_TYPE_DIRECTIVE
@ FMT_TYPE_STRING
@ FMT_TYPE_NUMERAL
@ FMT_TYPE_SYMBOL
@ FMT_TYPE_DEFAULT
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void text_font_end(const TextDrawContext *)
Definition text_draw.cc:80
static void space_text_draw_context_init(const SpaceText *st, TextDrawContext *tdc)
Definition text_draw.cc:67
uiWidgetColors wcol_scroll
ThemeUI tui
char text_id[MAX_ID_NAME - 2]
Definition text_draw.cc:619
ListBaseT< SpaceLink > spacedata
eSpaceText_Flags flags
ed::text::SpaceText_Runtime * runtime
struct SuggItem * next
void(*) format_line(SpaceText *st, TextLine *line, bool do_next)
struct TextLine * prev
struct TextLine * next
#define SUGG_LIST_WIDTH
#define TXT_LINE_SPACING(st)
#define TXT_LINE_HEIGHT(st)
#define TXT_NUMCOL_WIDTH(st)
#define TXT_NUMCOL_PAD
#define SUGG_LIST_SIZE
#define TXT_BODY_LEFT(st)
#define TXT_SCROLL_WIDTH