Esempio n. 1
0
        //no instance required
        /// <summary>Calculates width and height values for image with a given area params.</summary>
        /// <param name="image">
        /// the
        /// <see cref="iText.Layout.Properties.BackgroundImage"/>
        /// width and height of which you want to calculate
        /// </param>
        /// <param name="areaWidth">width of the area of this images</param>
        /// <param name="areaHeight">height of the area of this images</param>
        /// <returns>array of two Float values. NOTE that first value defines width, second defines height.</returns>
        /// <seealso cref="iText.Layout.Properties.BackgroundSize"/>
        public static float[] CalculateBackgroundImageSize(BackgroundImage image, float areaWidth, float areaHeight
                                                           )
        {
            bool           isGradient = image.GetLinearGradientBuilder() != null;
            BackgroundSize size;

            if (!isGradient && image.GetBackgroundSize().IsSpecificSize())
            {
                size = CalculateBackgroundSizeForArea(image, areaWidth, areaHeight);
            }
            else
            {
                size = image.GetBackgroundSize();
            }
            UnitValue width  = size.GetBackgroundWidthSize();
            UnitValue height = size.GetBackgroundHeightSize();

            float?[] widthAndHeight = new float?[2];
            if (width != null && width.GetValue() >= 0)
            {
                bool needScale = !isGradient && height == null;
                CalculateBackgroundWidth(width, areaWidth, needScale, image, widthAndHeight);
            }
            if (height != null && height.GetValue() >= 0)
            {
                bool needScale = !isGradient && width == null;
                CalculateBackgroundHeight(height, areaHeight, needScale, image, widthAndHeight);
            }
            SetDefaultSizeIfNull(widthAndHeight, areaWidth, areaHeight, image, isGradient);
            return(new float[] { (float)widthAndHeight[0], (float)widthAndHeight[1] });
        }
 private static void ApplyBackgroundWidth(String widthValue, BackgroundImage image, float em, float rem)
 {
     if (CommonCssConstants.BACKGROUND_SIZE_VALUES.Contains(widthValue))
     {
         if (widthValue.Equals(CommonCssConstants.CONTAIN))
         {
             image.GetBackgroundSize().SetBackgroundSizeToContain();
         }
         if (widthValue.Equals(CommonCssConstants.COVER))
         {
             image.GetBackgroundSize().SetBackgroundSizeToCover();
         }
         return;
     }
     image.GetBackgroundSize().SetBackgroundSizeToValues(CssDimensionParsingUtils.ParseLengthValueToPt(widthValue
                                                                                                       , em, rem), null);
 }
        public virtual void CalculateSizeWithCoverAndImageWeightMoreThatHeightTest()
        {
            PdfImageXObject xObject         = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
            BackgroundImage backgroundImage = new BackgroundImage(xObject);

            backgroundImage.GetBackgroundSize().SetBackgroundSizeToCover();
            float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
                                                                                                300f);
            NUnit.Framework.Assert.AreEqual(new float[] { 533.3333f, 300f }, widthAndHeight);
        }
        private static void ApplyBackgroundWidthHeight(IList <String> backgroundSizeValues, BackgroundImage image,
                                                       float em, float rem)
        {
            String widthValue = backgroundSizeValues[0];

            if (CommonCssConstants.BACKGROUND_SIZE_VALUES.Contains(widthValue))
            {
                if (widthValue.Equals(CommonCssConstants.AUTO))
                {
                    UnitValue height = CssDimensionParsingUtils.ParseLengthValueToPt(backgroundSizeValues[1], em, rem);
                    if (height != null)
                    {
                        image.GetBackgroundSize().SetBackgroundSizeToValues(null, height);
                    }
                }
                return;
            }
            image.GetBackgroundSize().SetBackgroundSizeToValues(CssDimensionParsingUtils.ParseLengthValueToPt(backgroundSizeValues
                                                                                                              [0], em, rem), CssDimensionParsingUtils.ParseLengthValueToPt(backgroundSizeValues[1], em, rem));
        }
        public virtual void CalculateSizeWithContainPropertyTest()
        {
            PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
                                                                                  ));
            BackgroundImage backgroundImage = new BackgroundImage(xObject);

            backgroundImage.GetBackgroundSize().SetBackgroundSizeToContain();
            float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
                                                                                                300f);
            NUnit.Framework.Assert.AreEqual(new float[] { 200f, 200.000015f }, widthAndHeight);
        }
Esempio n. 6
0
        private static BackgroundSize CalculateBackgroundSizeForArea(BackgroundImage image, float areaWidth, float
                                                                     areaHeight)
        {
            double widthDifference  = areaWidth / image.GetImageWidth();
            double heightDifference = areaHeight / image.GetImageHeight();

            if (image.GetBackgroundSize().IsCover())
            {
                return(CreateSizeWithMaxValueSide(widthDifference > heightDifference));
            }
            else
            {
                if (image.GetBackgroundSize().IsContain())
                {
                    return(CreateSizeWithMaxValueSide(widthDifference < heightDifference));
                }
                else
                {
                    return(new BackgroundSize());
                }
            }
        }