/**
         * Transforms the given Rectangle into the image coordinate system which is [0,1]x[0,1] by default
         */
        private Rectangle TransformIntersection(Matrix imageCTM, Rectangle rect)
        {
            AffineTransform t = new AffineTransform(imageCTM[Matrix.I11], imageCTM[Matrix.I12],
                                                    imageCTM[Matrix.I21], imageCTM[Matrix.I22],
                                                    imageCTM[Matrix.I31], imageCTM[Matrix.I32]);
            Point2D p1;
            Point2D p2;
            Point2D p3;
            Point2D p4;

            try
            {
                p1 = t.InverseTransform(new Point(rect.Left, rect.Bottom), null);
                p2 = t.InverseTransform(new Point(rect.Left, rect.Top), null);
                p3 = t.InverseTransform(new Point(rect.Right, rect.Bottom), null);
                p4 = t.InverseTransform(new Point(rect.Right, rect.Top), null);
            }
            catch (InvalidOperationException e)
            {
                throw new SystemException(e.Message, e);
            }

            return(GetRectangle(p1, p2, p3, p4));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds the
        /// <see cref="iText.Kernel.Colors.Color"/>
        /// object representing the linear gradient with specified configuration
        /// that fills the target bounding box.
        /// </summary>
        /// <param name="targetBoundingBox">the bounding box to be filled in current space</param>
        /// <param name="contextTransform">
        /// the transformation from the base coordinates space into
        /// the current space. The
        /// <see langword="null"/>
        /// value is valid and can be used
        /// if there is no transformation from base coordinates to current space
        /// specified, or it is equal to identity transformation.
        /// </param>
        /// <param name="document">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// for which the linear gradient would be built.
        /// </param>
        /// <returns>
        /// the constructed
        /// <see cref="iText.Kernel.Colors.Color"/>
        /// or
        /// <see langword="null"/>
        /// if no color to be applied
        /// or base gradient vector has been specified
        /// </returns>
        public virtual Color BuildColor(Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument
                                        document)
        {
            // TODO: DEVSIX-4136 the document argument would be required for opaque gradients (as we would need to create a mask form xObject)
            Point[] baseCoordinatesVector = GetGradientVector(targetBoundingBox, contextTransform);
            if (baseCoordinatesVector == null || this.stops.IsEmpty())
            {
                // Can not create gradient color with 0 stops or null coordinates vector
                return(null);
            }
            // evaluate actual coordinates and transformation
            AffineTransform shadingTransform = new AffineTransform();

            if (contextTransform != null)
            {
                shadingTransform.Concatenate(contextTransform);
            }
            AffineTransform gradientTransformation = GetCurrentSpaceToGradientVectorSpaceTransformation(targetBoundingBox
                                                                                                        , contextTransform);

            if (gradientTransformation != null)
            {
                try {
                    if (targetBoundingBox != null)
                    {
                        targetBoundingBox = Rectangle.CalculateBBox(JavaUtil.ArraysAsList(gradientTransformation.InverseTransform(
                                                                                              new Point(targetBoundingBox.GetLeft(), targetBoundingBox.GetBottom()), null), gradientTransformation.InverseTransform
                                                                                              (new Point(targetBoundingBox.GetLeft(), targetBoundingBox.GetTop()), null), gradientTransformation.InverseTransform
                                                                                              (new Point(targetBoundingBox.GetRight(), targetBoundingBox.GetBottom()), null), gradientTransformation
                                                                                          .InverseTransform(new Point(targetBoundingBox.GetRight(), targetBoundingBox.GetTop()), null)));
                    }
                    shadingTransform.Concatenate(gradientTransformation);
                }
                catch (NoninvertibleTransformException) {
                    LogManager.GetLogger(GetType()).Error(iText.IO.LogMessageConstant.UNABLE_TO_INVERT_GRADIENT_TRANSFORMATION
                                                          );
                }
            }
            PdfShading.Axial axial = CreateAxialShading(baseCoordinatesVector, this.stops, this.spreadMethod, targetBoundingBox
                                                        );
            if (axial == null)
            {
                return(null);
            }
            PdfPattern.Shading shading = new PdfPattern.Shading(axial);
            if (!shadingTransform.IsIdentity())
            {
                double[] matrix = new double[6];
                shadingTransform.GetMatrix(matrix);
                shading.SetMatrix(new PdfArray(matrix));
            }
            return(new PatternColor(shading));
        }