diff options
Diffstat (limited to 'source/blender/editors/space_text/text_draw.c')
-rw-r--r-- | source/blender/editors/space_text/text_draw.c | 186 |
1 files changed, 124 insertions, 62 deletions
diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c index b2c5f42adb4..cdd691fe879 100644 --- a/source/blender/editors/space_text/text_draw.c +++ b/source/blender/editors/space_text/text_draw.c @@ -204,10 +204,12 @@ void wrap_offset( *offl = *offc = 0; - if (!st->text) + if (!st->text) { return; - if (!st->wordwrap) + } + if (!st->wordwrap) { return; + } text = st->text; @@ -219,9 +221,10 @@ void wrap_offset( /* Line before top */ if (linep == linein) { - if (lines <= i) + if (lines <= i) { /* no visible part of line */ return; + } } if (i - lines < 0) { @@ -250,8 +253,9 @@ void wrap_offset( ch = linep->line[j]; if (ch == '\t') { chars = st->tabnumber - i % st->tabnumber; - if (linep == linein && i < cursin) + if (linep == linein && i < cursin) { cursin += chars - 1; + } ch = ' '; } else { @@ -281,14 +285,16 @@ void wrap_offset( else if (ch == ' ' || ch == '-') { end = i + 1; chop = 0; - if (linep == linein && i >= cursin) + if (linep == linein && i >= cursin) { return; + } } i += columns; } } - if (linep == linein) + if (linep == linein) { break; + } linep = linep->next; } } @@ -302,10 +308,12 @@ void wrap_offset_in_line( *offl = *offc = 0; - if (!st->text) + if (!st->text) { return; - if (!st->wordwrap) + } + if (!st->wordwrap) { return; + } max = wrap_width(st, ar); @@ -322,12 +330,14 @@ void wrap_offset_in_line( ch = linein->line[j]; if (ch == '\t') { chars = st->tabnumber - i % st->tabnumber; - if (i < cursin) + if (i < cursin) { cursin += chars - 1; + } ch = ' '; } - else + else { chars = 1; + } while (chars--) { if (i + columns - start > max) { @@ -352,8 +362,9 @@ void wrap_offset_in_line( else if (ch == ' ' || ch == '-') { end = i + 1; chop = 0; - if (i >= cursin) + if (i >= cursin) { return; + } } i += columns; } @@ -365,10 +376,12 @@ int text_get_char_pos(const SpaceText *st, const char *line, int cur) int a = 0, i; for (i = 0; i < cur && line[i]; i += BLI_str_utf8_size_safe(line + i)) { - if (line[i] == '\t') + if (line[i] == '\t') { a += st->tabnumber - a % st->tabnumber; - else + } + else { a += BLI_str_utf8_char_width_safe(line + i); + } } return a; } @@ -379,15 +392,18 @@ static const char *txt_utf8_forward_columns(const char *str, int columns, int *p const char *p = str; while (*p) { col = BLI_str_utf8_char_width(p); - if (columns - col < 0) + if (columns - col < 0) { break; + } columns -= col; p += BLI_str_utf8_size_safe(p); - if (columns == 0) + if (columns == 0) { break; + } } - if (padding) + if (padding) { *padding = *p ? columns : 0; + } return p; } @@ -414,8 +430,9 @@ static int text_draw_wrapped(const SpaceText *st, flatten_string(st, &fs, str); str = fs.buf; max = w / st->cwidth; - if (max < 8) + if (max < 8) { max = 8; + } basex = x; lines = 1; @@ -444,8 +461,9 @@ static int text_draw_wrapped(const SpaceText *st, /* Draw the visible portion of text on the overshot line */ for (a = fstart, ma = mstart; ma < mend; a++, ma += BLI_str_utf8_size_safe(str + ma)) { if (use_syntax) { - if (fmt_prev != format[a]) + if (fmt_prev != format[a]) { format_draw_color(tdc, fmt_prev = format[a]); + } } x += text_font_draw_character_utf8(tdc, x, y, str + ma); fpos++; @@ -458,8 +476,9 @@ static int text_draw_wrapped(const SpaceText *st, mend = txt_utf8_forward_columns(str + mend, max, &padding) - str; end = (wrap += max - padding); - if (y <= clip_min_y) + if (y <= clip_min_y) { break; + } } else if (str[mi] == ' ' || str[mi] == '-') { wrap = i + 1; @@ -471,8 +490,9 @@ static int text_draw_wrapped(const SpaceText *st, for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; a++, ma += BLI_str_utf8_size_safe(str + ma)) { if (use_syntax) { - if (fmt_prev != format[a]) + if (fmt_prev != format[a]) { format_draw_color(tdc, fmt_prev = format[a]); + } } x += text_font_draw_character_utf8(tdc, x, y, str + ma); @@ -506,12 +526,14 @@ static void text_draw(const SpaceText *st, padding = w - cshift; in = str; } - else if (format) + else if (format) { format++; + } } if (in) { - if (maxwidth && w + columns > cshift + maxwidth) + if (maxwidth && w + columns > cshift + maxwidth) { break; + } amount++; } @@ -530,8 +552,9 @@ static void text_draw(const SpaceText *st, char fmt_prev = 0xff; for (a = 0; a < amount; a++) { - if (format[a] != fmt_prev) + if (format[a] != fmt_prev) { format_draw_color(tdc, fmt_prev = format[a]); + } x += text_font_draw_character_utf8(tdc, x, y, in + str_shift); str_shift += BLI_str_utf8_size_safe(in + str_shift); } @@ -577,8 +600,9 @@ static void text_update_drawcache(SpaceText *st, ARegion *ar) int full_update = 0, nlines = 0; Text *txt = st->text; - if (!st->drawcache) + if (!st->drawcache) { text_drawcache_init(st); + } text_update_character_width(st); @@ -618,10 +642,12 @@ static void text_update_drawcache(SpaceText *st, ARegion *ar) nlines = BLI_listbase_count(&txt->lines); size = sizeof(int) * nlines; - if (fp) + if (fp) { fp = MEM_reallocN(fp, size); - else + } + else { fp = MEM_callocN(size, "text drawcache line_height"); + } drawcache->valid_tail = drawcache->valid_head = 0; old_tail = fp + drawcache->nlines - drawcache->valid_tail; @@ -630,8 +656,9 @@ static void text_update_drawcache(SpaceText *st, ARegion *ar) drawcache->total_lines = 0; - if (st->showlinenrs) + if (st->showlinenrs) { st->linenrs_tot = integer_digits_i(nlines); + } while (line) { if (drawcache->valid_head) { /* we're inside valid head lines */ @@ -664,8 +691,9 @@ static void text_update_drawcache(SpaceText *st, ARegion *ar) if (full_update || drawcache->update_flag) { nlines = BLI_listbase_count(&txt->lines); - if (st->showlinenrs) + if (st->showlinenrs) { st->linenrs_tot = integer_digits_i(nlines); + } } drawcache->total_lines = nlines; @@ -692,8 +720,9 @@ static void text_update_drawcache(SpaceText *st, ARegion *ar) void text_drawcache_tag_update(SpaceText *st, int full) { /* this happens if text editor ops are caled from python */ - if (st == NULL) + if (st == NULL) { return; + } if (st->drawcache) { DrawCache *drawcache = (DrawCache *)st->drawcache; @@ -721,10 +750,12 @@ void text_drawcache_tag_update(SpaceText *st, int full) /* quick cache recalculation is also used in delete operator, * which could merge lines which are adjacent to current selection lines * expand recalculate area to this lines */ - if (drawcache->valid_head > 0) + if (drawcache->valid_head > 0) { drawcache->valid_head--; - if (drawcache->valid_tail > 0) + } + if (drawcache->valid_tail > 0) { drawcache->valid_tail--; + } } else { drawcache->valid_head = 0; @@ -740,8 +771,9 @@ void text_free_caches(SpaceText *st) DrawCache *drawcache = (DrawCache *)st->drawcache; if (drawcache) { - if (drawcache->line_height) + if (drawcache->line_height) { MEM_freeN(drawcache->line_height); + } MEM_freeN(drawcache); } @@ -804,8 +836,9 @@ int text_get_span_wrap(const SpaceText *st, ARegion *ar, TextLine *from, TextLin /* Look forwards */ while (tmp) { - if (tmp == to) + if (tmp == to) { return ret; + } ret += text_get_visible_lines(st, ar, tmp->line); tmp = tmp->next; } @@ -876,8 +909,9 @@ static void calc_text_rcts(SpaceText *st, ARegion *ar, rcti *scroll, rcti *back) CLAMP(st->txtbar.ymax, pix_bottom_margin, ar->winy - pix_top_margin); st->pix_per_line = (pix_available > 0) ? (float)ltexth / pix_available : 0; - if (st->pix_per_line < 0.1f) + if (st->pix_per_line < 0.1f) { st->pix_per_line = 0.1f; + } curl_off = text_get_span_wrap(st, ar, st->text->lines.first, st->text->curl); sell_off = text_get_span_wrap(st, ar, st->text->lines.first, st->text->sell); @@ -1098,16 +1132,19 @@ static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc const int lheight = st->lheight_dpi + TXT_LINE_SPACING; const int margin_x = 2; - if (!st->text) + if (!st->text) { return; - if (!texttool_text_is_active(st->text)) + } + if (!texttool_text_is_active(st->text)) { return; + } first = texttool_suggest_first(); last = texttool_suggest_last(); - if (!first || !last) + if (!first || !last) { return; + } text_pop_suggest_list(); sel = texttool_suggest_selected(); @@ -1129,8 +1166,9 @@ static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc boxw = SUGG_LIST_WIDTH * st->cwidth + 20; boxh = SUGG_LIST_SIZE * lheight + 8; - if (x + boxw > ar->winx) + if (x + boxw > ar->winx) { x = MAX2(0, ar->winx - boxw); + } /* not needed but stands out nicer */ UI_draw_box_shadow(220, x, y - boxh, x + boxw, y); @@ -1147,8 +1185,9 @@ static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc immUnbindProgram(); /* Set the top 'item' of the visible list */ - for (i = 0, item = first; i < *top && item->next; i++, item = item->next) + for (i = 0, item = first; i < *top && item->next; i++, item = item->next) { ; + } for (i = 0; i < SUGG_LIST_SIZE && item; i++, item = item->next) { int len = txt_utf8_forward_columns(item->name, SUGG_LIST_WIDTH, NULL) - item->name; @@ -1173,8 +1212,9 @@ static void draw_suggestion_list(const SpaceText *st, const TextDrawContext *tdc format_draw_color(tdc, item->type); text_draw(st, tdc, str, 0, 0, x + margin_x, y - 1, NULL); - if (item == last) + if (item == last) { break; + } } } @@ -1307,8 +1347,9 @@ static void draw_text_decoration(SpaceText *st, ARegion *ar) y += TXT_LINE_SPACING; w = st->cwidth; - if (ch == '\t') + if (ch == '\t') { w *= st->tabnumber - (vselc + st->left) % st->tabnumber; + } immRecti(pos, x, y - lheight - 1, x + w, y - lheight + 1); } @@ -1332,16 +1373,19 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi char ch; // showsyntax must be on or else the format string will be null - if (!text->curl || !st->showsyntax) + if (!text->curl || !st->showsyntax) { return; + } startl = text->curl; startc = text->curc; b = text_check_bracket(startl->line[startc]); - if (b == 0 && startc > 0) + if (b == 0 && startc > 0) { b = text_check_bracket(startl->line[--startc]); - if (b == 0) + } + if (b == 0) { return; + } linep = startl; c = startc; @@ -1353,8 +1397,9 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi /* Don't highlight backets if syntax HL is off or bracket in string or comment. */ if (!linep->format || linep->format[fc] == FMT_TYPE_STRING || - linep->format[fc] == FMT_TYPE_COMMENT) + linep->format[fc] == FMT_TYPE_COMMENT) { return; + } if (b > 0) { /* opening bracket, search forward for close */ @@ -1380,8 +1425,9 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi fc++; c += BLI_str_utf8_size_safe(linep->line + c); } - if (endl) + if (endl) { break; + } linep = linep->next; c = 0; fc = 0; @@ -1390,8 +1436,9 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi else { /* closing bracket, search backward for open */ fc--; - if (c > 0) + if (c > 0) { c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c); + } while (linep) { while (fc >= 0) { if (linep->format && linep->format[fc] != FMT_TYPE_STRING && @@ -1410,27 +1457,34 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi } } fc--; - if (c > 0) + if (c > 0) { c -= linep->line + c - BLI_str_prev_char_utf8(linep->line + c); + } } - if (endl) + if (endl) { break; + } linep = linep->prev; if (linep) { - if (linep->format) + if (linep->format) { fc = strlen(linep->format) - 1; - else + } + else { fc = -1; - if (linep->len) + } + if (linep->len) { c = BLI_str_prev_char_utf8(linep->line + linep->len) - linep->line; - else + } + else { fc = -1; + } } } } - if (!endl || endc == -1) + if (!endl || endc == -1) { return; + } UI_FontThemeColor(tdc->font_id, TH_HILITE); x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; @@ -1480,8 +1534,9 @@ void draw_text_main(SpaceText *st, ARegion *ar) int margin_column_x; /* if no text, nothing to do */ - if (!text) + if (!text) { return; + } /* dpi controlled line height and font size */ st->lheight_dpi = (U.widget_unit * st->lheight) / 20; @@ -1498,8 +1553,9 @@ void draw_text_main(SpaceText *st, ARegion *ar) text_update_drawcache(st, ar); /* make sure all the positional pointers exist */ - if (!text->curl || !text->sell || !text->lines.first || !text->lines.last) + if (!text->curl || !text->sell || !text->lines.first || !text->lines.last) { txt_clean_text(text); + } /* update rects for scroll */ calc_text_rcts(st, ar, &scroll, &back); /* scroll will hold the entire bar size */ @@ -1509,8 +1565,9 @@ void draw_text_main(SpaceText *st, ARegion *ar) tmp = text->lines.first; lineno = 0; for (i = 0; i < st->top && tmp; i++) { - if (st->showsyntax && !tmp->format) + if (st->showsyntax && !tmp->format) { tft->format_line(st, tmp, false); + } if (st->wordwrap) { int lines = text_get_visible_lines_no(st, lineno); @@ -1563,8 +1620,9 @@ void draw_text_main(SpaceText *st, ARegion *ar) UI_FontThemeColor(tdc.font_id, TH_TEXT); for (i = 0; y > clip_min_y && i < st->viewlines && tmp; i++, tmp = tmp->next) { - if (st->showsyntax && !tmp->format) + if (st->showsyntax && !tmp->format) { tft->format_line(st, tmp, false); + } if (st->showlinenrs && !wrap_skip) { /* draw line number */ @@ -1654,8 +1712,9 @@ void text_scroll_to_cursor(SpaceText *st, ARegion *ar, const bool center) Text *text; int i, x, winx = ar->winx; - if (ELEM(NULL, st, st->text, st->text->curl)) + if (ELEM(NULL, st, st->text, st->text->curl)) { return; + } text = st->text; @@ -1704,10 +1763,12 @@ void text_scroll_to_cursor(SpaceText *st, ARegion *ar, const bool center) } } - if (st->top < 0) + if (st->top < 0) { st->top = 0; - if (st->left < 0) + } + if (st->left < 0) { st->left = 0; + } st->scroll_accum[0] = 0.0f; st->scroll_accum[1] = 0.0f; @@ -1718,8 +1779,9 @@ void text_scroll_to_cursor__area(SpaceText *st, ScrArea *sa, const bool center) { ARegion *ar; - if (ELEM(NULL, st, st->text, st->text->curl)) + if (ELEM(NULL, st, st->text, st->text->curl)) { return; + } ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW); |