Example #1
0
static int text_draw(SpaceText *st, char *str, int cshift, int maxwidth, int draw, int x, int y, char *format)
{
	FlattenString fs;
	int r=0, w= 0, amount;
	int *acc;
	char *in;

	w= flatten_string(st, &fs, str);
	if(w < cshift) {
		flatten_string_free(&fs);
		return 0; /* String is shorter than shift */
	}
	
	in= fs.buf+cshift;
	acc= fs.accum+cshift;
	w= w-cshift;

	if(draw) {
		if(st->showsyntax && format) {
			int a;
			format = format+cshift;
		
			amount = strlen(in);
			if(maxwidth)
				amount= MIN2(amount, maxwidth);
			
			for(a = 0; a < amount; a++) {
				format_draw_color(format[a]);
				x += text_font_draw_character(st, x, y, in[a]);
			}
		}
		else {
			amount = strlen(in);
			if(maxwidth)
				amount= MIN2(amount, maxwidth);

			in[amount]= 0;
			text_font_draw(st, x, y, in);
		}
	}
	else {
		while(w-- && *acc++ < maxwidth)
			r+= st->cwidth;
	}

	flatten_string_free(&fs);

	if(cshift && r==0)
		return 0;
	else if(st->showlinenrs)
		return r+TXT_OFFSET+TEXTXLOC;
	else
		return r+TXT_OFFSET;
}
Example #2
0
static void text_draw(
        const SpaceText *st, const TextDrawContext *tdc,
        char *str, int cshift, int maxwidth, int x, int y, const char *format)
{
	const bool use_syntax = (st->showsyntax && format);
	FlattenString fs;
	int columns, size, n, w = 0, padding, amount = 0;
	const char *in = NULL;

	for (n = flatten_string(st, &fs, str), str = fs.buf; n > 0; n--) {
		columns = BLI_str_utf8_char_width_safe(str);
		size = BLI_str_utf8_size_safe(str);

		if (!in) {
			if (w >= cshift) {
				padding = w - cshift;
				in = str;
			}
			else if (format)
				format++;
		}
		if (in) {
			if (maxwidth && w + columns > cshift + maxwidth)
				break;
			amount++;
		}

		w += columns;
		str += size;
	}
	if (!in) {
		flatten_string_free(&fs);
		return; /* String is shorter than shift or ends with a padding */
	}

	x += tdc->cwidth * padding;

	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(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);
		}
	}
	else {
		text_font_draw(tdc, x, y, in);
	}

	flatten_string_free(&fs);
}
Example #3
0
static int text_draw_wrapped(SpaceText *st, char *str, int x, int y, int w, char *format, int skip)
{
	FlattenString fs;
	int basex, i, a, len, start, end, max, lines;
	
	len= flatten_string(st, &fs, str);
	str= fs.buf;
	max= w/st->cwidth;
	if(max<8) max= 8;
	basex= x;

	lines= 1;
	start= 0;
	end= max;
	for(i=0; i<len; i++) {
		if(i-start >= max) {
			/* skip hidden part of line */
			if(skip) {
				skip--;
				start= end;
				end += max;
				continue;
			}

			/* Draw the visible portion of text on the overshot line */
			for(a=start; a<end; a++) {
				if(st->showsyntax && format) format_draw_color(format[a]);
				x += text_font_draw_character(st, x, y, str[a]);
			}
			y -= st->lheight;
			x= basex;
			lines++;
			start= end;
			end += max;

			if(y<=0) break;
		}
		else if(str[i]==' ' || str[i]=='-') {
			end = i+1;
		}
	}

	/* Draw the remaining text */
	for(a=start; a<len && y > 0; a++) {
		if(st->showsyntax && format)
			format_draw_color(format[a]);

		x += text_font_draw_character(st, x, y, str[a]);
	}

	flatten_string_free(&fs);

	return lines;
}
Example #4
0
static int text_draw_wrapped(SpaceText *st, const char *str, int x, int y, int w, const char *format, int skip)
{
	FlattenString fs;
	int basex, lines;
	int i, wrap, end, max, columns, padding; /* column */
	int a, fstart, fpos;                     /* utf8 chars */
	int mi, ma, mstart, mend;                /* mem */
	char fmt_prev = 0xff;
	
	flatten_string(st, &fs, str);
	str = fs.buf;
	max = w / st->cwidth;
	if (max < 8) max = 8;
	basex = x;
	lines = 1;
	
	fpos = fstart = 0; mstart = 0;
	mend = txt_utf8_forward_columns(str, max, &padding) - str;
	end = wrap = max - padding;
	
	for (i = 0, mi = 0; str[mi]; i += columns, mi += BLI_str_utf8_size_safe(str + mi)) {
		columns = BLI_str_utf8_char_width_safe(str + mi);
		if (i + columns > end) {
			/* skip hidden part of line */
			if (skip) {
				skip--;
				fstart = fpos; mstart = mend;
				mend = txt_utf8_forward_columns(str + mend, max, &padding) - str;
				end = (wrap += max - padding);
				continue;
			}

			/* 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 (st->showsyntax && format) {
					if (fmt_prev != format[a]) format_draw_color(fmt_prev = format[a]);
				}
				x += text_font_draw_character_utf8(st, x, y, str + ma);
				fpos++;
			}
			y -= st->lheight_dpi + TXT_LINE_SPACING;
			x = basex;
			lines++;
			fstart = fpos; mstart = mend;
			mend = txt_utf8_forward_columns(str + mend, max, &padding) - str;
			end = (wrap += max - padding);

			if (y <= 0) break;
		}
		else if (str[mi] == ' ' || str[mi] == '-') {
			wrap = i + 1; mend = mi + 1;
		}
	}

	/* Draw the remaining text */
	for (a = fstart, ma = mstart; str[ma] && y > 0; a++, ma += BLI_str_utf8_size_safe(str + ma)) {
		if (st->showsyntax && format) {
			if (fmt_prev != format[a]) format_draw_color(fmt_prev = format[a]);
		}

		x += text_font_draw_character_utf8(st, x, y, str + ma);
	}

	flatten_string_free(&fs);

	return lines;
}
Example #5
0
static void txtfmt_lua_format_line(SpaceText *st, TextLine *line, const bool do_next)
{
	FlattenString fs;
	const char *str;
	char *fmt;
	char cont_orig, cont, find, prev = ' ';
	int len, i;

	/* Get continuation from previous line */
	if (line->prev && line->prev->format != NULL) {
		fmt = line->prev->format;
		cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */
		BLI_assert((FMT_CONT_ALL & cont) == cont);
	}
	else {
		cont = FMT_CONT_NOP;
	}

	/* Get original continuation from this line */
	if (line->format != NULL) {
		fmt = line->format;
		cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */
		BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig);
	}
	else {
		cont_orig = 0xFF;
	}

	len = flatten_string(st, &fs, line->line);
	str = fs.buf;
	if (!text_check_format_len(line, len)) {
		flatten_string_free(&fs);
		return;
	}
	fmt = line->format;

	while (*str) {
		/* Handle escape sequences by skipping both \ and next char */
		if (*str == '\\') {
			*fmt = prev; fmt++; str++;
			if (*str == '\0') break;
			*fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str);
			continue;
		}
		/* Handle continuations */
		else if (cont) {
			/* Multi-line comments */
			if (cont & FMT_CONT_COMMENT_C) {
				if (*str == ']' && *(str + 1) == ']') {
					*fmt = FMT_TYPE_COMMENT; fmt++; str++;
					*fmt = FMT_TYPE_COMMENT;
					cont = FMT_CONT_NOP;
				}
				else {
					*fmt = FMT_TYPE_COMMENT;
				}
				/* Handle other comments */
			}
			else {
				find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\'';
				if (*str == find) cont = 0;
				*fmt = FMT_TYPE_STRING;
			}

			str += BLI_str_utf8_size_safe(str) - 1;
		}
		/* Not in a string... */
		else {
			/* Multi-line comments */
			if (*str == '-'       && *(str + 1) == '-' &&
			    *(str + 2) == '[' && *(str + 3) == '[')
			{
				cont = FMT_CONT_COMMENT_C;
				*fmt = FMT_TYPE_COMMENT; fmt++; str++;
				*fmt = FMT_TYPE_COMMENT; fmt++; str++;
				*fmt = FMT_TYPE_COMMENT; fmt++; str++;
				*fmt = FMT_TYPE_COMMENT;
			}
			/* Single line comment */
			else if (*str == '-' && *(str + 1) == '-') {
				text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format));
			}
			else if (*str == '"' || *str == '\'') {
				/* Strings */
				find = *str;
				cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
				*fmt = FMT_TYPE_STRING;
			}
			/* Whitespace (all ws. has been converted to spaces) */
			else if (*str == ' ') {
				*fmt = FMT_TYPE_WHITESPACE;
			}
			/* Numbers (digits not part of an identifier and periods followed by digits) */
			else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) ||
			         (*str == '.' && text_check_digit(*(str + 1))))
			{
				*fmt = FMT_TYPE_NUMERAL;
			}
			/* Booleans */
			else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_lua_find_bool(str)) != -1) {
				if (i > 0) {
					text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i);
				}
				else {
					str += BLI_str_utf8_size_safe(str) - 1;
					*fmt = FMT_TYPE_DEFAULT;
				}
			}
			/* Punctuation */
			else if ((*str != '#') && text_check_delim(*str)) {
				*fmt = FMT_TYPE_SYMBOL;
			}
			/* Identifiers and other text (no previous ws. or delims. so text continues) */
			else if (prev == FMT_TYPE_DEFAULT) {
				str += BLI_str_utf8_size_safe(str) - 1;
				*fmt = FMT_TYPE_DEFAULT;
			}
			/* Not ws, a digit, punct, or continuing text. Must be new, check for special words */
			else {
				/* Special vars(v) or built-in keywords(b) */
				/* keep in sync with 'txtfmt_osl_format_identifier()' */
				if      ((i = txtfmt_lua_find_specialvar(str))   != -1) prev = FMT_TYPE_SPECIAL;
				else if ((i = txtfmt_lua_find_keyword(str))      != -1) prev = FMT_TYPE_KEYWORD;

				if (i > 0) {
					text_format_fill_ascii(&str, &fmt, prev, i);
				}
				else {
					str += BLI_str_utf8_size_safe(str) - 1;
					*fmt = FMT_TYPE_DEFAULT;
				}
			}
		}
		prev = *fmt; fmt++; str++;
	}

	/* Terminate and add continuation char */
	*fmt = '\0'; fmt++;
	*fmt = cont;

	/* If continuation has changed and we're allowed, process the next line */
	if (cont != cont_orig && do_next && line->next) {
		txtfmt_lua_format_line(st, line->next, do_next);
	}

	flatten_string_free(&fs);
}
Example #6
0
static int text_draw_wrapped(
        const SpaceText *st, const TextDrawContext *tdc,
        const char *str, int x, int y, int w, const char *format, int skip)
{
	const bool use_syntax = (st->showsyntax && format);
	FlattenString fs;
	int basex, lines;
	int i, wrap, end, max, columns, padding; /* column */
	/* warning, only valid when 'use_syntax' is set */
	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->lheight_dpi - 1);

	flatten_string(st, &fs, str);
	str = fs.buf;
	max = w / st->cwidth;
	if (max < 8) max = 8;
	basex = x;
	lines = 1;
	
	fpos = fstart = 0; mstart = 0;
	mend = txt_utf8_forward_columns(str, max, &padding) - str;
	end = wrap = max - padding;
	
	for (i = 0, mi = 0; str[mi]; i += columns, mi += BLI_str_utf8_size_safe(str + mi)) {
		columns = BLI_str_utf8_char_width_safe(str + mi);
		if (i + columns > end) {
			/* skip hidden part of line */
			if (skip) {
				skip--;
				if (use_syntax) {
					/* currently fpos only used when formatting */
					fpos += BLI_strnlen_utf8(str + mstart, mend - mstart);
				}
				fstart = fpos; mstart = mend;
				mend = txt_utf8_forward_columns(str + mend, max, &padding) - str;
				end = (wrap += max - padding);
				continue;
			}

			/* 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]) format_draw_color(fmt_prev = format[a]);
				}
				x += text_font_draw_character_utf8(tdc, x, y, str + ma);
				fpos++;
			}
			y -= st->lheight_dpi + TXT_LINE_SPACING;
			x = basex;
			lines++;
			fstart = fpos; mstart = mend;
			mend = txt_utf8_forward_columns(str + mend, max, &padding) - str;
			end = (wrap += max - padding);

			if (y <= clip_min_y)
				break;
		}
		else if (str[mi] == ' ' || str[mi] == '-') {
			wrap = i + 1; mend = mi + 1;
		}
	}

	/* Draw the remaining text */
	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]) format_draw_color(fmt_prev = format[a]);
		}

		x += text_font_draw_character_utf8(tdc, x, y, str + ma);
	}

	flatten_string_free(&fs);

	return lines;
}
Example #7
0
static void txt_format_line(SpaceText *st, TextLine *line, int do_next)
{
	FlattenString fs;
	char *str, *fmt, orig, cont, find, prev = ' ';
	int len, i;

	/* Get continuation from previous line */
	if(line->prev && line->prev->format != NULL) {
		fmt= line->prev->format;
		cont = fmt[strlen(fmt)+1]; /* Just after the null-terminator */
	}
	else cont = 0;

	/* Get original continuation from this line */
	if(line->format != NULL) {
		fmt= line->format;
		orig = fmt[strlen(fmt)+1]; /* Just after the null-terminator */
	}
	else orig = 0xFF;

	flatten_string(st, &fs, line->line);
	str = fs.buf;
	len = strlen(str);
	if(!text_check_format_len(line, len)) {
		flatten_string_free(&fs);
		return;
	}
	fmt = line->format;

	while(*str) {
		/* Handle escape sequences by skipping both \ and next char */
		if(*str == '\\') {
			*fmt = prev; fmt++; str++;
			if(*str == '\0') break;
			*fmt = prev; fmt++; str++;
			continue;
		}
		/* Handle continuations */
		else if(cont) {
			/* Triple strings ("""...""" or '''...''') */
			if(cont & TXT_TRISTR) {
				find = (cont & TXT_DBLQUOTSTR) ? '"' : '\'';
				if(*str==find && *(str+1)==find && *(str+2)==find) {
					*fmt = 'l'; fmt++; str++;
					*fmt = 'l'; fmt++; str++;
					cont = 0;
				}
			/* Handle other strings */
			}
			else {
				find = (cont & TXT_DBLQUOTSTR) ? '"' : '\'';
				if(*str == find) cont = 0;
			}

			*fmt = 'l';
		}
		/* Not in a string... */
		else {
			/* Deal with comments first */
			if(prev == '#' || *str == '#')
				*fmt = '#';
			/* Strings */
			else if(*str == '"' || *str == '\'') {
				find = *str;
				cont = (*str== '"') ? TXT_DBLQUOTSTR : TXT_SNGQUOTSTR;
				if(*(str+1) == find && *(str+2) == find) {
					*fmt = 'l'; fmt++; str++;
					*fmt = 'l'; fmt++; str++;
					cont |= TXT_TRISTR;
				}
				*fmt = 'l';
			}
			/* Whitespace (all ws. has been converted to spaces) */
			else if(*str == ' ')
				*fmt = '_';
			/* Numbers (digits not part of an identifier and periods followed by digits) */
			else if((prev != 'q' && text_check_digit(*str)) || (*str == '.' && text_check_digit(*(str+1))))
				*fmt = 'n';
			/* Booleans */
			else if(prev != 'q' && (i=find_bool(str)) != -1)
				if(i>0) {
					while(i>1) {
						*fmt = 'n'; fmt++; str++;
						i--;
					}
					*fmt = 'n';
				}
				else
					*fmt = 'q';
			/* Punctuation */
			else if(text_check_delim(*str))
				*fmt = '!';
			/* Identifiers and other text (no previous ws. or delims. so text continues) */
			else if(prev == 'q')
				*fmt = 'q';
			/* Not ws, a digit, punct, or continuing text. Must be new, check for special words */
			else {
				/* Special vars(v) or built-in keywords(b) */
				if((i=find_specialvar(str)) != -1)
					prev = 'v';
				else if((i=find_builtinfunc(str)) != -1)
					prev = 'b';
				else if((i=find_decorator(str)) != -1)
					prev = 'v'; /* could have a new color for this */
				if(i>0) {
					while(i>1) {
						*fmt = prev; fmt++; str++;
						i--;
					}
					*fmt = prev;
				}
				else
					*fmt = 'q';
			}
		}
		prev = *fmt;
		fmt++;
		str++;
	}

	/* Terminate and add continuation char */
	*fmt = '\0'; fmt++;
	*fmt = cont;

	/* Debugging */
	//print_format(st, line);

	/* If continuation has changed and we're allowed, process the next line */
	if(cont!=orig && do_next && line->next) {
		txt_format_line(st, line->next, do_next);
	}

	flatten_string_free(&fs);
}