Mercurial > MadButterfly
changeset 576:5a68e2bcea17
Set font styles for stext in Javascript
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 11 Jun 2010 01:21:10 +0800 |
parents | 97159102f886 |
children | d561b2415711 |
files | nodejs/font.cc nodejs/paints.cc nodejs/shapes.cc nodejs/testcase.js |
diffstat | 4 files changed, 66 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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<FunctionTemplate>::New(temp); } +/*! \brief Add properties to the template of runtime objects. + */ void xnjsmb_font_init_mb_rt_temp(Handle<FunctionTemplate> 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); }
--- 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(); }
--- 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<Value> +xnjsmb_shape_stext_set_style(const Arguments &args) { + HandleScope scope; + int argc = args.Length(); + Handle<Object> 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<FunctionTemplate> func_temp; + Handle<FunctionTemplate> meth_temp; Handle<ObjectTemplate> inst_temp; + Handle<ObjectTemplate> 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<FunctionTemplate>::New(func_temp); }
--- 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;