Temporarily fixed font wrapping by removing scaling.
This commit is contained in:
@ -45,66 +45,8 @@ void fontDispose(font_t *font) {
|
||||
}
|
||||
|
||||
|
||||
fonttextinfo_t * fontGetTextInfo(font_t *font, char *text, float scale) {
|
||||
int32_t i, j;
|
||||
char c;
|
||||
float x, y;
|
||||
stbtt_aligned_quad *quad;
|
||||
|
||||
// Create info buffer.
|
||||
fonttextinfo_t *info = malloc(sizeof(fonttextinfo_t));
|
||||
|
||||
info->length = 0;
|
||||
info->realLength = 0;
|
||||
|
||||
// Count how many "real characters" are in the string.
|
||||
i = 0;
|
||||
while(c = text[i++]) {
|
||||
info->length++;
|
||||
if(c == FONT_SPACE || c == FONT_NEWLINE) continue;
|
||||
info->realLength++;
|
||||
}
|
||||
|
||||
// Now setup the size scales
|
||||
info->quads = malloc(sizeof(stbtt_aligned_quad) * info->realLength);
|
||||
info->scale = scale * FONT_SCALE_ADJUST;
|
||||
info->width = 0, info->height = 0;
|
||||
info->lines = 1;
|
||||
|
||||
// Begin buffering quads.
|
||||
x = 0;
|
||||
y = FONT_INITIAL_LINE;
|
||||
i = 0, j = 0;
|
||||
|
||||
while(c = text[i++]) {
|
||||
// Spaces, don't quad just next
|
||||
if(c == FONT_SPACE) {
|
||||
x += FONT_SPACE_SIZE;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Newlines reset X
|
||||
if(c == FONT_NEWLINE) {
|
||||
info->lines++;
|
||||
y += FONT_LINE_HEIGHT;
|
||||
x = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate the quad of the character, store into the array.
|
||||
quad = info->quads + j;
|
||||
stbtt_GetBakedQuad(font->characterData,
|
||||
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
|
||||
((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE
|
||||
);
|
||||
quad->x0 *= info->scale, quad->x1 *= info->scale;
|
||||
quad->y0 *= info->scale, quad->y1 *= info->scale;
|
||||
info->width = mathMax(info->width, quad->x1);
|
||||
info->height = mathMax(info->height, quad->y1);
|
||||
j++;
|
||||
}
|
||||
|
||||
return info;
|
||||
fonttextinfo_t * fontGetTextInfo(font_t *font, char *text) {
|
||||
return fontTextClamp(font, text, 999999);
|
||||
}
|
||||
|
||||
void fontTextInfoDispose(fonttextinfo_t *info) {
|
||||
@ -135,9 +77,7 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info) {
|
||||
}
|
||||
|
||||
|
||||
fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale,
|
||||
float maxWidth
|
||||
) {
|
||||
fonttextinfo_t * fontTextClamp(font_t *font, char *text, float maxWidth) {
|
||||
// This method is similar to fontGetTextInfo, but will modify the text.
|
||||
int32_t i, j, wordIndex;
|
||||
char c;
|
||||
@ -155,7 +95,6 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale,
|
||||
// Setup Scales
|
||||
info->length = 0;
|
||||
info->realLength = 0;
|
||||
info->scale = scale * FONT_SCALE_ADJUST;
|
||||
info->lines = 1;
|
||||
|
||||
// Unlike GetTextInfo we can actually calculate the quads at the same time
|
||||
@ -170,22 +109,24 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale,
|
||||
// When space, start of new word about to begin
|
||||
if(c == FONT_SPACE) {
|
||||
x += FONT_SPACE_SIZE;
|
||||
|
||||
// Did this space cause a newline?
|
||||
if(x > maxWidth) {
|
||||
info->lines++;
|
||||
wordStart = i+1;
|
||||
y += FONT_LINE_HEIGHT;
|
||||
x = 0;
|
||||
}
|
||||
// info->width = mathMax(info->width, x);
|
||||
// info->height = mathMax(info->height, y);
|
||||
wordX = x;
|
||||
wordStart = i+1;
|
||||
wordStart = info->realLength;
|
||||
continue;
|
||||
}
|
||||
|
||||
// New line.
|
||||
if(c == FONT_NEWLINE) {
|
||||
info->lines++;
|
||||
wordStart = i+1;
|
||||
wordStart = info->realLength;
|
||||
y += FONT_LINE_HEIGHT;
|
||||
x = 0;
|
||||
continue;
|
||||
@ -205,24 +146,21 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale,
|
||||
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
|
||||
((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE
|
||||
);
|
||||
quad->x0 *= info->scale, quad->x1 *= info->scale;
|
||||
quad->y0 *= info->scale, quad->y1 *= info->scale;
|
||||
|
||||
|
||||
// Now measure the width of the line (take the right side of that quad)
|
||||
if(quad->x1 > maxWidth) {
|
||||
if(x > maxWidth) {
|
||||
// We've exceeded the edge, go back to the start of the word and newline.
|
||||
info->lines++;
|
||||
for(j = wordStart; j <= i; j++) {
|
||||
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;
|
||||
x = 0;
|
||||
y += FONT_LINE_HEIGHT;
|
||||
}
|
||||
y += FONT_LINE_HEIGHT;
|
||||
info->lines++;
|
||||
wordX = 0;
|
||||
}
|
||||
|
||||
info->width = mathMax(info->width, quad->x1);
|
||||
info->height = mathMax(info->height, quad->y1);
|
||||
info->realLength++;
|
||||
}
|
||||
|
||||
|
@ -32,10 +32,9 @@ void fontDispose(font_t *Font);
|
||||
*
|
||||
* @param font Font to use for rendering and measuring
|
||||
* @param text Text to measure/render.
|
||||
* @param scale Scale of the text.
|
||||
* @returns Font info calculated.
|
||||
*/
|
||||
fonttextinfo_t * fontGetTextInfo(font_t *font, char *text, float scale);
|
||||
fonttextinfo_t * fontGetTextInfo(font_t *font, char *text);
|
||||
|
||||
/**
|
||||
* Disposes a previously calculated font info struct..
|
||||
@ -64,17 +63,12 @@ void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
|
||||
void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Clamps text to a max width, inserting newlines where possible.
|
||||
*
|
||||
* @param font Font to use
|
||||
* @param text Text to clamp
|
||||
* @param scale Scale of the text to render.
|
||||
* @param maxWidth Max width (in pixels) to clamp the text to.
|
||||
* @returns The calculated text information.
|
||||
*/
|
||||
fonttextinfo_t * fontTextClamp(font_t *font,char *text,float scale,
|
||||
float maxWidth
|
||||
);
|
||||
fonttextinfo_t * fontTextClamp(font_t *font,char *text, float maxWidth);
|
Reference in New Issue
Block a user