📄 svgtoolkit.java
字号:
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; if(width<height){ y=point2.y+(height-width); height=width; }else{ x=point2.x+(width-height); width=height; } }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; if(width<height){ height=width; }else{ x=point2.x+(width-height); width=height; } }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; if(width<height){ y=point2.y+(height-width); height=width; }else{ width=height; } }else if(point1.x<=point2.x && point1.y<=point2.y){ if(width<height){ height=width; }else{ width=height; } } return new Rectangle2D.Double(x, y, width, height); } return new Rectangle2D.Double(0, 0, 0, 0); } /** * removes the "url()" prefix in the given string * @param value the string to be modified * @return the string without the "url()" prefix */ public static String toUnURLValue(String value){ if(value==null){ value=""; } String val=new String(value); int ind0=val.indexOf("url(#"), ind1=val.indexOf(")"); if(ind0>=0 && ind1>=0){ val=val.substring(ind0+5, ind1); } return val; } /** * adds the "url()" prefix in the given string * @param value the string to be modified * @return the string withthe "url()" prefix */ public static String toURLValue(String value){ if(value==null || (value!=null && value.equals(""))){ value=""; }else{ value=new String("url(#"+value+")"); } return value; } /** * creates the list of the resources used by the given node and returns it * @param element an element * @param deep true if the children of the given node should be inspected * @return the list of the resources used by the given node */ public LinkedList<Element> getResourcesUsedByNode(Element element, boolean deep){ LinkedList<Element> resources=new LinkedList<Element>(); if(element!=null){ //getting the defs element Element root=element.getOwnerDocument().getDocumentElement(); Node cur=null; Element defs=null; for(cur=root.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element && cur.getNodeName().equals("defs")){ defs=(Element)cur; } } //the string containing the ids of the resources needed String style=element.getAttribute("style"); if(deep){ for(NodeIterator it=new NodeIterator(element); it.hasNext();){ cur=it.next(); if(cur instanceof Element){ style=style.concat(((Element)cur).getAttribute("style")); } } } if(defs!=null && style!=null && ! style.equals("")){ String id=""; Element el=null; //for each child of the "defs" element, adds it to the list if it is used by the given element for(cur=defs.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ el=(Element)cur; id=el.getAttribute("id"); //if the id of the resource is contained in the style attribute if(id!=null && style.indexOf("#".concat(id))!=-1){ resources.add(el); } } } } } return resources; } /** * returns whether the given element is a shape node or not * @param element * @return whether the given element is a shape node or not */ public static boolean isElementAShape(Element element){ if(element!=null){ String name=element.getNodeName(); return (name.equals("g") || name.equals("circle") || name.equals("ellipse") || name.equals("image") || name.equals("line") || name.equals("path") || name.equals("polygon") || name.equals("polyline") || name.equals("rect") || name.equals("text")); } return false; } /** * returns the label corresponding to the given element * @param element an element * @return the label corresponding to the given element */ public static String getElementLabel(Element element) { if(element!=null && svgElementLabels.containsKey(element.getTagName())) { return svgElementLabels.get(element.getTagName()); } return unknownShapeLabel; } /** * force a refresh of the current selection */ public void forceReselection(){ if(editor.getSVGSelection()!=null){ //sets that the selection has changed editor.getSVGSelection().selectionChanged(true); } } /** * converts a string to a double that is a percentage * @param str a string * @param isPercentage the boolean telling if the string describes a percentage value * @return the corresponding value of the given string */ public double getDoubleValue(String str, boolean isPercentage){ if(str==null){ str=""; } str=str.replaceAll("\\s+", ""); double val=0; boolean hasPercentSign=(str.indexOf("%")!=-1); try{ if(isPercentage){ if(hasPercentSign){ str=str.replaceAll("%", ""); val=Double.parseDouble(str); }else{ val=Double.parseDouble(str); val=val*100; } }else{ val=Double.parseDouble(str); } }catch (Exception ex){} return val; } /** * removes all the attributes and children in the duplicated node * and adds the attributes and children of the reference node to the duplicated node * @param duplicatedNode the duplicated node * @param referenceNode the reference node */ public void duplicateNode(Node duplicatedNode, Node referenceNode){ if( duplicatedNode!=null && referenceNode!=null && duplicatedNode instanceof Element && referenceNode instanceof Element){ //removes all the attributes and children of the duplicated node //and replaces them by those of the reference node //removes all the children of the duplicated node while(duplicatedNode.hasChildNodes()){ duplicatedNode.removeChild(duplicatedNode.getFirstChild()); } //appends all the cloned children of the reference node Node cur=null, clonedNode=null; for(cur=referenceNode.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ clonedNode=cur.cloneNode(true); duplicatedNode.appendChild(clonedNode); } } //removes all the attributes of the duplicated node NamedNodeMap attr=duplicatedNode.getAttributes(); Node at=null; int i; for(i=0; i<attr.getLength(); i++){ at=attr.item(i); if(at!=null){ ((Element)duplicatedNode).removeAttribute(at.getNodeName()); } } //adds the attributes of the reference node attr=referenceNode.getAttributes(); for(i=0; i<attr.getLength(); i++){ at=attr.item(i); if(at!=null){ ((Element)duplicatedNode).setAttribute(at.getNodeName(), at.getNodeValue()); } } } } /** * creates a general path given the node * @param elt * @return a general path */ public ExtendedGeneralPath getGeneralPath(Element elt){ ExtendedGeneralPath path=null; if(elt!=null){ Map segs=getPathSeg(elt); Point2D.Double point1=null, point2=null, point3=null; double d1=0, d2=0, d3=0; int i1=0, i2=0; String command=""; char cmd=' '; java.util.List list=null; path=new ExtendedGeneralPath(); if(segs==null){ SVGTransformMatrix matrix=getTransformMatrix(elt); if(matrix!=null){ //transforms the path AffineTransform af = matrix.getTransform(); path=new ExtendedGeneralPath(path.createTransformedShape(af)); } return path; } for(Iterator it=segs.keySet().iterator(); it.hasNext();){ try{ command=(String)it.next(); list=(java.util.List)segs.get(command); cmd=command.charAt(0); }catch (Exception ex){cmd=' '; list=null;} try{ if(cmd!=' ' && list!=null){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -