Something went wrong. Try again.
Minecraft text rendering inside the browser! www.npmjs.com/package/libmojangles
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186import type { BitmapProvider, SpaceProvider, GlyphRenderInfo,} from "../types";import { GlyphRenderInfoImpl, SpaceGlyph } from "./glyph";
export interface GlyphProvider { getGlyph(codepoint: number): GlyphRenderInfo | null; hasGlyph(codepoint: number): boolean; getCodepoints(): number[];}
export class BitmapGlyphProvider implements GlyphProvider { private glyphs = new Map<number, GlyphRenderInfo>(); private codepoints: number[] = [];
constructor( private config: BitmapProvider, private textureId: string, private textureWidth: number, private textureHeight: number, private imageData: Uint8Array | null = null ) { this.buildGlyphMap(); }
private buildGlyphMap(): void { const configuredHeight = this.config.height ?? 8; const ascent = this.config.ascent; const chars = this.config.chars;
const gridWidth = chars[0]?.length ?? 0; const gridHeight = chars.length; if (gridWidth === 0 || gridHeight === 0) return;
const cellWidth = this.textureWidth / gridWidth; const cellHeight = this.textureHeight / gridHeight; const pixelScale = configuredHeight / cellHeight; const oversample = 1 / pixelScale;
for (let row = 0; row < chars.length; row++) { const charRow = chars[row] ?? ""; let col = 0; for (const char of charRow) { const codepoint = char.codePointAt(0); if (codepoint !== undefined && codepoint !== 0) { const actualWidth = this.getActualGlyphWidth(col, row, cellWidth, cellHeight); const advance = Math.round(actualWidth * pixelScale) + 1;
const left = 0; const right = actualWidth / oversample; const up = 7.0 - ascent; const down = up + cellHeight / oversample;
const u0 = (col * cellWidth + 0.01) / this.textureWidth; const v0 = (row * cellHeight + 0.01) / this.textureHeight; const u1 = (col * cellWidth + actualWidth - 0.01) / this.textureWidth; const v1 = ((row + 1) * cellHeight - 0.01) / this.textureHeight;
const glyph = new GlyphRenderInfoImpl( codepoint, advance, left, right, up, down, u0, v0, u1, v1, this.textureId ); this.glyphs.set(codepoint, glyph); this.codepoints.push(codepoint); } col++; } }
if (!this.glyphs.has(32)) { this.glyphs.set(32, new SpaceGlyph(32, 4)); this.codepoints.push(32); } }
private getActualGlyphWidth( gridX: number, gridY: number, cellWidth: number, cellHeight: number ): number { if (!this.imageData) { return cellWidth; }
const startX = Math.floor(gridX * cellWidth); const startY = Math.floor(gridY * cellHeight); const endX = Math.floor(startX + cellWidth); const endY = Math.floor(startY + cellHeight);
for (let x = endX - 1; x >= startX; x--) { for (let y = startY; y < endY; y++) { const idx = (y * this.textureWidth + x) * 4; const alpha = this.imageData[idx + 3] ?? 0; if (alpha > 0) { return x - startX + 1; } } }
return 0; }
getGlyph(codepoint: number): GlyphRenderInfo | null { return this.glyphs.get(codepoint) ?? null; }
hasGlyph(codepoint: number): boolean { return this.glyphs.has(codepoint); }
getCodepoints(): number[] { return this.codepoints; }}
export class SpaceGlyphProvider implements GlyphProvider { private advances = new Map<number, number>();
constructor(config: SpaceProvider) { for (const [char, advance] of Object.entries(config.advances)) { const codepoint = char.codePointAt(0); if (codepoint !== undefined) { this.advances.set(codepoint, advance); } } }
getGlyph(codepoint: number): GlyphRenderInfo | null { const advance = this.advances.get(codepoint); if (advance === undefined) return null; return new SpaceGlyph(codepoint, advance); }
hasGlyph(codepoint: number): boolean { return this.advances.has(codepoint); }
getCodepoints(): number[] { return Array.from(this.advances.keys()); }}
export class MissingGlyphProvider implements GlyphProvider { private readonly missingGlyph: GlyphRenderInfo;
constructor(textureId: string) { this.missingGlyph = new GlyphRenderInfoImpl( 0xfffd, 6, 0, 5, 0, 7, 0, 0, 1, 1, textureId ); }
getGlyph(_codepoint: number): GlyphRenderInfo { return this.missingGlyph; }
hasGlyph(_codepoint: number): boolean { return true; }
getCodepoints(): number[] { return [0xfffd]; }}