📄 svgtoolkit.java
字号:
if(cmd=='Z'){ path.closePath(); }else if(cmd=='C'){ point1=(Point2D.Double)list.get(0); point2=(Point2D.Double)list.get(1); point3=(Point2D.Double)list.get(2); path.curveTo((float)point1.x, (float)point1.y, (float)point2.x, (float)point2.y, (float)point3.x, (float)point3.y); }else if(cmd=='L'){ point1=(Point2D.Double)list.get(0); path.lineTo((float)point1.x, (float)point1.y); }else if(cmd=='M'){ point1=(Point2D.Double)list.get(0); path.moveTo((float)point1.x, (float)point1.y); }else if(cmd=='Q'){ point1=(Point2D.Double)list.get(0); point2=(Point2D.Double)list.get(1); path.quadTo((float)point1.x, (float)point1.y, (float)point2.x, (float)point2.y); }else if(cmd=='A'){ d1=((Double)list.get(0)).doubleValue(); d2=((Double)list.get(1)).doubleValue(); d3=((Double)list.get(2)).doubleValue(); i1=((Integer)list.get(3)).intValue(); i2=((Integer)list.get(4)).intValue(); point1=(Point2D.Double)list.get(5); path.arcTo((float)d1, (float)d2, (float)d3, i1==0?false:true, i2==0?false:true, (float)point1.x, (float)point1.y); } } }catch (Exception ex){} } SVGTransformMatrix matrix=getTransformMatrix(elt); if(matrix!=null){ //transforms the path AffineTransform af=matrix.getTransform(); path=new ExtendedGeneralPath(path.createTransformedShape(af)); } } return path; } /** * gets the different segments of a path * @param node the node * @return the map containing the different segments of a path */ public LinkedHashMap<String, LinkedList<Object>> getPathSeg(Element node){ NamedNodeMap attributes=node.getAttributes(); if(attributes!=null){ //if the node has the "d" attribute Node att=attributes.getNamedItem("d"); if(att!=null){ //gets the value of the "d" attribute String value=att.getNodeValue(); value.replaceAll("[,]",""); //removes the first space characters from the string if(!value.equals("") && value.charAt(0)==' ')value=value.replaceFirst("[\\s]+",""); //if the first character is not a 'M', the string value is not correct if(value.charAt(0)=='M' || value.charAt(0)=='m'){ LinkedHashMap<String, LinkedList<Double>> map=new LinkedHashMap<String, LinkedList<Double>>(); String command=""; Double d=null; LinkedList<Double> list=null; int rg=0; //for each command in the value string while(! value.equals("") && Character.isLetter(value.charAt(0))){ //gets the command command=value.substring(0,1); value=value.substring(1,value.length()); if(!value.equals("") && value.charAt(0)==' '){ value=value.replaceFirst("[\\s]+",""); } //the list that will contains the values of the command (Double objects) list=new LinkedList<Double>(); while( ! value.equals("") && ! Character.isLetter(value.charAt(0))){ //adds the Double values found in the value string in the list try{ if(value.indexOf(' ')!=-1){ d=new Double(value.substring(0,value.indexOf(' '))); }else{ d=new Double(value); value=""; } }catch (Exception ex){return null;} value=value.substring(value.indexOf(' ')+1,value.length()); if(!value.equals("") && value.charAt(0)==' '){ value=value.replaceFirst("[\\s]+",""); } list.add(d); } //puts the command and its value to the map map.put(command+new Integer(rg++).toString(), list); } //converts the map made of Double values into a map that contains points and other values return convertPathValues(map); } } } return null; } /** * converts a map made of Double values into a map that contains points and other values * @param map the map containing only Double values * @return the map that contains points and other values */ public LinkedHashMap<String, LinkedList<Object>> convertPathValues(LinkedHashMap<String, LinkedList<Double>> map){ if(map.size()>0){ //the map that will be returned LinkedHashMap<String, LinkedList<Object>> map2=new LinkedHashMap<String, LinkedList<Object>>(); Iterator it2=null; LinkedList<Double> list=null; LinkedList<Object> nlist=null; Point2D.Double lastPoint=null, point=null, beforeLastPoint=null; Double[] dtab=new Double[6]; int rg=0, i, j; char cmd=' ', lastCmd=' '; //for each command in the map for(String command : map.keySet()){ //gets the list of the Double values if(command!=null && ! command.equals("")){ list=map.get(command); } if(list!=null){ cmd=command.charAt(0); //according to the command if(cmd=='M' || cmd=='m' || cmd=='L' || cmd=='l'){ for(i=0; i<list.size()-1; i+=2){ //the list of the points of the command nlist=new LinkedList<Object>(); //getting the values dtab[0]=list.get(i); dtab[1]=list.get(i+1); //adds the point created with the two double values to the list if(Character.isLowerCase(cmd)){ if(lastPoint==null) { lastPoint=new Point2D.Double(0, 0); } point=new Point2D.Double(lastPoint.x+dtab[0].doubleValue(), lastPoint.y+dtab[1].doubleValue()); }else{ point=new Point2D.Double(dtab[0].doubleValue(),dtab[1].doubleValue()); } //sets the value of the lastPoint beforeLastPoint=lastPoint; lastPoint=point; nlist.add(point); //adds the command to the list map2.put(Character.toUpperCase(cmd)+new Integer(rg++).toString(), nlist); } }else if(cmd=='T' || cmd=='t'){ for(i=0; i<list.size()-1; i+=2){ //the list of the points of the command nlist=new LinkedList<Object>(); //computing the control point if(lastPoint==null){ lastPoint=new Point2D.Double(0, 0); } //adding the first control point if(lastCmd=='Q' && beforeLastPoint!=null){ point=new Point2D.Double(2*lastPoint.x-beforeLastPoint.x, 2*lastPoint.y-beforeLastPoint.y); nlist.add(point); beforeLastPoint=lastPoint; lastPoint=point; }else{ nlist.add(lastPoint); beforeLastPoint=lastPoint; } //computing the new current point dtab[0]=list.get(i); dtab[1]=list.get(i+1); //adds the point created with the two double values to the list if(Character.isLowerCase(cmd)){ if(lastPoint==null){ lastPoint=new Point2D.Double(0, 0); } point=new Point2D.Double(lastPoint.x+dtab[0].doubleValue(), lastPoint.y+dtab[1].doubleValue()); }else{ point=new Point2D.Double(dtab[0].doubleValue(),dtab[1].doubleValue()); } //sets the value of the lastPoint beforeLastPoint=lastPoint; lastPoint=point; nlist.add(point); //adds the command to the list cmd='Q'; map2.put(cmd+new Integer(rg++).toString(),nlist); } }else if(cmd=='Q' || cmd=='q'){ for(i=0; i<list.size()-3; i+=4){ //the list of the points of the command nlist=new LinkedList<Object>(); dtab[0]=list.get(i); dtab[1]=list.get(i+1); dtab[2]=list.get(i+2); dtab[3]=list.get(i+3); for(j=0; j<4; j+=2){ //adds the point created with the two double values to the list if(Character.isLowerCase(cmd)){ if(lastPoint==null){ lastPoint=new Point2D.Double(0, 0); } point=new Point2D.Double(lastPoint.x+dtab[j].doubleValue(), lastPoint.y+dtab[j+1].doubleValue()); }else{ point=new Point2D.Double(dtab[j].doubleValue(), dtab[j+1].doubleValue()); } //sets the value of the last point beforeLastPoint=lastPoint; lastPoint=point; nlist.add(point); } //adds the command to the list cmd=Character.toUpperCase(cmd); map2.put(cmd+new Integer(rg++).toString(), nlist); } }else if(cmd=='C' || cmd=='c'){ for(i=0; i<list.size()-5; i+=6){ //the list of the points of the command nlist=new LinkedList<Object>(); dtab[0]=list.get(i); dtab[1]=list.get(i+1); dtab[2]=list.get(i+2); dtab[3]=list.get(i+3); dtab[4]=list.get(i+4); dtab[5]=list.get(i+5); for(j=0; j<6; j+=2){ //adds the point created with the two double values to the list if(Character.isLowerCase(cmd)){ if(lastPoint==null){ lastPoint=new Point2D.Double(0, 0); } point=new Point2D.Double(lastPoint.x+dtab[j].doubleValue(), lastPoint.y+dtab[j+1].doubleValue()); }else{ point=new Point2D.Double(dtab[j].doubleValue(),dtab[j+1].doubleValue()); } //sets the value of the lastPoint beforeLastPoint=lastPoint; lastPoint=point; nlist.add(point); } //adds the command to the list cmd=Character.toUpperCase(cmd); map2.put(cmd+new Integer(rg++).toString(), nlist); } }else if(cmd=='S' || cmd=='s'){ for(i=0; i<list.size()-3; i+=4){ //the list of the points of the command nlist=new LinkedList<Object>(); //computing the control point if(lastPoint==null){ lastPoint=new Point2D.Double(0, 0); } //addin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -