📄 svgtoolkit.java
字号:
/* * Created on 21 juin 2004 * ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 ============================================= GLIPS Graffiti Editor, a SVG Editor Copyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRIS This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact : jordi.suc@itris.fr; philippe.gil@itris.fr ============================================= */package fr.itris.glips.svgeditor;import org.w3c.dom.*;import org.w3c.dom.svg.*;import org.apache.batik.ext.awt.geom.*;import java.awt.geom.*;import java.awt.image.*;import java.text.*;import java.util.*;import java.awt.*;import javax.swing.*;import javax.swing.plaf.metal.*;import fr.itris.glips.svgeditor.canvas.*;/** * a class providing utility methods * @author Jordi SUC, Maciej Wojtkiewicz */public class SVGToolkit { /** * the xml file extension */ public static final String XML_FILE_EXTENSION=".xml"; /** * the svg file extension */ public static final String SVG_FILE_EXTENSION=".svg"; /** * the svgz file extension */ public static final String SVGZ_FILE_EXTENSION=".svgz"; /** * the editor */ private SVGEditor editor; /** * the name space for declaring namespaces */ public static final String xmlnsNS="http://www.w3.org/2000/xmlns/"; /** * the xlink attribute namespace name */ public static final String xmlnsXLinkAttributeName="xmlns:xlink"; /** * the xlink prefix */ public static final String xLinkprefix="xlink:"; /** * the xlink namespace */ public static final String xmlnsXLinkNS="http://www.w3.org/1999/xlink"; /** * the namespace of the rtda animations */ public static final String rtdaNameSpace="http://www.itris.fr/2003/animation"; /** * the prefix for the rtda animations */ public static final String rtdaPrefix="rtda:"; /** * the jwidget tag name */ public static final String jwidgetTagName="rtda:jwidget"; /** * the decimal formatter */ public static DecimalFormat format=null; /** * the map associating the name of a svg element shape to its label */ protected static HashMap<String, String> svgElementLabels=new HashMap<String, String>(); /** * the label for an unknow shape */ public static String unknownShapeLabel=""; static { //the object used to convert double values into strings DecimalFormatSymbols symbols=new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); format=new DecimalFormat("######.#", symbols); svgElementLabels.put("g", SVGEditor.getBundle().getString("svgElementName_g")); svgElementLabels.put("circle", SVGEditor.getBundle().getString("svgElementName_circle")); svgElementLabels.put("ellipse", SVGEditor.getBundle().getString("svgElementName_ellipse")); svgElementLabels.put("image", SVGEditor.getBundle().getString("svgElementName_image")); svgElementLabels.put("line", SVGEditor.getBundle().getString("svgElementName_line")); svgElementLabels.put("path", SVGEditor.getBundle().getString("svgElementName_path")); svgElementLabels.put("polygon", SVGEditor.getBundle().getString("svgElementName_polygon")); svgElementLabels.put("polyline", SVGEditor.getBundle().getString("svgElementName_polyline")); svgElementLabels.put("rect", SVGEditor.getBundle().getString("svgElementName_rect")); svgElementLabels.put("text", SVGEditor.getBundle().getString("svgElementName_text")); unknownShapeLabel=SVGEditor.getBundle().getString("svgElementName_unknown"); } /** * the constructror of the class * @param editor the editor */ public SVGToolkit(SVGEditor editor){ this.editor=editor; } /** * checks if the xlink namespace is defined in the given document * @param doc a document */ public static void checkXLinkNameSpace(Document doc){ if(doc!=null && ! doc.getDocumentElement().hasAttributeNS(xmlnsNS, xmlnsXLinkAttributeName)){ doc.getDocumentElement().setAttributeNS(xmlnsNS, xmlnsXLinkAttributeName, xmlnsXLinkNS); } } /** * checks if the given document contains the rtda name space, if not, the namespace is added * @param doc a svg document */ public static void checkRtdaXmlns(Document doc){ if(SVGEditor.isRtdaAnimationsVersion && doc!=null){ Element svgRoot=doc.getDocumentElement(); if(! svgRoot.hasAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:rtda")){ svgRoot.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:rtda", rtdaNameSpace); } } } /** * checks if the given document contains the given name space, if not, the namespace is added * @param doc a svg document * @param prefix the name space prefix * @param nameSpace a name space */ public static void checkXmlns(Document doc, String prefix, String nameSpace){ if(doc!=null && prefix!=null && nameSpace!=null){ Element svgRoot=doc.getDocumentElement(); if(! svgRoot.hasAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:"+prefix)){ svgRoot.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:"+prefix, nameSpace); } } } /** * computes and returns a split path corresponding to the given path * @param path * @return a split path corresponding to the given path */ public String[] getSplitPath(String path){ String[] splitPath=null; if(path!=null && ! path.equals("")){ String[] splitPath2=path.split("/"); LinkedList<String> list=new LinkedList<String>(); int i; for(i=0; i<splitPath2.length; i++){ if(splitPath2[i]!=null && ! splitPath.equals("")){ list.add(splitPath2[i]); } } splitPath=new String[list.size()]; i=0; for(String str : list){ splitPath[i]=str; i++; } } return splitPath; } /** * @return creates and returns a combo box enabling to choose the units */ public JComboBox getUnitsComboBoxChooser(){ //creating the items String[] items=new String[]{"px", "pt", "pc", "mm", "cm", "in"}; JComboBox combo=new JComboBox(items); combo.setSelectedIndex(0); return combo; } /** * computes the number corresponding to this string in pixel * @param str * @return the number corresponding to this string in pixel */ public static double getPixelledNumber(String str){ double i=0; if(str!=null && ! str.equals("")){ str=str.trim(); if(! Character.isDigit(str.charAt(str.length()-1))){ String unit=str.substring(str.length()-2, str.length()); String nb=str.substring(0, str.length()-2); try{ i=Double.parseDouble(nb); }catch (Exception ex){} if(unit.equals("pt")){ i=i*1.25; }else if(unit.equals("pc")){ i=i*15; }else if(unit.equals("mm")){ i=i*3.543307; }else if(unit.equals("cm")){ i=i*35.43307; }else if(unit.equals("in")){ i=i*90; } }else{ try{ i=Double.parseDouble(str); }catch (Exception ex){} } } return i; } /** * converts the given pixelled value into the given units value * @param value the pixelled value * @param unit the new unit * @return the value in the given units */ public static double convertFromPixelToUnit(double value, String unit){ double i=value; if(unit!=null && ! unit.equals("")){ unit=unit.trim(); if(unit.equals("pt")){ i=value/1.25; }else if(unit.equals("pc")){ i=value/15; }else if(unit.equals("mm")){ i=value/3.543307; }else if(unit.equals("cm")){ i=value/35.43307; }else if(unit.equals("in")){ i=value/90; } } return i; } /** * normalizes the given node * @param node a node */ public void normalizeNode(Node node){ removeTspans(node); transformToMatrix(node); translateFromAttributesToStyle(node); } /** * removes the attributes specified in the properties.xml and adds them to the style attribute for this node * @param node a node */ protected void translateFromAttributesToStyle(Node node){ if(node!=null && node instanceof Element && !(node instanceof SVGFontFaceElement)){ Element element=(Element)node; String style=element.getAttribute("style"); if(style==null){ style=""; } //the list of the attributes to be removed and added to the style attribute HashSet<String> styleProperties=editor.getResource().getAttributesToTranslate(); styleProperties.add("stop-opacity"); styleProperties.add("stop-color"); String name="", value=""; NamedNodeMap attributes=node.getAttributes();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -