diff options
Diffstat (limited to 'web')
| -rw-r--r-- | web/host.js | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/web/host.js b/web/host.js index 32bdbfb..8213dbd 100644 --- a/web/host.js +++ b/web/host.js @@ -519,8 +519,49 @@ function drawImage(id, x, y, w, h, sx, sy, sw, sh, r, g, b, a, place) { ctx.restore(); } +// Text (docs/text.md): sketchbook's faces in the page's fonts, in their +// weight and style, and a sketch's own font a FontFace (`fonts`, its bytes +// kept to give the same face for the same bytes). 13 px at scale 1. +const faceFonts = [['', 'monospace'], ['bold', 'monospace'], ['italic', 'monospace'], ['', 'sans-serif'], ['500', 'sans-serif'], ['bold', 'sans-serif'], ['', 'monospace'], ['bold', 'monospace'], ['italic', 'monospace'], ['500', 'monospace']]; +const fonts = []; +const fontOf = (face, scale) => { + const [style, family] = face < 10 ? faceFonts[face] : ['', fonts[face - 10]?.family ?? 'monospace']; + return `${style} ${13 * scale}px ${family}`.trim(); +}; +const measurer = document.createElement('canvas').getContext('2d'); + const imports = { host: { + // A run's width, ascent and line height, and each byte's x (a byte inside + // a codepoint at its codepoint's): the prefixes measured, so drawn is + // measured, kerning included. + textRun(face, scale, ptr, len, xsPtr, xsLen, outPtr) { + measurer.font = fontOf(face, scale); + const all = bytes(ptr, len).slice(); + const m = measurer.measureText(decoder.decode(all)); + new Float32Array(wasm.memory.buffer, outPtr, 3).set([m.width, m.fontBoundingBoxAscent, m.fontBoundingBoxAscent + m.fontBoundingBoxDescent]); + if (!xsLen) return; + const xs = new Float32Array(wasm.memory.buffer, xsPtr, xsLen); + let x = 0; + for (let i = 0; i < len; i++) { + if ((all[i] & 0xc0) !== 0x80) x = i ? measurer.measureText(decoder.decode(all.subarray(0, i))).width : 0; + xs[i] = x; + } + }, + font(ptr, len) { + const data = bytes(ptr, len).slice(); + const known = fonts.findIndex(f => f.data.length === data.length && f.data.every((b, i) => b === data[i])); + if (known >= 0) return 10 + known; + if (fonts.length === 54) return -1; + const family = `sketchbook-font-${fonts.length}`; + try { + const face = new FontFace(family, data); + document.fonts.add(face); + face.load().catch(err => console.warn(`font ${family}: ${err}`)); + } catch { return -1; } + fonts.push({family, data}); + return 10 + fonts.length - 1; + }, log(ptr, len) { console.log(string(ptr, len)); }, loadImage(ptr, len) { const path = string(ptr, len); @@ -615,15 +656,15 @@ const imports = { if (open) ctx.fill('nonzero'); }, // Always blended alpha, as native text is. - text(x, y, scale, ptr, len, r, g, b, a) { + // A run from its line box's top left, in the face textRun measures it in. + text(x, y, scale, face, ptr, len, r, g, b, a) { ctx.save(); ctx.globalCompositeOperation = 'source-over'; ctx.fillStyle = rgba(r, g, b, a); - // The native font is a fixed 8px cell: the same advance, and a size - // whose capitals fill the cell as native ones do. - ctx.font = `${10 * scale}px monospace`; - ctx.textBaseline = 'top'; - for (const [i, ch] of Array.from(string(ptr, len)).entries()) ctx.fillText(ch, x + i * 8 * scale, y); + ctx.font = fontOf(face, scale); + ctx.textBaseline = 'alphabetic'; + const s = string(ptr, len); + ctx.fillText(s, x, y + ctx.measureText(s).fontBoundingBoxAscent); ctx.restore(); }, clip(enabled, x, y, w, h) { |
