diff --git a/packages/cli/THIRD_PARTY_LICENSES.md b/packages/cli/THIRD_PARTY_LICENSES.md index 3d1e47486c..aab45064f3 100644 --- a/packages/cli/THIRD_PARTY_LICENSES.md +++ b/packages/cli/THIRD_PARTY_LICENSES.md @@ -1,28 +1,31 @@ # Third-Party Licenses -## Cozette +## Spleen -The Sixel dashboard includes a generated subset of Cozette bitmap glyph data, -version 1.30.0. Source: . +The Sixel dashboard includes a generated subset of Spleen bitmap glyph data, +version 2.2.0. Source: . -MIT License +Copyright (c) 2018-2026, Frederic Cambus +All rights reserved. -Copyright (c) 2020, Ines +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/cli/package.json b/packages/cli/package.json index fd0e14d4fa..510b751891 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -32,6 +32,7 @@ "dist/index.d.mts", "dist/ink-app.js", "dist/node-sqlite3-wasm.wasm", + "dist/assets/spleen-8x16.bin", "dist/vendor/symbolic_bg.wasm", "LICENSE.md", "THIRD_PARTY_LICENSES.md", diff --git a/packages/cli/script/build.ts b/packages/cli/script/build.ts index 5c20c56d5a..c0d697e89a 100644 --- a/packages/cli/script/build.ts +++ b/packages/cli/script/build.ts @@ -344,6 +344,18 @@ async function compileAllTargets( copyFileSync(DIF_WASM_SRC, DIF_WASM); assetArgs.push("--assets", DIF_WASM); + // Embed the Spleen dashboard glyph subset. The asset key matches the path + // requested through node:sea in src/lib/formatters/spleen-font.ts. + const SPLEEN_FONT_SRC = "src/lib/formatters/assets/spleen-8x16.bin"; + if (!existsSync(SPLEEN_FONT_SRC)) { + throw new Error( + `Missing Spleen dashboard font asset at ${SPLEEN_FONT_SRC}` + ); + } + const SPLEEN_FONT = `${BUILD_DIR}/spleen-8x16.bin`; + copyFileSync(SPLEEN_FONT_SRC, SPLEEN_FONT); + assetArgs.push("--assets", SPLEEN_FONT); + console.log( ` Step 2: Compiling ${platforms.length} target(s) (Node SEA via fossilize)...` ); diff --git a/packages/cli/script/bundle.ts b/packages/cli/script/bundle.ts index 0ba2388866..16207c3985 100644 --- a/packages/cli/script/bundle.ts +++ b/packages/cli/script/bundle.ts @@ -398,6 +398,16 @@ await copyFile( ); console.log(" -> dist/vendor/symbolic_bg.wasm (DIF parser)"); +// Ship the Spleen glyph subset next to the npm bundle. The formatter resolves +// it via new URL("./assets/spleen-8x16.bin", import.meta.url), relative to +// dist/index.cjs (see src/lib/formatters/spleen-font.ts). +await mkdir("./dist/assets", { recursive: true }); +await copyFile( + "./src/lib/formatters/assets/spleen-8x16.bin", + "./dist/assets/spleen-8x16.bin" +); +console.log(" -> dist/assets/spleen-8x16.bin (dashboard font)"); + // Ship the WASM SQLite driver's .wasm next to the bundle. The driver JS // (node-sqlite3-wasm) is inlined into dist/index.cjs by esbuild, and its // Emscripten glue locates the .wasm via `__dirname + "/node-sqlite3-wasm.wasm"`. diff --git a/packages/cli/src/lib/dif/index.ts b/packages/cli/src/lib/dif/index.ts index 00e1995c4a..9091f7bcff 100644 --- a/packages/cli/src/lib/dif/index.ts +++ b/packages/cli/src/lib/dif/index.ts @@ -22,6 +22,7 @@ import { SourceBundleWriter, } from "@sentry/symbolic"; import { logger } from "../logger.js"; +import { getSeaRawAsset } from "../sea-assets.js"; const log = logger.withTag("dif"); const _require = createRequire(import.meta.url); @@ -71,22 +72,6 @@ export type DifArchiveInfo = { let initialized = false; -/** Returns the SEA API when running inside a Node SEA binary, else null. */ -function isSeaBinary(): { getRawAsset: (key: string) => ArrayBuffer } | null { - try { - const sea = _require("node:sea") as { - isSea?: () => boolean; - getRawAsset?: (key: string) => ArrayBuffer; - }; - if (sea.isSea?.() && sea.getRawAsset) { - return { getRawAsset: sea.getRawAsset }; - } - } catch (err) { - log.debug("node:sea unavailable; treating as non-SEA runtime", err); - } - return null; -} - /** * Load the WASM bytes for the current runtime mode. * @@ -95,9 +80,9 @@ function isSeaBinary(): { getRawAsset: (key: string) => ArrayBuffer } | null { * time. In dev it is resolved from the installed `@sentry/symbolic` package. */ function loadWasmBytes(): Uint8Array { - const sea = isSeaBinary(); - if (sea) { - return new Uint8Array(sea.getRawAsset(DIF_WASM_ASSET_KEY)); + const seaAsset = getSeaRawAsset(DIF_WASM_ASSET_KEY); + if (seaAsset) { + return seaAsset; } const sibling = new URL("./vendor/symbolic_bg.wasm", import.meta.url); if (existsSync(sibling)) { diff --git a/packages/cli/src/lib/formatters/assets/spleen-8x16.bin b/packages/cli/src/lib/formatters/assets/spleen-8x16.bin new file mode 100644 index 0000000000..09dc42fccd Binary files /dev/null and b/packages/cli/src/lib/formatters/assets/spleen-8x16.bin differ diff --git a/packages/cli/src/lib/formatters/cozette-font.ts b/packages/cli/src/lib/formatters/cozette-font.ts deleted file mode 100644 index 947d058e97..0000000000 --- a/packages/cli/src/lib/formatters/cozette-font.ts +++ /dev/null @@ -1,182 +0,0 @@ -/*! - * Cozette bitmap font subset, version 1.30.0. - * Copyright (c) 2020, Ines - * https://github.com/the-moonwitch/Cozette - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -import { Buffer } from "node:buffer"; - -/** Cozette's native monospaced cell width. */ -export const COZETTE_CELL_WIDTH = 6; - -/** Cozette's native cell height, including descenders. */ -export const COZETTE_CELL_HEIGHT = 13; - -const FIRST_ASCII_CODE_POINT = 32; -const LAST_ASCII_CODE_POINT = 126; -const FULL_BLOCK_CODE_POINT = 0x25_88; -const BLACK_SQUARE_CODE_POINT = 0x25_a0; -const UPPER_HALF_BLOCK_CODE_POINT = 0x25_80; -const LOWER_HALF_BLOCK_CODE_POINT = 0x25_84; -const HORIZONTAL_RULE_CODE_POINT = 0x25_00; -const VERTICAL_RULE_CODE_POINT = 0x25_02; -const LOWER_LEFT_CORNER_CODE_POINT = 0x25_14; -const RIGHT_TEE_CODE_POINT = 0x25_24; -const DOWN_TEE_CODE_POINT = 0x25_2c; -const ELLIPSIS_CODE_POINT = 0x20_26; -const LIGHT_SHADE_CODE_POINT = 0x25_91; -const MEDIUM_SHADE_CODE_POINT = 0x25_92; -const DARK_SHADE_CODE_POINT = 0x25_93; -const LOWER_ONE_EIGHTH_BLOCK_CODE_POINT = 0x25_81; -const LOWER_TWO_EIGHTHS_BLOCK_CODE_POINT = 0x25_82; -const LOWER_THREE_EIGHTHS_BLOCK_CODE_POINT = 0x25_83; -const LOWER_FIVE_EIGHTHS_BLOCK_CODE_POINT = 0x25_85; -const LOWER_SIX_EIGHTHS_BLOCK_CODE_POINT = 0x25_86; -const LOWER_SEVEN_EIGHTHS_BLOCK_CODE_POINT = 0x25_87; -const FULL_BLOCK_INDEX = LAST_ASCII_CODE_POINT - FIRST_ASCII_CODE_POINT + 1; -const BLACK_SQUARE_INDEX = FULL_BLOCK_INDEX + 1; -const QUESTION_MARK_INDEX = 0x3f - FIRST_ASCII_CODE_POINT; - -/** Dashboard's compact number and table renderers use these block glyphs. */ -const UPPER_HALF_BLOCK_GLYPH = new Uint8Array([ - 63, 63, 63, 63, 63, 63, 0, 0, 0, 0, 0, 0, 0, -]); -const LOWER_HALF_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 0, 0, 0, 0, 63, 63, 63, 63, 63, 63, 63, -]); -const HORIZONTAL_RULE_GLYPH = new Uint8Array([ - 0, 0, 0, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, -]); -const VERTICAL_RULE_GLYPH = new Uint8Array([ - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, -]); -const LOWER_LEFT_CORNER_GLYPH = new Uint8Array([ - 32, 32, 32, 32, 32, 32, 63, 0, 0, 0, 0, 0, 0, -]); -const RIGHT_TEE_GLYPH = new Uint8Array([ - 1, 1, 1, 1, 1, 1, 63, 1, 1, 1, 1, 1, 1, -]); -const DOWN_TEE_GLYPH = new Uint8Array([0, 0, 0, 0, 0, 63, 8, 8, 8, 8, 8, 8, 8]); -const ELLIPSIS_GLYPH = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0]); -const LIGHT_SHADE_GLYPH = new Uint8Array([ - 33, 0, 0, 33, 0, 0, 33, 0, 0, 33, 0, 0, 33, -]); -const MEDIUM_SHADE_GLYPH = new Uint8Array([ - 36, 9, 36, 9, 36, 9, 36, 9, 36, 9, 36, 9, 36, -]); -const DARK_SHADE_GLYPH = new Uint8Array([ - 45, 54, 45, 54, 45, 54, 45, 54, 45, 54, 45, 54, 45, -]); -const LOWER_ONE_EIGHTH_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 63, -]); -const LOWER_TWO_EIGHTHS_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 63, 63, -]); -const LOWER_THREE_EIGHTHS_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 63, 63, 63, 63, 63, -]); -const LOWER_FIVE_EIGHTHS_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 0, 0, 0, 63, 63, 63, 63, 63, 63, 63, 63, -]); -const LOWER_SIX_EIGHTHS_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 0, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, -]); -const LOWER_SEVEN_EIGHTHS_BLOCK_GLYPH = new Uint8Array([ - 0, 0, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, -]); - -/** Raster glyphs needed by the dashboard's box, sparkline, and text output. */ -const DASHBOARD_GLYPHS = new Map([ - [UPPER_HALF_BLOCK_CODE_POINT, UPPER_HALF_BLOCK_GLYPH], - [LOWER_HALF_BLOCK_CODE_POINT, LOWER_HALF_BLOCK_GLYPH], - [HORIZONTAL_RULE_CODE_POINT, HORIZONTAL_RULE_GLYPH], - [VERTICAL_RULE_CODE_POINT, VERTICAL_RULE_GLYPH], - [LOWER_LEFT_CORNER_CODE_POINT, LOWER_LEFT_CORNER_GLYPH], - [RIGHT_TEE_CODE_POINT, RIGHT_TEE_GLYPH], - [DOWN_TEE_CODE_POINT, DOWN_TEE_GLYPH], - [ELLIPSIS_CODE_POINT, ELLIPSIS_GLYPH], - [LIGHT_SHADE_CODE_POINT, LIGHT_SHADE_GLYPH], - [MEDIUM_SHADE_CODE_POINT, MEDIUM_SHADE_GLYPH], - [DARK_SHADE_CODE_POINT, DARK_SHADE_GLYPH], - [LOWER_ONE_EIGHTH_BLOCK_CODE_POINT, LOWER_ONE_EIGHTH_BLOCK_GLYPH], - [LOWER_TWO_EIGHTHS_BLOCK_CODE_POINT, LOWER_TWO_EIGHTHS_BLOCK_GLYPH], - [LOWER_THREE_EIGHTHS_BLOCK_CODE_POINT, LOWER_THREE_EIGHTHS_BLOCK_GLYPH], - [LOWER_FIVE_EIGHTHS_BLOCK_CODE_POINT, LOWER_FIVE_EIGHTHS_BLOCK_GLYPH], - [LOWER_SIX_EIGHTHS_BLOCK_CODE_POINT, LOWER_SIX_EIGHTHS_BLOCK_GLYPH], - [LOWER_SEVEN_EIGHTHS_BLOCK_CODE_POINT, LOWER_SEVEN_EIGHTHS_BLOCK_GLYPH], -]); - -/** - * Printable ASCII, full block, and black square raster rows generated from - * Cozette's v1.30.0 BDF release. Each glyph has 13 rows packed into the low - * six bits of a byte, with bit 5 as the leftmost pixel. Source SHA-256: - * 8d740166af3a14053773ac7a8846bf288c8818a44f73ab28d4725d91b03a5639. - */ -const GLYPH_ROWS = Uint8Array.from( - Buffer.from( - "AAAAAAAAAAAAAAAAAAAABAQEBAQEAAQAAAAACgoKAAAAAAAAAAAAAAAKCh8KCh8KCgAAAAAEDhUUDgUFFQ4EAAAACBQJAgQIEgUCAAAAAAQKCgQNEhISDQAAAAAEBAQAAAAAAAAAAAAAAgQECAgICAgEBAIAAAgEBAICAgICBAQIAAAAAAAKBB8ECgAAAAAAAAAABAQfBAQAAAAAAAAAAAAAAAAMDAQIAAAAAAAAAB8AAAAAAAAAAAAAAAAAAAwMAAAAAAEBAgIEBAgIEBAAAAAADhERFRUREQ4AAAAAAAQMFAQEBAQfAAAAAAAOEQECBAgQHwAAAAAADhEBBgEBEQ4AAAAAAAEDBQkRHwEBAAAAAAAfEBAeAQERDgAAAAAABggQHhEREQ4AAAAAAB8BAgIEBAgIAAAAAAAOEREOERERDgAAAAAADhEREQ8BAgwAAAAAAAAADAwAAAwMAAAAAAAAAAwMAAAMDAQIAAAAAAECBAgEAgEAAAAAAAAAAB8AHwAAAAAAAAAAEAgEAgQIEAAAAAAADhEBAgQEAAQAAAAAAA4RERcVFxAPAAAAAAAOERERHxEREQAAAAAAHhERHhERER4AAAAAAA4REBAQEBEOAAAAAAAcEhERERESHAAAAAAAHxAQHhAQEB8AAAAAAB8QEB4QEBAQAAAAAAAOERAQExERDgAAAAAAERERHxEREREAAAAAAA4EBAQEBAQOAAAAAAAHAQEBARERDgAAAAAAERIUHBISEREAAAAAABAQEBAQEBAfAAAAAAARGxUVEREREQAAAAAAERkZFRUTExEAAAAAAA4REREREREOAAAAAAAeERERHhAQEAAAAAAADhEREREREg0BAAAAAB4RER4SERERAAAAAAAOERAOAQERDgAAAAAAHwQEBAQEBAQAAAAAABEREREREREOAAAAAAAREREKCgoEBAAAAAAAERERFRUOCgoAAAAAABERCgQEChERAAAAAAAREREKBAQEBAAAAAAAHwIEBAgIEB8AAAAADggICAgICAgICA4AABAQCAgEBAICAQEAAAAOAgICAgICAgICDgAEChEAAAAAAAAAAAAAAAAAAAAAAAAAAB8AAAAIBAAAAAAAAAAAAAAAAAAADxERERMNAAAAABAQEB4RERERHgAAAAAAAAAOERAQEQ4AAAAAAQEBDxEREREPAAAAAAAAAA4RHxARDgAAAAAHCAgeCAgICAgAAAAAAAAADxEREREPAQEOABAQEB4REREREQAAAAAABAAMBAQEBAMAAAAAAAIABgICAgICCgQAABAQEBESFBwSEQAAAAAMBAQEBAQEBAYAAAAAAAAAGhUVFRUVAAAAAAAAAB4REREREQAAAAAAAAAOEREREQ4AAAAAAAAAHhEREREeEBAQAAAAAA8RERERDwEBAQAAAAAeERAQEBAAAAAAAAAADxAOAQEeAAAAAAAICB4ICAgIBwAAAAAAAAAREREREQ8AAAAAAAAAEREKCgQEAAAAAAAAABERFRUKCgAAAAAAAAARCgQEChEAAAAAAAAAEREREREPAQEOAAAAAB8CBAgQHwAAAAADBAQEBBgEBAQEAwAABAQEBAQEBAQEBAQAABgEBAQEAwQEBAQYAAAAAAAACRUSAAAAAAA/Pz8/Pz8/Pz8/Pz8/AAAAPz8/Pz8/PwAAAA==", - "base64" - ) -); - -/** Look up a Cozette glyph by character without parsing or rasterizing a font. */ -export function getCozetteGlyph(character: string): Uint8Array { - const codePoint = character.codePointAt(0); - const dashboardGlyph = getDashboardGlyph(codePoint); - if (dashboardGlyph) { - return dashboardGlyph; - } - const index = getGlyphIndex(codePoint); - const start = (index ?? QUESTION_MARK_INDEX) * COZETTE_CELL_HEIGHT; - return GLYPH_ROWS.subarray(start, start + COZETTE_CELL_HEIGHT); -} - -/** Whether the embedded font can represent a character without substitution. */ -export function hasCozetteGlyph(character: string): boolean { - const codePoint = character.codePointAt(0); - return ( - getDashboardGlyph(codePoint) !== undefined || - getGlyphIndex(codePoint) !== undefined - ); -} - -function getDashboardGlyph( - codePoint: number | undefined -): Uint8Array | undefined { - return codePoint === undefined ? undefined : DASHBOARD_GLYPHS.get(codePoint); -} - -function getGlyphIndex(codePoint: number | undefined): number | undefined { - if ( - codePoint !== undefined && - codePoint >= FIRST_ASCII_CODE_POINT && - codePoint <= LAST_ASCII_CODE_POINT - ) { - return codePoint - FIRST_ASCII_CODE_POINT; - } - if (codePoint === FULL_BLOCK_CODE_POINT) { - return FULL_BLOCK_INDEX; - } - if (codePoint === BLACK_SQUARE_CODE_POINT) { - return BLACK_SQUARE_INDEX; - } -} diff --git a/packages/cli/src/lib/formatters/pixel-canvas.ts b/packages/cli/src/lib/formatters/pixel-canvas.ts index 2f1244033e..c1bc14a90b 100644 --- a/packages/cli/src/lib/formatters/pixel-canvas.ts +++ b/packages/cli/src/lib/formatters/pixel-canvas.ts @@ -9,11 +9,11 @@ import type { DecodedImage } from "../sixel-image.js"; import { - COZETTE_CELL_HEIGHT, - COZETTE_CELL_WIDTH, - getCozetteGlyph, - hasCozetteGlyph, -} from "./cozette-font.js"; + getSpleenGlyph, + hasSpleenGlyph, + SPLEEN_CELL_HEIGHT, + SPLEEN_CELL_WIDTH, +} from "./spleen-font.js"; /** Unicode punctuation that has a clear one- or two-cell ASCII equivalent. */ const BITMAP_TEXT_FALLBACKS = new Map([ @@ -171,7 +171,7 @@ function copyOpaquePixel( destination.data[targetOffset + 3] = source.data[sourceOffset + 3] ?? 255; } -/** Draw a Cozette text line, scaled and clipped to its terminal cells. */ +/** Draw a Spleen text line, scaled and clipped to its terminal cells. */ export function drawPixelText( image: DecodedImage, text: string, @@ -188,7 +188,7 @@ export function drawPixelText( break; } const rawChar = character.value; - drawCozetteGlyph(image, getCozetteGlyph(rawChar), { + drawSpleenGlyph(image, getSpleenGlyph(rawChar), { x: options.x + column * cellWidth, y: options.y, cellWidth, @@ -202,7 +202,7 @@ export function drawPixelText( /** Convert common Unicode text into glyphs that the embedded font can draw. */ function* normalizeBitmapText(text: string): Iterable { for (const character of text) { - if (hasCozetteGlyph(character)) { + if (hasSpleenGlyph(character)) { yield character; continue; } @@ -216,7 +216,7 @@ function* normalizeBitmapText(text: string): Iterable { .replace(COMBINING_MARK_RE, ""); if ( decomposed.length > 0 && - [...decomposed].every((candidate) => hasCozetteGlyph(candidate)) + [...decomposed].every((candidate) => hasSpleenGlyph(candidate)) ) { yield* decomposed; continue; @@ -225,7 +225,7 @@ function* normalizeBitmapText(text: string): Iterable { } } -type CozetteGlyphOptions = { +type SpleenGlyphOptions = { x: number; y: number; cellWidth: number; @@ -233,48 +233,48 @@ type CozetteGlyphOptions = { color: Rgb; }; -function drawCozetteGlyph( +function drawSpleenGlyph( image: DecodedImage, glyph: Uint8Array, - options: CozetteGlyphOptions + options: SpleenGlyphOptions ): void { - for (let row = 0; row < COZETTE_CELL_HEIGHT; row += 1) { + for (let row = 0; row < SPLEEN_CELL_HEIGHT; row += 1) { const pattern = glyph[row] ?? 0; - for (let column = 0; column < COZETTE_CELL_WIDTH; column += 1) { - if (!isCozettePixelSet(pattern, column)) { + for (let column = 0; column < SPLEEN_CELL_WIDTH; column += 1) { + if (!isSpleenPixelSet(pattern, column)) { continue; } - drawCozettePixel(image, { ...options, row, column }); + drawSpleenPixel(image, { ...options, row, column }); } } } -function isCozettePixelSet(pattern: number, column: number): boolean { - const divisor = 2 ** (COZETTE_CELL_WIDTH - 1 - column); +function isSpleenPixelSet(pattern: number, column: number): boolean { + const divisor = 2 ** (SPLEEN_CELL_WIDTH - 1 - column); return Math.floor(pattern / divisor) % 2 === 1; } -type CozettePixelOptions = CozetteGlyphOptions & { +type SpleenPixelOptions = SpleenGlyphOptions & { row: number; column: number; }; -function drawCozettePixel( +function drawSpleenPixel( image: DecodedImage, - options: CozettePixelOptions + options: SpleenPixelOptions ): void { const x = options.x + - scaleCoordinate(options.column, options.cellWidth, COZETTE_CELL_WIDTH); + scaleCoordinate(options.column, options.cellWidth, SPLEEN_CELL_WIDTH); const y = options.y + - scaleCoordinate(options.row, options.cellHeight, COZETTE_CELL_HEIGHT); + scaleCoordinate(options.row, options.cellHeight, SPLEEN_CELL_HEIGHT); const right = options.x + - scaleCoordinate(options.column + 1, options.cellWidth, COZETTE_CELL_WIDTH); + scaleCoordinate(options.column + 1, options.cellWidth, SPLEEN_CELL_WIDTH); const bottom = options.y + - scaleCoordinate(options.row + 1, options.cellHeight, COZETTE_CELL_HEIGHT); + scaleCoordinate(options.row + 1, options.cellHeight, SPLEEN_CELL_HEIGHT); drawPixelRect(image, { x, y, @@ -284,7 +284,7 @@ function drawCozettePixel( }); } -/** Scale a Cozette bitmap coordinate to its terminal-cell boundary. */ +/** Scale a Spleen bitmap coordinate to its terminal-cell boundary. */ function scaleCoordinate( coordinate: number, cellSize: number, diff --git a/packages/cli/src/lib/formatters/spleen-font.ts b/packages/cli/src/lib/formatters/spleen-font.ts new file mode 100644 index 0000000000..bcce8cc866 --- /dev/null +++ b/packages/cli/src/lib/formatters/spleen-font.ts @@ -0,0 +1,127 @@ +/*! + * Spleen bitmap font subset, version 2.2.0. + * Copyright (c) 2018-2026, Frederic Cambus + * https://github.com/fcambus/spleen + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +import { readFileSync } from "node:fs"; +import { getSeaRawAsset } from "../sea-assets.js"; + +/** Spleen's native monospaced cell width. */ +export const SPLEEN_CELL_WIDTH = 8; + +/** Spleen's native monospaced cell height, including descenders. */ +export const SPLEEN_CELL_HEIGHT = 16; + +const FIRST_ASCII_CODE_POINT = 32; +const LAST_ASCII_CODE_POINT = 126; +const QUESTION_MARK_INDEX = 0x3f - FIRST_ASCII_CODE_POINT; +const DASHBOARD_CODE_POINTS = [ + 0x25_80, // ▀ + 0x25_84, // ▄ + 0x25_00, // ─ + 0x25_02, // │ + 0x25_14, // └ + 0x25_24, // ┤ + 0x25_2c, // ┬ + 0x20_26, // … + 0x25_91, // ░ + 0x25_92, // ▒ + 0x25_93, // ▓ + 0x25_81, // ▁ + 0x25_82, // ▂ + 0x25_83, // ▃ + 0x25_85, // ▅ + 0x25_86, // ▆ + 0x25_87, // ▇ + 0x25_88, // █ + 0x25_a0, // ■ +] as const; + +const DASHBOARD_GLYPH_INDEX = new Map( + DASHBOARD_CODE_POINTS.map((codePoint, index) => [ + codePoint, + LAST_ASCII_CODE_POINT - FIRST_ASCII_CODE_POINT + 1 + index, + ]) +); + +const SPLEEN_FONT_ASSET_KEY = "dist-build/spleen-8x16.bin"; +const SPLEEN_FONT_ASSET_URL = new URL( + "./assets/spleen-8x16.bin", + import.meta.url +); +const EXPECTED_GLYPH_ROWS = + (LAST_ASCII_CODE_POINT - + FIRST_ASCII_CODE_POINT + + 1 + + DASHBOARD_CODE_POINTS.length) * + SPLEEN_CELL_HEIGHT; + +/** + * Printable ASCII and glyphs needed by the dashboard's box, sparkline, and + * text output, generated from Spleen 2.2.0's 8x16 BDF release. The binary + * stores one 8-bit row per glyph row, with bit 7 as the leftmost pixel. It is + * copied into `dist/assets` for npm packages and embedded in SEA builds. + * + * Source commit: 57f9219328c9f5873085320fe8bc8f7dd34b8791. Source BDF + * SHA-256: b38b32a66920068965a3101f98071d310c5c74659fe86e55d346140770f8f6e8. + */ +const GLYPH_ROWS = loadGlyphRows(); + +/** Look up a Spleen glyph by character without parsing or rasterizing a font. */ +export function getSpleenGlyph(character: string): Uint8Array { + const index = getGlyphIndex(character.codePointAt(0)); + const start = (index ?? QUESTION_MARK_INDEX) * SPLEEN_CELL_HEIGHT; + return GLYPH_ROWS.subarray(start, start + SPLEEN_CELL_HEIGHT); +} + +/** Whether the embedded font can represent a character without substitution. */ +export function hasSpleenGlyph(character: string): boolean { + return getGlyphIndex(character.codePointAt(0)) !== undefined; +} + +function getGlyphIndex(codePoint: number | undefined): number | undefined { + if ( + codePoint !== undefined && + codePoint >= FIRST_ASCII_CODE_POINT && + codePoint <= LAST_ASCII_CODE_POINT + ) { + return codePoint - FIRST_ASCII_CODE_POINT; + } + return codePoint === undefined + ? undefined + : DASHBOARD_GLYPH_INDEX.get(codePoint); +} + +function loadGlyphRows(): Uint8Array { + const glyphRows = + getSeaRawAsset(SPLEEN_FONT_ASSET_KEY) ?? + new Uint8Array(readFileSync(SPLEEN_FONT_ASSET_URL)); + if (glyphRows.length !== EXPECTED_GLYPH_ROWS) { + throw new Error( + `Invalid Spleen dashboard font asset: expected ${EXPECTED_GLYPH_ROWS} bytes, got ${glyphRows.length}` + ); + } + return glyphRows; +} diff --git a/packages/cli/src/lib/sea-assets.ts b/packages/cli/src/lib/sea-assets.ts new file mode 100644 index 0000000000..11b1d69574 --- /dev/null +++ b/packages/cli/src/lib/sea-assets.ts @@ -0,0 +1,28 @@ +import { createRequire } from "node:module"; +import { logger } from "./logger.js"; + +const _require = createRequire(import.meta.url); +const log = logger.withTag("sea-assets"); + +/** + * Return bytes embedded in a Node single-executable application, if any. + * + * `node:sea` cannot be imported in ordinary Node processes, so load it lazily + * and let callers use their normal package/development fallback when absent. + */ +export function getSeaRawAsset(key: string): Uint8Array | undefined { + let sea: { + isSea?: () => boolean; + getRawAsset?: (key: string) => ArrayBuffer; + }; + try { + sea = _require("node:sea") as typeof sea; + } catch (error) { + log.debug("node:sea unavailable; treating as non-SEA runtime", error); + return; + } + if (!(sea.isSea?.() && sea.getRawAsset)) { + return; + } + return new Uint8Array(sea.getRawAsset(key)); +} diff --git a/packages/cli/test/lib/formatters/pixel-canvas.test.ts b/packages/cli/test/lib/formatters/pixel-canvas.test.ts index ab1f090c26..4ff92a40b6 100644 --- a/packages/cli/test/lib/formatters/pixel-canvas.test.ts +++ b/packages/cli/test/lib/formatters/pixel-canvas.test.ts @@ -1,9 +1,9 @@ import { describe, expect, test, vi } from "vitest"; -import { getCozetteGlyph } from "../../../src/lib/formatters/cozette-font.js"; import { createPixelCanvas, drawPixelText, } from "../../../src/lib/formatters/pixel-canvas.js"; +import { getSpleenGlyph } from "../../../src/lib/formatters/spleen-font.js"; function opaquePixelCount(image: { data: Uint8Array }): number { let count = 0; @@ -16,45 +16,38 @@ function opaquePixelCount(image: { data: Uint8Array }): number { } describe("drawPixelText", () => { - test("uses Cozette's native 6x13 raster at terminal cell size", () => { - const image = createPixelCanvas({ width: 6, height: 13 }); + test("uses Spleen's native 8x16 raster at terminal cell size", () => { + const image = createPixelCanvas({ width: 8, height: 16 }); drawPixelText(image, "A", { x: 0, y: 0, - cellWidth: 6, - cellHeight: 13, + cellWidth: 8, + cellHeight: 16, maxColumns: 1, color: [255, 255, 255], }); - expect(opaquePixelCount(image)).toBe(20); + expect(opaquePixelCount(image)).toBe(44); expect(image.data[(2 * image.width + 2) * 4 + 3]).toBe(255); expect(image.data[(0 * image.width + 1) * 4 + 3]).toBe(0); }); - test("packs Cozette rows with bit five as the leftmost pixel", () => { - expect(getCozetteGlyph("A")[2]).toBe(0b00_1110); - }); - - test("includes the half-block and rule glyphs used by dashboard content", () => { - expect([...getCozetteGlyph("▀")]).toEqual([ - 63, 63, 63, 63, 63, 63, 0, 0, 0, 0, 0, 0, 0, - ]); - expect([...getCozetteGlyph("▄")]).toEqual([ - 0, 0, 0, 0, 0, 0, 63, 63, 63, 63, 63, 63, 63, + test("keeps Spleen's native box and block glyphs for dashboard content", () => { + expect([...getSpleenGlyph("▀")]).toEqual([ + 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, ]); - expect([...getCozetteGlyph("─")]).toEqual([ - 0, 0, 0, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, + expect([...getSpleenGlyph("─")]).toEqual([ + 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, ]); }); test("uses a question mark for unsupported text glyphs", () => { - expect([...getCozetteGlyph("\u6f22")]).toEqual([...getCozetteGlyph("?")]); + expect([...getSpleenGlyph("\u6f22")]).toEqual([...getSpleenGlyph("?")]); }); test("includes the dashboard axis, sparkline, and ellipsis glyphs", () => { - const questionMark = [...getCozetteGlyph("?")]; + const questionMark = [...getSpleenGlyph("?")]; for (const character of [ "│", "└", @@ -70,8 +63,9 @@ describe("drawPixelText", () => { "▇", "░", "▓", + "■", ]) { - expect([...getCozetteGlyph(character)]).not.toEqual(questionMark); + expect([...getSpleenGlyph(character)]).not.toEqual(questionMark); } }); @@ -84,14 +78,14 @@ describe("drawPixelText", () => { test("does not normalize text outside its visible width", () => { const normalize = vi.spyOn(String.prototype, "normalize"); - const image = createPixelCanvas({ width: 6, height: 13 }); + const image = createPixelCanvas({ width: 8, height: 16 }); try { drawPixelText(image, `A${"é".repeat(100)}`, { x: 0, y: 0, - cellWidth: 6, - cellHeight: 13, + cellWidth: 8, + cellHeight: 16, maxColumns: 1, color: [255, 255, 255], }); @@ -130,7 +124,7 @@ describe("drawPixelText", () => { color: [255, 255, 255], }); - expect(opaquePixelCount(image)).toBe(20 * 4); + expect(opaquePixelCount(image)).toBe(106); }); test("never draws outside an undersized terminal cell", () => { @@ -160,12 +154,12 @@ describe("drawPixelText", () => { }); function renderText(text: string) { - const image = createPixelCanvas({ width: 120, height: 13 }); + const image = createPixelCanvas({ width: 160, height: 16 }); drawPixelText(image, text, { x: 0, y: 0, - cellWidth: 6, - cellHeight: 13, + cellWidth: 8, + cellHeight: 16, maxColumns: 20, color: [255, 255, 255], }); diff --git a/packages/cli/test/lib/sea-assets.test.ts b/packages/cli/test/lib/sea-assets.test.ts new file mode 100644 index 0000000000..b718ea286a --- /dev/null +++ b/packages/cli/test/lib/sea-assets.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, test, vi } from "vitest"; + +const seaModule = vi.hoisted(() => ({ + load: () => { + throw new Error("node:sea unavailable"); + }, +})); + +vi.mock("node:module", () => ({ + createRequire: + () => + (...args: unknown[]) => + seaModule.load(...args), +})); + +const { getSeaRawAsset } = await import("../../src/lib/sea-assets.js"); + +describe("getSeaRawAsset", () => { + test("propagates an embedded asset read failure", () => { + seaModule.load = () => ({ + isSea: () => true, + getRawAsset: () => { + throw new Error("embedded asset is missing"); + }, + }); + + expect(() => getSeaRawAsset("dist-build/missing.bin")).toThrow( + "embedded asset is missing" + ); + }); +});