[Change] Extracted some more common font code.
This commit is contained in:
parent
a82f431cd4
commit
5fcf3bbc92
141
src/font.c
141
src/font.c
@ -36,6 +36,13 @@ static void glFontMakeDList(FT_Face face, char ch,
|
|||||||
static int font_limitSize(const glFont* ft_font, int* width,
|
static int font_limitSize(const glFont* ft_font, int* width,
|
||||||
char* text, const int max);
|
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,
|
static int font_limitSize(const glFont* ft_font, int* width,
|
||||||
char* text, const int max) {
|
char* text, const int max) {
|
||||||
int n, len, i;
|
int n, len, i;
|
||||||
@ -56,6 +63,50 @@ static int font_limitSize(const glFont* ft_font, int* width,
|
|||||||
return i;
|
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,
|
* @fn void gl_print(const glFont* ft_font, const double x, const double y,
|
||||||
* const glColour* c, const char* fmt, ...)
|
* 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. */
|
/*float h = ft_font->h / .63; // Slightly increase font size. */
|
||||||
char txt[4096];
|
char txt[4096];
|
||||||
char buf[256];
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int p, i, j, n, m, len, ret, lastspace;
|
int i, p;
|
||||||
double x, y;
|
double x, y;
|
||||||
|
|
||||||
ret = 0; /* Default return value. */
|
|
||||||
|
|
||||||
if(ft_font == NULL) ft_font = &gl_defFont;
|
if(ft_font == NULL) ft_font = &gl_defFont;
|
||||||
|
|
||||||
if(fmt == NULL) return -1;
|
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.);
|
if(c == NULL) glColor4d(1., 1., 1., 1.);
|
||||||
else COLOUR(*c);
|
else COLOUR(*c);
|
||||||
|
|
||||||
len = (int)strlen(txt);
|
p = 0; /* Where we last drew up to. */
|
||||||
/* Limit size per line. */
|
while(1) {
|
||||||
lastspace = -1; /* Last ' ' or \n int text. */
|
i = gl_printWidthForText(ft_font, &txt[p], width);
|
||||||
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. */
|
|
||||||
|
|
||||||
n += ft_font->w[(int)txt[i]];
|
glMatrixMode(GL_MODELVIEW); /* Using MODELVIEW, PROJECTION gets full fast. */
|
||||||
|
glPushMatrix(); /* Translation matrix. */
|
||||||
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. */
|
|
||||||
glTranslated(x, y, 0);
|
glTranslated(x, y, 0);
|
||||||
|
|
||||||
/* This is what we are displaying. */
|
glCallLists(i, GL_UNSIGNED_BYTE, &txt[p]); /* The actual displaying. */
|
||||||
glCallLists(j, GL_UNSIGNED_BYTE, &buf);
|
|
||||||
glPopMatrix(); /* Translation matrix. */
|
|
||||||
|
|
||||||
p = lastspace;
|
glPopMatrix(); /* Translation matrix. */
|
||||||
n = 0;
|
|
||||||
i = lastspace;
|
if(txt[p+i] == '\0')
|
||||||
y -= 1.5*(double)ft_font->h; /* Move position down. */
|
break;
|
||||||
}
|
p += i + 1;
|
||||||
i++;
|
y -= 1.5*(double)ft_font->h; /* Move position down. */
|
||||||
|
if(by - y > (double)height)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
gl_checkErr();
|
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. */
|
char txt[1024]; /* Holds the string. */
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int p, i, n, len, lastspace;
|
int i, p;
|
||||||
double y;
|
double y;
|
||||||
|
|
||||||
if(ft_font == NULL) ft_font = &gl_defFont;
|
if(ft_font == NULL) ft_font = &gl_defFont;
|
||||||
@ -379,27 +401,14 @@ int gl_printHeight(const glFont* ft_font, const int width,
|
|||||||
}
|
}
|
||||||
y = 0.;
|
y = 0.;
|
||||||
|
|
||||||
len = (int) strlen(txt);
|
p = 0;
|
||||||
/* Limit the size per line. */
|
while(1) {
|
||||||
lastspace = -1; /* Last ' ' or '\n' in the text. */
|
i = gl_printWidthForText(ft_font, &txt[p], width);
|
||||||
n = 0; /* Current width. */
|
|
||||||
i = 0; /* Current position. */
|
|
||||||
p = -1; /* Where we last drew up to. */
|
|
||||||
|
|
||||||
while(i < len+1) {
|
if(txt[p+i] == '\0')
|
||||||
n += ft_font->w[(int)txt[i]];
|
break;
|
||||||
|
p += i + 1;
|
||||||
if((txt[i] == ' ') || (txt[i] == '\n') || (txt[i] == '\0')) lastspace = i;
|
y += 1.5*(double)ft_font->h; /* Move position down. */
|
||||||
|
|
||||||
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++;
|
|
||||||
}
|
}
|
||||||
return (int)(y - 0.5*(double)ft_font->h);
|
return (int)(y - 0.5*(double)ft_font->h);
|
||||||
}
|
}
|
||||||
|
@ -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,
|
int gl_printText(const glFont* ft_font, const int width, const int height,
|
||||||
double bx, double by, glColour* c, const char* fmt, ...);
|
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_printWidth(const glFont* ft_font, const char* fmt, ...);
|
||||||
|
|
||||||
int gl_printHeight(const glFont* ft_font, const int width,
|
int gl_printHeight(const glFont* ft_font, const int width,
|
||||||
|
Loading…
Reference in New Issue
Block a user