comparison nodejs/svg.js @ 875:5d7c3c681851 abs_n_rel_center

Parse radial gradient
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 24 Sep 2010 23:55:02 +0800
parents 881efcd8a18f
children e6936110c48f
comparison
equal deleted inserted replaced
874:ec8d7e9c9642 875:5d7c3c681851
9 var _std_colors = { 9 var _std_colors = {
10 "white": [1, 1, 1], 10 "white": [1, 1, 1],
11 "black": [0, 0, 0], 11 "black": [0, 0, 0],
12 "red": [1, 0, 0] 12 "red": [1, 0, 0]
13 }; 13 };
14
15 function parse_color(color) {
16 var r, g, b;
17 var c;
18
19 if (color[0] == "#") {
20 r = parseInt(color.substring(1, 3), 16) / 255;
21 g = parseInt(color.substring(3, 5), 16) / 255;
22 b = parseInt(color.substring(5, 7), 16) / 255;
23 } else if(_std_colors[color]) {
24 c = _std_colors[color];
25 r = c[0];
26 g = c[1];
27 b = c[2];
28 } else {
29 r = g = b = 0;
30 }
31
32 return [r, g, b];
33 }
14 34
15 exports.loadSVG=function(mb_rt,root,filename) { 35 exports.loadSVG=function(mb_rt,root,filename) {
16 return new loadSVG(mb_rt, root, filename); 36 return new loadSVG(mb_rt, root, filename);
17 }; 37 };
18 38
25 var k; 45 var k;
26 var accu=[1,0,0,0,1,0]; 46 var accu=[1,0,0,0,1,0];
27 this.mb_rt = mb_rt; 47 this.mb_rt = mb_rt;
28 this.stop_ref={}; 48 this.stop_ref={};
29 this.gradients={}; 49 this.gradients={};
50 this.radials = {};
30 root.center=new Object(); 51 root.center=new Object();
31 root.center.x = 10000; 52 root.center.x = 10000;
32 root.center.y = 10000; 53 root.center.y = 10000;
33 54
34 if(_root.attr("width")) { 55 if(_root.attr("width")) {
81 if (s[i]<'0' || s[i] > '9') break; 102 if (s[i]<'0' || s[i] > '9') break;
82 fs = fs*10 + (s[i]-'0'); 103 fs = fs*10 + (s[i]-'0');
83 } 104 }
84 return fs; 105 return fs;
85 106
107 }
108
109 function parse_style(node) {
110 var style_attr;
111 var style;
112 var parts, part;
113 var kv, key, value;
114 var content = {};
115 var i;
116
117 style_attr = node.attr('style');
118 if(!style_attr)
119 return content;
120
121 style = style_attr.value();
122 parts = style.split(';');
123 for(i = 0; i < parts.length; i++) {
124 part = parts[i].trim();
125 if(part) {
126 kv = part.split(':');
127 key = kv[0].trim();
128 value = kv[1].trim();
129 content[key] = value;
130 }
131 }
132
133 return content;
86 } 134 }
87 135
88 function parseColor(c) 136 function parseColor(c)
89 { 137 {
90 if (c[0] == '#') { 138 if (c[0] == '#') {
178 c = _std_colors[color]; 226 c = _std_colors[color];
179 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha); 227 paint = this.mb_rt.paint_color_new(c[0], c[1], c[2], alpha);
180 } else if (color.substring(0,3) == 'url') { 228 } else if (color.substring(0,3) == 'url') {
181 var id = color.substring(5, color.length-1); 229 var id = color.substring(5, color.length-1);
182 var gr = this.gradients[id]; 230 var gr = this.gradients[id];
183 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]); 231 if(gr) {
232 paint = this.mb_rt.paint_linear_new(gr[0],gr[1],gr[2],gr[3]);
233 } else {
234 var radial = this.radials[id];
235 paint = this.mb_rt.paint_radial_new(radial[0],
236 radial[1],
237 radial[2]);
238 }
184 paint.set_stops(this.stop_ref[id]); 239 paint.set_stops(this.stop_ref[id]);
185 } else { 240 } else {
186 paint = this.mb_rt.paint_color_new(0,0,0,1); 241 paint = this.mb_rt.paint_color_new(0,0,0,1);
187 } 242 }
188 return paint; 243 return paint;
810 multiply(accu,coord); 865 multiply(accu,coord);
811 866
812 style = {}; 867 style = {};
813 parseGroupStyle(style, n); 868 parseGroupStyle(style, n);
814 if(style.opacity) { 869 if(style.opacity) {
815 sys.puts(style.opacity); 870 sys.puts("opacity=" + style.opacity);
816 coord.opacity=style.opacity; 871 coord.opacity=style.opacity;
817 } 872 }
818 873
819 for(k in nodes) { 874 for(k in nodes) {
820 var c = nodes[k].name(); 875 var c = nodes[k].name();
895 this._set_bbox(n, img); 950 this._set_bbox(n, img);
896 951
897 make_mbnames(this.mb_rt, n, img); 952 make_mbnames(this.mb_rt, n, img);
898 }; 953 };
899 954
955 function _parse_stops(n) {
956 var children;
957 var child;
958 var style;
959 var color;
960 var rgb;
961 var opacity;
962 var r, g, b, a;
963 var offset_atr, offset;
964 var stops = [];
965 var i;
966
967 children = n.childNodes();
968 for(i = 0; i < children.length; i++) {
969 child = children[i];
970 if(child.name() == "stop") {
971 style = parse_style(child);
972
973 color = style["stop-color"];
974 if(color) {
975 rgb = parse_color(color);
976 r = rgb[0];
977 g = rgb[1];
978 b = rgb[2];
979 }
980
981 opacity = style["stop-opacity"];
982 if(opacity)
983 a = parseFloat(opacity);
984 else
985 a = 1;
986 }
987
988 offset_attr = child.attr("offset");
989 if(offset_attr)
990 offset = parseFloat(offset_attr.value());
991 else
992 offset = 0;
993
994 stops.push([offset, r, g, b, a]);
995 }
996
997 return stops;
998 };
999
900 loadSVG.prototype._MB_parseLinearGradient=function(root,n) 1000 loadSVG.prototype._MB_parseLinearGradient=function(root,n)
901 { 1001 {
902 var id = n.attr('id'); 1002 var id = n.attr('id');
903 var k; 1003 var k;
904 var nodes = n.childNodes(); 1004 var nodes = n.childNodes();
910 var y2 = n.attr("y2"); 1010 var y2 = n.attr("y2");
911 var gr; 1011 var gr;
912 var color, opacity; 1012 var color, opacity;
913 var stops; 1013 var stops;
914 var r,g,b; 1014 var r,g,b;
915 stops=[]; 1015
916 for(k in nodes) { 1016 stops = _parse_stops(n);
917 var ss = nodes[k]; 1017
918 if (ss.name()=="stop") {
919 var style = ss.attr("style").value();
920 var items = style.split(';');
921 var off = parseInt(ss.attr('offset').value());
922 color = 'black';
923 opacity = 1;
924 for (i in items) {
925 it = items[i];
926 var f = it.split(':');
927 k = f[0];
928 v = f[1];
929 if (k == 'stop-color') {
930 color = v.substring(1);
931 if (v == 'white') {
932 r = 1;
933 g = 1;
934 b = 1;
935 } else if (v == 'black') {
936 r = 0;
937 g = 0;
938 b = 0;
939 } else {
940 r = parseInt(color.substring(0,2),16)/255.0;
941 g = parseInt(color.substring(2,4),16)/255.0;
942 b = parseInt(color.substring(4,6),16)/255.0;
943 }
944 } else if (k=='stop-opacity') {
945 opacity = parseFloat(v);
946 }
947 }
948 stops.push([off, r,g,b,opacity]);
949 }
950 }
951 var href = n.attr('href'); 1018 var href = n.attr('href');
952 if (href != null) { 1019 if (href != null) {
953 href = href.value(); 1020 href = href.value();
954 pstops = this.stop_ref[href.substring(1)]; 1021 var hrefid = href.substring(1);
1022 pstops = this.stop_ref[hrefid];
955 stops = pstops.concat(stops); 1023 stops = pstops.concat(stops);
1024
1025 var hrefgr = this.gradients[hrefid];
1026 if (typeof x1 == "undefined")
1027 x1 = hrefgr[0];
1028 if (typeof x2 == "undefined")
1029 x2 = hrefgr[2];
1030 if (typeof y1 == "undefined")
1031 y1 = hrefgr[1];
1032 if (typeof y2 == "undefined")
1033 y2 = hrefgr[3];
1034 sys.puts(hrefid);
1035 sys.puts([x1, y1, x2, y2]);
956 } 1036 }
957 id = id.value(); 1037 id = id.value();
958 this.stop_ref[id] = stops; 1038 this.stop_ref[id] = stops;
959 if (x1) 1039 if (x1)
960 x1 = parseFloat(x1.value()); 1040 x1 = parseFloat(x1.value());
965 if (y2) 1045 if (y2)
966 y2 = parseFloat(y2.value()); 1046 y2 = parseFloat(y2.value());
967 this.gradients[id] = [x1,y1,x2,y2]; 1047 this.gradients[id] = [x1,y1,x2,y2];
968 }; 1048 };
969 1049
1050 loadSVG.prototype._MB_parseRadialGradient = function(root,n) {
1051 var stops;
1052 var cx, cy;
1053 var id;
1054 var href;
1055 var r;
1056
1057 id = n.attr("id");
1058 if(!id)
1059 throw "Require an id";
1060 id = id.value();
1061
1062 stops = _parse_stops(n);
1063
1064 cx = n.attr("cx");
1065 if(!cx)
1066 throw "Miss cx attribute";
1067 cy = n.attr("cy");
1068 if(!cy)
1069 throw "Miss cy attribute";
1070 cx = parseFloat(cx.value());
1071 cy = parseFloat(cy.value());
1072
1073 r = n.attr("r");
1074 if(!r)
1075 throw "Miss r attribute";
1076 r = parseFloat(r.value());
1077
1078 href = n.attr("href");
1079 if(href) {
1080 href = href.value().substring(1);
1081 stops = this.stop_ref[href];
1082 }
1083
1084 this.radials[id] = [cx, cy, r];
1085 this.stop_ref[id] = stops;
1086 }
1087
970 loadSVG.prototype.parseDefs=function(root,n) 1088 loadSVG.prototype.parseDefs=function(root,n)
971 { 1089 {
972 var k; 1090 var k;
973 var nodes = n.childNodes(); 1091 var nodes = n.childNodes();
974 1092
975 for(k in nodes) { 1093 for(k in nodes) {
976 var name = nodes[k].name(); 1094 var name = nodes[k].name();
977 if (name == "linearGradient") { 1095 if (name == "linearGradient") {
978 this._MB_parseLinearGradient(root,nodes[k]); 1096 this._MB_parseLinearGradient(root,nodes[k]);
979 } 1097 } else if(name == "radialGradient") {
980 } 1098 this._MB_parseRadialGradient(root,nodes[k]);
981 }; 1099 }
982 1100 }
983 1101 };
1102
1103