From e44f7fb078b639919af8126741720d4e0eb66656 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Tue, 21 Nov 2023 23:31:32 +0200 Subject: [PATCH] better text positioning which fixes rotations and scaling --- cudoku.c | 10 +++++----- todo.txt | 2 -- ui.c | 38 +++++++++++++++++--------------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/cudoku.c b/cudoku.c index b39da88..b3f9660 100644 --- a/cudoku.c +++ b/cudoku.c @@ -58,15 +58,15 @@ void draw_win() { /* set_width_constraint(&constraints, window.width, UI_CONSTRAINT_FIXED); */ /* set_height_constraint(&constraints, window.height, UI_CONSTRAINT_FIXED); */ - Alignment align = ALIGN_CENTER; + Alignment align = ALIGN_TOP_RIGHT; int font_size = 72.0f; Sizef text_size = calculate_text_size(win_text, font_size); - set_x_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); + set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); set_y_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); - set_width_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); + set_width_constraint(&constraints, text_size.width, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, text_size.height, UI_CONSTRAINT_FIXED); UIConstraints circle_const; set_x_constraint(&circle_const, 50, UI_CONSTRAINT_FIXED); @@ -75,7 +75,7 @@ void draw_win() { set_height_constraint(&circle_const, 90, UI_CONSTRAINT_FIXED); /* draw_circle(circle_const, NULL, ALIGN_LEFT_CENTER); */ - /* draw_quad(constraints, &bg_color, 0.0, ALIGN_TOP_LEFT); */ + draw_quad(constraints, &bg_color, 0.0, align); /* set_x_constraint(&constraints, -50, UI_CONSTRAINT_FIXED); */ /* draw_quad(constraints, &bg_color, 25.0, ALIGN_TOP_RIGHT); */ /* set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); */ diff --git a/todo.txt b/todo.txt index 2b9c233..d40b962 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,3 @@ -- rotations/scaling for text (fix the code for it) - - maybe force the window to be a specific resolution - border decorations for shapes - look into SDF/vector text diff --git a/ui.c b/ui.c index 3655384..b06b05c 100644 --- a/ui.c +++ b/ui.c @@ -469,15 +469,21 @@ void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignme set_vec4f(font_shader, "textColor", 0.f, 0.f, 0.f, 1.f); } set_mat4f(font_shader, "projection", (float *)zephr_context.projection.m); + + Sizef text_size = calculate_text_size(text, FONT_PIXEL_SIZE); + float font_scale = (float)font_size / FONT_PIXEL_SIZE; + + apply_alignment(alignment, &pos, (Sizef){ text_size.width * font_scale, text_size.height * font_scale }); + Matrix4x4 model = identity(); + model.m[0][0] = font_scale; + model.m[1][1] = font_scale; + apply_rotation(&model, to_radians(0)); model.m[3][0] = pos.x; model.m[3][1] = pos.y; set_mat4f(font_shader, "model", (float *)model.m); - Sizef text_size = calculate_text_size(text, FONT_PIXEL_SIZE); - float font_scale = (float)font_size / FONT_PIXEL_SIZE; - int max_bearing_h = 0; for (uint i = 0; i < strlen(text); i++) { Character ch = characters[(int)text[i]]; @@ -493,29 +499,19 @@ void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignme int x = 0; while (text[c] != '\0') { Character ch = characters[(int)text[c]]; - // subtract the bearing width of the first character to remove the extra space // at the start of the text and move every char to the left by that width - float xpos = (x + (ch.bearing.width - first_char_bearing_w)) * font_scale; - - float ypos = (text_size.height - ch.bearing.height - (text_size.height - max_bearing_h)) * font_scale; - Vec2f final_pos = { xpos, ypos }; - - float w = (ch.size.width * font_scale); - float h = (ch.size.height * font_scale); - - apply_alignment(alignment, &final_pos, (Sizef){ text_size.width * font_scale, text_size.height * font_scale }); + float xpos = (x + (ch.bearing.width - first_char_bearing_w)); + float ypos = (text_size.height - ch.bearing.height - (text_size.height - max_bearing_h)); float vertices[6][4] = { - // bottom left tri - {final_pos.x, final_pos.y + h, 0.0, 1.0}, - {final_pos.x, final_pos.y, 0.0, 0.0}, - {final_pos.x + w, final_pos.y, 1.0, 0.0}, + {xpos, ypos + ch.size.height, 0.0, 1.0}, + {xpos, ypos, 0.0, 0.0}, + {xpos + ch.size.width, ypos, 1.0, 0.0}, - // top right tri - {final_pos.x, final_pos.y + h, 0.0, 1.0}, - {final_pos.x + w, final_pos.y, 1.0, 0.0}, - {final_pos.x + w, final_pos.y + h, 1.0, 1.0}, + {xpos, ypos + ch.size.height, 0.0, 1.0}, + {xpos + ch.size.width, ypos, 1.0, 0.0}, + {xpos + ch.size.width, ypos + ch.size.height, 1.0, 1.0}, }; glBindTexture(GL_TEXTURE_2D, ch.texture_id);