📄 svgtoolkit.java
字号:
double a=0, d=0; if(tvalues.indexOf(',')==-1){ try{ a=new Double(tvalues).doubleValue(); d=a; }catch(Exception ex){a=0; d=0; } }else{ try{ a=new Double(tvalues.substring(0,tvalues.indexOf(','))).doubleValue(); d=new Double(tvalues.substring(tvalues.indexOf(',')+1,tvalues.length())).doubleValue(); }catch(Exception ex){a=0; d=0;} } af.concatenate(AffineTransform.getScaleInstance(a , d)); }else if(transf.equals("rotate")){ double angle=0, e=0, f=0; if(tvalues.indexOf(',')==-1){ try{ angle=new Double(tvalues).doubleValue(); }catch(Exception ex){angle=0;} af.concatenate(AffineTransform.getRotateInstance(Math.toRadians(angle))); }else{ try{ angle=new Double(tvalues.substring(0,tvalues.indexOf(','))).doubleValue(); tvalues=tvalues.substring(tvalues.indexOf(',')+1,tvalues.length()); e=new Double(tvalues.substring(0,tvalues.indexOf(','))).doubleValue(); tvalues=tvalues.substring(tvalues.indexOf(',')+1,tvalues.length()); f=new Double(tvalues).doubleValue(); }catch(Exception ex){angle=0; e=0; f=0;} af.concatenate(AffineTransform.getTranslateInstance(e, f)); af.concatenate(AffineTransform.getRotateInstance(Math.toRadians(angle))); af.concatenate(AffineTransform.getTranslateInstance(-e, -f)); } }else if(transf.equals("skewX")){ double angle=0; try{ angle=new Double(tvalues).doubleValue(); }catch(Exception ex){angle=0;} af.concatenate(AffineTransform.getShearInstance(Math.tan(Math.toRadians(angle)), 0)); }else if(transf.equals("skewY")){ double angle=0; try{ angle=new Double(tvalues).doubleValue(); }catch(Exception ex){angle=0;} af.concatenate(AffineTransform.getShearInstance(0, Math.tan(Math.toRadians(angle)))); }else if(transf.equals("matrix")){ double[] m=new double[6]; int j=0, i=tvalues.indexOf(','); tvalues=tvalues.concat(","); while(i !=-1){ try{ m[j]=new Double(tvalues.substring(0,i)).doubleValue(); }catch (Exception ex){} tvalues=tvalues.substring(tvalues.indexOf(',')+1, tvalues.length()); i=tvalues.indexOf(','); j++; } af.concatenate(new AffineTransform(m[0], m[1], m[2], m[3], m[4], m[5])); }else{ break; } } matrix.concatenateTransform(af); } return matrix; } /** * replaces all the tranforms by their equivalent matrix transform * @param node the node on which the changes will be made */ public void transformToMatrix(Node node){ if(node!=null){ NamedNodeMap attributes=node.getAttributes(); if(attributes!=null){ //if the node has the transform atrribute Node att=attributes.getNamedItem("transform"); if(att!=null){ //gets the value of the transform attribute String value=new String(att.getNodeValue()); if(value!=null && ! value.equals("")){ //converts the transforms contained in the string to a single matrix transform SVGTransformMatrix matrix=transformToMatrix(value); //if the matrix is not the identity matrix, it is set as the new transform matrix if(matrix!=null && ! matrix.isIdentity()){ //if the node has the transform attribute ((Element)node).setAttributeNS(null,"transform", matrix.getMatrixRepresentation()); } } } } } } /** * gets a transformation matrix given a string containing a matrix transform * @param value the string containing a matrix transform * @return the corresponding transform matrix */ public SVGTransformMatrix getTransformMatrix(String value){ SVGTransformMatrix matrix=new SVGTransformMatrix(1,0,0,1,0,0); if(value!=null && ! value.equals("")){ int rang=value.indexOf("matrix"); //computes the double values of the matrix in the transform attribute if(rang>-1){ String subValue=""; subValue=value.substring(rang,value.length()); subValue=subValue.substring(0,subValue.indexOf(")")+1); value=value.replaceAll("["+subValue+"]",""); subValue=subValue.substring(subValue.indexOf("("),subValue.length()); //cleans the string value=cleanTransformString(value); subValue=subValue.replaceAll("[(]",""); subValue=subValue.replaceAll("[)]",""); subValue=subValue.concat(","); int i=subValue.indexOf(','), j=0; double[] matrixDb=new double[6]; while(i !=-1){ try{ matrixDb[j]=new Double(subValue.substring(0,i)).doubleValue(); }catch (Exception ex){return new SVGTransformMatrix(1,0,0,1,0,0);} subValue=subValue.substring(subValue.indexOf(',')+1, subValue.length()); i=subValue.indexOf(','); j++; } matrix=new SVGTransformMatrix(matrixDb[0], matrixDb[1], matrixDb[2], matrixDb[3], matrixDb[4], matrixDb[5]); } } return matrix; } /** * gets a node's transformation matrix * @param node the node from which to get the transformation matrix * @return the transformation matrix */ public SVGTransformMatrix getTransformMatrix(Node node){ if(node!=null){ NamedNodeMap attributes=node.getAttributes(); if(attributes!=null){ //if the node has the transform atrribute Node att=attributes.getNamedItem("transform"); if(att!=null){ //gets the value of the transform attribute String value=att.getNodeValue(); //creating the matrix transform return getTransformMatrix(value); } } } //otherwise returns the identity matrix return new SVGTransformMatrix(1,0,0,1,0,0); } /** * sets the transform matrix of a node * @param node the given node * @param matrix the transformation matrix */ public void setTransformMatrix(Node node, SVGTransformMatrix matrix){ if(node!=null && node instanceof Element && matrix!=null){ boolean isParticularGNode=false; if( node.getNodeName().equals("g") && (getElementChildCount((Element)node)>1 || (getElementChildCount((Element)node)==1 && ! getElementFirstChild((Element)node).getNodeName().equals("svg")))){ isParticularGNode=true; } if(isParticularGNode){ SVGTransformMatrix cMatrix=null; //for each child of the g element, gets its matrix, modifies and sets it for(Node cur=node.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ cMatrix=getTransformMatrix(cur); cMatrix.concatenateMatrix(matrix); setTransformMatrix(cur, cMatrix); } } ((Element)node).removeAttribute("transform"); }else{ //if the node has the transform attribute ((Element)node).setAttributeNS(null,"transform", matrix.getMatrixRepresentation()); } } } /** * returns the cloned node of the given node whose use nodes have been removed * @param node a node * @return the cloned node of the given node whose use nodes have been removed */ public Node getClonedNodeWithoutUseNodes(Node node){ Node clonedNode=null; if(node!=null){ clonedNode=node.cloneNode(true); if(! clonedNode.getNodeName().equals("use")){ //removes the use nodes from the subtree of the cloned node removeUseNodes(clonedNode); }else{ clonedNode=null; } } return clonedNode; } /** * removes the use nodes in the given nodes * @param node a node */ protected void removeUseNodes(Node node){ if(node!=null && node.hasChildNodes()){ NodeList children=node.getChildNodes(); Node cur=null; for(int i=0; i<children.getLength(); i++){ cur=children.item(i); if(cur!=null && cur.getNodeName().equals("use")){ //if the node is a use node, it is removed node.removeChild(cur); }else if(cur!=null){ //if the node is not a use node, its subtre is checked removeUseNodes(cur); } } } } /** * computes a rectangle given 2 the coordinates of two points * @param point1 the first point * @param point2 the second point * @return the correct rectangle */ public Rectangle2D.Double getComputedRectangle(Point2D.Double point1, Point2D.Double point2){ if(point1!=null && point2!=null){ double width=point2.x-point1.x, height=point2.y-point1.y, x=point1.x, y=point1.y; if(point1.x>point2.x && point1.y>point2.y){ x=point2.x; y=point2.y; width=point1.x-point2.x; height=point1.y-point2.y; }else if(point1.x>point2.x && point1.y<point2.y){ width=point1.x-point2.x; height=point2.y-point1.y; x=point2.x; y=point1.y; }else if(point1.x<point2.x && point1.y>point2.y){ width=point2.x-point1.x; height=point1.y-point2.y; x=point1.x; y=point2.y; } return new Rectangle2D.Double(x, y, width, height); } return new Rectangle2D.Double(0, 0, 0, 0); } /** * computes a square given 2 the coordinates of two points * @param point1 the first point * @param point2 the second point * @return the correct square */ public Rectangle2D.Double getComputedSquare(Point2D.Double point1, Point2D.Double point2){ if(point1!=null && point2!=null){ double width=point2.x-point1.x, height=point2.y-point1.y, x=point1.x, y=point1.y;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -