Esempio n. 1
0
        /// <summary>
        /// This method tries to calculate min-max-width of rotated element using heuristics
        /// of
        /// <see cref="iText.Layout.Minmaxwidth.RotationMinMaxWidth.Calculate(double, double, iText.Layout.Minmaxwidth.MinMaxWidth)
        ///     "/>.
        /// </summary>
        /// <remarks>
        /// This method tries to calculate min-max-width of rotated element using heuristics
        /// of
        /// <see cref="iText.Layout.Minmaxwidth.RotationMinMaxWidth.Calculate(double, double, iText.Layout.Minmaxwidth.MinMaxWidth)
        ///     "/>.
        /// This method may call
        /// <see cref="IRenderer.Layout(iText.Layout.Layout.LayoutContext)"/>
        /// once in best case
        /// (if the width is set on element, or if we are really lucky) and three times in worst case.
        /// </remarks>
        /// <param name="minMaxWidth">the minMaxWidth of NOT rotated renderer</param>
        /// <param name="renderer">the actual renderer</param>
        /// <returns>minMaxWidth of rotated renderer or original value in case rotated value can not be calculated, or renderer isn't rotated.
        ///     </returns>
        public static MinMaxWidth CountRotationMinMaxWidth(MinMaxWidth minMaxWidth, AbstractRenderer renderer)
        {
            RotationUtils.PropertiesBackup backup = new RotationUtils.PropertiesBackup(renderer);
            float?rotation = backup.StoreFloatProperty(Property.ROTATION_ANGLE);

            if (rotation != null)
            {
                float angle = (float)rotation;
                //This width results in more accurate values for min-width calculations.
                float        layoutWidth  = minMaxWidth.GetMaxWidth() + MinMaxWidthUtils.GetEps();
                LayoutResult layoutResult = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(layoutWidth,
                                                                                                              AbstractRenderer.INF))));
                if (layoutResult.GetOccupiedArea() != null)
                {
                    Rectangle layoutBBox = layoutResult.GetOccupiedArea().GetBBox();
                    if (MinMaxWidthUtils.IsEqual(minMaxWidth.GetMinWidth(), minMaxWidth.GetMaxWidth()))
                    {
                        backup.RestoreProperty(Property.ROTATION_ANGLE);
                        float rotatedWidth = (float)RotationMinMaxWidth.CalculateRotatedWidth(layoutBBox, angle);
                        return(new MinMaxWidth(rotatedWidth, rotatedWidth, 0));
                    }
                    double area = layoutResult.GetOccupiedArea().GetBBox().GetWidth() * layoutResult.GetOccupiedArea().GetBBox
                                      ().GetHeight();
                    RotationMinMaxWidth rotationMinMaxWidth = RotationMinMaxWidth.Calculate(angle, area, minMaxWidth);
                    float?rotatedMinWidth = GetLayoutRotatedWidth(renderer, (float)rotationMinMaxWidth.GetMinWidthOrigin(), layoutBBox
                                                                  , angle);
                    if (rotatedMinWidth != null)
                    {
                        if (rotatedMinWidth > rotationMinMaxWidth.GetMaxWidth())
                        {
                            rotationMinMaxWidth.SetChildrenMinWidth((float)rotatedMinWidth);
                            float?rotatedMaxWidth = GetLayoutRotatedWidth(renderer, (float)rotationMinMaxWidth.GetMaxWidthOrigin(), layoutBBox
                                                                          , angle);
                            if (rotatedMaxWidth != null && rotatedMaxWidth > rotatedMinWidth)
                            {
                                rotationMinMaxWidth.SetChildrenMaxWidth((float)rotatedMaxWidth);
                            }
                            else
                            {
                                rotationMinMaxWidth.SetChildrenMaxWidth((float)rotatedMinWidth);
                            }
                        }
                        else
                        {
                            rotationMinMaxWidth.SetChildrenMinWidth((float)rotatedMinWidth);
                        }
                        backup.RestoreProperty(Property.ROTATION_ANGLE);
                        return(rotationMinMaxWidth);
                    }
                }
            }
            backup.RestoreProperty(Property.ROTATION_ANGLE);
            return(minMaxWidth);
        }
Esempio n. 2
0
        public WidthDimensionContainer(CssContextNode node, float maxWidth, IRenderer renderer, float additionalWidthFix
                                       )
        {
            String width = node.GetStyles().Get(CssConstants.WIDTH);

            if (width != null && !width.Equals(CssConstants.AUTO))
            {
                dimension = ParseDimension(node, width, maxWidth, additionalWidthFix);
            }
            minDimension = GetMinWidth(node, maxWidth, additionalWidthFix);
            maxDimension = GetMaxWidth(node, maxWidth, additionalWidthFix);
            if (!IsAutoDimension())
            {
                maxContentDimension = dimension;
                maxContentDimension = dimension;
            }
            else
            {
                if (renderer is BlockRenderer)
                {
                    MinMaxWidth minMaxWidth = ((BlockRenderer)renderer).GetMinMaxWidth();
                    maxContentDimension = minMaxWidth.GetMaxWidth();
                    minContentDimension = minMaxWidth.GetMinWidth();
                }
            }
        }
        public WidthDimensionContainer(CssContextNode node, float maxWidth, IRenderer renderer, float additionalWidthFix
                                       )
        {
            String width = node.GetStyles().Get(CssConstants.WIDTH);

            if (width != null && !CssConstants.AUTO.Equals(width))
            {
                dimension = ParseDimension(node, width, maxWidth, additionalWidthFix);
            }
            minDimension = GetMinWidth(node, maxWidth, additionalWidthFix);
            maxDimension = GetMaxWidth(node, maxWidth, additionalWidthFix);
            if (!IsAutoDimension())
            {
                // According to point 3 of paragraph "5.3.2.3. Handling min-width and max-width" of the specification
                // maxContentDimension and minContentDimension will always be equal
                maxContentDimension = dimension;
                minContentDimension = dimension;
            }
            else
            {
                if (renderer is BlockRenderer)
                {
                    MinMaxWidth minMaxWidth = ((BlockRenderer)renderer).GetMinMaxWidth();
                    maxContentDimension = minMaxWidth.GetMaxWidth();
                    minContentDimension = minMaxWidth.GetMinWidth();
                }
            }
        }
Esempio n. 4
0
        internal static float?AdjustFloatedBlockLayoutBox(AbstractRenderer renderer, Rectangle parentBBox, float?
                                                          blockWidth, IList <Rectangle> floatRendererAreas, FloatPropertyValue?floatPropertyValue, OverflowPropertyValue?
                                                          overflowX)
        {
            renderer.SetProperty(Property.HORIZONTAL_ALIGNMENT, null);
            float floatElemWidth;
            bool  overflowFit = AbstractRenderer.IsOverflowFit(overflowX);

            if (blockWidth != null)
            {
                floatElemWidth = (float)blockWidth + AbstractRenderer.CalculateAdditionalWidth(renderer);
                if (overflowFit && floatElemWidth > parentBBox.GetWidth())
                {
                    floatElemWidth = parentBBox.GetWidth();
                }
            }
            else
            {
                MinMaxWidth minMaxWidth = CalculateMinMaxWidthForFloat(renderer, floatPropertyValue);
                float       maxWidth    = minMaxWidth.GetMaxWidth();
                if (maxWidth > parentBBox.GetWidth())
                {
                    maxWidth = parentBBox.GetWidth();
                }
                if (!overflowFit && minMaxWidth.GetMinWidth() > parentBBox.GetWidth())
                {
                    maxWidth = minMaxWidth.GetMinWidth();
                }
                floatElemWidth = maxWidth + AbstractRenderer.EPS;
                blockWidth     = maxWidth - minMaxWidth.GetAdditionalWidth() + AbstractRenderer.EPS;
            }
            AdjustBlockAreaAccordingToFloatRenderers(floatRendererAreas, parentBBox, floatElemWidth, FloatPropertyValue
                                                     .LEFT.Equals(floatPropertyValue));
            return(blockWidth);
        }
Esempio n. 5
0
        public virtual void DivWithRotatedPercentImage()
        {
            String          outFileName  = destinationFolder + "divRotatedPercentImage.pdf";
            String          cmpFileName  = sourceFolder + "cmp_divRotatedPercentImage.pdf";
            PdfDocument     pdfDocument  = new PdfDocument(new PdfWriter(outFileName));
            Document        doc          = new Document(pdfDocument);
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "itis.jpg"));

            iText.Layout.Element.Image img = new iText.Layout.Element.Image(imageXObject).SetRotationAngle(Math.PI * 3
                                                                                                           / 8);
            Div d = new Div().Add(img).SetBorder(new SolidBorder(ColorConstants.BLUE, 2f)).SetMarginBottom(10);

            iText.Layout.Element.Image imgPercent = new iText.Layout.Element.Image(imageXObject).SetWidth(UnitValue.CreatePercentValue
                                                                                                              (50)).SetRotationAngle(Math.PI * 3 / 8);
            Div         dPercent = new Div().Add(imgPercent).SetBorder(new SolidBorder(ColorConstants.BLUE, 2f));
            MinMaxWidth result   = ((AbstractRenderer)d.CreateRendererSubTree().SetParent(doc.GetRenderer())).GetMinMaxWidth
                                       ();

            d.SetWidth(ToEffectiveWidth(d, result.GetMinWidth()));
            MinMaxWidth resultPercent = ((AbstractRenderer)dPercent.CreateRendererSubTree().SetParent(doc.GetRenderer(
                                                                                                          ))).GetMinMaxWidth();

            dPercent.SetWidth(ToEffectiveWidth(dPercent, resultPercent.GetMaxWidth()));
            doc.Add(d);
            doc.Add(dPercent);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Esempio n. 6
0
        public virtual void CalculateColumnWidthsNotPointValue()
        {
            PdfDocument pdfDoc    = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            Document    doc       = new Document(pdfDoc);
            Rectangle   layoutBox = new Rectangle(0, 0, 1000, 100);
            Table       table     = new Table(UnitValue.CreatePercentArray(new float[] { 10, 10, 80 }));

            // Set margins and paddings in percents, which is not expected
            table.SetProperty(Property.MARGIN_RIGHT, UnitValue.CreatePercentValue(7));
            table.SetProperty(Property.MARGIN_LEFT, UnitValue.CreatePercentValue(7));
            table.SetProperty(Property.PADDING_RIGHT, UnitValue.CreatePercentValue(7));
            table.SetProperty(Property.PADDING_LEFT, UnitValue.CreatePercentValue(7));
            // Fill the table somehow. The layout area is wide enough to calculate the widths as expected
            for (int i = 0; i < 3; i++)
            {
                table.AddCell("Hello");
            }
            // Create a TableRenderer, the instance of which will be used to test the application of margins and paddings
            TableRenderer tableRenderer = (TableRenderer)table.CreateRendererSubTree().SetParent(doc.GetRenderer());

            tableRenderer.bordersHandler = (TableBorders) new SeparatedTableBorders(tableRenderer.rows, 3, tableRenderer
                                                                                    .GetBorders(), 0);
            tableRenderer.ApplyMarginsAndPaddingsAndCalculateColumnWidths(layoutBox);
            // Specify that the render is not original in order not to recalculate the column widths
            tableRenderer.isOriginalNonSplitRenderer = false;
            MinMaxWidth minMaxWidth = tableRenderer.GetMinMaxWidth();

            // TODO DEVSIX-3676: currently margins and paddings are still applied as if they are in points. After the mentioned ticket is fixed, the expected values should be updated.
            NUnit.Framework.Assert.AreEqual(minMaxWidth.GetMaxWidth(), 332.46f, 0.001);
            NUnit.Framework.Assert.AreEqual(minMaxWidth.GetMinWidth(), 332.46f, 0.001);
        }
 public virtual void UpdateMinMaxWidth(MinMaxWidth minMaxWidth)
 {
     if (minMaxWidth != null)
     {
         UpdateMaxChildWidth(minMaxWidth.GetMaxWidth());
         UpdateMinChildWidth(minMaxWidth.GetMinWidth());
     }
 }
Esempio n. 8
0
        public virtual void ImageWidthTest02()
        {
            PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "Desert.jpg"));

            iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject);
            ImageRenderer renderer           = new ImageRenderer(image);

            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePointValue(50));
            MinMaxWidth minMaxWidth = renderer.GetMinMaxWidth();

            NUnit.Framework.Assert.AreEqual(50.0, minMaxWidth.GetMaxWidth(), EPSILON);
            NUnit.Framework.Assert.AreEqual(0.0, minMaxWidth.GetMaxWidth() - minMaxWidth.GetMinWidth(), EPSILON);
            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(50));
            minMaxWidth = renderer.GetMinMaxWidth();
            NUnit.Framework.Assert.AreEqual(1024.0, minMaxWidth.GetMaxWidth(), EPSILON);
            image.SetProperty(Property.MAX_HEIGHT, UnitValue.CreatePointValue(100f));
            minMaxWidth = renderer.GetMinMaxWidth();
            NUnit.Framework.Assert.AreEqual(100.0 * 1024.0 / 768.0, minMaxWidth.GetMaxWidth(), EPSILON);
            image    = new iText.Layout.Element.Image(xObject);
            renderer = new ImageRenderer(image);
            image.SetProperty(Property.MIN_WIDTH, UnitValue.CreatePointValue(2000));
            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePointValue(3000));
            minMaxWidth = renderer.GetMinMaxWidth();
            NUnit.Framework.Assert.AreEqual(2000.0, minMaxWidth.GetMaxWidth(), EPSILON);
            NUnit.Framework.Assert.AreEqual(0.0, minMaxWidth.GetMaxWidth() - minMaxWidth.GetMinWidth(), EPSILON);
            image.SetProperty(Property.MIN_HEIGHT, UnitValue.CreatePointValue(100f));
            image.SetProperty(Property.HEIGHT, UnitValue.CreatePointValue(100f));
            minMaxWidth = renderer.GetMinMaxWidth();
            NUnit.Framework.Assert.AreEqual(100.0 * 1024.0 / 768.0, minMaxWidth.GetMaxWidth(), EPSILON);
        }
        public virtual void MinMaxWidthWithOneRenderer()
        {
            PdfDocument  pdfDocument  = new PdfDocument(new PdfWriter(new MemoryStream()));
            Document     document     = new Document(pdfDocument);
            TextRenderer textRenderer = new TextRenderer(new Text(""));

            textRenderer.SetParent(document.GetRenderer());
            textRenderer.SetProperty(Property.FONT, PdfFontFactory.CreateFont(THAI_FONT, PdfEncodings.IDENTITY_H));
            textRenderer.SetText(THAI_TEXT);
            textRenderer.SetSpecialScriptsWordBreakPoints(new List <int>(JavaUtil.ArraysAsList(3, 8, 10, 12, 15, 20, 23
                                                                                               , 26, 28, 30, 36)));
            MinMaxWidth minMaxWidth = textRenderer.GetMinMaxWidth();

            NUnit.Framework.Assert.IsTrue(minMaxWidth.GetMinWidth() < minMaxWidth.GetMaxWidth());
        }
        public virtual void OverflowWrapAnywhereProperty()
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream()));

            pdfDoc.AddNewPage();
            Document     doc = new Document(pdfDoc);
            RootRenderer documentRenderer = doc.GetRenderer();
            Text         text             = new Text("wow");

            text.SetProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.ANYWHERE);
            TextRenderer textRenderer = (TextRenderer)text.GetRenderer();

            textRenderer.SetParent(documentRenderer);
            MinMaxWidth minMaxWidth = textRenderer.GetMinMaxWidth();

            NUnit.Framework.Assert.IsTrue(minMaxWidth.GetMinWidth() < minMaxWidth.GetMaxWidth());
        }
        public virtual void SpecialScriptsWordBreakPointsSplit()
        {
            PdfDocument  pdfDocument  = new PdfDocument(new PdfWriter(new MemoryStream()));
            Document     document     = new Document(pdfDocument);
            TextRenderer textRenderer = new TextRenderer(new Text(""));

            textRenderer.SetProperty(Property.FONT, PdfFontFactory.CreateFont(THAI_FONT, PdfEncodings.IDENTITY_H));
            textRenderer.SetText(THAI_TEXT);
            textRenderer.SetSpecialScriptsWordBreakPoints(new List <int>(JavaUtil.ArraysAsList(3, 8, 10, 12, 15, 20, 23
                                                                                               , 26, 28, 30, 36)));
            LineRenderer lineRenderer = new LineRenderer();

            lineRenderer.SetParent(document.GetRenderer());
            lineRenderer.AddChild(textRenderer);
            MinMaxWidth  minMaxWidth  = lineRenderer.GetMinMaxWidth();
            float        width        = minMaxWidth.GetMinWidth() + minMaxWidth.GetMaxWidth() / 2;
            LayoutResult layoutResult = lineRenderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, 500
                                                                                                              ))));
            IRenderer lineSplitRenderer = layoutResult.GetSplitRenderer();

            NUnit.Framework.Assert.IsNotNull(lineSplitRenderer);
            NUnit.Framework.Assert.IsNotNull(lineSplitRenderer.GetChildRenderers());
            NUnit.Framework.Assert.IsTrue(lineSplitRenderer.GetChildRenderers()[0] is TextRenderer);
            TextRenderer textSplitRenderer = (TextRenderer)lineSplitRenderer.GetChildRenderers()[0];

            NUnit.Framework.Assert.IsNotNull(textSplitRenderer.GetSpecialScriptsWordBreakPoints());
            IRenderer lineOverflowRenderer = layoutResult.GetOverflowRenderer();

            NUnit.Framework.Assert.IsNotNull(lineOverflowRenderer);
            NUnit.Framework.Assert.IsNotNull(lineOverflowRenderer.GetChildRenderers());
            NUnit.Framework.Assert.IsTrue(lineOverflowRenderer.GetChildRenderers()[0] is TextRenderer);
            TextRenderer textOverflowRenderer = (TextRenderer)lineOverflowRenderer.GetChildRenderers()[0];

            NUnit.Framework.Assert.IsNotNull(textOverflowRenderer.GetSpecialScriptsWordBreakPoints());
            int textSplitRendererTextLength = textSplitRenderer.text.ToString().Length;

            foreach (int specialScriptsWordBreakPoint in textSplitRenderer.GetSpecialScriptsWordBreakPoints())
            {
                NUnit.Framework.Assert.IsTrue(specialScriptsWordBreakPoint <= textSplitRendererTextLength);
            }
            foreach (int specialScriptsWordBreakPoint in textOverflowRenderer.GetSpecialScriptsWordBreakPoints())
            {
                NUnit.Framework.Assert.IsTrue(specialScriptsWordBreakPoint > textSplitRendererTextLength && specialScriptsWordBreakPoint
                                              <= textOverflowRenderer.text.Size());
            }
        }
Esempio n. 12
0
        /// <summary>This method tries to calculate width of not rotated renderer, so after rotation it fits availableWidth.
        ///     </summary>
        /// <remarks>
        /// This method tries to calculate width of not rotated renderer, so after rotation it fits availableWidth.
        /// This method uses heuristics of
        /// <see cref="iText.Layout.Minmaxwidth.RotationMinMaxWidth.Calculate(double, double, iText.Layout.Minmaxwidth.MinMaxWidth, double)
        ///     "/>.
        /// It doesn't take into account any of height properties of renderer or height of layoutArea.
        /// The minMaxWidth calculations and initial layout may take long time, but they won't be called if the renderer have width property.
        /// </remarks>
        /// <param name="availableWidth">the width of layoutArea</param>
        /// <param name="renderer">the actual renderer</param>
        /// <returns>
        /// the width that should be set as width of layout area to properly layout element, or fallback to
        /// <see cref="AbstractRenderer.RetrieveWidth(float)"/>
        /// in case it can not be calculated, or renderer isn't rotated.
        /// </returns>
        public static float?RetrieveRotatedLayoutWidth(float availableWidth, AbstractRenderer renderer)
        {
            RotationUtils.PropertiesBackup backup = new RotationUtils.PropertiesBackup(renderer);
            float?rotation = backup.StoreFloatProperty(Property.ROTATION_ANGLE);

            if (rotation != null && renderer.GetProperty <UnitValue>(Property.WIDTH) == null)
            {
                float angle = (float)rotation;
                backup.StoreProperty <UnitValue>(Property.HEIGHT);
                backup.StoreProperty <UnitValue>(Property.MIN_HEIGHT);
                backup.StoreProperty <UnitValue>(Property.MAX_HEIGHT);
                MinMaxWidth minMaxWidth = renderer.GetMinMaxWidth();
                //Using this width for initial layout helps in case of small elements. They may have more free spaces but it's more likely they fit.
                float        length       = (minMaxWidth.GetMaxWidth() + minMaxWidth.GetMinWidth()) / 2 + MinMaxWidthUtils.GetEps();
                LayoutResult layoutResult = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(length, AbstractRenderer
                                                                                                              .INF))));
                backup.RestoreProperty(Property.HEIGHT);
                backup.RestoreProperty(Property.MIN_HEIGHT);
                backup.RestoreProperty(Property.MAX_HEIGHT);
                Rectangle additions = new Rectangle(0, 0);
                renderer.ApplyPaddings(additions, true);
                renderer.ApplyBorderBox(additions, true);
                renderer.ApplyMargins(additions, true);
                if (layoutResult.GetOccupiedArea() != null)
                {
                    double area = layoutResult.GetOccupiedArea().GetBBox().GetWidth() * layoutResult.GetOccupiedArea().GetBBox
                                      ().GetHeight();
                    RotationMinMaxWidth result = RotationMinMaxWidth.Calculate(angle, area, minMaxWidth, availableWidth);
                    if (result != null)
                    {
                        backup.RestoreProperty(Property.ROTATION_ANGLE);
                        if (result.GetMaxWidthHeight() > result.GetMinWidthHeight())
                        {
                            return((float)(result.GetMinWidthOrigin() - additions.GetWidth() + MinMaxWidthUtils.GetEps()));
                        }
                        else
                        {
                            return((float)(result.GetMaxWidthOrigin() - additions.GetWidth() + MinMaxWidthUtils.GetEps()));
                        }
                    }
                }
            }
            backup.RestoreProperty(Property.ROTATION_ANGLE);
            return(renderer.RetrieveWidth(availableWidth));
        }