- java.lang.Object
-
- java.awt.Graphics
-
- java.awt.Graphics2D
-
- org.jfree.svg.SVGGraphics2D
-
public final class SVGGraphics2D extends java.awt.Graphics2DA
UsageGraphics2Dimplementation that creates SVG output. After rendering the graphics via theSVGGraphics2D, you can retrieve an SVG element (seegetSVGElement()) or an SVG document (seegetSVGDocument()) containing your content.
Using the
SVGGraphics2Dclass is straightforward. First, create an instance specifying the height and width of the SVG element that will be created. Then, use standard Java2D API calls to draw content into the element. Finally, retrieve the SVG element that has been accumulated. For example:SVGGraphics2D g2 = new SVGGraphics2D(300, 200); g2.setPaint(Color.RED); g2.draw(new Rectangle(10, 10, 280, 180)); String svgElement = g2.getSVGElement();For the content generation step, you can make use of third party libraries, such as JFreeChart and Orson Charts, that render output using standard Java2D API calls.
Rendering Hints
The
Other NotesSVGGraphics2Dsupports a couple of custom rendering hints - for details, refer to theSVGHintsclass documentation. Also see the examples in this blog post: Orson Charts 3D / Enhanced SVG Export.
Some additional notes:- by default, JFreeSVG uses a fast conversion of numerical values to
strings for the SVG output (the 'RyuDouble' implementation). If you
prefer a different approach (for example, controlling the number of
decimal places in the output to reduce the file size) you can set your
own functions for converting numerical values - see the
setGeomDoubleConverter(DoubleFunction)andsetTransformDoubleConverter(DoubleFunction)methods. - the
getFontMetrics(java.awt.Font)andgetFontRenderContext()methods return values that come from an internalBufferedImage, this is a short-cut and we don't know if there are any negative consequences (if you know of any, please let us know and we'll add the info here or find a way to fix it); - Images are supported, but for methods with an
ImageObserverparameter note that the observer is ignored completely. In any case, using images that are not fully loaded already would not be a good idea in the context of generating SVG data/files; - when an HTML page contains multiple SVG elements, the items within
the DEFS element for each SVG element must have IDs that are unique across
all SVG elements in the page. JFreeSVG auto-populates the
defsKeyPrefixattribute to help ensure that unique IDs are generated.
For some demos showing how to use this class, look at the JFree-Demos project at GitHub: https://github.com/jfree/jfree-demos.
- by default, JFreeSVG uses a fast conversion of numerical values to
strings for the SVG output (the 'RyuDouble' implementation). If you
prefer a different approach (for example, controlling the number of
decimal places in the output to reduce the file size) you can set your
own functions for converting numerical values - see the
-
-
Field Summary
Fields Modifier and Type Field Description private java.awt.geom.Arc2DarcAn instance that is reused in draw/fillArc to avoid creating a lot of garbage.private java.awt.ColorbackgroundThe background color, used by clearRect().private booleancheckStrokeControlHintA flag that controls whether or not the KEY_STROKE_CONTROL hint is checked.private java.awt.ShapeclipThe user clip (can be null).private static java.lang.StringCLIP_KEY_PREFIXThe prefix for keys used to identify clip paths.private java.util.List<java.lang.String>clipPathsA list of the registered clip regions.private java.lang.StringclipRefThe reference for the current clip.private java.awt.Colorcolorprivate java.awt.Compositecompositeprivate static floatDEFAULT_MITER_LIMITprivate static java.lang.StringDEFAULT_STROKE_CAPprivate static java.lang.StringDEFAULT_STROKE_JOINprivate java.lang.StringdefsKeyPrefixA prefix for the keys used in the DEFS element.private java.awt.GraphicsConfigurationdeviceConfigurationThe device configuration (this is lazily instantiated in the getDeviceConfiguration() method).private java.util.Set<java.lang.String>elementIDsA set of element IDs.private java.lang.StringfilePrefixThe filename prefix for images that are referenced rather than embedded but don't have anhrefsupplied via theSVGHints.KEY_IMAGE_HREFhint.private java.lang.StringfileSuffixThe filename suffix for images that are referenced rather than embedded but don't have anhrefsupplied via theSVGHints.KEY_IMAGE_HREFhint.private java.awt.image.BufferedImagefmImageAn internal image used for font metrics.private java.awt.Graphics2DfmImageG2DThe graphics target for the internal image that is used for font metrics.private java.awt.FontfontThe last font that was set.private java.util.function.Function<java.lang.String,java.lang.String>fontFunctionGenerates the SVG font from the Java font family name (this function provides a hook for custom output formatting (for example putting quotes around the font family name - see issue #27) and font substitutions.private java.awt.font.FontRenderContextfontRenderContextThe font render context.private SVGUnitsfontSizeUnitsThe font size units.private java.util.function.DoubleFunction<java.lang.String>geomDoubleConverterThe function used to convert double values to strings for the geometry coordinates in the SVG output.private java.lang.StringgradientPaintRefIf the current paint is an instance ofGradientPaint, this field will contain the reference id that is used in the DEFS element for that linear gradient.private java.util.Map<GradientPaintKey,java.lang.String>gradientPaintsA map of all the gradients used, and the corresponding id.private doubleheightThe height of the SVG.private java.awt.RenderingHintshintsRendering hints (see SVGHints).private java.util.List<ImageElement>imageElementsA list of images that are referenced but not embedded in the SVG.private java.awt.geom.Line2DlineAn instance that is lazily instantiated in drawLine and then subsequently reused to avoid creating a lot of garbage.private java.util.Map<LinearGradientPaintKey,java.lang.String>linearGradientPaintsA map of all the linear gradients used, and the corresponding id.private java.awt.geom.Ellipse2DovalAn instance that is lazily instantiated in draw/fillOval and then subsequently reused to avoid creating a lot of garbage.private java.awt.PaintpaintThe paint used to draw or fill shapes and text.private java.util.Map<RadialGradientPaintKey,java.lang.String>radialGradientPaintsA map of all the radial gradients used, and the corresponding id.private java.awt.geom.Rectangle2DrectAn instance that is lazily instantiated in fillRect and then subsequently reused to avoid creating a lot of garbage.private java.awt.geom.RoundRectangle2DroundRectAn instance that is lazily instantiated in draw/fillRoundRect and then subsequently reused to avoid creating a lot of garbage.private java.lang.StringBuildersbThe buffer that accumulates the SVG output.private java.awt.StrokestrokeThe current stroke.private java.awt.geom.AffineTransformtransformThe current transform.private java.util.function.DoubleFunction<java.lang.String>transformDoubleConverterThe function used to convert double values to strings when writing matrix values for transforms in the SVG output.private SVGUnitsunitsUnits for the width and height of the SVG, if null then no unit information is written in the SVG output.private doublewidthThe width of the SVG.private doublezeroStrokeWidthThe width of the SVG stroke to use when the user supplies a BasicStroke with a width of 0.0 (in this case the Java specification says "If width is set to 0.0f, the stroke is rendered as the thinnest possible line for the target device and the antialias hint setting.")
-
Constructor Summary
Constructors Modifier Constructor Description SVGGraphics2D(double width, double height)Creates a new instance with the specified width and height.SVGGraphics2D(double width, double height, SVGUnits units)Creates a new instance with the specified width and height in the given units.SVGGraphics2D(double width, double height, SVGUnits units, java.lang.StringBuilder sb)Creates a new instance with the specified width and height that will populate the suppliedStringBuilderinstance.privateSVGGraphics2D(SVGGraphics2D parent)Creates a new instance that is a child of the supplied parent.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRenderingHints(java.util.Map<?,?> hints)Adds all the supplied rendering hints.private voidappendOptionalElementIDFromHint(java.lang.StringBuilder builder)A utility method that appends an optional element id if one is specified via the rendering hints.voidclearRect(int x, int y, int width, int height)Clears the specified rectangle by filling it with the current background color.voidclip(java.awt.Shape s)Clips to the intersection of the current clipping region and the specified shape.voidclipRect(int x, int y, int width, int height)Clips to the intersection of the current clipping region and the specified rectangle.voidcopyArea(int x, int y, int width, int height, int dx, int dy)This method does nothing.java.awt.Graphicscreate()Creates a new graphics object that is a copy of this graphics object (except that it has not accumulated the drawing operations).voiddispose()This method does nothing, there are no resources to dispose.voiddraw(java.awt.Shape s)Draws the specified shape with the currentpaintandstroke.voiddrawArc(int x, int y, int width, int height, int startAngle, int arcAngle)Draws an arc contained within the rectangle(x, y, width, height), starting atstartAngleand continuing througharcAngledegrees using the currentpaintandstroke.voiddrawGlyphVector(java.awt.font.GlyphVector g, float x, float y)Draws the specified glyph vector at the location(x, y).voiddrawImage(java.awt.image.BufferedImage img, java.awt.image.BufferedImageOp op, int x, int y)Draws the image resulting from applying theBufferedImageOpto the specified image at the location(x, y).booleandrawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)) into the destination rectangle(dx1, dy1, dx2, dy2).booleandrawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver observer)Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)) into the destination rectangle(dx1, dy1, dx2, dy2).booleandrawImage(java.awt.Image img, int x, int y, int w, int h, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws an image to the rectangle(x, y, w, h)(scaling it if required), first filling the background with the specified color.booleandrawImage(java.awt.Image img, int x, int y, int w, int h, java.awt.image.ImageObserver observer)Draws the image into the rectangle defined by(x, y, w, h).booleandrawImage(java.awt.Image img, int x, int y, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws an image at the location(x, y).booleandrawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)Draws an image at the location(x, y).booleandrawImage(java.awt.Image img, java.awt.geom.AffineTransform xform, java.awt.image.ImageObserver obs)Draws an image with the specified transform.voiddrawLine(int x1, int y1, int x2, int y2)Draws a line from(x1, y1)to(x2, y2)using the currentpaintandstroke.voiddrawOval(int x, int y, int width, int height)Draws an oval framed by the rectangle(x, y, width, height)using the currentpaintandstroke.voiddrawPolygon(int[] xPoints, int[] yPoints, int nPoints)Draws the specified polygon using the currentpaintandstroke.voiddrawPolyline(int[] xPoints, int[] yPoints, int nPoints)Draws the specified multi-segment line using the currentpaintandstroke.voiddrawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)Draws the renderable image.voiddrawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)Draws the rendered image.voiddrawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Draws a rectangle with rounded corners using the currentpaintandstroke.voiddrawString(java.lang.String str, float x, float y)Draws a string at(x, y).voiddrawString(java.lang.String str, int x, int y)Draws a string at(x, y).voiddrawString(java.text.AttributedCharacterIterator iterator, float x, float y)Draws a string of attributed characters at(x, y).voiddrawString(java.text.AttributedCharacterIterator iterator, int x, int y)Draws a string of attributed characters at(x, y).voidfill(java.awt.Shape s)Fills the specified shape with the currentpaint.voidfillArc(int x, int y, int width, int height, int startAngle, int arcAngle)Fills an arc contained within the rectangle(x, y, width, height), starting atstartAngleand continuing througharcAngledegrees, using the currentpaint.voidfillOval(int x, int y, int width, int height)Fills an oval framed by the rectangle(x, y, width, height).voidfillPolygon(int[] xPoints, int[] yPoints, int nPoints)Fills the specified polygon using the currentpaint.voidfillRect(int x, int y, int width, int height)Fills the specified rectangle with the currentpaint.voidfillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Fills a rectangle with rounded corners using the currentpaint.private java.lang.StringgeomDP(double d)Returns a string representation of the specified number for use in the SVG output.private floatgetAlpha()Returns the current alpha (transparency) in the range 0.0 to 1.0.java.awt.ColorgetBackground()Returns the background color.booleangetCheckStrokeControlHint()Returns the flag that controls whether or not this object will observe theKEY_STROKE_CONTROLrendering hint.java.awt.ShapegetClip()Returns the user clipping region.java.awt.RectanglegetClipBounds()Returns the bounds of the user clipping region.private java.lang.StringgetClipPathRef()Returns a clip path reference for the current user clip.java.awt.ColorgetColor()Returns the foreground color.private floatgetColorAlpha()Returns the alpha value of the currentpaint, or1.0fif it is not an instance ofColor.java.awt.CompositegetComposite()Returns the current composite.java.lang.StringgetDefsKeyPrefix()Returns the prefix used for all keys in the DEFS element.java.awt.GraphicsConfigurationgetDeviceConfiguration()Returns the device configuration associated with thisGraphics2D.java.util.Set<java.lang.String>getElementIDs()Returns a new set containing the element IDs that have been used in output so far.java.lang.StringgetFilePrefix()Returns the prefix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.java.lang.StringgetFileSuffix()Returns the suffix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.java.awt.FontgetFont()Returns the current font used for drawing text.java.util.function.Function<java.lang.String,java.lang.String>getFontFunction()Returns the function that generates SVG font references from a supplied Java font family name.java.awt.FontMetricsgetFontMetrics(java.awt.Font f)Returns the font metrics for the specified font.java.awt.font.FontRenderContextgetFontRenderContext()Returns the font render context.SVGUnitsgetFontSizeUnits()Returns the font size units.java.util.function.DoubleFunction<java.lang.String>getGeomDoubleConverter()Returns the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output.doublegetHeight()Returns the height for the SVG element, specified in the constructor.private java.lang.StringgetLinearGradientElement(java.lang.String id, java.awt.GradientPaint paint)Returns an element to represent a linear gradient.private java.lang.StringgetLinearGradientElement(java.lang.String id, java.awt.LinearGradientPaint paint)Returns an element to represent a linear gradient.java.awt.PaintgetPaint()Returns the paint used to draw or fill shapes (or text).private byte[]getPNGBytes(java.awt.Image img)Returns the bytes representing a PNG format image.private java.lang.StringgetRadialGradientElement(java.lang.String id, java.awt.RadialGradientPaint rgp)Returns an element to represent a radial gradient.java.lang.ObjectgetRenderingHint(java.awt.RenderingHints.Key hintKey)Returns the current value for the specified hint.java.awt.RenderingHintsgetRenderingHints()Returns a copy of the rendering hints.java.awt.StrokegetStroke()Returns the current stroke (used when drawing shapes).java.lang.StringgetSVGDocument()Returns an SVG document (this contains the content returned by thegetSVGElement()method, prepended with the required document header).java.lang.StringgetSVGElement()Returns the SVG element that has been generated by calls to thisGraphics2Dimplementation.java.lang.StringgetSVGElement(java.lang.String id)Returns the SVG element that has been generated by calls to thisGraphics2Dimplementation, giving it the specifiedid.java.lang.StringgetSVGElement(java.lang.String id, boolean includeDimensions, ViewBox viewBox, PreserveAspectRatio preserveAspectRatio, MeetOrSlice meetOrSlice)Returns the SVG element that has been generated by calls to thisGraphics2Dimplementation, giving it the specifiedid.private java.lang.StringgetSVGFillStyle()Returns a fill style string based on the current paint and alpha settings.private java.lang.StringgetSVGFontStyle()Returns a string containing font style info.java.util.List<ImageElement>getSVGImages()Returns the list of image elements that have been referenced in the SVG output but not embedded.private java.lang.StringgetSVGPathData(java.awt.geom.Path2D path)Creates an SVG path string for the supplied Java2D path.private java.lang.StringgetSVGTransform(java.awt.geom.AffineTransform t)java.awt.geom.AffineTransformgetTransform()Returns a copy of the current transform.java.util.function.DoubleFunction<java.lang.String>getTransformDoubleConverter()Returns the double-to-string function that is used when writing values for matrix transformations in the SVG output.SVGUnitsgetUnits()Returns the units for the width and height of the SVG element's viewport, as specified in the constructor.doublegetWidth()Returns the width for the SVG element, specified in the constructor.doublegetZeroStrokeWidth()Returns the width to use for the SVG stroke when the AWT stroke specified has a zero width (the default value is0.1).booleanhit(java.awt.Rectangle rect, java.awt.Shape s, boolean onStroke)Returnstrueif the rectangle (in device space) intersects with the shape (the interior, ifonStrokeisfalse, otherwise the stroked outline of the shape).private booleanisDefsOutputRequired()Returnstrueif there are items that need to be written to the DEFS element, andfalseotherwise.private java.lang.StringregisterClip(java.awt.Shape clip)Registers the clip so that we can later write out all the clip definitions in the DEFS element.private java.lang.StringrgbaColorStr(java.awt.Color c)Returns a string representing the specified color in RGBA format.private java.lang.StringrgbColorStr(java.awt.Color c)Returns the SVG RGB color string for the specified color.voidrotate(double theta)Applies a rotation (anti-clockwise) about(0, 0).voidrotate(double theta, double x, double y)Applies a rotation (anti-clockwise) about(x, y).voidscale(double sx, double sy)Applies a scale transformation.voidsetBackground(java.awt.Color color)Sets the background color.voidsetCheckStrokeControlHint(boolean check)Sets the flag that controls whether or not this object will observe theKEY_STROKE_CONTROLrendering hint.voidsetClip(int x, int y, int width, int height)Sets the user clipping region to the specified rectangle.voidsetClip(java.awt.Shape shape)Sets the user clipping region.voidsetColor(java.awt.Color c)Sets the foreground color.voidsetComposite(java.awt.Composite comp)Sets the composite (onlyAlphaCompositeis handled).voidsetDefsKeyPrefix(java.lang.String prefix)Sets the prefix that will be used for all keys in the DEFS element.voidsetFilePrefix(java.lang.String prefix)Sets the prefix used to generate a filename for any image that is referenced from the SVG element.voidsetFileSuffix(java.lang.String suffix)Sets the suffix used to generate a filename for any image that is referenced from the SVG element.voidsetFont(java.awt.Font font)Sets the font to be used for drawing text.voidsetFontFunction(java.util.function.Function<java.lang.String,java.lang.String> fontFunction)Sets the font function that is used to generate SVG font references from Java font family names.voidsetFontSizeUnits(SVGUnits fontSizeUnits)Sets the font size units.voidsetGeomDoubleConverter(java.util.function.DoubleFunction<java.lang.String> converter)Sets the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output.private voidsetOval(int x, int y, int width, int height)Sets the attributes of the reusableEllipse2Dobject that is used by thedrawOval(int, int, int, int)andfillOval(int, int, int, int)methods.voidsetPaint(java.awt.Paint paint)Sets the paint used to draw or fill shapes (or text).voidsetPaintMode()Does nothing in thisSVGGraphics2Dimplementation.private voidsetRect(int x, int y, int width, int height)Sets the attributes of the reusableRectangle2Dobject that is used by theGraphics.drawRect(int, int, int, int)andfillRect(int, int, int, int)methods.voidsetRenderingHint(java.awt.RenderingHints.Key hintKey, java.lang.Object hintValue)Sets the value for a hint.voidsetRenderingHints(java.util.Map<?,?> hints)Sets the rendering hints to the specified collection.private voidsetRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Sets the attributes of the reusableRoundRectangle2Dobject that is used by thedrawRoundRect(int, int, int, int, int, int)andfillRoundRect(int, int, int, int, int, int)methods.voidsetStroke(java.awt.Stroke s)Sets the stroke that will be used to draw shapes.voidsetTransform(java.awt.geom.AffineTransform t)Sets the transform.voidsetTransformDoubleConverter(java.util.function.DoubleFunction<java.lang.String> converter)Sets the double-to-string function that is used when writing coordinates for matrix transformations in the SVG output.voidsetXORMode(java.awt.Color c)Does nothing in thisSVGGraphics2Dimplementation.voidsetZeroStrokeWidth(double width)Sets the width to use for the SVG stroke when the current AWT stroke has a width of 0.0.voidshear(double shx, double shy)Applies a shear transformation.private java.lang.StringstrokeStyle()Returns a stroke style string based on the current stroke and alpha settings.private java.lang.StringsvgColorStr()Returns an SVG color string based on the current paint.voidtransform(java.awt.geom.AffineTransform t)Applies this transform to the existing transform by concatenating it.private java.lang.StringtransformDP(double d)Returns a string representation of the specified number for use in the SVG output.voidtranslate(double tx, double ty)Applies the translation(tx, ty).voidtranslate(int tx, int ty)Applies the translation(tx, ty).
-
-
-
Field Detail
-
CLIP_KEY_PREFIX
private static final java.lang.String CLIP_KEY_PREFIX
The prefix for keys used to identify clip paths.- See Also:
- Constant Field Values
-
width
private final double width
The width of the SVG.
-
height
private final double height
The height of the SVG.
-
units
private final SVGUnits units
Units for the width and height of the SVG, if null then no unit information is written in the SVG output. This is set via the class constructors.
-
fontSizeUnits
private SVGUnits fontSizeUnits
The font size units.
-
hints
private final java.awt.RenderingHints hints
Rendering hints (see SVGHints).
-
checkStrokeControlHint
private boolean checkStrokeControlHint
A flag that controls whether or not the KEY_STROKE_CONTROL hint is checked.
-
transformDoubleConverter
private java.util.function.DoubleFunction<java.lang.String> transformDoubleConverter
The function used to convert double values to strings when writing matrix values for transforms in the SVG output.
-
geomDoubleConverter
private java.util.function.DoubleFunction<java.lang.String> geomDoubleConverter
The function used to convert double values to strings for the geometry coordinates in the SVG output.
-
sb
private final java.lang.StringBuilder sb
The buffer that accumulates the SVG output.
-
defsKeyPrefix
private java.lang.String defsKeyPrefix
A prefix for the keys used in the DEFS element. This can be used to ensure that the keys are unique when creating more than one SVG element for a single HTML page.
-
gradientPaints
private java.util.Map<GradientPaintKey,java.lang.String> gradientPaints
A map of all the gradients used, and the corresponding id. When generating the SVG file, all the gradient paints used must be defined in the defs element.
-
linearGradientPaints
private java.util.Map<LinearGradientPaintKey,java.lang.String> linearGradientPaints
A map of all the linear gradients used, and the corresponding id. When generating the SVG file, all the linear gradient paints used must be defined in the defs element.
-
radialGradientPaints
private java.util.Map<RadialGradientPaintKey,java.lang.String> radialGradientPaints
A map of all the radial gradients used, and the corresponding id. When generating the SVG file, all the radial gradient paints used must be defined in the defs element.
-
clipPaths
private java.util.List<java.lang.String> clipPaths
A list of the registered clip regions. These will be written to the DEFS element.
-
filePrefix
private java.lang.String filePrefix
The filename prefix for images that are referenced rather than embedded but don't have anhrefsupplied via theSVGHints.KEY_IMAGE_HREFhint.
-
fileSuffix
private java.lang.String fileSuffix
The filename suffix for images that are referenced rather than embedded but don't have anhrefsupplied via theSVGHints.KEY_IMAGE_HREFhint.
-
imageElements
private java.util.List<ImageElement> imageElements
A list of images that are referenced but not embedded in the SVG. After the SVG is generated, the caller can make use of this list to write PNG files if they don't already exist.
-
clip
private java.awt.Shape clip
The user clip (can be null).
-
clipRef
private java.lang.String clipRef
The reference for the current clip.
-
transform
private java.awt.geom.AffineTransform transform
The current transform.
-
paint
private java.awt.Paint paint
The paint used to draw or fill shapes and text.
-
color
private java.awt.Color color
-
composite
private java.awt.Composite composite
-
stroke
private java.awt.Stroke stroke
The current stroke.
-
zeroStrokeWidth
private double zeroStrokeWidth
The width of the SVG stroke to use when the user supplies a BasicStroke with a width of 0.0 (in this case the Java specification says "If width is set to 0.0f, the stroke is rendered as the thinnest possible line for the target device and the antialias hint setting.")
-
font
private java.awt.Font font
The last font that was set.
-
fontRenderContext
private final java.awt.font.FontRenderContext fontRenderContext
The font render context. The fractional metrics flag solves the glyph positioning issue identified by Christoph Nahr: http://news.kynosarges.org/2014/06/28/glyph-positioning-in-jfreesvg-orsonpdf/
-
fontFunction
private java.util.function.Function<java.lang.String,java.lang.String> fontFunction
Generates the SVG font from the Java font family name (this function provides a hook for custom output formatting (for example putting quotes around the font family name - see issue #27) and font substitutions.
-
background
private java.awt.Color background
The background color, used by clearRect().
-
fmImage
private java.awt.image.BufferedImage fmImage
An internal image used for font metrics.
-
fmImageG2D
private java.awt.Graphics2D fmImageG2D
The graphics target for the internal image that is used for font metrics.
-
line
private java.awt.geom.Line2D line
An instance that is lazily instantiated in drawLine and then subsequently reused to avoid creating a lot of garbage.
-
rect
private java.awt.geom.Rectangle2D rect
An instance that is lazily instantiated in fillRect and then subsequently reused to avoid creating a lot of garbage.
-
roundRect
private java.awt.geom.RoundRectangle2D roundRect
An instance that is lazily instantiated in draw/fillRoundRect and then subsequently reused to avoid creating a lot of garbage.
-
oval
private java.awt.geom.Ellipse2D oval
An instance that is lazily instantiated in draw/fillOval and then subsequently reused to avoid creating a lot of garbage.
-
arc
private final java.awt.geom.Arc2D arc
An instance that is reused in draw/fillArc to avoid creating a lot of garbage.
-
gradientPaintRef
private java.lang.String gradientPaintRef
If the current paint is an instance ofGradientPaint, this field will contain the reference id that is used in the DEFS element for that linear gradient.
-
deviceConfiguration
private java.awt.GraphicsConfiguration deviceConfiguration
The device configuration (this is lazily instantiated in the getDeviceConfiguration() method).
-
elementIDs
private final java.util.Set<java.lang.String> elementIDs
A set of element IDs.
-
DEFAULT_STROKE_CAP
private static final java.lang.String DEFAULT_STROKE_CAP
- See Also:
- Constant Field Values
-
DEFAULT_STROKE_JOIN
private static final java.lang.String DEFAULT_STROKE_JOIN
- See Also:
- Constant Field Values
-
DEFAULT_MITER_LIMIT
private static final float DEFAULT_MITER_LIMIT
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SVGGraphics2D
public SVGGraphics2D(double width, double height)Creates a new instance with the specified width and height.- Parameters:
width- the width of the SVG element.height- the height of the SVG element.
-
SVGGraphics2D
public SVGGraphics2D(double width, double height, SVGUnits units)Creates a new instance with the specified width and height in the given units.- Parameters:
width- the width of the SVG element.height- the height of the SVG element.units- the units for the width and height (nullpermitted).- Since:
- 3.2
-
SVGGraphics2D
public SVGGraphics2D(double width, double height, SVGUnits units, java.lang.StringBuilder sb)Creates a new instance with the specified width and height that will populate the suppliedStringBuilderinstance.- Parameters:
width- the width of the SVG element.height- the height of the SVG element.units- the units for the width and height (nullpermitted).sb- the string builder (nullnot permitted).- Since:
- 3.2
-
SVGGraphics2D
private SVGGraphics2D(SVGGraphics2D parent)
Creates a new instance that is a child of the supplied parent.- Parameters:
parent- the parent (nullnot permitted).
-
-
Method Detail
-
getWidth
public double getWidth()
Returns the width for the SVG element, specified in the constructor. This value will be written to the SVG element returned by thegetSVGElement()method.- Returns:
- The width for the SVG element.
-
getHeight
public double getHeight()
Returns the height for the SVG element, specified in the constructor. This value will be written to the SVG element returned by thegetSVGElement()method.- Returns:
- The height for the SVG element.
-
getUnits
public SVGUnits getUnits()
Returns the units for the width and height of the SVG element's viewport, as specified in the constructor. The default value isnull).- Returns:
- The units (possibly
null). - Since:
- 3.2
-
getCheckStrokeControlHint
public boolean getCheckStrokeControlHint()
Returns the flag that controls whether or not this object will observe theKEY_STROKE_CONTROLrendering hint. The default value istrue.- Returns:
- A boolean.
- Since:
- 2.0
- See Also:
setCheckStrokeControlHint(boolean)
-
setCheckStrokeControlHint
public void setCheckStrokeControlHint(boolean check)
Sets the flag that controls whether or not this object will observe theKEY_STROKE_CONTROLrendering hint. When enabled (the default), a hint to normalise strokes will write astroke-styleattribute with the valuecrispEdges.- Parameters:
check- the new flag value.- Since:
- 2.0
- See Also:
getCheckStrokeControlHint()
-
getDefsKeyPrefix
public java.lang.String getDefsKeyPrefix()
Returns the prefix used for all keys in the DEFS element. The default value is"_"+ String.valueOf(System.nanoTime()).- Returns:
- The prefix string (never
null). - Since:
- 1.9
-
setDefsKeyPrefix
public void setDefsKeyPrefix(java.lang.String prefix)
Sets the prefix that will be used for all keys in the DEFS element. If required, this must be set immediately after construction (before any content generation methods have been called).- Parameters:
prefix- the prefix (nullnot permitted).- Since:
- 1.9
-
getGeomDoubleConverter
public java.util.function.DoubleFunction<java.lang.String> getGeomDoubleConverter()
Returns the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output. The default function uses the Ryu algorithm for speed (see class description for more details).- Returns:
- The double-to-string function (never
null). - Since:
- 5.0
-
setGeomDoubleConverter
public void setGeomDoubleConverter(java.util.function.DoubleFunction<java.lang.String> converter)
Sets the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output. The default converter optimises for speed when generating the SVG and should cover normal usage. However, this method provides the ability to substitute an alternative function (for example, one that favours output size over speed of generation).- Parameters:
converter- the convertor function (nullnot permitted).- Since:
- 5.0
- See Also:
setTransformDoubleConverter(java.util.function.DoubleFunction)
-
getTransformDoubleConverter
public java.util.function.DoubleFunction<java.lang.String> getTransformDoubleConverter()
Returns the double-to-string function that is used when writing values for matrix transformations in the SVG output.- Returns:
- The double-to-string function (never
null). - Since:
- 5.0
-
setTransformDoubleConverter
public void setTransformDoubleConverter(java.util.function.DoubleFunction<java.lang.String> converter)
Sets the double-to-string function that is used when writing coordinates for matrix transformations in the SVG output. The default converter optimises for speed when generating the SVG and should cover normal usage. However this method provides the ability to substitute an alternative function (for example, one that favours output size over speed of generation).- Parameters:
converter- the convertor function (nullnot permitted).- Since:
- 5.0
- See Also:
setGeomDoubleConverter(java.util.function.DoubleFunction)
-
getFilePrefix
public java.lang.String getFilePrefix()
Returns the prefix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.- Returns:
- The file prefix (never
null). - Since:
- 1.5
-
setFilePrefix
public void setFilePrefix(java.lang.String prefix)
Sets the prefix used to generate a filename for any image that is referenced from the SVG element.- Parameters:
prefix- the new prefix (nullnot permitted).- Since:
- 1.5
-
getFileSuffix
public java.lang.String getFileSuffix()
Returns the suffix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.- Returns:
- The file suffix (never
null). - Since:
- 1.5
-
setFileSuffix
public void setFileSuffix(java.lang.String suffix)
Sets the suffix used to generate a filename for any image that is referenced from the SVG element.- Parameters:
suffix- the new prefix (nullnot permitted).- Since:
- 1.5
-
getZeroStrokeWidth
public double getZeroStrokeWidth()
Returns the width to use for the SVG stroke when the AWT stroke specified has a zero width (the default value is0.1). In the Java specification forBasicStrokeit states "If width is set to 0.0f, the stroke is rendered as the thinnest possible line for the target device and the antialias hint setting." We don't have a means to implement that accurately since we must specify a fixed width.- Returns:
- The width.
- Since:
- 1.9
-
setZeroStrokeWidth
public void setZeroStrokeWidth(double width)
Sets the width to use for the SVG stroke when the current AWT stroke has a width of 0.0.- Parameters:
width- the new width (must be 0 or greater).- Since:
- 1.9
-
getDeviceConfiguration
public java.awt.GraphicsConfiguration getDeviceConfiguration()
Returns the device configuration associated with thisGraphics2D.- Specified by:
getDeviceConfigurationin classjava.awt.Graphics2D- Returns:
- The graphics configuration.
-
create
public java.awt.Graphics create()
Creates a new graphics object that is a copy of this graphics object (except that it has not accumulated the drawing operations). Not sure yet when or why this would be useful when creating SVG output. Note that thefontFunctionobject (getFontFunction()) is shared between the existing instance and the new one.- Specified by:
createin classjava.awt.Graphics- Returns:
- A new graphics object.
-
getPaint
public java.awt.Paint getPaint()
Returns the paint used to draw or fill shapes (or text). The default value isColor.BLACK.- Specified by:
getPaintin classjava.awt.Graphics2D- Returns:
- The paint (never
null). - See Also:
setPaint(java.awt.Paint)
-
setPaint
public void setPaint(java.awt.Paint paint)
Sets the paint used to draw or fill shapes (or text). Ifpaintis an instance ofColor, this method will also update the current color attribute (seegetColor()). If you passnullto this method, it does nothing (in accordance with the JDK specification).- Specified by:
setPaintin classjava.awt.Graphics2D- Parameters:
paint- the paint (nullis permitted but ignored).- See Also:
getPaint()
-
getColor
public java.awt.Color getColor()
Returns the foreground color. This method exists for backwards compatibility in AWT, you should use thegetPaint()method.- Specified by:
getColorin classjava.awt.Graphics- Returns:
- The foreground color (never
null). - See Also:
getPaint()
-
setColor
public void setColor(java.awt.Color c)
Sets the foreground color. This method exists for backwards compatibility in AWT, you should use thesetPaint(java.awt.Paint)method.- Specified by:
setColorin classjava.awt.Graphics- Parameters:
c- the color (nullpermitted but ignored).- See Also:
setPaint(java.awt.Paint)
-
getBackground
public java.awt.Color getBackground()
Returns the background color. The default value isColor.BLACK. This is used by theclearRect(int, int, int, int)method.- Specified by:
getBackgroundin classjava.awt.Graphics2D- Returns:
- The background color (possibly
null). - See Also:
setBackground(java.awt.Color)
-
setBackground
public void setBackground(java.awt.Color color)
Sets the background color. This is used by theclearRect(int, int, int, int)method. The reference implementation allowsnullfor the background color, so we allow that too (but for that case, the clearRect method will do nothing).- Specified by:
setBackgroundin classjava.awt.Graphics2D- Parameters:
color- the color (nullpermitted).- See Also:
getBackground()
-
getComposite
public java.awt.Composite getComposite()
Returns the current composite.- Specified by:
getCompositein classjava.awt.Graphics2D- Returns:
- The current composite (never
null). - See Also:
setComposite(java.awt.Composite)
-
setComposite
public void setComposite(java.awt.Composite comp)
Sets the composite (onlyAlphaCompositeis handled).- Specified by:
setCompositein classjava.awt.Graphics2D- Parameters:
comp- the composite (nullnot permitted).- See Also:
getComposite()
-
getStroke
public java.awt.Stroke getStroke()
Returns the current stroke (used when drawing shapes).- Specified by:
getStrokein classjava.awt.Graphics2D- Returns:
- The current stroke (never
null). - See Also:
setStroke(java.awt.Stroke)
-
setStroke
public void setStroke(java.awt.Stroke s)
Sets the stroke that will be used to draw shapes.- Specified by:
setStrokein classjava.awt.Graphics2D- Parameters:
s- the stroke (nullnot permitted).- See Also:
getStroke()
-
getRenderingHint
public java.lang.Object getRenderingHint(java.awt.RenderingHints.Key hintKey)
Returns the current value for the specified hint. See theSVGHintsclass for information about the hints that can be used withSVGGraphics2D.- Specified by:
getRenderingHintin classjava.awt.Graphics2D- Parameters:
hintKey- the hint key (nullpermitted, but the result will benullalso).- Returns:
- The current value for the specified hint
(possibly
null). - See Also:
setRenderingHint(java.awt.RenderingHints.Key, java.lang.Object)
-
setRenderingHint
public void setRenderingHint(java.awt.RenderingHints.Key hintKey, java.lang.Object hintValue)Sets the value for a hint. See theSVGHintsclass for information about the hints that can be used with this implementation.- Specified by:
setRenderingHintin classjava.awt.Graphics2D- Parameters:
hintKey- the hint key (nullnot permitted).hintValue- the hint value.- See Also:
getRenderingHint(java.awt.RenderingHints.Key)
-
getRenderingHints
public java.awt.RenderingHints getRenderingHints()
Returns a copy of the rendering hints. Modifying the returned copy will have no impact on the state of thisGraphics2Dinstance.- Specified by:
getRenderingHintsin classjava.awt.Graphics2D- Returns:
- The rendering hints (never
null). - See Also:
setRenderingHints(java.util.Map)
-
setRenderingHints
public void setRenderingHints(java.util.Map<?,?> hints)
Sets the rendering hints to the specified collection.- Specified by:
setRenderingHintsin classjava.awt.Graphics2D- Parameters:
hints- the new set of hints (nullnot permitted).- See Also:
getRenderingHints()
-
addRenderingHints
public void addRenderingHints(java.util.Map<?,?> hints)
Adds all the supplied rendering hints.- Specified by:
addRenderingHintsin classjava.awt.Graphics2D- Parameters:
hints- the hints (nullnot permitted).
-
appendOptionalElementIDFromHint
private void appendOptionalElementIDFromHint(java.lang.StringBuilder builder)
A utility method that appends an optional element id if one is specified via the rendering hints.- Parameters:
builder- the string builder (nullnot permitted).
-
draw
public void draw(java.awt.Shape s)
Draws the specified shape with the currentpaintandstroke. There is direct handling forLine2D,Rectangle2D,Ellipse2DandPath2D. All other shapes are mapped to aGeneralPathand then drawn (effectively asPath2Dobjects).- Specified by:
drawin classjava.awt.Graphics2D- Parameters:
s- the shape (nullnot permitted).- See Also:
fill(java.awt.Shape)
-
fill
public void fill(java.awt.Shape s)
Fills the specified shape with the currentpaint. There is direct handling forRectangle2D,Ellipse2DandPath2D. All other shapes are mapped to aGeneralPathand then filled.- Specified by:
fillin classjava.awt.Graphics2D- Parameters:
s- the shape (nullnot permitted).- See Also:
draw(java.awt.Shape)
-
getSVGPathData
private java.lang.String getSVGPathData(java.awt.geom.Path2D path)
Creates an SVG path string for the supplied Java2D path.- Parameters:
path- the path (nullnot permitted).- Returns:
- An SVG path string.
-
getAlpha
private float getAlpha()
Returns the current alpha (transparency) in the range 0.0 to 1.0. If the current composite is anAlphaCompositewe read the alpha value from there, otherwise this method returns 1.0.- Returns:
- The current alpha (transparency) in the range 0.0 to 1.0.
-
svgColorStr
private java.lang.String svgColorStr()
Returns an SVG color string based on the current paint. To handleGradientPaintwe rely on thesetPaint()method having set thegradientPaintRefattribute.- Returns:
- An SVG color string.
-
rgbColorStr
private java.lang.String rgbColorStr(java.awt.Color c)
Returns the SVG RGB color string for the specified color.- Parameters:
c- the color (nullnot permitted).- Returns:
- The SVG RGB color string.
-
rgbaColorStr
private java.lang.String rgbaColorStr(java.awt.Color c)
Returns a string representing the specified color in RGBA format.- Parameters:
c- the color (nullnot permitted).- Returns:
- The SVG RGBA color string.
-
strokeStyle
private java.lang.String strokeStyle()
Returns a stroke style string based on the current stroke and alpha settings. Implementation note: the last attribute in the string will not have a semicolon after it.- Returns:
- A stroke style string.
-
getColorAlpha
private float getColorAlpha()
Returns the alpha value of the currentpaint, or1.0fif it is not an instance ofColor.- Returns:
- The alpha value (in the range
0.0to1.0).
-
getSVGFillStyle
private java.lang.String getSVGFillStyle()
Returns a fill style string based on the current paint and alpha settings.- Returns:
- A fill style string.
-
getFont
public java.awt.Font getFont()
Returns the current font used for drawing text.- Specified by:
getFontin classjava.awt.Graphics- Returns:
- The current font (never
null). - See Also:
setFont(java.awt.Font)
-
setFont
public void setFont(java.awt.Font font)
Sets the font to be used for drawing text.- Specified by:
setFontin classjava.awt.Graphics- Parameters:
font- the font (nullis permitted but ignored).- See Also:
getFont()
-
getFontFunction
public java.util.function.Function<java.lang.String,java.lang.String> getFontFunction()
Returns the function that generates SVG font references from a supplied Java font family name. The default function will convert Java logical font names to the equivalent SVG generic font name, pass-through all other font names unchanged, and surround the result in single quotes.- Returns:
- The font mapper (never
null). - Since:
- 5.0
- See Also:
setFontFunction(java.util.function.Function)
-
setFontFunction
public void setFontFunction(java.util.function.Function<java.lang.String,java.lang.String> fontFunction)
Sets the font function that is used to generate SVG font references from Java font family names.- Parameters:
fontFunction- the font mapper (nullnot permitted).- Since:
- 5.0
-
getFontSizeUnits
public SVGUnits getFontSizeUnits()
Returns the font size units. The default value isSVGUnits.PX.- Returns:
- The font size units.
- Since:
- 3.4
-
setFontSizeUnits
public void setFontSizeUnits(SVGUnits fontSizeUnits)
Sets the font size units. In general, if this method is used it should be called immediately after theSVGGraphics2Dinstance is created and before any content is generated.- Parameters:
fontSizeUnits- the font size units (nullnot permitted).- Since:
- 3.4
-
getSVGFontStyle
private java.lang.String getSVGFontStyle()
Returns a string containing font style info.- Returns:
- A string containing font style info.
-
getFontMetrics
public java.awt.FontMetrics getFontMetrics(java.awt.Font f)
Returns the font metrics for the specified font.- Specified by:
getFontMetricsin classjava.awt.Graphics- Parameters:
f- the font.- Returns:
- The font metrics.
-
getFontRenderContext
public java.awt.font.FontRenderContext getFontRenderContext()
Returns the font render context.- Specified by:
getFontRenderContextin classjava.awt.Graphics2D- Returns:
- The font render context (never
null).
-
drawString
public void drawString(java.lang.String str, int x, int y)Draws a string at(x, y). The start of the text at the baseline level will be aligned with the(x, y)point.
Note that you can make use of theSVGHints.KEY_TEXT_RENDERINGhint when drawing strings (this is completely optional though).- Specified by:
drawStringin classjava.awt.Graphics2D- Parameters:
str- the string (nullnot permitted).x- the x-coordinate.y- the y-coordinate.- See Also:
drawString(java.lang.String, float, float)
-
drawString
public void drawString(java.lang.String str, float x, float y)Draws a string at(x, y). The start of the text at the baseline level will be aligned with the(x, y)point.
Note that you can make use of theSVGHints.KEY_TEXT_RENDERINGhint when drawing strings (this is completely optional though).- Specified by:
drawStringin classjava.awt.Graphics2D- Parameters:
str- the string (nullnot permitted).x- the x-coordinate.y- the y-coordinate.
-
drawString
public void drawString(java.text.AttributedCharacterIterator iterator, int x, int y)Draws a string of attributed characters at(x, y). The call is delegated todrawString(AttributedCharacterIterator, float, float).- Specified by:
drawStringin classjava.awt.Graphics2D- Parameters:
iterator- an iterator for the characters.x- the x-coordinate.y- the x-coordinate.
-
drawString
public void drawString(java.text.AttributedCharacterIterator iterator, float x, float y)Draws a string of attributed characters at(x, y).- Specified by:
drawStringin classjava.awt.Graphics2D- Parameters:
iterator- an iterator over the characters (nullnot permitted).x- the x-coordinate.y- the y-coordinate.
-
drawGlyphVector
public void drawGlyphVector(java.awt.font.GlyphVector g, float x, float y)Draws the specified glyph vector at the location(x, y).- Specified by:
drawGlyphVectorin classjava.awt.Graphics2D- Parameters:
g- the glyph vector (nullnot permitted).x- the x-coordinate.y- the y-coordinate.
-
translate
public void translate(int tx, int ty)Applies the translation(tx, ty). This call is delegated totranslate(double, double).- Specified by:
translatein classjava.awt.Graphics2D- Parameters:
tx- the x-translation.ty- the y-translation.- See Also:
translate(double, double)
-
translate
public void translate(double tx, double ty)Applies the translation(tx, ty).- Specified by:
translatein classjava.awt.Graphics2D- Parameters:
tx- the x-translation.ty- the y-translation.
-
rotate
public void rotate(double theta)
Applies a rotation (anti-clockwise) about(0, 0).- Specified by:
rotatein classjava.awt.Graphics2D- Parameters:
theta- the rotation angle (in radians).
-
rotate
public void rotate(double theta, double x, double y)Applies a rotation (anti-clockwise) about(x, y).- Specified by:
rotatein classjava.awt.Graphics2D- Parameters:
theta- the rotation angle (in radians).x- the x-coordinate.y- the y-coordinate.
-
scale
public void scale(double sx, double sy)Applies a scale transformation.- Specified by:
scalein classjava.awt.Graphics2D- Parameters:
sx- the x-scaling factor.sy- the y-scaling factor.
-
shear
public void shear(double shx, double shy)Applies a shear transformation. This is equivalent to the following call to thetransformmethod:
-
transform(AffineTransform.getShearInstance(shx, shy));
- Specified by:
shearin classjava.awt.Graphics2D- Parameters:
shx- the x-shear factor.shy- the y-shear factor.
-
-
transform
public void transform(java.awt.geom.AffineTransform t)
Applies this transform to the existing transform by concatenating it.- Specified by:
transformin classjava.awt.Graphics2D- Parameters:
t- the transform (nullnot permitted).
-
getTransform
public java.awt.geom.AffineTransform getTransform()
Returns a copy of the current transform.- Specified by:
getTransformin classjava.awt.Graphics2D- Returns:
- A copy of the current transform (never
null). - See Also:
setTransform(java.awt.geom.AffineTransform)
-
setTransform
public void setTransform(java.awt.geom.AffineTransform t)
Sets the transform.- Specified by:
setTransformin classjava.awt.Graphics2D- Parameters:
t- the new transform (nullpermitted, resets to the identity transform).- See Also:
getTransform()
-
hit
public boolean hit(java.awt.Rectangle rect, java.awt.Shape s, boolean onStroke)Returnstrueif the rectangle (in device space) intersects with the shape (the interior, ifonStrokeisfalse, otherwise the stroked outline of the shape).- Specified by:
hitin classjava.awt.Graphics2D- Parameters:
rect- a rectangle (in device space).s- the shape.onStroke- test the stroked outline only?- Returns:
- A boolean.
-
setPaintMode
public void setPaintMode()
Does nothing in thisSVGGraphics2Dimplementation.- Specified by:
setPaintModein classjava.awt.Graphics
-
setXORMode
public void setXORMode(java.awt.Color c)
Does nothing in thisSVGGraphics2Dimplementation.- Specified by:
setXORModein classjava.awt.Graphics- Parameters:
c- ignored
-
getClipBounds
public java.awt.Rectangle getClipBounds()
Returns the bounds of the user clipping region.- Specified by:
getClipBoundsin classjava.awt.Graphics- Returns:
- The clip bounds (possibly
null). - See Also:
getClip()
-
getClip
public java.awt.Shape getClip()
Returns the user clipping region. The initial default value isnull.- Specified by:
getClipin classjava.awt.Graphics- Returns:
- The user clipping region (possibly
null). - See Also:
setClip(java.awt.Shape)
-
setClip
public void setClip(java.awt.Shape shape)
Sets the user clipping region.- Specified by:
setClipin classjava.awt.Graphics- Parameters:
shape- the new user clipping region (nullpermitted).- See Also:
getClip()
-
registerClip
private java.lang.String registerClip(java.awt.Shape clip)
Registers the clip so that we can later write out all the clip definitions in the DEFS element.- Parameters:
clip- the clip (ignored ifnull)
-
transformDP
private java.lang.String transformDP(double d)
Returns a string representation of the specified number for use in the SVG output.- Parameters:
d- the number.- Returns:
- A string representation of the number.
-
geomDP
private java.lang.String geomDP(double d)
Returns a string representation of the specified number for use in the SVG output.- Parameters:
d- the number.- Returns:
- A string representation of the number.
-
getSVGTransform
private java.lang.String getSVGTransform(java.awt.geom.AffineTransform t)
-
clip
public void clip(java.awt.Shape s)
Clips to the intersection of the current clipping region and the specified shape. According to the Oracle API specification, this method will accept anullargument, however there is a bug report (opened in 2004 and fixed in 2021) that describes the passing ofnullas "not recommended":- Specified by:
clipin classjava.awt.Graphics2D- Parameters:
s- the clip shape (nullnot recommended).
-
clipRect
public void clipRect(int x, int y, int width, int height)Clips to the intersection of the current clipping region and the specified rectangle.- Specified by:
clipRectin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.
-
setClip
public void setClip(int x, int y, int width, int height)Sets the user clipping region to the specified rectangle.- Specified by:
setClipin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.- See Also:
getClip()
-
drawLine
public void drawLine(int x1, int y1, int x2, int y2)Draws a line from(x1, y1)to(x2, y2)using the currentpaintandstroke.- Specified by:
drawLinein classjava.awt.Graphics- Parameters:
x1- the x-coordinate of the start point.y1- the y-coordinate of the start point.x2- the x-coordinate of the end point.y2- the x-coordinate of the end point.
-
fillRect
public void fillRect(int x, int y, int width, int height)Fills the specified rectangle with the currentpaint.- Specified by:
fillRectin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the rectangle width.height- the rectangle height.
-
clearRect
public void clearRect(int x, int y, int width, int height)Clears the specified rectangle by filling it with the current background color. If the background color isnull, this method will do nothing.- Specified by:
clearRectin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.- See Also:
getBackground()
-
drawRoundRect
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Draws a rectangle with rounded corners using the currentpaintandstroke.- Specified by:
drawRoundRectin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.arcWidth- the arc-width.arcHeight- the arc-height.- See Also:
fillRoundRect(int, int, int, int, int, int)
-
fillRoundRect
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Fills a rectangle with rounded corners using the currentpaint.- Specified by:
fillRoundRectin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.arcWidth- the arc-width.arcHeight- the arc-height.- See Also:
drawRoundRect(int, int, int, int, int, int)
-
drawOval
public void drawOval(int x, int y, int width, int height)Draws an oval framed by the rectangle(x, y, width, height)using the currentpaintandstroke.- Specified by:
drawOvalin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.- See Also:
fillOval(int, int, int, int)
-
fillOval
public void fillOval(int x, int y, int width, int height)Fills an oval framed by the rectangle(x, y, width, height).- Specified by:
fillOvalin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.- See Also:
drawOval(int, int, int, int)
-
drawArc
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)Draws an arc contained within the rectangle(x, y, width, height), starting atstartAngleand continuing througharcAngledegrees using the currentpaintandstroke.- Specified by:
drawArcin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.startAngle- the start angle in degrees, 0 = 3 o'clock.arcAngle- the angle (anticlockwise) in degrees.- See Also:
fillArc(int, int, int, int, int, int)
-
fillArc
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)Fills an arc contained within the rectangle(x, y, width, height), starting atstartAngleand continuing througharcAngledegrees, using the currentpaint.- Specified by:
fillArcin classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.startAngle- the start angle in degrees, 0 = 3 o'clock.arcAngle- the angle (anticlockwise) in degrees.- See Also:
drawArc(int, int, int, int, int, int)
-
drawPolyline
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)Draws the specified multi-segment line using the currentpaintandstroke.- Specified by:
drawPolylinein classjava.awt.Graphics- Parameters:
xPoints- the x-points.yPoints- the y-points.nPoints- the number of points to use for the polyline.
-
drawPolygon
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)Draws the specified polygon using the currentpaintandstroke.- Specified by:
drawPolygonin classjava.awt.Graphics- Parameters:
xPoints- the x-points.yPoints- the y-points.nPoints- the number of points to use for the polygon.- See Also:
fillPolygon(int[], int[], int)
-
fillPolygon
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)Fills the specified polygon using the currentpaint.- Specified by:
fillPolygonin classjava.awt.Graphics- Parameters:
xPoints- the x-points.yPoints- the y-points.nPoints- the number of points to use for the polygon.- See Also:
drawPolygon(int[], int[], int)
-
getPNGBytes
private byte[] getPNGBytes(java.awt.Image img)
Returns the bytes representing a PNG format image.- Parameters:
img- the image to encode (nullnot permitted).- Returns:
- The bytes representing a PNG format image.
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)Draws an image at the location(x, y). Note that theobserveris ignored.- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the image (nullpermitted...method will do nothing).x- the x-coordinate.y- the y-coordinate.observer- ignored.- Returns:
trueif there is no more drawing to be done.
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, int w, int h, java.awt.image.ImageObserver observer)Draws the image into the rectangle defined by(x, y, w, h). Note that theobserveris ignored (it is not useful in this context).- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the image (nullpermitted...draws nothing).x- the x-coordinate.y- the y-coordinate.w- the width.h- the height.observer- ignored.- Returns:
trueif there is no more drawing to be done.
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws an image at the location(x, y). Note that theobserveris ignored.- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the image (nullpermitted...draws nothing).x- the x-coordinate.y- the y-coordinate.bgcolor- the background color (nullpermitted).observer- ignored.- Returns:
trueif there is no more drawing to be done.
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, int w, int h, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws an image to the rectangle(x, y, w, h)(scaling it if required), first filling the background with the specified color. Note that theobserveris ignored.- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the image.x- the x-coordinate.y- the y-coordinate.w- the width.h- the height.bgcolor- the background color (nullpermitted).observer- ignored.- Returns:
trueif the image is drawn.
-
drawImage
public boolean drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver observer)Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)) into the destination rectangle(dx1, dy1, dx2, dy2). Note that theobserveris ignored.- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the image.dx1- the x-coordinate for the top left of the destination.dy1- the y-coordinate for the top left of the destination.dx2- the x-coordinate for the bottom right of the destination.dy2- the y-coordinate for the bottom right of the destination.sx1- the x-coordinate for the top left of the source.sy1- the y-coordinate for the top left of the source.sx2- the x-coordinate for the bottom right of the source.sy2- the y-coordinate for the bottom right of the source.- Returns:
trueif the image is drawn.
-
drawImage
public boolean drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)) into the destination rectangle(dx1, dy1, dx2, dy2). The destination rectangle is first cleared by filling it with the specifiedbgcolor. Note that theobserveris ignored.- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the image.dx1- the x-coordinate for the top left of the destination.dy1- the y-coordinate for the top left of the destination.dx2- the x-coordinate for the bottom right of the destination.dy2- the y-coordinate for the bottom right of the destination.sx1- the x-coordinate for the top left of the source.sy1- the y-coordinate for the top left of the source.sx2- the x-coordinate for the bottom right of the source.sy2- the y-coordinate for the bottom right of the source.bgcolor- the background color (nullpermitted).observer- ignored.- Returns:
trueif the image is drawn.
-
drawRenderedImage
public void drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)Draws the rendered image. Ifimgisnullthis method does nothing.- Specified by:
drawRenderedImagein classjava.awt.Graphics2D- Parameters:
img- the image (nullpermitted).xform- the transform.
-
drawRenderableImage
public void drawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)Draws the renderable image.- Specified by:
drawRenderableImagein classjava.awt.Graphics2D- Parameters:
img- the renderable image.xform- the transform.
-
drawImage
public boolean drawImage(java.awt.Image img, java.awt.geom.AffineTransform xform, java.awt.image.ImageObserver obs)Draws an image with the specified transform. Note that theobserveris ignored.- Specified by:
drawImagein classjava.awt.Graphics2D- Parameters:
img- the image.xform- the transform (nullpermitted).obs- the image observer (ignored).- Returns:
trueif the image is drawn.
-
drawImage
public void drawImage(java.awt.image.BufferedImage img, java.awt.image.BufferedImageOp op, int x, int y)Draws the image resulting from applying theBufferedImageOpto the specified image at the location(x, y).- Specified by:
drawImagein classjava.awt.Graphics2D- Parameters:
img- the image.op- the operation (nullpermitted).x- the x-coordinate.y- the y-coordinate.
-
copyArea
public void copyArea(int x, int y, int width, int height, int dx, int dy)This method does nothing. The operation assumes that the output is in bitmap form, which is not the case for SVG, so we silently ignore this method call.- Specified by:
copyAreain classjava.awt.Graphics- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width of the area.height- the height of the area.dx- the delta x.dy- the delta y.
-
dispose
public void dispose()
This method does nothing, there are no resources to dispose.- Specified by:
disposein classjava.awt.Graphics
-
getSVGElement
public java.lang.String getSVGElement()
Returns the SVG element that has been generated by calls to thisGraphics2Dimplementation.- Returns:
- The SVG element.
-
getSVGElement
public java.lang.String getSVGElement(java.lang.String id)
Returns the SVG element that has been generated by calls to thisGraphics2Dimplementation, giving it the specifiedid. Ifidisnull, the element will have noidattribute.- Parameters:
id- the element id (nullpermitted).- Returns:
- A string containing the SVG element.
- Since:
- 1.8
-
getSVGElement
public java.lang.String getSVGElement(java.lang.String id, boolean includeDimensions, ViewBox viewBox, PreserveAspectRatio preserveAspectRatio, MeetOrSlice meetOrSlice)Returns the SVG element that has been generated by calls to thisGraphics2Dimplementation, giving it the specifiedid. Ifidisnull, the element will have noidattribute. This method also allows for aviewBoxto be defined, along with the settings that handle scaling.- Parameters:
id- the element id (nullpermitted).includeDimensions- include the width and height attributes?viewBox- the view box specification (ifnullthen noviewBoxattribute will be defined).preserveAspectRatio- the value of thepreserveAspectRatioattribute (ifnullthen not attribute will be defined).meetOrSlice- the value of the meetOrSlice attribute.- Returns:
- A string containing the SVG element.
- Since:
- 3.2
-
isDefsOutputRequired
private boolean isDefsOutputRequired()
Returnstrueif there are items that need to be written to the DEFS element, andfalseotherwise.- Returns:
- A boolean.
-
getSVGDocument
public java.lang.String getSVGDocument()
Returns an SVG document (this contains the content returned by thegetSVGElement()method, prepended with the required document header).- Returns:
- An SVG document.
-
getSVGImages
public java.util.List<ImageElement> getSVGImages()
Returns the list of image elements that have been referenced in the SVG output but not embedded. If the image files don't already exist, you can use this list as the basis for creating the image files.- Returns:
- The list of image elements.
- See Also:
SVGHints.KEY_IMAGE_HANDLING
-
getElementIDs
public java.util.Set<java.lang.String> getElementIDs()
Returns a new set containing the element IDs that have been used in output so far.- Returns:
- The element IDs.
- Since:
- 1.5
-
getLinearGradientElement
private java.lang.String getLinearGradientElement(java.lang.String id, java.awt.GradientPaint paint)Returns an element to represent a linear gradient. All the linear gradients that are used get written to the DEFS element in the SVG.- Parameters:
id- the reference id.paint- the gradient.- Returns:
- The SVG element.
-
getLinearGradientElement
private java.lang.String getLinearGradientElement(java.lang.String id, java.awt.LinearGradientPaint paint)Returns an element to represent a linear gradient. All the linear gradients that are used get written to the DEFS element in the SVG.- Parameters:
id- the reference id.paint- the gradient.- Returns:
- The SVG element.
-
getRadialGradientElement
private java.lang.String getRadialGradientElement(java.lang.String id, java.awt.RadialGradientPaint rgp)Returns an element to represent a radial gradient. All the radial gradients that are used get written to the DEFS element in the SVG.- Parameters:
id- the reference id.rgp- the radial gradient.- Returns:
- The SVG element.
-
getClipPathRef
private java.lang.String getClipPathRef()
Returns a clip path reference for the current user clip. This is written out on all SVG elements that draw or fill shapes or text.- Returns:
- A clip path reference.
-
setRect
private void setRect(int x, int y, int width, int height)Sets the attributes of the reusableRectangle2Dobject that is used by theGraphics.drawRect(int, int, int, int)andfillRect(int, int, int, int)methods.- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.
-
setRoundRect
private void setRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Sets the attributes of the reusableRoundRectangle2Dobject that is used by thedrawRoundRect(int, int, int, int, int, int)andfillRoundRect(int, int, int, int, int, int)methods.- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.arcWidth- the arc width.arcHeight- the arc height.
-
setOval
private void setOval(int x, int y, int width, int height)Sets the attributes of the reusableEllipse2Dobject that is used by thedrawOval(int, int, int, int)andfillOval(int, int, int, int)methods.- Parameters:
x- the x-coordinate.y- the y-coordinate.width- the width.height- the height.
-
-