add font_metrics() method

main
Dominik Madarász 2023-12-10 16:47:42 +01:00
parent 83cbeddb88
commit 616ade56d7
3 changed files with 45 additions and 5 deletions

View File

@ -117,7 +117,8 @@ int main() {
char *txt = "This is the first line.\nAnd now the second line.\nYou can do a third great line, too!\n"; 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); font_goto(pos.x, pos.y);
vec2 size=font_rect(txt); vec2 size=font_rect(txt);
ddraw_aabb(vec3(pos.x,pos.y,0), vec3(pos.x+size.x,pos.y+size.y,0)); font_metrics_t m=font_metrics(txt);
ddraw_aabb(vec3(pos.x,pos.y,0), vec3(pos.x+size.x,pos.y+size.y-m.descent+m.linegap,0));
font_print(txt); font_print(txt);
ddraw_pop_2d(); ddraw_pop_2d();
} }

View File

@ -11050,6 +11050,37 @@ vec2 font_highlight(const char *text, const void *colors) {
vec2 font_rect(const char *str) { vec2 font_rect(const char *str) {
return font_draw_ex(str, gotoxy, NULL, NULL); return font_draw_ex(str, gotoxy, NULL, NULL);
} }
font_metrics_t font_metrics(const char *text) {
font_metrics_t m={0};
int S = 3;
font_t *f = &fonts[0];
// utf8 to utf32
array(uint32_t) unicode = string32(text);
// parse string
for( int i = 0, end = array_count(unicode); i < end; ++i ) {
uint32_t ch = unicode[i];
if( ch >= 1 && ch <= 6 ) {
S = ch;
continue;
}
if( ch >= 0x1a && ch <= 0x1f ) {
if( fonts[ ch - 0x1a ].initialized) {
// change face
f = &fonts[ ch - 0x1a ];
}
continue;
}
}
m.ascent = f->ascent*f->factor*f->scale[S];
m.descent = f->descent*f->factor*f->scale[S];
m.linegap = f->linegap*f->factor*f->scale[S];
m.linedist = f->linedist*f->factor*f->scale[S];
return m;
}
#line 0 #line 0
#line 1 "v4k_gui.c" #line 1 "v4k_gui.c"

View File

@ -2182,6 +2182,13 @@ enum FONT_FLAGS {
// FONT_DEFAULTS = FONT_512 | FONT_NO_OVERSAMPLE | FONT_ASCII, // FONT_DEFAULTS = FONT_512 | FONT_NO_OVERSAMPLE | FONT_ASCII,
}; };
typedef struct font_metrics_t {
float ascent; // max distance above baseline for all glyphs
float descent; // max distance below baseline for all glyphs
float linegap; // distance betwen ascent of next line and descent of current line
float linedist; // distance between the baseline of two lines (ascent - descent + linegap)
} font_metrics_t;
// configures // configures
API void font_face(const char *face_tag, const char *filename_ttf, float font_size, unsigned flags); API void font_face(const char *face_tag, const char *filename_ttf, float font_size, unsigned flags);
API void font_face_from_mem(const char *tag, const void *ttf_buffer, unsigned ttf_len, float font_size, unsigned flags); API void font_face_from_mem(const char *tag, const void *ttf_buffer, unsigned ttf_len, float font_size, unsigned flags);
@ -2190,10 +2197,11 @@ API void font_color(const char *color_tag, uint32_t color);
API void ui_font(); API void ui_font();
// commands // commands
API vec2 font_xy(); API vec2 font_xy();
API void font_goto(float x, float y); API void font_goto(float x, float y);
API vec2 font_print(const char *text); API vec2 font_print(const char *text);
API vec2 font_rect(const char *text); API vec2 font_rect(const char *text);
API font_metrics_t font_metrics(const char *text);
// void font_clip(vec2 topleft, vec2 bottomright); // void font_clip(vec2 topleft, vec2 bottomright);
// void font_wrap(vec2 topleft, vec2 bottomright); // void font_wrap(vec2 topleft, vec2 bottomright);