-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add generator API for creating icons
- Loading branch information
Showing
9 changed files
with
255 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const renderSvg = require('svg-render'); | ||
const toIco = require('@catdad/to-ico'); | ||
const { Icns, IcnsImage } = require('@fiahfy/icns'); | ||
const { createCanvas, loadImage } = require('canvas'); | ||
const cheerio = require('cheerio'); | ||
|
||
const { toArray } = require('./helpers.js'); | ||
|
||
const createPng = async (buffers, size) => { | ||
const canvas = createCanvas(size, size); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
for (const buffer of buffers) { | ||
const png = await renderSvg({ buffer, width: size, height: size }); | ||
const image = await loadImage(png); | ||
ctx.drawImage(image, 0, 0); | ||
} | ||
|
||
return canvas.toBuffer('image/png'); | ||
}; | ||
|
||
const createIco = async svg => await toIco( | ||
await Promise.all([16, 24, 32, 48, 64, 128, 256].map(size => createPng(svg, size))) | ||
); | ||
|
||
const createIcns = async svg => { | ||
const icns = new Icns(); | ||
|
||
for (const { osType, size, format } of Icns.supportedIconTypes) { | ||
if (format === 'PNG') { | ||
icns.append(IcnsImage.fromPNG(await createPng(svg, size), osType)); | ||
} | ||
} | ||
|
||
return icns.data; | ||
}; | ||
|
||
const createSvg = async svg => { | ||
const size = 100; | ||
|
||
const sizeNormalized = svg.map(s => { | ||
const $ = cheerio.load(s.toString(), { xmlMode: true }); | ||
const $svg = $('svg'); | ||
$svg.attr('width', size); | ||
$svg.attr('height', size); | ||
$svg.attr('version', '1.1'); | ||
$svg.attr('xmlns', 'http://www.w3.org/2000/svg'); | ||
|
||
return $.xml('svg'); | ||
}); | ||
|
||
return [ | ||
`<svg viewBox="0 0 ${size} ${size}" version="1.1" xmlns="http://www.w3.org/2000/svg">`, | ||
...sizeNormalized, | ||
'</svg>' | ||
].join('\n'); | ||
}; | ||
|
||
const getInputArray = input => { | ||
return toArray(input).map(i => Buffer.isBuffer(i) ? i : Buffer.from(i)); | ||
}; | ||
|
||
async function* generateIcons(input, { icns = true, ico = true, png = true, svg = true, pngSizes = [32, 256, 512] } = {}) { | ||
const buffers = getInputArray(input); | ||
|
||
if (svg) { | ||
yield { | ||
name: 'icon.svg', | ||
ext: 'svg', | ||
buffer: Buffer.from(await createSvg(buffers)) | ||
}; | ||
} | ||
|
||
if (ico) { | ||
yield { | ||
name: 'icon.ico', | ||
ext: 'ico', | ||
buffer: Buffer.from(await createIco(buffers)) | ||
}; | ||
} | ||
|
||
if (icns) { | ||
yield { | ||
name: 'icon.icns', | ||
ext: 'icns', | ||
buffer: Buffer.from(await createIcns(buffers)) | ||
}; | ||
} | ||
|
||
if (png) { | ||
for (let size of pngSizes) { | ||
yield { | ||
name: `${size}x${size}.png`, | ||
ext: 'png', | ||
buffer: Buffer.from(await createPng(buffers, size)), | ||
size | ||
}; | ||
} | ||
} | ||
} | ||
|
||
module.exports = generateIcons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = require('./maker.js'); | ||
module.exports.generateIcons = require('./generator.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.