/// <summary>Resolve the font size stored inside the passed renderer</summary> /// <param name="renderer">renderer containing the font size declaration</param> /// <param name="parentFontSize">parent font size to fall back on if the renderer does not contain a font size declarations or if the stored declaration is invalid /// </param> /// <returns>float containing the font-size, or the parent font size if the renderer's declaration cannot be resolved /// </returns> public static float ResolveFontSize(ISvgTextNodeRenderer renderer, float parentFontSize) { //Use own font-size declaration if it is present, parent's otherwise float fontSize = float.NaN; String elementFontSize = renderer.GetAttribute(SvgConstants.Attributes.FONT_SIZE); if (null != elementFontSize && !String.IsNullOrEmpty(elementFontSize)) { if (CssUtils.IsRelativeValue(elementFontSize) || CommonCssConstants.LARGER.Equals(elementFontSize) || CommonCssConstants .SMALLER.Equals(elementFontSize)) { // TODO DEVSIX-2866 Support rem value for svgs fontSize = CssUtils.ParseRelativeFontSize(elementFontSize, parentFontSize); } else { fontSize = CssUtils.ParseAbsoluteFontSize(elementFontSize, CommonCssConstants.PX); } } if ((float.IsNaN(fontSize)) || fontSize < 0f) { fontSize = parentFontSize; } return(fontSize); }
/* (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); }