Allow variable width in font

This commit is contained in:
dylan
2023-05-08 21:39:08 -07:00
parent 107a5370b1
commit ad5acdeb12
6 changed files with 967 additions and 806 deletions

View File

@@ -3,7 +3,7 @@ import {
clearScreen,
fillRect,
} from "./window.ts";
import { font } from "./font.ts";
import { Font, font } from "./font.ts";
import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
import { addToContext, runCode } from "./runcode.ts";
import { resetRepl } from "./repl.ts";
@@ -32,14 +32,41 @@ export const drawIcon = (x: number, y: number, icon: Array<number>, color: numbe
setPixelsInRect(x, y, 8, icon.map(n => n*color));
}
export const drawChar = (x: number, y: number, char: string, color: number) => {
setPixelsInRect(x, y, 4, font[char].map(n => n*color));
export const measureCharFont = (char: string, fnt: Font) => {
return (fnt.chars[char]?.length ?? 0)/fnt.height;
}
export const drawCharFont = (x: number, y: number, char: string, fnt: Font, color: number) => {
const w = measureCharFont(char, fnt);
if (!fnt.chars[char]) {
return 0;
}
setPixelsInRect(x, y, w, fnt.chars[char].map(n => n*color));
return w;
}
export const drawTextFont = (x: number, y: number, text: string, fnt: Font, color?: number) => {
let dx = 0;
[...text].forEach((char) => {
dx += 1+drawCharFont(x+dx, y, char, fnt, color ?? COLOR.WHITE);
});
return dx-1;
}
export const measureTextFont = (text: string, fnt: Font) => {
let w = 0;
[...text].forEach((char) => {
w += measureCharFont(char, fnt)+1;
});
return Math.max(0, w-1);
}
export const drawText = (x: number, y: number, text: string, color?: number) => {
[...text].forEach((char, i) => {
drawChar(x+4*i, y, char, color ?? COLOR.WHITE);
});
return drawTextFont(x, y, text, font, color);
}
export const measureText = (text: string) => {
return measureTextFont(text, font);
}
const faux = {