@ascii-fx/compiler 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/chromatic.d.ts +26 -0
- package/dist/chunk-ZQML5MF3.js +403 -0
- package/dist/chunk-ZQML5MF3.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +162 -0
- package/dist/cli.js.map +1 -0
- package/dist/cliArgs.d.ts +20 -0
- package/dist/config.d.ts +27 -0
- package/dist/font.d.ts +11 -0
- package/dist/fontkit.d.ts +28 -0
- package/dist/frameBuild.d.ts +11 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +219 -0
- package/dist/index.js.map +1 -0
- package/dist/png.d.ts +2 -0
- package/dist/profile.d.ts +37 -0
- package/dist/raster.d.ts +27 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Amir Abushanab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @ascii-fx/compiler
|
|
2
|
+
|
|
3
|
+
Node-only build-time compiler: deterministic font rasterization (fontkit parsing + an in-house fixed-point scanline rasterizer — no system rasterizers, so `.asciip` bytes are identical across platforms), glyph atlases, structural masks, and static frame precompilation.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { readFile } from 'node:fs/promises'
|
|
7
|
+
import { buildProfile, buildFrame, decodePng } from '@ascii-fx/compiler'
|
|
8
|
+
|
|
9
|
+
const { profile, binary } = buildProfile({
|
|
10
|
+
font: await readFile('./GeistMono-Regular.ttf'),
|
|
11
|
+
charset: 'ascii', // or characters: '@#*+. '
|
|
12
|
+
shape6: { lut: true }, // optional approximate-matcher data (adds ~514KiB raw)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const frame = buildFrame({ image: decodePng(pngBytes), profile, columns: 140, color: 'full' })
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## CLI
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
ascii-fx profile build --font GeistMono-Regular.ttf --out default.asciip [--shape6|--shape6-lut]
|
|
22
|
+
ascii-fx frame build --image hero.png --profile default.asciip --columns 140 --color full --out hero.asciif
|
|
23
|
+
ascii-fx inspect default.asciip
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Constraints: monospace fonts only (equal advances over the charset — enforced); the charset must be fully covered by the font (missing glyphs are listed, never tofu'd); PNG only for `frame build` in v1. Profiles embed content fingerprints; identical inputs → identical bytes, verified by tests.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AsciiProfile, RawImage } from '@ascii-fx/core';
|
|
2
|
+
/**
|
|
3
|
+
* Reference cell for chromatic profiles. Square, unlike a text cell, and sized
|
|
4
|
+
* so `cell + 2 * ATLAS_PADDING` is already a power of two: the pow2 pitch rule
|
|
5
|
+
* (§13) exists so padding survives mip levels, and any other cell size rounds
|
|
6
|
+
* up and wastes the difference. 32 would round 40 up to 64 and throw away 61%
|
|
7
|
+
* of the atlas; 24 fits 32 exactly. The efficient sizes are 24, 56, and 120.
|
|
8
|
+
*/
|
|
9
|
+
export declare const CHROMATIC_CELL = 24;
|
|
10
|
+
export declare const CHROMATIC_RASTER_VERSION = "raster-chromatic-v1";
|
|
11
|
+
export interface ChromaticGlyphSource {
|
|
12
|
+
/** The grapheme this glyph draws; may be several code points (ZWJ, VS16, modifiers). */
|
|
13
|
+
char: string;
|
|
14
|
+
image: RawImage;
|
|
15
|
+
}
|
|
16
|
+
export interface BuildChromaticOptions {
|
|
17
|
+
glyphs: readonly ChromaticGlyphSource[];
|
|
18
|
+
/** Reference cell size in px, square. Default CHROMATIC_CELL (24). */
|
|
19
|
+
cellSize?: number;
|
|
20
|
+
id?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface BuiltChromaticProfile {
|
|
23
|
+
profile: AsciiProfile;
|
|
24
|
+
binary: Uint8Array;
|
|
25
|
+
}
|
|
26
|
+
export declare function buildChromaticProfile(options: BuildChromaticOptions): BuiltChromaticProfile;
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// src/font.ts
|
|
2
|
+
import { Buffer } from "buffer";
|
|
3
|
+
import * as FK from "fontkit";
|
|
4
|
+
function loadFont(bytes) {
|
|
5
|
+
const create2 = FK.create ?? FK.default?.create;
|
|
6
|
+
if (!create2) throw new Error("fontkit did not expose create(); incompatible fontkit version.");
|
|
7
|
+
const font = create2(Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
|
|
8
|
+
if (font.fonts && font.fonts.length > 0) {
|
|
9
|
+
throw new Error("Font collections (.ttc/.otc) are not supported; extract a single face first.");
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
font,
|
|
13
|
+
unitsPerEm: font.unitsPerEm,
|
|
14
|
+
ascent: font.ascent,
|
|
15
|
+
descent: font.descent,
|
|
16
|
+
lineGap: font.lineGap,
|
|
17
|
+
familyName: font.familyName ?? font.postscriptName,
|
|
18
|
+
weight: font["OS/2"]?.usWeightClass
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/raster.ts
|
|
23
|
+
import { fdiv, idiv, rdiv } from "@ascii-fx/core";
|
|
24
|
+
var SUPERSAMPLE = 4;
|
|
25
|
+
var QUAD_SEGMENTS = 16;
|
|
26
|
+
var CUBIC_SEGMENTS = 24;
|
|
27
|
+
function flattenCommands(commands, map) {
|
|
28
|
+
const edges = [];
|
|
29
|
+
let curX = 0;
|
|
30
|
+
let curY = 0;
|
|
31
|
+
let startX = 0;
|
|
32
|
+
let startY = 0;
|
|
33
|
+
let open = false;
|
|
34
|
+
const push = (x0, y0, x1, y1) => {
|
|
35
|
+
if (y0 !== y1) edges.push(x0, y0, x1, y1);
|
|
36
|
+
};
|
|
37
|
+
const lineTo = (x, y) => {
|
|
38
|
+
push(curX, curY, x, y);
|
|
39
|
+
curX = x;
|
|
40
|
+
curY = y;
|
|
41
|
+
};
|
|
42
|
+
const close = () => {
|
|
43
|
+
if (open && (curX !== startX || curY !== startY)) push(curX, curY, startX, startY);
|
|
44
|
+
open = false;
|
|
45
|
+
};
|
|
46
|
+
for (const cmd of commands) {
|
|
47
|
+
const a = cmd.args;
|
|
48
|
+
switch (cmd.command) {
|
|
49
|
+
case "moveTo":
|
|
50
|
+
close();
|
|
51
|
+
curX = startX = map.x(a[0]);
|
|
52
|
+
curY = startY = map.y(a[1]);
|
|
53
|
+
open = true;
|
|
54
|
+
break;
|
|
55
|
+
case "lineTo":
|
|
56
|
+
lineTo(map.x(a[0]), map.y(a[1]));
|
|
57
|
+
break;
|
|
58
|
+
case "quadraticCurveTo": {
|
|
59
|
+
const n = QUAD_SEGMENTS;
|
|
60
|
+
const x0 = curX;
|
|
61
|
+
const y0 = curY;
|
|
62
|
+
const cx = map.x(a[0]);
|
|
63
|
+
const cy = map.y(a[1]);
|
|
64
|
+
const x2 = map.x(a[2]);
|
|
65
|
+
const y2 = map.y(a[3]);
|
|
66
|
+
for (let i = 1; i <= n; i++) {
|
|
67
|
+
const w0 = (n - i) * (n - i);
|
|
68
|
+
const w1 = 2 * i * (n - i);
|
|
69
|
+
const w2 = i * i;
|
|
70
|
+
lineTo(rdiv(w0 * x0 + w1 * cx + w2 * x2, n * n), rdiv(w0 * y0 + w1 * cy + w2 * y2, n * n));
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case "bezierCurveTo": {
|
|
75
|
+
const n = CUBIC_SEGMENTS;
|
|
76
|
+
const x0 = curX;
|
|
77
|
+
const y0 = curY;
|
|
78
|
+
const c1x = map.x(a[0]);
|
|
79
|
+
const c1y = map.y(a[1]);
|
|
80
|
+
const c2x = map.x(a[2]);
|
|
81
|
+
const c2y = map.y(a[3]);
|
|
82
|
+
const x3 = map.x(a[4]);
|
|
83
|
+
const y3 = map.y(a[5]);
|
|
84
|
+
for (let i = 1; i <= n; i++) {
|
|
85
|
+
const u = n - i;
|
|
86
|
+
const w0 = u * u * u;
|
|
87
|
+
const w1 = 3 * i * u * u;
|
|
88
|
+
const w2 = 3 * i * i * u;
|
|
89
|
+
const w3 = i * i * i;
|
|
90
|
+
lineTo(
|
|
91
|
+
rdiv(w0 * x0 + w1 * c1x + w2 * c2x + w3 * x3, n * n * n),
|
|
92
|
+
rdiv(w0 * y0 + w1 * c1y + w2 * c2y + w3 * y3, n * n * n)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "closePath":
|
|
98
|
+
close();
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
close();
|
|
103
|
+
return edges;
|
|
104
|
+
}
|
|
105
|
+
function maskTotals(cellW, cellH) {
|
|
106
|
+
const S = SUPERSAMPLE;
|
|
107
|
+
const subW = cellW * S;
|
|
108
|
+
const subH = cellH * S;
|
|
109
|
+
const totals = new Int32Array(64);
|
|
110
|
+
for (let sy = 0; sy < subH; sy++) {
|
|
111
|
+
const rowBase = idiv(sy * 8, subH) * 8;
|
|
112
|
+
for (let sx = 0; sx < subW; sx++) totals[rowBase + idiv(sx * 8, subW)]++;
|
|
113
|
+
}
|
|
114
|
+
return totals;
|
|
115
|
+
}
|
|
116
|
+
function rasterizeGlyph(edges, cellW, cellH) {
|
|
117
|
+
const S = SUPERSAMPLE;
|
|
118
|
+
const step = idiv(64, S);
|
|
119
|
+
const half = idiv(step, 2);
|
|
120
|
+
const subW = cellW * S;
|
|
121
|
+
const subH = cellH * S;
|
|
122
|
+
const hits = new Uint8Array(cellW * cellH);
|
|
123
|
+
const maskHits = new Int32Array(64);
|
|
124
|
+
let totalHits = 0;
|
|
125
|
+
if (edges.length === 0) return { hits, maskHits, totalHits };
|
|
126
|
+
for (let sy = 0; sy < subH; sy++) {
|
|
127
|
+
const y = sy * step + half;
|
|
128
|
+
const crossings = [];
|
|
129
|
+
for (let e = 0; e < edges.length; e += 4) {
|
|
130
|
+
const y0 = edges[e + 1];
|
|
131
|
+
const y1 = edges[e + 3];
|
|
132
|
+
const lo = Math.min(y0, y1);
|
|
133
|
+
const hi = Math.max(y0, y1);
|
|
134
|
+
if (y < lo || y >= hi) continue;
|
|
135
|
+
const x0 = edges[e];
|
|
136
|
+
const x1 = edges[e + 2];
|
|
137
|
+
crossings.push([x0 + fdiv((x1 - x0) * (y - y0), y1 - y0), y1 > y0 ? 1 : -1]);
|
|
138
|
+
}
|
|
139
|
+
if (crossings.length === 0) continue;
|
|
140
|
+
crossings.sort((a, b) => a[0] - b[0]);
|
|
141
|
+
const mRowBase = idiv(sy * 8, subH) * 8;
|
|
142
|
+
const pRow = idiv(sy, S) * cellW;
|
|
143
|
+
let ci = 0;
|
|
144
|
+
let winding = 0;
|
|
145
|
+
for (let sx = 0; sx < subW; sx++) {
|
|
146
|
+
const X = sx * step + half;
|
|
147
|
+
while (ci < crossings.length && crossings[ci][0] <= X) {
|
|
148
|
+
winding += crossings[ci][1];
|
|
149
|
+
ci++;
|
|
150
|
+
}
|
|
151
|
+
if (winding !== 0) {
|
|
152
|
+
hits[pRow + idiv(sx, S)]++;
|
|
153
|
+
maskHits[mRowBase + idiv(sx * 8, subW)]++;
|
|
154
|
+
totalHits++;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return { hits, maskHits, totalHits };
|
|
159
|
+
}
|
|
160
|
+
function deriveMask(maskHits, totals) {
|
|
161
|
+
let lo = 0;
|
|
162
|
+
let hi = 0;
|
|
163
|
+
for (let m = 0; m < 64; m++) {
|
|
164
|
+
if (2 * maskHits[m] >= totals[m]) {
|
|
165
|
+
if (m < 32) lo |= 1 << m;
|
|
166
|
+
else hi |= 1 << m - 32;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return { lo: lo >>> 0, hi: hi >>> 0 };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/profile.ts
|
|
173
|
+
import { createHash } from "crypto";
|
|
174
|
+
import { readFileSync } from "fs";
|
|
175
|
+
import {
|
|
176
|
+
encodeProfile,
|
|
177
|
+
idiv as idiv2,
|
|
178
|
+
nextPow2,
|
|
179
|
+
rdiv as rdiv2,
|
|
180
|
+
resolveCharset,
|
|
181
|
+
shape6BucketCenter
|
|
182
|
+
} from "@ascii-fx/core";
|
|
183
|
+
var COMPILER_VERSION = "0.1.0";
|
|
184
|
+
var COMPILER_PACKAGE_VERSION = JSON.parse(
|
|
185
|
+
readFileSync(new URL("../package.json", import.meta.url), "utf8")
|
|
186
|
+
).version;
|
|
187
|
+
var REF_GLYPH_HEIGHT = 64;
|
|
188
|
+
var ATLAS_PADDING = 4;
|
|
189
|
+
var sha256 = (data) => createHash("sha256").update(data).digest("hex");
|
|
190
|
+
function glyphShape6(lumas, out, at) {
|
|
191
|
+
let sum = 0;
|
|
192
|
+
let top = 0;
|
|
193
|
+
let left = 0;
|
|
194
|
+
let tl = 0;
|
|
195
|
+
let tr = 0;
|
|
196
|
+
let bl = 0;
|
|
197
|
+
let br = 0;
|
|
198
|
+
let center = 0;
|
|
199
|
+
for (let m = 0; m < 64; m++) {
|
|
200
|
+
const row = m >> 3;
|
|
201
|
+
const col = m & 7;
|
|
202
|
+
const v = lumas[m];
|
|
203
|
+
sum += v;
|
|
204
|
+
if (row < 4) top += v;
|
|
205
|
+
if (col < 4) left += v;
|
|
206
|
+
if (row < 4) {
|
|
207
|
+
if (col < 4) tl += v;
|
|
208
|
+
else tr += v;
|
|
209
|
+
} else if (col < 4) bl += v;
|
|
210
|
+
else br += v;
|
|
211
|
+
if (row >= 2 && row < 6 && col >= 2 && col < 6) center += v;
|
|
212
|
+
}
|
|
213
|
+
const v0 = sum / 64;
|
|
214
|
+
out[at] = v0;
|
|
215
|
+
out[at + 1] = top / 32 - (sum - top) / 32;
|
|
216
|
+
out[at + 2] = left / 32 - (sum - left) / 32;
|
|
217
|
+
out[at + 3] = (tl + br - tr - bl) / 32;
|
|
218
|
+
out[at + 4] = center / 16 - (sum - center) / 48;
|
|
219
|
+
let dev = 0;
|
|
220
|
+
for (let m = 0; m < 64; m++) dev += Math.abs(lumas[m] - v0);
|
|
221
|
+
out[at + 5] = dev / 64;
|
|
222
|
+
}
|
|
223
|
+
function buildProfile(options) {
|
|
224
|
+
const { name: charsetName, glyphs } = resolveCharset(options.charset, options.characters);
|
|
225
|
+
const lf = loadFont(options.font);
|
|
226
|
+
const missing = glyphs.filter((g) => !lf.font.hasGlyphForCodePoint(g.codePointAt(0)));
|
|
227
|
+
if (missing.length > 0) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
`Font "${lf.familyName ?? "unknown"}" has no glyph for: ${missing.map(
|
|
230
|
+
(c) => `${JSON.stringify(c)} (U+${c.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")})`
|
|
231
|
+
).join(", ")}. Remove them from the charset or pick a font that covers them.`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
const fontGlyphs = glyphs.map((g) => lf.font.glyphForCodePoint(g.codePointAt(0)));
|
|
235
|
+
const advance = fontGlyphs[0].advanceWidth;
|
|
236
|
+
const proportional = glyphs.filter((_, i) => fontGlyphs[i].advanceWidth !== advance);
|
|
237
|
+
if (proportional.length > 0) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
`Font "${lf.familyName ?? "unknown"}" is not monospace over this charset \u2014 differing advances for: ${proportional.slice(0, 8).map((c) => JSON.stringify(c)).join(", ")}${proportional.length > 8 ? ", \u2026" : ""}. v1 requires a monospace font.`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
const unitsCell = lf.ascent - lf.descent;
|
|
243
|
+
if (!(unitsCell > 0))
|
|
244
|
+
throw new Error("Font reports non-positive ascent\u2212descent; cannot derive a cell box.");
|
|
245
|
+
const cellH = REF_GLYPH_HEIGHT;
|
|
246
|
+
const cellW = Math.max(1, rdiv2(advance * REF_GLYPH_HEIGHT, unitsCell));
|
|
247
|
+
const baseline = rdiv2(lf.ascent * REF_GLYPH_HEIGHT, unitsCell);
|
|
248
|
+
const baselineFx = rdiv2(lf.ascent * REF_GLYPH_HEIGHT * 64, unitsCell);
|
|
249
|
+
const map = {
|
|
250
|
+
x: (v) => rdiv2(v * REF_GLYPH_HEIGHT * 64, unitsCell),
|
|
251
|
+
y: (v) => baselineFx - rdiv2(v * REF_GLYPH_HEIGHT * 64, unitsCell)
|
|
252
|
+
};
|
|
253
|
+
const n = glyphs.length;
|
|
254
|
+
const masksLo = new Uint32Array(n);
|
|
255
|
+
const masksHi = new Uint32Array(n);
|
|
256
|
+
const coverage = new Uint16Array(n);
|
|
257
|
+
const pitchW = nextPow2(cellW + 2 * ATLAS_PADDING);
|
|
258
|
+
const pitchH = nextPow2(cellH + 2 * ATLAS_PADDING);
|
|
259
|
+
let atlasColumns = 1;
|
|
260
|
+
while (atlasColumns * atlasColumns < n) atlasColumns++;
|
|
261
|
+
const atlasRows = idiv2(n + atlasColumns - 1, atlasColumns);
|
|
262
|
+
const atlasW = atlasColumns * pitchW;
|
|
263
|
+
const atlasH = atlasRows * pitchH;
|
|
264
|
+
const atlasData = new Uint8Array(atlasW * atlasH);
|
|
265
|
+
const totals = maskTotals(cellW, cellH);
|
|
266
|
+
const subsamplesPerCell = cellW * cellH * SUPERSAMPLE * SUPERSAMPLE;
|
|
267
|
+
const wantShape6 = options.shape6 !== void 0 && options.shape6 !== false;
|
|
268
|
+
const cellLumas = wantShape6 ? new Float64Array(n * 64) : void 0;
|
|
269
|
+
for (let i = 0; i < n; i++) {
|
|
270
|
+
const edges = flattenCommands(fontGlyphs[i].path.commands, map);
|
|
271
|
+
const raster = rasterizeGlyph(edges, cellW, cellH);
|
|
272
|
+
const mask = deriveMask(raster.maskHits, totals);
|
|
273
|
+
masksLo[i] = mask.lo;
|
|
274
|
+
masksHi[i] = mask.hi;
|
|
275
|
+
coverage[i] = rdiv2(raster.totalHits * 65535, subsamplesPerCell);
|
|
276
|
+
if (cellLumas) {
|
|
277
|
+
for (let m = 0; m < 64; m++) cellLumas[i * 64 + m] = raster.maskHits[m] / totals[m] * 255;
|
|
278
|
+
}
|
|
279
|
+
const tileX = i % atlasColumns * pitchW + ATLAS_PADDING;
|
|
280
|
+
const tileY = idiv2(i, atlasColumns) * pitchH + ATLAS_PADDING;
|
|
281
|
+
for (let py = 0; py < cellH; py++) {
|
|
282
|
+
const dst = (tileY + py) * atlasW + tileX;
|
|
283
|
+
const src = py * cellW;
|
|
284
|
+
for (let px = 0; px < cellW; px++) {
|
|
285
|
+
atlasData[dst + px] = rdiv2(raster.hits[src + px] * 255, SUPERSAMPLE * SUPERSAMPLE);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
let shape6;
|
|
290
|
+
if (cellLumas) {
|
|
291
|
+
const vectors6 = new Float32Array(n * 6);
|
|
292
|
+
for (let i = 0; i < n; i++)
|
|
293
|
+
glyphShape6(cellLumas.subarray(i * 64, i * 64 + 64), vectors6, i * 6);
|
|
294
|
+
let lut3;
|
|
295
|
+
if (typeof options.shape6 === "object" && options.shape6.lut) {
|
|
296
|
+
lut3 = new Uint16Array(8 ** 6);
|
|
297
|
+
const center = new Float64Array(6);
|
|
298
|
+
for (let idx = 0; idx < lut3.length; idx++) {
|
|
299
|
+
shape6BucketCenter(idx, center);
|
|
300
|
+
let best = 0;
|
|
301
|
+
let bestD = Infinity;
|
|
302
|
+
for (let g = 0; g < n; g++) {
|
|
303
|
+
let d = 0;
|
|
304
|
+
for (let c = 0; c < 6; c++) {
|
|
305
|
+
const e = center[c] - vectors6[g * 6 + c];
|
|
306
|
+
d += e * e;
|
|
307
|
+
}
|
|
308
|
+
if (d < bestD) {
|
|
309
|
+
bestD = d;
|
|
310
|
+
best = g;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
lut3[idx] = best;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
shape6 = { vectors6, lut3 };
|
|
317
|
+
}
|
|
318
|
+
const fontHash = sha256(options.font);
|
|
319
|
+
const charsetHash = sha256(glyphs.join("\0"));
|
|
320
|
+
const fingerprint = sha256(
|
|
321
|
+
`asciip/1|structural-v1|raster-v1|font:${fontHash}|charset:${charsetHash}|H:${REF_GLYPH_HEIGHT}|S:${SUPERSAMPLE}|pad:${ATLAS_PADDING}|quad:${QUAD_SEGMENTS}|cubic:${CUBIC_SEGMENTS}`
|
|
322
|
+
);
|
|
323
|
+
const profile = {
|
|
324
|
+
version: 1,
|
|
325
|
+
id: options.id ?? "default",
|
|
326
|
+
fingerprint,
|
|
327
|
+
charsetHash,
|
|
328
|
+
fontHash,
|
|
329
|
+
glyphs,
|
|
330
|
+
glyphCount: n,
|
|
331
|
+
metrics: {
|
|
332
|
+
unitsPerEm: lf.unitsPerEm,
|
|
333
|
+
ascent: lf.ascent,
|
|
334
|
+
descent: lf.descent,
|
|
335
|
+
lineGap: lf.lineGap,
|
|
336
|
+
advanceUnits: advance,
|
|
337
|
+
cellWidth: cellW,
|
|
338
|
+
cellHeight: cellH,
|
|
339
|
+
baseline
|
|
340
|
+
},
|
|
341
|
+
atlas: {
|
|
342
|
+
width: atlasW,
|
|
343
|
+
height: atlasH,
|
|
344
|
+
pitchWidth: pitchW,
|
|
345
|
+
pitchHeight: pitchH,
|
|
346
|
+
cellWidth: cellW,
|
|
347
|
+
cellHeight: cellH,
|
|
348
|
+
padding: ATLAS_PADDING,
|
|
349
|
+
columns: atlasColumns,
|
|
350
|
+
data: atlasData
|
|
351
|
+
},
|
|
352
|
+
structural: { masksLo, masksHi, coverage },
|
|
353
|
+
shape6,
|
|
354
|
+
metadata: {
|
|
355
|
+
id: options.id ?? "default",
|
|
356
|
+
charset: charsetName,
|
|
357
|
+
fontFamily: lf.familyName,
|
|
358
|
+
fontWeight: lf.weight,
|
|
359
|
+
compilerVersion: COMPILER_VERSION
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
return { profile, binary: encodeProfile(profile) };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// src/frameBuild.ts
|
|
366
|
+
import { encodeFrame, matchFrame } from "@ascii-fx/core";
|
|
367
|
+
function buildFrame(options) {
|
|
368
|
+
const { image, ...match } = options;
|
|
369
|
+
const frame = matchFrame(image, match);
|
|
370
|
+
return { frame, binary: encodeFrame(frame) };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// src/png.ts
|
|
374
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
375
|
+
import { PNG } from "pngjs";
|
|
376
|
+
function decodePng(bytes) {
|
|
377
|
+
const png = PNG.sync.read(Buffer2.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
|
|
378
|
+
return {
|
|
379
|
+
width: png.width,
|
|
380
|
+
height: png.height,
|
|
381
|
+
data: new Uint8Array(png.data.buffer, png.data.byteOffset, png.data.length)
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export {
|
|
386
|
+
loadFont,
|
|
387
|
+
SUPERSAMPLE,
|
|
388
|
+
QUAD_SEGMENTS,
|
|
389
|
+
CUBIC_SEGMENTS,
|
|
390
|
+
flattenCommands,
|
|
391
|
+
maskTotals,
|
|
392
|
+
rasterizeGlyph,
|
|
393
|
+
deriveMask,
|
|
394
|
+
COMPILER_VERSION,
|
|
395
|
+
COMPILER_PACKAGE_VERSION,
|
|
396
|
+
REF_GLYPH_HEIGHT,
|
|
397
|
+
ATLAS_PADDING,
|
|
398
|
+
sha256,
|
|
399
|
+
buildProfile,
|
|
400
|
+
buildFrame,
|
|
401
|
+
decodePng
|
|
402
|
+
};
|
|
403
|
+
//# sourceMappingURL=chunk-ZQML5MF3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/font.ts","../src/raster.ts","../src/profile.ts","../src/frameBuild.ts","../src/png.ts"],"sourcesContent":["import { Buffer } from 'node:buffer'\nimport * as FK from 'fontkit'\nimport type { Font } from 'fontkit'\n\nexport interface LoadedFont {\n font: Font\n unitsPerEm: number\n ascent: number\n descent: number\n lineGap: number\n familyName?: string\n weight?: number\n}\n\nexport function loadFont(bytes: Uint8Array): LoadedFont {\n const create =\n (FK as { create?: typeof FK.create }).create ??\n (FK as { default?: { create: typeof FK.create } }).default?.create\n if (!create) throw new Error('fontkit did not expose create(); incompatible fontkit version.')\n const font = create(Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength))\n if (font.fonts && font.fonts.length > 0) {\n throw new Error('Font collections (.ttc/.otc) are not supported; extract a single face first.')\n }\n return {\n font,\n unitsPerEm: font.unitsPerEm,\n ascent: font.ascent,\n descent: font.descent,\n lineGap: font.lineGap,\n familyName: font.familyName ?? font.postscriptName,\n weight: font['OS/2']?.usWeightClass,\n }\n}\n","// raster-v1 (ALGORITHM.md §13): deterministic fixed-point (26.6) scanline\n// rasterizer. No system rasterizer may replace this — profile bytes must be\n// identical across platforms.\n\nimport { fdiv, idiv, rdiv } from '@ascii-fx/core'\nimport type { FontPathCommand } from 'fontkit'\n\nexport const SUPERSAMPLE = 4\nexport const QUAD_SEGMENTS = 16\nexport const CUBIC_SEGMENTS = 24\n\n/** Font-unit → 26.6 pixel-space mappers (y-down). */\nexport interface FxMapping {\n x(v: number): number\n y(v: number): number\n}\n\n/** Flatten outline commands to non-horizontal line segments [x0,y0,x1,y1, ...] in 26.6. */\nexport function flattenCommands(commands: readonly FontPathCommand[], map: FxMapping): number[] {\n const edges: number[] = []\n let curX = 0\n let curY = 0\n let startX = 0\n let startY = 0\n let open = false\n\n const push = (x0: number, y0: number, x1: number, y1: number): void => {\n if (y0 !== y1) edges.push(x0, y0, x1, y1)\n }\n const lineTo = (x: number, y: number): void => {\n push(curX, curY, x, y)\n curX = x\n curY = y\n }\n const close = (): void => {\n if (open && (curX !== startX || curY !== startY)) push(curX, curY, startX, startY)\n open = false\n }\n\n for (const cmd of commands) {\n const a = cmd.args\n switch (cmd.command) {\n case 'moveTo':\n close()\n curX = startX = map.x(a[0])\n curY = startY = map.y(a[1])\n open = true\n break\n case 'lineTo':\n lineTo(map.x(a[0]), map.y(a[1]))\n break\n case 'quadraticCurveTo': {\n const n = QUAD_SEGMENTS\n const x0 = curX\n const y0 = curY\n const cx = map.x(a[0])\n const cy = map.y(a[1])\n const x2 = map.x(a[2])\n const y2 = map.y(a[3])\n for (let i = 1; i <= n; i++) {\n const w0 = (n - i) * (n - i)\n const w1 = 2 * i * (n - i)\n const w2 = i * i\n lineTo(rdiv(w0 * x0 + w1 * cx + w2 * x2, n * n), rdiv(w0 * y0 + w1 * cy + w2 * y2, n * n))\n }\n break\n }\n case 'bezierCurveTo': {\n const n = CUBIC_SEGMENTS\n const x0 = curX\n const y0 = curY\n const c1x = map.x(a[0])\n const c1y = map.y(a[1])\n const c2x = map.x(a[2])\n const c2y = map.y(a[3])\n const x3 = map.x(a[4])\n const y3 = map.y(a[5])\n for (let i = 1; i <= n; i++) {\n const u = n - i\n const w0 = u * u * u\n const w1 = 3 * i * u * u\n const w2 = 3 * i * i * u\n const w3 = i * i * i\n lineTo(\n rdiv(w0 * x0 + w1 * c1x + w2 * c2x + w3 * x3, n * n * n),\n rdiv(w0 * y0 + w1 * c1y + w2 * c2y + w3 * y3, n * n * n),\n )\n }\n break\n }\n case 'closePath':\n close()\n break\n }\n }\n close()\n return edges\n}\n\nexport interface GlyphRaster {\n /** Per-pixel subsample hit counts, cellW·cellH, each 0..SUPERSAMPLE². */\n hits: Uint8Array\n /** Subsample hits per 8×8 mask cell. */\n maskHits: Int32Array\n totalHits: number\n}\n\n/** Subsample totals per 8×8 mask cell — identical for every glyph of a given cell size. */\nexport function maskTotals(cellW: number, cellH: number): Int32Array {\n const S = SUPERSAMPLE\n const subW = cellW * S\n const subH = cellH * S\n const totals = new Int32Array(64)\n for (let sy = 0; sy < subH; sy++) {\n const rowBase = idiv(sy * 8, subH) * 8\n for (let sx = 0; sx < subW; sx++) totals[rowBase + idiv(sx * 8, subW)]++\n }\n return totals\n}\n\n/** Scanline fill (non-zero winding) over subsample centers. */\nexport function rasterizeGlyph(\n edges: readonly number[],\n cellW: number,\n cellH: number,\n): GlyphRaster {\n const S = SUPERSAMPLE\n const step = idiv(64, S) // 26.6 units between subsample centers\n const half = idiv(step, 2)\n const subW = cellW * S\n const subH = cellH * S\n const hits = new Uint8Array(cellW * cellH)\n const maskHits = new Int32Array(64)\n let totalHits = 0\n if (edges.length === 0) return { hits, maskHits, totalHits }\n\n for (let sy = 0; sy < subH; sy++) {\n const y = sy * step + half\n const crossings: Array<[number, number]> = []\n for (let e = 0; e < edges.length; e += 4) {\n const y0 = edges[e + 1]\n const y1 = edges[e + 3]\n const lo = Math.min(y0, y1)\n const hi = Math.max(y0, y1)\n if (y < lo || y >= hi) continue\n const x0 = edges[e]\n const x1 = edges[e + 2]\n crossings.push([x0 + fdiv((x1 - x0) * (y - y0), y1 - y0), y1 > y0 ? 1 : -1])\n }\n if (crossings.length === 0) continue\n crossings.sort((a, b) => a[0] - b[0])\n const mRowBase = idiv(sy * 8, subH) * 8\n const pRow = idiv(sy, S) * cellW\n let ci = 0\n let winding = 0\n for (let sx = 0; sx < subW; sx++) {\n const X = sx * step + half\n while (ci < crossings.length && crossings[ci][0] <= X) {\n winding += crossings[ci][1]\n ci++\n }\n if (winding !== 0) {\n hits[pRow + idiv(sx, S)]++\n maskHits[mRowBase + idiv(sx * 8, subW)]++\n totalHits++\n }\n }\n }\n return { hits, maskHits, totalHits }\n}\n\n/** Mask bit = 1 ⇔ 2·hits ≥ total (tie → ink). Returns 64-bit mask as lo/hi u32. */\nexport function deriveMask(maskHits: Int32Array, totals: Int32Array): { lo: number; hi: number } {\n let lo = 0\n let hi = 0\n for (let m = 0; m < 64; m++) {\n if (2 * maskHits[m] >= totals[m]) {\n if (m < 32) lo |= 1 << m\n else hi |= 1 << (m - 32)\n }\n }\n return { lo: lo >>> 0, hi: hi >>> 0 }\n}\n","import { createHash } from 'node:crypto'\nimport { readFileSync } from 'node:fs'\nimport type { AsciiProfile, Shape6GlyphData } from '@ascii-fx/core'\nimport {\n encodeProfile,\n idiv,\n nextPow2,\n rdiv,\n resolveCharset,\n shape6BucketCenter,\n} from '@ascii-fx/core'\nimport { loadFont } from './font.js'\nimport {\n CUBIC_SEGMENTS,\n QUAD_SEGMENTS,\n SUPERSAMPLE,\n deriveMask,\n flattenCommands,\n maskTotals,\n rasterizeGlyph,\n} from './raster.js'\n\nexport const COMPILER_VERSION = '0.1.0'\n\n/**\n * The @ascii-fx/compiler version actually installed, read from its own manifest so it cannot\n * drift the way a constant can. Every @ascii-fx/* package releases in one fixed group with\n * exact internal pins, so this moves on any release that could change output bytes — which is\n * what makes it a sound build-cache salt (@ascii-fx/vite keys on it). Deliberately separate\n * from COMPILER_VERSION: that string is embedded in profile metadata, where following the\n * package version would change profile bytes and break the golden fixtures on every release.\n */\nexport const COMPILER_PACKAGE_VERSION: string = JSON.parse(\n readFileSync(new URL('../package.json', import.meta.url), 'utf8'),\n).version\nexport const REF_GLYPH_HEIGHT = 64\nexport const ATLAS_PADDING = 4\n\nexport interface BuildProfileOptions {\n /** Raw font bytes (ttf/otf/woff/woff2). */\n font: Uint8Array\n id?: string\n /** Built-in charset name; default 'ascii'. */\n charset?: string\n /** Custom characters (overrides charset). */\n characters?: string | readonly string[]\n /**\n * Compile shape6-v1 data (ALGORITHM.md §18): 6D glyph vectors, and with\n * `{ lut: true }` the 512KiB dense winner LUT. Opt-in — it grows the profile\n * and only the explicit `matcher: 'shape6'` uses it (spec §5–6).\n */\n shape6?: boolean | { lut?: boolean }\n}\n\nexport interface BuiltProfile {\n profile: AsciiProfile\n binary: Uint8Array\n}\n\nexport const sha256 = (data: Uint8Array | string): string =>\n createHash('sha256').update(data).digest('hex')\n\n/** Glyph-side shape6 vector (ALGORITHM.md §18): float64 means over mask-cell lumas. */\nfunction glyphShape6(lumas: Float64Array, out: Float32Array, at: number): void {\n let sum = 0\n let top = 0\n let left = 0\n let tl = 0\n let tr = 0\n let bl = 0\n let br = 0\n let center = 0\n for (let m = 0; m < 64; m++) {\n const row = m >> 3\n const col = m & 7\n const v = lumas[m]\n sum += v\n if (row < 4) top += v\n if (col < 4) left += v\n if (row < 4) {\n if (col < 4) tl += v\n else tr += v\n } else if (col < 4) bl += v\n else br += v\n if (row >= 2 && row < 6 && col >= 2 && col < 6) center += v\n }\n const v0 = sum / 64\n out[at] = v0\n out[at + 1] = top / 32 - (sum - top) / 32\n out[at + 2] = left / 32 - (sum - left) / 32\n out[at + 3] = (tl + br - tr - bl) / 32\n out[at + 4] = center / 16 - (sum - center) / 48\n let dev = 0\n for (let m = 0; m < 64; m++) dev += Math.abs(lumas[m] - v0)\n out[at + 5] = dev / 64\n}\n\n/** Build a deterministic .asciip profile (ALGORITHM.md §13–14). */\nexport function buildProfile(options: BuildProfileOptions): BuiltProfile {\n const { name: charsetName, glyphs } = resolveCharset(options.charset, options.characters)\n const lf = loadFont(options.font)\n\n const missing = glyphs.filter((g) => !lf.font.hasGlyphForCodePoint(g.codePointAt(0)!))\n if (missing.length > 0) {\n throw new Error(\n `Font \"${lf.familyName ?? 'unknown'}\" has no glyph for: ${missing\n .map(\n (c) =>\n `${JSON.stringify(c)} (U+${c.codePointAt(0)!.toString(16).toUpperCase().padStart(4, '0')})`,\n )\n .join(', ')}. Remove them from the charset or pick a font that covers them.`,\n )\n }\n\n const fontGlyphs = glyphs.map((g) => lf.font.glyphForCodePoint(g.codePointAt(0)!))\n const advance = fontGlyphs[0].advanceWidth\n const proportional = glyphs.filter((_, i) => fontGlyphs[i].advanceWidth !== advance)\n if (proportional.length > 0) {\n throw new Error(\n `Font \"${lf.familyName ?? 'unknown'}\" is not monospace over this charset — differing advances for: ${proportional\n .slice(0, 8)\n .map((c) => JSON.stringify(c))\n .join(', ')}${proportional.length > 8 ? ', …' : ''}. v1 requires a monospace font.`,\n )\n }\n\n const unitsCell = lf.ascent - lf.descent\n if (!(unitsCell > 0))\n throw new Error('Font reports non-positive ascent−descent; cannot derive a cell box.')\n const cellH = REF_GLYPH_HEIGHT\n const cellW = Math.max(1, rdiv(advance * REF_GLYPH_HEIGHT, unitsCell))\n const baseline = rdiv(lf.ascent * REF_GLYPH_HEIGHT, unitsCell)\n const baselineFx = rdiv(lf.ascent * REF_GLYPH_HEIGHT * 64, unitsCell)\n const map = {\n x: (v: number) => rdiv(v * REF_GLYPH_HEIGHT * 64, unitsCell),\n y: (v: number) => baselineFx - rdiv(v * REF_GLYPH_HEIGHT * 64, unitsCell),\n }\n\n const n = glyphs.length\n const masksLo = new Uint32Array(n)\n const masksHi = new Uint32Array(n)\n const coverage = new Uint16Array(n)\n\n const pitchW = nextPow2(cellW + 2 * ATLAS_PADDING)\n const pitchH = nextPow2(cellH + 2 * ATLAS_PADDING)\n let atlasColumns = 1\n while (atlasColumns * atlasColumns < n) atlasColumns++\n const atlasRows = idiv(n + atlasColumns - 1, atlasColumns)\n const atlasW = atlasColumns * pitchW\n const atlasH = atlasRows * pitchH\n const atlasData = new Uint8Array(atlasW * atlasH)\n\n const totals = maskTotals(cellW, cellH)\n const subsamplesPerCell = cellW * cellH * SUPERSAMPLE * SUPERSAMPLE\n const wantShape6 = options.shape6 !== undefined && options.shape6 !== false\n const cellLumas = wantShape6 ? new Float64Array(n * 64) : undefined\n\n for (let i = 0; i < n; i++) {\n const edges = flattenCommands(fontGlyphs[i].path.commands, map)\n const raster = rasterizeGlyph(edges, cellW, cellH)\n const mask = deriveMask(raster.maskHits, totals)\n masksLo[i] = mask.lo\n masksHi[i] = mask.hi\n coverage[i] = rdiv(raster.totalHits * 65535, subsamplesPerCell)\n if (cellLumas) {\n for (let m = 0; m < 64; m++) cellLumas[i * 64 + m] = (raster.maskHits[m] / totals[m]) * 255\n }\n const tileX = (i % atlasColumns) * pitchW + ATLAS_PADDING\n const tileY = idiv(i, atlasColumns) * pitchH + ATLAS_PADDING\n for (let py = 0; py < cellH; py++) {\n const dst = (tileY + py) * atlasW + tileX\n const src = py * cellW\n for (let px = 0; px < cellW; px++) {\n atlasData[dst + px] = rdiv(raster.hits[src + px] * 255, SUPERSAMPLE * SUPERSAMPLE)\n }\n }\n }\n\n let shape6: Shape6GlyphData | undefined\n if (cellLumas) {\n const vectors6 = new Float32Array(n * 6)\n for (let i = 0; i < n; i++)\n glyphShape6(cellLumas.subarray(i * 64, i * 64 + 64), vectors6, i * 6)\n let lut3: Uint16Array | undefined\n if (typeof options.shape6 === 'object' && options.shape6.lut) {\n lut3 = new Uint16Array(8 ** 6)\n const center = new Float64Array(6)\n for (let idx = 0; idx < lut3.length; idx++) {\n shape6BucketCenter(idx, center)\n let best = 0\n let bestD = Infinity\n for (let g = 0; g < n; g++) {\n let d = 0\n for (let c = 0; c < 6; c++) {\n const e = center[c] - vectors6[g * 6 + c]\n d += e * e\n }\n if (d < bestD) {\n bestD = d\n best = g\n }\n }\n lut3[idx] = best\n }\n }\n shape6 = { vectors6, lut3 }\n }\n\n const fontHash = sha256(options.font)\n const charsetHash = sha256(glyphs.join('\\u0000'))\n const fingerprint = sha256(\n `asciip/1|structural-v1|raster-v1|font:${fontHash}|charset:${charsetHash}` +\n `|H:${REF_GLYPH_HEIGHT}|S:${SUPERSAMPLE}|pad:${ATLAS_PADDING}|quad:${QUAD_SEGMENTS}|cubic:${CUBIC_SEGMENTS}`,\n )\n\n const profile: AsciiProfile = {\n version: 1,\n id: options.id ?? 'default',\n fingerprint,\n charsetHash,\n fontHash,\n glyphs,\n glyphCount: n,\n metrics: {\n unitsPerEm: lf.unitsPerEm,\n ascent: lf.ascent,\n descent: lf.descent,\n lineGap: lf.lineGap,\n advanceUnits: advance,\n cellWidth: cellW,\n cellHeight: cellH,\n baseline,\n },\n atlas: {\n width: atlasW,\n height: atlasH,\n pitchWidth: pitchW,\n pitchHeight: pitchH,\n cellWidth: cellW,\n cellHeight: cellH,\n padding: ATLAS_PADDING,\n columns: atlasColumns,\n data: atlasData,\n },\n structural: { masksLo, masksHi, coverage },\n shape6,\n metadata: {\n id: options.id ?? 'default',\n charset: charsetName,\n fontFamily: lf.familyName,\n fontWeight: lf.weight,\n compilerVersion: COMPILER_VERSION,\n },\n }\n\n return { profile, binary: encodeProfile(profile) }\n}\n","import type { AsciiFrame, AsciiProfile, MatchOptions, RawImage } from '@ascii-fx/core'\nimport { encodeFrame, matchFrame } from '@ascii-fx/core'\n\nexport interface BuildFrameOptions extends Omit<MatchOptions, 'profile'> {\n image: RawImage\n profile: AsciiProfile\n}\n\nexport interface BuiltFrame {\n frame: AsciiFrame\n binary: Uint8Array\n}\n\n/** Build-time static frame: run the exact matcher now so the client never has to. */\nexport function buildFrame(options: BuildFrameOptions): BuiltFrame {\n const { image, ...match } = options\n const frame = matchFrame(image, match)\n return { frame, binary: encodeFrame(frame) }\n}\n","import { Buffer } from 'node:buffer'\nimport { PNG } from 'pngjs'\nimport type { RawImage } from '@ascii-fx/core'\n\nexport function decodePng(bytes: Uint8Array): RawImage {\n const png = PNG.sync.read(Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength))\n return {\n width: png.width,\n height: png.height,\n data: new Uint8Array(png.data.buffer, png.data.byteOffset, png.data.length),\n }\n}\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,YAAY,QAAQ;AAab,SAAS,SAAS,OAA+B;AACtD,QAAMA,UACkC,aACa,YAAS;AAC9D,MAAI,CAACA,QAAQ,OAAM,IAAI,MAAM,gEAAgE;AAC7F,QAAM,OAAOA,QAAO,OAAO,KAAK,MAAM,QAAQ,MAAM,YAAY,MAAM,UAAU,CAAC;AACjF,MAAI,KAAK,SAAS,KAAK,MAAM,SAAS,GAAG;AACvC,UAAM,IAAI,MAAM,8EAA8E;AAAA,EAChG;AACA,SAAO;AAAA,IACL;AAAA,IACA,YAAY,KAAK;AAAA,IACjB,QAAQ,KAAK;AAAA,IACb,SAAS,KAAK;AAAA,IACd,SAAS,KAAK;AAAA,IACd,YAAY,KAAK,cAAc,KAAK;AAAA,IACpC,QAAQ,KAAK,MAAM,GAAG;AAAA,EACxB;AACF;;;AC5BA,SAAS,MAAM,MAAM,YAAY;AAG1B,IAAM,cAAc;AACpB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AASvB,SAAS,gBAAgB,UAAsC,KAA0B;AAC9F,QAAM,QAAkB,CAAC;AACzB,MAAI,OAAO;AACX,MAAI,OAAO;AACX,MAAI,SAAS;AACb,MAAI,SAAS;AACb,MAAI,OAAO;AAEX,QAAM,OAAO,CAAC,IAAY,IAAY,IAAY,OAAqB;AACrE,QAAI,OAAO,GAAI,OAAM,KAAK,IAAI,IAAI,IAAI,EAAE;AAAA,EAC1C;AACA,QAAM,SAAS,CAAC,GAAW,MAAoB;AAC7C,SAAK,MAAM,MAAM,GAAG,CAAC;AACrB,WAAO;AACP,WAAO;AAAA,EACT;AACA,QAAM,QAAQ,MAAY;AACxB,Q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AG;AAAA,MACf,YAAY,GAAG;AAAA,MACf,iBAAiB;AAAA,IACnB;AAAA,EACF;AAEA,SAAO,EAAE,SAAS,QAAQ,cAAc,OAAO,EAAE;AACnD;;;AC/PA,SAAS,aAAa,kBAAkB;AAajC,SAAS,WAAW,SAAwC;AACjE,QAAM,EAAE,OAAO,GAAG,MAAM,IAAI;AAC5B,QAAM,QAAQ,WAAW,OAAO,KAAK;AACrC,SAAO,EAAE,OAAO,QAAQ,YAAY,KAAK,EAAE;AAC7C;;;AClBA,SAAS,UAAAE,eAAc;AACvB,SAAS,WAAW;AAGb,SAAS,UAAU,OAA6B;AACrD,QAAM,MAAM,IAAI,KAAK,KAAKA,QAAO,KAAK,MAAM,QAAQ,MAAM,YAAY,MAAM,UAAU,CAAC;AACvF,SAAO;AAAA,IACL,OAAO,IAAI;AAAA,IACX,QAAQ,IAAI;AAAA,IACZ,MAAM,IAAI,WAAW,IAAI,KAAK,QAAQ,IAAI,KAAK,YAAY,IAAI,KAAK,MAAM;AAAA,EAC5E;AACF;","names":["create","idiv","rdiv","rdiv","idiv","Buffer"]}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
buildFrame,
|
|
4
|
+
buildProfile,
|
|
5
|
+
decodePng
|
|
6
|
+
} from "./chunk-ZQML5MF3.js";
|
|
7
|
+
|
|
8
|
+
// src/cli.ts
|
|
9
|
+
import { readFile, writeFile } from "fs/promises";
|
|
10
|
+
import process from "process";
|
|
11
|
+
import { decodeProfile, peekFrame } from "@ascii-fx/core";
|
|
12
|
+
|
|
13
|
+
// src/cliArgs.ts
|
|
14
|
+
function parseArgs(argv) {
|
|
15
|
+
const positional = [];
|
|
16
|
+
const flags = /* @__PURE__ */ new Map();
|
|
17
|
+
for (let i = 0; i < argv.length; i++) {
|
|
18
|
+
const a = argv[i];
|
|
19
|
+
if (a.startsWith("--")) {
|
|
20
|
+
const key = a.slice(2);
|
|
21
|
+
const next = argv[i + 1];
|
|
22
|
+
if (next !== void 0 && !next.startsWith("--")) {
|
|
23
|
+
flags.set(key, next);
|
|
24
|
+
i++;
|
|
25
|
+
} else {
|
|
26
|
+
flags.set(key, true);
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
positional.push(a);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return { positional, flags };
|
|
33
|
+
}
|
|
34
|
+
var str = (args, key) => {
|
|
35
|
+
const v = args.flags.get(key);
|
|
36
|
+
return typeof v === "string" ? v : void 0;
|
|
37
|
+
};
|
|
38
|
+
function enumFlag(key, value, allowed) {
|
|
39
|
+
if (value === void 0) return void 0;
|
|
40
|
+
if (allowed.includes(value)) return value;
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Invalid --${key} ${JSON.stringify(value)}: expected one of ${allowed.join(" | ")}.`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
function positiveIntFlag(key, value) {
|
|
46
|
+
if (value === void 0) return void 0;
|
|
47
|
+
const n = Number(value);
|
|
48
|
+
if (!Number.isInteger(n) || n <= 0) {
|
|
49
|
+
throw new Error(`Invalid --${key} ${JSON.stringify(value)}: expected a positive integer.`);
|
|
50
|
+
}
|
|
51
|
+
return n;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/cli.ts
|
|
55
|
+
var USAGE = `ascii-fx \u2014 ASCII FX build-time compiler
|
|
56
|
+
|
|
57
|
+
Usage:
|
|
58
|
+
ascii-fx profile build --font <file> --out <file.asciip> [--charset ascii] [--characters "<chars>"] [--id default]
|
|
59
|
+
[--shape6] [--shape6-lut]
|
|
60
|
+
ascii-fx frame build --image <file.png> --profile <file.asciip> --out <file.asciif>
|
|
61
|
+
[--columns N] [--rows N] [--color mono|foreground|full] [--alpha mask|ignore]
|
|
62
|
+
ascii-fx inspect <file.asciip | file.asciif>
|
|
63
|
+
`;
|
|
64
|
+
var requireStr = (args, key) => {
|
|
65
|
+
const v = str(args, key);
|
|
66
|
+
if (v === void 0) {
|
|
67
|
+
console.error(`Missing required --${key}
|
|
68
|
+
|
|
69
|
+
${USAGE}`);
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
return v;
|
|
73
|
+
};
|
|
74
|
+
async function main() {
|
|
75
|
+
const args = parseArgs(process.argv.slice(2));
|
|
76
|
+
const [cmd, sub] = args.positional;
|
|
77
|
+
if (cmd === "profile" && sub === "build") {
|
|
78
|
+
const fontPath = requireStr(args, "font");
|
|
79
|
+
const outPath = requireStr(args, "out");
|
|
80
|
+
const font = new Uint8Array(await readFile(fontPath));
|
|
81
|
+
const characters = str(args, "characters");
|
|
82
|
+
const { profile, binary } = buildProfile({
|
|
83
|
+
font,
|
|
84
|
+
id: str(args, "id") ?? "default",
|
|
85
|
+
charset: str(args, "charset"),
|
|
86
|
+
characters,
|
|
87
|
+
shape6: args.flags.get("shape6-lut") === true ? { lut: true } : args.flags.get("shape6") === true
|
|
88
|
+
});
|
|
89
|
+
await writeFile(outPath, binary);
|
|
90
|
+
console.log(
|
|
91
|
+
`${outPath}: ${profile.glyphCount} glyphs (${profile.metadata.charset}), cell ${profile.atlas.cellWidth}\xD7${profile.atlas.cellHeight}, atlas ${profile.atlas.width}\xD7${profile.atlas.height}, ${binary.length} bytes, fingerprint ${profile.fingerprint.slice(0, 12)}\u2026`
|
|
92
|
+
);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (cmd === "frame" && sub === "build") {
|
|
96
|
+
const imagePath = requireStr(args, "image");
|
|
97
|
+
const profilePath = requireStr(args, "profile");
|
|
98
|
+
const outPath = requireStr(args, "out");
|
|
99
|
+
if (!imagePath.toLowerCase().endsWith(".png")) {
|
|
100
|
+
console.error("frame build currently decodes PNG only.");
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
const profile = decodeProfile(new Uint8Array(await readFile(profilePath)));
|
|
104
|
+
const image = decodePng(new Uint8Array(await readFile(imagePath)));
|
|
105
|
+
const { frame, binary } = buildFrame({
|
|
106
|
+
image,
|
|
107
|
+
profile,
|
|
108
|
+
columns: positiveIntFlag("columns", str(args, "columns")),
|
|
109
|
+
rows: positiveIntFlag("rows", str(args, "rows")),
|
|
110
|
+
color: enumFlag("color", str(args, "color"), ["mono", "foreground", "full"]) ?? "mono",
|
|
111
|
+
alpha: enumFlag("alpha", str(args, "alpha"), ["mask", "ignore"])
|
|
112
|
+
});
|
|
113
|
+
await writeFile(outPath, binary);
|
|
114
|
+
console.log(
|
|
115
|
+
`${outPath}: ${frame.columns}\xD7${frame.rows} cells, ${frame.colorMode}, ${binary.length} bytes`
|
|
116
|
+
);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (cmd === "inspect") {
|
|
120
|
+
const file = args.positional[1];
|
|
121
|
+
if (!file) {
|
|
122
|
+
console.error(USAGE);
|
|
123
|
+
process.exit(1);
|
|
124
|
+
}
|
|
125
|
+
const bytes = new Uint8Array(await readFile(file));
|
|
126
|
+
if (bytes[3] === 73) {
|
|
127
|
+
const p = decodeProfile(bytes);
|
|
128
|
+
console.log(
|
|
129
|
+
JSON.stringify(
|
|
130
|
+
{
|
|
131
|
+
format: "asciip/1",
|
|
132
|
+
id: p.id,
|
|
133
|
+
charset: p.metadata.charset,
|
|
134
|
+
fontFamily: p.metadata.fontFamily,
|
|
135
|
+
glyphCount: p.glyphCount,
|
|
136
|
+
cell: `${p.atlas.cellWidth}\xD7${p.atlas.cellHeight}`,
|
|
137
|
+
baseline: p.metrics.baseline,
|
|
138
|
+
atlas: `${p.atlas.width}\xD7${p.atlas.height} (pitch ${p.atlas.pitchWidth}\xD7${p.atlas.pitchHeight})`,
|
|
139
|
+
fingerprint: p.fingerprint,
|
|
140
|
+
fontHash: p.fontHash,
|
|
141
|
+
charsetHash: p.charsetHash,
|
|
142
|
+
compilerVersion: p.metadata.compilerVersion,
|
|
143
|
+
bytes: bytes.length
|
|
144
|
+
},
|
|
145
|
+
null,
|
|
146
|
+
2
|
|
147
|
+
)
|
|
148
|
+
);
|
|
149
|
+
} else {
|
|
150
|
+
const m = peekFrame(bytes);
|
|
151
|
+
console.log(JSON.stringify({ format: "asciif/1", ...m, bytes: bytes.length }, null, 2));
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
console.error(USAGE);
|
|
156
|
+
process.exit(cmd === void 0 ? 0 : 1);
|
|
157
|
+
}
|
|
158
|
+
main().catch((err) => {
|
|
159
|
+
console.error(err instanceof Error ? err.message : err);
|
|
160
|
+
process.exit(1);
|
|
161
|
+
});
|
|
162
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts","../src/cliArgs.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { readFile, writeFile } from 'node:fs/promises'\nimport process from 'node:process'\nimport { decodeProfile, peekFrame } from '@ascii-fx/core'\nimport { type Args, enumFlag, parseArgs, positiveIntFlag, str } from './cliArgs.js'\nimport { buildProfile } from './profile.js'\nimport { buildFrame } from './frameBuild.js'\nimport { decodePng } from './png.js'\n\nconst USAGE = `ascii-fx — ASCII FX build-time compiler\n\nUsage:\n ascii-fx profile build --font <file> --out <file.asciip> [--charset ascii] [--characters \"<chars>\"] [--id default]\n [--shape6] [--shape6-lut]\n ascii-fx frame build --image <file.png> --profile <file.asciip> --out <file.asciif>\n [--columns N] [--rows N] [--color mono|foreground|full] [--alpha mask|ignore]\n ascii-fx inspect <file.asciip | file.asciif>\n`\n\nconst requireStr = (args: Args, key: string): string => {\n const v = str(args, key)\n if (v === undefined) {\n console.error(`Missing required --${key}\\n\\n${USAGE}`)\n process.exit(1)\n }\n return v\n}\n\nasync function main(): Promise<void> {\n const args = parseArgs(process.argv.slice(2))\n const [cmd, sub] = args.positional\n\n if (cmd === 'profile' && sub === 'build') {\n const fontPath = requireStr(args, 'font')\n const outPath = requireStr(args, 'out')\n const font = new Uint8Array(await readFile(fontPath))\n const characters = str(args, 'characters')\n const { profile, binary } = buildProfile({\n font,\n id: str(args, 'id') ?? 'default',\n charset: str(args, 'charset'),\n characters,\n shape6:\n args.flags.get('shape6-lut') === true ? { lut: true } : args.flags.get('shape6') === true,\n })\n await writeFile(outPath, binary)\n console.log(\n `${outPath}: ${profile.glyphCount} glyphs (${profile.metadata.charset}), ` +\n `cell ${profile.atlas.cellWidth}×${profile.atlas.cellHeight}, atlas ${profile.atlas.width}×${profile.atlas.height}, ` +\n `${binary.length} bytes, fingerprint ${profile.fingerprint.slice(0, 12)}…`,\n )\n return\n }\n\n if (cmd === 'frame' && sub === 'build') {\n const imagePath = requireStr(args, 'image')\n const profilePath = requireStr(args, 'profile')\n const outPath = requireStr(args, 'out')\n if (!imagePath.toLowerCase().endsWith('.png')) {\n console.error('frame build currently decodes PNG only.')\n process.exit(1)\n }\n const profile = decodeProfile(new Uint8Array(await readFile(profilePath)))\n const image = decodePng(new Uint8Array(await readFile(imagePath)))\n const { frame, binary } = buildFrame({\n image,\n profile,\n columns: positiveIntFlag('columns', str(args, 'columns')),\n rows: positiveIntFlag('rows', str(args, 'rows')),\n color:\n enumFlag('color', str(args, 'color'), ['mono', 'foreground', 'full'] as const) ?? 'mono',\n alpha: enumFlag('alpha', str(args, 'alpha'), ['mask', 'ignore'] as const),\n })\n await writeFile(outPath, binary)\n console.log(\n `${outPath}: ${frame.columns}×${frame.rows} cells, ${frame.colorMode}, ${binary.length} bytes`,\n )\n return\n }\n\n if (cmd === 'inspect') {\n const file = args.positional[1]\n if (!file) {\n console.error(USAGE)\n process.exit(1)\n }\n const bytes = new Uint8Array(await readFile(file))\n if (bytes[3] === 0x49) {\n const p = decodeProfile(bytes)\n console.log(\n JSON.stringify(\n {\n format: 'asciip/1',\n id: p.id,\n charset: p.metadata.charset,\n fontFamily: p.metadata.fontFamily,\n glyphCount: p.glyphCount,\n cell: `${p.atlas.cellWidth}×${p.atlas.cellHeight}`,\n baseline: p.metrics.baseline,\n atlas: `${p.atlas.width}×${p.atlas.height} (pitch ${p.atlas.pitchWidth}×${p.atlas.pitchHeight})`,\n fingerprint: p.fingerprint,\n fontHash: p.fontHash,\n charsetHash: p.charsetHash,\n compilerVersion: p.metadata.compilerVersion,\n bytes: bytes.length,\n },\n null,\n 2,\n ),\n )\n } else {\n const m = peekFrame(bytes)\n console.log(JSON.stringify({ format: 'asciif/1', ...m, bytes: bytes.length }, null, 2))\n }\n return\n }\n\n console.error(USAGE)\n process.exit(cmd === undefined ? 0 : 1)\n}\n\nmain().catch((err: unknown) => {\n console.error(err instanceof Error ? err.message : err)\n process.exit(1)\n})\n","/**\n * CLI argument parsing and validation, separate from cli.ts so it is testable\n * without spawning the built binary (the node suite runs before `pnpm build`\n * in CI, so dist/cli.js may not exist when tests do).\n *\n * Validators throw plain Errors; cli.ts's top-level catch prints the message\n * and exits 1. They exist because the underlying builders do not reject what\n * they cannot handle: an unknown --color used to encode a silently wrong\n * artifact and a non-numeric --columns an unloadable one, both with exit 0.\n */\n\nexport interface Args {\n positional: string[]\n flags: Map<string, string | true>\n}\n\nexport function parseArgs(argv: string[]): Args {\n const positional: string[] = []\n const flags = new Map<string, string | true>()\n for (let i = 0; i < argv.length; i++) {\n const a = argv[i]\n if (a.startsWith('--')) {\n const key = a.slice(2)\n const next = argv[i + 1]\n if (next !== undefined && !next.startsWith('--')) {\n flags.set(key, next)\n i++\n } else {\n flags.set(key, true)\n }\n } else {\n positional.push(a)\n }\n }\n return { positional, flags }\n}\n\nexport const str = (args: Args, key: string): string | undefined => {\n const v = args.flags.get(key)\n return typeof v === 'string' ? v : undefined\n}\n\n/** An enum-valued flag: anything outside `allowed` is an error, never a guess. */\nexport function enumFlag<T extends string>(\n key: string,\n value: string | undefined,\n allowed: readonly T[],\n): T | undefined {\n if (value === undefined) return undefined\n if ((allowed as readonly string[]).includes(value)) return value as T\n throw new Error(\n `Invalid --${key} ${JSON.stringify(value)}: expected one of ${allowed.join(' | ')}.`,\n )\n}\n\n/** A grid-dimension flag: positive integers only — NaN and fractions reach typed-array sizes otherwise. */\nexport function positiveIntFlag(key: string, value: string | undefined): number | undefined {\n if (value === undefined) return undefined\n const n = Number(value)\n if (!Number.isInteger(n) || n <= 0) {\n throw new Error(`Invalid --${key} ${JSON.stringify(value)}: expected a positive integer.`)\n }\n return n\n}\n"],"mappings":";;;;;;;;AACA,SAAS,UAAU,iBAAiB;AACpC,OAAO,aAAa;AACpB,SAAS,eAAe,iBAAiB;;;ACalC,SAAS,UAAU,MAAsB;AAC9C,QAAM,aAAuB,CAAC;AAC9B,QAAM,QAAQ,oBAAI,IAA2B;AAC7C,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,UAAM,IAAI,KAAK,CAAC;AAChB,QAAI,EAAE,WAAW,IAAI,GAAG;AACtB,YAAM,MAAM,EAAE,MAAM,CAAC;AACrB,YAAM,OAAO,KAAK,IAAI,CAAC;AACvB,UAAI,SAAS,UAAa,CAAC,KAAK,WAAW,IAAI,GAAG;AAChD,cAAM,IAAI,KAAK,IAAI;AACnB;AAAA,MACF,OAAO;AACL,cAAM,IAAI,KAAK,IAAI;AAAA,MACrB;AAAA,IACF,OAAO;AACL,iBAAW,KAAK,CAAC;AAAA,IACnB;AAAA,EACF;AACA,SAAO,EAAE,YAAY,MAAM;AAC7B;AAEO,IAAM,MAAM,CAAC,MAAY,QAAoC;AAClE,QAAM,IAAI,KAAK,MAAM,IAAI,GAAG;AAC5B,SAAO,OAAO,MAAM,WAAW,IAAI;AACrC;AAGO,SAAS,SACd,KACA,OACA,SACe;AACf,MAAI,UAAU,OAAW,QAAO;AAChC,MAAK,QAA8B,SAAS,KAAK,EAAG,QAAO;AAC3D,QAAM,IAAI;AAAA,IACR,aAAa,GAAG,IAAI,KAAK,UAAU,KAAK,CAAC,qBAAqB,QAAQ,KAAK,KAAK,CAAC;AAAA,EACnF;AACF;AAGO,SAAS,gBAAgB,KAAa,OAA+C;AAC1F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AC;AAAA,IAChB;AACA,UAAM,QAAQ,IAAI,WAAW,MAAM,SAAS,IAAI,CAAC;AACjD,QAAI,MAAM,CAAC,MAAM,IAAM;AACrB,YAAM,IAAI,cAAc,KAAK;AAC7B,cAAQ;AAAA,QACN,KAAK;AAAA,UACH;AAAA,YACE,QAAQ;AAAA,YACR,IAAI,EAAE;AAAA,YACN,SAAS,EAAE,SAAS;AAAA,YACpB,YAAY,EAAE,SAAS;AAAA,YACvB,YAAY,EAAE;AAAA,YACd,MAAM,GAAG,EAAE,MAAM,SAAS,OAAI,EAAE,MAAM,UAAU;AAAA,YAChD,UAAU,EAAE,QAAQ;AAAA,YACpB,OAAO,GAAG,EAAE,MAAM,KAAK,OAAI,EAAE,MAAM,MAAM,WAAW,EAAE,MAAM,UAAU,OAAI,EAAE,MAAM,WAAW;AAAA,YAC7F,aAAa,EAAE;AAAA,YACf,UAAU,EAAE;AAAA,YACZ,aAAa,EAAE;AAAA,YACf,iBAAiB,EAAE,SAAS;AAAA,YAC5B,OAAO,MAAM;AAAA,UACf;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF,OAAO;AACL,YAAM,IAAI,UAAU,KAAK;AACzB,cAAQ,IAAI,KAAK,UAAU,EAAE,QAAQ,YAAY,GAAG,GAAG,OAAO,MAAM,OAAO,GAAG,MAAM,CAAC,CAAC;AAAA,IACxF;AACA;AAAA,EACF;AAEA,UAAQ,MAAM,KAAK;AACnB,UAAQ,KAAK,QAAQ,SAAY,IAAI,CAAC;AACxC;AAEA,KAAK,EAAE,MAAM,CAAC,QAAiB;AAC7B,UAAQ,MAAM,eAAe,QAAQ,IAAI,UAAU,GAAG;AACtD,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":[]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI argument parsing and validation, separate from cli.ts so it is testable
|
|
3
|
+
* without spawning the built binary (the node suite runs before `pnpm build`
|
|
4
|
+
* in CI, so dist/cli.js may not exist when tests do).
|
|
5
|
+
*
|
|
6
|
+
* Validators throw plain Errors; cli.ts's top-level catch prints the message
|
|
7
|
+
* and exits 1. They exist because the underlying builders do not reject what
|
|
8
|
+
* they cannot handle: an unknown --color used to encode a silently wrong
|
|
9
|
+
* artifact and a non-numeric --columns an unloadable one, both with exit 0.
|
|
10
|
+
*/
|
|
11
|
+
export interface Args {
|
|
12
|
+
positional: string[];
|
|
13
|
+
flags: Map<string, string | true>;
|
|
14
|
+
}
|
|
15
|
+
export declare function parseArgs(argv: string[]): Args;
|
|
16
|
+
export declare const str: (args: Args, key: string) => string | undefined;
|
|
17
|
+
/** An enum-valued flag: anything outside `allowed` is an error, never a guess. */
|
|
18
|
+
export declare function enumFlag<T extends string>(key: string, value: string | undefined, allowed: readonly T[]): T | undefined;
|
|
19
|
+
/** A grid-dimension flag: positive integers only — NaN and fractions reach typed-array sizes otherwise. */
|
|
20
|
+
export declare function positiveIntFlag(key: string, value: string | undefined): number | undefined;
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AlphaMode, ColorMode } from '@ascii-fx/core';
|
|
2
|
+
export interface AsciiProfileConfig {
|
|
3
|
+
/** Path to the font file, relative to the config file. */
|
|
4
|
+
font: string;
|
|
5
|
+
charset?: string;
|
|
6
|
+
characters?: string | string[];
|
|
7
|
+
/** Compile shape6-v1 data (ALGORITHM.md §18); `{ lut: true }` adds the dense LUT. */
|
|
8
|
+
shape6?: boolean | {
|
|
9
|
+
lut?: boolean;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** Static build-time frame (spec §16). Matching happens at build, not runtime. */
|
|
13
|
+
export interface AsciiFrameConfig {
|
|
14
|
+
/** Path to the source image (PNG in v1), relative to the config file. */
|
|
15
|
+
image: string;
|
|
16
|
+
/** Profile name from `profiles`. Default 'default'. */
|
|
17
|
+
profile?: string;
|
|
18
|
+
columns?: number;
|
|
19
|
+
rows?: number;
|
|
20
|
+
color?: ColorMode;
|
|
21
|
+
alpha?: AlphaMode;
|
|
22
|
+
}
|
|
23
|
+
export interface AsciiConfig {
|
|
24
|
+
profiles: Record<string, AsciiProfileConfig>;
|
|
25
|
+
frames?: Record<string, AsciiFrameConfig>;
|
|
26
|
+
}
|
|
27
|
+
export declare function defineAsciiConfig(config: AsciiConfig): AsciiConfig;
|
package/dist/font.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Font } from 'fontkit';
|
|
2
|
+
export interface LoadedFont {
|
|
3
|
+
font: Font;
|
|
4
|
+
unitsPerEm: number;
|
|
5
|
+
ascent: number;
|
|
6
|
+
descent: number;
|
|
7
|
+
lineGap: number;
|
|
8
|
+
familyName?: string;
|
|
9
|
+
weight?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function loadFont(bytes: Uint8Array): LoadedFont;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare module 'fontkit' {
|
|
2
|
+
export interface FontPathCommand {
|
|
3
|
+
command: 'moveTo' | 'lineTo' | 'quadraticCurveTo' | 'bezierCurveTo' | 'closePath'
|
|
4
|
+
args: number[]
|
|
5
|
+
}
|
|
6
|
+
export interface FontPath {
|
|
7
|
+
commands: FontPathCommand[]
|
|
8
|
+
}
|
|
9
|
+
export interface FontGlyph {
|
|
10
|
+
advanceWidth: number
|
|
11
|
+
path: FontPath
|
|
12
|
+
}
|
|
13
|
+
export interface Font {
|
|
14
|
+
unitsPerEm: number
|
|
15
|
+
ascent: number
|
|
16
|
+
descent: number
|
|
17
|
+
lineGap: number
|
|
18
|
+
familyName?: string
|
|
19
|
+
postscriptName?: string
|
|
20
|
+
'OS/2'?: { usWeightClass?: number }
|
|
21
|
+
fonts?: Font[]
|
|
22
|
+
hasGlyphForCodePoint(codePoint: number): boolean
|
|
23
|
+
glyphForCodePoint(codePoint: number): FontGlyph
|
|
24
|
+
}
|
|
25
|
+
export function create(buffer: Uint8Array): Font
|
|
26
|
+
const fontkit: { create(buffer: Uint8Array): Font }
|
|
27
|
+
export default fontkit
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AsciiFrame, AsciiProfile, MatchOptions, RawImage } from '@ascii-fx/core';
|
|
2
|
+
export interface BuildFrameOptions extends Omit<MatchOptions, 'profile'> {
|
|
3
|
+
image: RawImage;
|
|
4
|
+
profile: AsciiProfile;
|
|
5
|
+
}
|
|
6
|
+
export interface BuiltFrame {
|
|
7
|
+
frame: AsciiFrame;
|
|
8
|
+
binary: Uint8Array;
|
|
9
|
+
}
|
|
10
|
+
/** Build-time static frame: run the exact matcher now so the client never has to. */
|
|
11
|
+
export declare function buildFrame(options: BuildFrameOptions): BuiltFrame;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference path="./fontkit.d.ts" />
|
|
2
|
+
export { ATLAS_PADDING, COMPILER_PACKAGE_VERSION, COMPILER_VERSION, REF_GLYPH_HEIGHT, buildProfile, type BuildProfileOptions, type BuiltProfile, } from './profile.js';
|
|
3
|
+
export { buildFrame, type BuildFrameOptions, type BuiltFrame } from './frameBuild.js';
|
|
4
|
+
export { decodePng } from './png.js';
|
|
5
|
+
export { loadFont, type LoadedFont } from './font.js';
|
|
6
|
+
export { CUBIC_SEGMENTS, QUAD_SEGMENTS, SUPERSAMPLE, deriveMask, flattenCommands, maskTotals, rasterizeGlyph, type FxMapping, type GlyphRaster, } from './raster.js';
|
|
7
|
+
export { defineAsciiConfig, type AsciiConfig, type AsciiFrameConfig, type AsciiProfileConfig, } from './config.js';
|
|
8
|
+
export { buildChromaticProfile, CHROMATIC_CELL, CHROMATIC_RASTER_VERSION, type BuildChromaticOptions, type BuiltChromaticProfile, type ChromaticGlyphSource, } from './chromatic.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ATLAS_PADDING,
|
|
3
|
+
COMPILER_PACKAGE_VERSION,
|
|
4
|
+
COMPILER_VERSION,
|
|
5
|
+
CUBIC_SEGMENTS,
|
|
6
|
+
QUAD_SEGMENTS,
|
|
7
|
+
REF_GLYPH_HEIGHT,
|
|
8
|
+
SUPERSAMPLE,
|
|
9
|
+
buildFrame,
|
|
10
|
+
buildProfile,
|
|
11
|
+
decodePng,
|
|
12
|
+
deriveMask,
|
|
13
|
+
flattenCommands,
|
|
14
|
+
loadFont,
|
|
15
|
+
maskTotals,
|
|
16
|
+
rasterizeGlyph,
|
|
17
|
+
sha256
|
|
18
|
+
} from "./chunk-ZQML5MF3.js";
|
|
19
|
+
|
|
20
|
+
// src/config.ts
|
|
21
|
+
function defineAsciiConfig(config) {
|
|
22
|
+
return config;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/chromatic.ts
|
|
26
|
+
import { encodeProfile, idiv, nextPow2, rdiv } from "@ascii-fx/core";
|
|
27
|
+
var ATLAS_PADDING2 = 4;
|
|
28
|
+
var CHROMATIC_CELL = 24;
|
|
29
|
+
var CHROMATIC_RASTER_VERSION = "raster-chromatic-v1";
|
|
30
|
+
function boxFilter(img, tw, th) {
|
|
31
|
+
const { width: w, height: h, data } = img;
|
|
32
|
+
const out = new Uint8Array(tw * th * 4);
|
|
33
|
+
for (let ty = 0; ty < th; ty++) {
|
|
34
|
+
const y0 = idiv(ty * h, th);
|
|
35
|
+
const y1 = Math.max(y0 + 1, idiv((ty + 1) * h, th));
|
|
36
|
+
for (let tx = 0; tx < tw; tx++) {
|
|
37
|
+
const x0 = idiv(tx * w, tw);
|
|
38
|
+
const x1 = Math.max(x0 + 1, idiv((tx + 1) * w, tw));
|
|
39
|
+
let sr = 0;
|
|
40
|
+
let sg = 0;
|
|
41
|
+
let sb = 0;
|
|
42
|
+
let sa = 0;
|
|
43
|
+
let count = 0;
|
|
44
|
+
for (let y = y0; y < y1; y++) {
|
|
45
|
+
for (let x = x0; x < x1; x++) {
|
|
46
|
+
const p = (y * w + x) * 4;
|
|
47
|
+
const a = data[p + 3];
|
|
48
|
+
sr += data[p] * a;
|
|
49
|
+
sg += data[p + 1] * a;
|
|
50
|
+
sb += data[p + 2] * a;
|
|
51
|
+
sa += a;
|
|
52
|
+
count++;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const q = (ty * tw + tx) * 4;
|
|
56
|
+
if (sa === 0) {
|
|
57
|
+
out[q] = 0;
|
|
58
|
+
out[q + 1] = 0;
|
|
59
|
+
out[q + 2] = 0;
|
|
60
|
+
out[q + 3] = 0;
|
|
61
|
+
} else {
|
|
62
|
+
out[q] = rdiv(sr, sa);
|
|
63
|
+
out[q + 1] = rdiv(sg, sa);
|
|
64
|
+
out[q + 2] = rdiv(sb, sa);
|
|
65
|
+
out[q + 3] = rdiv(sa, count);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function buildChromaticProfile(options) {
|
|
72
|
+
const sources = options.glyphs;
|
|
73
|
+
const n = sources.length;
|
|
74
|
+
if (n === 0) throw new Error("buildChromaticProfile needs at least one glyph.");
|
|
75
|
+
if (n > 65536)
|
|
76
|
+
throw new Error(`Chromatic profiles hold at most 65536 glyphs (got ${n}); glyph ids are u16.`);
|
|
77
|
+
const seen = /* @__PURE__ */ new Set();
|
|
78
|
+
for (const g of sources) {
|
|
79
|
+
if (g.char.length === 0) throw new Error("A chromatic glyph has an empty character.");
|
|
80
|
+
if (seen.has(g.char)) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Duplicate chromatic glyph ${JSON.stringify(g.char)}. Each grapheme may appear once.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
seen.add(g.char);
|
|
86
|
+
if (g.image.width < 1 || g.image.height < 1) {
|
|
87
|
+
throw new Error(`Glyph ${JSON.stringify(g.char)} has an empty image.`);
|
|
88
|
+
}
|
|
89
|
+
if (g.image.data.length !== g.image.width * g.image.height * 4) {
|
|
90
|
+
throw new Error(`Glyph ${JSON.stringify(g.char)} image data is not width * height * 4.`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const cell = options.cellSize ?? CHROMATIC_CELL;
|
|
94
|
+
if (!Number.isInteger(cell) || cell < 8)
|
|
95
|
+
throw new Error(`cellSize must be an integer >= 8 (got ${cell}).`);
|
|
96
|
+
const pitch = nextPow2(cell + 2 * ATLAS_PADDING2);
|
|
97
|
+
let atlasColumns = 1;
|
|
98
|
+
while (atlasColumns * atlasColumns < n) atlasColumns++;
|
|
99
|
+
const atlasRows = idiv(n + atlasColumns - 1, atlasColumns);
|
|
100
|
+
const atlasW = atlasColumns * pitch;
|
|
101
|
+
const atlasH = atlasRows * pitch;
|
|
102
|
+
const coverageAtlas = new Uint8Array(atlasW * atlasH);
|
|
103
|
+
const rgbaAtlas = new Uint8Array(atlasW * atlasH * 4);
|
|
104
|
+
const samples = new Uint8Array(n * 256);
|
|
105
|
+
const masksLo = new Uint32Array(n);
|
|
106
|
+
const masksHi = new Uint32Array(n);
|
|
107
|
+
const coverage = new Uint16Array(n);
|
|
108
|
+
for (let i = 0; i < n; i++) {
|
|
109
|
+
const tile = boxFilter(sources[i].image, cell, cell);
|
|
110
|
+
const tileX = i % atlasColumns * pitch + ATLAS_PADDING2;
|
|
111
|
+
const tileY = idiv(i, atlasColumns) * pitch + ATLAS_PADDING2;
|
|
112
|
+
let totalAlpha = 0;
|
|
113
|
+
for (let y = 0; y < cell; y++) {
|
|
114
|
+
for (let x = 0; x < cell; x++) {
|
|
115
|
+
const src = (y * cell + x) * 4;
|
|
116
|
+
const dst = (tileY + y) * atlasW + tileX + x;
|
|
117
|
+
rgbaAtlas[dst * 4] = tile[src];
|
|
118
|
+
rgbaAtlas[dst * 4 + 1] = tile[src + 1];
|
|
119
|
+
rgbaAtlas[dst * 4 + 2] = tile[src + 2];
|
|
120
|
+
rgbaAtlas[dst * 4 + 3] = tile[src + 3];
|
|
121
|
+
coverageAtlas[dst] = tile[src + 3];
|
|
122
|
+
totalAlpha += tile[src + 3];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
coverage[i] = rdiv(totalAlpha * 65535, cell * cell * 255);
|
|
126
|
+
const small = boxFilter(sources[i].image, 8, 8);
|
|
127
|
+
samples.set(small, i * 256);
|
|
128
|
+
let lo = 0;
|
|
129
|
+
let hi = 0;
|
|
130
|
+
for (let k = 0; k < 64; k++) {
|
|
131
|
+
if (small[k * 4 + 3] >= 128) {
|
|
132
|
+
if (k < 32) lo |= 1 << k;
|
|
133
|
+
else hi |= 1 << k - 32;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
masksLo[i] = lo >>> 0;
|
|
137
|
+
masksHi[i] = hi >>> 0;
|
|
138
|
+
}
|
|
139
|
+
const glyphs = sources.map((g) => g.char);
|
|
140
|
+
let total = 0;
|
|
141
|
+
for (const g of sources) total += g.image.data.length;
|
|
142
|
+
const imageBytes = new Uint8Array(total);
|
|
143
|
+
let at = 0;
|
|
144
|
+
for (const g of sources) {
|
|
145
|
+
imageBytes.set(
|
|
146
|
+
g.image.data instanceof Uint8Array ? g.image.data : new Uint8Array(g.image.data),
|
|
147
|
+
at
|
|
148
|
+
);
|
|
149
|
+
at += g.image.data.length;
|
|
150
|
+
}
|
|
151
|
+
const sourceHash = sha256(imageBytes);
|
|
152
|
+
const charsetHash = sha256(glyphs.join(" "));
|
|
153
|
+
const fingerprint = sha256(
|
|
154
|
+
`asciip/1|chromatic-v1|${CHROMATIC_RASTER_VERSION}|images:${sourceHash}|charset:${charsetHash}|cell:${cell}|pad:${ATLAS_PADDING2}`
|
|
155
|
+
);
|
|
156
|
+
const profile = {
|
|
157
|
+
version: 1,
|
|
158
|
+
id: options.id ?? "chromatic",
|
|
159
|
+
fingerprint,
|
|
160
|
+
charsetHash,
|
|
161
|
+
fontHash: sourceHash,
|
|
162
|
+
glyphs,
|
|
163
|
+
glyphCount: n,
|
|
164
|
+
// A chromatic cell is square and has no baseline; these describe that box so
|
|
165
|
+
// grid-v1 derives square cells without the matcher special-casing anything.
|
|
166
|
+
metrics: {
|
|
167
|
+
unitsPerEm: cell,
|
|
168
|
+
ascent: cell,
|
|
169
|
+
descent: 0,
|
|
170
|
+
lineGap: 0,
|
|
171
|
+
advanceUnits: cell,
|
|
172
|
+
cellWidth: cell,
|
|
173
|
+
cellHeight: cell,
|
|
174
|
+
baseline: cell
|
|
175
|
+
},
|
|
176
|
+
atlas: {
|
|
177
|
+
width: atlasW,
|
|
178
|
+
height: atlasH,
|
|
179
|
+
pitchWidth: pitch,
|
|
180
|
+
pitchHeight: pitch,
|
|
181
|
+
cellWidth: cell,
|
|
182
|
+
cellHeight: cell,
|
|
183
|
+
padding: ATLAS_PADDING2,
|
|
184
|
+
columns: atlasColumns,
|
|
185
|
+
data: coverageAtlas,
|
|
186
|
+
rgba: rgbaAtlas
|
|
187
|
+
},
|
|
188
|
+
structural: { masksLo, masksHi, coverage },
|
|
189
|
+
chromatic: { samples },
|
|
190
|
+
metadata: {
|
|
191
|
+
id: options.id ?? "chromatic",
|
|
192
|
+
charset: "chromatic",
|
|
193
|
+
compilerVersion: COMPILER_VERSION
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
return { profile, binary: encodeProfile(profile) };
|
|
197
|
+
}
|
|
198
|
+
export {
|
|
199
|
+
ATLAS_PADDING,
|
|
200
|
+
CHROMATIC_CELL,
|
|
201
|
+
CHROMATIC_RASTER_VERSION,
|
|
202
|
+
COMPILER_PACKAGE_VERSION,
|
|
203
|
+
COMPILER_VERSION,
|
|
204
|
+
CUBIC_SEGMENTS,
|
|
205
|
+
QUAD_SEGMENTS,
|
|
206
|
+
REF_GLYPH_HEIGHT,
|
|
207
|
+
SUPERSAMPLE,
|
|
208
|
+
buildChromaticProfile,
|
|
209
|
+
buildFrame,
|
|
210
|
+
buildProfile,
|
|
211
|
+
decodePng,
|
|
212
|
+
defineAsciiConfig,
|
|
213
|
+
deriveMask,
|
|
214
|
+
flattenCommands,
|
|
215
|
+
loadFont,
|
|
216
|
+
maskTotals,
|
|
217
|
+
rasterizeGlyph
|
|
218
|
+
};
|
|
219
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/config.ts","../src/chromatic.ts"],"sourcesContent":["import type { AlphaMode, ColorMode } from '@ascii-fx/core'\n\nexport interface AsciiProfileConfig {\n /** Path to the font file, relative to the config file. */\n font: string\n charset?: string\n characters?: string | string[]\n /** Compile shape6-v1 data (ALGORITHM.md §18); `{ lut: true }` adds the dense LUT. */\n shape6?: boolean | { lut?: boolean }\n}\n\n/** Static build-time frame (spec §16). Matching happens at build, not runtime. */\nexport interface AsciiFrameConfig {\n /** Path to the source image (PNG in v1), relative to the config file. */\n image: string\n /** Profile name from `profiles`. Default 'default'. */\n profile?: string\n columns?: number\n rows?: number\n color?: ColorMode\n alpha?: AlphaMode\n}\n\nexport interface AsciiConfig {\n profiles: Record<string, AsciiProfileConfig>\n frames?: Record<string, AsciiFrameConfig>\n}\n\nexport function defineAsciiConfig(config: AsciiConfig): AsciiConfig {\n return config\n}\n","// Builds chromatic-v1 profiles (ALGORITHM.md §C) from decoded colour images\n// rather than from a font. There is no rasterization step: colour emoji ship as\n// PNG assets (Noto, Twemoji, Fluent) or as bitmap strikes inside a font, so the\n// deterministic path is decode-then-box-filter, not outline flattening. That\n// makes raster-v1 (§13) inapplicable here by construction, not by omission.\n\nimport type { AsciiProfile, RawImage } from '@ascii-fx/core'\nimport { encodeProfile, idiv, nextPow2, rdiv } from '@ascii-fx/core'\nimport { COMPILER_VERSION, sha256 } from './profile.js'\n\nconst ATLAS_PADDING = 4\n/**\n * Reference cell for chromatic profiles. Square, unlike a text cell, and sized\n * so `cell + 2 * ATLAS_PADDING` is already a power of two: the pow2 pitch rule\n * (§13) exists so padding survives mip levels, and any other cell size rounds\n * up and wastes the difference. 32 would round 40 up to 64 and throw away 61%\n * of the atlas; 24 fits 32 exactly. The efficient sizes are 24, 56, and 120.\n */\nexport const CHROMATIC_CELL = 24\nexport const CHROMATIC_RASTER_VERSION = 'raster-chromatic-v1'\n\nexport interface ChromaticGlyphSource {\n /** The grapheme this glyph draws; may be several code points (ZWJ, VS16, modifiers). */\n char: string\n image: RawImage\n}\n\nexport interface BuildChromaticOptions {\n glyphs: readonly ChromaticGlyphSource[]\n /** Reference cell size in px, square. Default CHROMATIC_CELL (24). */\n cellSize?: number\n id?: string\n}\n\nexport interface BuiltChromaticProfile {\n profile: AsciiProfile\n binary: Uint8Array\n}\n\n/**\n * Alpha-weighted box filter, the same rule as reduce-v1 (§4): colour is\n * weighted by alpha so transparent pixels cannot drag the mean toward whatever\n * RGB happens to sit under them, while alpha itself is a plain mean.\n */\nfunction boxFilter(img: RawImage, tw: number, th: number): Uint8Array {\n const { width: w, height: h, data } = img\n const out = new Uint8Array(tw * th * 4)\n for (let ty = 0; ty < th; ty++) {\n const y0 = idiv(ty * h, th)\n const y1 = Math.max(y0 + 1, idiv((ty + 1) * h, th))\n for (let tx = 0; tx < tw; tx++) {\n const x0 = idiv(tx * w, tw)\n const x1 = Math.max(x0 + 1, idiv((tx + 1) * w, tw))\n let sr = 0\n let sg = 0\n let sb = 0\n let sa = 0\n let count = 0\n for (let y = y0; y < y1; y++) {\n for (let x = x0; x < x1; x++) {\n const p = (y * w + x) * 4\n const a = data[p + 3]\n sr += data[p] * a\n sg += data[p + 1] * a\n sb += data[p + 2] * a\n sa += a\n count++\n }\n }\n const q = (ty * tw + tx) * 4\n if (sa === 0) {\n out[q] = 0\n out[q + 1] = 0\n out[q + 2] = 0\n out[q + 3] = 0\n } else {\n out[q] = rdiv(sr, sa)\n out[q + 1] = rdiv(sg, sa)\n out[q + 2] = rdiv(sb, sa)\n out[q + 3] = rdiv(sa, count)\n }\n }\n }\n return out\n}\n\nexport function buildChromaticProfile(options: BuildChromaticOptions): BuiltChromaticProfile {\n const sources = options.glyphs\n const n = sources.length\n if (n === 0) throw new Error('buildChromaticProfile needs at least one glyph.')\n if (n > 65536)\n throw new Error(`Chromatic profiles hold at most 65536 glyphs (got ${n}); glyph ids are u16.`)\n\n const seen = new Set<string>()\n for (const g of sources) {\n if (g.char.length === 0) throw new Error('A chromatic glyph has an empty character.')\n if (seen.has(g.char)) {\n throw new Error(\n `Duplicate chromatic glyph ${JSON.stringify(g.char)}. Each grapheme may appear once.`,\n )\n }\n seen.add(g.char)\n if (g.image.width < 1 || g.image.height < 1) {\n throw new Error(`Glyph ${JSON.stringify(g.char)} has an empty image.`)\n }\n if (g.image.data.length !== g.image.width * g.image.height * 4) {\n throw new Error(`Glyph ${JSON.stringify(g.char)} image data is not width * height * 4.`)\n }\n }\n\n const cell = options.cellSize ?? CHROMATIC_CELL\n if (!Number.isInteger(cell) || cell < 8)\n throw new Error(`cellSize must be an integer >= 8 (got ${cell}).`)\n\n const pitch = nextPow2(cell + 2 * ATLAS_PADDING)\n let atlasColumns = 1\n while (atlasColumns * atlasColumns < n) atlasColumns++\n const atlasRows = idiv(n + atlasColumns - 1, atlasColumns)\n const atlasW = atlasColumns * pitch\n const atlasH = atlasRows * pitch\n\n const coverageAtlas = new Uint8Array(atlasW * atlasH)\n const rgbaAtlas = new Uint8Array(atlasW * atlasH * 4)\n const samples = new Uint8Array(n * 256)\n const masksLo = new Uint32Array(n)\n const masksHi = new Uint32Array(n)\n const coverage = new Uint16Array(n)\n\n for (let i = 0; i < n; i++) {\n const tile = boxFilter(sources[i].image, cell, cell)\n const tileX = (i % atlasColumns) * pitch + ATLAS_PADDING\n const tileY = idiv(i, atlasColumns) * pitch + ATLAS_PADDING\n let totalAlpha = 0\n for (let y = 0; y < cell; y++) {\n for (let x = 0; x < cell; x++) {\n const src = (y * cell + x) * 4\n const dst = (tileY + y) * atlasW + tileX + x\n rgbaAtlas[dst * 4] = tile[src]\n rgbaAtlas[dst * 4 + 1] = tile[src + 1]\n rgbaAtlas[dst * 4 + 2] = tile[src + 2]\n rgbaAtlas[dst * 4 + 3] = tile[src + 3]\n // The coverage plane is the alpha channel, which keeps blankGlyphId and\n // every structural consumer meaningful on a chromatic profile.\n coverageAtlas[dst] = tile[src + 3]\n totalAlpha += tile[src + 3]\n }\n }\n coverage[i] = rdiv(totalAlpha * 65535, cell * cell * 255)\n\n const small = boxFilter(sources[i].image, 8, 8)\n samples.set(small, i * 256)\n let lo = 0\n let hi = 0\n for (let k = 0; k < 64; k++) {\n // Same 0.5 threshold as raster-v1's mask rule (§13); ties are ink.\n if (small[k * 4 + 3] >= 128) {\n if (k < 32) lo |= 1 << k\n else hi |= 1 << (k - 32)\n }\n }\n masksLo[i] = lo >>> 0\n masksHi[i] = hi >>> 0\n }\n\n const glyphs = sources.map((g) => g.char)\n // There is no font, so the slot carrying a font's identity carries the\n // identity of the source images instead: same role, different provenance.\n let total = 0\n for (const g of sources) total += g.image.data.length\n const imageBytes = new Uint8Array(total)\n let at = 0\n for (const g of sources) {\n imageBytes.set(\n g.image.data instanceof Uint8Array ? g.image.data : new Uint8Array(g.image.data),\n at,\n )\n at += g.image.data.length\n }\n const sourceHash = sha256(imageBytes)\n const charsetHash = sha256(glyphs.join(' '))\n const fingerprint = sha256(\n `asciip/1|chromatic-v1|${CHROMATIC_RASTER_VERSION}|images:${sourceHash}|charset:${charsetHash}` +\n `|cell:${cell}|pad:${ATLAS_PADDING}`,\n )\n\n const profile: AsciiProfile = {\n version: 1,\n id: options.id ?? 'chromatic',\n fingerprint,\n charsetHash,\n fontHash: sourceHash,\n glyphs,\n glyphCount: n,\n // A chromatic cell is square and has no baseline; these describe that box so\n // grid-v1 derives square cells without the matcher special-casing anything.\n metrics: {\n unitsPerEm: cell,\n ascent: cell,\n descent: 0,\n lineGap: 0,\n advanceUnits: cell,\n cellWidth: cell,\n cellHeight: cell,\n baseline: cell,\n },\n atlas: {\n width: atlasW,\n height: atlasH,\n pitchWidth: pitch,\n pitchHeight: pitch,\n cellWidth: cell,\n cellHeight: cell,\n padding: ATLAS_PADDING,\n columns: atlasColumns,\n data: coverageAtlas,\n rgba: rgbaAtlas,\n },\n structural: { masksLo, masksHi, coverage },\n chromatic: { samples },\n metadata: {\n id: options.id ?? 'chromatic',\n charset: 'chromatic',\n compilerVersion: COMPILER_VERSION,\n },\n }\n\n return { profile, binary: encodeProfile(profile) }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA4BO,SAAS,kBAAkB,QAAkC;AAClE,SAAO;AACT;;;ACvBA,SAAS,eAAe,MAAM,UAAU,YAAY;AAGpD,IAAMA,iBAAgB;AAQf,IAAM,iBAAiB;AACvB,IAAM,2BAA2B;AAyBxC,SAAS,UAAU,KAAe,IAAY,IAAwB;AACpE,QAAM,EAAE,OAAO,GAAG,QAAQ,GAAG,KAAK,IAAI;AACtC,QAAM,MAAM,IAAI,WAAW,KAAK,KAAK,CAAC;AACtC,WAAS,KAAK,GAAG,KAAK,IAAI,MAAM;AAC9B,UAAM,KAAK,KAAK,KAAK,GAAG,EAAE;AAC1B,UAAM,KAAK,KAAK,IAAI,KAAK,GAAG,MAAM,KAAK,KAAK,GAAG,EAAE,CAAC;AAClD,aAAS,KAAK,GAAG,KAAK,IAAI,MAAM;AAC9B,YAAM,KAAK,KAAK,KAAK,GAAG,EAAE;AAC1B,YAAM,KAAK,KAAK,IAAI,KAAK,GAAG,MAAM,KAAK,KAAK,GAAG,EAAE,CAAC;AAClD,UAAI,KAAK;AACT,UAAI,KAAK;AACT,UAAI,KAAK;AACT,UAAI,KAAK;AACT,UAAI,QAAQ;AACZ,eAAS,IAAI,IAAI,IAAI,IAAI,KAAK;AAC5B,iBAAS,IAAI,IAAI,IAAI,IAAI,KAAK;AAC5B,gBAAM,KAAK,IAAI,IAAI,KAAK;AACxB,gBAAM,IAAI,KAAK,IAAI,CAAC;AACpB,gBAAM,KAAK,CAAC,IAAI;AAChB,gBAAM,KAAK,IAAI,CAAC,IAAI;AACpB,gBAAM,KAAK,IAAI,CAAC,IAAI;AACpB,gBAAM;AACN;AAAA,QACF;AAAA,MACF;AACA,YAAM,KAAK,KAAK,KAAK,MAAM;AAC3B,UAAI,OAAO,GAAG;AACZ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A,MACZ,UAAU;AAAA,IACZ;AAAA,IACA,OAAO;AAAA,MACL,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,YAAY;AAAA,MACZ,aAAa;AAAA,MACb,WAAW;AAAA,MACX,YAAY;AAAA,MACZ,SAASA;AAAA,MACT,SAAS;AAAA,MACT,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,IACA,YAAY,EAAE,SAAS,SAAS,SAAS;AAAA,IACzC,WAAW,EAAE,QAAQ;AAAA,IACrB,UAAU;AAAA,MACR,IAAI,QAAQ,MAAM;AAAA,MAClB,SAAS;AAAA,MACT,iBAAiB;AAAA,IACnB;AAAA,EACF;AAEA,SAAO,EAAE,SAAS,QAAQ,cAAc,OAAO,EAAE;AACnD;","names":["ATLAS_PADDING"]}
|
package/dist/png.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AsciiProfile } from '@ascii-fx/core';
|
|
2
|
+
export declare const COMPILER_VERSION = "0.1.0";
|
|
3
|
+
/**
|
|
4
|
+
* The @ascii-fx/compiler version actually installed, read from its own manifest so it cannot
|
|
5
|
+
* drift the way a constant can. Every @ascii-fx/* package releases in one fixed group with
|
|
6
|
+
* exact internal pins, so this moves on any release that could change output bytes — which is
|
|
7
|
+
* what makes it a sound build-cache salt (@ascii-fx/vite keys on it). Deliberately separate
|
|
8
|
+
* from COMPILER_VERSION: that string is embedded in profile metadata, where following the
|
|
9
|
+
* package version would change profile bytes and break the golden fixtures on every release.
|
|
10
|
+
*/
|
|
11
|
+
export declare const COMPILER_PACKAGE_VERSION: string;
|
|
12
|
+
export declare const REF_GLYPH_HEIGHT = 64;
|
|
13
|
+
export declare const ATLAS_PADDING = 4;
|
|
14
|
+
export interface BuildProfileOptions {
|
|
15
|
+
/** Raw font bytes (ttf/otf/woff/woff2). */
|
|
16
|
+
font: Uint8Array;
|
|
17
|
+
id?: string;
|
|
18
|
+
/** Built-in charset name; default 'ascii'. */
|
|
19
|
+
charset?: string;
|
|
20
|
+
/** Custom characters (overrides charset). */
|
|
21
|
+
characters?: string | readonly string[];
|
|
22
|
+
/**
|
|
23
|
+
* Compile shape6-v1 data (ALGORITHM.md §18): 6D glyph vectors, and with
|
|
24
|
+
* `{ lut: true }` the 512KiB dense winner LUT. Opt-in — it grows the profile
|
|
25
|
+
* and only the explicit `matcher: 'shape6'` uses it (spec §5–6).
|
|
26
|
+
*/
|
|
27
|
+
shape6?: boolean | {
|
|
28
|
+
lut?: boolean;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface BuiltProfile {
|
|
32
|
+
profile: AsciiProfile;
|
|
33
|
+
binary: Uint8Array;
|
|
34
|
+
}
|
|
35
|
+
export declare const sha256: (data: Uint8Array | string) => string;
|
|
36
|
+
/** Build a deterministic .asciip profile (ALGORITHM.md §13–14). */
|
|
37
|
+
export declare function buildProfile(options: BuildProfileOptions): BuiltProfile;
|
package/dist/raster.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { FontPathCommand } from 'fontkit';
|
|
2
|
+
export declare const SUPERSAMPLE = 4;
|
|
3
|
+
export declare const QUAD_SEGMENTS = 16;
|
|
4
|
+
export declare const CUBIC_SEGMENTS = 24;
|
|
5
|
+
/** Font-unit → 26.6 pixel-space mappers (y-down). */
|
|
6
|
+
export interface FxMapping {
|
|
7
|
+
x(v: number): number;
|
|
8
|
+
y(v: number): number;
|
|
9
|
+
}
|
|
10
|
+
/** Flatten outline commands to non-horizontal line segments [x0,y0,x1,y1, ...] in 26.6. */
|
|
11
|
+
export declare function flattenCommands(commands: readonly FontPathCommand[], map: FxMapping): number[];
|
|
12
|
+
export interface GlyphRaster {
|
|
13
|
+
/** Per-pixel subsample hit counts, cellW·cellH, each 0..SUPERSAMPLE². */
|
|
14
|
+
hits: Uint8Array;
|
|
15
|
+
/** Subsample hits per 8×8 mask cell. */
|
|
16
|
+
maskHits: Int32Array;
|
|
17
|
+
totalHits: number;
|
|
18
|
+
}
|
|
19
|
+
/** Subsample totals per 8×8 mask cell — identical for every glyph of a given cell size. */
|
|
20
|
+
export declare function maskTotals(cellW: number, cellH: number): Int32Array;
|
|
21
|
+
/** Scanline fill (non-zero winding) over subsample centers. */
|
|
22
|
+
export declare function rasterizeGlyph(edges: readonly number[], cellW: number, cellH: number): GlyphRaster;
|
|
23
|
+
/** Mask bit = 1 ⇔ 2·hits ≥ total (tie → ink). Returns 64-bit mask as lo/hi u32. */
|
|
24
|
+
export declare function deriveMask(maskHits: Int32Array, totals: Int32Array): {
|
|
25
|
+
lo: number;
|
|
26
|
+
hi: number;
|
|
27
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ascii-fx/compiler",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Build-time compiler for ASCII FX: deterministic font rasterization, glyph atlases, .asciip/.asciif generation.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ascii",
|
|
7
|
+
"ascii-art",
|
|
8
|
+
"ascii-fx",
|
|
9
|
+
"build-tool",
|
|
10
|
+
"cli",
|
|
11
|
+
"font",
|
|
12
|
+
"glyph-atlas",
|
|
13
|
+
"rasterizer"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/Amir-Abushanab/ascii-fx#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/Amir-Abushanab/ascii-fx/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Amir Abushanab (https://github.com/Amir-Abushanab)",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Amir-Abushanab/ascii-fx.git",
|
|
24
|
+
"directory": "packages/compiler"
|
|
25
|
+
},
|
|
26
|
+
"bin": {
|
|
27
|
+
"ascii-fx": "./dist/cli.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"default": "./dist/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"fontkit": "^2.0.4",
|
|
45
|
+
"pngjs": "^7.0.0",
|
|
46
|
+
"@ascii-fx/core": "0.2.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^26.2.0",
|
|
50
|
+
"@types/pngjs": "^6.0.5",
|
|
51
|
+
"tsup": "^8.5.1",
|
|
52
|
+
"typescript": "^7.0.2"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=22"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsup && tsc -p tsconfig.build.json && node scripts/postbuild.mjs",
|
|
59
|
+
"typecheck": "tsc --noEmit"
|
|
60
|
+
}
|
|
61
|
+
}
|