ugui.c3l/src/font.c3

228 lines
5.7 KiB
Plaintext
Raw Normal View History

module ugui::font;
2024-12-06 22:03:35 +01:00
import schrift;
2024-12-11 01:14:14 +01:00
import std::collections::map;
import std::core::mem;
2025-05-05 16:23:26 +02:00
import std::core::mem::allocator;
2024-12-11 20:39:06 +01:00
import std::io;
2024-12-15 22:29:07 +01:00
import std::ascii;
2024-12-06 22:03:35 +01:00
// ---------------------------------------------------------------------------------- //
// CODEPOINT //
// ---------------------------------------------------------------------------------- //
2024-12-11 01:14:14 +01:00
// unicode code point, different type for a different hash
2025-05-05 16:23:26 +02:00
alias Codepoint = uint;
2024-12-11 01:14:14 +01:00
2025-05-05 16:23:26 +02:00
//macro uint Codepoint.hash(self) => ((uint)self).hash();
2024-12-11 01:14:14 +01:00
// ---------------------------------------------------------------------------------- //
// FONT ATLAS //
// ---------------------------------------------------------------------------------- //
2024-12-11 01:14:14 +01:00
/* width and height of a glyph contain the kering advance
* (u,v)
* +-------------*---+ -
* | ^ | | ^
* | |oy | | |
* | v | | |
* | .ii. | | |
2024-12-11 20:39:06 +01:00
* | @@@@@@. | | |
* | V@Mio@@o | | |
* | :i. V@V | | h
2024-12-11 01:14:14 +01:00
* | :oM@@M | | |
* | :@@@MM@M | | |
* | @@o o@M | | |
* |<->:@@. M@M | | |
* |ox @@@o@@@@ | | |
* | :M@@V:@@.| | v
* +-------------*---+ -
2024-12-11 20:39:06 +01:00
* |<---- w ---->|
* |<------ adv ---->|
2024-12-11 01:14:14 +01:00
*/
struct Glyph {
Codepoint code;
ushort u, v;
ushort w, h;
short adv, ox, oy;
}
2025-02-08 12:51:10 +01:00
const uint FONT_CACHED = 255;
2025-05-05 16:23:26 +02:00
alias GlyphTable = map::HashMap{Codepoint, Glyph};
2024-12-11 01:14:14 +01:00
2025-05-05 16:23:26 +02:00
faultdef TTF_LOAD_FAILED, MISSING_GLYPH, BAD_GLYPH_METRICS, RENDER_ERROR;
2024-12-11 01:14:14 +01:00
struct Font {
2024-12-06 22:03:35 +01:00
schrift::Sft sft;
String path;
2024-12-15 21:39:26 +01:00
Id id; // font id, same as atlas id
2024-12-11 01:14:14 +01:00
GlyphTable table;
float size;
float ascender, descender, linegap; // Line Metrics
2024-12-15 21:39:26 +01:00
Atlas atlas;
bool should_update; // should send update_atlas command, resets at frame_end()
2024-12-11 01:14:14 +01:00
}
2025-10-16 17:36:23 +02:00
macro Rect Glyph.bounds(&g) => {.x = g.ox, .y = g.oy, .w = g.w, .h = g.h};
macro Rect Glyph.uv(&g) => {.x = g.u, .y = g.v, .w = g.w, .h = g.h};
2025-10-04 19:42:59 +02:00
<*
@param [&inout] font
@param [in] name
@param [&in] path
@require height > 0, scale > 0: "height and scale must be positive non-zero"
*>
fn void? Font.load(&font, Allocator allocator, String name, ZString path, uint height, float scale)
2024-12-06 22:03:35 +01:00
{
font.table.init(allocator, capacity: FONT_CACHED);
2024-12-15 21:39:26 +01:00
font.id = name.hash();
2024-12-11 01:14:14 +01:00
font.size = height*scale;
2025-05-05 16:23:26 +02:00
font.sft = {
2024-12-11 01:14:14 +01:00
.xScale = (double)font.size,
.yScale = (double)font.size,
.flags = schrift::SFT_DOWNWARD_Y,
};
font.sft.font = schrift::loadfile(path);
if (font.sft.font == null) {
2024-12-25 12:30:35 +01:00
font.table.free();
2025-05-05 16:23:26 +02:00
return TTF_LOAD_FAILED?;
2024-12-11 01:14:14 +01:00
}
2024-12-06 22:03:35 +01:00
2024-12-11 01:14:14 +01:00
schrift::SftLMetrics lmetrics;
schrift::lmetrics(&font.sft, &lmetrics);
font.ascender = (float)lmetrics.ascender;
font.descender = (float)lmetrics.descender;
font.linegap = (float)lmetrics.lineGap;
2024-12-11 20:39:06 +01:00
//io::printfn("ascender:%d, descender:%d, linegap:%d", font.ascender, font.descender, font.linegap);
2024-12-11 01:14:14 +01:00
// TODO: allocate buffer based on FONT_CACHED and the size of a sample letter
// like the letter 'A'
2025-01-30 22:27:24 +01:00
ushort size = (ushort)font.size*(ushort)($$sqrt((float)FONT_CACHED));
2024-12-15 21:39:26 +01:00
font.atlas.new(font.id, ATLAS_GRAYSCALE, size, size)!;
// preallocate the ASCII range
for (char c = ' '; c < '~'; c++) {
// FIXME: without @inline, this crashes with O1 or greater
font.get_glyph((Codepoint)c) @inline!;
}
2024-12-06 22:03:35 +01:00
}
2025-10-04 19:42:59 +02:00
<*
@param [&inout] font
*>
2025-05-05 16:23:26 +02:00
fn Glyph*? Font.get_glyph(&font, Codepoint code)
2024-12-06 22:03:35 +01:00
{
2025-05-05 16:23:26 +02:00
Glyph*? gp;
2024-12-11 01:14:14 +01:00
gp = font.table.get_ref(code);
2024-12-15 21:39:26 +01:00
2024-12-11 01:14:14 +01:00
if (catch excuse = gp) {
2025-05-05 16:23:26 +02:00
if (excuse != NOT_FOUND) {
2024-12-11 01:14:14 +01:00
return excuse?;
}
} else {
return gp;
}
2024-12-11 01:14:14 +01:00
// missing glyph, render and place into an atlas
Glyph glyph;
2024-12-06 22:03:35 +01:00
2024-12-11 01:14:14 +01:00
schrift::SftGlyph gid;
schrift::SftGMetrics gmtx;
2024-12-15 21:39:26 +01:00
2025-05-05 16:23:26 +02:00
if (schrift::lookup(&font.sft, (SftUChar)code, &gid) < 0) {
return MISSING_GLYPH?;
2024-12-11 01:14:14 +01:00
}
if (schrift::gmetrics(&font.sft, gid, &gmtx) < 0) {
2025-05-05 16:23:26 +02:00
return BAD_GLYPH_METRICS?;
2024-12-11 01:14:14 +01:00
}
schrift::SftImage img = {
.width = gmtx.minWidth,
.height = gmtx.minHeight,
};
char[] pixels = mem::new_array(char, (usz)img.width * img.height);
img.pixels = pixels;
if (schrift::render(&font.sft, gid, img) < 0) {
2025-05-05 16:23:26 +02:00
return RENDER_ERROR?;
2024-12-11 01:14:14 +01:00
}
glyph.code = code;
glyph.w = (ushort)img.width;
glyph.h = (ushort)img.height;
2024-12-11 20:39:06 +01:00
glyph.ox = (short)gmtx.leftSideBearing;
glyph.oy = (short)gmtx.yOffset;
2024-12-11 01:14:14 +01:00
glyph.adv = (short)gmtx.advanceWidth;
2024-12-11 20:39:06 +01:00
//io::printfn("code=%c, w=%d, h=%d, ox=%d, oy=%d, adv=%d",
// glyph.code, glyph.w, glyph.h, glyph.ox, glyph.oy, glyph.adv);
2024-12-11 01:14:14 +01:00
Point uv = font.atlas.place(pixels, glyph.w, glyph.h, (ushort)img.width)!;
2024-12-11 01:14:14 +01:00
glyph.u = uv.x;
glyph.v = uv.y;
mem::free(pixels);
font.table.set(code, glyph);
2024-12-15 21:39:26 +01:00
font.should_update = true;
2024-12-11 01:14:14 +01:00
return font.table.get_ref(code);
}
2025-10-04 19:42:59 +02:00
<*
@param [&inout] font
*>
2024-12-11 01:14:14 +01:00
fn void Font.free(&font)
{
2024-12-15 21:39:26 +01:00
font.atlas.free();
font.table.free();
2024-12-11 01:14:14 +01:00
schrift::freefont(font.sft.font);
2024-12-06 22:03:35 +01:00
}
2024-12-15 21:39:26 +01:00
// ---------------------------------------------------------------------------------- //
// FONT LOAD AND QUERY //
// ---------------------------------------------------------------------------------- //
module ugui;
2025-10-04 19:42:59 +02:00
<*
@param [&inout] ctx
@param [in] name
@param [&in] path
@require height > 0, scale > 0: "height and scale must be positive non-zero"
*>
fn void? Ctx.load_font(&ctx, Allocator allocator, String name, ZString path, uint height, float scale = 1.0)
2024-12-15 21:39:26 +01:00
{
return ctx.font.load(allocator, name, path, height, scale);
2024-12-15 22:29:07 +01:00
}
2025-10-04 19:42:59 +02:00
<*
@param [&in] ctx
@param [in] label
*>
// TODO: check if the font is present in the context
2025-10-04 19:42:59 +02:00
fn Id Ctx.get_font_id(&ctx, String label) => (Id)label.hash();
2025-10-04 19:42:59 +02:00
<*
@param [&in] ctx
@param [in] name
*>
fn Atlas*? Ctx.get_font_atlas(&ctx, String name)
{
// TODO: use the font name, for now there is only one font
if (name.hash() != ctx.font.id) {
return WRONG_ID?;
2024-12-15 22:29:07 +01:00
}
return &ctx.font.atlas;
2024-12-15 22:29:07 +01:00
}
2025-10-04 19:42:59 +02:00
<* @param [&in] font *>
fn int Font.line_height(&font) => (int)(font.ascender - font.descender + (float)0.5);