-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
55 lines (52 loc) · 1.92 KB
/
index.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
const fs = require('fs')
const canvas = require('canvas')
const path = require('path')
const dot = require('dot')
const { registerFont, createCanvas } = require('canvas')
const camelize = (str) => {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
const FontDir = path.join(__dirname, 'fonts')
const fonts = fs.readdirSync(FontDir)
const templates = fs.readdirSync('./template').map(v => {
const filename = v
const filePath = path.join('template', v)
const compiled = dot.template(fs.readFileSync(filePath).toString(), {
...dot.templateSettings,
strip: false
})
return {
filename, compiled
}
})
for(let FontFile of fonts) {
console.log(`=> Processing ${FontFile}`)
const FontFamily = path.parse(FontFile).name
const ShortPackageName = camelize(FontFamily)
const PackageName = `@canvas-fonts/${FontFamily.split(' ').join('-').toLowerCase()}`
const URLName = encodeURI(ShortPackageName)
const FontPreview = `https://github.com/retrohacker/canvas-fonts/raw/master/previews/${URLName}.png`
const fontDefinition = { FontFile, FontFamily, ShortPackageName, PackageName, FontPreview }
const PackageDir = path.join(__dirname, 'packages', ShortPackageName)
fs.mkdirSync(PackageDir, { recursive: true })
fs.copyFileSync(
path.join(FontDir, FontFile),
path.join(PackageDir, FontFile)
)
for (let Template of templates) {
fs.writeFileSync(
path.join(PackageDir, Template.filename),
Template.compiled(fontDefinition)
)
}
const PreviewDir = path.join(__dirname, 'previews')
registerFont(path.join(__dirname, 'fonts', FontFile), { family: FontFamily })
const canvas = createCanvas(400, 48)
const ctx = canvas.getContext('2d')
ctx.font = `24px "${FontFamily}"`
ctx.fillText(FontFamily, 5, 30)
fs.mkdirSync(PreviewDir, { recursive: true })
fs.writeFileSync(path.join(PreviewDir, `${URLName}.png`), canvas.toBuffer())
}