Ejemplo n.º 1
0
        // TODO these are css properties actually, but it is not supported by the browsers currently
        /// <summary>Applies hyphenation to an element.</summary>
        /// <param name="cssProps">the CSS props</param>
        /// <param name="context">the processor context</param>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="element">the element</param>
        public static void ApplyHyphenation(IDictionary <String, String> cssProps, ProcessorContext context, IStylesContainer
                                            stylesContainer, IPropertyContainer element)
        {
            String value = cssProps.Get(CssConstants.HYPHENS);

            if (value == null)
            {
                value = CssDefaults.GetDefaultValue(CssConstants.HYPHENS);
            }
            if (CssConstants.NONE.Equals(value))
            {
                element.SetProperty(Property.HYPHENATION, null);
            }
            else
            {
                if (CssConstants.MANUAL.Equals(value))
                {
                    element.SetProperty(Property.HYPHENATION, new HyphenationConfig(HYPHENATE_BEFORE, HYPHENATE_AFTER));
                }
                else
                {
                    if (CssConstants.AUTO.Equals(value) && stylesContainer is IElementNode)
                    {
                        String lang = ((IElementNode)stylesContainer).GetLang();
                        if (lang != null && lang.Length > 0)
                        {
                            element.SetProperty(Property.HYPHENATION, new HyphenationConfig(lang.JSubstring(0, 2), "", HYPHENATE_BEFORE
                                                                                            , HYPHENATE_AFTER));
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override float GetCurrentFontSize()
        {
            String fontSizeValue = GetAttribute(SvgConstants.Attributes.FONT_SIZE);

            if (fontSizeValue == null)
            {
                fontSizeValue = CssDefaults.GetDefaultValue(CommonCssConstants.FONT_SIZE);
            }
            return(CssUtils.ParseAbsoluteFontSize(fontSizeValue));
        }
Ejemplo n.º 3
0
 private static void FillNotProcessedProperties(IDictionary <CssBackgroundUtils.BackgroundPropertyType, String
                                                             > resolvedProps, ICollection <CssBackgroundUtils.BackgroundPropertyType> usedTypes)
 {
     foreach (CssBackgroundUtils.BackgroundPropertyType type in new List <CssBackgroundUtils.BackgroundPropertyType
                                                                          >(resolvedProps.Keys))
     {
         if (!usedTypes.Contains(type) && type != CssBackgroundUtils.BackgroundPropertyType.BACKGROUND_COLOR)
         {
             if (resolvedProps.Get(type) == null)
             {
                 resolvedProps.Put(type, CssDefaults.GetDefaultValue(CssBackgroundUtils.GetBackgroundPropertyNameFromType(type
                                                                                                                          )));
             }
             else
             {
                 resolvedProps.Put(type, resolvedProps.Get(type) + "," + CssDefaults.GetDefaultValue(CssBackgroundUtils.GetBackgroundPropertyNameFromType
                                                                                                         (type)));
             }
         }
     }
 }
Ejemplo n.º 4
0
 /// <summary>Create an instance of the context that is used to store information when converting SVG.</summary>
 /// <param name="resourceResolver">
 /// instance of
 /// <see cref="iText.StyledXmlParser.Resolver.Resource.ResourceResolver"/>
 /// </param>
 /// <param name="fontProvider">
 /// instance of
 /// <see cref="iText.Layout.Font.FontProvider"/>
 /// </param>
 /// <param name="svgRootRenderer">svg element that is root for current file</param>
 public SvgDrawContext(ResourceResolver resourceResolver, FontProvider fontProvider, ISvgNodeRenderer svgRootRenderer
                       )
 {
     if (resourceResolver == null)
     {
         resourceResolver = new ResourceResolver(null);
     }
     this.resourceResolver = resourceResolver;
     if (fontProvider == null)
     {
         fontProvider = new BasicFontProvider();
     }
     this.fontProvider = fontProvider;
     if (svgRootRenderer is AbstractSvgNodeRenderer)
     {
         remValue = ((AbstractSvgNodeRenderer)svgRootRenderer).GetCurrentFontSize();
     }
     else
     {
         // default font-size value
         remValue = CssUtils.ParseAbsoluteFontSize(CssDefaults.GetDefaultValue(CommonCssConstants.FONT_SIZE));
     }
 }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a
        /// <see cref="iText.Layout.Borders.Border"/>
        /// instance based on specific properties.
        /// </summary>
        /// <param name="outlineWidth">the outline width</param>
        /// <param name="outlineStyle">the outline style</param>
        /// <param name="outlineColor">the outline color</param>
        /// <param name="em">the em value</param>
        /// <param name="rem">the root em value</param>
        /// <returns>the border</returns>
        public static Border GetCertainBorder(String outlineWidth, String outlineStyle, String outlineColor, float
                                              em, float rem)
        {
            if (outlineStyle == null || CssConstants.NONE.Equals(outlineStyle))
            {
                return(null);
            }
            if (outlineWidth == null)
            {
                outlineWidth = CssDefaults.GetDefaultValue(CssConstants.OUTLINE_WIDTH);
            }
            float outlineWidthValue;

            if (CssConstants.BORDER_WIDTH_VALUES.Contains(outlineWidth))
            {
                if (CssConstants.THIN.Equals(outlineWidth))
                {
                    outlineWidth = "1px";
                }
                else
                {
                    if (CssConstants.MEDIUM.Equals(outlineWidth))
                    {
                        outlineWidth = "2px";
                    }
                    else
                    {
                        if (CssConstants.THICK.Equals(outlineWidth))
                        {
                            outlineWidth = "3px";
                        }
                    }
                }
            }
            UnitValue unitValue = CssDimensionParsingUtils.ParseLengthValueToPt(outlineWidth, em, rem);

            if (unitValue == null)
            {
                return(null);
            }
            if (unitValue.IsPercentValue())
            {
                LOGGER.Error("outline-width in percents is not supported");
                return(null);
            }
            outlineWidthValue = unitValue.GetValue();
            Border outline = null;

            if (outlineWidthValue > 0)
            {
                DeviceRgb color   = (DeviceRgb)ColorConstants.BLACK;
                float     opacity = 1f;
                if (outlineColor != null)
                {
                    if (!CssConstants.TRANSPARENT.Equals(outlineColor))
                    {
                        float[] rgbaColor = CssDimensionParsingUtils.ParseRgbaColor(outlineColor);
                        color   = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
                        opacity = rgbaColor[3];
                    }
                    else
                    {
                        opacity = 0f;
                    }
                }
                else
                {
                    if (CssConstants.GROOVE.Equals(outlineStyle) || CssConstants.RIDGE.Equals(outlineStyle) || CssConstants.INSET
                        .Equals(outlineStyle) || CssConstants.OUTSET.Equals(outlineStyle))
                    {
                        color = new DeviceRgb(212, 208, 200);
                    }
                }
                switch (outlineStyle)
                {
                case CssConstants.SOLID:
                case CssConstants.AUTO: {
                    outline = new SolidBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.DASHED: {
                    outline = new DashedBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.DOTTED: {
                    outline = new DottedBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.DOUBLE: {
                    outline = new DoubleBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.GROOVE: {
                    outline = new GrooveBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.RIDGE: {
                    outline = new RidgeBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.INSET: {
                    outline = new InsetBorder(color, outlineWidthValue, opacity);
                    break;
                }

                case CssConstants.OUTSET: {
                    outline = new OutsetBorder(color, outlineWidthValue, opacity);
                    break;
                }

                default: {
                    outline = null;
                    break;
                }
                }
            }
            return(outline);
        }
        /// <summary>
        /// Creates a
        /// <see cref="iText.Layout.Borders.Border"/>
        /// instance based on specific properties.
        /// </summary>
        /// <param name="borderWidth">the border width</param>
        /// <param name="borderStyle">the border style</param>
        /// <param name="borderColor">the border color</param>
        /// <param name="em">the em value</param>
        /// <param name="rem">the root em value</param>
        /// <returns>the border</returns>
        public static Border GetCertainBorder(String borderWidth, String borderStyle, String borderColor, float em
                                              , float rem)
        {
            if (borderStyle == null || CssConstants.NONE.Equals(borderStyle))
            {
                return(null);
            }
            if (borderWidth == null)
            {
                borderWidth = CssDefaults.GetDefaultValue(CssConstants.BORDER_WIDTH);
            }
            float borderWidthValue;

            if (CssConstants.BORDER_WIDTH_VALUES.Contains(borderWidth))
            {
                if (CssConstants.THIN.Equals(borderWidth))
                {
                    borderWidth = "1px";
                }
                else
                {
                    if (CssConstants.MEDIUM.Equals(borderWidth))
                    {
                        borderWidth = "2px";
                    }
                    else
                    {
                        if (CssConstants.THICK.Equals(borderWidth))
                        {
                            borderWidth = "3px";
                        }
                    }
                }
            }
            UnitValue unitValue = CssUtils.ParseLengthValueToPt(borderWidth, em, rem);

            if (unitValue == null)
            {
                return(null);
            }
            if (unitValue.IsPercentValue())
            {
                return(null);
            }
            borderWidthValue = unitValue.GetValue();
            Border border = null;

            if (borderWidthValue > 0)
            {
                DeviceRgb color   = (DeviceRgb)ColorConstants.BLACK;
                float     opacity = 1f;
                if (borderColor != null)
                {
                    if (!CssConstants.TRANSPARENT.Equals(borderColor))
                    {
                        float[] rgbaColor = CssUtils.ParseRgbaColor(borderColor);
                        color   = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
                        opacity = rgbaColor[3];
                    }
                    else
                    {
                        opacity = 0f;
                    }
                }
                else
                {
                    if (CssConstants.GROOVE.Equals(borderStyle) || CssConstants.RIDGE.Equals(borderStyle) || CssConstants.INSET
                        .Equals(borderStyle) || CssConstants.OUTSET.Equals(borderStyle))
                    {
                        color = new DeviceRgb(212, 208, 200);
                    }
                }
                switch (borderStyle)
                {
                case CssConstants.SOLID: {
                    border = new SolidBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.DASHED: {
                    border = new DashedBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.DOTTED: {
                    border = new RoundDotsBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.DOUBLE: {
                    border = new DoubleBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.GROOVE: {
                    border = new GrooveBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.RIDGE: {
                    border = new RidgeBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.INSET: {
                    border = new InsetBorder(color, borderWidthValue, opacity);
                    break;
                }

                case CssConstants.OUTSET: {
                    border = new OutsetBorder(color, borderWidthValue, opacity);
                    break;
                }

                default: {
                    border = null;
                    break;
                }
                }
            }
            return(border);
        }