예제 #1
0
        public virtual IDictionary <String, String> ResolveStyles(INode node, AbstractCssContext context)
        {
            IDictionary <String, String> styles = ResolveNativeStyles(node, context);

            //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);
                }
                ICollection <IStyleInheritance> inheritanceRules = new HashSet <IStyleInheritance>();
                inheritanceRules.Add(new CssInheritance());
                inheritanceRules.Add(new SvgAttributeInheritance());
                if (parentStyles != null)
                {
                    foreach (KeyValuePair <String, String> entry in parentStyles)
                    {
                        String parentFontSizeString = parentStyles.Get(CommonCssConstants.FONT_SIZE);
                        if (parentFontSizeString == null)
                        {
                            parentFontSizeString = "0";
                        }
                        styles = StyleUtil.MergeParentStyleDeclaration(styles, entry.Key, entry.Value, parentFontSizeString, inheritanceRules
                                                                       );
                    }
                }
            }
            return(styles);
        }
예제 #2
0
 protected internal virtual void ApplyStyles(ISvgNodeRenderer parent, ISvgNodeRenderer child)
 {
     if (parent != null && child != null)
     {
         IDictionary <String, String> childStyles = child.GetAttributeMapCopy();
         if (childStyles == null)
         {
             childStyles = new Dictionary <String, String>();
         }
         IDictionary <String, String> parentStyles = parent.GetAttributeMapCopy();
         String parentFontSize = parent.GetAttribute(SvgConstants.Attributes.FONT_SIZE);
         if (parentFontSize == null)
         {
             parentFontSize = "0";
         }
         ICollection <IStyleInheritance> inheritanceRules = new HashSet <IStyleInheritance>();
         inheritanceRules.Add(new CssInheritance());
         inheritanceRules.Add(new SvgAttributeInheritance());
         foreach (KeyValuePair <String, String> parentAttribute in parentStyles)
         {
             childStyles = StyleUtil.MergeParentStyleDeclaration(childStyles, parentAttribute.Key, parentAttribute.Value
                                                                 , parentFontSize, inheritanceRules);
         }
         child.SetAttributesAndStyles(childStyles);
     }
 }
예제 #3
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.resolve.ICssResolver#resolveStyles(com.itextpdf.html2pdf.html.node.INode, com.itextpdf.html2pdf.css.resolve.CssContext)
         */
        private IDictionary <String, String> ResolveStyles(INode element, CssContext context)
        {
            IList <CssRuleSet> ruleSets = new List <CssRuleSet>();

            ruleSets.Add(new CssRuleSet(null, UserAgentCss.GetStyles(element)));
            if (element is IElementNode)
            {
                ruleSets.Add(new CssRuleSet(null, HtmlStylesToCssConverter.Convert((IElementNode)element)));
            }
            ruleSets.AddAll(cssStyleSheet.GetCssRuleSets(element, deviceDescription));
            if (element is IElementNode)
            {
                String styleAttribute = ((IElementNode)element).GetAttribute(AttributeConstants.STYLE);
                if (styleAttribute != null)
                {
                    ruleSets.Add(new CssRuleSet(null, CssRuleSetParser.ParsePropertyDeclarations(styleAttribute)));
                }
            }
            IDictionary <String, String> elementStyles = CssStyleSheet.ExtractStylesFromRuleSets(ruleSets);

            if (CssConstants.CURRENTCOLOR.Equals(elementStyles.Get(CssConstants.COLOR)))
            {
                // css-color-3/#currentcolor:
                // If the ‘currentColor’ keyword is set on the ‘color’ property itself, it is treated as ‘color: inherit’.
                elementStyles.Put(CssConstants.COLOR, CssConstants.INHERIT);
            }
            String parentFontSizeStr = null;

            if (element.ParentNode() is IStylesContainer)
            {
                IStylesContainer             parentNode   = (IStylesContainer)element.ParentNode();
                IDictionary <String, String> parentStyles = parentNode.GetStyles();
                if (parentStyles == null && !(element.ParentNode() is IDocumentNode))
                {
                    ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Css.Resolve.DefaultCssResolver));
                    logger.Error(iText.Html2pdf.LogMessageConstant.ERROR_RESOLVING_PARENT_STYLES);
                }
                if (parentStyles != null)
                {
                    ICollection <IStyleInheritance> inheritanceRules = new HashSet <IStyleInheritance>();
                    inheritanceRules.Add(cssInheritance);
                    foreach (KeyValuePair <String, String> entry in parentStyles)
                    {
                        elementStyles = StyleUtil.MergeParentStyleDeclaration(elementStyles, entry.Key, entry.Value, parentStyles.
                                                                              Get(CommonCssConstants.FONT_SIZE), inheritanceRules);
                    }
                    parentFontSizeStr = parentStyles.Get(CssConstants.FONT_SIZE);
                }
            }
            String elementFontSize = elementStyles.Get(CssConstants.FONT_SIZE);

            if (CssUtils.IsRelativeValue(elementFontSize) || CssConstants.LARGER.Equals(elementFontSize) || CssConstants
                .SMALLER.Equals(elementFontSize))
            {
                float baseFontSize;
                if (CssUtils.IsRemValue(elementFontSize))
                {
                    baseFontSize = context.GetRootFontSize();
                }
                else
                {
                    if (parentFontSizeStr == null)
                    {
                        baseFontSize = CssUtils.ParseAbsoluteFontSize(CssDefaults.GetDefaultValue(CssConstants.FONT_SIZE));
                    }
                    else
                    {
                        baseFontSize = CssUtils.ParseAbsoluteLength(parentFontSizeStr);
                    }
                }
                float absoluteFontSize = CssUtils.ParseRelativeFontSize(elementFontSize, baseFontSize);
                // Format to 4 decimal places to prevent differences between Java and C#
                elementStyles.Put(CssConstants.FONT_SIZE, DecimalFormatUtil.FormatNumber(absoluteFontSize, "0.####") + CssConstants
                                  .PT);
            }
            else
            {
                elementStyles.Put(CssConstants.FONT_SIZE, Convert.ToString(CssUtils.ParseAbsoluteFontSize(elementFontSize)
                                                                           , System.Globalization.CultureInfo.InvariantCulture) + CssConstants.PT);
            }
            // Update root font size
            if (element is IElementNode && TagConstants.HTML.Equals(((IElementNode)element).Name()))
            {
                context.SetRootFontSize(elementStyles.Get(CssConstants.FONT_SIZE));
            }
            ICollection <String> keys = new HashSet <String>();

            foreach (KeyValuePair <String, String> entry in elementStyles)
            {
                if (CssConstants.INITIAL.Equals(entry.Value) || CssConstants.INHERIT.Equals(entry.Value))
                {
                    // if "inherit" is not resolved till now, parents don't have it
                    keys.Add(entry.Key);
                }
            }
            foreach (String key in keys)
            {
                elementStyles.Put(key, CssDefaults.GetDefaultValue(key));
            }
            // This is needed for correct resolving of content property, so doing it right here
            CounterProcessorUtil.ProcessCounters(elementStyles, context, element);
            ResolveContentProperty(elementStyles, element, context);
            return(elementStyles);
        }