Skip to content

Commit 684731d

Browse files
committed
implement Canvas::registerFont(ttf)
this is implemented differently depending on the operating system. for MacOS, CTFontManagerRegisterFontsForURL is the call to add a temporary font. for Linux we use FontConfig, and for Windows use AddFontResourceEx. all have been tested successfully also updated examples/font.js
1 parent abaa9e6 commit 684731d

File tree

6 files changed

+57
-19
lines changed

6 files changed

+57
-19
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
'src/color.cc',
5454
'src/Image.cc',
5555
'src/ImageData.cc',
56+
'src/register_font.cc',
5657
'src/init.cc'
5758
],
5859
'conditions': [

examples/font.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,32 @@
44
*/
55

66
var Canvas = require('../lib/canvas')
7-
, canvas = new Canvas(320, 320)
8-
, Font = Canvas.Font
9-
, ctx = canvas.getContext('2d')
107
, fs = require('fs')
118
, path = require("path");
129

13-
if (!Font) {
14-
throw new Error('Need to compile with font support');
15-
}
16-
1710
function fontFile(name) {
1811
return path.join(__dirname, '/pfennigFont/', name);
1912
}
2013

21-
var pfennigFont = new Font('pfennigFont', fontFile('Pfennig.ttf'));
22-
pfennigFont.addFace(fontFile('PfennigBold.ttf'), 'bold');
23-
pfennigFont.addFace(fontFile('PfennigItalic.ttf'), 'normal', 'italic');
24-
pfennigFont.addFace(fontFile('PfennigBoldItalic.ttf'), 'bold', 'italic');
25-
26-
var canvas = new Canvas(320, 320)
27-
var ctx = canvas.getContext('2d')
14+
Canvas.registerFont(fontFile('Pfennig.ttf'));
15+
Canvas.registerFont(fontFile('PfennigBold.ttf'));
16+
Canvas.registerFont(fontFile('PfennigItalic.ttf'));
17+
Canvas.registerFont(fontFile('PfennigBoldItalic.ttf'));
2818

29-
// Tell the ctx to use the font.
30-
ctx.addFont(pfennigFont);
19+
var canvas = new Canvas(320, 320);
20+
var ctx = canvas.getContext('2d');
3121

3222
ctx.font = 'normal normal 50px Helvetica';
3323

3424
ctx.fillText('Quo Vaids?', 0, 70);
3525

36-
ctx.font = 'bold 50px pfennigFont';
26+
ctx.font = 'bold 50px Pfennig';
3727
ctx.fillText('Quo Vaids?', 0, 140);
3828

39-
ctx.font = 'italic 50px pfennigFont';
29+
ctx.font = 'italic 50px Pfennig';
4030
ctx.fillText('Quo Vaids?', 0, 210);
4131

42-
ctx.font = 'bold italic 50px pfennigFont';
32+
ctx.font = 'bold italic 50px Pfennig';
4333
ctx.fillText('Quo Vaids?', 0, 280);
4434

4535
var out = fs.createWriteStream(__dirname + '/font.png');

lib/canvas.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var canvas = require('./bindings')
1313
, Canvas = canvas.Canvas
1414
, Image = canvas.Image
1515
, cairoVersion = canvas.cairoVersion
16+
, registerFont = canvas.registerFont
1617
, Context2d = require('./context2d')
1718
, PNGStream = require('./pngstream')
1819
, JPEGStream = require('./jpegstream')
@@ -33,6 +34,12 @@ var Canvas = exports = module.exports = Canvas;
3334

3435
exports.version = packageJson.version;
3536

37+
/**
38+
* Register custom font
39+
*/
40+
41+
exports.registerFont = registerFont;
42+
3643
/**
3744
* Cairo version.
3845
*/

src/init.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,27 @@
1212
#include "CanvasGradient.h"
1313
#include "CanvasPattern.h"
1414
#include "CanvasRenderingContext2d.h"
15+
#include "register_font.h"
1516

1617
// Compatibility with Visual Studio versions prior to VS2015
1718
#if defined(_MSC_VER) && _MSC_VER < 1900
1819
#define snprintf _snprintf
1920
#endif
2021

22+
NAN_METHOD(register_font_js) {
23+
if (!info[0]->IsString()) {
24+
return Nan::ThrowError("Wrong argument type");
25+
}
26+
27+
String::Utf8Value filePath(info[0]);
28+
29+
if (!register_font((unsigned char*) *filePath)) {
30+
Nan::ThrowError("Could not load font to the system's font host");
31+
} else {
32+
printf("registered %s ok\n", *filePath);
33+
}
34+
}
35+
2136
NAN_MODULE_INIT(init) {
2237
Canvas::Initialize(target);
2338
Image::Initialize(target);
@@ -26,6 +41,8 @@ NAN_MODULE_INIT(init) {
2641
Gradient::Initialize(target);
2742
Pattern::Initialize(target);
2843

44+
Nan::SetMethod(target, "registerFont", register_font_js);
45+
2946
target->Set(Nan::New<String>("cairoVersion").ToLocalChecked(), Nan::New<String>(cairo_version_string()).ToLocalChecked());
3047
#ifdef HAVE_JPEG
3148

src/register_font.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifdef __APPLE__
2+
#include <CoreFoundation/CoreFoundation.h>
3+
#include <CoreText/CoreText.h>
4+
#elif defined(_WIN32)
5+
#include <windows.h>
6+
#else
7+
#include <fontconfig/fontconfig.h>
8+
#endif
9+
10+
bool
11+
register_font(unsigned char *filepath) {
12+
#ifdef __APPLE__
13+
CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false);
14+
return CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
15+
#elif defined(_WIN32)
16+
return AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0;
17+
#else
18+
return FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
19+
#endif
20+
}
21+

src/register_font.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bool register_font(unsigned char *filepath);
2+

0 commit comments

Comments
 (0)