Add skills

This commit is contained in:
2025-02-02 20:05:52 -08:00
parent 810edb7e3b
commit 06a7263ad9
9 changed files with 588 additions and 45 deletions

View File

@ -98,13 +98,15 @@ class Font {
return this.#glyphwise(text, forceWidth, () => {});
}
#glyphwise(text: string, forceWidth: number | undefined, callback: (x: number, y: number, char: string) => void): {w: number, h: number} {
#glyphwise(text: string, forceWidth: number | undefined, callback: (x: number, y: number, char: string) => void): Size {
let cx = 0;
let cy = 0;
let cw = 0;
let ch = 0;
let wcx = forceWidth == undefined ? undefined : Math.floor(forceWidth / this.#px);
text = betterWordWrap(text, wcx);
for (let i = 0; i < text.length; i++) {
let char = text[i]
if (char == '\n') {
@ -126,8 +128,19 @@ class Font {
}
}
return { w: cw * this.#px, h: ch * this.#py };
return new Size(cw * this.#px, ch * this.#py);
}
}
// https://stackoverflow.com/users/1993501/edi9999
function betterWordWrap(s: string, wcx?: number) {
if (wcx === undefined) {
return s;
}
return s.replace(
new RegExp(`(?![^\\n]{1,${wcx}}$)([^\\n]{1,${wcx}})\\s`, 'g'), '$1\n'
);
}
export let mainFont = new Font(fontSheet, new Size(32, 8), new Size(8, 16));