Better text wrapping

This commit is contained in:
jacksonmj
2011-02-05 15:44:29 +00:00
parent 196ccd1e36
commit 21d0dd0ca2

View File

@@ -855,7 +855,20 @@ int drawtextwrap(pixel *vid, int x, int y, int w, const char *s, int r, int g, i
int rh = 12;
int rw = 0;
int cw = x;
for (; *s; s++)
int wordlen;
int charspace;
while (*s)
{
wordlen = strcspn(s," .,!?\n");
charspace = textwidthx(s, w-(x-cw));
if (charspace<wordlen && wordlen && w-(x-cw)<w/3)
{
x = sx;
rw = 0;
y+=FONT_H+2;
rh+=FONT_H+2;
}
for (; *s && --wordlen>=-1; s++)
{
if (*s == '\n')
{
@@ -891,7 +904,9 @@ int drawtextwrap(pixel *vid, int x, int y, int w, const char *s, int r, int g, i
}
else
{
if (x-cw>=w) {
if (x-cw>=w)
{
x = sx;
rw = 0;
y+=FONT_H+2;
@@ -900,6 +915,7 @@ int drawtextwrap(pixel *vid, int x, int y, int w, const char *s, int r, int g, i
x = drawchar(vid, x, y, *(unsigned char *)s, r, g, b, a);
}
}
}
#endif
return rh;
}
@@ -1009,19 +1025,30 @@ void textnpos(char *s, int n, int w, int *cx, int *cy)
{
int x = 0;
int y = 0;
//TODO: Implement Textnheight for wrapped text
for (; *s; s++)
int wordlen, charspace;
while (*s&&n)
{
wordlen = strcspn(s," .,!?\n");
charspace = textwidthx(s, w-x);
if (charspace<wordlen && wordlen && w-x<w/3)
{
x = 0;
y += FONT_H+2;
}
for (; *s && --wordlen>=-1; s++)
{
if (!n) {
break;
}
x += font_data[font_ptrs[(int)(*(unsigned char *)s)]];
if (x>=w) {
if (x>=w)
{
x = 0;
y += FONT_H+2;
}
n--;
}
}
*cx = x-1;
*cy = y;
}
@@ -1041,12 +1068,21 @@ int textwidthx(char *s, int w)
}
int textposxy(char *s, int width, int w, int h)
{
int x=0,y=0,n=0,cw;
for (; *s; s++)
int x=0,y=0,n=0,cw, wordlen, charspace;
while (*s)
{
wordlen = strcspn(s," .,!?\n");
charspace = textwidthx(s, width-x);
if (charspace<wordlen && wordlen && width-x<width/3)
{
x = 0;
y += FONT_H+2;
}
for (; *s && --wordlen>=-1; s++)
{
cw = font_data[font_ptrs[(int)(*(unsigned char *)s)]];
if (x+(cw/2) >= w && y+6 >= h)
break;
if ((x+(cw/2) >= w && y+6 >= h)||(y+6 >= h+FONT_H+2))
return n++;
x += cw;
if (x>=width) {
x = 0;
@@ -1054,6 +1090,7 @@ int textposxy(char *s, int width, int w, int h)
}
n++;
}
}
return n;
}