// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { AssetManager } from "../assets.ts"; import { push_quad, use_texture } from "./core.ts"; import { type Plane3d, push_plane_quad } from "./shapes.ts"; import { FntChar, FntFont, Texture } from "./types.ts"; let font_texture: Texture; let font_fnt: FntFont; export function init_font() { font_texture = AssetManager.instance.get("cubi:m6x11"); font_fnt = parse_fnt(AssetManager.instance.get("cubi:m6x11_fnt")); } // bmfont's text format. glyphs in the image have transparent padding around them, which their offsets don't count, so // it's taken off here or text is drawn padding * scale pixels right of and below where it's measured function parse_fnt(text: string): FntFont { const lines = text.split(/\r?\n/); // up, right, down, left let padding = [0, 0, 0, 0]; const font: FntFont = { line_height: 0, chars: {}, pages: [], }; for (const line of lines) { const parts = line.split(" "); const type = parts[0]; const data: Record = {}; for (const part of parts.slice(1)) { const [k, v] = part.split("="); if (!k) { continue; } data[k] = v?.replace(/"/g, ""); } if (type === "info" && data.padding) { padding = data.padding.split(",").map(Number); } if (type === "common") { font.line_height = Number(data.lineHeight); } if (type === "page") { font.pages.push(data.file); } if (type === "char") { const char: FntChar = { id: Number(data.id), x: Number(data.x), y: Number(data.y), width: Number(data.width), height: Number(data.height), xoffset: Number(data.xoffset) - padding[3], yoffset: Number(data.yoffset) - padding[0], xadvance: Number(data.xadvance), page: Number(data.page), }; font.chars[char.id] = char; } } return font; } export function draw_text(str: string, x: number, y: number, scale: number = 1, color = [1, 1, 1, 1]) { if (!font_texture) { return; } use_texture(font_texture.tex); for (const ch of str) { const glyph = font_fnt.chars[ch.charCodeAt(0)]; if (!glyph) { continue; } const dx = x + glyph.xoffset * scale; const dy = y + glyph.yoffset * scale; const dw = glyph.width * scale; const dh = glyph.height * scale; const u0 = glyph.x / font_texture.width; const v0 = glyph.y / font_texture.height; const u1 = (glyph.x + glyph.width) / font_texture.width; const v1 = (glyph.y + glyph.height) / font_texture.height; push_quad(dx, dy, dw, dh, u0, v0, u1, v1, color[0], color[1], color[2], color[3]); x += glyph.xadvance * scale; } } // text on a plane in the world (see Plane3d), x and y in the plane's units like draw_text's are in pixels. scale is // how big a font pixel is in them export function draw_text_3d( plane: Plane3d, str: string, x: number, y: number, scale: number, color: readonly number[] = [1, 1, 1, 1], ) { if (!font_texture) { return; } use_texture(font_texture.tex); for (const ch of str) { const glyph = font_fnt.chars[ch.charCodeAt(0)]; if (!glyph) { continue; } push_plane_quad( plane, x + glyph.xoffset * scale, y + glyph.yoffset * scale, glyph.width * scale, glyph.height * scale, glyph.x / font_texture.width, glyph.y / font_texture.height, (glyph.x + glyph.width) / font_texture.width, (glyph.y + glyph.height) / font_texture.height, color, ); x += glyph.xadvance * scale; } } export function font_line_height() { return font_fnt?.line_height ?? 0; } export function measure_text(str: string, scale: number = 1) { let x = 0; for (const ch of str) { const glyph = font_fnt.chars[ch.charCodeAt(0)]; if (!glyph) { continue; } x += glyph.xadvance * scale; } return x; }