Fix #115444: Revert "Refactor: Text Editor Formatted Output" #115458

Merged
Harley Acheson merged 1 commits from Harley/blender:Fix#115444 into main 2023-11-27 01:41:34 +01:00
1 changed files with 99 additions and 63 deletions

View File

@ -62,6 +62,52 @@ static void text_font_begin(const TextDrawContext *tdc)
static void text_font_end(const TextDrawContext * /*tdc*/) {}
static int text_font_draw(const TextDrawContext *tdc, int x, int y, const char *str)
{
const char tab_columns = 1; /* Tab characters aren't used here. */
BLF_position(tdc->font_id, x, y, 0);
const int columns = BLF_draw_mono(
tdc->font_id, str, BLF_DRAW_STR_DUMMY_MAX, tdc->cwidth_px, tab_columns);
return tdc->cwidth_px * columns;
}
static int text_font_draw_character(const TextDrawContext *tdc, int x, int y, char c)
{
const char tab_columns = 1;
BLF_position(tdc->font_id, x, y, 0);
BLF_draw_mono(tdc->font_id, &c, 1, tdc->cwidth_px, tab_columns);
return tdc->cwidth_px;
}
static int text_font_draw_character_utf8(
const TextDrawContext *tdc, int x, int y, const char *c, const int c_len)
{
BLI_assert(c_len == BLI_str_utf8_size_safe(c));
const char tab_columns = 1; /* Tab characters aren't used here. */
BLF_position(tdc->font_id, x, y, 0);
const int columns = BLF_draw_mono(tdc->font_id, c, c_len, tdc->cwidth_px, tab_columns);
return tdc->cwidth_px * columns;
}
#if 0
/* Formats every line of the current text */
static void txt_format_text(SpaceText *st)
{
TextLine *linep;
if (!st->text) {
return;
}
for (linep = st->text->lines.first; linep; linep = linep->next) {
txt_format_line(st, linep, 0);
}
}
#endif
/* Sets the current drawing color based on the format character specified */
static void format_draw_color(const TextDrawContext *tdc, char formatchar)
{
@ -99,47 +145,6 @@ static void format_draw_color(const TextDrawContext *tdc, char formatchar)
}
}
/* Draw plain or formatted monospaced text. */
static int text_font_draw(const TextDrawContext *tdc,
int x,
int y,
const char *str,
const int str_len,
const char *format)
{
int columns = 0;
int str_pos = 0;
const int tab_columns = 1; /* Tab characters aren't used here. */
if (tdc->syntax_highlight && format) {
char fmt_prev = 0xff;
for (int a = 0; a < str_len; a++) {
if (format[a] != fmt_prev) {
/* Change of format so output the previous section. */
if (a - str_pos > 0) {
BLF_position(tdc->font_id, x + (columns * tdc->cwidth_px), y, 0);
columns += BLF_draw_mono(
tdc->font_id, str + str_pos, a - str_pos, tdc->cwidth_px, tab_columns);
}
format_draw_color(tdc, fmt_prev = format[a]);
str_pos = a;
}
if (a == (str_len - 1)) {
/* At end so output the last section. */
BLF_position(tdc->font_id, x + (columns * tdc->cwidth_px), y, 0);
columns += BLF_draw_mono(
tdc->font_id, str + str_pos, str_len - str_pos, tdc->cwidth_px, tab_columns);
}
}
}
else {
/* Not syntax highlighted, so output the entire line. */
BLF_position(tdc->font_id, x, y, 0);
columns = BLF_draw_mono(tdc->font_id, str, str_len, tdc->cwidth_px, tab_columns);
}
return tdc->cwidth_px * columns;
}
/** \} */
/* -------------------------------------------------------------------- */
@ -412,8 +417,9 @@ static int text_draw_wrapped(const SpaceText *st,
int basex, lines;
int i, wrap, end, max, columns, padding; /* column */
/* warning, only valid when 'use_syntax' is set */
int fstart, fpos; /* utf8 chars */
int mi, mstart, mend; /* mem */
int a, fstart, fpos; /* utf8 chars */
int mi, ma, mstart, mend; /* mem */
char fmt_prev = 0xff;
/* don't draw lines below this */
const int clip_min_y = -int(st->runtime.lheight_px - 1);
@ -449,9 +455,17 @@ static int text_draw_wrapped(const SpaceText *st,
}
/* Draw the visible portion of text on the overshot line */
x += text_font_draw(tdc, x, y, str + fstart, mend - fstart, format + fstart);
fpos += (mend - fstart);
for (a = fstart, ma = mstart; ma < mend; a++) {
if (use_syntax) {
if (fmt_prev != format[a]) {
format_draw_color(tdc, fmt_prev = format[a]);
}
}
const int c_len = BLI_str_utf8_size_safe(str + ma);
x += text_font_draw_character_utf8(tdc, x, y, str + ma, c_len);
ma += c_len;
fpos++;
}
y -= TXT_LINE_HEIGHT(st);
x = basex;
lines++;
@ -471,8 +485,16 @@ static int text_draw_wrapped(const SpaceText *st,
}
/* Draw the remaining text */
if (y > clip_min_y) {
x += text_font_draw(tdc, x, y, str + fstart, strlen(str + fstart), format + fstart);
for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; a++) {
if (use_syntax) {
if (fmt_prev != format[a]) {
format_draw_color(tdc, fmt_prev = format[a]);
}
}
const int c_len = BLI_str_utf8_size_safe(str + ma);
x += text_font_draw_character_utf8(tdc, x, y, str + ma, c_len);
ma += c_len;
}
flatten_string_free(&fs);
@ -489,6 +511,7 @@ static void text_draw(const SpaceText *st,
int y,
const char *format)
{
const bool use_syntax = (tdc->syntax_highlight && format);
FlattenString fs;
int columns, size, n, w = 0, padding, amount = 0;
const char *in = nullptr;
@ -522,7 +545,23 @@ static void text_draw(const SpaceText *st,
}
x += tdc->cwidth_px * padding;
x += text_font_draw(tdc, x, y, in, amount, format);
if (use_syntax) {
int a, str_shift = 0;
char fmt_prev = 0xff;
for (a = 0; a < amount; a++) {
if (format[a] != fmt_prev) {
format_draw_color(tdc, fmt_prev = format[a]);
}
const int c_len = BLI_str_utf8_size_safe(in + str_shift);
x += text_font_draw_character_utf8(tdc, x, y, in + str_shift, c_len);
str_shift += c_len;
}
}
else {
text_font_draw(tdc, x, y, in);
}
flatten_string_free(&fs);
}
@ -1415,10 +1454,11 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi
if (viewc >= 0) {
viewl = txt_get_span(static_cast<TextLine *>(text->lines.first), startl) - st->top + offl;
BLF_enable(tdc->font_id, BLF_BOLD);
text_font_draw(
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), &ch, 1, nullptr);
BLF_disable(tdc->font_id, BLF_BOLD);
text_font_draw_character(
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), ch);
text_font_draw_character(
tdc, x + viewc * st->runtime.cwidth_px + 1, y - viewl * TXT_LINE_HEIGHT(st), ch);
}
/* draw closing bracket */
@ -1428,10 +1468,11 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi
if (viewc >= 0) {
viewl = txt_get_span(static_cast<TextLine *>(text->lines.first), endl) - st->top + offl;
BLF_enable(tdc->font_id, BLF_BOLD);
text_font_draw(
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), &ch, 1, nullptr);
BLF_disable(tdc->font_id, BLF_BOLD);
text_font_draw_character(
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), ch);
text_font_draw_character(
tdc, x + viewc * st->runtime.cwidth_px + 1, y - viewl * TXT_LINE_HEIGHT(st), ch);
}
}
@ -1552,12 +1593,7 @@ void draw_text_main(SpaceText *st, ARegion *region)
/* Draw line number. */
UI_FontThemeColor(tdc.font_id, (tmp == text->sell) ? TH_HILITE : TH_LINENUMBERS);
SNPRINTF(linenr, "%*d", st->runtime.line_number_display_digits, i + linecount + 1);
text_font_draw(&tdc,
TXT_NUMCOL_PAD * st->runtime.cwidth_px,
y,
linenr,
st->runtime.line_number_display_digits,
nullptr);
text_font_draw(&tdc, TXT_NUMCOL_PAD * st->runtime.cwidth_px, y, linenr);
/* Change back to text color. */
UI_FontThemeColor(tdc.font_id, TH_TEXT);
}