This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
generate.js
86 lines (80 loc) · 2.82 KB
/
generate.js
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
'use strict'
let P = require('bluebird')
let glob = P.promisify(require('glob'))
let svg2png = require('svg2png')
let fs = P.promisifyAll(require('fs'))
let targets = [128, 256, 512, 1024]
let resolutions = {
logo: {height: 512, width: 512},
text: {height: 512, width: 1280 - 512},
joined: {height: 512, width: 1280}
}
for (let key of Object.keys(resolutions)) {
var obj = resolutions[key]
obj.ratio = obj.width / obj.height
}
// logo, text(, postfix)
let mixes = [
// logos only
['black'], ['ice'], ['ice-text'], ['white'], ['white-outline'],
// joined
['black', 'black'], ['ice', 'black'], ['ice', 'white', 'white'],
['white', 'white'], ['white-outline', 'white'],
// text only
[null, 'white'], [null, 'black']
]
glob('vector/*-vector-*.svg')
.map(name => {
return fs.readFileAsync(name).then(buff => {
return {name: name, content: buff}
})
})
.then(files => {
files.forEach(file => {
file.type = file.name.match(/-vector-(.+).svg/)[1]
})
let logos = {}
files.filter(file => file.name.match(/ipfs-logo/)).forEach(logo => {
logos[logo.type] = logo
})
let texts = {}
files.filter(file => file.name.match(/ipfs-text/)).forEach(text => {
texts[text.type] = text
})
return P.map(targets, target => P.map(mixes, mix => {
let name = 'ipfs-'
let logo = mix[0]
let text = mix[1]
let postfix = mix[2] ? '-' + mix[2] : ''
let type = 'logo'
if (!logo) {
type = 'text'
} else if (text) {
type = 'joined'
}
name += {text: 'text', joined: 'logo-text', logo: 'logo'}[type] + '-'
name += target + '-'
name += (logo || text)
name += postfix
name += '.png'
let height = target
let width = resolutions[type].ratio * height
let content = (type === 'logo' ? logos[logo] : texts[text]).content
let png
if (type !== 'joined') {
png = svg2png(content, {width: width, height: height})
} else {
let res = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="512" width="1280" version="1.1" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" viewBox="0 0 1280 512">'
res += logos[logo].content.toString().replace(/<\?xml(.*?)>/, '').replace(/<svg(.*?)>/, '').replace(/<\/svg(.*?)>/, '')
res += '<g transform="translate(512 0)">'
res += texts[text].content.toString().replace(/<\?xml(.*?)>/, '').replace(/<svg(.*?)>/, '').replace(/<\/svg(.*?)>/, '')
res += '</g>'
res += '</svg>'
png = svg2png(new Buffer(res), {width: width, height: height})
}
return png.then(buff => {
fs.writeFileAsync('raster-generated/' + name, buff)
})
})
)
}).all()