File size: 4,588 Bytes
1ee6ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node

import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import sharp from 'sharp';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Configuration
const TYPOGRAPHY_BASE = __dirname;
const GENERATED_DIR = path.join(TYPOGRAPHY_BASE, 'generated');
const SVGS_DIR = path.join(GENERATED_DIR, 'svgs');
const PNGS_DIR = path.join(GENERATED_DIR, 'pngs');
const PNG_SIZE = 40; // Final size 40x40 pixels

/**
 * Converts an SVG to PNG
 */
async function convertSvgToPng(svgPath, pngPath) {
    try {
        const svgBuffer = await fs.readFile(svgPath);

        await sharp(svgBuffer)
            .resize(PNG_SIZE, PNG_SIZE, {
                fit: 'contain',
                background: { r: 255, g: 255, b: 255, alpha: 1 } // White background
            })
            .flatten({ background: { r: 255, g: 255, b: 255 } }) // Force white background
            .png()
            .toFile(pngPath);

        return true;
    } catch (error) {
        console.error(`❌ Error during conversion ${svgPath}:`, error.message);
        return false;
    }
}

/**
 * Generates PNGs for all SVGs
 */
async function generatePNGsForAllSVGs() {
    console.log(`πŸ–ΌοΈ  Generating ${PNG_SIZE}x${PNG_SIZE} PNGs for all SVGs\n`);

    try {
        // Create PNG folder if necessary
        await fs.mkdir(PNGS_DIR, { recursive: true });

        // Read all SVG files
        const svgFiles = await fs.readdir(SVGS_DIR);
        const svgFilesFiltered = svgFiles.filter(file => file.endsWith('.svg'));

        if (svgFilesFiltered.length === 0) {
            console.error('❌ No SVG files found in', SVGS_DIR);
            process.exit(1);
        }

        console.log(`πŸ“ Found ${svgFilesFiltered.length} SVG files`);

        const results = [];

        for (let i = 0; i < svgFilesFiltered.length; i++) {
            const svgFile = svgFilesFiltered[i];
            const svgPath = path.join(SVGS_DIR, svgFile);

            // Create PNG filename
            const pngFile = svgFile.replace('.svg', '.png');
            const pngPath = path.join(PNGS_DIR, pngFile);

            // Extract font name from filename
            const fontId = svgFile.replace('_a.svg', '');
            const fontFamily = fontId.replace(/_/g, ' ');

            console.log(`[${i + 1}/${svgFilesFiltered.length}] πŸ”„ Converting "${fontFamily}"...`);

            try {
                const success = await convertSvgToPng(svgPath, pngPath);

                if (success) {
                    console.log(`βœ… PNG generated: ${fontFamily} (${PNG_SIZE}x${PNG_SIZE})`);
                    results.push({
                        fontFamily,
                        fontId,
                        status: 'success',
                        pngPath: `/content/embeds/typography/font_pngs/${pngFile}`,
                        dimensions: {
                            width: PNG_SIZE,
                            height: PNG_SIZE
                        }
                    });
                } else {
                    results.push({
                        fontFamily,
                        fontId,
                        status: 'error',
                        error: 'SVG to PNG conversion failed'
                    });
                }

            } catch (error) {
                console.error(`❌ Error for ${fontFamily}:`, error.message);
                results.push({
                    fontFamily,
                    fontId,
                    status: 'error',
                    error: error.message
                });
            }
        }

        // Display final statistics
        const successful = results.filter(r => r.status === 'success').length;
        const errors = results.filter(r => r.status === 'error').length;

        console.log('\nπŸ“Š Final statistics:');
        console.log(`βœ… PNGs generated successfully: ${successful}`);
        console.log(`❌ Errors: ${errors}`);
        console.log(`πŸ“‹ Total processed: ${results.length}`);

        if (errors > 0) {
            console.log('\n❌ Fonts with errors:');
            results
                .filter(r => r.status === 'error')
                .forEach(r => console.log(`  - ${r.fontFamily}: ${r.error}`));
        }

    } catch (error) {
        console.error('πŸ’₯ Fatal error:', error.message);
        process.exit(1);
    }
}

// Execute script if run directly
if (import.meta.url === `file://${process.argv[1]}`) {
    generatePNGsForAllSVGs();
}

export { generatePNGsForAllSVGs };