|
| 1 | +import type { PNG } from './png.ts'; |
| 2 | + |
| 3 | +/** |
| 4 | + * The bitmap glyph table both capture annotators paint with: the `eN` badges the screenshot ref |
| 5 | + * overlay prints, and the elapsed-time labels a contact sheet prints. It covers the characters |
| 6 | + * those labels are built from — digits, the separator and decimal point of a timestamp, and the |
| 7 | + * `e` ref prefix — and nothing else, so an unsupported character paints as blank rather than |
| 8 | + * inventing a shape nobody reviewed. |
| 9 | + */ |
| 10 | +export type PngGlyphColor = readonly [number, number, number, number]; |
| 11 | + |
| 12 | +const GLYPH_WIDTH = 5; |
| 13 | +const GLYPH_HEIGHT = 7; |
| 14 | +const GLYPH_SPACING = 1; |
| 15 | +const GLYPH_WIDTH_PITCH = GLYPH_WIDTH + GLYPH_SPACING; |
| 16 | + |
| 17 | +const FONT: Record<string, readonly string[]> = { |
| 18 | + e: ['01110', '10000', '11110', '10000', '10000', '10001', '01110'], |
| 19 | + '0': ['01110', '10001', '10011', '10101', '11001', '10001', '01110'], |
| 20 | + '1': ['00100', '01100', '00100', '00100', '00100', '00100', '01110'], |
| 21 | + '2': ['01110', '10001', '00001', '00010', '00100', '01000', '11111'], |
| 22 | + '3': ['11110', '00001', '00001', '01110', '00001', '00001', '11110'], |
| 23 | + '4': ['00010', '00110', '01010', '10010', '11111', '00010', '00010'], |
| 24 | + '5': ['11111', '10000', '10000', '11110', '00001', '00001', '11110'], |
| 25 | + '6': ['01110', '10000', '10000', '11110', '10001', '10001', '01110'], |
| 26 | + '7': ['11111', '00001', '00010', '00100', '01000', '01000', '01000'], |
| 27 | + '8': ['01110', '10001', '10001', '01110', '10001', '10001', '01110'], |
| 28 | + '9': ['01110', '10001', '10001', '01111', '00001', '00001', '01110'], |
| 29 | + ':': ['00000', '00100', '00100', '00000', '00100', '00100', '00000'], |
| 30 | + '.': ['00000', '00000', '00000', '00000', '00000', '00110', '00110'], |
| 31 | +} as const; |
| 32 | + |
| 33 | +/** Width the painted text actually occupies, with no trailing inter-character gap. */ |
| 34 | +export function measurePngGlyphText(text: string, scale = 1): number { |
| 35 | + if (text === '') return 0; |
| 36 | + return measurePngGlyphTextAdvance(text, scale) - GLYPH_SPACING * scale; |
| 37 | +} |
| 38 | + |
| 39 | +/** Distance the cursor moves, including the gap after the final character. */ |
| 40 | +export function measurePngGlyphTextAdvance(text: string, scale = 1): number { |
| 41 | + return text.length * GLYPH_WIDTH_PITCH * scale; |
| 42 | +} |
| 43 | + |
| 44 | +export function measurePngGlyphTextHeight(scale = 1): number { |
| 45 | + return GLYPH_HEIGHT * scale; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Paints `text` at `x, y` at the given integer scale, clipping at the image edge. A character the |
| 50 | + * table does not cover still advances the cursor, so a partially supported label keeps its layout. |
| 51 | + */ |
| 52 | +export function drawPngGlyphText( |
| 53 | + png: PNG, |
| 54 | + input: Readonly<{ |
| 55 | + x: number; |
| 56 | + y: number; |
| 57 | + text: string; |
| 58 | + color: PngGlyphColor; |
| 59 | + scale?: number; |
| 60 | + }>, |
| 61 | +): void { |
| 62 | + const scale = input.scale ?? 1; |
| 63 | + let cursorX = input.x; |
| 64 | + for (const character of input.text.toLowerCase()) { |
| 65 | + const glyph = FONT[character]; |
| 66 | + if (glyph) { |
| 67 | + for (let row = 0; row < glyph.length; row += 1) { |
| 68 | + for (let column = 0; column < glyph[row]!.length; column += 1) { |
| 69 | + if (glyph[row]![column] !== '1') continue; |
| 70 | + fillGlyphPixel(png, cursorX + column * scale, input.y + row * scale, scale, input.color); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + cursorX += GLYPH_WIDTH_PITCH * scale; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function fillGlyphPixel(png: PNG, x: number, y: number, scale: number, color: PngGlyphColor): void { |
| 79 | + for (let row = 0; row < scale; row += 1) { |
| 80 | + for (let column = 0; column < scale; column += 1) { |
| 81 | + setPngPixel(png, x + column, y + row, color); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export function setPngPixel(png: PNG, x: number, y: number, color: PngGlyphColor): void { |
| 87 | + if (x < 0 || y < 0 || x >= png.width || y >= png.height) return; |
| 88 | + const index = (png.width * y + x) * 4; |
| 89 | + png.data[index] = color[0]; |
| 90 | + png.data[index + 1] = color[1]; |
| 91 | + png.data[index + 2] = color[2]; |
| 92 | + png.data[index + 3] = color[3]; |
| 93 | +} |
0 commit comments