Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: preserve transparency in Windows ICO by building PNG-in-ICO with sharp

Tw9325151b51b006
diff --git a/bin/options/icon.ts b/bin/options/icon.ts
index 19b3264d..623096f2 100644
--- a/bin/options/icon.ts
+++ b/bin/options/icon.ts
@@ -17,7 +17,7 @@ import {
} from '@/utils/icon-source';
import { generateLinuxPackageName, generateSafeFilename } from '@/utils/name';
import { PakeAppOptions } from '@/types';
-import { writeIcoWithPreferredSize } from '@/utils/ico';
+import { writeIcoWithPreferredSize, buildIcoFromPngBuffers } from '@/utils/ico';
type PlatformIconConfig = {
format: string;
@@ -207,18 +207,26 @@ async function convertIconFormat(
// Generate platform-specific format
if (IS_WIN) {
- // Support multiple sizes for better Windows compatibility
- await icongen(processedInputPath, platformOutputDir, {
- report: false,
- ico: {
- name: `${iconName}_256`,
- sizes: PLATFORM_CONFIG.win.sizes,
- },
- });
- return path.join(
+ const icoPath = path.join(
platformOutputDir,
`${iconName}_256${PLATFORM_CONFIG.win.format}`,
);
+ const frames = await Promise.all(
+ (PLATFORM_CONFIG.win.sizes as number[]).map(async (size) => {
+ const png = await sharp(processedInputPath)
+ .resize(size, size, {
+ fit: 'contain',
+ background: { r: 0, g: 0, b: 0, alpha: 0 },
+ })
+ .ensureAlpha()
+ .png()
+ .toBuffer();
+ return { size, png };
+ }),
+ );
+ const icoBuffer = buildIcoFromPngBuffers(frames);
+ await fsExtra.outputFile(icoPath, icoBuffer);
+ return icoPath;
}
if (IS_LINUX) {
diff --git a/bin/utils/ico.ts b/bin/utils/ico.ts
index af435b8b..33f59070 100644
--- a/bin/utils/ico.ts
+++ b/bin/utils/ico.ts
@@ -141,3 +141,41 @@ export async function writeIcoWithPreferredSize(
return false;
}
}
+
+/**
+ * Builds an ICO file from an array of PNG buffers using the PNG-in-ICO format
+ * (supported since Windows Vista). This preserves alpha transparency.
+ */
+export function buildIcoFromPngBuffers(
+ frames: Array<{ size: number; png: Buffer }>,
+): Buffer {
+ const count = frames.length;
+ const headerSize = ICO_HEADER_SIZE + count * ICO_DIR_ENTRY_SIZE;
+ const totalPayload = frames.reduce((acc, f) => acc + f.png.length, 0);
+ const output = Buffer.alloc(headerSize + totalPayload);
+
+ output.writeUInt16LE(0, 0);
+ output.writeUInt16LE(ICO_TYPE_ICON, 2);
+ output.writeUInt16LE(count, 4);
+
+ let currentOffset = headerSize;
+ for (let i = 0; i < count; i++) {
+ const { size, png } = frames[i];
+ const entryOffset = ICO_HEADER_SIZE + i * ICO_DIR_ENTRY_SIZE;
+ const sizeByte = size >= 256 ? 0 : size;
+
+ output.writeUInt8(sizeByte, entryOffset);
+ output.writeUInt8(sizeByte, entryOffset + 1);
+ output.writeUInt8(0, entryOffset + 2);
+ output.writeUInt8(0, entryOffset + 3);
+ output.writeUInt16LE(1, entryOffset + 4);
+ output.writeUInt16LE(32, entryOffset + 6);
+ output.writeUInt32LE(png.length, entryOffset + 8);
+ output.writeUInt32LE(currentOffset, entryOffset + 12);
+
+ png.copy(output, currentOffset);
+ currentOffset += png.length;
+ }
+
+ return output;
+}