Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/space_text/text_draw.c
| Context not available. | |||||
| * | * | ||||
| */ | */ | ||||
| int wrap_width(SpaceText *st, ARegion *ar) | |||||
| { | |||||
| int winx = ar->winx - TXT_SCROLL_WIDTH; | |||||
| int x, max; | |||||
| x = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; | |||||
| max = st->cwidth ? (winx - x) / st->cwidth : 0; | |||||
| return max > 8 ? max : 8; | |||||
| } | |||||
| /* Sets (offl, offc) for transforming (line, curs) to its wrapped position */ | |||||
| void wrap_offset(SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) | |||||
| { | |||||
| Text *text; | |||||
| TextLine *linep; | |||||
| int i, j, start, end, max, chop; | |||||
| char ch; | |||||
| *offl = *offc = 0; | |||||
| if (!st->text) return; | |||||
| if (!st->wordwrap) return; | |||||
| text = st->text; | |||||
| /* Move pointer to first visible line (top) */ | |||||
| linep = text->lines.first; | |||||
| i = st->top; | |||||
| while (i > 0 && linep) { | |||||
| int lines = text_get_visible_lines(st, ar, linep->line); | |||||
| /* Line before top */ | |||||
| if (linep == linein) { | |||||
| if (lines <= i) | |||||
| /* no visible part of line */ | |||||
| return; | |||||
| } | |||||
| if (i - lines < 0) { | |||||
| break; | |||||
| } | |||||
| else { | |||||
| linep = linep->next; | |||||
| (*offl) += lines - 1; | |||||
| i -= lines; | |||||
| } | |||||
| } | |||||
| max = wrap_width(st, ar); | |||||
| cursin = txt_utf8_offset_to_column(linein->line, cursin); | |||||
| while (linep) { | |||||
| start = 0; | |||||
| end = max; | |||||
| chop = 1; | |||||
| *offc = 0; | |||||
| for (i = 0, j = 0; linep->line[j]; j += BLI_str_utf8_size_safe(linep->line + j)) { | |||||
| int chars; | |||||
| int columns = BLI_str_utf8_char_width_safe(linep->line + j); /* = 1 for tab */ | |||||
| /* Mimic replacement of tabs */ | |||||
| ch = linep->line[j]; | |||||
| if (ch == '\t') { | |||||
| chars = st->tabnumber - i % st->tabnumber; | |||||
| if (linep == linein && i < cursin) cursin += chars - 1; | |||||
| ch = ' '; | |||||
| } | |||||
| else { | |||||
| chars = 1; | |||||
| } | |||||
| while (chars--) { | |||||
| if (i + columns - start > max) { | |||||
| end = MIN2(end, i); | |||||
| if (chop && linep == linein && i >= cursin) { | |||||
| if (i == cursin) { | |||||
| (*offl)++; | |||||
| *offc -= end - start; | |||||
| } | |||||
| return; | |||||
| } | |||||
| (*offl)++; | |||||
| *offc -= end - start; | |||||
| start = end; | |||||
| end += max; | |||||
| chop = 1; | |||||
| } | |||||
| else if (ch == ' ' || ch == '-') { | |||||
| end = i + 1; | |||||
| chop = 0; | |||||
| if (linep == linein && i >= cursin) | |||||
| return; | |||||
| } | |||||
| i += columns; | |||||
| } | |||||
| } | |||||
| if (linep == linein) break; | |||||
| linep = linep->next; | |||||
| } | |||||
| } | |||||
| /* cursin - mem, offc - view */ | /* cursin - mem, offc - view */ | ||||
| void wrap_offset_in_line(SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) | void wrap_offset_in_line(SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc) | ||||
| { | { | ||||
| Context not available. | |||||
| return drawcache->line_height[lineno]; | return drawcache->line_height[lineno]; | ||||
| } | } | ||||
| int text_get_visible_lines(SpaceText *st, ARegion *ar, const char *str) | |||||
| { | |||||
| int i, j, start, end, max, lines, chars; | |||||
| char ch; | |||||
| max = wrap_width(st, ar); | |||||
| lines = 1; | |||||
| start = 0; | |||||
| end = max; | |||||
| for (i = 0, j = 0; str[j]; j += BLI_str_utf8_size_safe(str + j)) { | |||||
| int columns = BLI_str_utf8_char_width_safe(str + j); /* = 1 for tab */ | |||||
| /* Mimic replacement of tabs */ | |||||
| ch = str[j]; | |||||
| if (ch == '\t') { | |||||
| chars = st->tabnumber - i % st->tabnumber; | |||||
| ch = ' '; | |||||
| } | |||||
| else { | |||||
| chars = 1; | |||||
| } | |||||
| while (chars--) { | |||||
| if (i + columns - start > max) { | |||||
| lines++; | |||||
| start = MIN2(end, i); | |||||
| end += max; | |||||
| } | |||||
| else if (ch == ' ' || ch == '-') { | |||||
| end = i + 1; | |||||
| } | |||||
| i += columns; | |||||
| } | |||||
| } | |||||
| return lines; | |||||
| } | |||||
| int text_get_span_wrap(SpaceText *st, ARegion *ar, TextLine *from, TextLine *to) | int text_get_span_wrap(SpaceText *st, ARegion *ar, TextLine *from, TextLine *to) | ||||
| { | { | ||||
| Context not available. | |||||
Severin: Should be removed, but maybe it's useful to leave this as a debug option | |||||
Not Done Inline ActionsTo be removed? see comment below. mont29: To be removed? see comment below. | |||||
Not Done Inline ActionsDo not really see the point to have intermediate vars here, would just do: i = txt_get_span(text->lines.first, text->curl); st->text_cursor_pos[0] = (text->curc - st->left) * st->cwidth; st->text_cursor_pos[1] = sa->winy - ((i - st->top) * st->lheight); … and remove the debug print in final version too, we do not want that on by default even in debug build. ;) mont29: Do not really see the point to have intermediate vars here, would just do:
i = txt_get_span… | |||||
Not Done Inline Actionstsst… needless blank lines. :P mont29: tsst… needless blank lines. :P | |||||
Not Done Inline Actionssa is no more available here in master (since 10/11), ar->winy maybe? mont29: sa is no more available here in master (since 10/11), ar->winy maybe? | |||||
Not Done Inline Actionsthis is a reasonably expensive function. Calling it each time on the offchange someone might want to access text_cursor_pos is unnecessary. campbellbarton: this is a reasonably expensive function.
Calling it each time on the offchange someone might… | |||||
Should be removed, but maybe it's useful to leave this as a debug option