Class SVGDocument

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

    public class SVGDocument
    extends 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 Detail

      • svgNamespaceURI

        public static java.lang.String svgNamespaceURI
    • Constructor Detail

      • SVGDocument

        public SVGDocument()
        Create a new SVGDocument
    • Method Detail

      • setUnits

        public void setUnits​(SVGDocument.Units units)
        Set the units for interpreting the dimension values in the SVG file.
        Parameters:
        units - SVGDocument.Units enum value for desired units
      • setUnits

        public void setUnits​(int unitsCode)
        Set the units for interpreting the dimension values in the SVG file; deprecated, use setUnits(Units) method employing Units enum instead.
        Parameters:
        unitsCode - SVG code indicating units: 0 = Unitless 1 = Inches 2 = Feet 3 = Miles 4 = Millimeters 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​(SVGEntity entity)
        Utility method used by the associated SVGGraphics object.
        Parameters:
        entity - an svgEntity instance
      • getLastEntity

        public SVGEntity getLastEntity()
        Utility method for use with addSet method of SVGObject to enable animation of elements.
        Returns:
        Last entity added to the document; may be null if no entities
      • setAttribute

        public void setAttribute​(java.lang.String attributeName,
                                 java.lang.Object toValue,
                                 double beginTimeSeconds,
                                 double durationSeconds,
                                 boolean scaleValue)
        For the last added SVG entity (the last object created by one the Graphics draw methods), set the value of the specified attribute to the value specified by toValue, beginning at the specified start time and for the specified duration. Internally, this adds an SVG object as a child of the supplied entity. The exact syntax and behavior can be found in the SVG specification.
        Parameters:
        attributeName - name of attribute to be set
        toValue - value to which attribute should be set; this may be a numerical value (primitive or object), a String, or a Java Color object. A numerical value will be scaled by the current scale value as set by setScale(); a Color object will be converted into the RGB format used in SVG; and a String will be included as-is
        beginTimeSeconds - when the attribute value should be set
        durationSeconds - how long the attribute should remain set, in seconds, or Double.MAX_VALUE or Double.POSITIVE_INFINITY for indefinite (permanent) set
        scaleValue - whether to scale the from and to values by the current scale value as set by setScale(); only meaningful if the values are numerical
      • animateAttribute

        public void animateAttribute​(java.lang.String attributeName,
                                     java.lang.Object fromValue,
                                     java.lang.Object toValue,
                                     double beginTimeSeconds,
                                     double durationSeconds,
                                     int repeatCount,
                                     boolean freezeAtEnd,
                                     boolean scaleValues)
        For the supplied SVG entity, animate the value of the specified attribute between the supplied fromValue and toValue, starting at the specified beginning time and over the period specified by the duration. Repeat the indicated number of times; if freezeAtEnd is true, hold the final value of the attribute, otherwise remove the attribute changes when the animation period is finished. Internally, this adds an SVG object as a child of the supplied entity. The exact syntax and behavior can be found in the SVG specification.
        Parameters:
        attributeName - name of the attribute to be animated
        fromValue - value to which attribute should be set to start the animation; this may be a numerical value (primitive or object), a String, or a Java Color object. A numerical value will be scaled by the current scale value if scaleValue is true; a Color object will be converted into the RGB format used in SVG; and a String will be included as-is. The special value "" indicates that the starting value should be whatever the current value is for the attribute.
        toValue - value to which attribute should approach over the animation duration; this may be a numerical value (primitive or object), a String, or a Java Color object. A numerical value will be scaled by the current scale value if scaleValue is true; a Color object will be converted into the RGB format used in SVG; and a String will be included as-is.
        beginTimeSeconds - when the animation should begin, in seconds
        durationSeconds - the duration of the animation (time from starting to ending attribute value), in seconds
        repeatCount - an integer indicating how many times the animation should repeat; Integer.MAX_VALUE continues the repetition indefinitely
        freezeAtEnd - if true, the animation freezes in its last state; if false, the effects of the animation are removed after all cycles have been completed
        scaleValues - whether to scale the from and to values by the current scale value as set by setScale(); only meaningful if the values are numerical
      • animateAttribute

        public void animateAttribute​(java.lang.String attributeName,
                                     java.lang.String valueList,
                                     double beginTimeSeconds,
                                     double durationSeconds,
                                     int repeatCount,
                                     boolean freezeAtEnd,
                                     boolean scaleValues)
        For the supplied SVG entity, animate the value of the specified attribute between the supplied fromValue and toValue, starting at the specified beginning time and over the period specified by the duration. Repeat the indicated number of times; if freezeAtEnd is true, hold the final value of the attribute, otherwise remove the attribute changes when the animation period is finished. Internally, this adds an SVG object as a child of the supplied entity. The exact syntax and behavior can be found in the SVG specification.
        Parameters:
        attributeName - name of the attribute to be animated
        valueList - string with semicolon-separated entries; these will be interpreted as a sequence of values the attribute should be animated through over the duration of the animation, as per the SVG specification. Numerical values will be scaled by the current scale value if scaleValue is true
        beginTimeSeconds - when the animation should begin, in seconds
        durationSeconds - the duration of the animation (time from starting to ending attribute value), in seconds
        repeatCount - an integer indicating how many times the animation should repeat; Integer.MAX_VALUE continues the repetition indefinitely
        freezeAtEnd - if true, the animation freezes in its last state; if false, the effects of the animation are removed after all cycles have been completed
        scaleValues - whether to scale the from and to values by the current scale value as set by setScale(); only meaningful if the values are numerical
      • scaleValueWithUnits

        public static java.lang.String scaleValueWithUnits​(double value)
        Utility routine for use with addSetToEntity method when geometric values are supplied. Given a double value, scale it according to the scale factor previously set, and add the units string.
        Parameters:
        value - Value to be scaled
        Returns:
        String giving the value with units appended.
      • scaleValue

        public static java.lang.String scaleValue​(double value)
        Utility routine for use with addSetToEntity method when geometric values are supplied. Given a double value, scale it according to the scale factor previously set, and return the String representation.
        Parameters:
        value - Value to be scaled
        Returns:
        Scaled value
      • addUnits

        public static java.lang.String addUnits​(double value)
        Utility routine for use with addSetToEntity method when geometric values are supplied. Given a double value, convert to a String and add the units string.
        Parameters:
        value - Value
        Returns:
        String giving the value with units appended.