@skathi-native/cli 0.0.0-stage → 0.1.0-beta.1

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/cli/icon.ts ADDED
@@ -0,0 +1,303 @@
1
+ // The exe's icon: `RT_ICON` 1..n plus `RT_GROUP_ICON` 1. Id 1 is not our choice: gpui's window class
2
+ // loads `MAKEINTRESOURCE(1)`, so a group icon under that id lands on the window, taskbar, Alt-Tab
3
+ // and the file in Explorer.
4
+ //
5
+ // Two inputs: an `.ico`, taken verbatim; or a square 8-bit `.png`, which becomes PNG-compressed
6
+ // images at 256 (the file itself when already 256), 48, 32, 24 and 16. Anything else is an error.
7
+
8
+ import { readFileSync } from "node:fs";
9
+ import { extname } from "node:path";
10
+ import { deflateSync, inflateSync } from "node:zlib";
11
+ import { crc32 } from "./skpk";
12
+
13
+ export interface IconImage {
14
+ width: number;
15
+ height: number;
16
+ bitCount: number;
17
+ planes: number;
18
+ colorCount: number;
19
+ data: Uint8Array;
20
+ }
21
+
22
+ export const PNG_ICON_SIZES = [256, 48, 32, 24, 16];
23
+
24
+ const PNG_SIG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
25
+
26
+ export interface Rgba {
27
+ width: number;
28
+ height: number;
29
+ data: Uint8Array;
30
+ }
31
+
32
+ export function isPng(bytes: Uint8Array): boolean {
33
+ return PNG_SIG.every((b, i) => bytes[i] === b);
34
+ }
35
+
36
+ const be32 = (b: Uint8Array, o: number) => ((b[o]! << 24) | (b[o + 1]! << 16) | (b[o + 2]! << 8) | b[o + 3]!) >>> 0;
37
+
38
+ export function decodePng(bytes: Uint8Array, label = "png"): Rgba {
39
+ if (!isPng(bytes)) throw new Error(`${label}: not a PNG`);
40
+ let o = 8;
41
+ let width = 0;
42
+ let height = 0;
43
+ let depth = 0;
44
+ let color = 0;
45
+ let interlace = 0;
46
+ let palette: Uint8Array | null = null;
47
+ let trns: Uint8Array | null = null;
48
+ const idat: Uint8Array[] = [];
49
+ while (o + 8 <= bytes.length) {
50
+ const len = be32(bytes, o);
51
+ const type = String.fromCharCode(...bytes.subarray(o + 4, o + 8));
52
+ const body = bytes.subarray(o + 8, o + 8 + len);
53
+ o += 12 + len;
54
+ if (type === "IHDR") {
55
+ width = be32(body, 0);
56
+ height = be32(body, 4);
57
+ depth = body[8]!;
58
+ color = body[9]!;
59
+ interlace = body[12]!;
60
+ } else if (type === "PLTE") palette = body;
61
+ else if (type === "tRNS") trns = body;
62
+ else if (type === "IDAT") idat.push(body);
63
+ else if (type === "IEND") break;
64
+ }
65
+ if (!width || !height) throw new Error(`${label}: no IHDR`);
66
+ if (depth !== 8 || interlace !== 0 || ![0, 2, 3, 4, 6].includes(color)) {
67
+ throw new Error(
68
+ `${label}: only 8-bit, non-interlaced PNGs are converted (this one is ${depth}-bit, colour type ${color}` +
69
+ `${interlace ? ", interlaced" : ""}) — give windows.icon an .ico instead`,
70
+ );
71
+ }
72
+ const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[color]!;
73
+ const stride = width * channels;
74
+ const total = idat.reduce((n, c) => n + c.length, 0);
75
+ const z = new Uint8Array(total);
76
+ let zo = 0;
77
+ for (const c of idat) {
78
+ z.set(c, zo);
79
+ zo += c.length;
80
+ }
81
+ const raw = new Uint8Array(inflateSync(z));
82
+ if (raw.length < (stride + 1) * height) throw new Error(`${label}: truncated image data`);
83
+ const px = new Uint8Array(stride * height);
84
+ const bpp = channels;
85
+ for (let y = 0; y < height; y++) {
86
+ const filter = raw[y * (stride + 1)]!;
87
+ const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
88
+ const cur = px.subarray(y * stride, (y + 1) * stride);
89
+ const prev = y ? px.subarray((y - 1) * stride, y * stride) : new Uint8Array(stride);
90
+ for (let x = 0; x < stride; x++) {
91
+ const a = x >= bpp ? cur[x - bpp]! : 0;
92
+ const b = prev[x]!;
93
+ const c = x >= bpp ? prev[x - bpp]! : 0;
94
+ let v = line[x]!;
95
+ if (filter === 1) v += a;
96
+ else if (filter === 2) v += b;
97
+ else if (filter === 3) v += (a + b) >> 1;
98
+ else if (filter === 4) {
99
+ const p = a + b - c;
100
+ const pa = Math.abs(p - a);
101
+ const pb = Math.abs(p - b);
102
+ const pc = Math.abs(p - c);
103
+ v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
104
+ } else if (filter !== 0) throw new Error(`${label}: bad filter ${filter} on row ${y}`);
105
+ cur[x] = v & 0xff;
106
+ }
107
+ }
108
+ const out = new Uint8Array(width * height * 4);
109
+ for (let i = 0; i < width * height; i++) {
110
+ let r = 0;
111
+ let g = 0;
112
+ let b = 0;
113
+ let a = 255;
114
+ if (color === 0) r = g = b = px[i]!;
115
+ else if (color === 4) {
116
+ r = g = b = px[i * 2]!;
117
+ a = px[i * 2 + 1]!;
118
+ } else if (color === 2) {
119
+ r = px[i * 3]!;
120
+ g = px[i * 3 + 1]!;
121
+ b = px[i * 3 + 2]!;
122
+ } else if (color === 6) {
123
+ r = px[i * 4]!;
124
+ g = px[i * 4 + 1]!;
125
+ b = px[i * 4 + 2]!;
126
+ a = px[i * 4 + 3]!;
127
+ } else {
128
+ const idx = px[i]!;
129
+ if (!palette || idx * 3 + 2 >= palette.length) throw new Error(`${label}: palette index ${idx} out of range`);
130
+ r = palette[idx * 3]!;
131
+ g = palette[idx * 3 + 1]!;
132
+ b = palette[idx * 3 + 2]!;
133
+ a = trns && idx < trns.length ? trns[idx]! : 255;
134
+ }
135
+ out[i * 4] = r;
136
+ out[i * 4 + 1] = g;
137
+ out[i * 4 + 2] = b;
138
+ out[i * 4 + 3] = a;
139
+ }
140
+ return { width, height, data: out };
141
+ }
142
+
143
+ function chunk(type: string, body: Uint8Array): Uint8Array {
144
+ const out = new Uint8Array(12 + body.length);
145
+ const dv = new DataView(out.buffer);
146
+ dv.setUint32(0, body.length);
147
+ for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
148
+ out.set(body, 8);
149
+ dv.setUint32(8 + body.length, crc32(out.subarray(4, 8 + body.length)));
150
+ return out;
151
+ }
152
+
153
+ export function encodePng(img: Rgba): Uint8Array {
154
+ const { width, height, data } = img;
155
+ const stride = width * 4;
156
+ const raw = new Uint8Array((stride + 1) * height);
157
+ for (let y = 0; y < height; y++) {
158
+ raw[y * (stride + 1)] = 0;
159
+ raw.set(data.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
160
+ }
161
+ const ihdr = new Uint8Array(13);
162
+ const dv = new DataView(ihdr.buffer);
163
+ dv.setUint32(0, width);
164
+ dv.setUint32(4, height);
165
+ ihdr[8] = 8;
166
+ ihdr[9] = 6;
167
+ const parts = [
168
+ new Uint8Array(PNG_SIG),
169
+ chunk("IHDR", ihdr),
170
+ chunk("IDAT", new Uint8Array(deflateSync(raw, { level: 9 }))),
171
+ chunk("IEND", new Uint8Array(0)),
172
+ ];
173
+ const n = parts.reduce((a, p) => a + p.length, 0);
174
+ const out = new Uint8Array(n);
175
+ let o = 0;
176
+ for (const p of parts) {
177
+ out.set(p, o);
178
+ o += p.length;
179
+ }
180
+ return out;
181
+ }
182
+
183
+ export function resizeRgba(src: Rgba, dw: number, dh: number): Rgba {
184
+ const { width: sw, height: sh, data } = src;
185
+ const out = new Uint8Array(dw * dh * 4);
186
+ const sx = sw / dw;
187
+ const sy = sh / dh;
188
+ for (let y = 0; y < dh; y++) {
189
+ const y0 = y * sy;
190
+ const y1 = y0 + sy;
191
+ for (let x = 0; x < dw; x++) {
192
+ const x0 = x * sx;
193
+ const x1 = x0 + sx;
194
+ let r = 0;
195
+ let g = 0;
196
+ let b = 0;
197
+ let a = 0;
198
+ let wsum = 0;
199
+ for (let yy = Math.floor(y0); yy < Math.ceil(y1); yy++) {
200
+ const wy = Math.min(yy + 1, y1) - Math.max(yy, y0);
201
+ if (wy <= 0) continue;
202
+ for (let xx = Math.floor(x0); xx < Math.ceil(x1); xx++) {
203
+ const wx = Math.min(xx + 1, x1) - Math.max(xx, x0);
204
+ if (wx <= 0) continue;
205
+ const w = wx * wy;
206
+ const i = (Math.min(yy, sh - 1) * sw + Math.min(xx, sw - 1)) * 4;
207
+ const pa = data[i + 3]! / 255;
208
+ r += data[i]! * pa * w;
209
+ g += data[i + 1]! * pa * w;
210
+ b += data[i + 2]! * pa * w;
211
+ a += pa * w;
212
+ wsum += w;
213
+ }
214
+ }
215
+ const o = (y * dw + x) * 4;
216
+ const alpha = wsum ? a / wsum : 0;
217
+ if (a > 0) {
218
+ out[o] = Math.round(r / a);
219
+ out[o + 1] = Math.round(g / a);
220
+ out[o + 2] = Math.round(b / a);
221
+ }
222
+ out[o + 3] = Math.round(alpha * 255);
223
+ }
224
+ }
225
+ return { width: dw, height: dh, data: out };
226
+ }
227
+
228
+ export function parseIco(bytes: Uint8Array, label = "ico"): IconImage[] {
229
+ const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
230
+ if (bytes.length < 6 || dv.getUint16(0, true) !== 0 || dv.getUint16(2, true) !== 1) {
231
+ throw new Error(`${label}: not an .ico (ICONDIR type must be 1)`);
232
+ }
233
+ const count = dv.getUint16(4, true);
234
+ if (!count) throw new Error(`${label}: the .ico holds no images`);
235
+ const images: IconImage[] = [];
236
+ for (let i = 0; i < count; i++) {
237
+ const e = 6 + i * 16;
238
+ if (e + 16 > bytes.length) throw new Error(`${label}: truncated ICONDIRENTRY ${i}`);
239
+ const size = dv.getUint32(e + 8, true);
240
+ const off = dv.getUint32(e + 12, true);
241
+ if (off + size > bytes.length) throw new Error(`${label}: image ${i} runs past the end of the file`);
242
+ images.push({
243
+ width: bytes[e]! || 256,
244
+ height: bytes[e + 1]! || 256,
245
+ colorCount: bytes[e + 2]!,
246
+ planes: dv.getUint16(e + 4, true),
247
+ bitCount: dv.getUint16(e + 6, true),
248
+ data: bytes.slice(off, off + size),
249
+ });
250
+ }
251
+ return images;
252
+ }
253
+
254
+ export function iconImagesFrom(path: string): IconImage[] {
255
+ const bytes = new Uint8Array(readFileSync(path));
256
+ const ext = extname(path).toLowerCase();
257
+ if (ext === ".ico") return parseIco(bytes, path);
258
+ if (ext !== ".png" && !isPng(bytes)) {
259
+ throw new Error(`${path}: windows.icon must be an .ico or a square .png`);
260
+ }
261
+ const img = decodePng(bytes, path);
262
+ if (img.width !== img.height) {
263
+ throw new Error(
264
+ `${path}: a PNG icon must be square (this one is ${img.width}×${img.height}) — give an .ico instead`,
265
+ );
266
+ }
267
+ if (img.width < 16) throw new Error(`${path}: ${img.width}px is smaller than the smallest icon size (16)`);
268
+ const sizes = PNG_ICON_SIZES.filter((s) => s <= img.width);
269
+ if (img.width < 256 && !sizes.includes(img.width)) sizes.unshift(img.width);
270
+ return sizes.map((s) => ({
271
+ width: s,
272
+ height: s,
273
+ bitCount: 32,
274
+ planes: 1,
275
+ colorCount: 0,
276
+ // The file itself when it is already the right size: re-encoding would only lose the author's tool's work.
277
+ data: s === img.width ? bytes : encodePng(resizeRgba(img, s, s)),
278
+ }));
279
+ }
280
+
281
+ export function groupIcon(
282
+ images: IconImage[],
283
+ firstId = 1,
284
+ ): { icons: { id: number; data: Uint8Array }[]; group: Uint8Array } {
285
+ const group = new Uint8Array(6 + images.length * 14);
286
+ const dv = new DataView(group.buffer);
287
+ dv.setUint16(0, 0, true);
288
+ dv.setUint16(2, 1, true);
289
+ dv.setUint16(4, images.length, true);
290
+ const icons = images.map((img, i) => {
291
+ const o = 6 + i * 14;
292
+ group[o] = img.width >= 256 ? 0 : img.width;
293
+ group[o + 1] = img.height >= 256 ? 0 : img.height;
294
+ group[o + 2] = img.colorCount;
295
+ group[o + 3] = 0;
296
+ dv.setUint16(o + 4, img.planes || 1, true);
297
+ dv.setUint16(o + 6, img.bitCount || 32, true);
298
+ dv.setUint32(o + 8, img.data.length, true);
299
+ dv.setUint16(o + 12, firstId + i, true);
300
+ return { id: firstId + i, data: img.data };
301
+ });
302
+ return { icons, group };
303
+ }