예제 #1
0
 /// <summary>Sets linearGradientBuilder.</summary>
 /// <remarks>
 /// Sets linearGradientBuilder.
 /// <para />
 /// Makes image null as far as we can't have them both. It also makes background-repeat: no-repeat.
 /// </remarks>
 /// <param name="linearGradientBuilder">
 ///
 /// <see cref="iText.Kernel.Colors.Gradients.AbstractLinearGradientBuilder"/>
 /// to be set.
 /// </param>
 /// <returns>
 /// this
 /// <see cref="Builder"/>.
 /// </returns>
 public virtual BackgroundImage.Builder SetLinearGradientBuilder(AbstractLinearGradientBuilder linearGradientBuilder
                                                                 )
 {
     this.linearGradientBuilder = linearGradientBuilder;
     this.repeat = new BackgroundRepeat(BackgroundRepeat.BackgroundRepeatValue.NO_REPEAT);
     this.image  = null;
     return(this);
 }
        private void GeneratePdf(PdfDocument pdfDocument, AffineTransform transform,
                                 AbstractLinearGradientBuilder gradientBuilder, Rectangle rectangleToDraw)
        {
            PdfCanvas canvas = new PdfCanvas(pdfDocument.AddNewPage());

            if (transform != null)
            {
                canvas.ConcatMatrix(transform);
            }

            canvas.SetFillColor(gradientBuilder.BuildColor(rectangleToDraw, transform, pdfDocument))
            .SetStrokeColor(ColorConstants.BLACK)
            .Rectangle(rectangleToDraw)
            .FillStroke();
        }
예제 #3
0
 /// <summary>
 /// Creates a new
 /// <see cref="BackgroundImage"/>
 /// instance.
 /// </summary>
 /// <param name="image">
 /// background-image property.
 /// <see cref="iText.Kernel.Pdf.Xobject.PdfXObject"/>
 /// instance.
 /// </param>
 /// <param name="repeat">
 /// background-repeat property.
 /// <see cref="BackgroundRepeat"/>
 /// instance.
 /// </param>
 /// <param name="position">
 /// background-position property.
 /// <see cref="BackgroundPosition"/>
 /// instance.
 /// </param>
 /// <param name="backgroundSize">
 /// background-size property.
 /// <see cref="BackgroundSize"/>
 /// instance.
 /// </param>
 /// <param name="linearGradientBuilder">
 /// background-image property.
 /// <see cref="iText.Kernel.Colors.Gradients.AbstractLinearGradientBuilder"/>
 /// instance.
 /// </param>
 /// <param name="blendMode">
 /// the image's blend mode.
 /// <see cref="BlendMode"/>
 /// instance.
 /// </param>
 /// <param name="clip">
 /// background-clip property.
 /// <see cref="BackgroundBox"/>
 /// instance.
 /// </param>
 /// <param name="origin">
 /// background-origin property.
 /// <see cref="BackgroundBox"/>
 /// instance.
 /// </param>
 private BackgroundImage(PdfXObject image, BackgroundRepeat repeat, BackgroundPosition position, BackgroundSize
                         backgroundSize, AbstractLinearGradientBuilder linearGradientBuilder, BlendMode blendMode, BackgroundBox
                         clip, BackgroundBox origin)
 {
     this.image = image;
     if (repeat != null)
     {
         this.repeatX = !repeat.IsNoRepeatOnXAxis();
         this.repeatY = !repeat.IsNoRepeatOnYAxis();
     }
     this.repeat                = repeat;
     this.position              = position;
     this.backgroundSize        = backgroundSize;
     this.linearGradientBuilder = linearGradientBuilder;
     if (blendMode != null)
     {
         this.blendMode = blendMode;
     }
     this.backgroundClip   = clip;
     this.backgroundOrigin = origin;
 }
예제 #4
0
        private static void AddStopColors(AbstractLinearGradientBuilder builder, IList <String> argumentsList, int
                                          stopsStartIndex, float emValue, float remValue)
        {
            GradientColorStop lastCreatedStopColor = null;
            int lastStopIndex = argumentsList.Count - 1;

            for (int i = stopsStartIndex; i <= lastStopIndex; ++i)
            {
                String         argument     = argumentsList[i];
                IList <String> elementsList = new List <String>();
                CssDeclarationValueTokenizer       tokenizer = new CssDeclarationValueTokenizer(argument);
                CssDeclarationValueTokenizer.Token nextToken;
                while ((nextToken = tokenizer.GetNextValidToken()) != null)
                {
                    elementsList.Add(nextToken.GetValue());
                }
                // cases: color, color + offset, color + offset + offset, offset (hint)
                if (elementsList.IsEmpty() || elementsList.Count > 3)
                {
                    throw new StyledXMLParserException(MessageFormatUtil.Format(StyledXMLParserException.INVALID_GRADIENT_COLOR_STOP_VALUE
                                                                                , argument));
                }
                if (CssUtils.IsColorProperty(elementsList[0]))
                {
                    float[] rgba = CssUtils.ParseRgbaColor(elementsList[0]);
                    if (elementsList.Count == 1)
                    {
                        UnitValue offset = i == stopsStartIndex ? new UnitValue(UnitValue.PERCENT, 0f) : i == lastStopIndex ? new
                                           UnitValue(UnitValue.PERCENT, 100f) : null;
                        lastCreatedStopColor = CreateStopColor(rgba, offset);
                        builder.AddColorStop(lastCreatedStopColor);
                    }
                    else
                    {
                        for (int j = 1; j < elementsList.Count; ++j)
                        {
                            if (CssUtils.IsNumericValue(elementsList[j]))
                            {
                                // the numeric value is invalid in linear gradient function.
                                // So check it here as parsing method will use the default pt metric
                                throw new StyledXMLParserException(MessageFormatUtil.Format(StyledXMLParserException.INVALID_GRADIENT_COLOR_STOP_VALUE
                                                                                            , argument));
                            }
                            UnitValue offset = CssUtils.ParseLengthValueToPt(elementsList[j], emValue, remValue);
                            if (offset == null)
                            {
                                throw new StyledXMLParserException(MessageFormatUtil.Format(StyledXMLParserException.INVALID_GRADIENT_COLOR_STOP_VALUE
                                                                                            , argument));
                            }
                            lastCreatedStopColor = CreateStopColor(rgba, offset);
                            builder.AddColorStop(lastCreatedStopColor);
                        }
                    }
                }
                else
                {
                    // it should be a color hint case
                    if (elementsList.Count != 1 || lastCreatedStopColor == null || lastCreatedStopColor.GetHintOffsetType() !=
                        GradientColorStop.HintOffsetType.NONE || i == lastStopIndex)
                    {
                        // hint is not a single value, or no color at the beginning,
                        // or two hints in a row, or hint as a last value
                        throw new StyledXMLParserException(MessageFormatUtil.Format(StyledXMLParserException.INVALID_GRADIENT_COLOR_STOP_VALUE
                                                                                    , argument));
                    }
                    UnitValue hint = CssUtils.ParseLengthValueToPt(elementsList[0], emValue, remValue);
                    if (hint == null)
                    {
                        throw new StyledXMLParserException(MessageFormatUtil.Format(StyledXMLParserException.INVALID_GRADIENT_COLOR_STOP_VALUE
                                                                                    , argument));
                    }
                    if (hint.GetUnitType() == UnitValue.PERCENT)
                    {
                        lastCreatedStopColor.SetHint(hint.GetValue() / 100, GradientColorStop.HintOffsetType.RELATIVE_ON_GRADIENT);
                    }
                    else
                    {
                        lastCreatedStopColor.SetHint(hint.GetValue(), GradientColorStop.HintOffsetType.ABSOLUTE_ON_GRADIENT);
                    }
                }
            }
        }
예제 #5
0
 public BackgroundImage(AbstractLinearGradientBuilder linearGradientBuilder)
 {
     this.linearGradientBuilder = linearGradientBuilder;
     this.repeatX = false;
     this.repeatY = false;
 }
예제 #6
0
 /// <summary>Sets image.</summary>
 /// <remarks>
 /// Sets image.
 /// <para />
 /// Makes linearGradientBuilder null as far as we can't have them both.
 /// </remarks>
 /// <param name="image">
 ///
 /// <see cref="iText.Kernel.Pdf.Xobject.PdfXObject"/>
 /// to be set.
 /// </param>
 /// <returns>
 /// this
 /// <see cref="Builder"/>.
 /// </returns>
 public virtual BackgroundImage.Builder SetImage(PdfXObject image)
 {
     this.image = image;
     this.linearGradientBuilder = null;
     return(this);
 }
예제 #7
0
 public BackgroundImage(AbstractLinearGradientBuilder linearGradientBuilder, BlendMode blendMode)
     : this(null, new BackgroundRepeat(BackgroundRepeat.BackgroundRepeatValue.NO_REPEAT), new BackgroundPosition
                (), new BackgroundSize(), linearGradientBuilder, blendMode, BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX
            )
 {
 }
예제 #8
0
 public BackgroundImage(AbstractLinearGradientBuilder linearGradientBuilder)
     : this(linearGradientBuilder, DEFAULT_BLEND_MODE)
 {
 }