From 99f59089f8be59657c250b27a0f9c90e77456236 Mon Sep 17 00:00:00 2001 From: Joe Freeman Date: Sat, 4 Apr 2015 14:39:24 +0530 Subject: [PATCH 1/2] Unify the API for working with fonts when Pango is enabled. --- src/CanvasRenderingContext2d.cc | 26 ++++++++++++++++++++++++++ src/FontFace.h | 1 + 2 files changed, 27 insertions(+) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 1844cd943..574c6abfb 100755 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -1822,11 +1822,37 @@ NAN_METHOD(Context2d::SetFontFace) { double size = args[1]->NumberValue(); Context2d *context = ObjectWrap::Unwrap(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 diff --git a/src/FontFace.h b/src/FontFace.h index b1e13e40e..2fa12cdb9 100644 --- a/src/FontFace.h +++ b/src/FontFace.h @@ -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; From 4ed014575a38de7e0d8d30b5a8fda650d1108ee6 Mon Sep 17 00:00:00 2001 From: Joe Freeman Date: Sat, 4 Apr 2015 14:40:06 +0530 Subject: [PATCH 2/2] Update the font example to include a case where the style isn't specified. --- examples/font.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/font.js b/examples/font.js index 1a33f3b6a..b19d205d7 100644 --- a/examples/font.js +++ b/examples/font.js @@ -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. @@ -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();