6#include "cJSON/cJSON.h"
35 FILE *f = fopen(json_path,
"rb");
36 fseek(f, 0, SEEK_END);
40 char *json_data = malloc(len + 1);
41 fread(json_data, 1, len, f);
42 json_data[len] =
'\0';
45 cJSON *root = cJSON_Parse(json_data);
48 fprintf(stderr,
"Failed to parse font json\n");
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;
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);
65 int highest_y = INT_MAX;
68 for (
int i = 0; i < count; i++) {
70 cJSON * ch = cJSON_GetArrayItem(chars, i);
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;
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;
99 int len = strlen(tb->
text);
101 if (len <= 0)
return;
106 int tex_w = f->
tex_w;
107 int tex_h = f->
tex_h;
110 float max_x = 0, max_y = 0;
113 for (
int i = 0; i < len; i++){
114 char ch = tb->
text[i];
118 if (glyphs[j].
id == ch) {
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;
135 if (x > max_x) max_x = x;
136 if (y1 > max_y) max_y = y1;
142 if (width) *width = max_x - tb->
start.
x;
143 if (height) *height = max_y - tb->
start.
y;
146 float dy = fabsf(max_y - tb->
start.
y) / 2 + tb->
start.
y;
207 UIMenu * m = &ms[menu_id];
212 printf(
"No empty text box slot found.\n");
215 printf(
"TB: %d %d %d\n", empty, m->
uID, menu_id);
220 tb->
text = malloc(10 *
sizeof(
char));
223 sprintf(tb->
text,
"test");
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.
int create_text_box(int menu_id)
Creates a DEFAULT_TEXT_BOX.
Vector FONTS
All loaded Fonts.
int find_empty_text_box_slot(UIMenu *m)
Finds an empty slot for a TextBox that is marked as free.
void delete_text_box(int menu, int id)
Deletes a TextBox and its text and frees it from the UIMenu.
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.
void move_text_box_to_origin(TextBox *box)
Moves a TextBox to , while accounting for Justify.
Functions that modify TextBoxes and load Fonts.
float h
The height of the box after pseudo-rendering in determine_text_box_dimensions().
int id
The ID of the Font in FONTS.
float w
The width of the box after pseudo-rendering in determine_text_box_dimensions().
int xadvance
How much the x advances when it is typed.
int font_id
The Font ID inside of FONTS.
TextBox text_boxes[1024]
The TextBoxes.
int tex_w
The texture dimensions.
char name[256]
The name of the file path.
vec2 start
The start of the text in relation to .
Glyph * glyphs
Glyphs for each character.
int text_cap
The amount of characters allocated.
uint32_t uID
The index inside of MENUS.
vec2 center
The center in relation to .
Justify justify
How the text is centered in relation to and/or its UILabel.
UIContainer containers[1024]
The UIContainers.
int xoffset
The x offset before the character.
int id
This ID == '<char>' when looking up which Glyph to use (ascii).
int parent_container
The UIContainer that contains this TextBox.
int yoffset
The y offset before the character.
uint32_t uID
The ID inside of UIMenu.text_boxes.
char * text
The characters to render.
size_t glyph_count
The amount of Glyphs.
bool free
If this TextBox is empty and can be overwritten.
int highest_dist
Used to determine the amount of space to cursor shift for newlines.
int menu
The UIMenu that this box is within.
int tex_h
The texture dimensions.
float text_size
The size to render the Font.
int highest_y
Used to determine the amount of space to cursor shift for newlines.
void remove_text_box_from_container(UIContainer *c, TextBox *tb)
Removes a TextBox from a UIContainer.
A Font and its Glyphs used for rendering TextBoxes.
A single Glyph in a Font.
A structure holding a Font ID and text to be rendered and transformed with its properties.
uint32_t TEXT_INDEX_COUNT
The standalone UI library that controls text rendering, ui rendering, and events.
void * vector_expand(Vector *v, size_t i)
#define CAST_VEC(vec, type)