Skip to content

Commit d2d6324

Browse files
committed
document Canvas#registerFont in readme+example
1 parent 0cf3bb4 commit d2d6324

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ canvas.toDataURL('image/jpeg', {opts...}, function(err, jpeg){ }); // see Canvas
170170
canvas.toDataURL('image/jpeg', quality, function(err, jpeg){ }); // spec-following; quality from 0 to 1
171171
```
172172

173+
### Canvas#registerFont for bundled fonts
174+
175+
It can be useful to use a custom font file if you are distributing code that uses node-canvas and a specific font. Or perhaps you are using it to do automated tests and you want the renderings to be the same across operating systems regardless of what fonts they have installed.
176+
177+
To do that, you should use `Canvas#registerFont`.
178+
179+
**You need to call it before the Canvas is created**
180+
181+
```javascript
182+
Canvas.registerFont('comicsans.ttf');
183+
184+
var canvas = new Canvas(500, 500),
185+
ctx = canvas.getContext('2d');
186+
187+
ctx.font = '12px "Comic Sans"';
188+
ctx.fillText(250, 10, 'Everyone hates this font :(');
189+
```
190+
173191
### CanvasRenderingContext2d#patternQuality
174192

175193
Given one of the values below will alter pattern (gradients, images, etc) render quality, defaults to _good_.

examples/font.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function fontFile(name) {
1111
return path.join(__dirname, '/pfennigFont/', name);
1212
}
1313

14+
// Pass each font, including all of its individual variants if there are any, to
15+
// `registerFont`. When you set `ctx.font`, refer to the styles and the family
16+
// name as it is embedded in the TTF. If you aren't sure, open the font in
17+
// FontForge and visit Element -> Font Information and copy the Family Name
1418
Canvas.registerFont(fontFile('Pfennig.ttf'));
1519
Canvas.registerFont(fontFile('PfennigBold.ttf'));
1620
Canvas.registerFont(fontFile('PfennigItalic.ttf'));

0 commit comments

Comments
 (0)