/* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.apply.ICssApplier#apply(com.itextpdf.html2pdf.attach.ProcessorContext, com.itextpdf.html2pdf.html.node.IStylesContainer, com.itextpdf.html2pdf.attach.ITagWorker)
         */
        public virtual void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                  )
        {
            IDictionary <String, String> cssProps  = stylesContainer.GetStyles();
            IPropertyContainer           container = tagWorker.GetElementResult();

            if (container != null)
            {
                WidthHeightApplierUtil.ApplyWidthHeight(cssProps, context, container);
                BackgroundApplierUtil.ApplyBackground(cssProps, context, container);
                MarginApplierUtil.ApplyMargins(cssProps, context, container);
                PaddingApplierUtil.ApplyPaddings(cssProps, context, container);
                FontStyleApplierUtil.ApplyFontStyles(cssProps, context, stylesContainer, container);
                BorderStyleApplierUtil.ApplyBorders(cssProps, context, container);
                HyphenationApplierUtil.ApplyHyphenation(cssProps, context, stylesContainer, container);
                FloatApplierUtil.ApplyFloating(cssProps, context, container);
                PositionApplierUtil.ApplyPosition(cssProps, context, container);
                OpacityApplierUtil.ApplyOpacity(cssProps, context, container);
                PageBreakApplierUtil.ApplyPageBreakProperties(cssProps, context, container);
                OverflowApplierUtil.ApplyOverflow(cssProps, container);
                TransformationApplierUtil.ApplyTransformation(cssProps, context, container);
                OutlineApplierUtil.ApplyOutlines(cssProps, context, container);
                OrphansWidowsApplierUtil.ApplyOrphansAndWidows(cssProps, container);
            }
        }
Exemple #2
0
        public virtual void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                  )
        {
            IDictionary <String, String> boxStyles = stylesContainer.GetStyles();
            IPropertyContainer           marginBox = tagWorker.GetElementResult();

            BackgroundApplierUtil.ApplyBackground(boxStyles, context, marginBox);
            FontStyleApplierUtil.ApplyFontStyles(boxStyles, context, stylesContainer, marginBox);
            BorderStyleApplierUtil.ApplyBorders(boxStyles, context, marginBox);
            VerticalAlignmentApplierUtil.ApplyVerticalAlignmentForCells(boxStyles, context, marginBox);
            // Set overflow to HIDDEN if it's not explicitly set in css in order to avoid overlapping with page content.
            String overflow = CssConstants.OVERFLOW_VALUES.Contains(boxStyles.Get(CssConstants.OVERFLOW)) ? boxStyles.
                              Get(CssConstants.OVERFLOW) : null;
            String overflowX = CssConstants.OVERFLOW_VALUES.Contains(boxStyles.Get(CssConstants.OVERFLOW_X)) ? boxStyles
                               .Get(CssConstants.OVERFLOW_X) : overflow;

            if (overflowX == null || CssConstants.HIDDEN.Equals(overflowX))
            {
                marginBox.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.HIDDEN);
            }
            else
            {
                marginBox.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
            }
            String overflowY = CssConstants.OVERFLOW_VALUES.Contains(boxStyles.Get(CssConstants.OVERFLOW_Y)) ? boxStyles
                               .Get(CssConstants.OVERFLOW_Y) : overflow;

            if (overflowY == null || CssConstants.HIDDEN.Equals(overflowY))
            {
                marginBox.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.HIDDEN);
            }
            else
            {
                marginBox.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
            }
            // TODO outlines are currently not supported for page margin boxes, because of the outlines handling specificity (they are handled on renderer's parent level)
            OutlineApplierUtil.ApplyOutlines(boxStyles, context, marginBox);
            marginBox.SetProperty(Property.FONT_PROVIDER, context.GetFontProvider());
            marginBox.SetProperty(Property.FONT_SET, context.GetTempFonts());
            if (!(stylesContainer is PageMarginBoxContextNode))
            {
                ILog logger = LogManager.GetLogger(typeof(PageMarginBoxCssApplier));
                logger.Warn(iText.Html2pdf.LogMessageConstant.PAGE_MARGIN_BOX_SOME_PROPERTIES_NOT_PROCESSED);
                return;
            }
            float availableWidth = ((PageMarginBoxContextNode)stylesContainer).GetContainingBlockForMarginBox().GetWidth
                                       ();
            float availableHeight = ((PageMarginBoxContextNode)stylesContainer).GetContainingBlockForMarginBox().GetHeight
                                        ();

            MarginApplierUtil.ApplyMargins(boxStyles, context, marginBox, availableHeight, availableWidth);
            PaddingApplierUtil.ApplyPaddings(boxStyles, context, marginBox, availableHeight, availableWidth);
        }
Exemple #3
0
 /// <summary>Applies styles to child elements.</summary>
 /// <param name="element">the element</param>
 /// <param name="css">the CSS mapping</param>
 /// <param name="context">the processor context</param>
 /// <param name="stylesContainer">the styles container</param>
 protected internal virtual void ApplyChildElementStyles(IPropertyContainer element, IDictionary <String, String
                                                                                                  > css, ProcessorContext context, IStylesContainer stylesContainer)
 {
     FontStyleApplierUtil.ApplyFontStyles(css, context, stylesContainer, element);
     BackgroundApplierUtil.ApplyBackground(css, context, element);
     //TODO: Border-applying currently doesn't work in html way for spans inside other spans.
     BorderStyleApplierUtil.ApplyBorders(css, context, element);
     OutlineApplierUtil.ApplyOutlines(css, context, element);
     HyphenationApplierUtil.ApplyHyphenation(css, context, stylesContainer, element);
     //TODO: Margins-applying currently doesn't work in html way for spans inside other spans. (see SpanTest#spanTest07)
     MarginApplierUtil.ApplyMargins(css, context, element);
     PositionApplierUtil.ApplyPosition(css, context, element);
     FloatApplierUtil.ApplyFloating(css, context, element);
     PaddingApplierUtil.ApplyPaddings(css, context, element);
 }
        public virtual void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                  )
        {
            IDictionary <String, String> cssStyles           = stylesContainer.GetStyles();
            IStylesContainer             parentToBeProcessed = (IStylesContainer)((CssPseudoElementNode)stylesContainer).ParentNode
                                                                   ();
            IPropertyContainer elementResult = context.GetState().Top().GetElementResult();

            if (elementResult is IPlaceholderable)
            {
                IPropertyContainer element = ((IPlaceholderable)elementResult).GetPlaceholder();
                FontStyleApplierUtil.ApplyFontStyles(cssStyles, context, parentToBeProcessed, element);
                BackgroundApplierUtil.ApplyBackground(cssStyles, context, element);
                OpacityApplierUtil.ApplyOpacity(cssStyles, context, element);
            }
        }
        internal virtual float ParseDimension(CssContextNode node, String content, float maxAvailableDimension, float
                                              additionalWidthFix)
        {
            float     fontSize  = FontStyleApplierUtil.ParseAbsoluteFontSize(node.GetStyles().Get(CssConstants.FONT_SIZE));
            UnitValue unitValue = CssUtils.ParseLengthValueToPt(content, fontSize, 0);

            if (unitValue == null)
            {
                return(0);
            }
            if (unitValue.IsPointValue())
            {
                return(unitValue.GetValue() + additionalWidthFix);
            }
            return(maxAvailableDimension * unitValue.GetValue() / 100f);
        }
Exemple #6
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.apply.ICssApplier#apply(com.itextpdf.html2pdf.attach.ProcessorContext, com.itextpdf.html2pdf.html.node.IStylesContainer, com.itextpdf.html2pdf.attach.ITagWorker)
         */
        public virtual void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                  )
        {
            IDictionary <String, String> cssProps      = stylesContainer.GetStyles();
            BodyHtmlStylesContainer      styleProperty = new BodyHtmlStylesContainer();
            IPropertyContainer           container     = tagWorker.GetElementResult();

            if (container != null)
            {
                BackgroundApplierUtil.ApplyBackground(cssProps, context, styleProperty);
                MarginApplierUtil.ApplyMargins(cssProps, context, styleProperty);
                PaddingApplierUtil.ApplyPaddings(cssProps, context, styleProperty);
                BorderStyleApplierUtil.ApplyBorders(cssProps, context, styleProperty);
                if (styleProperty.HasStylesToApply())
                {
                    container.SetProperty(Html2PdfProperty.BODY_STYLING, styleProperty);
                }
                FontStyleApplierUtil.ApplyFontStyles(cssProps, context, stylesContainer, container);
            }
        }
        /* (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)
                {
                    foreach (KeyValuePair <String, String> entry in parentStyles)
                    {
                        MergeParentCssDeclaration(elementStyles, entry.Key, entry.Value, parentStyles);
                    }
                    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 = FontStyleApplierUtil.ParseAbsoluteFontSize(CssDefaults.GetDefaultValue(CssConstants.FONT_SIZE
                                                                                                              ));
                    }
                    else
                    {
                        baseFontSize = CssUtils.ParseAbsoluteLength(parentFontSizeStr);
                    }
                }
                float absoluteFontSize = FontStyleApplierUtil.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(FontStyleApplierUtil.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);
        }
Exemple #8
0
 /// <summary>Sets the root font size.</summary>
 /// <param name="fontSizeStr">the new root font size</param>
 public virtual void SetRootFontSize(String fontSizeStr)
 {
     this.rootFontSize = FontStyleApplierUtil.ParseAbsoluteFontSize(fontSizeStr);
 }