xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / text.js 98 lines3.3 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { T } from './theme.js';
3
4const BODY = '"Alegreya", Georgia, serif';
5const DISPLAY = '"MedievalSharp", "Alegreya", serif';
6
7export async function loadFonts() {
8  const faces = [new FontFace('Alegreya', 'url(/fonts/Alegreya.woff2)', { weight: '400 900', featureSettings: '"lnum", "tnum"' }), new FontFace('MedievalSharp', 'url(/fonts/MedievalSharp.woff2)')];
9  for (const f of await Promise.all(faces.map((x) => x.load()))) document.fonts.add(f);
10}
11
12export const font = (size, weight = 'normal', display = false) => `${weight} ${size}px ${display ? DISPLAY : BODY}`;
13
14const caps = new Map();
15function capHeight(ctx, f) {
16  if (!caps.has(f)) {
17    ctx.font = f;
18    caps.set(f, ctx.measureText('H').actualBoundingBoxAscent);
19  }
20  return caps.get(f);
21}
22
23function measure(ctx, text, size, weight, display) {
24  ctx.font = font(size, weight, display);
25  return ctx.measureText(text).width;
26}
27
28function ellipsize(ctx, text, size, weight, maxW, display) {
29  if (measure(ctx, text, size, weight, display) <= maxW) return text;
30  let s = text;
31  while (s.length > 1 && measure(ctx, s + '…', size, weight, display) > maxW) s = s.slice(0, -1);
32  return s + '…';
33}
34
35export function wrap(ctx, text, size, weight, maxW, display) {
36  const lines = [];
37  for (const para of String(text).split('\n')) {
38    let line = '';
39    for (const word of para.split(/\s+/).filter(Boolean)) {
40      const next = line ? `${line} ${word}` : word;
41      if (line && measure(ctx, next, size, weight, display) > maxW) {
42        lines.push(line);
43        line = word;
44      } else line = next;
45    }
46    lines.push(line);
47  }
48  return lines;
49}
50
51export function drawText(ctx, text, x, y, o = {}) {
52  const { size = 14, color = T.fg, weight = 'normal', align = 'left', maxW, shadow = null, fit = false, display = false } = o;
53  let s = size;
54  let t = String(text);
55  if (maxW && fit) while (s > 9 && measure(ctx, t, s, weight, display) > maxW) s -= 0.5;
56  if (maxW) t = ellipsize(ctx, t, s, weight, maxW, display);
57  const f = font(s, weight, display);
58  const h = o.h ?? lineHeight(size);
59  const base = Math.round(y + h / 2 + capHeight(ctx, f) / 2);
60  ctx.font = f;
61  ctx.textAlign = align;
62  ctx.textBaseline = 'alphabetic';
63  if (shadow) {
64    ctx.fillStyle = shadow;
65    ctx.fillText(t, x + 1, base + 1);
66  }
67  ctx.fillStyle = color;
68  ctx.fillText(t, x, base);
69  return s;
70}
71
72export const lineHeight = (size) => Math.round(size * 1.32);
73
74const labels = new Map();
75
76export function labelTexture(text, color, size = 40) {
77  const key = `${text}|${color}|${size}`;
78  if (labels.has(key)) return labels.get(key);
79  const probe = new OffscreenCanvas(8, 8).getContext('2d');
80  const w = Math.ceil(measure(probe, text, size, 'normal', true)) + 24;
81  const h = Math.ceil(size * 1.5);
82  const c = new OffscreenCanvas(w, h);
83  const g = c.getContext('2d');
84  g.font = font(size, 'normal', true);
85  g.textAlign = 'center';
86  g.textBaseline = 'middle';
87  g.lineJoin = 'round';
88  g.lineWidth = size / 4;
89  g.strokeStyle = T.bg;
90  g.strokeText(text, w / 2, h / 2);
91  g.fillStyle = color;
92  g.fillText(text, w / 2, h / 2);
93  const texture = new THREE.CanvasTexture(c);
94  texture.colorSpace = THREE.SRGBColorSpace;
95  const entry = { texture, aspect: w / h, h: size * 1.5 };
96  labels.set(key, entry);
97  return entry;
98}