📄 svgtoolkit.java
字号:
LinkedList<String> attToBeRemoved=new LinkedList<String>(); Node att=null; for(int i=0;i<attributes.getLength();i++){ att=attributes.item(i); if(att.getNodeName()!=null && styleProperties.contains(att.getNodeName())){ name=att.getNodeName(); value=att.getNodeValue(); if(value!=null && ! value.equals("") && style.indexOf(name+":")==-1){ //if the attribute is not in the style value, it is added to the style value if(style.length()>0 && ! style.endsWith(";")){ style=style.concat(";"); } style=style+name+":"+value+";"; } attToBeRemoved.add(new String(name)); } } //removes the attributes that have been added to the style attribute String str=""; for(Iterator it=attToBeRemoved.iterator(); it.hasNext();){ try{str=(String)it.next();}catch (Exception ex){str=null;} if(str!=null && !str.equals("")){ element.removeAttribute(str); } } if(style.equals("")){ //removes the style attribute element.removeAttribute("style"); }else{ //modifies the style attribute element.setAttribute("style", style); } } } /** * removes the tspans inside a text node * @param node the node on which the changes will be made */ protected void removeTspans(Node node){ if(node!=null && node.getNodeName().equals("text")){ String value=getText(node); if(value==null){ value=""; } //removes all the text children from the node NodeList children=node.getChildNodes(); for(int i=0; i<children.getLength(); i++){ if( children.item(i)!=null && (children.item(i) instanceof Text || children.item(i).getNodeName().equals("tspan"))){ node.removeChild(children.item(i)); } } children=node.getChildNodes(); for(int i=0; i<children.getLength(); i++){ if( children.item(i)!=null && (children.item(i) instanceof Text || children.item(i).getNodeName().equals("tspan"))){ node.removeChild(children.item(i)); } } //adds a #text node Document doc=node.getOwnerDocument(); if(doc!=null){ Text txt=doc.createTextNode(value); node.appendChild(txt); } } } /** * normalizes a group element, setting the transform of this node to identity * and modifying the matrix transform of its children * @param g a group element */ public void normalizeGroupNode(Element g){ if(g!=null && g.getNodeName().equals("g")){ boolean canNormalize=true; if(getElementChildCount(g)==1 && getElementFirstChild(g).getNodeName().equals("svg")){ canNormalize=false; } SVGTransformMatrix gMatrix=getTransformMatrix(g), matrix=null; if(canNormalize && ! gMatrix.isIdentity()){ for(Node cur=g.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ //getting, modifying and setting the matrix of this node matrix=getTransformMatrix(cur); matrix.concatenateMatrix(gMatrix); setTransformMatrix(cur, matrix); } } //setting the matrix of the g element to identity g.removeAttribute("transform"); } } } /** * gets the text written in a text node * @param node the text node * @return a string of the text value */ protected String getText(Node node){ String value=""; if(node!=null && (node.getNodeName().equals("text") || node.getNodeName().equals("tspan"))){ //for each child of the given node, computes the text it contains and concatenates it to the current value for(Node cur=node.getFirstChild();cur!=null;cur=cur.getNextSibling()){ if(cur.getNodeName().equals("#text")){ value=value+cur.getNodeValue(); }else if(cur.getNodeName().equals("tspan")){ value=value+getText(cur); } } } value=normalizeTextNodeValue(value); return value; } /** * modifies the string to removes the extra whitespaces * @param value the string to be modified * @return the modified string */ public String cleanTransformString(String value){ String val=new String(value); val=val.replaceAll("0\\s","0,"); val=val.replaceAll("1\\s","1,"); val=val.replaceAll("2\\s","2,"); val=val.replaceAll("3\\s","3,"); val=val.replaceAll("4\\s","4,"); val=val.replaceAll("5\\s","5,"); val=val.replaceAll("6\\s","6,"); val=val.replaceAll("7\\s","7,"); val=val.replaceAll("8\\s","8,"); val=val.replaceAll("9\\s","9,"); val=val.replaceAll("\\s*[,]\\s*[,]\\s*",","); val=val.replaceAll("\\s+",""); return val; } /** * normalizes the value of a text node * @param value * @return a normalized string */ public String normalizeTextNodeValue(String value){ String textValue=""; if(value!=null && !value.equals("")){ textValue=new String(value); textValue=textValue.replaceAll("\\t+", " "); textValue=textValue.replaceAll("\\s+", " "); textValue.trim(); if(! textValue.equals("") && textValue.charAt(0)==' '){ textValue=textValue.substring(1, textValue.length()); } if(! textValue.equals("") && textValue.charAt(textValue.length()-1)==' '){ textValue=textValue.substring(0, textValue.length()-1); } } return textValue; } /** * returns the previous element sibling of the given element * @param element an element * @return the previous element sibling of the given element */ public Element getPreviousElementSibling(Element element){ Element previousSibling=null; if(element!=null){ Node cur=null; for(cur=element.getPreviousSibling(); cur!=null; cur=cur.getPreviousSibling()){ if(cur instanceof Element){ previousSibling=(Element)cur; break; } } } return previousSibling; } /** * returns the next element sibling of the given element * @param element an element * @return the next element sibling of the given element */ public Element getNextElementSibling(Element element){ Element nextSibling=null; if(element!=null){ Node cur=null; for(cur=element.getNextSibling(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ nextSibling=(Element)cur; break; } } } return nextSibling; } /** * returns the number of elements that are children of the given node * @param element an element * @return the number of elements that are children of the given node */ public int getElementChildCount(Element element){ int count=0; if(element!=null){ Node cur=null; for(cur=element.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ count++; } } } return count; } /** * returns the first child element that is an element * @param element an element * @return the first child element that is an element */ public Element getElementFirstChild(Element element){ Element firstElement=null; if(element!=null){ Node cur=null; for(cur=element.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ firstElement=(Element)cur; } } } return firstElement; } /** * converts the given transform string into a transform matrix * @param value the input string * @return a transform matrix */ public SVGTransformMatrix transformToMatrix(String value){ //creates the matrix that will replace the transforms SVGTransformMatrix matrix=new SVGTransformMatrix(1,0,0,1,0,0); if(value!=null && ! value.equals("")){ String transf="", tvalues=""; //cleans the string value=cleanTransformString(value); AffineTransform af=new AffineTransform(); //for each transform found in the value of the transform attribute while(value.length()!=0 && value.indexOf('(')!=-1){ //the name of the transform transf=value.substring(0, value.indexOf('(')); tvalues=value.substring(0, value.indexOf(')')); //removes the current transform from the value of the transform attribute value=value.substring(tvalues.length()+1, value.length()); //the numeric value of the transform tvalues=tvalues.substring(tvalues.indexOf('(')+1, tvalues.length()); //for each kind of transform, gets the numeric values and concatenates the transform to the matrix if(transf.equals("translate")){ double e=0, f=0; try{ e=new Double(tvalues.substring(0, tvalues.indexOf(','))).doubleValue(); f=new Double(tvalues.substring(tvalues.indexOf(',')+1, tvalues.length())).doubleValue(); }catch(Exception ex){e=0; f=0;} af.concatenate(AffineTransform.getTranslateInstance(e, f)); }else if(transf.equals("scale")){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -