Added font sizing, needs full testing

This commit is contained in:
2021-08-03 10:19:31 -07:00
parent c451538def
commit 23f1cb3d7e
17 changed files with 202 additions and 15 deletions

View File

@ -44,7 +44,11 @@ void fontDispose(font_t *font) {
textureDispose(&font->texture);
}
void fontTextBuffer(font_t *font,primitive_t *primitive,fonttextinfo_t *info) {
float fontGetScale(float fontSize) {
return fontSize / FONT_SIZE_DEFAULT;
}
void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info){
stbtt_aligned_quad *quad;
int32_t i;
@ -66,13 +70,19 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info) {
}
void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
float maxWidth
float maxWidth, float fontSize
) {
int32_t i, j, wordIndex;
char c;
float x, y, wordX;
float x, y, wordX, scale;
stbtt_aligned_quad *quad;
// Get the font scale
scale = fontGetScale(fontSize);
// Adjust the max width to match the scale, and allow "no max width".
maxWidth = maxWidth == -1 ? 999999 : maxWidth * (1 / scale);
/** Which index in the original text var is the current word from */
int32_t wordStart = 0;
@ -98,7 +108,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
// When space, start of new word about to begin
if(c == FONT_SPACE) {
x += FONT_SPACE_SIZE;
x += FONT_SPACE_SIZE * scale;
// Did this space cause a newline?
if(x > maxWidth) {
@ -106,7 +116,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
info->lines[info->lineCount].start = info->realLength;
info->lines[info->lineCount].length = 0;
y += FONT_LINE_HEIGHT;
y += FONT_LINE_HEIGHT * scale;
x = 0;
}
wordX = x;
@ -121,7 +131,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
info->lines[info->lineCount].length = 0;
wordStart = info->realLength;
y += FONT_LINE_HEIGHT;
y += FONT_LINE_HEIGHT * scale;
x = 0;
continue;
}
@ -139,8 +149,10 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
x = quad->x1 - wordX;
for(j = wordStart; j <= info->realLength; j++) {
quad = info->quads + j;
quad->x0 -= wordX, quad->x1 -= wordX;
quad->y0 += FONT_LINE_HEIGHT, quad->y1 += FONT_LINE_HEIGHT;
quad->x0 -= wordX;
quad->x1 -= wordX;
quad->y0 += FONT_LINE_HEIGHT;
quad->y1 += FONT_LINE_HEIGHT;
}
// Go back to the previous (still current) line and remove the chars
@ -163,6 +175,16 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
for(j = 0; j < info->realLength; j++) {
quad = info->quads + j;
// Scale the Quad
if(scale != 1.0) {
quad->x0 *= scale;
quad->x1 *= scale;
quad->y0 *= scale;
quad->y1 *= scale;
}
// Update the dimensions.
info->width = mathMax(info->width, quad->x1);
info->height = mathMax(info->height, quad->y1);
}

View File

@ -45,6 +45,14 @@ void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
*/
void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
/**
* Convert a Font's Size into a Font Scale. The scale is based on the font's
* default size for the given texture size (Refer to FONT_SIZE_DEFAULT).
*
* @param fontSize Font size to convert.
* @return The times scale that the font size is to the textured default.
*/
float fontGetScale(float fontSize);
/**
* Clamps text to a max width, inserting newlines where possible.
@ -52,10 +60,11 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
* @param font Font to use
* @param info Font text info to clamp into.
* @param text Text to clamp.
* @param maxWidth Max width (in pixels) to clamp the text to.
* @param maxWidth Max width (pixels) to clamp the text to, -1 for no max width.
* @param fontSize Font Size of the text.
*/
void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
float maxWidth
float maxWidth, float fontSize
);
/**