prosperon/source/engine/font.c

307 lines
7.7 KiB
C
Raw Permalink Normal View History

2021-11-30 21:29:18 -06:00
#include "font.h"
2023-05-12 13:22:05 -05:00
#include "log.h"
2021-11-30 21:29:18 -06:00
#include <ctype.h>
2023-12-22 07:14:44 -06:00
#include "log.h"
2021-11-30 21:29:18 -06:00
#include <limits.h>
2023-05-12 13:22:05 -05:00
#include <stdio.h>
2022-06-21 15:21:00 -05:00
#include <stdlib.h>
2023-05-12 13:22:05 -05:00
#include <string.h>
2022-06-26 08:36:38 -05:00
#include <window.h>
#include "resources.h"
#include "render.h"
2022-12-29 04:26:21 -06:00
#include "stb_image_write.h"
2023-05-12 13:22:05 -05:00
#include "stb_rect_pack.h"
#include "stb_truetype.h"
2021-11-30 21:29:18 -06:00
2023-05-24 20:45:50 -05:00
#include "HandmadeMath.h"
2024-04-09 16:48:15 -05:00
struct sFont *use_font;
2023-05-12 13:22:05 -05:00
2023-05-16 01:31:13 -05:00
struct text_vert {
2024-05-12 18:36:14 -05:00
HMM_Vec2 pos;
HMM_Vec2 wh;
HMM_Vec2 uv;
HMM_Vec2 st;
HMM_Vec4 color;
2023-05-16 01:31:13 -05:00
};
2023-05-04 17:07:00 -05:00
static struct text_vert *text_buffer;
2022-06-30 13:03:17 -05:00
2024-04-09 16:48:15 -05:00
void font_free(font *f)
2024-03-02 02:59:50 -06:00
{
2024-04-09 16:48:15 -05:00
sg_destroy_image(f->texID);
free(f);
}
2024-03-02 02:59:50 -06:00
2023-05-24 20:45:50 -05:00
struct sFont *MakeSDFFont(const char *fontfile, int height)
{
YughInfo("Making sdf font %s.", fontfile);
int packsize = 1024;
struct sFont *newfont = calloc(1, sizeof(struct sFont));
newfont->height = height;
char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile);
unsigned char *ttf_buffer = slurp_file(fontpath, NULL);
2023-05-24 20:45:50 -05:00
unsigned char *bitmap = malloc(packsize * packsize);
stbtt_fontinfo fontinfo;
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0))) {
YughError("Failed to make font %s", fontfile);
}
for (int i = 32; i < 95; i++) {
int w, h, xoff, yoff;
// unsigned char *stbtt_GetGlyphSDF(&fontinfo, height, i, 1, 0, 1, &w, &h, &xoff, &yoff);
}
return newfont;
2023-05-24 20:45:50 -05:00
}
2023-05-12 13:22:05 -05:00
struct sFont *MakeFont(const char *fontfile, int height) {
2024-07-09 01:03:39 -05:00
int packsize = 2048;
2022-12-29 04:26:21 -06:00
2023-05-12 13:22:05 -05:00
struct sFont *newfont = calloc(1, sizeof(struct sFont));
newfont->height = height;
2021-11-30 21:29:18 -06:00
unsigned char *ttf_buffer = slurp_file(fontfile, NULL);
2024-04-09 16:48:15 -05:00
if (!ttf_buffer) {
2024-10-13 19:44:28 -05:00
YughWarn("Could not find font at %s.", fontfile);
2024-04-09 16:48:15 -05:00
return NULL;
}
2023-05-12 13:22:05 -05:00
unsigned char *bitmap = malloc(packsize * packsize);
2022-12-28 16:50:54 -06:00
2023-05-12 13:22:05 -05:00
stbtt_packedchar glyphs[95];
2022-12-28 16:50:54 -06:00
2023-05-12 13:22:05 -05:00
stbtt_pack_context pc;
2022-12-28 16:50:54 -06:00
2023-05-24 20:45:50 -05:00
int pad = 2;
stbtt_PackBegin(&pc, bitmap, packsize, packsize, 0, pad, NULL);
2023-05-12 13:22:05 -05:00
stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs);
stbtt_PackEnd(&pc);
2022-12-28 16:50:54 -06:00
2023-05-12 13:22:05 -05:00
stbtt_fontinfo fontinfo;
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0))) {
YughError("Failed to make font %s", fontfile);
}
2024-07-09 01:03:39 -05:00
int ascent, descent, linegap;
2022-06-23 16:15:43 -05:00
2024-07-09 01:03:39 -05:00
stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap);
2024-10-14 20:07:32 -05:00
float emscale = stbtt_ScaleForMappingEmToPixels(&fontinfo, height);
2024-07-09 01:03:39 -05:00
newfont->ascent = ascent*emscale;
newfont->descent = descent*emscale;
newfont->linegap = linegap*emscale;
2024-10-15 17:15:50 -05:00
2024-05-12 18:36:14 -05:00
newfont->texture = malloc(sizeof(texture));
newfont->texture->id = sg_make_image(&(sg_image_desc){
2024-04-09 16:48:15 -05:00
.type = SG_IMAGETYPE_2D,
.width = packsize,
.height = packsize,
.pixel_format = SG_PIXELFORMAT_R8,
.usage = SG_USAGE_IMMUTABLE,
.data.subimage[0][0] = {
.ptr = bitmap,
.size = packsize * packsize
}
});
2024-05-12 18:36:14 -05:00
newfont->texture->width = packsize;
newfont->texture->height = packsize;
2023-05-12 13:22:05 -05:00
for (unsigned char c = 32; c < 127; c++) {
stbtt_packedchar glyph = glyphs[c - 32];
struct rect r;
r.x = (glyph.x0) / (float)packsize;
r.w = (glyph.x1-glyph.x0) / (float)packsize;
2024-10-17 17:23:33 -05:00
r.y = (glyph.y1) / (float)packsize;
r.h = (glyph.y0-glyph.y1) / (float)packsize;
2023-05-12 13:22:05 -05:00
2024-10-14 20:07:32 -05:00
newfont->Characters[c].size = (HMM_Vec2){
.x = glyph.x1-glyph.x0,
.y = glyph.y1-glyph.y0
};
newfont->Characters[c].Advance = glyph.xadvance; /* x distance from this char to the next */
2024-10-15 17:15:50 -05:00
newfont->Characters[c].leftbearing = glyph.xoff;
// printf("char %c: ascent %g, yoff %g, yoff2 %g\n", c, newfont->ascent, glyph.yoff, glyph.yoff2);
2024-10-17 17:23:33 -05:00
newfont->Characters[c].topbearing = -glyph.yoff2;//newfont->ascent - glyph.yoff;
2023-05-12 13:22:05 -05:00
newfont->Characters[c].rect = r;
}
free(ttf_buffer);
free(bitmap);
2023-05-12 13:22:05 -05:00
return newfont;
2021-11-30 21:29:18 -06:00
}
2024-07-18 17:09:35 -05:00
int text_flush(sg_buffer *buf) {
2024-05-12 18:36:14 -05:00
if (arrlen(text_buffer) == 0) return 0;
2024-04-23 15:58:08 -05:00
2023-05-12 13:22:05 -05:00
sg_range verts;
verts.ptr = text_buffer;
2024-04-23 15:58:08 -05:00
verts.size = sizeof(struct text_vert) * arrlen(text_buffer);
2024-07-18 17:09:35 -05:00
if (sg_query_buffer_will_overflow(*buf, verts.size)) {
sg_destroy_buffer(*buf);
*buf = sg_make_buffer(&(sg_buffer_desc){
2024-04-23 15:58:08 -05:00
.size = verts.size,
2024-05-12 18:36:14 -05:00
.type = SG_BUFFERTYPE_STORAGEBUFFER,
2024-04-23 15:58:08 -05:00
.usage = SG_USAGE_STREAM,
.label = "text buffer"
});
2024-05-12 18:36:14 -05:00
}
2024-04-23 15:58:08 -05:00
2024-07-18 17:09:35 -05:00
sg_append_buffer(*buf, &verts);
2024-05-12 18:36:14 -05:00
int n = arrlen(text_buffer);
2024-04-23 15:58:08 -05:00
arrsetlen(text_buffer, 0);
2024-05-12 18:36:14 -05:00
return n;
2023-05-06 21:16:10 -05:00
}
2023-10-04 17:57:37 -05:00
void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color) {
2023-05-16 01:31:13 -05:00
struct text_vert vert;
2023-05-24 20:45:50 -05:00
2024-10-15 17:15:50 -05:00
vert.pos.x = cursor.X + c.leftbearing;
vert.pos.y = cursor.Y + c.topbearing;
2024-10-14 20:07:32 -05:00
vert.wh = c.size;
2023-10-04 08:18:09 -05:00
// if (vert.pos.x > frame.l || vert.pos.y > frame.t || (vert.pos.y + vert.wh.y) < frame.b || (vert.pos.x + vert.wh.x) < frame.l) return;
2024-05-12 18:36:14 -05:00
vert.uv.x = c.rect.x;
vert.uv.y = c.rect.y;
vert.st.x = c.rect.w;
vert.st.y = c.rect.h;
rgba2floats(vert.color.e, color);
2023-05-16 01:31:13 -05:00
2024-04-23 15:58:08 -05:00
arrput(text_buffer, vert);
2021-11-30 21:29:18 -06:00
}
const char *esc_color(const char *c, struct rgba *color, struct rgba defc)
2023-05-30 13:07:18 -05:00
{
2023-10-09 13:03:12 -05:00
struct rgba d;
if (!color) color = &d;
2023-10-09 18:10:10 -05:00
if (*c != '\e') return c;
2023-10-09 13:03:12 -05:00
c++;
if (*c != '[') return c;
c++;
if (*c == '0') {
*color = defc;
c++;
return c;
}
else if (!strncmp(c, "38;2;", 5)) {
c += 5;
*color = (struct rgba){0,0,0,255};
color->r = atoi(c);
c = strchr(c, ';')+1;
color->g = atoi(c);
c = strchr(c,';')+1;
color->b = atoi(c);
c = strchr(c,';')+1;
return c;
}
return c;
}
2024-10-14 20:07:32 -05:00
// text is a string, font f, size is height in pixels, wrap is how long a line is before wrapping. -1to not wrap
HMM_Vec2 measure_text(const char *text, font *f, float size, float letterSpacing, float wrap)
2023-10-09 13:03:12 -05:00
{
2024-10-14 20:07:32 -05:00
HMM_Vec2 dim = {0};
float maxWidth = 0; // max width of any line
float lineWidth = 0; // current line width
float scale = size/f->height;
2024-10-17 17:23:33 -05:00
scale = 1;
2024-10-14 20:07:32 -05:00
float lineHeight = f->ascent - f->descent;
lineHeight *= scale;
letterSpacing *= scale;
float height = lineHeight; // total height
for (char *c = text; *c != 0; c++) {
if (*c == '\n') {
maxWidth = fmaxf(maxWidth, lineWidth);
lineWidth = 0;
height += lineHeight + f->linegap;
continue;
2023-05-30 13:07:18 -05:00
}
2024-10-14 20:07:32 -05:00
lineWidth += f->Characters[*c].Advance + letterSpacing;
2023-05-30 13:07:18 -05:00
}
2023-10-04 17:57:37 -05:00
2024-10-14 20:07:32 -05:00
maxWidth = fmaxf(maxWidth, lineWidth);
dim.x = maxWidth;
dim.y = height;
return dim;
}
/* pos given in screen coordinates */
2024-10-14 20:07:32 -05:00
void renderText(const char *text, HMM_Vec2 pos, font *f, float scale, struct rgba color, float wrap) {
2023-05-12 13:22:05 -05:00
int len = strlen(text);
2022-12-28 16:50:54 -06:00
2023-05-24 20:45:50 -05:00
HMM_Vec2 cursor = pos;
2024-10-14 20:07:32 -05:00
float lineHeight = f->ascent - f->descent;
2024-10-17 17:23:33 -05:00
float lineWidth = 0;
2024-10-14 20:07:32 -05:00
for (char *c = text; *c != 0; c++) {
if (*c == '\n') {
cursor.x = pos.x;
cursor.y += lineHeight + f->linegap;
continue;
}
sdrawCharacter(f->Characters[*c], cursor, scale, color);
cursor.x += f->Characters[*c].Advance;
}
return;
2023-05-12 13:22:05 -05:00
const char *line, *wordstart, *drawstart;
line = drawstart = text;
2023-05-12 13:22:05 -05:00
2023-05-16 01:31:13 -05:00
struct rgba usecolor = color;
2023-10-09 13:03:12 -05:00
while (*line != '\0') {
if (isblank(*line)) {
2024-10-14 20:07:32 -05:00
sdrawCharacter(f->Characters[*line], cursor, scale, usecolor);
cursor.X += f->Characters[*line].Advance * scale;
2023-10-09 13:03:12 -05:00
line++;
} else if (isspace(*line)) {
2024-10-14 20:07:32 -05:00
sdrawCharacter(f->Characters[*line], cursor, scale, usecolor);
cursor.Y -= scale * f->linegap;
2023-05-24 20:45:50 -05:00
cursor.X = pos.X;
2023-10-09 13:03:12 -05:00
line++;
2023-05-12 13:22:05 -05:00
} else {
2023-10-09 13:03:12 -05:00
if (*line == '\e')
line = esc_color(line, &usecolor, color);
wordstart = line;
2023-05-12 13:22:05 -05:00
int wordWidth = 0;
2023-10-09 13:03:12 -05:00
while (!isspace(*line) && *line != '\0') {
2024-10-14 20:07:32 -05:00
wordWidth += f->Characters[*line].Advance * scale;
2023-10-09 13:03:12 -05:00
line++;
2023-05-12 13:22:05 -05:00
}
2024-10-14 20:07:32 -05:00
if (wrap > 0 && (cursor.X + wordWidth - pos.X) >= wrap) {
2023-05-24 20:45:50 -05:00
cursor.X = pos.X;
2024-10-14 20:07:32 -05:00
cursor.Y -= scale * f->linegap;
2023-05-12 13:22:05 -05:00
}
2023-10-09 13:03:12 -05:00
while (wordstart < line) {
2024-04-04 17:28:11 -05:00
if (*wordstart == '\e')
wordstart = esc_color(wordstart, &usecolor, color);
2024-10-14 20:07:32 -05:00
sdrawCharacter(f->Characters[*wordstart], cursor, scale, usecolor);
2024-10-14 20:07:32 -05:00
cursor.X += f->Characters[*wordstart].Advance * scale;
2023-05-12 13:22:05 -05:00
wordstart++;
}
2023-03-17 10:25:35 -05:00
}
2023-05-12 13:22:05 -05:00
}
2021-11-30 21:29:18 -06:00
}