Skip to content

Commit

Permalink
document Canvas#registerFont in readme+example
Browse files Browse the repository at this point in the history
  • Loading branch information
chearon committed Feb 11, 2016
1 parent 0cf3bb4 commit d2d6324
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ canvas.toDataURL('image/jpeg', {opts...}, function(err, jpeg){ }); // see Canvas
canvas.toDataURL('image/jpeg', quality, function(err, jpeg){ }); // spec-following; quality from 0 to 1
```

### Canvas#registerFont for bundled fonts

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.

To do that, you should use `Canvas#registerFont`.

**You need to call it before the Canvas is created**

```javascript
Canvas.registerFont('comicsans.ttf');

var canvas = new Canvas(500, 500),
ctx = canvas.getContext('2d');

ctx.font = '12px "Comic Sans"';
ctx.fillText(250, 10, 'Everyone hates this font :(');
```

### CanvasRenderingContext2d#patternQuality

Given one of the values below will alter pattern (gradients, images, etc) render quality, defaults to _good_.
Expand Down
4 changes: 4 additions & 0 deletions examples/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ 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'));
Canvas.registerFont(fontFile('PfennigBold.ttf'));
Canvas.registerFont(fontFile('PfennigItalic.ttf'));
Expand Down

0 comments on commit d2d6324

Please sign in to comment.