font system improvements

main
Dominik Madarász 2023-12-10 16:01:36 +01:00
parent 990f58b282
commit 02ddcb0af8
11 changed files with 162 additions and 40 deletions

View File

@ -2327,6 +2327,7 @@ FONT_CJK = FONT_ZH|FONT_JP|FONT_KR,
void font_face_from_mem(const char *tag, const void *ttf_buffer, unsigned ttf_len, float font_size, unsigned flags);
void font_scales(const char *face_tag, float h1, float h2, float h3, float h4, float h5, float h6);
void font_color(const char *color_tag, uint32_t color);
void ui_font();
vec2 font_xy();
void font_goto(float x, float y);
vec2 font_print(const char *text);
@ -3341,7 +3342,7 @@ unsigned play;
bool paused;
struct atlas_t *a;
} sprite_t;
enum { OBJTYPE_sprite_t = 10 }; typedef struct { unsigned static_assert_on_L__LINE__ : !!(10 <= 255); } static_assert_on_Lconcat(_L,3962)___COUNTER__; typedef struct { unsigned static_assert_on_L__LINE__ : !!(sizeof(sprite_t)); } static_assert_on_Lconcat(_L,3962)___COUNTER__;;
enum { OBJTYPE_sprite_t = 10 }; typedef struct { unsigned static_assert_on_L__LINE__ : !!(10 <= 255); } static_assert_on_Lconcat(_L,3963)___COUNTER__; typedef struct { unsigned static_assert_on_L__LINE__ : !!(sizeof(sprite_t)); } static_assert_on_Lconcat(_L,3963)___COUNTER__;;
void sprite_ctor(sprite_t *s);
void sprite_dtor(sprite_t *s);
void sprite_tick(sprite_t *s);

View File

@ -110,5 +110,45 @@ int main() {
font_print(FONT_MIDDLE FONT_RIGHT "middle\n");
font_print(FONT_BASELINE FONT_RIGHT "baseline\n");
font_print(FONT_BOTTOM FONT_CENTER "bottom\n");
{
vec2 pos = vec2(1290,120);
ddraw_push_2d();
char *txt = "This is the first line.\nAnd now the second line.\nYou can do a third great line, too!\n";
font_goto(pos.x, pos.y);
vec2 size=font_rect(txt);
ddraw_aabb(vec3(pos.x,pos.y,0), vec3(pos.x+size.x,pos.y+size.y,0));
font_print(txt);
ddraw_pop_2d();
}
{
vec2 pos = vec2(830,80);
ddraw_push_2d();
char *txt = "Very iffy global text.";
font_goto(pos.x, pos.y);
vec2 size=font_rect(txt);
ddraw_aabb(vec3(pos.x,pos.y,0), vec3(pos.x+size.x,pos.y+size.y,0));
font_print(txt);
ddraw_pop_2d();
}
{
vec2 pos = vec2(830,160);
ddraw_push_2d();
char *txt = FONT_H1 "Very iffy global text.";
font_goto(pos.x, pos.y);
vec2 size=font_rect(txt);
ddraw_aabb(vec3(pos.x,pos.y,0), vec3(pos.x+size.x,pos.y+size.y,0));
font_print(txt);
ddraw_pop_2d();
}
if (ui_panel("Fonts", 0)) {
ui_font();
ui_panel_end();
}
}
}

View File

@ -16120,6 +16120,7 @@ API void font_face(const char *face_tag, const char *filename_ttf, float font_s
API void font_face_from_mem(const char *tag, const void *ttf_buffer, unsigned ttf_len, float font_size, unsigned flags);
API void font_scales(const char *face_tag, float h1, float h2, float h3, float h4, float h5, float h6);
API void font_color(const char *color_tag, uint32_t color);
API void ui_font();
// commands
API vec2 font_xy();
@ -359908,6 +359909,7 @@ typedef struct font_t {
int height; // bitmap height
int width; // bitmap width
float font_size; // font size in pixels (matches scale[0+1] size below)
float factor; // font factor (font_size / (ascent - descent))
float scale[7]; // user defined font scale (match H1..H6 tags)
// displacement info
@ -359971,6 +359973,27 @@ void font_color(const char *tag, uint32_t color) {
}
}
void ui_font() {
for( int i = 0; i < countof(fonts); ++i ) {
if( ui_collapse(va("Font %d", i), va("%p%d", &fonts[i], i) ) ) {
font_t *f = &fonts[i];
// changed = i+1;
// for( int j = 0; j < array_count(a->anims[i].frames); ++j ) {
// if( ui_collapse(va("[%d]",j), va("%p%d.%d", a, a->anims[i].name,j) ) ) {
// ui_unsigned("Frame", &a->anims[i].frames[j]);
// ui_atlas_frame(a->frames + a->anims[i].frames[j]);
// ui_collapse_end();
// }
// }
ui_float("Ascent", &f->ascent);
ui_float("Descent", &f->descent);
ui_float("Line Gap", &f->linegap);
f->linedist = (f->ascent-f->descent+f->linegap);
ui_collapse_end();
}
}
}
void font_scales(const char *tag, float h1, float h2, float h3, float h4, float h5, float h6) {
font_init();
@ -360116,13 +360139,14 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len,
stbtt_InitFont(&info, ttf_data, stbtt_GetFontOffsetForIndex(ttf_data,0));
int a, d, l;
float s = stbtt_ScaleForPixelHeight(&info, f->font_size);
stbtt_GetFontVMetrics(&info, &a, &d, &l);
if (!stbtt_GetFontVMetricsOS2(&info, &a, &d, &l))
stbtt_GetFontVMetrics(&info, &a, &d, &l);
f->ascent = a * s;
f->descent = d * s;
f->linegap = l * s;
f->linedist = (a - d + l) * s;
f->ascent = a;
f->descent = d;
f->linegap = l;
f->linedist = (a - d + l);
f->factor = (f->font_size / (f->ascent - f->descent));
// save some gpu memory by truncating unused vertical space in atlas texture
{
@ -360240,8 +360264,6 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len,
glUniform2f(glGetUniformLocation(f->program, "res_bitmap"), f->width, f->height);
glUniform2f(glGetUniformLocation(f->program, "res_meta"), f->num_glyphs, 2);
glUniform1f(glGetUniformLocation(f->program, "num_colors"), FONT_MAX_COLORS);
glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->linedist-f->linegap);
(void)flags;
}
@ -360302,6 +360324,7 @@ void font_draw_cmd(font_t *f, const float *glyph_data, int glyph_idx, float fact
glUseProgram(f->program);
glUniform1f(glGetUniformLocation(f->program, "scale_factor"), factor);
glUniform2fv(glGetUniformLocation(f->program, "string_offset"), 1, &offset.x);
glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->ascent*f->factor);
GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, dims);
@ -360353,7 +360376,7 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
font_t *f = &fonts[0];
int S = 3;
uint32_t color = 0;
float X = 0, Y = 0, W = 0, L = f->linedist*f->scale[S], LL = L; // LL=largest linedist
float X = 0, Y = 0, W = 0, L = f->ascent*f->factor*f->scale[S], LL = L; // LL=largest linedist
offset.y = -offset.y; // invert y polarity
// utf8 to utf32
@ -360368,7 +360391,10 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
// change cursor, advance y, record largest x as width, increase height
if( X > W ) W = X;
X = 0.0;
Y -= L;
Y -= f->linedist*f->factor*f->scale[S];
if (i+1==end) { //@hack: ensures we terminate the height at the correct position
Y -= (f->descent+f->linegap)*f->factor*f->scale[S];
}
continue;
}
if( ch >= 1 && ch <= 6 ) {
@ -360377,11 +360403,12 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
t = text_glyph_data;
// reposition offset to align new baseline
offset.y += (f->linedist - f->linegap) * ( f->scale[ch] - f->scale[S] );
// @fixme:
// offset.y += (f->linedist - f->linegap) * ( f->scale[ch] - f->scale[S] );
// change size
S = ch;
L = f->linedist*f->scale[S];
L = f->ascent*f->factor*f->scale[S];
if(L > LL) LL = L;
continue;
}
@ -360962,7 +360989,7 @@ bool gui_slider_id(int id, vec4 rect, const char *skin, float min, float max, fl
cursorrect.x += cursorpos.x;
cursorrect.y += cursorpos.y;
cursorrect.z = cursorsize.x;
cursorrect.w = cursorsize.y;
cursorrect.w = max(cursorsize.y, rect.w);
if (last_skin->drawrect) last_skin->drawrect(last_skin->userdata, cursorskin, fbcursor, cursorrect);
return entry->held && (old_value!=*value);

View File

@ -1583,6 +1583,7 @@ typedef struct font_t {
int height; // bitmap height
int width; // bitmap width
float font_size; // font size in pixels (matches scale[0+1] size below)
float factor; // font factor (font_size / (ascent - descent))
float scale[7]; // user defined font scale (match H1..H6 tags)
// displacement info
@ -1646,6 +1647,27 @@ void font_color(const char *tag, uint32_t color) {
}
}
void ui_font() {
for( int i = 0; i < countof(fonts); ++i ) {
if( ui_collapse(va("Font %d", i), va("%p%d", &fonts[i], i) ) ) {
font_t *f = &fonts[i];
// changed = i+1;
// for( int j = 0; j < array_count(a->anims[i].frames); ++j ) {
// if( ui_collapse(va("[%d]",j), va("%p%d.%d", a, a->anims[i].name,j) ) ) {
// ui_unsigned("Frame", &a->anims[i].frames[j]);
// ui_atlas_frame(a->frames + a->anims[i].frames[j]);
// ui_collapse_end();
// }
// }
ui_float("Ascent", &f->ascent);
ui_float("Descent", &f->descent);
ui_float("Line Gap", &f->linegap);
f->linedist = (f->ascent-f->descent+f->linegap);
ui_collapse_end();
}
}
}
void font_scales(const char *tag, float h1, float h2, float h3, float h4, float h5, float h6) {
font_init();
@ -1791,13 +1813,14 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len,
stbtt_InitFont(&info, ttf_data, stbtt_GetFontOffsetForIndex(ttf_data,0));
int a, d, l;
float s = stbtt_ScaleForPixelHeight(&info, f->font_size);
stbtt_GetFontVMetrics(&info, &a, &d, &l);
if (!stbtt_GetFontVMetricsOS2(&info, &a, &d, &l))
stbtt_GetFontVMetrics(&info, &a, &d, &l);
f->ascent = a * s;
f->descent = d * s;
f->linegap = l * s;
f->linedist = (a - d + l) * s;
f->ascent = a;
f->descent = d;
f->linegap = l;
f->linedist = (a - d + l);
f->factor = (f->font_size / (f->ascent - f->descent));
// save some gpu memory by truncating unused vertical space in atlas texture
{
@ -1915,8 +1938,6 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len,
glUniform2f(glGetUniformLocation(f->program, "res_bitmap"), f->width, f->height);
glUniform2f(glGetUniformLocation(f->program, "res_meta"), f->num_glyphs, 2);
glUniform1f(glGetUniformLocation(f->program, "num_colors"), FONT_MAX_COLORS);
glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->linedist-f->linegap);
(void)flags;
}
@ -1977,6 +1998,7 @@ void font_draw_cmd(font_t *f, const float *glyph_data, int glyph_idx, float fact
glUseProgram(f->program);
glUniform1f(glGetUniformLocation(f->program, "scale_factor"), factor);
glUniform2fv(glGetUniformLocation(f->program, "string_offset"), 1, &offset.x);
glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->ascent*f->factor);
GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, dims);
@ -2028,7 +2050,7 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
font_t *f = &fonts[0];
int S = 3;
uint32_t color = 0;
float X = 0, Y = 0, W = 0, L = f->linedist*f->scale[S], LL = L; // LL=largest linedist
float X = 0, Y = 0, W = 0, L = f->ascent*f->factor*f->scale[S], LL = L; // LL=largest linedist
offset.y = -offset.y; // invert y polarity
// utf8 to utf32
@ -2043,7 +2065,10 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
// change cursor, advance y, record largest x as width, increase height
if( X > W ) W = X;
X = 0.0;
Y -= L;
Y -= f->linedist*f->factor*f->scale[S];
if (i+1==end) { //@hack: ensures we terminate the height at the correct position
Y -= (f->descent+f->linegap)*f->factor*f->scale[S];
}
continue;
}
if( ch >= 1 && ch <= 6 ) {
@ -2052,11 +2077,12 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
t = text_glyph_data;
// reposition offset to align new baseline
offset.y += (f->linedist - f->linegap) * ( f->scale[ch] - f->scale[S] );
// @fixme:
// offset.y += (f->linedist - f->linegap) * ( f->scale[ch] - f->scale[S] );
// change size
S = ch;
L = f->linedist*f->scale[S];
L = f->ascent*f->factor*f->scale[S];
if(L > LL) LL = L;
continue;
}

View File

@ -75,6 +75,7 @@ API void font_face(const char *face_tag, const char *filename_ttf, float font_s
API void font_face_from_mem(const char *tag, const void *ttf_buffer, unsigned ttf_len, float font_size, unsigned flags);
API void font_scales(const char *face_tag, float h1, float h2, float h3, float h4, float h5, float h6);
API void font_color(const char *color_tag, uint32_t color);
API void ui_font();
// commands
API vec2 font_xy();

View File

@ -262,7 +262,7 @@ bool gui_slider_id(int id, vec4 rect, const char *skin, float min, float max, fl
cursorrect.x += cursorpos.x;
cursorrect.y += cursorpos.y;
cursorrect.z = cursorsize.x;
cursorrect.w = cursorsize.y;
cursorrect.w = max(cursorsize.y, rect.w);
if (last_skin->drawrect) last_skin->drawrect(last_skin->userdata, cursorskin, fbcursor, cursorrect);
return entry->held && (old_value!=*value);

View File

@ -10237,6 +10237,7 @@ typedef struct font_t {
int height; // bitmap height
int width; // bitmap width
float font_size; // font size in pixels (matches scale[0+1] size below)
float factor; // font factor (font_size / (ascent - descent))
float scale[7]; // user defined font scale (match H1..H6 tags)
// displacement info
@ -10300,6 +10301,27 @@ void font_color(const char *tag, uint32_t color) {
}
}
void ui_font() {
for( int i = 0; i < countof(fonts); ++i ) {
if( ui_collapse(va("Font %d", i), va("%p%d", &fonts[i], i) ) ) {
font_t *f = &fonts[i];
// changed = i+1;
// for( int j = 0; j < array_count(a->anims[i].frames); ++j ) {
// if( ui_collapse(va("[%d]",j), va("%p%d.%d", a, a->anims[i].name,j) ) ) {
// ui_unsigned("Frame", &a->anims[i].frames[j]);
// ui_atlas_frame(a->frames + a->anims[i].frames[j]);
// ui_collapse_end();
// }
// }
ui_float("Ascent", &f->ascent);
ui_float("Descent", &f->descent);
ui_float("Line Gap", &f->linegap);
f->linedist = (f->ascent-f->descent+f->linegap);
ui_collapse_end();
}
}
}
void font_scales(const char *tag, float h1, float h2, float h3, float h4, float h5, float h6) {
font_init();
@ -10445,13 +10467,14 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len,
stbtt_InitFont(&info, ttf_data, stbtt_GetFontOffsetForIndex(ttf_data,0));
int a, d, l;
float s = stbtt_ScaleForPixelHeight(&info, f->font_size);
stbtt_GetFontVMetrics(&info, &a, &d, &l);
if (!stbtt_GetFontVMetricsOS2(&info, &a, &d, &l))
stbtt_GetFontVMetrics(&info, &a, &d, &l);
f->ascent = a * s;
f->descent = d * s;
f->linegap = l * s;
f->linedist = (a - d + l) * s;
f->ascent = a;
f->descent = d;
f->linegap = l;
f->linedist = (a - d + l);
f->factor = (f->font_size / (f->ascent - f->descent));
// save some gpu memory by truncating unused vertical space in atlas texture
{
@ -10569,8 +10592,6 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len,
glUniform2f(glGetUniformLocation(f->program, "res_bitmap"), f->width, f->height);
glUniform2f(glGetUniformLocation(f->program, "res_meta"), f->num_glyphs, 2);
glUniform1f(glGetUniformLocation(f->program, "num_colors"), FONT_MAX_COLORS);
glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->linedist-f->linegap);
(void)flags;
}
@ -10631,6 +10652,7 @@ void font_draw_cmd(font_t *f, const float *glyph_data, int glyph_idx, float fact
glUseProgram(f->program);
glUniform1f(glGetUniformLocation(f->program, "scale_factor"), factor);
glUniform2fv(glGetUniformLocation(f->program, "string_offset"), 1, &offset.x);
glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->ascent*f->factor);
GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, dims);
@ -10682,7 +10704,7 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
font_t *f = &fonts[0];
int S = 3;
uint32_t color = 0;
float X = 0, Y = 0, W = 0, L = f->linedist*f->scale[S], LL = L; // LL=largest linedist
float X = 0, Y = 0, W = 0, L = f->ascent*f->factor*f->scale[S], LL = L; // LL=largest linedist
offset.y = -offset.y; // invert y polarity
// utf8 to utf32
@ -10697,7 +10719,10 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
// change cursor, advance y, record largest x as width, increase height
if( X > W ) W = X;
X = 0.0;
Y -= L;
Y -= f->linedist*f->factor*f->scale[S];
if (i+1==end) { //@hack: ensures we terminate the height at the correct position
Y -= (f->descent+f->linegap)*f->factor*f->scale[S];
}
continue;
}
if( ch >= 1 && ch <= 6 ) {
@ -10706,11 +10731,12 @@ vec2 font_draw_ex(const char *text, vec2 offset, const char *col, void (*draw_cm
t = text_glyph_data;
// reposition offset to align new baseline
offset.y += (f->linedist - f->linegap) * ( f->scale[ch] - f->scale[S] );
// @fixme:
// offset.y += (f->linedist - f->linegap) * ( f->scale[ch] - f->scale[S] );
// change size
S = ch;
L = f->linedist*f->scale[S];
L = f->ascent*f->factor*f->scale[S];
if(L > LL) LL = L;
continue;
}
@ -11291,7 +11317,7 @@ bool gui_slider_id(int id, vec4 rect, const char *skin, float min, float max, fl
cursorrect.x += cursorpos.x;
cursorrect.y += cursorpos.y;
cursorrect.z = cursorsize.x;
cursorrect.w = cursorsize.y;
cursorrect.w = max(cursorsize.y, rect.w);
if (last_skin->drawrect) last_skin->drawrect(last_skin->userdata, cursorskin, fbcursor, cursorrect);
return entry->held && (old_value!=*value);

View File

@ -2187,6 +2187,7 @@ API void font_face(const char *face_tag, const char *filename_ttf, float font_s
API void font_face_from_mem(const char *tag, const void *ttf_buffer, unsigned ttf_len, float font_size, unsigned flags);
API void font_scales(const char *face_tag, float h1, float h2, float h3, float h4, float h5, float h6);
API void font_color(const char *color_tag, uint32_t color);
API void ui_font();
// commands
API vec2 font_xy();

Binary file not shown.

Binary file not shown.

Binary file not shown.