Class SVGDocument

  • All Implemented Interfaces:
    java.lang.Cloneable, org.w3c.dom.Document, org.w3c.dom.Node

    public class SVGDocument
    extends com.jsevy.jxml.XMLDocument
    Class representing a Scalable Vector Graphics document, which owns an svgGraphics on which drawing commands can be made. The document's toSVGString method can then be called to generate the SVG text corresponding to the drawing done on the Graphics object. The typical workflow is as follows:
     
     // Create an svg document and get its associated SVGGraphics instance
     SVGDocument svgDocument = new SVGDocument(); 
     SVGGraphics svgGraphics = svgDocument.getGraphics(); 
     
     // Do drawing commands as on any other Graphics. If you have a paint(Graphics) method, 
     // you can just use it with the SVGGraphics instance since it's a subclass of Graphics. 
     graphics.setColor(Color.RED);  
     graphics.setStroke(new BasicStroke(3));
     graphics.drawLine(0, 0, 1000, 500); 
     graphics.drawRect(1000, 500, 150, 150); 
     graphics.drawRoundRect(20, 200, 130, 100, 20, 10); 
     
     // Get the flat SVG output as a string - it's just XML text - and  save  in a file for use 
     // with LibreOffice Draw or any other draw package that accepts flat SVG (.svg) files
     String stringOutput = svgDocument.toSVGString(); 
     String filePath = "path/to/file.svg"; 
     FileWriter fileWriter = new FileWriter(filePath); 
     fileWriter.write(svgText); 
     fileWriter.flush(); 
     fileWriter.close();
     
     
    Author:
    jsevy
    • Field Summary

      • Fields inherited from interface org.w3c.dom.Node

        ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE
    • Constructor Summary

      Constructors 
      Constructor Description
      SVGDocument()
      Create a new SVGDocument
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addEntity​(com.jsevy.jsvg.SVGEntity entity)
      Utility method used by the associated SVGGraphics object.
      void autosizeDocument​(boolean autosize)
      Specify whether or not to autosize the document, which will set the viewport to the minimum size needed to enclose all of the graphics entities.
      java.awt.geom.Rectangle2D.Double getDocumentBounds()
      Return a copy of the rectangle defining the viewport of the document.
      java.awt.geom.Rectangle2D.Double getEntityBounds()
      Return the smallest rectangle which encloses the graphical elements included in the drawing up to the point of this call.
      SVGGraphics getGraphics()
      Get the SVGGraphics associated with this document for use with standard Graphics drawing operations to generate an svg text representation.
      void setDocumentBounds​(java.awt.geom.Rectangle2D.Double bounds)
      Set the document rectangle, which equates to the viewBox for the SVG drawing which is added as an attribute to the top-level svg element.
      void setDocumentSize​(double documentWidth, double documentHeight)
      Set the document width and height, which equates to the overall viewBox width and height for the SVG drawing added as an attribute to the top-level svg element.
      void setScaleFactor​(double javaUnitsPerUnit)
      Set the scale factor for dimensions in the Java drawing - how many Java units/pixels correspond to one unit in the SVG output.
      void setUnits​(int unitsCode)
      Set the units for interpreting the dimension values in the SVG file.
      java.lang.String toSVGString()
      Return the SVG text associated with this SVG document.
      • Methods inherited from class com.jsevy.jxml.XMLDocument

        adoptNode, cloneNode, createAttribute, createAttributeNS, createCDATASection, createComment, createDocumentFragment, createElement, createElementNS, createEntityReference, createProcessingInstruction, createTextNode, getDoctype, getDocumentElement, getDocumentURI, getDomConfig, getElementById, getElementsByTagName, getElementsByTagNameNS, getImplementation, getInputEncoding, getNodeType, getStrictErrorChecking, getTextContent, getXmlEncoding, getXmlStandalone, getXmlVersion, importNode, normalizeDocument, renameNode, setDoctype, setDocumentElement, setDocumentURI, setInputEncoding, setStrictErrorChecking, setXmlEncoding, setXmlStandalone, setXmlVersion
      • Methods inherited from class com.jsevy.jxml.XMLNode

        appendChild, clone, compareDocumentPosition, equals, getAttributes, getBaseURI, getChildNodes, getDescendants, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getUserData, hasAttributes, hasChildNodes, insertAfter, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, prependChild, removeChild, replaceChild, setNamespaceURI, setNodeName, setNodeValue, setOwnerDocument, setParentNode, setPrefix, setTextContent, setUserData
      • Methods inherited from class java.lang.Object

        getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.w3c.dom.Node

        appendChild, compareDocumentPosition, getAttributes, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getUserData, hasAttributes, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setPrefix, setTextContent, setUserData
    • Constructor Detail

      • SVGDocument

        public SVGDocument()
        Create a new SVGDocument
    • Method Detail

      • setUnits

        public void setUnits​(int unitsCode)
        Set the units for interpreting the dimension values in the SVG file.
        Parameters:
        unitsCode - SVG code indicating units: 0 = Unitless 1 = Inches 2 = Feet 3 = Miles 4 = Millimeter 5 = Centimeters 6 = Meters 7 = Kilometers 8 = Microinches 9 = Mils 10 = Yards
      • setDocumentSize

        public void setDocumentSize​(double documentWidth,
                                    double documentHeight)
        Set the document width and height, which equates to the overall viewBox width and height for the SVG drawing added as an attribute to the top-level svg element. Note that the units used are those set with a call to setUnits(). This also turns off autosizing of the document.
        Parameters:
        documentWidth - Document/viewBox width, in units set by call to setUnits()
        documentHeight - Document/viewBox height, in units set by call to setUnits()
      • setDocumentBounds

        public void setDocumentBounds​(java.awt.geom.Rectangle2D.Double bounds)
        Set the document rectangle, which equates to the viewBox for the SVG drawing which is added as an attribute to the top-level svg element. Note that the units used are those set with a call to setUnits(). This also turns off autosizing of the document.
        Parameters:
        bounds - Document/viewBox rectangle, in units set by call to setUnits()
      • getDocumentBounds

        public java.awt.geom.Rectangle2D.Double getDocumentBounds()
        Return a copy of the rectangle defining the viewport of the document.
        Returns:
        A copy of the rectangle defining the document viewport
      • autosizeDocument

        public void autosizeDocument​(boolean autosize)
        Specify whether or not to autosize the document, which will set the viewport to the minimum size needed to enclose all of the graphics entities. The default is true, but this may be set to false by a call to this method, at which point a default viewport will be used, or by a call to either the setDocumentSize() or setDocumentBounds() methods, which explicitly specify the document (viewport) size.
        Parameters:
        autosize - Boolean specifying whether the document should be automatically sized to enclose all of the graphical entities in the generated SVG; default is true
      • getEntityBounds

        public java.awt.geom.Rectangle2D.Double getEntityBounds()
        Return the smallest rectangle which encloses the graphical elements included in the drawing up to the point of this call. This can be used to specify a viewport for the SVG, for example if it is desired to include margins around the graphics, by calling after drawing is completed and using the result to define an enlarged rectangle for use in a call to setDocumentBounds().
        Returns:
        Rectangle enclosing all of the graphical elements in the design at the point of the call to this method
      • setScaleFactor

        public void setScaleFactor​(double javaUnitsPerUnit)
        Set the scale factor for dimensions in the Java drawing - how many Java units/pixels correspond to one unit in the SVG output. Note that the units can be set with a call to setUnits
        Parameters:
        javaUnitsPerUnit - How many Java units/pixels correspond to one SVG unit
      • getGraphics

        public SVGGraphics getGraphics()
        Get the SVGGraphics associated with this document for use with standard Graphics drawing operations to generate an svg text representation.
        Returns:
        The SVGGraphics associated with this document, on which standard Java Graphics drawing calls can be made
      • toSVGString

        public java.lang.String toSVGString()
        Return the SVG text associated with this SVG document. This includes the header and entities populated with content generated by graphics calls on the associated SVGGraphics.
        Returns:
        The SVG text associated with this document.
        Throws:
        javax.xml.transform.TransformerException
      • addEntity

        public void addEntity​(com.jsevy.jsvg.SVGEntity entity)
        Utility method used by the associated SVGGraphics object.
        Parameters:
        entity - an svgEntity instance