Exemplo n.º 1
0
 /// <summary>Resolves the property type.</summary>
 /// <param name="value">the value</param>
 /// <returns>the property type value</returns>
 private int ResolvePropertyType(String value)
 {
     if (value.Contains("url(") || CssGradientUtil.IsCssLinearGradientValue(value) || CommonCssConstants.NONE.Equals
             (value))
     {
         return(BACKGROUND_IMAGE_TYPE);
     }
     else
     {
         if (CommonCssConstants.BACKGROUND_REPEAT_VALUES.Contains(value))
         {
             return(BACKGROUND_REPEAT_TYPE);
         }
         else
         {
             if (CommonCssConstants.BACKGROUND_ATTACHMENT_VALUES.Contains(value))
             {
                 return(BACKGROUND_ATTACHMENT_TYPE);
             }
             else
             {
                 if (CommonCssConstants.BACKGROUND_POSITION_VALUES.Contains(value))
                 {
                     return(BACKGROUND_POSITION_TYPE);
                 }
                 else
                 {
                     if (CssUtils.IsNumericValue(value) || CssUtils.IsMetricValue(value) || CssUtils.IsRelativeValue(value))
                     {
                         return(BACKGROUND_POSITION_OR_SIZE_TYPE);
                     }
                     else
                     {
                         if (CommonCssConstants.BACKGROUND_SIZE_VALUES.Contains(value))
                         {
                             return(BACKGROUND_POSITION_OR_SIZE_TYPE);
                         }
                         else
                         {
                             if (CssUtils.IsColorProperty(value))
                             {
                                 return(BACKGROUND_COLOR_TYPE);
                             }
                             else
                             {
                                 if (CommonCssConstants.BACKGROUND_ORIGIN_OR_CLIP_VALUES.Contains(value))
                                 {
                                     return(BACKGROUND_ORIGIN_OR_CLIP_TYPE);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(UNDEFINED_TYPE);
 }
        /* (non-Javadoc)
         * @see com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver#resolveShorthand(java.lang.String)
         */
        public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression)
        {
            if (CommonCssConstants.INITIAL.Equals(shorthandExpression) || CommonCssConstants.INHERIT.Equals(shorthandExpression
                                                                                                            ))
            {
                return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.LIST_STYLE_TYPE, shorthandExpression),
                                             new CssDeclaration(CommonCssConstants.LIST_STYLE_POSITION, shorthandExpression), new CssDeclaration(CommonCssConstants
                                                                                                                                                 .LIST_STYLE_IMAGE, shorthandExpression)));
            }
            IList <String> props = CssUtils.ExtractShorthandProperties(shorthandExpression)[0];
            String         listStyleTypeValue     = null;
            String         listStylePositionValue = null;
            String         listStyleImageValue    = null;

            foreach (String value in props)
            {
                if (value.Contains("url(") || CssGradientUtil.IsCssLinearGradientValue(value) || (CommonCssConstants.NONE.
                                                                                                  Equals(value) && listStyleTypeValue != null))
                {
                    listStyleImageValue = value;
                }
                else
                {
                    if (LIST_STYLE_TYPE_VALUES.Contains(value))
                    {
                        listStyleTypeValue = value;
                    }
                    else
                    {
                        if (LIST_STYLE_POSITION_VALUES.Contains(value))
                        {
                            listStylePositionValue = value;
                        }
                    }
                }
            }
            IList <CssDeclaration> resolvedDecl = new List <CssDeclaration>();

            resolvedDecl.Add(new CssDeclaration(CommonCssConstants.LIST_STYLE_TYPE, listStyleTypeValue == null ? CommonCssConstants
                                                .INITIAL : listStyleTypeValue));
            resolvedDecl.Add(new CssDeclaration(CommonCssConstants.LIST_STYLE_POSITION, listStylePositionValue == null
                 ? CommonCssConstants.INITIAL : listStylePositionValue));
            resolvedDecl.Add(new CssDeclaration(CommonCssConstants.LIST_STYLE_IMAGE, listStyleImageValue == null ? CommonCssConstants
                                                .INITIAL : listStyleImageValue));
            return(resolvedDecl);
        }
        private void AddLinearGradientCssApproach(PdfDocument pdfDoc)
        {
            String gradientValue = "linear-gradient(to left, #ff0000, #008000, #0000ff)";

            if (CssGradientUtil.IsCssLinearGradientValue(gradientValue))
            {
                StrategyBasedLinearGradientBuilder gradientBuilder = CssGradientUtil

                                                                     // "em/rem" parameters are mandatory but don't have effect in case of such parameters aren't used
                                                                     // within passing "gradientValue" variable
                                                                     .ParseCssLinearGradient(gradientValue, 12, 12);
                Rectangle rectangleToDraw = new Rectangle(50f, 450f, 500f, 300f);

                GeneratePdf(pdfDoc, null, gradientBuilder, rectangleToDraw);
            }
            else
            {
                Console.Out.WriteLine("The passed parameter: " + "\n" + gradientValue + "\n" +
                                      " is not a linear gradient or repeating linear gradient function");
            }
        }
        private static IList <BackgroundImage> GetBackgroundImagesList(IList <String> backgroundImagesArray, ProcessorContext
                                                                       context, float em, float rem, IList <String> backgroundPositionXArray, IList <String> backgroundPositionYArray
                                                                       , IList <IList <String> > backgroundSizeArray, IList <String> backgroundBlendModeArray, IList <String> backgroundRepeatArray
                                                                       , IList <String> backgroundClipArray, IList <String> backgroundOriginArray)
        {
            IList <BackgroundImage> backgroundImagesList = new List <BackgroundImage>();

            for (int i = 0; i < backgroundImagesArray.Count; ++i)
            {
                String backgroundImage = backgroundImagesArray[i];
                if (backgroundImage == null || CssConstants.NONE.Equals(backgroundImage))
                {
                    continue;
                }
                BackgroundPosition position = ApplyBackgroundPosition(backgroundPositionXArray, backgroundPositionYArray,
                                                                      i, em, rem);
                BlendMode        blendMode    = ApplyBackgroundBlendMode(backgroundBlendModeArray, i);
                bool             imageApplied = false;
                BackgroundRepeat repeat       = ApplyBackgroundRepeat(backgroundRepeatArray, i);
                BackgroundBox    clip         = GetBackgroundBoxProperty(backgroundClipArray, i, BackgroundBox.BORDER_BOX);
                BackgroundBox    origin       = GetBackgroundBoxProperty(backgroundOriginArray, i, BackgroundBox.PADDING_BOX);
                if (CssGradientUtil.IsCssLinearGradientValue(backgroundImage))
                {
                    imageApplied = ApplyLinearGradient(backgroundImage, backgroundImagesList, blendMode, position, em, rem, repeat
                                                       , clip, origin);
                }
                else
                {
                    PdfXObject image = context.GetResourceResolver().RetrieveImageExtended(CssUtils.ExtractUrl(backgroundImage
                                                                                                               ));
                    imageApplied = ApplyBackgroundImage(image, backgroundImagesList, repeat, blendMode, position, clip, origin
                                                        );
                }
                if (imageApplied)
                {
                    ApplyBackgroundSize(backgroundSizeArray, em, rem, i, backgroundImagesList[backgroundImagesList.Count - 1]);
                }
            }
            return(backgroundImagesList);
        }
Exemplo n.º 5
0
        /// <summary>Resolves content.</summary>
        /// <param name="styles">the styles map</param>
        /// <param name="contentContainer">the content container</param>
        /// <param name="context">the CSS context</param>
        /// <returns>
        /// a list of
        /// <see cref="iText.StyledXmlParser.Node.INode"/>
        /// instances
        /// </returns>
        internal static IList <INode> ResolveContent(IDictionary <String, String> styles, INode contentContainer, CssContext
                                                     context)
        {
            String        contentStr = styles.Get(CssConstants.CONTENT);
            IList <INode> result     = new List <INode>();

            if (contentStr == null || CssConstants.NONE.Equals(contentStr) || CssConstants.NORMAL.Equals(contentStr))
            {
                return(null);
            }
            CssDeclarationValueTokenizer tokenizer = new CssDeclarationValueTokenizer(contentStr);

            CssDeclarationValueTokenizer.Token token;
            CssQuotes quotes = null;

            while ((token = tokenizer.GetNextValidToken()) != null)
            {
                if (token.IsString())
                {
                    result.Add(new CssContentPropertyResolver.ContentTextNode(contentContainer, token.GetValue()));
                }
                else
                {
                    if (token.GetValue().StartsWith(CssConstants.COUNTERS + "("))
                    {
                        String paramsStr = token.GetValue().JSubstring(CssConstants.COUNTERS.Length + 1, token.GetValue().Length -
                                                                       1);
                        String[] @params = iText.IO.Util.StringUtil.Split(paramsStr, ",");
                        if (@params.Length == 0)
                        {
                            return(ErrorFallback(contentStr));
                        }
                        // Counters are denoted by case-sensitive identifiers
                        String counterName          = @params[0].Trim();
                        String counterSeparationStr = @params[1].Trim();
                        counterSeparationStr = counterSeparationStr.JSubstring(1, counterSeparationStr.Length - 1);
                        String            listStyleType  = @params.Length > 2 ? @params[2].Trim() : null;
                        CssCounterManager counterManager = context.GetCounterManager();
                        INode             scope          = contentContainer;
                        if (CssConstants.PAGE.Equals(counterName))
                        {
                            result.Add(new PageCountElementNode(false, contentContainer));
                        }
                        else
                        {
                            if (CssConstants.PAGES.Equals(counterName))
                            {
                                result.Add(new PageCountElementNode(true, contentContainer));
                            }
                            else
                            {
                                String resolvedCounter = counterManager.ResolveCounters(counterName, counterSeparationStr, listStyleType,
                                                                                        scope);
                                if (resolvedCounter == null)
                                {
                                    logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RESOLVE_COUNTER, counterName
                                                                          ));
                                }
                                else
                                {
                                    result.Add(new CssContentPropertyResolver.ContentTextNode(scope, resolvedCounter));
                                }
                            }
                        }
                    }
                    else
                    {
                        if (token.GetValue().StartsWith(CssConstants.COUNTER + "("))
                        {
                            String paramsStr = token.GetValue().JSubstring(CssConstants.COUNTER.Length + 1, token.GetValue().Length -
                                                                           1);
                            String[] @params = iText.IO.Util.StringUtil.Split(paramsStr, ",");
                            if (@params.Length == 0)
                            {
                                return(ErrorFallback(contentStr));
                            }
                            // Counters are denoted by case-sensitive identifiers
                            String            counterName    = @params[0].Trim();
                            String            listStyleType  = @params.Length > 1 ? @params[1].Trim() : null;
                            CssCounterManager counterManager = context.GetCounterManager();
                            INode             scope          = contentContainer;
                            if (CssConstants.PAGE.Equals(counterName))
                            {
                                result.Add(new PageCountElementNode(false, contentContainer));
                            }
                            else
                            {
                                if (CssConstants.PAGES.Equals(counterName))
                                {
                                    result.Add(new PageCountElementNode(true, contentContainer));
                                }
                                else
                                {
                                    String resolvedCounter = counterManager.ResolveCounter(counterName, listStyleType, scope);
                                    if (resolvedCounter == null)
                                    {
                                        logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RESOLVE_COUNTER, counterName
                                                                              ));
                                    }
                                    else
                                    {
                                        result.Add(new CssContentPropertyResolver.ContentTextNode(scope, resolvedCounter));
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (token.GetValue().StartsWith("url("))
                            {
                                IDictionary <String, String> attributes = new Dictionary <String, String>();
                                attributes.Put(AttributeConstants.SRC, CssUtils.ExtractUrl(token.GetValue()));
                                //TODO: probably should add user agent styles on CssContentElementNode creation, not here.
                                attributes.Put(AttributeConstants.STYLE, CssConstants.DISPLAY + ":" + CssConstants.INLINE_BLOCK);
                                result.Add(new CssContentElementNode(contentContainer, TagConstants.IMG, attributes));
                            }
                            else
                            {
                                if (CssGradientUtil.IsCssLinearGradientValue(token.GetValue()))
                                {
                                    IDictionary <String, String> attributes = new Dictionary <String, String>();
                                    attributes.Put(AttributeConstants.STYLE, CssConstants.BACKGROUND_IMAGE + ":" + token.GetValue() + ";" + CssConstants
                                                   .HEIGHT + ":" + CssConstants.INHERIT + ";" + CssConstants.WIDTH + ":" + CssConstants.INHERIT + ";");
                                    result.Add(new CssContentElementNode(contentContainer, TagConstants.DIV, attributes));
                                }
                                else
                                {
                                    if (token.GetValue().StartsWith("attr(") && contentContainer is CssPseudoElementNode)
                                    {
                                        int endBracket = token.GetValue().IndexOf(')');
                                        if (endBracket > 5)
                                        {
                                            String attrName = token.GetValue().JSubstring(5, endBracket);
                                            if (attrName.Contains("(") || attrName.Contains(" ") || attrName.Contains("'") || attrName.Contains("\""))
                                            {
                                                return(ErrorFallback(contentStr));
                                            }
                                            IElementNode element = (IElementNode)contentContainer.ParentNode();
                                            String       value   = element.GetAttribute(attrName);
                                            result.Add(new CssContentPropertyResolver.ContentTextNode(contentContainer, value == null ? "" : value));
                                        }
                                    }
                                    else
                                    {
                                        if (token.GetValue().EndsWith("quote") && contentContainer is IStylesContainer)
                                        {
                                            if (quotes == null)
                                            {
                                                quotes = CssQuotes.CreateQuotes(styles.Get(CssConstants.QUOTES), true);
                                            }
                                            String value = quotes.ResolveQuote(token.GetValue(), context);
                                            if (value == null)
                                            {
                                                return(ErrorFallback(contentStr));
                                            }
                                            result.Add(new CssContentPropertyResolver.ContentTextNode(contentContainer, value));
                                        }
                                        else
                                        {
                                            if (token.GetValue().StartsWith(CssConstants.ELEMENT + "(") && contentContainer is PageMarginBoxContextNode
                                                )
                                            {
                                                String paramsStr = token.GetValue().JSubstring(CssConstants.ELEMENT.Length + 1, token.GetValue().Length -
                                                                                               1);
                                                String[] @params = iText.IO.Util.StringUtil.Split(paramsStr, ",");
                                                if (@params.Length == 0)
                                                {
                                                    return(ErrorFallback(contentStr));
                                                }
                                                String name = @params[0].Trim();
                                                String runningElementOccurrence = null;
                                                if (@params.Length > 1)
                                                {
                                                    runningElementOccurrence = @params[1].Trim();
                                                }
                                                result.Add(new PageMarginRunningElementNode(name, runningElementOccurrence));
                                            }
                                            else
                                            {
                                                return(ErrorFallback(contentStr));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        /// <summary>Applies an image list style to an element.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="element">the element</param>
        public static void ApplyListStyleImageProperty(IDictionary <String, String> cssProps, ProcessorContext context
                                                       , IPropertyContainer element)
        {
            String     listStyleImageStr = cssProps.Get(CssConstants.LIST_STYLE_IMAGE);
            PdfXObject imageXObject      = null;

            if (listStyleImageStr != null && !CssConstants.NONE.Equals(listStyleImageStr))
            {
                if (CssGradientUtil.IsCssLinearGradientValue(listStyleImageStr))
                {
                    float em  = CssUtils.ParseAbsoluteLength(cssProps.Get(CssConstants.FONT_SIZE));
                    float rem = context.GetCssContext().GetRootFontSize();
                    try {
                        StrategyBasedLinearGradientBuilder gradientBuilder = CssGradientUtil.ParseCssLinearGradient(listStyleImageStr
                                                                                                                    , em, rem);
                        if (gradientBuilder != null)
                        {
                            Rectangle formBBox = new Rectangle(0, 0, em * LIST_ITEM_MARKER_SIZE_COEFFICIENT, em * LIST_ITEM_MARKER_SIZE_COEFFICIENT
                                                               );
                            PdfDocument pdfDocument   = context.GetPdfDocument();
                            Color       gradientColor = gradientBuilder.BuildColor(formBBox, null, pdfDocument);
                            if (gradientColor != null)
                            {
                                imageXObject = new PdfFormXObject(formBBox);
                                new PdfCanvas((PdfFormXObject)imageXObject, context.GetPdfDocument()).SetColor(gradientColor, true).Rectangle
                                    (formBBox).Fill();
                            }
                        }
                    }
                    catch (StyledXMLParserException) {
                        LOGGER.Warn(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.INVALID_GRADIENT_DECLARATION, listStyleImageStr
                                                             ));
                    }
                }
                else
                {
                    imageXObject = context.GetResourceResolver().RetrieveImageExtended(CssUtils.ExtractUrl(listStyleImageStr));
                }
                if (imageXObject != null)
                {
                    Image image = null;
                    if (imageXObject is PdfImageXObject)
                    {
                        image = new Image((PdfImageXObject)imageXObject);
                    }
                    else
                    {
                        if (imageXObject is PdfFormXObject)
                        {
                            image = new Image((PdfFormXObject)imageXObject);
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    element.SetProperty(Property.LIST_SYMBOL, image);
                    element.SetProperty(Property.LIST_SYMBOL_INDENT, 5);
                }
            }
        }