Package com.jsevy.jsvg
Class SVGDocument
- java.lang.Object
-
- com.jsevy.jxml.XMLNode
-
- com.jsevy.jxml.XMLDocument
-
- com.jsevy.jsvg.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SVGDocument.Units
Enumeration of units that may be used for dimensions in the SVG file; used in setUnits() method call
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
svgNamespaceURI
-
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 Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEntity(SVGEntity entity)
Utility method used by the associated SVGGraphics object.static java.lang.String
addUnits(double value)
Utility routine for use with addSetToEntity method when geometric values are supplied.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.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.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.SVGEntity
getLastEntity()
Utility method for use with addSet method of SVGObject to enable animation of elements.static java.lang.String
scaleValue(double value)
Utility routine for use with addSetToEntity method when geometric values are supplied.static java.lang.String
scaleValueWithUnits(double value)
Utility routine for use with addSetToEntity method when geometric values are supplied.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.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; deprecated, use setUnits(Units) method employing Units enum instead.void
setUnits(SVGDocument.Units units)
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
-
-
-
-
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 SVGobject 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 settoValue
- 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-isbeginTimeSeconds
- when the attribute value should be setdurationSeconds
- how long the attribute should remain set, in seconds, or Double.MAX_VALUE or Double.POSITIVE_INFINITY for indefinite (permanent) setscaleValue
- 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 SVGobject 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 animatedfromValue
- 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 secondsdurationSeconds
- the duration of the animation (time from starting to ending attribute value), in secondsrepeatCount
- an integer indicating how many times the animation should repeat; Integer.MAX_VALUE continues the repetition indefinitelyfreezeAtEnd
- if true, the animation freezes in its last state; if false, the effects of the animation are removed after all cycles have been completedscaleValues
- 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 SVGobject 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 animatedvalueList
- 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 truebeginTimeSeconds
- when the animation should begin, in secondsdurationSeconds
- the duration of the animation (time from starting to ending attribute value), in secondsrepeatCount
- an integer indicating how many times the animation should repeat; Integer.MAX_VALUE continues the repetition indefinitelyfreezeAtEnd
- if true, the animation freezes in its last state; if false, the effects of the animation are removed after all cycles have been completedscaleValues
- 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.
-
-