/// <summary>
        /// Converts a the HTML styles of an element to a list of
        /// <see cref="iText.StyledXmlParser.Css.CssDeclaration"/>
        /// instances.
        /// </summary>
        /// <param name="element">the element</param>
        /// <returns>
        /// the resulting list of
        /// <see cref="iText.StyledXmlParser.Css.CssDeclaration"/>
        /// instances.
        /// </returns>
        public static IList <CssDeclaration> Convert(IElementNode element)
        {
            List <CssDeclaration> convertedHtmlStyles = new List <CssDeclaration>();

            if (element.GetAdditionalHtmlStyles() != null)
            {
                Dictionary <String, String> additionalStyles = new Dictionary <String, String>();
                foreach (IDictionary <String, String> styles in element.GetAdditionalHtmlStyles())
                {
                    additionalStyles.AddAll(styles);
                }
                convertedHtmlStyles.EnsureCapacity(convertedHtmlStyles.Count + additionalStyles.Count);
                foreach (KeyValuePair <String, String> entry in additionalStyles)
                {
                    convertedHtmlStyles.Add(new CssDeclaration(entry.Key, entry.Value));
                }
            }
            foreach (IAttribute a in element.GetAttributes())
            {
                HtmlStylesToCssConverter.IAttributeConverter aConverter = htmlAttributeConverters.Get(a.GetKey());
                if (aConverter != null && aConverter.IsSupportedForElement(element.Name()))
                {
                    convertedHtmlStyles.AddAll(aConverter.Convert(element, a.GetValue()));
                }
            }
            return(convertedHtmlStyles);
        }
Example #2
0
        public virtual IDictionary <String, String> ResolveStyles(INode node, AbstractCssContext context)
        {
            IDictionary <String, String> styles = new Dictionary <String, String>();

            if (node is IElementNode)
            {
                IElementNode eNode = (IElementNode)node;
                foreach (IAttribute attr in eNode.GetAttributes())
                {
                    styles.Put(attr.GetKey(), attr.GetValue());
                }
            }
            return(styles);
        }
        public virtual IDictionary <String, String> ResolveStyles(INode node, AbstractCssContext context)
        {
            IDictionary <String, String> styles = new Dictionary <String, String>();
            //Load in from collected style sheets
            IList <CssDeclaration> styleSheetDeclarations = css.GetCssDeclarations(node, MediaDeviceDescription.CreateDefault
                                                                                       ());

            foreach (CssDeclaration ssd in styleSheetDeclarations)
            {
                styles.Put(ssd.GetProperty(), ssd.GetExpression());
            }
            //Load in attributes declarations
            if (node is IElementNode)
            {
                IElementNode eNode = (IElementNode)node;
                foreach (IAttribute attr in eNode.GetAttributes())
                {
                    ProcessAttribute(attr, styles);
                }
            }
            //Load in and merge inherited declarations from parent
            if (node.ParentNode() is IStylesContainer)
            {
                IStylesContainer             parentNode   = (IStylesContainer)node.ParentNode();
                IDictionary <String, String> parentStyles = parentNode.GetStyles();
                if (parentStyles == null && !(node.ParentNode() is IDocumentNode))
                {
                    ILog logger = LogManager.GetLogger(typeof(iText.Svg.Css.Impl.SvgStyleResolver));
                    logger.Error(iText.StyledXmlParser.LogMessageConstant.ERROR_RESOLVING_PARENT_STYLES);
                }
                if (parentStyles != null)
                {
                    foreach (KeyValuePair <String, String> entry in parentStyles)
                    {
                        String parentFontSizeString = parentStyles.Get(CommonCssConstants.FONT_SIZE);
                        if (parentFontSizeString == null)
                        {
                            parentFontSizeString = "0";
                        }
                        sru.MergeParentStyleDeclaration(styles, entry.Key, entry.Value, parentFontSizeString);
                    }
                }
            }
            return(styles);
        }
Example #4
0
        /// <summary>Resolves node styles without inheritance of parent element styles.</summary>
        /// <param name="node">the node</param>
        /// <param name="cssContext">the CSS context (RootFontSize, etc.)</param>
        /// <returns>the map containing the resolved styles that are defined in the body of the element</returns>
        public virtual IDictionary <String, String> ResolveNativeStyles(INode node, AbstractCssContext cssContext)
        {
            IDictionary <String, String> styles = new Dictionary <String, String>();
            // Load in from collected style sheets
            IList <CssDeclaration> styleSheetDeclarations = css.GetCssDeclarations(node, MediaDeviceDescription.CreateDefault
                                                                                       ());

            foreach (CssDeclaration ssd in styleSheetDeclarations)
            {
                styles.Put(ssd.GetProperty(), ssd.GetExpression());
            }
            // Load in attributes declarations
            if (node is IElementNode)
            {
                IElementNode eNode = (IElementNode)node;
                foreach (IAttribute attr in eNode.GetAttributes())
                {
                    ProcessAttribute(attr, styles);
                }
            }
            return(styles);
        }