Skip to content

Unify the API for working with fonts when Pango is enabled. #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pfennigFont.addFace(fontFile('PfennigBold.ttf'), 'bold');
pfennigFont.addFace(fontFile('PfennigItalic.ttf'), 'normal', 'italic');
pfennigFont.addFace(fontFile('PfennigBoldItalic.ttf'), 'bold', 'italic');

var canvas = new Canvas(320, 320)
var canvas = new Canvas(320, 390)
var ctx = canvas.getContext('2d')

// Tell the ctx to use the font.
Expand All @@ -36,12 +36,15 @@ ctx.fillText('Quo Vaids?', 0, 70);
ctx.font = 'bold 50px pfennigFont';
ctx.fillText('Quo Vaids?', 0, 140);

ctx.font = 'italic 50px pfennigFont';
ctx.font = '50px pfennigFont';
ctx.fillText('Quo Vaids?', 0, 210);

ctx.font = 'bold italic 50px pfennigFont';
ctx.font = 'italic 50px pfennigFont';
ctx.fillText('Quo Vaids?', 0, 280);

ctx.font = 'bold italic 50px pfennigFont';
ctx.fillText('Quo Vaids?', 0, 350);

var out = fs.createWriteStream(__dirname + '/font.png');
var stream = canvas.createPNGStream();

Expand Down
26 changes: 26 additions & 0 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1822,11 +1822,37 @@ NAN_METHOD(Context2d::SetFontFace) {
double size = args[1]->NumberValue();

Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());

#if HAVE_PANGO

state_assign_fontFamily(context->state, face->freeTypeFace()->family_name);
context->state->fontSize = size;

FT_Long styleFlags = face->freeTypeFace()->style_flags;

PangoStyle s = PANGO_STYLE_NORMAL;
if (styleFlags & FT_STYLE_FLAG_ITALIC) {
s = PANGO_STYLE_ITALIC;
}
context->state->fontStyle = s;

PangoWeight w = PANGO_WEIGHT_NORMAL;
if (styleFlags & FT_STYLE_FLAG_BOLD) {
w = PANGO_WEIGHT_BOLD;
}
context->state->fontWeight = w;

context->setFontFromState();

#else

cairo_t *ctx = context->context();

cairo_set_font_size(ctx, size);
cairo_set_font_face(ctx, face->cairoFace());

#endif

NanReturnUndefined();
}
#endif
Expand Down
1 change: 1 addition & 0 deletions src/FontFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FontFace: public node::ObjectWrap {
:_ftFace(ftFace), _crFace(crFace) {}

inline cairo_font_face_t *cairoFace(){ return _crFace; }
inline FT_Face freeTypeFace(){ return _ftFace; }
private:
~FontFace();
FT_Face _ftFace;
Expand Down