Refactor: Text Editor Formatted Output #110174
|
@ -62,52 +62,6 @@ static void text_font_begin(const TextDrawContext *tdc)
|
||||||
|
|
||||||
static void text_font_end(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 */
|
/* Sets the current drawing color based on the format character specified */
|
||||||
static void format_draw_color(const TextDrawContext *tdc, char formatchar)
|
static void format_draw_color(const TextDrawContext *tdc, char formatchar)
|
||||||
{
|
{
|
||||||
|
@ -145,6 +99,47 @@ 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 ouptut 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;
|
||||||
|
}
|
||||||
|
|
||||||
/** \} */
|
/** \} */
|
||||||
|
|
||||||
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
||||||
|
@ -417,9 +412,8 @@ static int text_draw_wrapped(const SpaceText *st,
|
||||||
int basex, lines;
|
int basex, lines;
|
||||||
int i, wrap, end, max, columns, padding; /* column */
|
int i, wrap, end, max, columns, padding; /* column */
|
||||||
/* warning, only valid when 'use_syntax' is set */
|
/* warning, only valid when 'use_syntax' is set */
|
||||||
int a, fstart, fpos; /* utf8 chars */
|
int fstart, fpos; /* utf8 chars */
|
||||||
int mi, ma, mstart, mend; /* mem */
|
int mi, mstart, mend; /* mem */
|
||||||
char fmt_prev = 0xff;
|
|
||||||
/* don't draw lines below this */
|
/* don't draw lines below this */
|
||||||
const int clip_min_y = -int(st->runtime.lheight_px - 1);
|
const int clip_min_y = -int(st->runtime.lheight_px - 1);
|
||||||
|
|
||||||
|
@ -455,17 +449,9 @@ static int text_draw_wrapped(const SpaceText *st,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Draw the visible portion of text on the overshot line */
|
/* Draw the visible portion of text on the overshot line */
|
||||||
for (a = fstart, ma = mstart; ma < mend; a++) {
|
x += text_font_draw(tdc, x, y, str + fstart, mend - fstart, format + fstart);
|
||||||
if (use_syntax) {
|
fpos += (mend - fstart);
|
||||||
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);
|
y -= TXT_LINE_HEIGHT(st);
|
||||||
x = basex;
|
x = basex;
|
||||||
lines++;
|
lines++;
|
||||||
|
@ -485,16 +471,8 @@ static int text_draw_wrapped(const SpaceText *st,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Draw the remaining text */
|
/* Draw the remaining text */
|
||||||
for (a = fstart, ma = mstart; str[ma] && y > clip_min_y; a++) {
|
if (y > clip_min_y) {
|
||||||
if (use_syntax) {
|
x += text_font_draw(tdc, x, y, str + fstart, strlen(str + fstart), format + fstart);
|
||||||
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);
|
flatten_string_free(&fs);
|
||||||
|
@ -511,7 +489,6 @@ static void text_draw(const SpaceText *st,
|
||||||
int y,
|
int y,
|
||||||
const char *format)
|
const char *format)
|
||||||
{
|
{
|
||||||
const bool use_syntax = (tdc->syntax_highlight && format);
|
|
||||||
FlattenString fs;
|
FlattenString fs;
|
||||||
int columns, size, n, w = 0, padding, amount = 0;
|
int columns, size, n, w = 0, padding, amount = 0;
|
||||||
const char *in = nullptr;
|
const char *in = nullptr;
|
||||||
|
@ -545,23 +522,7 @@ static void text_draw(const SpaceText *st,
|
||||||
}
|
}
|
||||||
|
|
||||||
x += tdc->cwidth_px * padding;
|
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);
|
flatten_string_free(&fs);
|
||||||
}
|
}
|
||||||
|
@ -1454,11 +1415,10 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi
|
||||||
|
|
||||||
if (viewc >= 0) {
|
if (viewc >= 0) {
|
||||||
viewl = txt_get_span(static_cast<TextLine *>(text->lines.first), startl) - st->top + offl;
|
viewl = txt_get_span(static_cast<TextLine *>(text->lines.first), startl) - st->top + offl;
|
||||||
|
BLF_enable(tdc->font_id, BLF_BOLD);
|
||||||
text_font_draw_character(
|
text_font_draw(
|
||||||
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), ch);
|
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), &ch, 1, nullptr);
|
||||||
text_font_draw_character(
|
BLF_disable(tdc->font_id, BLF_BOLD);
|
||||||
tdc, x + viewc * st->runtime.cwidth_px + 1, y - viewl * TXT_LINE_HEIGHT(st), ch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* draw closing bracket */
|
/* draw closing bracket */
|
||||||
|
@ -1468,11 +1428,10 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi
|
||||||
|
|
||||||
if (viewc >= 0) {
|
if (viewc >= 0) {
|
||||||
viewl = txt_get_span(static_cast<TextLine *>(text->lines.first), endl) - st->top + offl;
|
viewl = txt_get_span(static_cast<TextLine *>(text->lines.first), endl) - st->top + offl;
|
||||||
|
BLF_enable(tdc->font_id, BLF_BOLD);
|
||||||
text_font_draw_character(
|
text_font_draw(
|
||||||
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), ch);
|
tdc, x + viewc * st->runtime.cwidth_px, y - viewl * TXT_LINE_HEIGHT(st), &ch, 1, nullptr);
|
||||||
text_font_draw_character(
|
BLF_disable(tdc->font_id, BLF_BOLD);
|
||||||
tdc, x + viewc * st->runtime.cwidth_px + 1, y - viewl * TXT_LINE_HEIGHT(st), ch);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1593,7 +1552,12 @@ void draw_text_main(SpaceText *st, ARegion *region)
|
||||||
/* Draw line number. */
|
/* Draw line number. */
|
||||||
UI_FontThemeColor(tdc.font_id, (tmp == text->sell) ? TH_HILITE : TH_LINENUMBERS);
|
UI_FontThemeColor(tdc.font_id, (tmp == text->sell) ? TH_HILITE : TH_LINENUMBERS);
|
||||||
SNPRINTF(linenr, "%*d", st->runtime.line_number_display_digits, i + linecount + 1);
|
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);
|
text_font_draw(&tdc,
|
||||||
|
TXT_NUMCOL_PAD * st->runtime.cwidth_px,
|
||||||
|
y,
|
||||||
|
linenr,
|
||||||
|
st->runtime.line_number_display_digits,
|
||||||
|
nullptr);
|
||||||
/* Change back to text color. */
|
/* Change back to text color. */
|
||||||
UI_FontThemeColor(tdc.font_id, TH_TEXT);
|
UI_FontThemeColor(tdc.font_id, TH_TEXT);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue