-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
font.js
34 lines (25 loc) · 1.29 KB
/
font.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
const fs = require('fs')
const path = require('path')
const Canvas = require('..')
function fontFile (name) {
return path.join(__dirname, '/pfennigFont/', name)
}
// Pass each font, including all of its individual variants if there are any, to
// `registerFont`. When you set `ctx.font`, refer to the styles and the family
// name as it is embedded in the TTF. If you aren't sure, open the font in
// FontForge and visit Element -> Font Information and copy the Family Name
Canvas.registerFont(fontFile('Pfennig.ttf'), { family: 'pfennigFont' })
Canvas.registerFont(fontFile('PfennigBold.ttf'), { family: 'pfennigFont', weight: 'bold' })
Canvas.registerFont(fontFile('PfennigItalic.ttf'), { family: 'pfennigFont', style: 'italic' })
Canvas.registerFont(fontFile('PfennigBoldItalic.ttf'), { family: 'pfennigFont', weight: 'bold', style: 'italic' })
const canvas = Canvas.createCanvas(320, 320)
const ctx = canvas.getContext('2d')
ctx.font = 'normal normal 50px Helvetica'
ctx.fillText('Quo Vaids?', 0, 70)
ctx.font = 'bold 50px pfennigFont'
ctx.fillText('Quo Vaids?', 0, 140, 100)
ctx.font = 'italic 50px pfennigFont'
ctx.fillText('Quo Vaids?', 0, 210)
ctx.font = 'bold italic 50px pfennigFont'
ctx.fillText('Quo Vaids?', 0, 280)
canvas.createPNGStream().pipe(fs.createWriteStream(path.join(__dirname, 'font.png')))