@mrdemonwolf/iconwolf 0.1.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/LICENSE +21 -0
- package/dist/generator.d.ts +3 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +122 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/lib.d.ts +10 -0
- package/dist/lib.d.ts.map +1 -0
- package/dist/lib.js +9 -0
- package/dist/lib.js.map +1 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/icon-composer.d.ts +14 -0
- package/dist/utils/icon-composer.d.ts.map +1 -0
- package/dist/utils/icon-composer.js +200 -0
- package/dist/utils/icon-composer.js.map +1 -0
- package/dist/utils/image.d.ts +22 -0
- package/dist/utils/image.d.ts.map +1 -0
- package/dist/utils/image.js +145 -0
- package/dist/utils/image.js.map +1 -0
- package/dist/utils/logger.d.ts +10 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +38 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/paths.d.ts +17 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +27 -0
- package/dist/utils/paths.js.map +1 -0
- package/dist/utils/update-notifier.d.ts +9 -0
- package/dist/utils/update-notifier.d.ts.map +1 -0
- package/dist/utils/update-notifier.js +80 -0
- package/dist/utils/update-notifier.js.map +1 -0
- package/dist/variants/android.d.ts +5 -0
- package/dist/variants/android.d.ts.map +1 -0
- package/dist/variants/android.js +18 -0
- package/dist/variants/android.js.map +1 -0
- package/dist/variants/favicon.d.ts +3 -0
- package/dist/variants/favicon.d.ts.map +1 -0
- package/dist/variants/favicon.js +23 -0
- package/dist/variants/favicon.js.map +1 -0
- package/dist/variants/splash.d.ts +3 -0
- package/dist/variants/splash.d.ts.map +1 -0
- package/dist/variants/splash.js +7 -0
- package/dist/variants/splash.js.map +1 -0
- package/dist/variants/standard.d.ts +3 -0
- package/dist/variants/standard.d.ts.map +1 -0
- package/dist/variants/standard.js +7 -0
- package/dist/variants/standard.js.map +1 -0
- package/eslint.config.js +10 -0
- package/package.json +57 -0
- package/scripts/build-release.sh +63 -0
- package/src/generator.ts +163 -0
- package/src/index.ts +73 -0
- package/src/lib.ts +16 -0
- package/src/types.ts +22 -0
- package/src/utils/icon-composer.ts +283 -0
- package/src/utils/image.ts +207 -0
- package/src/utils/logger.ts +61 -0
- package/src/utils/paths.ts +30 -0
- package/src/utils/update-notifier.ts +99 -0
- package/src/variants/android.ts +45 -0
- package/src/variants/favicon.ts +32 -0
- package/src/variants/splash.ts +11 -0
- package/src/variants/standard.ts +11 -0
- package/tests/cli.test.ts +84 -0
- package/tests/generator.test.ts +368 -0
- package/tests/helpers.ts +36 -0
- package/tests/utils/icon-composer.test.ts +208 -0
- package/tests/utils/image.test.ts +207 -0
- package/tests/utils/logger.test.ts +128 -0
- package/tests/utils/paths.test.ts +51 -0
- package/tests/utils/update-notifier.test.ts +184 -0
- package/tests/variants/android.test.ts +77 -0
- package/tests/variants/favicon.test.ts +36 -0
- package/tests/variants/splash.test.ts +35 -0
- package/tests/variants/standard.test.ts +35 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +8 -0
package/src/generator.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
isIconComposerFolder,
|
|
5
|
+
renderIconComposerFolder,
|
|
6
|
+
} from './utils/icon-composer.js';
|
|
7
|
+
import { validateSourceImage } from './utils/image.js';
|
|
8
|
+
import * as logger from './utils/logger.js';
|
|
9
|
+
import { DEFAULT_OUTPUT_DIR } from './utils/paths.js';
|
|
10
|
+
import { generateStandardIcon } from './variants/standard.js';
|
|
11
|
+
import { generateFavicon } from './variants/favicon.js';
|
|
12
|
+
import { generateSplashIcon } from './variants/splash.js';
|
|
13
|
+
import { generateAndroidIcons } from './variants/android.js';
|
|
14
|
+
import type { GeneratorOptions, GenerationResult } from './types.js';
|
|
15
|
+
|
|
16
|
+
interface ResolvedInput {
|
|
17
|
+
inputPath: string;
|
|
18
|
+
cleanupPath?: string;
|
|
19
|
+
bgColor: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function resolveInput(
|
|
23
|
+
resolvedPath: string,
|
|
24
|
+
bgColor: string,
|
|
25
|
+
silent: boolean,
|
|
26
|
+
): Promise<ResolvedInput> {
|
|
27
|
+
if (isIconComposerFolder(resolvedPath)) {
|
|
28
|
+
if (!silent) logger.info(`Apple Icon Composer file: ${resolvedPath}`);
|
|
29
|
+
const result = await renderIconComposerFolder(resolvedPath);
|
|
30
|
+
const inputPath = result.composedImagePath;
|
|
31
|
+
const cleanupPath = path.dirname(result.composedImagePath);
|
|
32
|
+
|
|
33
|
+
if (bgColor === '#FFFFFF') {
|
|
34
|
+
bgColor = result.extractedBgColor;
|
|
35
|
+
if (!silent) logger.info(`Extracted background color: ${bgColor}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return { inputPath, cleanupPath, bgColor };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return { inputPath: resolvedPath, bgColor };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function generate(
|
|
45
|
+
options: GeneratorOptions,
|
|
46
|
+
): Promise<GenerationResult[]> {
|
|
47
|
+
const resolvedInput = path.resolve(options.inputPath);
|
|
48
|
+
const outputDir = path.resolve(options.outputDir || DEFAULT_OUTPUT_DIR);
|
|
49
|
+
let { bgColor } = options;
|
|
50
|
+
const { variants } = options;
|
|
51
|
+
const silent = options.silent ?? false;
|
|
52
|
+
|
|
53
|
+
if (!silent) logger.banner();
|
|
54
|
+
|
|
55
|
+
// Check source exists
|
|
56
|
+
if (!fs.existsSync(resolvedInput)) {
|
|
57
|
+
throw new Error(`Source not found: ${resolvedInput}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let inputPath: string;
|
|
61
|
+
let cleanupPath: string | undefined;
|
|
62
|
+
let splashPath: string | undefined;
|
|
63
|
+
let splashCleanupPath: string | undefined;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
// Resolve main input (Apple Icon Composer .icon folder or PNG)
|
|
67
|
+
const mainInput = await resolveInput(resolvedInput, bgColor, silent);
|
|
68
|
+
inputPath = mainInput.inputPath;
|
|
69
|
+
cleanupPath = mainInput.cleanupPath;
|
|
70
|
+
bgColor = mainInput.bgColor;
|
|
71
|
+
|
|
72
|
+
// Resolve splash input if provided
|
|
73
|
+
if (options.splashInputPath) {
|
|
74
|
+
const resolvedSplashInput = path.resolve(options.splashInputPath);
|
|
75
|
+
if (!fs.existsSync(resolvedSplashInput)) {
|
|
76
|
+
throw new Error(`Splash source not found: ${resolvedSplashInput}`);
|
|
77
|
+
}
|
|
78
|
+
const splashInput = await resolveInput(
|
|
79
|
+
resolvedSplashInput,
|
|
80
|
+
bgColor,
|
|
81
|
+
silent,
|
|
82
|
+
);
|
|
83
|
+
splashPath = splashInput.inputPath;
|
|
84
|
+
splashCleanupPath = splashInput.cleanupPath;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Validate source image
|
|
88
|
+
if (!silent) logger.info(`Validating source image: ${inputPath}`);
|
|
89
|
+
const meta = await validateSourceImage(inputPath);
|
|
90
|
+
if (!silent)
|
|
91
|
+
logger.info(
|
|
92
|
+
`Source: ${meta.width}x${meta.height} ${meta.format.toUpperCase()}`,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// Validate splash source image if separate
|
|
96
|
+
if (splashPath) {
|
|
97
|
+
if (!silent) logger.info(`Validating splash source image: ${splashPath}`);
|
|
98
|
+
const splashMeta = await validateSourceImage(splashPath);
|
|
99
|
+
if (!silent)
|
|
100
|
+
logger.info(
|
|
101
|
+
`Splash source: ${splashMeta.width}x${splashMeta.height} ${splashMeta.format.toUpperCase()}`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Create output directory
|
|
106
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
107
|
+
if (!silent) logger.info(`Output directory: ${outputDir}`);
|
|
108
|
+
|
|
109
|
+
// Determine which variants to generate
|
|
110
|
+
const anyFlagSet =
|
|
111
|
+
variants.android || variants.favicon || variants.splash || variants.icon;
|
|
112
|
+
const generateAll = !anyFlagSet;
|
|
113
|
+
|
|
114
|
+
const results: GenerationResult[] = [];
|
|
115
|
+
|
|
116
|
+
// Generate selected variants
|
|
117
|
+
if (generateAll || variants.icon) {
|
|
118
|
+
const result = await generateStandardIcon(inputPath, outputDir);
|
|
119
|
+
results.push(result);
|
|
120
|
+
if (!silent) logger.generated(result);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (generateAll || variants.android) {
|
|
124
|
+
const androidResults = await generateAndroidIcons(
|
|
125
|
+
inputPath,
|
|
126
|
+
outputDir,
|
|
127
|
+
bgColor,
|
|
128
|
+
{ includeBackground: variants.android },
|
|
129
|
+
);
|
|
130
|
+
for (const result of androidResults) {
|
|
131
|
+
results.push(result);
|
|
132
|
+
if (!silent) logger.generated(result);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (generateAll || variants.favicon) {
|
|
137
|
+
const result = await generateFavicon(inputPath, outputDir);
|
|
138
|
+
results.push(result);
|
|
139
|
+
if (!silent) logger.generated(result);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (generateAll || variants.splash) {
|
|
143
|
+
const result = await generateSplashIcon(
|
|
144
|
+
splashPath || inputPath,
|
|
145
|
+
outputDir,
|
|
146
|
+
);
|
|
147
|
+
results.push(result);
|
|
148
|
+
if (!silent) logger.generated(result);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!silent) logger.summary(results);
|
|
152
|
+
|
|
153
|
+
return results;
|
|
154
|
+
} finally {
|
|
155
|
+
// Clean up temp composed images
|
|
156
|
+
if (cleanupPath) {
|
|
157
|
+
fs.rmSync(cleanupPath, { recursive: true, force: true });
|
|
158
|
+
}
|
|
159
|
+
if (splashCleanupPath) {
|
|
160
|
+
fs.rmSync(splashCleanupPath, { recursive: true, force: true });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import { generate } from './generator.js';
|
|
5
|
+
import * as logger from './utils/logger.js';
|
|
6
|
+
import { resolveDefaultOutputDir } from './utils/paths.js';
|
|
7
|
+
import {
|
|
8
|
+
readCachedUpdateInfo,
|
|
9
|
+
refreshCacheInBackground,
|
|
10
|
+
} from './utils/update-notifier.js';
|
|
11
|
+
import type { GeneratorOptions } from './types.js';
|
|
12
|
+
|
|
13
|
+
const VERSION = '0.1.0';
|
|
14
|
+
|
|
15
|
+
const program = new Command();
|
|
16
|
+
|
|
17
|
+
program
|
|
18
|
+
.name('iconwolf')
|
|
19
|
+
.description(
|
|
20
|
+
'Generate all necessary icon variants for cross-platform Expo/React Native projects from a single source icon.',
|
|
21
|
+
)
|
|
22
|
+
.version(VERSION)
|
|
23
|
+
.argument(
|
|
24
|
+
'<input>',
|
|
25
|
+
'Path to an Apple Icon Composer .icon folder or a source PNG',
|
|
26
|
+
)
|
|
27
|
+
.option('-o, --output <dir>', 'Output directory', resolveDefaultOutputDir())
|
|
28
|
+
.option('--android', 'Generate Android adaptive icon variants only')
|
|
29
|
+
.option('--favicon', 'Generate web favicon only')
|
|
30
|
+
.option('--splash', 'Generate splash screen icon only')
|
|
31
|
+
.option('--icon', 'Generate standard icon.png only')
|
|
32
|
+
.option(
|
|
33
|
+
'--splash-input <path>',
|
|
34
|
+
'Use a separate image for the splash screen icon',
|
|
35
|
+
)
|
|
36
|
+
.option(
|
|
37
|
+
'--bg-color <hex>',
|
|
38
|
+
'Background color for Android adaptive icon',
|
|
39
|
+
'#FFFFFF',
|
|
40
|
+
)
|
|
41
|
+
.action(async (input: string, opts) => {
|
|
42
|
+
const updateInfo = readCachedUpdateInfo(VERSION);
|
|
43
|
+
refreshCacheInBackground().catch(() => {});
|
|
44
|
+
|
|
45
|
+
const options: GeneratorOptions = {
|
|
46
|
+
inputPath: input,
|
|
47
|
+
outputDir: opts.output,
|
|
48
|
+
variants: {
|
|
49
|
+
android: opts.android ?? false,
|
|
50
|
+
favicon: opts.favicon ?? false,
|
|
51
|
+
splash: opts.splash ?? false,
|
|
52
|
+
icon: opts.icon ?? false,
|
|
53
|
+
},
|
|
54
|
+
bgColor: opts.bgColor,
|
|
55
|
+
splashInputPath: opts.splashInput,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
await generate(options);
|
|
60
|
+
|
|
61
|
+
if (updateInfo?.updateAvailable) {
|
|
62
|
+
logger.updateNotice(
|
|
63
|
+
updateInfo.currentVersion,
|
|
64
|
+
updateInfo.latestVersion,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger.error(err instanceof Error ? err.message : String(err));
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
program.parse();
|
package/src/lib.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { generate } from './generator.js';
|
|
2
|
+
export { generateStandardIcon } from './variants/standard.js';
|
|
3
|
+
export { generateFavicon } from './variants/favicon.js';
|
|
4
|
+
export { generateSplashIcon } from './variants/splash.js';
|
|
5
|
+
export { generateAndroidIcons } from './variants/android.js';
|
|
6
|
+
export { validateSourceImage } from './utils/image.js';
|
|
7
|
+
export {
|
|
8
|
+
isIconComposerFolder,
|
|
9
|
+
renderIconComposerFolder,
|
|
10
|
+
} from './utils/icon-composer.js';
|
|
11
|
+
export { OUTPUT_FILES, resolveOutputPath } from './utils/paths.js';
|
|
12
|
+
export type {
|
|
13
|
+
VariantFlags,
|
|
14
|
+
GeneratorOptions,
|
|
15
|
+
GenerationResult,
|
|
16
|
+
} from './types.js';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface VariantFlags {
|
|
2
|
+
android: boolean;
|
|
3
|
+
favicon: boolean;
|
|
4
|
+
splash: boolean;
|
|
5
|
+
icon: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface GeneratorOptions {
|
|
9
|
+
inputPath: string;
|
|
10
|
+
outputDir: string;
|
|
11
|
+
variants: VariantFlags;
|
|
12
|
+
bgColor: string;
|
|
13
|
+
splashInputPath?: string;
|
|
14
|
+
silent?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GenerationResult {
|
|
18
|
+
filePath: string;
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
size: number;
|
|
22
|
+
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import sharp from 'sharp';
|
|
5
|
+
|
|
6
|
+
const ICON_SIZE = 1024;
|
|
7
|
+
|
|
8
|
+
interface IconComposerFill {
|
|
9
|
+
'linear-gradient'?: string[];
|
|
10
|
+
solid?: string;
|
|
11
|
+
color?: string;
|
|
12
|
+
orientation?: {
|
|
13
|
+
start: { x: number; y: number };
|
|
14
|
+
stop: { x: number; y: number };
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface IconComposerLayer {
|
|
19
|
+
'image-name': string;
|
|
20
|
+
name: string;
|
|
21
|
+
position: {
|
|
22
|
+
scale: number;
|
|
23
|
+
'translation-in-points': [number, number];
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface IconComposerGroup {
|
|
28
|
+
layers: IconComposerLayer[];
|
|
29
|
+
shadow?: { kind: string; opacity: number };
|
|
30
|
+
translucency?: { enabled: boolean; value: number };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface IconComposerManifest {
|
|
34
|
+
fill: IconComposerFill;
|
|
35
|
+
groups: IconComposerGroup[];
|
|
36
|
+
'supported-platforms'?: unknown;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface IconComposerResult {
|
|
40
|
+
composedImagePath: string;
|
|
41
|
+
extractedBgColor: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Check if a path is an Apple Icon Composer .icon folder.
|
|
46
|
+
*/
|
|
47
|
+
export function isIconComposerFolder(inputPath: string): boolean {
|
|
48
|
+
if (!inputPath.endsWith('.icon')) return false;
|
|
49
|
+
try {
|
|
50
|
+
const stat = fs.statSync(inputPath);
|
|
51
|
+
if (!stat.isDirectory()) return false;
|
|
52
|
+
return fs.existsSync(path.join(inputPath, 'icon.json'));
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Parse a color string from icon.json to sRGB values.
|
|
60
|
+
* Supports formats: "display-p3:R,G,B,A" and "srgb:R,G,B,A" (values 0-1).
|
|
61
|
+
*/
|
|
62
|
+
function parseIconColor(color: string): {
|
|
63
|
+
r: number;
|
|
64
|
+
g: number;
|
|
65
|
+
b: number;
|
|
66
|
+
a: number;
|
|
67
|
+
} {
|
|
68
|
+
const match = color.match(/^[\w-]+:([\d.]+),([\d.]+),([\d.]+),([\d.]+)$/);
|
|
69
|
+
if (!match) {
|
|
70
|
+
throw new Error(`Unsupported color format: ${color}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
r: Math.round(parseFloat(match[1]) * 255),
|
|
75
|
+
g: Math.round(parseFloat(match[2]) * 255),
|
|
76
|
+
b: Math.round(parseFloat(match[3]) * 255),
|
|
77
|
+
a: parseFloat(match[4]),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Convert RGB values to hex string.
|
|
83
|
+
*/
|
|
84
|
+
function rgbToHex(r: number, g: number, b: number): string {
|
|
85
|
+
return (
|
|
86
|
+
'#' +
|
|
87
|
+
[r, g, b]
|
|
88
|
+
.map((c) => c.toString(16).padStart(2, '0'))
|
|
89
|
+
.join('')
|
|
90
|
+
.toUpperCase()
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Create an SVG string for a linear gradient background.
|
|
96
|
+
*/
|
|
97
|
+
function createGradientSvg(
|
|
98
|
+
colors: string[],
|
|
99
|
+
orientation: {
|
|
100
|
+
start: { x: number; y: number };
|
|
101
|
+
stop: { x: number; y: number };
|
|
102
|
+
},
|
|
103
|
+
size: number,
|
|
104
|
+
): string {
|
|
105
|
+
const parsedColors = colors.map(parseIconColor);
|
|
106
|
+
const x1 = (orientation.start.x * 100).toFixed(1);
|
|
107
|
+
const y1 = (orientation.start.y * 100).toFixed(1);
|
|
108
|
+
const x2 = (orientation.stop.x * 100).toFixed(1);
|
|
109
|
+
const y2 = (orientation.stop.y * 100).toFixed(1);
|
|
110
|
+
|
|
111
|
+
const stops = parsedColors
|
|
112
|
+
.map((c, i) => {
|
|
113
|
+
const offset =
|
|
114
|
+
parsedColors.length === 1 ? 0 : (i / (parsedColors.length - 1)) * 100;
|
|
115
|
+
return `<stop offset="${offset}%" stop-color="rgb(${c.r},${c.g},${c.b})" stop-opacity="${c.a}" />`;
|
|
116
|
+
})
|
|
117
|
+
.join('\n ');
|
|
118
|
+
|
|
119
|
+
return `<svg width="${size}" height="${size}" xmlns="http://www.w3.org/2000/svg">
|
|
120
|
+
<defs>
|
|
121
|
+
<linearGradient id="bg" x1="${x1}%" y1="${y1}%" x2="${x2}%" y2="${y2}%">
|
|
122
|
+
${stops}
|
|
123
|
+
</linearGradient>
|
|
124
|
+
</defs>
|
|
125
|
+
<rect width="${size}" height="${size}" fill="url(#bg)" />
|
|
126
|
+
</svg>`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Create the background buffer from the icon.json fill definition.
|
|
131
|
+
*/
|
|
132
|
+
async function createBackground(
|
|
133
|
+
fill: IconComposerFill,
|
|
134
|
+
): Promise<{ buffer: Buffer; bgColor: string }> {
|
|
135
|
+
if (fill['linear-gradient'] && fill.orientation) {
|
|
136
|
+
const colors = fill['linear-gradient'];
|
|
137
|
+
const svg = createGradientSvg(colors, fill.orientation, ICON_SIZE);
|
|
138
|
+
const buffer = await sharp(Buffer.from(svg)).png().toBuffer();
|
|
139
|
+
const firstColor = parseIconColor(colors[0]);
|
|
140
|
+
return {
|
|
141
|
+
buffer,
|
|
142
|
+
bgColor: rgbToHex(firstColor.r, firstColor.g, firstColor.b),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const solidColor = fill.solid ?? fill.color;
|
|
147
|
+
if (solidColor) {
|
|
148
|
+
const color = parseIconColor(solidColor);
|
|
149
|
+
const buffer = await sharp({
|
|
150
|
+
create: {
|
|
151
|
+
width: ICON_SIZE,
|
|
152
|
+
height: ICON_SIZE,
|
|
153
|
+
channels: 4,
|
|
154
|
+
background: {
|
|
155
|
+
r: color.r,
|
|
156
|
+
g: color.g,
|
|
157
|
+
b: color.b,
|
|
158
|
+
alpha: Math.round(color.a * 255),
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
.png()
|
|
163
|
+
.toBuffer();
|
|
164
|
+
return { buffer, bgColor: rgbToHex(color.r, color.g, color.b) };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Fallback: white background
|
|
168
|
+
const buffer = await sharp({
|
|
169
|
+
create: {
|
|
170
|
+
width: ICON_SIZE,
|
|
171
|
+
height: ICON_SIZE,
|
|
172
|
+
channels: 4,
|
|
173
|
+
background: { r: 255, g: 255, b: 255, alpha: 255 },
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
.png()
|
|
177
|
+
.toBuffer();
|
|
178
|
+
return { buffer, bgColor: '#FFFFFF' };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Render an Apple Icon Composer .icon folder into a composed 1024x1024 PNG.
|
|
183
|
+
* Returns the path to the composed image and the extracted background color.
|
|
184
|
+
*/
|
|
185
|
+
export async function renderIconComposerFolder(
|
|
186
|
+
iconFolderPath: string,
|
|
187
|
+
): Promise<IconComposerResult> {
|
|
188
|
+
const manifestPath = path.join(iconFolderPath, 'icon.json');
|
|
189
|
+
const assetsPath = path.join(iconFolderPath, 'Assets');
|
|
190
|
+
|
|
191
|
+
if (!fs.existsSync(manifestPath)) {
|
|
192
|
+
throw new Error(`icon.json not found in ${iconFolderPath}`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const manifestRaw = fs.readFileSync(manifestPath, 'utf-8');
|
|
196
|
+
const manifest: IconComposerManifest = JSON.parse(manifestRaw);
|
|
197
|
+
|
|
198
|
+
// Create background
|
|
199
|
+
const { buffer: backgroundBuffer, bgColor: extractedBgColor } =
|
|
200
|
+
await createBackground(manifest.fill);
|
|
201
|
+
|
|
202
|
+
// Build composite operations for each layer
|
|
203
|
+
const compositeOps: sharp.OverlayOptions[] = [];
|
|
204
|
+
|
|
205
|
+
for (const group of manifest.groups) {
|
|
206
|
+
for (const layer of group.layers) {
|
|
207
|
+
const imagePath = path.join(assetsPath, layer['image-name']);
|
|
208
|
+
if (!fs.existsSync(imagePath)) {
|
|
209
|
+
throw new Error(`Layer image not found: ${imagePath}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const meta = await sharp(imagePath).metadata();
|
|
213
|
+
if (!meta.width || !meta.height) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`Cannot read dimensions of layer: ${layer['image-name']}`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Scale relative to original image size
|
|
220
|
+
const scaledWidth = Math.round(meta.width * layer.position.scale);
|
|
221
|
+
const scaledHeight = Math.round(meta.height * layer.position.scale);
|
|
222
|
+
|
|
223
|
+
// Center on canvas with translation offset (in points/pixels)
|
|
224
|
+
const tx = layer.position['translation-in-points'][0];
|
|
225
|
+
const ty = layer.position['translation-in-points'][1];
|
|
226
|
+
let left = Math.round((ICON_SIZE - scaledWidth) / 2 + tx);
|
|
227
|
+
let top = Math.round((ICON_SIZE - scaledHeight) / 2 + ty);
|
|
228
|
+
|
|
229
|
+
let layerBuffer = await sharp(imagePath)
|
|
230
|
+
.resize(scaledWidth, scaledHeight, {
|
|
231
|
+
fit: 'contain',
|
|
232
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
233
|
+
})
|
|
234
|
+
.png()
|
|
235
|
+
.toBuffer();
|
|
236
|
+
|
|
237
|
+
// If the layer extends beyond the canvas, crop to visible region
|
|
238
|
+
if (
|
|
239
|
+
left < 0 ||
|
|
240
|
+
top < 0 ||
|
|
241
|
+
left + scaledWidth > ICON_SIZE ||
|
|
242
|
+
top + scaledHeight > ICON_SIZE
|
|
243
|
+
) {
|
|
244
|
+
const cropLeft = Math.max(0, -left);
|
|
245
|
+
const cropTop = Math.max(0, -top);
|
|
246
|
+
const cropRight = Math.min(scaledWidth, ICON_SIZE - left);
|
|
247
|
+
const cropBottom = Math.min(scaledHeight, ICON_SIZE - top);
|
|
248
|
+
const cropWidth = cropRight - cropLeft;
|
|
249
|
+
const cropHeight = cropBottom - cropTop;
|
|
250
|
+
|
|
251
|
+
if (cropWidth <= 0 || cropHeight <= 0) continue;
|
|
252
|
+
|
|
253
|
+
layerBuffer = await sharp(layerBuffer)
|
|
254
|
+
.extract({
|
|
255
|
+
left: cropLeft,
|
|
256
|
+
top: cropTop,
|
|
257
|
+
width: cropWidth,
|
|
258
|
+
height: cropHeight,
|
|
259
|
+
})
|
|
260
|
+
.png()
|
|
261
|
+
.toBuffer();
|
|
262
|
+
left = Math.max(0, left);
|
|
263
|
+
top = Math.max(0, top);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
compositeOps.push({ input: layerBuffer, left, top });
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Compose final image
|
|
271
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'iconwolf-compose-'));
|
|
272
|
+
const composedPath = path.join(tmpDir, 'composed-icon.png');
|
|
273
|
+
|
|
274
|
+
await sharp(backgroundBuffer)
|
|
275
|
+
.composite(compositeOps)
|
|
276
|
+
.png()
|
|
277
|
+
.toFile(composedPath);
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
composedImagePath: composedPath,
|
|
281
|
+
extractedBgColor,
|
|
282
|
+
};
|
|
283
|
+
}
|