Package com.itextpdf.svg.renderers
Class SvgDrawContext
- java.lang.Object
-
- com.itextpdf.svg.renderers.SvgDrawContext
-
public class SvgDrawContext extends java.lang.Object
The SvgDrawContext keeps a stack ofPdfCanvas
instances, which represent all levels of XObjects that are added to the root canvas.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Deque<PdfCanvas>
canvases
private AffineTransform
clippingElementTransform
private SvgCssContext
cssContext
private Rectangle
customViewport
private FontProvider
fontProvider
private java.util.Map<java.lang.String,ISvgNodeRenderer>
namedObjects
private java.util.Stack<java.lang.String>
patternIds
private float[]
relativePosition
private ResourceResolver
resourceResolver
private AffineTransform
rootTransform
private FontSet
tempFonts
private float[]
textMove
private SvgTextProperties
textProperties
private java.util.Stack<java.lang.String>
useIds
private java.util.Deque<Rectangle>
viewports
-
Constructor Summary
Constructors Constructor Description SvgDrawContext(ResourceResolver resourceResolver, FontProvider fontProvider)
Create an instance of the context that is used to store information when converting SVG.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addNamedObject(java.lang.String name, ISvgNodeRenderer namedObject)
Adds a named object to the draw context.void
addNamedObjects(java.util.Map<java.lang.String,ISvgNodeRenderer> namedObjects)
* Adds a number of named object to the draw context.void
addTextMove(float additionalMoveX, float additionalMoveY)
Increment the stored text move.void
addUsedId(java.lang.String elementId)
Adds an ID that has been referenced by a use element.void
addViewPort(Rectangle viewPort)
Adds a viewbox to the context.AffineTransform
getClippingElementTransform()
Gets clipping element transformation matrix.AffineTransform
getConcatenatedTransform()
Concatenates all transformations applied from the top level of the svg to the current one.SvgCssContext
getCssContext()
Gets the SVG CSS context.PdfCanvas
getCurrentCanvas()
Retrieves the current top of the stack, without modifying the stack.AffineTransform
getCurrentCanvasTransform()
Get the current canvas transformation.Rectangle
getCurrentViewPort()
Get the current viewbox.Rectangle
getCustomViewport()
Gets the custom viewport of SVG.FontProvider
getFontProvider()
Gets the FontProvider to be used during the drawing operations.AffineTransform
getLastTextTransform()
Deprecated.in favour ofgetRootTransform()
ISvgNodeRenderer
getNamedObject(java.lang.String name)
Get a named object based on its name.float[]
getPreviousElementTextMove()
Deprecated.was replaced bygetSvgTextProperties()
float[]
getRelativePosition()
Retrieves relative position for the current text SVG element relative to the last origin identified by absolute position.ResourceResolver
getResourceResolver()
Gets the ResourceResolver to be used during the drawing operations.AffineTransform
getRootTransform()
Get the current root transformation that was last applied.Rectangle
getRootViewPort()
Get the viewbox which is the root viewport for the current document.SvgTextProperties
getSvgTextProperties()
RetrievesSvgTextProperties
for text SVG elements.FontSet
getTempFonts()
Gets list of temporary fonts from @font-face.float[]
getTextMove()
Get the stored current text move.boolean
isIdUsedByUseTagBefore(java.lang.String elementId)
Returns true when this id has been used beforevoid
moveRelativePosition(float dx, float dy)
Adds move to the current relative position for the text SVG element.PdfCanvas
popCanvas()
Retrieves the current top of the stack, thereby taking the current item off the stack.void
popPatternId()
Pops the last template id from the stack.void
pushCanvas(PdfCanvas canvas)
Adds aPdfCanvas
to the stack (by definition its top), for use in drawing operations.boolean
pushPatternId(java.lang.String patternId)
Add pattern id to stack.void
removeCurrentViewPort()
Remove the currently set view box.void
removeUsedId(java.lang.String elementId)
Removes an ID that has been referenced by a use element.void
resetClippingElementTransform()
Resets clipping element transformation matrix.void
resetRelativePosition()
Resets current relative position for the text SVG element.void
resetTextMove()
Reset the stored text move to [0f,0f]void
setCssContext(SvgCssContext cssContext)
Sets the SVG CSS context.void
setCustomViewport(Rectangle customViewport)
Sets the custom viewport of SVG.void
setLastTextTransform(AffineTransform newTransform)
Deprecated.in favour ofsetRootTransform(AffineTransform)
void
setPreviousElementTextMove(float[] previousElementTextMove)
Deprecated.was replaced bysetSvgTextProperties(SvgTextProperties)
void
setRootTransform(AffineTransform newTransform)
Set the current root transformation.void
setSvgTextProperties(SvgTextProperties textProperties)
SetsSvgTextProperties
for textSVG elements.void
setTempFonts(FontSet tempFonts)
Sets the FontSet.int
size()
Get the current size of the stack, signifying the nesting level of the XObjects.
-
-
-
Field Detail
-
namedObjects
private final java.util.Map<java.lang.String,ISvgNodeRenderer> namedObjects
-
canvases
private final java.util.Deque<PdfCanvas> canvases
-
viewports
private final java.util.Deque<Rectangle> viewports
-
useIds
private final java.util.Stack<java.lang.String> useIds
-
patternIds
private final java.util.Stack<java.lang.String> patternIds
-
resourceResolver
private final ResourceResolver resourceResolver
-
fontProvider
private final FontProvider fontProvider
-
textProperties
private SvgTextProperties textProperties
-
tempFonts
private FontSet tempFonts
-
cssContext
private SvgCssContext cssContext
-
rootTransform
private AffineTransform rootTransform
-
clippingElementTransform
private AffineTransform clippingElementTransform
-
textMove
private float[] textMove
-
relativePosition
private float[] relativePosition
-
customViewport
private Rectangle customViewport
-
-
Constructor Detail
-
SvgDrawContext
public SvgDrawContext(ResourceResolver resourceResolver, FontProvider fontProvider)
Create an instance of the context that is used to store information when converting SVG.- Parameters:
resourceResolver
- instance ofResourceResolver
fontProvider
- instance ofFontProvider
-
-
Method Detail
-
getCustomViewport
public Rectangle getCustomViewport()
Gets the custom viewport of SVG.The custom viewport is used to resolve percent values of the top level svg.
- Returns:
- the custom viewport
-
setCustomViewport
public void setCustomViewport(Rectangle customViewport)
Sets the custom viewport of SVG.The custom viewport is used to resolve percent values of the top level svg.
- Parameters:
customViewport
- the custom viewport
-
getCurrentCanvas
public PdfCanvas getCurrentCanvas()
Retrieves the current top of the stack, without modifying the stack.- Returns:
- the current canvas that can be used for drawing operations.
-
popCanvas
public PdfCanvas popCanvas()
Retrieves the current top of the stack, thereby taking the current item off the stack.- Returns:
- the current canvas that can be used for drawing operations.
-
pushCanvas
public void pushCanvas(PdfCanvas canvas)
Adds aPdfCanvas
to the stack (by definition its top), for use in drawing operations.- Parameters:
canvas
- the new top of the stack
-
size
public int size()
Get the current size of the stack, signifying the nesting level of the XObjects.- Returns:
- the current size of the stack.
-
addViewPort
public void addViewPort(Rectangle viewPort)
Adds a viewbox to the context.- Parameters:
viewPort
- rectangle representing the current viewbox
-
getCurrentViewPort
public Rectangle getCurrentViewPort()
Get the current viewbox.- Returns:
- the viewbox as it is currently set
-
getRootViewPort
public Rectangle getRootViewPort()
Get the viewbox which is the root viewport for the current document.- Returns:
- root viewbox.
-
removeCurrentViewPort
public void removeCurrentViewPort()
Remove the currently set view box.
-
addNamedObject
public void addNamedObject(java.lang.String name, ISvgNodeRenderer namedObject)
Adds a named object to the draw context. These objects can then be referenced from a different tag.- Parameters:
name
- name of the objectnamedObject
- object to be referenced
-
getNamedObject
public ISvgNodeRenderer getNamedObject(java.lang.String name)
Get a named object based on its name. If the name isn't listed, this method will return null.- Parameters:
name
- name of the object you want to reference- Returns:
- the referenced object
-
getResourceResolver
public ResourceResolver getResourceResolver()
Gets the ResourceResolver to be used during the drawing operations.- Returns:
- resource resolver instance
-
addNamedObjects
public void addNamedObjects(java.util.Map<java.lang.String,ISvgNodeRenderer> namedObjects)
* Adds a number of named object to the draw context. These objects can then be referenced from a different tag.- Parameters:
namedObjects
- Map containing the named objects keyed to their ID strings
-
getFontProvider
public FontProvider getFontProvider()
Gets the FontProvider to be used during the drawing operations.- Returns:
- font provider instance
-
getTempFonts
public FontSet getTempFonts()
Gets list of temporary fonts from @font-face.- Returns:
- font set instance
-
setTempFonts
public void setTempFonts(FontSet tempFonts)
Sets the FontSet.- Parameters:
tempFonts
- font set to be used during drawing operations
-
isIdUsedByUseTagBefore
public boolean isIdUsedByUseTagBefore(java.lang.String elementId)
Returns true when this id has been used before- Parameters:
elementId
- element id to check- Returns:
- true if id has been encountered before through a use element
-
addUsedId
public void addUsedId(java.lang.String elementId)
Adds an ID that has been referenced by a use element.- Parameters:
elementId
- referenced element ID
-
removeUsedId
public void removeUsedId(java.lang.String elementId)
Removes an ID that has been referenced by a use element.- Parameters:
elementId
- referenced element ID
-
getLastTextTransform
@Deprecated public AffineTransform getLastTextTransform()
Deprecated.in favour ofgetRootTransform()
Get the text transformation that was last applied.- Returns:
AffineTransform
representing the last text transformation
-
setLastTextTransform
@Deprecated public void setLastTextTransform(AffineTransform newTransform)
Deprecated.in favour ofsetRootTransform(AffineTransform)
Set the last text transformation.- Parameters:
newTransform
- last text transformation
-
getRootTransform
public AffineTransform getRootTransform()
Get the current root transformation that was last applied.- Returns:
AffineTransform
representing the root transformation.
-
setRootTransform
public void setRootTransform(AffineTransform newTransform)
Set the current root transformation.- Parameters:
newTransform
- root transformation.
-
getTextMove
public float[] getTextMove()
Get the stored current text move.- Returns:
- [horizontal text move, vertical text move]
-
resetTextMove
public void resetTextMove()
Reset the stored text move to [0f,0f]
-
addTextMove
public void addTextMove(float additionalMoveX, float additionalMoveY)
Increment the stored text move.- Parameters:
additionalMoveX
- horizontal value to addadditionalMoveY
- vertical value to add
-
getCurrentCanvasTransform
public AffineTransform getCurrentCanvasTransform()
Get the current canvas transformation.- Returns:
- the
AffineTransform
representing the current canvas transformation
-
getCssContext
public SvgCssContext getCssContext()
Gets the SVG CSS context.- Returns:
- the SVG CSS context
-
setCssContext
public void setCssContext(SvgCssContext cssContext)
Sets the SVG CSS context.- Parameters:
cssContext
- the SVG CSS context
-
pushPatternId
public boolean pushPatternId(java.lang.String patternId)
Add pattern id to stack. Check if the id is already in the stack. If it is, then returnfalse
and not add, if it is not - add and returntrue
.- Parameters:
patternId
- pattern id- Returns:
true
if pattern id was not on the stack and was pushed;false
if it is on the stack
-
popPatternId
public void popPatternId()
Pops the last template id from the stack.
-
setPreviousElementTextMove
@Deprecated public void setPreviousElementTextMove(float[] previousElementTextMove)
Deprecated.was replaced bysetSvgTextProperties(SvgTextProperties)
Sets a previous element text move.- Parameters:
previousElementTextMove
- previous element text move
-
getPreviousElementTextMove
@Deprecated public float[] getPreviousElementTextMove()
Deprecated.was replaced bygetSvgTextProperties()
Gets a previous element text move.- Returns:
- the previous element text move
-
getSvgTextProperties
public SvgTextProperties getSvgTextProperties()
RetrievesSvgTextProperties
for text SVG elements.- Returns:
SvgTextProperties
text properties
-
setSvgTextProperties
public void setSvgTextProperties(SvgTextProperties textProperties)
SetsSvgTextProperties
for textSVG elements.- Parameters:
textProperties
-SvgTextProperties
to set
-
getRelativePosition
public float[] getRelativePosition()
Retrieves relative position for the current text SVG element relative to the last origin identified by absolute position.- Returns:
- relative position for the current text SVG element
-
moveRelativePosition
public void moveRelativePosition(float dx, float dy)
Adds move to the current relative position for the text SVG element.- Parameters:
dx
- x-axis movementdy
- y-axis movement
-
resetRelativePosition
public void resetRelativePosition()
Resets current relative position for the text SVG element.
-
getClippingElementTransform
public AffineTransform getClippingElementTransform()
Gets clipping element transformation matrix.It is used to preserve clipping element transformation matrix and before drawing clipped element revert canvas transformation matrix into original state. After clipped element will be drawn, clipping element transformation matrix will be used once again to return clipping element matrix for next siblings.
- Returns:
- the current clipping element transformation matrix
-
resetClippingElementTransform
public void resetClippingElementTransform()
Resets clipping element transformation matrix.See
getClippingElementTransform()
for more info about clipping element transformation matrix.
-
getConcatenatedTransform
public AffineTransform getConcatenatedTransform()
Concatenates all transformations applied from the top level of the svg to the current one.- Returns:
AffineTransform
instance
-
-