[Change] Extracted some more common font code.

This commit is contained in:
Allanis 2014-02-11 04:18:03 +00:00
parent a82f431cd4
commit 5fcf3bbc92
2 changed files with 79 additions and 67 deletions

View File

@ -36,6 +36,13 @@ static void glFontMakeDList(FT_Face face, char ch,
static int font_limitSize(const glFont* ft_font, int* width,
char* text, const int max);
/**
* @brief Limits the text to max.
* @param ft_font Font to calculate width with.
* @param width Actual width it takes up.
* @param text Text to parse.
* @param max Max to look for.
*/
static int font_limitSize(const glFont* ft_font, int* width,
char* text, const int max) {
int n, len, i;
@ -56,6 +63,50 @@ static int font_limitSize(const glFont* ft_font, int* width,
return i;
}
/**
* @brief Get the number of characters in text that fit into width.
* @param ft_font Font to use.
* @param text Text to check.
* @param width Width to match.
* @return Number of characters that fit.
*/
int gl_printWidthForText(const glFont* ft_font, char* text,
const int width) {
int i, n, lastspace;
if(ft_font == NULL) ft_font = &gl_defFont;
/* Limit size per line. */
lastspace = 0; /* last ' ' or '\n' in the text. */
n = 0; /* Current width. */
i = 0; /* Current position. */
while(1) {
/* Check if we found an EOL character. */
if((text[i] == '\n') || (text[i] == '\0'))
return i;
/* Characters we should ignore. */
if(text[i] == '\t')
continue;
/* Increase size. */
n += ft_font->w[(int)text[i]];
/* Save last space. */
if(text[i] == ' ')
lastspace = i;
/* Check if out of bounds. */
if(n > width)
break;
/* Check next character. */
i++;
}
return lastspace;
}
/**
* @fn void gl_print(const glFont* ft_font, const double x, const double y,
* const glColour* c, const char* fmt, ...)
@ -238,13 +289,10 @@ int gl_printText(const glFont* ft_font, const int width, const int height,
/*float h = ft_font->h / .63; // Slightly increase font size. */
char txt[4096];
char buf[256];
va_list ap;
int p, i, j, n, m, len, ret, lastspace;
int i, p;
double x, y;
ret = 0; /* Default return value. */
if(ft_font == NULL) ft_font = &gl_defFont;
if(fmt == NULL) return -1;
@ -265,56 +313,30 @@ int gl_printText(const glFont* ft_font, const int width, const int height,
if(c == NULL) glColor4d(1., 1., 1., 1.);
else COLOUR(*c);
len = (int)strlen(txt);
/* Limit size per line. */
lastspace = -1; /* Last ' ' or \n int text. */
n = 0; /* Current width. */
i = 0; /* Current position. */
p = -1; /* Where we last drew up to. */
while(i < len+1) {
if(by - y > (double)height) return len-lastspace; /* Past height. */
p = 0; /* Where we last drew up to. */
while(1) {
i = gl_printWidthForText(ft_font, &txt[p], width);
n += ft_font->w[(int)txt[i]];
if((txt[i] == ' ') || (txt[i] == '\n') || (txt[i] == '\0')) lastspace = i;
if(((n > width) && ((p != lastspace)))
|| (txt[i] == '\n') || (txt[i] == '\0')) {
/* Time to draw the line. */
m = 0;
if(lastspace == -1) lastspace = 0;
for(j = 0; j < (lastspace-p-1); j++) {
if(txt[p+j+1]=='\t') {
p++;
continue;
}
m += ft_font->w[(int)txt[p+j+1]];
if(m > width) break;
buf[j] = txt[p+j+1];
}
/* No need for null termination. */
glMatrixMode(GL_MODELVIEW); /* using modelview, projection gets full fast. */
glPushMatrix(); /* Translation matrix. */
glMatrixMode(GL_MODELVIEW); /* Using MODELVIEW, PROJECTION gets full fast. */
glPushMatrix(); /* Translation matrix. */
glTranslated(x, y, 0);
/* This is what we are displaying. */
glCallLists(j, GL_UNSIGNED_BYTE, &buf);
glPopMatrix(); /* Translation matrix. */
glCallLists(i, GL_UNSIGNED_BYTE, &txt[p]); /* The actual displaying. */
p = lastspace;
n = 0;
i = lastspace;
y -= 1.5*(double)ft_font->h; /* Move position down. */
}
i++;
glPopMatrix(); /* Translation matrix. */
if(txt[p+i] == '\0')
break;
p += i + 1;
y -= 1.5*(double)ft_font->h; /* Move position down. */
if(by - y > (double)height)
break;
}
glDisable(GL_TEXTURE_2D);
gl_checkErr();
return ret;
return 0;
}
/**
@ -365,7 +387,7 @@ int gl_printHeight(const glFont* ft_font, const int width,
char txt[1024]; /* Holds the string. */
va_list ap;
int p, i, n, len, lastspace;
int i, p;
double y;
if(ft_font == NULL) ft_font = &gl_defFont;
@ -379,27 +401,14 @@ int gl_printHeight(const glFont* ft_font, const int width,
}
y = 0.;
len = (int) strlen(txt);
/* Limit the size per line. */
lastspace = -1; /* Last ' ' or '\n' in the text. */
n = 0; /* Current width. */
i = 0; /* Current position. */
p = -1; /* Where we last drew up to. */
p = 0;
while(1) {
i = gl_printWidthForText(ft_font, &txt[p], width);
while(i < len+1) {
n += ft_font->w[(int)txt[i]];
if((txt[i] == ' ') || (txt[i] == '\n') || (txt[i] == '\0')) lastspace = i;
if(((n > width) && (p != lastspace)) ||
(txt[i] == '\n') || (txt[i] == '\0')) {
p = lastspace;
n = 0;
i = lastspace;
y += 1.5*(double)ft_font->h; /* Move position down. */
}
i++;
if(txt[p+i] == '\0')
break;
p += i + 1;
y += 1.5*(double)ft_font->h; /* Move position down. */
}
return (int)(y - 0.5*(double)ft_font->h);
}

View File

@ -37,7 +37,10 @@ int gl_printMid(const glFont* ft_font, const int width,
int gl_printText(const glFont* ft_font, const int width, const int height,
double bx, double by, glColour* c, const char* fmt, ...);
/* Get the width of the text that you wish to print. */
/* Dimension stuff. */
int gl_printWidthForText(const glFont* ft_font, char* text,
const int width);
int gl_printWidth(const glFont* ft_font, const char* fmt, ...);
int gl_printHeight(const glFont* ft_font, const int width,