Mercurial > MadButterfly
comparison nodejs/svg.js @ 1378:9ce82873d406
CHange the SVG parser for the scribbo animation.
author | wycc |
---|---|
date | Tue, 22 Mar 2011 01:03:16 +0800 |
parents | 4c5bcaba28ea |
children | 9a585df24e52 |
comparison
equal
deleted
inserted
replaced
1377:8ecee58c85c6 | 1378:9ce82873d406 |
---|---|
102 | 102 |
103 mbname = n.attr("mbname"); | 103 mbname = n.attr("mbname"); |
104 if(mbname) { | 104 if(mbname) { |
105 name = mbname.value(); | 105 name = mbname.value(); |
106 mb_rt.mbnames[name] = obj; | 106 mb_rt.mbnames[name] = obj; |
107 return; | |
108 } | 107 } |
109 mbname = n.attr("label"); | 108 mbname = n.attr("label"); |
110 if(mbname&&(mbname.value()!="")) { | 109 if(mbname&&(mbname.value()!="")) { |
111 name = mbname.value(); | 110 name = mbname.value(); |
112 mb_rt.mbnames[name] = obj; | 111 mb_rt.mbnames[name] = obj; |
113 } | 112 } |
114 try { | 113 try { |
115 var gname = n.attr('id').value(); | 114 var gname = n.attr('id').value(); |
116 sys.puts(gname); | 115 sys.puts("defone object "+ gname); |
117 mb_rt.mbnames[gname] = obj; | 116 mb_rt.mbnames[gname] = obj; |
118 } catch(e) { | 117 } catch(e) { |
119 } | 118 } |
120 } | 119 } |
121 | 120 |
788 { | 787 { |
789 var d = formalize_path_data(n.attr('d').value()); | 788 var d = formalize_path_data(n.attr('d').value()); |
790 var style = n.attr('style'); | 789 var style = n.attr('style'); |
791 var path = this.mb_rt.path_new(d); | 790 var path = this.mb_rt.path_new(d); |
792 var pcoord = this.mb_rt.coord_new(coord); | 791 var pcoord = this.mb_rt.coord_new(coord); |
792 pcoord.node = n; | |
793 n.coord = pcoord; | |
794 this._check_duplicate_src(n,pcoord); | |
793 | 795 |
794 guessPathBoundingBox(pcoord,d); | 796 guessPathBoundingBox(pcoord,d); |
795 pcoord.add_shape(path); | 797 pcoord.add_shape(path); |
796 this._set_paint(n, path); | 798 this._set_paint(n, path); |
797 this._set_bbox(n, path); | 799 this._set_bbox(n, path); |
803 { | 805 { |
804 var x = getInteger(n,'x'); | 806 var x = getInteger(n,'x'); |
805 var y = getInteger(n,'y'); | 807 var y = getInteger(n,'y'); |
806 var tcoord = this.mb_rt.coord_new(coord); | 808 var tcoord = this.mb_rt.coord_new(coord); |
807 var style; | 809 var style; |
810 | |
811 tcoord.node = n; | |
812 n.coord = tcoord; | |
813 this._check_duplicate_src(n,tcoord); | |
808 | 814 |
809 if (n.attr('x')) { | 815 if (n.attr('x')) { |
810 var nx = coord[0]*x+coord[1]*y+coord[2]; | 816 var nx = coord[0]*x+coord[1]*y+coord[2]; |
811 if (coord.center.x > nx) | 817 if (coord.center.x > nx) |
812 coord.center.x = nx; | 818 coord.center.x = nx; |
882 newm[3] = parseFloat(fields[1]); | 888 newm[3] = parseFloat(fields[1]); |
883 newm[4] = parseFloat(fields[3]); | 889 newm[4] = parseFloat(fields[3]); |
884 newm[5] = parseFloat(fields[5]); | 890 newm[5] = parseFloat(fields[5]); |
885 multiply(coord,newm); | 891 multiply(coord,newm); |
886 } | 892 } |
893 if (coord[0]*coord[4] == coord[1]*coord[3]) { | |
894 sys.puts("Singular affine matrix\n"); | |
895 coord.sx = 1; | |
896 coord.sy = 1; | |
897 coord.r = 0; | |
898 coord.tx = 0; | |
899 coord.ty = 0; | |
900 return; | |
901 } | |
902 A = coord[0]; | |
903 B = coord[3]; | |
904 C = coord[1]; | |
905 D = coord[4]; | |
906 E = coord[2]; | |
907 F = coord[5]; | |
908 sx = Math.sqrt(A*A+B*B); | |
909 A = A / sx; | |
910 B = B / sx; | |
911 shear = A*C+B*D; | |
912 C = C - A*shear; | |
913 D = D - B*shear; | |
914 sy = Math.sqrt(C*C+D*D); | |
915 C = C / sy; | |
916 D = D / sy; | |
917 r = A*D - B*C; | |
918 if (r == -1) { | |
919 shear = - shear; | |
920 sy = -sy; | |
921 } | |
922 R = Math.atan2(-B,A); | |
923 coord.sx = sx; | |
924 coord.sy = sy; | |
925 coord.r = R; | |
926 coord.tx = E; | |
927 coord.ty = F; | |
928 sys.puts("transform="+s); | |
929 sys.puts("coord[0]="+coord[0]); | |
930 sys.puts("coord[1]="+coord[1]); | |
931 sys.puts("coord[2]="+coord[2]); | |
932 sys.puts("coord[3]="+coord[3]); | |
933 sys.puts("coord[4]="+coord[4]); | |
934 sys.puts("coord[5]="+coord[5]); | |
935 sys.puts("coord.sx="+coord.sx); | |
936 sys.puts("coord.sy="+coord.sy); | |
937 sys.puts("coord.r="+coord.r); | |
938 sys.puts("coord.tx="+coord.tx); | |
939 sys.puts("coord.ty="+coord.ty); | |
887 } | 940 } |
888 | 941 |
889 loadSVG.prototype.parseRect=function(accu_matrix,coord, id, n) | 942 loadSVG.prototype.parseRect=function(accu_matrix,coord, id, n) |
890 { | 943 { |
891 var x = getInteger(n,'x'); | 944 var x = getInteger(n,'x'); |
894 var w = getInteger(n,'width'); | 947 var w = getInteger(n,'width'); |
895 var h = getInteger(n,'height'); | 948 var h = getInteger(n,'height'); |
896 var trans = n.attr('transform'); | 949 var trans = n.attr('transform'); |
897 var paint; | 950 var paint; |
898 var tcoord = this.mb_rt.coord_new(coord); | 951 var tcoord = this.mb_rt.coord_new(coord); |
952 tcoord.node = n; | |
953 n.coord = tcoord; | |
954 this._check_duplicate_src(n,tcoord); | |
899 | 955 |
900 var style = n.attr('style'); | 956 var style = n.attr('style'); |
901 | 957 |
902 if (trans) | 958 if (trans) |
903 parseTransform(tcoord,trans.value()); | 959 parseTransform(tcoord,trans.value()); |
960 else { | |
961 tcoord.sx = 1; | |
962 tcoord.sy = 1; | |
963 tcoord.r = 0; | |
964 tcoord.tx = 0; | |
965 tcoord.ty = 0; | |
966 } | |
967 | |
968 | |
904 | 969 |
905 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); | 970 var rect = this.mb_rt.rect_new(x,y,w,h,10, 10); |
906 tcoord.add_shape(rect); | 971 tcoord.add_shape(rect); |
907 this._set_paint(n, rect); | 972 this._set_paint(n, rect); |
908 this._set_bbox(n, tcoord); | 973 this._set_bbox(n, tcoord); |
947 n = this._groupMap[id]; | 1012 n = this._groupMap[id]; |
948 var m = [1,0,0,1,0,0] | 1013 var m = [1,0,0,1,0,0] |
949 this.parseGroup(m,root,id, n) | 1014 this.parseGroup(m,root,id, n) |
950 } | 1015 } |
951 | 1016 |
1017 loadSVG.prototype._check_duplicate_src=function(n,coord) { | |
1018 var attr = n.attr('duplicate-src'); | |
1019 if (attr == null) return; | |
1020 sys.puts("---->"+attr.value()); | |
1021 var id = attr.value(); | |
1022 try { | |
1023 this.mb_rt.mbnames[id].target = coord; | |
1024 } catch(e) { | |
1025 sys.puts("id "+id+" is not defined"); | |
1026 } | |
1027 } | |
1028 | |
952 loadSVG.prototype.parseGroup=function(accu_matrix,root, group_id, n) { | 1029 loadSVG.prototype.parseGroup=function(accu_matrix,root, group_id, n) { |
953 var k; | 1030 var k; |
954 var nodes = n.childNodes(); | 1031 var nodes = n.childNodes(); |
955 var coord = this.mb_rt.coord_new(root); | 1032 var coord = this.mb_rt.coord_new(root); |
956 // Parse the transform and style here | 1033 // Parse the transform and style here |
957 var trans = n.attr('transform'); | 1034 var trans = n.attr('transform'); |
958 var accu=[1,0,0,0,1,0]; | 1035 var accu=[1,0,0,0,1,0]; |
959 var style; | 1036 var style; |
1037 n.coord = coord; | |
1038 coord.node = n; | |
1039 this._check_duplicate_src(n,coord); | |
960 | 1040 |
961 coord.center= new Object(); | 1041 coord.center= new Object(); |
962 coord.center.x = 10000; | 1042 coord.center.x = 10000; |
963 coord.center.y = 10000; | 1043 coord.center.y = 10000; |
964 if (trans!=null) { | 1044 if (trans!=null) { |
965 parseTransform(coord, trans.value()); | 1045 parseTransform(coord, trans.value()); |
966 } | 1046 } else { |
1047 coord.sx = 1; | |
1048 coord.sy = 1; | |
1049 coord.r = 0; | |
1050 coord.tx = 0; | |
1051 coord.ty = 0; | |
1052 } | |
967 multiply(accu,accu_matrix); | 1053 multiply(accu,accu_matrix); |
968 multiply(accu,coord); | 1054 multiply(accu,coord); |
969 | 1055 |
970 style = {}; | 1056 style = {}; |
971 parseGroupStyle(style, n); | 1057 parseGroupStyle(style, n); |
972 if(style.opacity) { | 1058 if(style.opacity) { |
973 sys.puts("opacity=" + style.opacity); | 1059 sys.puts("opacity=" + style.opacity); |
974 coord.opacity=style.opacity; | 1060 coord.opacity=style.opacity; |
975 } | 1061 } |
976 | |
977 for(k in nodes) { | 1062 for(k in nodes) { |
978 var c = nodes[k].name(); | 1063 var c = nodes[k].name(); |
979 var attr = nodes[k].attr('id'); | 1064 var attr = nodes[k].attr('id'); |
980 var id; | 1065 var id; |
981 if (attr) { | 1066 if (attr) { |
989 this.parseText(accu,coord, id, nodes[k]); | 1074 this.parseText(accu,coord, id, nodes[k]); |
990 } else if (c == "rect") { | 1075 } else if (c == "rect") { |
991 this.parseRect(accu_matrix,coord, id, nodes[k]); | 1076 this.parseRect(accu_matrix,coord, id, nodes[k]); |
992 } else if (c == "image") { | 1077 } else if (c == "image") { |
993 this.parseImage(accu_matrix,coord, id, nodes[k]); | 1078 this.parseImage(accu_matrix,coord, id, nodes[k]); |
1079 } else if (c == "use") { | |
1080 this.parseUse(accu_matrix,coord, id, nodes[k]); | |
994 } | 1081 } |
995 } | 1082 } |
996 if (root.center.x > coord.center.x) | 1083 if (root.center.x > coord.center.x) |
997 root.center.x = coord.center.x; | 1084 root.center.x = coord.center.x; |
998 if (root.center.y > coord.center.y) | 1085 if (root.center.y > coord.center.y) |
999 root.center.y = coord.center.y; | 1086 root.center.y = coord.center.y; |
1000 | 1087 |
1001 this._set_bbox(n, coord); | 1088 this._set_bbox(n, coord); |
1089 // Set the group map only it is not defined before. The group might be | |
1090 // redefined by the svg:use tag | |
1091 if (this._groupMap[n.name()]==null) | |
1092 this._groupMap[n.name()] = n; | |
1093 | |
1094 make_mbnames(this.mb_rt, n, coord); | |
1095 return coord; | |
1096 }; | |
1097 | |
1098 loadSVG.prototype.parseUse=function(accu_matrix,root, use_id, n) { | |
1099 var k; | |
1100 var nodes; | |
1101 var coord = this.mb_rt.coord_new(root); | |
1102 // Parse the transform and style here | |
1103 var trans = n.attr('transform'); | |
1104 var accu=[1,0,0,0,1,0]; | |
1105 var style; | |
1106 n.coord = coord; | |
1107 coord.node = n; | |
1108 this._check_duplicate_src(n,coord); | |
1109 | |
1110 coord.center= new Object(); | |
1111 coord.center.x = 10000; | |
1112 coord.center.y = 10000; | |
1113 if (trans!=null) { | |
1114 parseTransform(coord, trans.value()); | |
1115 } else { | |
1116 tcoord.sx = 1; | |
1117 tcoord.sy = 1; | |
1118 tcoord.r = 0; | |
1119 tcoord.tx = 0; | |
1120 tcoord.ty = 0; | |
1121 | |
1122 } | |
1123 multiply(accu,accu_matrix); | |
1124 multiply(accu,coord); | |
1125 | |
1126 style = {}; | |
1127 parseGroupStyle(style, n); | |
1128 if(style.opacity) { | |
1129 sys.puts("opacity=" + style.opacity); | |
1130 coord.opacity=style.opacity; | |
1131 } | |
1132 // For a use tag, we will duplicate the group inside it. | |
1133 attr = n.attr('duplicate-src'); | |
1134 if (attr != null) { | |
1135 n = this._groupMap[attr.value()]; | |
1136 if (n == null) { | |
1137 sys.puts("Can not find object "+attr.value()); | |
1138 return; | |
1139 } | |
1140 nodes = n.childNodes(); | |
1141 for(k in nodes) { | |
1142 var c = nodes[k].name(); | |
1143 var attr = nodes[k].attr('id'); | |
1144 var id; | |
1145 if (attr) { | |
1146 id = attr.value(); | |
1147 } | |
1148 if (c == "g") { | |
1149 this.parseGroup(accu,coord, id, nodes[k]); | |
1150 } else if (c == "path") { | |
1151 this.parsePath(accu,coord, id, nodes[k]); | |
1152 } else if (c == "text") { | |
1153 this.parseText(accu,coord, id, nodes[k]); | |
1154 } else if (c == "rect") { | |
1155 this.parseRect(accu_matrix,coord, id, nodes[k]); | |
1156 } else if (c == "image") { | |
1157 this.parseImage(accu_matrix,coord, id, nodes[k]); | |
1158 } else if (c == "use") { | |
1159 this.parseUse(accu_matrix,coord, id, nodes[k]); | |
1160 } | |
1161 attr = nodes[k].attr('duplicate-src'); | |
1162 if (attr == null) continue; | |
1163 id = attr.value(); | |
1164 try { | |
1165 this.mbnames[id].target = coord; | |
1166 } catch(e) { | |
1167 sys.puts("id "+id+" is not defined"); | |
1168 } | |
1169 } | |
1170 } | |
1171 if (root.center.x > coord.center.x) | |
1172 root.center.x = coord.center.x; | |
1173 if (root.center.y > coord.center.y) | |
1174 root.center.y = coord.center.y; | |
1175 | |
1176 this._set_bbox(n, coord); | |
1002 this._groupMap[n.name()] = n; | 1177 this._groupMap[n.name()] = n; |
1003 | 1178 |
1004 make_mbnames(this.mb_rt, n, coord); | 1179 make_mbnames(this.mb_rt, n, coord); |
1005 }; | 1180 }; |
1006 | |
1007 loadSVG.prototype.parseImage=function(accu,coord,id, n) | 1181 loadSVG.prototype.parseImage=function(accu,coord,id, n) |
1008 { | 1182 { |
1009 var ref = n.attr('href').value(); | 1183 var ref = n.attr('href').value(); |
1010 var tcoord = this.mb_rt.coord_new(coord); | 1184 var tcoord = this.mb_rt.coord_new(coord); |
1011 var trans = n.attr('transform'); | 1185 var trans = n.attr('transform'); |
1186 n.coord = tcoord; | |
1187 tcoord.node = n; | |
1188 this._check_duplicate_src(n,tcoord); | |
1012 | 1189 |
1013 if (ref == null) return; | 1190 if (ref == null) return; |
1014 if (ref.substr(0,7) == "file://") { | 1191 if (ref.substr(0,7) == "file://") { |
1015 ref = ref.substring(7); | 1192 ref = ref.substring(7); |
1016 } else if (ref.substr(0,5)=="file:") { | 1193 } else if (ref.substr(0,5)=="file:") { |