# HG changeset patch # User Thinker K.F. Li # Date 1276190470 -28800 # Node ID 5a68e2bcea17bbe0d5dff0efea264c21bbab2649 # Parent 97159102f886850780ea3a9709cd5b15989d8abf Set font styles for stext in Javascript diff -r 97159102f886 -r 5a68e2bcea17 nodejs/font.cc --- a/nodejs/font.cc Thu Jun 10 09:36:41 2010 +0800 +++ b/nodejs/font.cc Fri Jun 11 01:21:10 2010 +0800 @@ -69,6 +69,8 @@ xnjsmb_font_face_temp = Persistent::New(temp); } +/*! \brief Add properties to the template of runtime objects. + */ void xnjsmb_font_init_mb_rt_temp(Handle mb_rt_temp) { HandleScope scope; @@ -83,5 +85,5 @@ rt_proto_temp = mb_rt_temp->PrototypeTemplate(); query_func_temp = FunctionTemplate::New(xnjsmb_font_face_query); - SET(rt_proto_temp, "font_fact_query", query_func_temp); + SET(rt_proto_temp, "font_face_query", query_func_temp); } diff -r 97159102f886 -r 5a68e2bcea17 nodejs/paints.cc --- a/nodejs/paints.cc Thu Jun 10 09:36:41 2010 +0800 +++ b/nodejs/paints.cc Fri Jun 11 01:21:10 2010 +0800 @@ -42,7 +42,8 @@ rdman_paint_fill(rdman, paint, sh); - rdman_shape_changed(rdman, sh); + if(sh_get_coord(sh)) + rdman_shape_changed(rdman, sh); return Null(); } @@ -77,7 +78,8 @@ rdman_paint_stroke(rdman, paint, sh); - rdman_shape_changed(rdman, sh); + if(sh_get_coord(sh)) + rdman_shape_changed(rdman, sh); return Null(); } diff -r 97159102f886 -r 5a68e2bcea17 nodejs/shapes.cc --- a/nodejs/shapes.cc Thu Jun 10 09:36:41 2010 +0800 +++ b/nodejs/shapes.cc Fri Jun 11 01:21:10 2010 +0800 @@ -84,7 +84,8 @@ ASSERT(rt != NULL); rdman = xnjsmb_rt_rdman(rt); - rdman_shape_changed(rdman, sh); + if(sh_get_coord(sh)) + rdman_shape_changed(rdman, sh); } static void @@ -266,12 +267,58 @@ return stext_obj; } +/*! \brief Setup style blocks for a stext. + * + * It defines font style and size for blocks of text message. + * + * \param blks is a list (n_char, face, font size) tuples. + */ +static Handle +xnjsmb_shape_stext_set_style(const Arguments &args) { + HandleScope scope; + int argc = args.Length(); + Handle self = args.This(); + shape_t *sh; + Array *blksobj; + Array *blkobj; + mb_style_blk_t *blks; + int nblks; + int i; + int r; + + if(argc != 1) + THROW("Invalid number of arguments (!= 1)"); + if(!args[0]->IsArray()) + THROW("Invalid type of the argument"); + + blksobj = Array::Cast(*args[0]); + nblks = blksobj->Length(); + blks = new mb_style_blk_t[nblks]; + for(i = 0; i < nblks; i++) { + blkobj = Array::Cast(*blksobj->Get(i)); + blks[i].n_chars = blkobj->Get(0)->ToInt32()->Value(); + blks[i].face = (mb_font_face_t *)UNWRAP(blkobj->Get(1)->ToObject()); + blks[i].font_sz = blkobj->Get(2)->ToNumber()->Value(); + } + + sh = (shape_t *)UNWRAP(self); + r = sh_stext_set_style(sh, blks, nblks); + if(r != 0) + THROW("Unknown error"); + + delete blks; + + return Null(); +} + /*! \brief Initialize function template for stext objects. */ void xnjsmb_init_stext_temp(void) { Handle func_temp; + Handle meth_temp; Handle inst_temp; + Handle proto_temp; func_temp = FunctionTemplate::New(xnjsmb_shape_stext); func_temp->Inherit(xnjsmb_shape_temp); @@ -279,6 +326,10 @@ inst_temp = func_temp->InstanceTemplate(); inst_temp->SetInternalFieldCount(1); + + proto_temp = func_temp->PrototypeTemplate(); + meth_temp = FunctionTemplate::New(xnjsmb_shape_stext_set_style); + SET(proto_temp, "set_style", meth_temp); xnjsmb_shape_stext_temp = Persistent::New(func_temp); } diff -r 97159102f886 -r 5a68e2bcea17 nodejs/testcase.js --- a/nodejs/testcase.js Thu Jun 10 09:36:41 2010 +0800 +++ b/nodejs/testcase.js Fri Jun 11 01:21:10 2010 +0800 @@ -26,6 +26,13 @@ path.stroke_width = 2; sys.puts(path.stroke_width); +var face = mb_rt.font_face_query("courier", 2, 100); +var blks = [[5, face, 20]]; +var stext = mb_rt.stext_new("Hello", 100, 50); +stext.set_style(blks); +paint.fill(stext); +coord.add_shape(stext); + mb_rt.redraw_all(); var i = 0;