-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·261 lines (221 loc) · 7.62 KB
/
Copy pathindex.js
File metadata and controls
executable file
·261 lines (221 loc) · 7.62 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
const { Command } = require('commander');
const { optimize } = require('svgo');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const program = new Command();
// Custom plugin to ensure 1:1 aspect ratio and center content
const ensureSquareAspectRatio = {
name: 'ensureSquareAspectRatio',
fn: () => {
return {
element: {
enter: (node) => {
if (node.name === 'svg') {
// Get current viewBox or calculate from width/height
let viewBox = node.attributes.viewBox;
if (!viewBox) {
const width = parseFloat(node.attributes.width) || 800;
const height = parseFloat(node.attributes.height) || 800;
viewBox = `0 0 ${width} ${height}`;
}
// Parse viewBox values
const [x, y, w, h] = viewBox.split(' ').map(parseFloat);
// Make it square (1:1 aspect ratio) by using the larger dimension + 5% padding
const maxDimension = Math.max(w, h);
const padding = maxDimension * 0.05; // 5% padding
const size = maxDimension + (padding * 2); // Add padding to both sides
// Center the content within the padded square
const offsetX = (size - w) / 2;
const offsetY = (size - h) / 2;
// Update viewBox to be square, centered, and padded
node.attributes.viewBox = `${x - offsetX} ${y - offsetY} ${size} ${size}`;
// Remove width and height attributes to make it responsive
delete node.attributes.width;
delete node.attributes.height;
}
}
}
};
}
};
const appleCompliantConfig = {
plugins: [
// Ensure 1:1 aspect ratio first
ensureSquareAspectRatio,
// Remove disallowed elements using removeUnknownsAndDefaults
{
name: 'removeUnknownsAndDefaults',
params: {
unknownContent: false,
unknownAttrs: false,
defaultAttrs: false,
uselessOverrides: false,
keepDataAttrs: false,
keepAriaAttrs: false,
keepRoleAttr: false
}
},
// Remove disallowed attributes (but keep inline style attributes)
{
name: 'removeAttrs',
params: {
attrs: [
'accumulate', 'additive', 'begin', 'by', 'calcMode', 'filter',
'from', 'href', 'keySplines', 'keyTimes', 'origin',
'repeatCount', 'target', 'xlink:href'
]
}
},
// Remove event handler attributes (on*)
{
name: 'removeAttrs',
params: {
attrs: '(on.*)'
}
},
// Ensure proper SVG namespace
{
name: 'addAttributesToSVGElement',
params: {
attributes: {
xmlns: 'http://www.w3.org/2000/svg'
}
}
},
// Remove specific disallowed elements
'removeScriptElement',
'removeStyleElement',
'convertStyleToAttrs',
// Standard SVGO optimizations while maintaining compliance
'removeDoctype',
'removeXMLProcInst',
'removeComments',
'removeMetadata',
'removeTitle',
'removeDesc',
'removeUselessDefs',
'removeEditorsNSData',
'removeEmptyAttrs',
'removeHiddenElems',
'removeEmptyText',
'removeEmptyContainers',
'convertPathData',
'convertTransform',
'removeNonInheritableGroupAttrs',
'removeUnusedNS',
'cleanupNumericValues',
'moveElemsAttrsToGroup',
'moveGroupAttrsToElems',
'collapseGroups',
'mergePaths',
'convertShapeToPath',
'sortAttrs',
'removeDimensions'
]
};
function validateSVG(filePath) {
if (!fs.existsSync(filePath)) {
console.error(chalk.red(`Error: File "${filePath}" does not exist.`));
process.exit(1);
}
const fileExtension = path.extname(filePath).toLowerCase();
if (fileExtension !== '.svg') {
console.error(chalk.red(`Error: File "${filePath}" is not an SVG file.`));
process.exit(1);
}
}
function generateOutputPath(inputPath, outputDir) {
const baseName = path.basename(inputPath, '.svg');
const outputFileName = `${baseName}-apple-optimized.svg`;
return outputDir ? path.join(outputDir, outputFileName) : outputFileName;
}
async function optimizeSVG(inputPath, outputPath, options = {}) {
try {
console.log(chalk.blue(`Optimizing SVG for Apple brand compliance...`));
console.log(chalk.gray(`Input: ${inputPath}`));
console.log(chalk.gray(`Output: ${outputPath}`));
const svgString = fs.readFileSync(inputPath, 'utf8');
const config = options.preserveViewBox ?
{ ...appleCompliantConfig, plugins: appleCompliantConfig.plugins.map(plugin =>
plugin.name === 'removeViewBox' ? { ...plugin, active: false } : plugin
)} : appleCompliantConfig;
const result = optimize(svgString, {
path: inputPath,
...config
});
if (result.error) {
throw new Error(result.error);
}
const optimizedSVG = result.data;
fs.writeFileSync(outputPath, optimizedSVG);
const originalSize = Buffer.byteLength(svgString, 'utf8');
const optimizedSize = Buffer.byteLength(optimizedSVG, 'utf8');
const reduction = ((originalSize - optimizedSize) / originalSize * 100).toFixed(1);
console.log(chalk.green(`✓ SVG optimized successfully!`));
console.log(chalk.yellow(`Original size: ${originalSize} bytes`));
console.log(chalk.yellow(`Optimized size: ${optimizedSize} bytes`));
console.log(chalk.yellow(`Size reduction: ${reduction}%`));
console.log(chalk.green(`Apple-compliant SVG saved to: ${outputPath}`));
if (options.validate) {
validateAppleCompliance(optimizedSVG);
}
} catch (error) {
console.error(chalk.red(`Error optimizing SVG: ${error.message}`));
process.exit(1);
}
}
function validateAppleCompliance(svgContent) {
const issues = [];
if (svgContent.includes('raster')) {
issues.push('Contains raster images (not recommended for Apple brand assets)');
}
if (svgContent.includes('data:')) {
issues.push('Contains embedded data URIs (may affect performance)');
}
if (svgContent.match(/<style[^>]*>/)) {
issues.push('Contains style tags (prefer inline styles for Apple assets)');
}
if (!svgContent.includes('viewBox')) {
issues.push('Missing viewBox attribute (recommended for scalability)');
}
if (issues.length > 0) {
console.log(chalk.yellow('\n⚠️ Apple Brand Compliance Warnings:'));
issues.forEach(issue => {
console.log(chalk.yellow(` • ${issue}`));
});
} else {
console.log(chalk.green('\n✓ SVG passes basic Apple brand compliance checks'));
}
}
program
.name('svg-apple')
.description('Optimizer for Apple SVG logos to ensure brand compliance')
.version('0.1.0');
program
.argument('<input>', 'Input SVG file path')
.option('-o, --output <path>', 'Output file path or directory')
.option('--preserve-viewbox', 'Preserve viewBox attribute for scalability')
.option('--validate', 'Validate Apple brand compliance after optimization')
.action(async (input, options) => {
validateSVG(input);
let outputPath;
if (options.output) {
if (fs.existsSync(options.output) && fs.statSync(options.output).isDirectory()) {
outputPath = generateOutputPath(input, options.output);
} else {
outputPath = options.output;
}
} else {
outputPath = generateOutputPath(input);
}
await optimizeSVG(input, outputPath, {
preserveViewBox: options.preserveViewbox,
validate: options.validate
});
});
if (require.main === module) {
program.parse();
}
module.exports = { optimizeSVG, appleCompliantConfig };