Untitled
Loading...
Searching...
No Matches
fonts.c
Go to the documentation of this file.
1
2#include <limits.h>
3#include <stdbool.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include "cJSON/cJSON.h"
7#include "utilities/types.h"
8#include "ui/ui.h"
10#include "ui/fonts.h"
11
12
13
15
16uint32_t TEXT_INDEX_COUNT = 0;
17
18
19
20
22{
23 for (int i = 0; i < MAX_TEXT_BOXES; i++)
24 {
25
26 if (m->text_boxes[i].free) return i;
27
28 }
29 return -1;
30}
31
32
33Font * read_json_msdf(const char * json_path, const char * png_path) {
34
35 FILE *f = fopen(json_path, "rb");
36 fseek(f, 0, SEEK_END);
37 long len = ftell(f);
38 rewind(f);
39
40 char *json_data = malloc(len + 1);
41 fread(json_data, 1, len, f);
42 json_data[len] = '\0';
43 fclose(f);
44
45 cJSON *root = cJSON_Parse(json_data);
46 free(json_data);
47 if (!root) {
48 fprintf(stderr, "Failed to parse font json\n");
49 return NULL;
50 }
51
52 cJSON *common = cJSON_GetObjectItem(root, "common");
53 int tex_w = cJSON_GetObjectItem(common, "scaleW")->valueint;
54 int tex_h = cJSON_GetObjectItem(common, "scaleH")->valueint;
55
56 Font * font = vector_expand(&FONTS, 1);
57 font->tex_w = tex_w;
58 font->tex_h = tex_h;
59 font->id = FONTS.count-1;
60 strncpy(font->name, png_path, 255);
61 font->name[255] = '\0';
62 cJSON *chars = cJSON_GetObjectItem(root, "chars");
63 int count = cJSON_GetArraySize(chars);
64 font->glyphs = malloc(sizeof(Glyph) * count);
65 int highest_y = INT_MAX;
66 int highest_dist = 1;
67
68 for (int i = 0; i < count; i++) {
69
70 cJSON * ch = cJSON_GetArrayItem(chars, i);
71 Glyph * g = &font->glyphs[i];
72
73 g->id = cJSON_GetObjectItem(ch, "id")->valueint;
74 int x = cJSON_GetObjectItem(ch, "x")->valueint;
75 int y = cJSON_GetObjectItem(ch, "y")->valueint;
76 int width = cJSON_GetObjectItem(ch, "width")->valueint;
77 int height = cJSON_GetObjectItem(ch, "height")->valueint;
78 g->xoffset = cJSON_GetObjectItem(ch, "xoffset")->valueint;
79 g->yoffset = cJSON_GetObjectItem(ch, "yoffset")->valueint;
80 g->xadvance = cJSON_GetObjectItem(ch, "xadvance")->valueint;
81
82 g->width = width;
83 g->height = height;
84 if (g->yoffset < highest_y && g->height > 0) highest_y = g->yoffset;
85 if (g->yoffset + g->height > font->highest_dist) font->highest_dist = g->yoffset + g->height;
86 g->u0 = (float)x / tex_w;
87 g->v0 = (float)y / tex_h;
88 g->u1 = (float)(x + width) / tex_w;
89 g->v1 = (float)(y + height) / tex_h;
90 }
91 font->highest_y = highest_y;
92 font->glyph_count = count;
93 cJSON_Delete(root);
94 return font;
95}
96
97void determine_text_box_dimensions(TextBox * tb, float * width, float * height){
98
99 int len = strlen(tb->text);
100
101 if (len <= 0) return;
102
103 float x = tb->start.x, y = tb->start.y;
104 Font * fonts = CAST_VEC(FONTS, Font);
105 Font * f = &fonts[tb->font_id];
106 int tex_w = f->tex_w;
107 int tex_h = f->tex_h;
108 float scale = tb->text_size;
109
110 float max_x = 0, max_y = 0;
111 Glyph * glyphs = f->glyphs;
112
113 for (int i = 0; i < len; i++){
114 char ch = tb->text[i];
115
116 Glyph *g = NULL;
117 for (int j = 0; j < f->glyph_count; j++) {
118 if (glyphs[j].id == ch) {
119 g = &glyphs[j];
120 break;
121 }
122 }
123 if (!g) continue;
124
125 float x0 = x + g->xoffset * scale;
126 float y0 = (y - 7 * scale) + g->yoffset * scale;
127 float x1 = x0 + g->width * scale;
128 float y1 = y0 + g->height * scale;
129
130 float u0 = g->u0;
131 float v0 = g->v0;
132 float u1 = g->u1;
133 float v1 = g->v1;
134 x += g->xadvance * scale;
135 if (x > max_x) max_x = x;
136 if (y1 > max_y) max_y = y1;
137 if (x > tb->start.x + tb->w) {
138 y += f->highest_dist * scale;
139 x = tb->start.x;
140 }
141 }
142 if (width) *width = max_x - tb->start.x;
143 if (height) *height = max_y - tb->start.y;
144
145 float dx = (max_x - tb->start.x) / 2 + tb->start.x;
146 float dy = fabsf(max_y - tb->start.y) / 2 + tb->start.y;
147}
148
149
151
152 float w, h;
154 switch(box->justify)
155 {
156 case JUSTIFY_CENTER:
157 box->start.x = -w/2;
158 box->start.y = -h/2;
159 box->center = (vec2){0,0};
160 box->w = w;
161 box->h = h;
162 break;
163 case JUSTIFY_LEFT:
164 box->start.x = 0;
165 box->start.y = 0;
166 box->center = (vec2){w/2,h/2};
167 box->w = w;
168 box->h = h;
169 break;
170 case JUSTIFY_RIGHT:
171 box->start.x = -w;
172 box->start.y = -h;
173 box->center = (vec2){-w/2, -h/2};
174 box->w = w;
175 box->h = h;
176 break;
177 default:
178 break;
179 }
180
181}
182
183
184void delete_text_box(int menu, int id)
185{
186
187 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
188 UIMenu * m = &ms[menu];
189
190 TextBox * tb = &m->text_boxes[id];
191
192 if (tb->parent_container != -1)
193 {
195 }
196 if (tb->text) free(tb->text);
197 tb->text = NULL;
198 *tb = DEFAULT_TEXT_BOX;
199 tb->free = true;
200}
201
202int create_text_box(int menu_id)
203{
204
205 UIMenu * ms = CAST_VEC(MENUS, UIMenu);
206
207 UIMenu * m = &ms[menu_id];
208
209 int empty = find_empty_text_box_slot(m);
210 if (empty == -1)
211 {
212 printf("No empty text box slot found.\n");
213 return -1;
214 }
215 printf("TB: %d %d %d\n", empty, m->uID, menu_id);
216
217 TextBox * tb = &m->text_boxes[empty];
218
219 *tb = DEFAULT_TEXT_BOX;
220 tb->text = malloc(10 * sizeof(char));
221 tb->text_cap = 10;
222
223 sprintf(tb->text, "test");
224
225
226 tb->uID = empty;
227 tb->menu = menu_id;
228
230
231 return tb->uID;
232
233}
234
Font * read_json_msdf(const char *json_path, const char *png_path)
Reads the JSON for an MSDF Font, and creates a Font from it.
Definition fonts.c:33
int create_text_box(int menu_id)
Creates a DEFAULT_TEXT_BOX.
Definition fonts.c:202
Vector FONTS
All loaded Fonts.
Definition fonts.c:14
int find_empty_text_box_slot(UIMenu *m)
Finds an empty slot for a TextBox that is marked as free.
Definition fonts.c:21
void delete_text_box(int menu, int id)
Deletes a TextBox and its text and frees it from the UIMenu.
Definition fonts.c:184
void determine_text_box_dimensions(TextBox *tb, float *width, float *height)
Determines TextBox dimensions by iterating through its text as if it was rendering the Font.
Definition fonts.c:97
void move_text_box_to_origin(TextBox *box)
Moves a TextBox to , while accounting for Justify.
Definition fonts.c:150
Functions that modify TextBoxes and load Fonts.
float h
The height of the box after pseudo-rendering in determine_text_box_dimensions().
Definition ui.h:269
int id
The ID of the Font in FONTS.
Definition ui.h:76
float v0
.
Definition ui.h:56
float w
The width of the box after pseudo-rendering in determine_text_box_dimensions().
Definition ui.h:268
int xadvance
How much the x advances when it is typed.
Definition ui.h:63
int font_id
The Font ID inside of FONTS.
Definition ui.h:262
float v1
.
Definition ui.h:58
TextBox text_boxes[1024]
The TextBoxes.
Definition ui.h:567
int tex_w
The texture dimensions.
Definition ui.h:80
char name[256]
The name of the file path.
Definition ui.h:84
vec2 start
The start of the text in relation to .
Definition ui.h:284
Glyph * glyphs
Glyphs for each character.
Definition ui.h:78
int text_cap
The amount of characters allocated.
Definition ui.h:266
uint32_t uID
The index inside of MENUS.
Definition ui.h:564
vec2 center
The center in relation to .
Definition ui.h:283
Justify justify
How the text is centered in relation to and/or its UILabel.
Definition ui.h:264
int height
.
Definition ui.h:60
UIContainer containers[1024]
The UIContainers.
Definition ui.h:568
int xoffset
The x offset before the character.
Definition ui.h:61
int id
This ID == '<char>' when looking up which Glyph to use (ascii).
Definition ui.h:54
int parent_container
The UIContainer that contains this TextBox.
Definition ui.h:290
int yoffset
The y offset before the character.
Definition ui.h:62
int width
.
Definition ui.h:59
uint32_t uID
The ID inside of UIMenu.text_boxes.
Definition ui.h:263
char * text
The characters to render.
Definition ui.h:265
size_t glyph_count
The amount of Glyphs.
Definition ui.h:79
bool free
If this TextBox is empty and can be overwritten.
Definition ui.h:261
int highest_dist
Used to determine the amount of space to cursor shift for newlines.
Definition ui.h:83
int menu
The UIMenu that this box is within.
Definition ui.h:289
float u0
.
Definition ui.h:55
int tex_h
The texture dimensions.
Definition ui.h:81
float text_size
The size to render the Font.
Definition ui.h:267
int highest_y
Used to determine the amount of space to cursor shift for newlines.
Definition ui.h:82
float u1
.
Definition ui.h:57
#define MAX_TEXT_BOXES
Definition ui.h:37
TextBox DEFAULT_TEXT_BOX
Definition ui.h:661
Vector MENUS
All UIMenus.
Definition ui.c:13
void remove_text_box_from_container(UIContainer *c, TextBox *tb)
Removes a TextBox from a UIContainer.
Definition ui.c:157
@ JUSTIFY_RIGHT
Definition ui.h:156
@ JUSTIFY_LEFT
Definition ui.h:155
@ JUSTIFY_CENTER
Definition ui.h:154
A Font and its Glyphs used for rendering TextBoxes.
Definition ui.h:74
A single Glyph in a Font.
Definition ui.h:52
A structure holding a Font ID and text to be rendered and transformed with its properties.
Definition text.vert:5
The top-most UI structure that contains UIElements, TextBoxes, and UIContainers.
Definition ui.h:562
uint32_t TEXT_INDEX_COUNT
Definition fonts.c:16
float x
Definition types.h:11
float y
Definition types.h:11
size_t count
Definition types.h:209
Definition types.h:11
The standalone UI library that controls text rendering, ui rendering, and events.
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
#define CAST_VEC(vec, type)
Definition utilities.h:134