Fix left and top border issues with canvas text (nicer graphs)

This commit is contained in:
pancake 2015-01-11 02:28:59 +01:00
parent c61368a0f3
commit d713debdfc
3 changed files with 40 additions and 12 deletions

View file

@ -55,7 +55,7 @@ R_API int r_cons_canvas_gotoxy(RConsCanvas *c, int x, int y) {
ret = R_FALSE;
}
if (x <0) {
c->x = 0;
//c->x = 0;
ret = R_FALSE;
}
if (y <0) {
@ -99,7 +99,7 @@ static char *prefixline(RConsCanvas *c, int *left) {
}
R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) {
int left, slen, i;
int left, slen, i, linenum = 0;
char *p, *s, *str;
char *line, *n;
@ -110,8 +110,9 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) {
line = getrow (s, &n);
p = prefixline (c, &left);
slen = R_MIN (left-1, strlen (line));
if (slen<1)
if (slen<1) {
break;
}
if (!G (c->x-c->sx+slen, c->y-c->sy)) {
// TODO : chop slen
slen = (c->w - (c->x-c->sx));
@ -119,13 +120,20 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) {
break;
continue;
}
// top border skipping lines
//if ((c->y-1-c->sy)<-40) {
// continue;
//}
int delta = 0;
if (!G (c->x-c->sx-slen, c->y-c->sy))
continue;
memcpy (p, line, slen);
memcpy (p, line+delta, slen-delta);
if (!n) break;
s = n;
if (!G (c->x-c->sx, c->y+1-c->sy))
if (!G (c->x-c->sx, c->y+1-c->sy)) {
break;
}
linenum ++;
}
free (str);
}

View file

@ -55,6 +55,8 @@ static Edge edges[] = {
static void Node_print(RConsCanvas *can, Node *n, int cur) {
char title[128];
int delta_x = 0;
int delta_y = 0;
if (small_nodes) {
if (!G (n->x+2, n->y-1))
@ -89,6 +91,16 @@ static void Node_print(RConsCanvas *can, Node *n, int cur) {
return;
}
#endif
{
int x = n->x + can->sx;
int y = n->y + can->sy;
if (x<-2) {
delta_x = -x-2;
}
if (y<-1) {
delta_y = -y-2;
}
}
if (cur) {
//F (n->x,n->y, n->w, n->h, '.');
snprintf (title, sizeof (title)-1,
@ -102,9 +114,16 @@ static void Node_print(RConsCanvas *can, Node *n, int cur) {
G (n->x+2, n->y+2);
//if (
// TODO: temporary crop depending on out of screen offsets
//n->text = r_str_crop (n->text, 1,1,4,4);
W (n->text);
//}
{
char *text = r_str_crop (n->text,
delta_x, delta_y, 9999, 9999);
if (text) {
W (text);
free (text);
} else {
W (n->text);
}
}
if (G (n->x+1, n->y+1))
W (title);
B (n->x, n->y, n->w, n->h);

View file

@ -1474,10 +1474,11 @@ R_API char *r_str_crop(const char *str, int x, int y, int w, int h) {
*r++ = *str;
ch++;
cw = 0;
} else
if (ch>=y && ch<h)
if (cw>=x && cw<w)
*r++ = *str;
} else {
if (ch>=y && ch<h)
if (cw>=x && cw<w)
*r++ = *str;
}
str++;
cw++;
}