prosperon/source/engine/font.c

368 lines
10 KiB
C
Raw 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"
2022-02-06 10:14:57 -06:00
#include "render.h"
2021-11-30 21:29:18 -06:00
#include <ctype.h>
#include <limits.h>
2023-05-12 13:22:05 -05:00
#include <shader.h>
#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>
2023-05-16 01:31:13 -05:00
#include <chipmunk/chipmunk.h>
#include "2dphysics.h"
#include "resources.h"
2022-12-22 16:58:06 -06:00
#include "text.sglsl.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"
2022-01-31 17:04:59 -06:00
struct sFont *font;
2023-05-12 13:22:05 -05:00
#define max_chars 4000
2023-01-25 21:32:58 -06:00
2023-05-12 13:22:05 -05:00
static sg_shader fontshader;
2023-05-04 17:07:00 -05:00
static sg_bindings bind_text;
static sg_pipeline pipe_text;
2023-05-16 01:31:13 -05:00
struct text_vert {
struct draw_p pos;
struct draw_p wh;
2023-05-16 01:31:13 -05:00
struct uv_n uv;
struct uv_n st;
struct rgba color;
};
2023-05-04 17:07:00 -05:00
static struct text_vert *text_buffer;
2022-06-30 13:03:17 -05:00
void font_init() {
text_buffer = malloc(sizeof(*text_buffer)*max_chars);
fontshader = sg_make_shader(text_shader_desc(sg_query_backend()));
2023-05-12 13:22:05 -05:00
pipe_text = sg_make_pipeline(&(sg_pipeline_desc){
2023-05-04 17:07:00 -05:00
.shader = fontshader,
.layout = {
2023-05-12 13:22:05 -05:00
.attrs = {
2023-05-16 01:31:13 -05:00
[0].format = SG_VERTEXFORMAT_FLOAT2, /* verts */
[0].buffer_index = 1,
[1].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[2].format = SG_VERTEXFORMAT_FLOAT2, /* width and height */
[3].format = SG_VERTEXFORMAT_USHORT2N, /* uv pos */
[4].format = SG_VERTEXFORMAT_USHORT2N, /* uv width and height */
[5].format = SG_VERTEXFORMAT_UBYTE4N, /* color */
2023-05-12 13:22:05 -05:00
},
2023-05-16 01:31:13 -05:00
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE
2023-05-04 17:07:00 -05:00
},
2023-05-22 00:08:08 -05:00
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.colors[0].blend = blend_trans,
2023-05-16 01:31:13 -05:00
});
float text_verts[8] = {
0,0,
0,1,
1,0,
1,1
};
bind_text.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(text_verts),
.usage = SG_USAGE_IMMUTABLE
});
2023-05-04 17:07:00 -05:00
2023-05-12 13:22:05 -05:00
bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
2023-05-16 01:31:13 -05:00
.size = sizeof(struct text_vert)*max_chars,
2023-05-04 17:07:00 -05:00
.type = SG_BUFFERTYPE_VERTEXBUFFER,
2023-05-04 22:39:23 -05:00
.usage = SG_USAGE_STREAM,
.label = "text buffer"
});
2023-05-12 13:22:05 -05:00
font = MakeFont("fonts/LessPerfectDOSVGA.ttf", 16);
bind_text.fs.images[0] = font->texID;
bind_text.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
2022-06-26 08:36:38 -05: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) {
YughInfo("Making font %s.", fontfile);
2022-06-30 13:03:17 -05:00
2023-05-12 13:22:05 -05:00
int packsize = 1024;
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
2023-05-12 13:22:05 -05:00
char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile);
unsigned char *ttf_buffer = slurp_file(fontfile, 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
stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char) * packsize);
2021-11-30 21:29:18 -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);
}
2022-06-23 16:15:43 -05:00
2023-05-30 15:41:02 -05:00
stbtt_GetFontVMetrics(&fontinfo, &newfont->ascent, &newfont->descent, &newfont->linegap);
2023-06-05 10:32:45 -05:00
newfont->emscale = stbtt_ScaleForMappingEmToPixels(&fontinfo, 16);
newfont->linegap = (newfont->ascent - newfont->descent)* 2 * newfont->emscale;
2023-05-12 13:22:05 -05:00
newfont->texID = sg_make_image(&(sg_image_desc){
2023-05-04 17:07:00 -05:00
.type = SG_IMAGETYPE_2D,
.width = packsize,
.height = packsize,
.pixel_format = SG_PIXELFORMAT_R8,
.usage = SG_USAGE_IMMUTABLE,
// .min_filter = SG_FILTER_NEAREST,
// .mag_filter = SG_FILTER_NEAREST,
2023-05-04 17:07:00 -05:00
.data.subimage[0][0] = {
2023-05-12 13:22:05 -05:00
.ptr = bitmap,
.size = packsize * packsize}});
free(ttf_buffer);
free(bitmap);
for (unsigned char c = 32; c < 127; c++) {
stbtt_packedchar glyph = glyphs[c - 32];
struct glrect r;
2023-05-24 20:45:50 -05:00
r.s0 = (glyph.x0) / (float)packsize;
r.s1 = (glyph.x1) / (float)packsize;
r.t0 = (glyph.y0) / (float)packsize;
r.t1 = (glyph.y1) / (float)packsize;
2023-05-12 13:22:05 -05:00
2023-05-30 15:41:02 -05:00
stbtt_GetCodepointHMetrics(&fontinfo, c, &newfont->Characters[c].Advance, &newfont->Characters[c].leftbearing);
2023-06-05 10:32:45 -05:00
newfont->Characters[c].Advance *= newfont->emscale;
newfont->Characters[c].leftbearing *= newfont->emscale;
2023-05-30 15:41:02 -05:00
// newfont->Characters[c].Advance = glyph.xadvance; /* x distance from this char to the next */
newfont->Characters[c].Size[0] = glyph.x1 - glyph.x0;
2023-05-12 13:22:05 -05:00
newfont->Characters[c].Size[1] = glyph.y1 - glyph.y0;
newfont->Characters[c].Bearing[0] = glyph.xoff;
newfont->Characters[c].Bearing[1] = glyph.yoff2;
newfont->Characters[c].rect = r;
}
return newfont;
2021-11-30 21:29:18 -06:00
}
2022-12-28 16:50:54 -06:00
static int curchar = 0;
2023-05-25 21:55:55 -05:00
void draw_char_box(struct Character c, cpVect cursor, float scale, struct rgba color)
{
cpVect wh;
wh.x = 8 * scale;
wh.y = 14;
cursor.x += wh.x / 2.f;
cursor.y += wh.y / 2.f;
2023-03-10 13:13:48 -06:00
2023-05-30 11:39:22 -05:00
// draw_box(cursor, wh, color);
2023-03-10 13:13:48 -06:00
}
2023-06-28 11:35:41 -05:00
void text_flush(HMM_Mat4 *proj) {
2023-05-12 13:22:05 -05:00
if (curchar == 0) return;
sg_apply_pipeline(pipe_text);
sg_apply_bindings(&bind_text);
2023-06-28 11:35:41 -05:00
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*proj));
2023-05-12 13:22:05 -05:00
sg_range verts;
verts.ptr = text_buffer;
2023-05-16 01:31:13 -05:00
verts.size = sizeof(struct text_vert) * curchar;
2023-05-12 13:22:05 -05:00
sg_update_buffer(bind_text.vertex_buffers[0], &verts);
2023-05-16 01:31:13 -05:00
sg_draw(0, 4, curchar);
2023-05-06 21:16:10 -05:00
curchar = 0;
}
2023-03-10 13:13:48 -06:00
static int drawcaret = 0;
2023-05-24 20:45:50 -05:00
void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color) {
if (curchar == max_chars)
return;
2023-05-16 01:31:13 -05:00
struct text_vert vert;
2023-05-24 20:45:50 -05:00
float lsize = 1.0 / 1024.0;
2023-06-05 10:32:45 -05:00
float oline = 0.0;
2023-05-16 01:31:13 -05:00
2023-05-30 15:41:02 -05:00
vert.pos.x = cursor.X + c.Bearing[0] * scale + oline;
vert.pos.y = cursor.Y - c.Bearing[1] * scale - oline;
2023-05-24 20:45:50 -05:00
vert.wh.x = c.Size[0] * scale + (oline*2);
vert.wh.y = c.Size[1] * scale + (oline*2);
vert.uv.u = (c.rect.s0 - oline*lsize)*USHRT_MAX;
vert.uv.v = (c.rect.t0 - oline*lsize)*USHRT_MAX;
vert.st.u = (c.rect.s1-c.rect.s0+oline*lsize*2.0)*USHRT_MAX;
vert.st.v = (c.rect.t1-c.rect.t0+oline*lsize*2.0)*USHRT_MAX;
2023-05-16 01:31:13 -05:00
vert.color = color;
memcpy(text_buffer + curchar, &vert, sizeof(struct text_vert));
2023-05-12 13:22:05 -05:00
curchar++;
return;
/*
if (drawcaret == curchar) {
draw_char_box(c, cursor, scale, color);
shader_use(shader);
}
*/
/*
sg_append_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
offset[0] = 1;
offset[1] = -1;
fill_charverts(verts, cursor, scale, c, offset);
sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
offset[1] = 1;
fill_charverts(verts, cursor, scale, c, offset);
sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
offset[0] = -1;
offset[1] = -1;
fill_charverts(verts, cursor, scale, c, offset);
sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts));
*/
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
void text_settype(struct sFont *mfont) {
font = mfont;
2022-01-31 17:04:59 -06:00
}
2023-05-30 15:41:02 -05:00
struct boundingbox text_bb(const char *text, float scale, float lw, float tracking)
2023-05-30 13:07:18 -05:00
{
HMM_Vec2 cursor = {0,0};
const unsigned char *c = text;
const unsigned char *wordstart;
2023-05-30 13:07:18 -05:00
while (*c != '\0') {
if (isblank(*c)) {
2023-05-30 15:41:02 -05:00
cursor.X += font->Characters[*c].Advance * tracking * scale;
2023-05-30 13:07:18 -05:00
c++;
} else if (isspace(*c)) {
2023-05-30 15:41:02 -05:00
cursor.Y -= scale * font->linegap;
2023-05-30 13:07:18 -05:00
cursor.X = 0;
c++;
} else {
wordstart = c;
int wordwidth = 0;
while (!isspace(*c) && *c != '\0') {
2023-05-30 15:41:02 -05:00
wordwidth += font->Characters[*c].Advance * tracking * scale;
2023-05-30 13:07:18 -05:00
c++;
}
if (lw > 0 && (cursor.X + wordwidth) >= lw) {
cursor.X = 0;
2023-05-30 15:41:02 -05:00
cursor.Y -= scale * font->linegap;
2023-05-30 13:07:18 -05:00
}
while (wordstart < c) {
2023-05-30 15:41:02 -05:00
cursor.X += font->Characters[*wordstart].Advance * tracking * scale;
2023-05-30 13:07:18 -05:00
wordstart++;
}
}
}
2023-06-05 10:32:45 -05:00
float height = cursor.Y + (font->height*scale);
2023-05-30 13:07:18 -05:00
float width = lw > 0 ? lw : cursor.X;
2023-06-05 10:32:45 -05:00
struct boundingbox bb = {};
bb.l = 0;
bb.t = font->ascent * font->emscale * scale;
bb.b = font->descent * font->emscale * scale;
bb.r = cursor.X;
return bb;
2023-05-30 13:07:18 -05:00
return cwh2bb((HMM_Vec2){0,0}, (HMM_Vec2){width,height});
}
2023-05-30 15:41:02 -05:00
int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking) {
2023-05-12 13:22:05 -05:00
int len = strlen(text);
drawcaret = caret;
2022-12-28 16:50:54 -06:00
2023-05-24 20:45:50 -05:00
HMM_Vec2 cursor = pos;
2023-05-12 13:22:05 -05:00
const unsigned char *line, *wordstart, *drawstart;
line = drawstart = (unsigned char *)text;
2023-05-16 01:31:13 -05:00
struct rgba usecolor = color;
2023-05-12 13:22:05 -05:00
while (*line != '\0') {
if (isblank(*line)) {
sdrawCharacter(font->Characters[*line], cursor, scale, usecolor);
2023-05-30 15:41:02 -05:00
cursor.X += font->Characters[*line].Advance * tracking * scale;
2023-05-12 13:22:05 -05:00
line++;
} else if (isspace(*line)) {
sdrawCharacter(font->Characters[*line], cursor, scale, usecolor);
2023-05-30 15:41:02 -05:00
cursor.Y -= scale * font->linegap;
2023-05-24 20:45:50 -05:00
cursor.X = pos.X;
2023-05-12 13:22:05 -05:00
line++;
} else {
wordstart = line;
int wordWidth = 0;
while (!isspace(*line) && *line != '\0') {
2023-05-30 15:41:02 -05:00
wordWidth += font->Characters[*line].Advance * tracking * scale;
2023-05-12 13:22:05 -05:00
line++;
}
2023-05-24 20:45:50 -05:00
if (lw > 0 && (cursor.X + wordWidth - pos.X) >= lw) {
cursor.X = pos.X;
2023-05-30 15:41:02 -05:00
cursor.Y -= scale * font->linegap;
2023-05-12 13:22:05 -05:00
}
while (wordstart < line) {
sdrawCharacter(font->Characters[*wordstart], cursor, scale, usecolor);
2023-05-30 15:41:02 -05:00
cursor.X += font->Characters[*wordstart].Advance * tracking * 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
}
/* if (caret > curchar) {
draw_char_box(font->Characters[69], cursor, scale, color);
}
*/
2023-03-17 10:25:35 -05:00
2023-05-24 20:45:50 -05:00
return cursor.Y - pos.Y;
2021-11-30 21:29:18 -06:00
}