Mercurial > MadButterfly
comparison nodejs/svg.js @ 631:01e960bfc9ff
Implement text style parser
author | wycc |
---|---|
date | Wed, 21 Jul 2010 07:26:12 +0800 |
parents | 9f2080b68f8e |
children | e517bbefe0e9 |
comparison
equal
deleted
inserted
replaced
625:9f2080b68f8e | 631:01e960bfc9ff |
---|---|
24 if (n == null) return 0; | 24 if (n == null) return 0; |
25 var a = n.attr(name); | 25 var a = n.attr(name); |
26 if (a==null) return 0; | 26 if (a==null) return 0; |
27 return parseInt(a.value()); | 27 return parseInt(a.value()); |
28 } | 28 } |
29 | 29 function parsePointSize(s) |
30 function parseTextStyle(obj,n) | 30 { |
31 var fs=0; | |
32 var i; | |
33 | |
34 for(i=0;i<s.length;i++) { | |
35 if (s[i]<'0' || s[i] > '9') break; | |
36 fs = fs*10 + (s[i]-'0'); | |
37 } | |
38 return fs; | |
39 | |
40 } | |
41 | |
42 | |
43 function parseColor(c) | |
44 { | |
45 if (c[0] == '#') { | |
46 return parseInt(c.substring(1,3),16)<<16 | parseInt(c.substring(3,5),16)<<8 | parseInt(c.substring(5,7),16); | |
47 } | |
48 } | |
49 function parseTextStyle(style,n) | |
31 { | 50 { |
32 var attr; | 51 var attr; |
33 if (n) { | 52 if (n) { |
34 attr = n.attr('style'); | 53 attr = n.attr('style'); |
35 } else { | 54 } else { |
36 attr = null; | 55 attr = null; |
37 } | 56 } |
38 var fs = 20; | |
39 var family="ciurier"; | |
40 if (attr == null) { | 57 if (attr == null) { |
41 var paint = mb_rt.paint_color_new(1,1,1,1); | 58 return; |
42 var face=mb_rt.font_face_query(family, 2, 100); | |
43 obj.set_style([[20,face,fs]]); | |
44 return paint; | |
45 } | 59 } |
46 var f = attr.value().split(';'); | 60 var f = attr.value().split(';'); |
47 | 61 |
48 for(i in f) { | 62 for(i in f) { |
49 var kv = f[i].split(':'); | 63 var kv = f[i].split(':'); |
50 if (kv[0] == 'font-size') { | 64 if (kv[0] == 'font-size') { |
51 fs = parsePointSize(kv[1]); | 65 style.fs = parsePointSize(kv[1]); |
52 } else if (kv[0] == "font-style") { | 66 } else if (kv[0] == "font-style") { |
53 } else if (kv[0] == "font-weight") { | 67 } else if (kv[0] == "font-weight") { |
54 } else if (kv[0] == "fill") { | 68 } else if (kv[0] == "fill") { |
69 style.color = parseColor(kv[1]); | |
55 } else if (kv[0] == "fill-opacity") { | 70 } else if (kv[0] == "fill-opacity") { |
56 } else if (kv[0] == "stroke") { | 71 } else if (kv[0] == "stroke") { |
57 } else if (kv[0] == "stroke-width") { | 72 } else if (kv[0] == "stroke-width") { |
58 } else if (kv[0] == "stroke-linecap") { | 73 } else if (kv[0] == "stroke-linecap") { |
59 } else if (kv[0] == "stroke-linejoin") { | 74 } else if (kv[0] == "stroke-linejoin") { |
60 } else if (kv[0] == "stroke-lineopacity") { | 75 } else if (kv[0] == "stroke-lineopacity") { |
61 } else if (kv[0] == "font-family") { | 76 } else if (kv[0] == "font-family") { |
77 style.family = kv[1]; | |
62 } else if (kv[0] == "font-stretch") { | 78 } else if (kv[0] == "font-stretch") { |
63 } else if (kv[0] == "font-variant") { | 79 } else if (kv[0] == "font-variant") { |
64 } else if (kv[0] == "text-anchor") { | 80 } else if (kv[0] == "text-anchor") { |
65 } else if (kv[0] == "text-align") { | 81 } else if (kv[0] == "text-align") { |
66 } else if (kv[0] == "writing-mode") { | 82 } else if (kv[0] == "writing-mode") { |
67 } else if (kv[0] == "line-height") { | 83 } else if (kv[0] == "line-height") { |
68 sys.puts("Unknown style: "+kv[0]); | 84 sys.puts("Unknown style: "+kv[0]); |
69 } | 85 } |
70 } | 86 } |
71 var paint = mb_rt.paint_color_new(1,1,1,1); | 87 } |
72 var face=mb_rt.font_face_query(family, 2, 100); | 88 |
73 obj.set_style([[20,face,fs]]); | 89 function _MB_parseTSpan(coord, n,style) |
74 return paint; | |
75 } | |
76 | |
77 function _MB_parseTSpan(coord, n) | |
78 { | 90 { |
79 var x = getInteger(n,'x'); | 91 var x = getInteger(n,'x'); |
80 var y = getInteger(n,'y'); | 92 var y = getInteger(n,'y'); |
81 var tcoord = mb_rt.coord_new(coord); | 93 var tcoord = mb_rt.coord_new(coord); |
82 var nodes = n.childNodes(); | 94 var nodes = n.childNodes(); |
83 var k; | 95 var k; |
84 | 96 |
85 sys.puts(n.text()); | 97 sys.puts(n.text()); |
86 var obj = mb_rt.stext_new(n.text(),x,y); | 98 var obj = mb_rt.stext_new(n.text(),x,y); |
87 var paint = parseTextStyle(obj,n); | 99 parseTextStyle(style,n); |
88 paint.fill(obj); | 100 style.paint = mb_rt.paint_color_new(1,1,1,1); |
101 style.face=mb_rt.font_face_query(style.family, 2, 100); | |
102 obj.set_style([[20,style.face,style.fs]]); | |
103 style.paint.fill(obj); | |
89 tcoord.add_shape(obj); | 104 tcoord.add_shape(obj); |
90 for(k in nodes) { | 105 for(k in nodes) { |
91 var name = nodes[k].name(); | 106 var name = nodes[k].name(); |
92 if (name == "tspan") { | 107 if (name == "tspan") { |
93 _MB_parseTSpan(tcoord,nodes[k]); | 108 _MB_parseTSpan(tcoord,nodes[k]); |
99 function _MB_parseText(coord,id, n) | 114 function _MB_parseText(coord,id, n) |
100 { | 115 { |
101 var x = getInteger(n,'x'); | 116 var x = getInteger(n,'x'); |
102 var y = getInteger(n,'y'); | 117 var y = getInteger(n,'y'); |
103 var tcoord = mb_rt.coord_new(coord); | 118 var tcoord = mb_rt.coord_new(coord); |
104 //var paint = parseTextStyle(n); | 119 var style = new Object(); |
120 style.fs = 20; | |
121 style.family = 'courier'; | |
122 parseTextStyle(style,n); | |
105 var nodes = n.childNodes(); | 123 var nodes = n.childNodes(); |
106 var k; | 124 var k; |
107 for(k in nodes) { | 125 for(k in nodes) { |
108 var n = nodes[k].name(); | 126 var n = nodes[k].name(); |
109 if (n == "tspan") { | 127 if (n == "tspan") { |
110 _MB_parseTSpan(tcoord,nodes[k]); | 128 _MB_parseTSpan(tcoord,nodes[k],style); |
111 } else { | 129 } else { |
112 } | 130 } |
113 } | 131 } |
114 | 132 |
115 | 133 |