Package com.itextpdf.svg.css.impl
Class SvgStyleResolver
- java.lang.Object
-
- com.itextpdf.svg.css.impl.SvgStyleResolver
-
- All Implemented Interfaces:
ICssResolver
public class SvgStyleResolver extends java.lang.Object implements ICssResolver
Default implementation of SVG`s styles and attribute resolver .
-
-
Field Summary
Fields Modifier and Type Field Description private CssStyleSheet
css
private static java.lang.String
DEFAULT_CSS_PATH
static float
DEFAULT_FONT_SIZE
private MediaDeviceDescription
deviceDescription
The device description.private static java.lang.String[]
ELEMENTS_INHERITING_PARENT_STYLES
private java.util.List<CssFontFaceRule>
fonts
The list of fonts.static java.util.Set<IStyleInheritance>
INHERITANCE_RULES
private boolean
isFirstSvgElement
private static org.slf4j.Logger
LOGGER
private ResourceResolver
resourceResolver
The resource resolver
-
Constructor Summary
Constructors Constructor Description SvgStyleResolver(INode rootNode, SvgProcessorContext context)
Creates aSvgStyleResolver
.SvgStyleResolver(SvgProcessorContext context)
Creates aSvgStyleResolver
.SvgStyleResolver(java.io.InputStream defaultCssStream, SvgProcessorContext context)
Creates aSvgStyleResolver
with a given default CSS.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
collectCssDeclarations(INode rootNode, ResourceResolver resourceResolver)
private void
collectFonts()
Collects fonts from the style sheet.private void
collectFonts(CssStatement cssStatement)
Collects fonts from aCssStatement
.java.util.List<CssFontFaceRule>
getFonts()
Gets the list of fonts.static boolean
isElementNested(IElementNode element, java.lang.String parentElementNameForSearch)
Checks whether element is nested within the passed parent element.private boolean
isStartedWithHash(java.lang.String s)
Checks if string starts with #.private static boolean
onlyNativeStylesShouldBeResolved(IElementNode element)
private java.util.Map<java.lang.String,java.lang.String>
parseStylesFromStyleAttribute(java.lang.String style)
private void
parseStylesheet(IAttributesContainer attributesNode)
private void
processAttribute(IAttribute attr, java.util.Map<java.lang.String,java.lang.String> styles)
private void
processXLink(IAttribute attr, java.util.Map<java.lang.String,java.lang.String> attributesMap)
Resolves the full path of link href attribute, thanks to the resource resolver.private static void
putMissingVariables(java.util.Map<java.lang.String,java.lang.String> styles, java.util.Map<java.lang.String,java.lang.String> parentStyles)
Merge variables from parent node with current one.static void
resolveFontSizeStyle(java.util.Map<java.lang.String,java.lang.String> styles, SvgCssContext cssContext, java.lang.String parentFontSizeStr)
Resolves the font size stored inside the current element.java.util.Map<java.lang.String,java.lang.String>
resolveNativeStyles(INode node, AbstractCssContext cssContext)
Resolves node styles without inheritance of parent element styles.java.util.Map<java.lang.String,java.lang.String>
resolveStyles(INode element, AbstractCssContext context)
Resolves the styles of a node given the passed context.private java.util.Map<java.lang.String,java.lang.String>
resolveStyles(INode element, SvgCssContext context)
-
-
-
Field Detail
-
INHERITANCE_RULES
public static final java.util.Set<IStyleInheritance> INHERITANCE_RULES
-
DEFAULT_FONT_SIZE
public static final float DEFAULT_FONT_SIZE
-
ELEMENTS_INHERITING_PARENT_STYLES
private static final java.lang.String[] ELEMENTS_INHERITING_PARENT_STYLES
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
css
private CssStyleSheet css
-
DEFAULT_CSS_PATH
private static final java.lang.String DEFAULT_CSS_PATH
- See Also:
- Constant Field Values
-
isFirstSvgElement
private boolean isFirstSvgElement
-
deviceDescription
private MediaDeviceDescription deviceDescription
The device description.
-
fonts
private final java.util.List<CssFontFaceRule> fonts
The list of fonts.
-
resourceResolver
private final ResourceResolver resourceResolver
The resource resolver
-
-
Constructor Detail
-
SvgStyleResolver
public SvgStyleResolver(java.io.InputStream defaultCssStream, SvgProcessorContext context) throws java.io.IOException
Creates aSvgStyleResolver
with a given default CSS.- Parameters:
defaultCssStream
- the default CSScontext
- the processor context- Throws:
java.io.IOException
- if any input/output issue occurs
-
SvgStyleResolver
public SvgStyleResolver(SvgProcessorContext context)
Creates aSvgStyleResolver
.- Parameters:
context
- the processor context
-
SvgStyleResolver
public SvgStyleResolver(INode rootNode, SvgProcessorContext context)
Creates aSvgStyleResolver
. This constructor will instantiate its internal style sheet and it will collect the css declarations from the provided node.- Parameters:
rootNode
- node to collect css fromcontext
- the processor context
-
-
Method Detail
-
resolveFontSizeStyle
public static void resolveFontSizeStyle(java.util.Map<java.lang.String,java.lang.String> styles, SvgCssContext cssContext, java.lang.String parentFontSizeStr)
Resolves the font size stored inside the current element.- Parameters:
styles
- attributes map of the current elementcssContext
-SvgCssContext
instance in order to resolve relative font sizeparentFontSizeStr
- parent font size value
-
isElementNested
public static boolean isElementNested(IElementNode element, java.lang.String parentElementNameForSearch)
Checks whether element is nested within the passed parent element. Nesting is checked at several levels (recursively).- Parameters:
element
-IElementNode
element to checkparentElementNameForSearch
- expected parent element name- Returns:
true
if element is nested within the expected parent,false
otherwise
-
resolveStyles
public java.util.Map<java.lang.String,java.lang.String> resolveStyles(INode element, AbstractCssContext context)
Description copied from interface:ICssResolver
Resolves the styles of a node given the passed context.- Specified by:
resolveStyles
in interfaceICssResolver
- Parameters:
element
- the nodecontext
- the CSS context (RootFontSize, etc.)- Returns:
- the map containing the resolved styles
-
resolveNativeStyles
public java.util.Map<java.lang.String,java.lang.String> resolveNativeStyles(INode node, AbstractCssContext cssContext)
Resolves node styles without inheritance of parent element styles.- Parameters:
node
- the nodecssContext
- the CSS context (RootFontSize, etc.)- Returns:
- the map containing the resolved styles that are defined in the body of the element
-
onlyNativeStylesShouldBeResolved
private static boolean onlyNativeStylesShouldBeResolved(IElementNode element)
-
putMissingVariables
private static void putMissingVariables(java.util.Map<java.lang.String,java.lang.String> styles, java.util.Map<java.lang.String,java.lang.String> parentStyles)
Merge variables from parent node with current one. This is needed for svg elements like <defs>, which do not inherit parent styles, but do inherit css variables.- Parameters:
styles
- current element styles to put variable inparentStyles
- styles to search variables for
-
resolveStyles
private java.util.Map<java.lang.String,java.lang.String> resolveStyles(INode element, SvgCssContext context)
-
processXLink
private void processXLink(IAttribute attr, java.util.Map<java.lang.String,java.lang.String> attributesMap)
Resolves the full path of link href attribute, thanks to the resource resolver.- Parameters:
attr
- the attribute to processattributesMap
- the element styles map
-
isStartedWithHash
private boolean isStartedWithHash(java.lang.String s)
Checks if string starts with #.- Parameters:
s
- the test string- Returns:
- true if the string starts with #, otherwise false
-
collectCssDeclarations
private void collectCssDeclarations(INode rootNode, ResourceResolver resourceResolver)
-
parseStylesheet
private void parseStylesheet(IAttributesContainer attributesNode)
-
getFonts
public java.util.List<CssFontFaceRule> getFonts()
Gets the list of fonts.- Returns:
- the list of
CssFontFaceRule
instances
-
collectFonts
private void collectFonts()
Collects fonts from the style sheet.
-
collectFonts
private void collectFonts(CssStatement cssStatement)
Collects fonts from aCssStatement
.- Parameters:
cssStatement
- the CSS statement
-
processAttribute
private void processAttribute(IAttribute attr, java.util.Map<java.lang.String,java.lang.String> styles)
-
parseStylesFromStyleAttribute
private java.util.Map<java.lang.String,java.lang.String> parseStylesFromStyleAttribute(java.lang.String style)
-
-