📄 svgparserandstore.java
字号:
return null; } private static Shape createShapeFromRectElement(final SVGOMRectElement el, int transX, int transY) { //Shape shape = null; int x; //Shape shape = null; int y; //Shape shape = null; int width; //Shape shape = null; int height; x = parseToInt(el.getAttribute("x")) + transX; y = parseToInt(el.getAttribute("y")) + transY; width = parseToInt(el.getAttribute("width")); height = parseToInt(el.getAttribute("height")); // to do: include transform Rectangle r = new Rectangle(x, y, width, height); //shape = new StyledShape(r); //style /* float strokeWidth = getComputedStrokeWidth(el); if (strokeWidth > 0) { shape.setStrokeWidth(strokeWidth); } Color strokeColor = getComputedColor(el, "stroke"); if (strokeColor != null) { shape.setStrokeColor(strokeColor); } Color fillColor = getComputedColor(el, "fill"); if (fillColor != null) { shape.setFillColor(fillColor); } */ return r; } private static Shape createShapeFromEllipseElement( final SVGOMEllipseElement el, int transX, int transY) { //StyledShape shape = null; int cx; //StyledShape shape = null; int cy; //StyledShape shape = null; int rx; //StyledShape shape = null; int ry; cx = parseToInt(el.getAttribute("cx")); cy = parseToInt(el.getAttribute("cy")); rx = parseToInt(el.getAttribute("rx")); ry = parseToInt(el.getAttribute("ry")); Ellipse2D.Float ellipse = new Ellipse2D.Float((transX + cx) - (rx / 2), (transY + cy) - (ry / 2), 2 * rx, 2 * ry); //shape = new StyledShape(ellipse); //style /* float strokeWidth = getComputedStrokeWidth(el); if (strokeWidth > 0) { shape.setStrokeWidth(strokeWidth); } Color strokeColor = getComputedColor(el, "stroke"); if (strokeColor != null) { shape.setStrokeColor(strokeColor); } Color fillColor = getComputedColor(el, "fill"); if (fillColor != null) { shape.setFillColor(fillColor); } */ return ellipse; } private static Shape createShapeFromCircleElement( final SVGOMCircleElement el, int transX, int transY) { //StyledShape shape = null; int cx; //StyledShape shape = null; int cy; //StyledShape shape = null; int r; cx = parseToInt(el.getAttribute("cx")); cy = parseToInt(el.getAttribute("cy")); r = parseToInt(el.getAttribute("r")); Ellipse2D.Float ellipse = new Ellipse2D.Float((transX + cx) - (r / 2), (transY + cy) - (r / 2), 2 * r, 2 * r); //shape = new StyledShape(ellipse); //style /* float strokeWidth = getComputedStrokeWidth(el); if (strokeWidth > 0) { shape.setStrokeWidth(strokeWidth); } Color strokeColor = getComputedColor(el, "stroke"); if (strokeColor != null) { shape.setStrokeColor(strokeColor); } Color fillColor = getComputedColor(el, "fill"); if (fillColor != null) { shape.setFillColor(fillColor); } */ return ellipse; } private static Shape createShapeFromPathElement(final SVGOMPathElement el, int transX, int transY) { Shape shape = null; return shape; } private static Shape createShapeFromLineElement(final SVGOMLineElement el, int transX, int transY) { int p1x; int p1y; int p2x; int p2y; p1x = parseToInt(el.getAttribute("x1")); p1y = parseToInt(el.getAttribute("y1")); p2x = parseToInt(el.getAttribute("x2")); p2y = parseToInt(el.getAttribute("y2")); Line2D.Float line = new Line2D.Float(p1x + transX, p1y + transY, p2x + transX, p2y + transY); return line; } ///////////////////////////////////////////////////// // utility methods ///////////////////////////////////////////////////// private static int parseToInt(String s) { if ((s == null) || (s.length() == 0)) { return 0; } try { int val = 0; if (s.indexOf('.') > -1) { float floatVal = Float.parseFloat(s); val = (int) floatVal; } else { val = Integer.parseInt(s); } return val; } catch (NumberFormatException nfe) { LOG.warning("NFE: " + nfe.getMessage()); } return 0; } /* private static float getComputedStrokeWidth(CSSStylableElement el) { float strokeWidth = -1; int index = -1; index = cssEngine.getPropertyIndex("stroke-width"); if (index >= 0) { strokeWidth = cssEngine.getComputedStyle(el, null, index).getFloatValue(); } return strokeWidth; } */ /* private static Color getComputedColor(CSSStylableElement el, String attrib) { Color c = null; int index = -1; index = cssEngine.getPropertyIndex(attrib); if (index >= 0) { Value v = cssEngine.getComputedStyle(el, null, index); if (v != null) { c = new Color((int)v.getRed().getFloatValue(), (int)v.getGreen().getFloatValue(), (int)v.getBlue().getFloatValue()); } } return c; } */ ///////////////////////////////////////////////////////////////////// // Storage of the svg file ///////////////////////////////////////////////////////////////////// /** * Creates a dom tree, fetching the <code>defs</code> from the stored * SVGDocument, and writes it to the svg file referenced by the * Transcription. * * @param transcription the Transcription containing svg id's and graphical * shapes */ public static synchronized void storeSVG(Transcription transcription) { if (transcription != null) { // check the svg path String svgFileString = null; if (transcription instanceof TranscriptionImpl) { svgFileString = ((TranscriptionImpl) transcription).getSVGFile(); } if ((svgFileString == null) || (svgFileString.length() == 0)) { // create a svg file path String path = ""; if (transcription instanceof TranscriptionImpl) { path = transcription.getFullPath(); } else { path = transcription.getName(); } if (path.endsWith(".eaf")) { int index = path.lastIndexOf(".eaf"); svgFileString = path.substring(0, index) + ".svg"; } else { return; } if (!svgFileString.toLowerCase().endsWith(".svg")) { svgFileString += ".svg"; } } if (svgFileString.startsWith("file:")) { svgFileString = svgFileString.substring(5); } /* if ((((TranscriptionImpl) transcription).getSVGFile() == null) || !((TranscriptionImpl) transcription).getSVGFile().equals(svgFileString)) { ((TranscriptionImpl) transcription).setSVGFile(svgFileString); } */ if (((TranscriptionImpl) transcription).getSVGFile() != null) { if (documents.get(transcription) instanceof SVGOMDocument) { svgDoc = (SVGOMDocument) documents.get(transcription); } Element docElement = null; try { docElement = createDOM(transcription); } catch (DOMException dome) { LOG.severe( "Could not save the svg file: could not create DOM tree."); LOG.severe(LogUtil.formatStackTrace(dome)); return; } if (docElement != null) { try { IoUtil.writeEncodedFile("UTF-8", svgFileString, docElement); } catch (Exception ex) { LOG.severe("Error while saving file."); LOG.warning(LogUtil.formatStackTrace(ex)); } } else { LOG.warning("Empty svg file."); } } else { // try to rename an existing svg file File svgFile = new File(svgFileString); if (svgFile.exists()) { File renamed = new File(svgFileString + "_old"); boolean success = svgFile.renameTo(renamed); if (!success) { LOG.warning("Could not rename empty svg file"); } } } /* for the time being rename the file when there are no tiers with a linguistic type * that allows graphical annotations. This must change when svg files are decently * referenced in the eaf as external files. Renaming prevents that the next time * the eaf is loaded an SVGViewer is being created. */ /* done elsewhere now boolean atLeastOne = false; Vector tiers = transcription.getTiers(); Iterator tierIt = tiers.iterator(); TierImpl tier; while (tierIt.hasNext()) { tier = (TierImpl) tierIt.next(); if (tier.getLinguisticType().hasGraphicReferences()) { atLeastOne = true; break; } } if (!atLeastOne) { File svgFile = new File(svgFileString); if (svgFile.exists()) { File renamed = new File(svgFileString + "_old"); boolean success = svgFile.renameTo(renamed); if (!success) { LOG.warning("Could not rename empty svg file"); } } } */ } else { return; } } private static Element createDOM(Transcription transcription) throws DOMException { boolean defsEmpty = true; SVG10Factory svgFactory = new SVG10Factory(); // clone the doctype element - does not get parsed yet if ((svgDoc != null) && (svgDoc.getDoctype() != null)) { svgFactory.appendChild(svgDoc.getDoctype()); } Element svgEl = svgFactory.getDocumentElement(); //svgFactory.appendChild(svgEl); // clone the defs element from the original file, if any if (svgDoc != null) { NodeList children = svgDoc.getRootElement().getChildNodes(); SVGOMDefsElement defsEl = null; for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (n.getNodeName().equals("defs")) { defsEl = (SVGOMDefsElement) n; break; } } if ((defsEl != null) && defsEl.hasChildNodes()) { defsEmpty = false; AbstractNode copyNode = (AbstractNode) defsEl.cloneNode(true); Document d = (Document) svgFactory.getDocument(); copyNode.setOwnerDocument(d); svgEl.appendChild(copyNode); } } // now iterate over the tiers with graphic annotations Shape shape = null; Iterator it = transcription.getTiers().iterator(); while (it.hasNext()) { TierImpl tier = (TierImpl) it.next(); if ((tier.getLinguisticType() != null) && tier.getLinguisticType().hasGraphicReferences()) { //iter over the annotations Iterator annIt = tier.getAnnotations().iterator(); SVGAlignableAnnotation ann; while (annIt.hasNext()) { ann = (SVGAlignableAnnotation) annIt.next(); shape = ann.getShape(); String id = ann.getSVGElementID(); if ((shape == null) || (id.length() == 0)) { continue; } Element group = svgFactory.newGroup(id); if (shape instanceof Rectangle) { Element rectEl = svgFactory.newRect((RectangularShape) shape); group.appendChild(rectEl); svgEl.appendChild(group); } else if (shape instanceof Ellipse2D) { Element ellEl = svgFactory.newEllipse((RectangularShape) shape); group.appendChild(ellEl); svgEl.appendChild(group); } else if (shape instanceof Line2D) { Element ellEl = svgFactory.newLine((Line2D) shape); group.appendChild(ellEl); svgEl.appendChild(group); } } } } // there should at least be content in either the defs element or the svg element //if (!defsEmpty || svgFactory.getDocumentElement().hasChildNodes()) { return svgFactory.getDocumentElement(); //} else { // return null; //} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -