public override void Draw(DrawContext drawContext)
            {
                // Fictitiously layout the renderer and find out, how much space does it require
                IRenderer pr = content.CreateRendererSubTree().SetParent(this);

                LayoutResult textArea = pr.Layout(new LayoutContext(
                                                      new LayoutArea(0, new Rectangle(GetOccupiedAreaBBox().GetWidth(), 1000))));

                float spaceNeeded = textArea.GetOccupiedArea().GetBBox().GetHeight();

                Console.WriteLine("The content requires {0} pt whereas the height is {1} pt.",
                                  spaceNeeded, GetOccupiedAreaBBox().GetHeight());

                float offset = (GetOccupiedAreaBBox().GetHeight() - textArea.GetOccupiedArea()
                                .GetBBox().GetHeight()) / 2;

                Console.WriteLine("The difference is {0} pt; we'll need an offset of {1} pt.",
                                  -2f * offset, offset);

                PdfFormXObject xObject = new PdfFormXObject(new Rectangle(GetOccupiedAreaBBox().GetWidth(),
                                                                          GetOccupiedAreaBBox().GetHeight()));

                Canvas layoutCanvas = new Canvas(new PdfCanvas(xObject, drawContext.GetDocument()),
                                                 new Rectangle(0, offset, GetOccupiedAreaBBox().GetWidth(), spaceNeeded));

                layoutCanvas.Add(content);

                drawContext.GetCanvas().AddXObjectAt(xObject, occupiedArea.GetBBox().GetLeft(),
                                                     occupiedArea.GetBBox().GetBottom());
            }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private static Image GetWatermarkedImage(PdfDocument pdfDocument, Image img)
        {
            float width  = img.GetImageScaledWidth();
            float height = img.GetImageScaledHeight();

            Table table = InitTable(width);

            TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();

            renderer.SetParent(new DocumentRenderer(new Document(pdfDocument)));

            // Simulate the positioning of the renderer to find out how much space the table will occupy.
            LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(
                                                                        1, new Rectangle(10000, 10000))));

            PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));

            new Canvas(template, pdfDocument)
            .Add(img)
            .Close();

            float left   = 0;
            float bottom = height - result.GetOccupiedArea().GetBBox().GetHeight();

            new Canvas(template, pdfDocument)
            .Add(table.SetFixedPosition(left, bottom, width))
            .Close();

            return(new Image(template));
        }
Ejemplo n.º 4
0
        //Get actual width of element based on it's layout. May use occupied are of layout result of initial layout for time saving.
        private static float?GetLayoutRotatedWidth(AbstractRenderer renderer, float availableWidth, Rectangle previousBBox
                                                   , double angle)
        {
            if (MinMaxWidthUtils.IsEqual(availableWidth, previousBBox.GetWidth()))
            {
                return((float)RotationMinMaxWidth.CalculateRotatedWidth(previousBBox, angle));
            }
            LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(availableWidth + MinMaxWidthUtils
                                                                                                    .GetEps(), AbstractRenderer.INF))));

            if (result.GetOccupiedArea() != null)
            {
                return((float)RotationMinMaxWidth.CalculateRotatedWidth(result.GetOccupiedArea().GetBBox(), angle));
            }
            return(null);
        }
Ejemplo n.º 5
0
        private void ManipulatePdf(string dest)
        {
            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();

            for (int i = 0; i < 10; i++)
            {
                Cell cell;

                if (i == 9)
                {
                    cell = new Cell().Add(new Paragraph("Two\nLines"));
                }
                else
                {
                    cell = new Cell().Add(new Paragraph(i.ToString()));
                }

                table.AddCell(cell);
            }

            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            // Fictitiously layout the element to find out, how much space it takes
            IRenderer    tableRenderer     = table.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult tableLayoutResult = tableRenderer.Layout(new LayoutContext(
                                                                      new LayoutArea(0, new Rectangle(550 + 72, 1000))));

            pdfDoc.SetDefaultPageSize(new PageSize(550 + 72,
                                                   tableLayoutResult.GetOccupiedArea().GetBBox().GetHeight() + 72));

            doc.Add(table);

            doc.Close();
        }
Ejemplo n.º 6
0
        public override LayoutResult Layout(LayoutContext layoutContext)
        {
            LayoutResult errorResult = InitializeListSymbols(layoutContext);

            if (errorResult != null)
            {
                return(errorResult);
            }
            LayoutResult result = base.Layout(layoutContext);

            // cannot place even the first ListItemRenderer
            if (true.Equals(GetPropertyAsBoolean(Property.FORCED_PLACEMENT)) && null != result.GetCauseOfNothing())
            {
                if (LayoutResult.FULL == result.GetStatus())
                {
                    result = CorrectListSplitting(this, null, result.GetCauseOfNothing(), result.GetOccupiedArea());
                }
                else
                {
                    if (LayoutResult.PARTIAL == result.GetStatus())
                    {
                        result = CorrectListSplitting(result.GetSplitRenderer(), result.GetOverflowRenderer(), result.GetCauseOfNothing
                                                          (), result.GetOccupiedArea());
                    }
                }
            }
            return(result);
        }
        internal HeightDimensionContainer(CssContextNode pmbcNode, float width, float maxHeight, IRenderer renderer
                                          , float additionalWidthFix)
        {
            String height = pmbcNode.GetStyles().Get(CssConstants.HEIGHT);

            if (height != null && !CssConstants.AUTO.Equals(height))
            {
                dimension = ParseDimension(pmbcNode, height, maxHeight, additionalWidthFix);
            }
            minDimension = GetMinHeight(pmbcNode, maxHeight, additionalWidthFix);
            maxDimension = GetMaxHeight(pmbcNode, maxHeight, additionalWidthFix);
            if (!IsAutoDimension())
            {
                maxContentDimension = dimension;
                maxContentDimension = dimension;
            }
            else
            {
                LayoutArea    layoutArea     = new LayoutArea(1, new Rectangle(0, 0, width, infHeight));
                LayoutContext minimalContext = new LayoutContext(layoutArea);
                LayoutResult  quickLayout    = renderer.Layout(minimalContext);
                if (quickLayout.GetStatus() != LayoutResult.NOTHING)
                {
                    maxContentDimension = quickLayout.GetOccupiedArea().GetBBox().GetHeight();
                    minContentDimension = maxContentDimension;
                }
            }
        }
        public virtual void MinMaxWidthEqualsActualMarginsBordersPaddings()
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new MemoryStream()));
            Document    document    = new Document(pdfDocument);
            Text        ranText     = new Text("ran");

            ranText.SetProperty(Property.MARGIN_LEFT, new UnitValue(UnitValue.POINT, 8f));
            ranText.SetProperty(Property.MARGIN_RIGHT, new UnitValue(UnitValue.POINT, 10f));
            ranText.SetProperty(Property.BORDER_RIGHT, new SolidBorder(3));
            ranText.SetProperty(Property.PADDING_RIGHT, new UnitValue(UnitValue.POINT, 13f));
            TextRenderer ran     = new TextRenderer(ranText);
            Text         domText = new Text("dom");

            domText.SetProperty(Property.MARGIN_LEFT, new UnitValue(UnitValue.POINT, 17f));
            domText.SetProperty(Property.BORDER_LEFT, new SolidBorder(4));
            domText.SetProperty(Property.PADDING_LEFT, new UnitValue(UnitValue.POINT, 12f));
            domText.SetProperty(Property.MARGIN_RIGHT, new UnitValue(UnitValue.POINT, 2f));
            TextRenderer dom          = new TextRenderer(domText);
            LayoutArea   layoutArea   = new LayoutArea(1, new Rectangle(AbstractRenderer.INF, AbstractRenderer.INF));
            LineRenderer lineRenderer = new LineRenderer();

            lineRenderer.SetParent(document.GetRenderer());
            lineRenderer.AddChild(ran);
            lineRenderer.AddChild(dom);
            float        countedMinWidth = lineRenderer.GetMinMaxWidth().GetMinWidth();
            LayoutResult result          = lineRenderer.Layout(new LayoutContext(layoutArea));

            NUnit.Framework.Assert.AreEqual(result.GetOccupiedArea().GetBBox().GetWidth(), countedMinWidth, 0.0001);
        }
Ejemplo n.º 9
0
        protected LayoutArea GetParagraphArea(Paragraph p, int pageIndex)
        {
            IRenderer    paragraphRenderer = p.CreateRendererSubTree();
            LayoutResult result            = paragraphRenderer.SetParent(document.GetRenderer()).
                                             Layout(new LayoutContext(new LayoutArea(pageIndex, new Rectangle(100, 1000))));

            return(result.GetOccupiedArea());
        }
Ejemplo n.º 10
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));
        }
Ejemplo n.º 11
0
        public static float CalculateHeightForLinesNum(Document doc, Paragraph p, float width, float linesNum, bool
                                                       orphans)
        {
            ParagraphRenderer renderer  = (ParagraphRenderer)p.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult      layoutRes = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, 100000))
                                                                            ));
            float lineHeight = layoutRes.GetOccupiedArea().GetBBox().GetHeight() / renderer.GetLines().Count;
            float height     = lineHeight * linesNum;

            if (orphans)
            {
                return(height);
            }
            else
            {
                return(layoutRes.GetOccupiedArea().GetBBox().GetHeight() - height);
            }
        }
        public virtual void LayoutTest()
        {
            AreaBreakRenderer areaBreakRenderer = new AreaBreakRenderer(new AreaBreak());
            LayoutResult      layoutResult      = areaBreakRenderer.Layout(new LayoutContext(null));

            NUnit.Framework.Assert.AreEqual(LayoutResult.NOTHING, layoutResult.GetStatus());
            NUnit.Framework.Assert.IsNull(layoutResult.GetOccupiedArea());
            NUnit.Framework.Assert.IsNull(layoutResult.GetSplitRenderer());
            NUnit.Framework.Assert.IsNull(layoutResult.GetOverflowRenderer());
            NUnit.Framework.Assert.AreEqual(areaBreakRenderer, layoutResult.GetCauseOfNothing());
            NUnit.Framework.Assert.AreEqual(areaBreakRenderer.areaBreak, layoutResult.GetAreaBreak());
        }
        private float GetParagraafHeight(Paragraph paragraaf, Document doc, int width)
        {
            // Create renderer tree
            IRenderer paragraphRenderer = paragraaf.CreateRendererSubTree();
            // Do not forget setParent(). Set the dimensions of the viewport as needed
            LayoutResult layoutResult = paragraphRenderer
                                        .SetParent(doc.GetRenderer())
                                        .Layout(new LayoutContext(new LayoutArea(1, new iText.Kernel.Geom.Rectangle(width, 1000))));

            // LayoutResult#getOccupiedArea() contains the information you need
            return(layoutResult.GetOccupiedArea().GetBBox().GetHeight());
        }
Ejemplo n.º 14
0
        public static float CalculateHeightForLinesNumKeepTogetherCaseSpecific(Document doc, Paragraph p, float width
                                                                               , float height, float linesNum)
        {
            ParagraphRenderer renderer  = (ParagraphRenderer)p.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult      layoutRes = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, 10000)))
                                                          );
            int   allLinesCount = renderer.GetLines().Count;
            float lineHeight    = layoutRes.GetOccupiedArea().GetBBox().GetHeight() / allLinesCount;

            renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, height))));
            int linesWithinOnePageCount = renderer.GetLines().Count;

            return((allLinesCount - linesWithinOnePageCount - linesNum) * lineHeight);
        }
Ejemplo n.º 15
0
            public TableHeaderEventHandler(Document doc)
            {
                this.doc = doc;
                InitTable();

                TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();

                renderer.SetParent(new DocumentRenderer(doc));

                // Simulate the positioning of the renderer to find out how much space the header table will occupy.
                LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.A4)));

                tableHeight = result.GetOccupiedArea().GetBBox().GetHeight();
            }
Ejemplo n.º 16
0
        public virtual void InlineBlockWithBigMinWidth01()
        {
            Document     dummyDocument       = CreateDocument();
            LineRenderer lineRenderer        = (LineRenderer) new LineRenderer().SetParent(dummyDocument.GetRenderer());
            Div          div                 = new Div().SetMinWidth(2000).SetHeight(100);
            DivRenderer  inlineBlockRenderer = (DivRenderer)div.CreateRendererSubTree();

            lineRenderer.AddChild(inlineBlockRenderer);
            LayoutResult result = lineRenderer.Layout(new LayoutContext(CreateLayoutArea(1000, 1000)));

            // In case there is an inline block child with large min-width, the inline block child will be force placed (not layouted properly)
            NUnit.Framework.Assert.AreEqual(LayoutResult.FULL, result.GetStatus());
            NUnit.Framework.Assert.AreEqual(0, result.GetOccupiedArea().GetBBox().GetHeight(), EPS);
            NUnit.Framework.Assert.AreEqual(true, inlineBlockRenderer.GetPropertyAsBoolean(Property.FORCED_PLACEMENT));
        }
Ejemplo n.º 17
0
 private void UpdateCurrentAreaAndProcessRenderer(IRenderer renderer, IList <IRenderer> resultRenderers, LayoutResult
                                                  result)
 {
     if (currentArea != null)
     {
         currentArea.GetBBox().SetHeight(currentArea.GetBBox().GetHeight() - result.GetOccupiedArea().GetBBox().GetHeight
                                             ());
         currentArea.SetEmptyArea(false);
         if (renderer != null)
         {
             ProcessRenderer(renderer, resultRenderers);
         }
     }
     if (!immediateFlush)
     {
         childRenderers.AddAll(resultRenderers);
     }
 }
Ejemplo n.º 18
0
 protected internal virtual void ShrinkCurrentAreaAndProcessRenderer(IRenderer renderer, IList <IRenderer> resultRenderers
                                                                     , LayoutResult result)
 {
     if (currentArea != null)
     {
         float resultRendererHeight = result.GetOccupiedArea().GetBBox().GetHeight();
         currentArea.GetBBox().SetHeight(currentArea.GetBBox().GetHeight() - resultRendererHeight);
         if (currentArea.IsEmptyArea() && (resultRendererHeight > 0 || FloatingHelper.IsRendererFloating(renderer))
             )
         {
             currentArea.SetEmptyArea(false);
         }
         ProcessRenderer(renderer, resultRenderers);
     }
     if (!immediateFlush)
     {
         childRenderers.AddAll(resultRenderers);
     }
 }
Ejemplo n.º 19
0
            public override LayoutResult Layout(LayoutContext layoutContext)
            {
                Rectangle area = layoutContext.GetArea().GetBBox();

                LayoutContext context = new LayoutContext(new LayoutArea(layoutContext.GetArea().GetPageNumber(),
                                                                         new Rectangle(area.GetLeft(), area.GetTop() - (float)RetrieveHeight(), area.GetWidth(),
                                                                                       (float)RetrieveHeight())));

                LayoutResult result = base.Layout(context);

                // If content doesn't fit the size of a cell,
                // iTest will still return layout result full as if everything is OK.
                // As a result, the cell's content will be clipped.
                if (LayoutResult.FULL != result.GetStatus())
                {
                    return(new LayoutResult(LayoutResult.FULL, result.GetOccupiedArea(), null, null));
                }

                return(result);
            }
Ejemplo n.º 20
0
        public virtual void NextRendererTest()
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream()));

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

            text.SetNextRenderer(new TextRenderer(text));
            IRenderer     textRenderer1 = text.GetRenderer().SetParent(documentRenderer);
            IRenderer     textRenderer2 = text.GetRenderer().SetParent(documentRenderer);
            LayoutArea    area          = new LayoutArea(1, new Rectangle(100, 100, 100, 100));
            LayoutContext layoutContext = new LayoutContext(area);

            doc.Close();
            LayoutResult result1 = textRenderer1.Layout(layoutContext);
            LayoutResult result2 = textRenderer2.Layout(layoutContext);

            NUnit.Framework.Assert.AreEqual(result1.GetOccupiedArea(), result2.GetOccupiedArea());
        }
Ejemplo n.º 21
0
        private static Rectangle ResolveTableRect(Document doc, Table table)
        {
            Rectangle    pageDimension     = new Rectangle(36, 36, 523, 770);
            IRenderer    tableRenderer     = table.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult tableLayoutResult = tableRenderer.Layout(new LayoutContext(new LayoutArea(0, pageDimension)));

            Rectangle resultRect;

            if (LayoutResult.PARTIAL == tableLayoutResult.GetStatus())
            {
                resultRect = pageDimension;
            }
            else
            {
                Rectangle tableBBox = tableLayoutResult.GetOccupiedArea().GetBBox();
                resultRect = new Rectangle(pageDimension.GetX(), ((tableBBox.GetBottom() + pageDimension.GetX()) / 2),
                                           pageDimension.GetWidth(), tableBBox.GetHeight());
            }

            return(resultRect);
        }
Ejemplo n.º 22
0
        public virtual void LineRendererLayoutInHtmlModeWithLineHeightPropertyNotSet()
        {
            LineRenderer lineRenderer = new LineRenderer();

            lineRenderer.SetParent(CreateDocument().GetRenderer());
            lineRenderer.SetProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
            // Set fonts with different ascent/descent to line and text
            lineRenderer.SetProperty(Property.FONT, PdfFontFactory.CreateFont(StandardFonts.HELVETICA));
            TextRenderer textRenderer = new TextRenderer(new Text("text"));

            textRenderer.SetProperty(Property.FONT, PdfFontFactory.CreateFont(StandardFonts.COURIER));
            lineRenderer.AddChild(textRenderer);
            LayoutResult layoutResLineHeightNotSet = lineRenderer.Layout(new LayoutContext(CreateLayoutArea(1000, 1000
                                                                                                            )));

            lineRenderer.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue());
            LayoutResult layoutResLineHeightNormal = lineRenderer.Layout(new LayoutContext(CreateLayoutArea(1000, 1000
                                                                                                            )));
            Rectangle bboxLineHeightNotSet = layoutResLineHeightNotSet.GetOccupiedArea().GetBBox();
            Rectangle bboxLineHeightNormal = layoutResLineHeightNormal.GetOccupiedArea().GetBBox();

            NUnit.Framework.Assert.IsTrue(bboxLineHeightNotSet.EqualsWithEpsilon(bboxLineHeightNormal));
        }
Ejemplo n.º 23
0
 public override void AddChild(IRenderer renderer)
 {
     base.AddChild(renderer);
     if (currentArea == null)
     {
         UpdateCurrentArea(null);
     }
     // Static layout
     if (currentArea != null && !childRenderers.IsEmpty() && childRenderers[childRenderers.Count - 1] == renderer
         )
     {
         childRenderers.JRemoveAt(childRenderers.Count - 1);
         IList <IRenderer> resultRenderers = new List <IRenderer>();
         LayoutResult      result          = null;
         LayoutArea        storedArea      = null;
         LayoutArea        nextStoredArea  = null;
         while (currentArea != null && renderer != null && (result = renderer.SetParent(this).Layout(new LayoutContext
                                                                                                         (currentArea.Clone()))).GetStatus() != LayoutResult.FULL)
         {
             if (result.GetStatus() == LayoutResult.PARTIAL)
             {
                 if (result.GetOverflowRenderer() is ImageRenderer)
                 {
                     ((ImageRenderer)result.GetOverflowRenderer()).AutoScale(currentArea);
                 }
                 else
                 {
                     ProcessRenderer(result.GetSplitRenderer(), resultRenderers);
                     if (nextStoredArea != null)
                     {
                         currentArea       = nextStoredArea;
                         currentPageNumber = nextStoredArea.GetPageNumber();
                         nextStoredArea    = null;
                     }
                     else
                     {
                         UpdateCurrentArea(result);
                     }
                 }
             }
             else
             {
                 if (result.GetStatus() == LayoutResult.NOTHING)
                 {
                     if (result.GetOverflowRenderer() is ImageRenderer)
                     {
                         if (currentArea.GetBBox().GetHeight() < ((ImageRenderer)result.GetOverflowRenderer()).imageHeight && !currentArea
                             .IsEmptyArea())
                         {
                             UpdateCurrentArea(result);
                         }
                         ((ImageRenderer)result.GetOverflowRenderer()).AutoScale(currentArea);
                     }
                     else
                     {
                         if (currentArea.IsEmptyArea() && !(renderer is AreaBreakRenderer))
                         {
                             if (true.Equals(result.GetOverflowRenderer().GetModelElement().GetProperty <bool?>(Property.KEEP_TOGETHER))
                                 )
                             {
                                 result.GetOverflowRenderer().GetModelElement().SetProperty(Property.KEEP_TOGETHER, false);
                                 ILogger logger = LoggerFactory.GetLogger(typeof(RootRenderer));
                                 logger.Warn(String.Format(LogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA, "KeepTogether property will be ignored."
                                                           ));
                                 if (storedArea != null)
                                 {
                                     nextStoredArea    = currentArea;
                                     currentArea       = storedArea;
                                     currentPageNumber = storedArea.GetPageNumber();
                                 }
                                 storedArea = currentArea;
                             }
                             else
                             {
                                 result.GetOverflowRenderer().SetProperty(Property.FORCED_PLACEMENT, true);
                                 ILogger logger = LoggerFactory.GetLogger(typeof(RootRenderer));
                                 logger.Warn(String.Format(LogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA, ""));
                             }
                             renderer = result.GetOverflowRenderer();
                             continue;
                         }
                         storedArea = currentArea;
                         if (nextStoredArea != null)
                         {
                             currentArea       = nextStoredArea;
                             currentPageNumber = nextStoredArea.GetPageNumber();
                             nextStoredArea    = null;
                         }
                         else
                         {
                             UpdateCurrentArea(result);
                         }
                     }
                 }
             }
             renderer = result.GetOverflowRenderer();
         }
         if (currentArea != null)
         {
             System.Diagnostics.Debug.Assert(result != null && result.GetOccupiedArea() != null);
             currentArea.GetBBox().SetHeight(currentArea.GetBBox().GetHeight() - result.GetOccupiedArea().GetBBox().GetHeight
                                                 ());
             currentArea.SetEmptyArea(false);
             if (renderer != null)
             {
                 ProcessRenderer(renderer, resultRenderers);
             }
         }
         if (!immediateFlush)
         {
             childRenderers.AddAll(resultRenderers);
         }
     }
     else
     {
         if (positionedRenderers.Count > 0 && positionedRenderers[positionedRenderers.Count - 1] == renderer)
         {
             int?positionedPageNumber = renderer.GetProperty <int?>(Property.PAGE_NUMBER);
             if (positionedPageNumber == null)
             {
                 positionedPageNumber = currentPageNumber;
             }
             renderer.SetParent(this).Layout(new LayoutContext(new LayoutArea((int)positionedPageNumber, currentArea.GetBBox
                                                                                  ().Clone())));
             if (immediateFlush)
             {
                 FlushSingleRenderer(renderer);
                 positionedRenderers.JRemoveAt(positionedRenderers.Count - 1);
             }
         }
     }
 }
Ejemplo n.º 24
0
 private void ProcessWaitingKeepWithNextElement(IRenderer renderer)
 {
     if (keepWithNextHangingRenderer != null)
     {
         LayoutArea rest = currentArea.Clone();
         rest.GetBBox().SetHeight(rest.GetBBox().GetHeight() - keepWithNextHangingRendererLayoutResult.GetOccupiedArea
                                      ().GetBBox().GetHeight());
         bool ableToProcessKeepWithNext = false;
         if (renderer.SetParent(this).Layout(new LayoutContext(rest)).GetStatus() != LayoutResult.NOTHING)
         {
             // The area break will not be introduced and we are safe to place everything as is
             ShrinkCurrentAreaAndProcessRenderer(keepWithNextHangingRenderer, new List <IRenderer>(), keepWithNextHangingRendererLayoutResult
                                                 );
             ableToProcessKeepWithNext = true;
         }
         else
         {
             float originalElementHeight = keepWithNextHangingRendererLayoutResult.GetOccupiedArea().GetBBox().GetHeight
                                               ();
             IList <float> trySplitHeightPoints = new List <float>();
             float         delta = 35;
             for (int i = 1; i <= 5 && originalElementHeight - delta * i > originalElementHeight / 2; i++)
             {
                 trySplitHeightPoints.Add(originalElementHeight - delta * i);
             }
             for (int i = 0; i < trySplitHeightPoints.Count && !ableToProcessKeepWithNext; i++)
             {
                 float          curElementSplitHeight       = trySplitHeightPoints[i];
                 RootLayoutArea firstElementSplitLayoutArea = (RootLayoutArea)currentArea.Clone();
                 firstElementSplitLayoutArea.GetBBox().SetHeight(curElementSplitHeight).MoveUp(currentArea.GetBBox().GetHeight
                                                                                                   () - curElementSplitHeight);
                 LayoutResult firstElementSplitLayoutResult = keepWithNextHangingRenderer.SetParent(this).Layout(new LayoutContext
                                                                                                                     (firstElementSplitLayoutArea.Clone()));
                 if (firstElementSplitLayoutResult.GetStatus() == LayoutResult.PARTIAL)
                 {
                     RootLayoutArea storedArea = currentArea;
                     UpdateCurrentAndInitialArea(firstElementSplitLayoutResult);
                     LayoutResult firstElementOverflowLayoutResult = firstElementSplitLayoutResult.GetOverflowRenderer().Layout
                                                                         (new LayoutContext(currentArea.Clone()));
                     if (firstElementOverflowLayoutResult.GetStatus() == LayoutResult.FULL)
                     {
                         LayoutArea secondElementLayoutArea = currentArea.Clone();
                         secondElementLayoutArea.GetBBox().SetHeight(secondElementLayoutArea.GetBBox().GetHeight() - firstElementOverflowLayoutResult
                                                                     .GetOccupiedArea().GetBBox().GetHeight());
                         LayoutResult secondElementLayoutResult = renderer.SetParent(this).Layout(new LayoutContext(secondElementLayoutArea
                                                                                                                    ));
                         if (secondElementLayoutResult.GetStatus() != LayoutResult.NOTHING)
                         {
                             ableToProcessKeepWithNext = true;
                             currentArea       = firstElementSplitLayoutArea;
                             currentPageNumber = firstElementSplitLayoutArea.GetPageNumber();
                             ShrinkCurrentAreaAndProcessRenderer(firstElementSplitLayoutResult.GetSplitRenderer(), new List <IRenderer>(
                                                                     ), firstElementSplitLayoutResult);
                             UpdateCurrentAndInitialArea(firstElementSplitLayoutResult);
                             ShrinkCurrentAreaAndProcessRenderer(firstElementSplitLayoutResult.GetOverflowRenderer(), new List <IRenderer
                                                                                                                                >(), firstElementOverflowLayoutResult);
                         }
                     }
                     if (!ableToProcessKeepWithNext)
                     {
                         currentArea       = storedArea;
                         currentPageNumber = storedArea.GetPageNumber();
                     }
                 }
             }
         }
         if (!ableToProcessKeepWithNext && !currentArea.IsEmptyArea())
         {
             RootLayoutArea storedArea = currentArea;
             UpdateCurrentAndInitialArea(null);
             LayoutResult firstElementLayoutResult = keepWithNextHangingRenderer.SetParent(this).Layout(new LayoutContext
                                                                                                            (currentArea.Clone()));
             if (firstElementLayoutResult.GetStatus() == LayoutResult.FULL)
             {
                 LayoutArea secondElementLayoutArea = currentArea.Clone();
                 secondElementLayoutArea.GetBBox().SetHeight(secondElementLayoutArea.GetBBox().GetHeight() - firstElementLayoutResult
                                                             .GetOccupiedArea().GetBBox().GetHeight());
                 LayoutResult secondElementLayoutResult = renderer.SetParent(this).Layout(new LayoutContext(secondElementLayoutArea
                                                                                                            ));
                 if (secondElementLayoutResult.GetStatus() != LayoutResult.NOTHING)
                 {
                     ableToProcessKeepWithNext = true;
                     ShrinkCurrentAreaAndProcessRenderer(keepWithNextHangingRenderer, new List <IRenderer>(), keepWithNextHangingRendererLayoutResult
                                                         );
                 }
             }
             if (!ableToProcessKeepWithNext)
             {
                 currentArea       = storedArea;
                 currentPageNumber = storedArea.GetPageNumber();
             }
         }
         if (!ableToProcessKeepWithNext)
         {
             ILog logger = LogManager.GetLogger(typeof(RootRenderer));
             logger.Warn(iText.IO.LogMessageConstant.RENDERER_WAS_NOT_ABLE_TO_PROCESS_KEEP_WITH_NEXT);
             ShrinkCurrentAreaAndProcessRenderer(keepWithNextHangingRenderer, new List <IRenderer>(), keepWithNextHangingRendererLayoutResult
                                                 );
         }
         keepWithNextHangingRenderer             = null;
         keepWithNextHangingRendererLayoutResult = null;
     }
 }
Ejemplo n.º 25
0
        // TODO underlying should not be applied
        // https://jira.itextsupport.com/browse/SUP-952
        public override LayoutResult Layout(LayoutContext layoutContext)
        {
            if (!HasOwnProperty(Property.LIST_SYMBOLS_INITIALIZED))
            {
                IList <IRenderer> symbolRenderers = new List <IRenderer>();
                int listItemNum = (int)this.GetProperty <int?>(Property.LIST_START, 1);
                for (int i = 0; i < childRenderers.Count; i++)
                {
                    if (childRenderers[i].GetModelElement() is ListItem)
                    {
                        childRenderers[i].SetParent(this);
                        IRenderer currentSymbolRenderer = MakeListSymbolRenderer(listItemNum++, childRenderers[i]);
                        childRenderers[i].SetParent(null);
                        symbolRenderers.Add(currentSymbolRenderer);
                        LayoutResult listSymbolLayoutResult = currentSymbolRenderer.SetParent(this).Layout(layoutContext);
                        currentSymbolRenderer.SetParent(null);
                        if (listSymbolLayoutResult.GetStatus() != LayoutResult.FULL)
                        {
                            return(new LayoutResult(LayoutResult.NOTHING, null, null, this, listSymbolLayoutResult.GetCauseOfNothing()
                                                    ));
                        }
                    }
                }
                float maxSymbolWidth = 0;
                foreach (IRenderer symbolRenderer in symbolRenderers)
                {
                    maxSymbolWidth = Math.Max(maxSymbolWidth, symbolRenderer.GetOccupiedArea().GetBBox().GetWidth());
                }
                float?symbolIndent = modelElement.GetProperty <float?>(Property.LIST_SYMBOL_INDENT);
                listItemNum = 0;
                foreach (IRenderer childRenderer in childRenderers)
                {
                    childRenderer.DeleteOwnProperty(Property.MARGIN_LEFT);
                    childRenderer.SetProperty(Property.MARGIN_LEFT, childRenderer.GetProperty(Property.MARGIN_LEFT, (float?)0f
                                                                                              ) + maxSymbolWidth + (symbolIndent != null ? symbolIndent : 0f));
                    if (childRenderer.GetModelElement() is ListItem)
                    {
                        IRenderer symbolRenderer_1 = symbolRenderers[listItemNum++];
                        ((ListItemRenderer)childRenderer).AddSymbolRenderer(symbolRenderer_1, maxSymbolWidth);
                    }
                }
            }
            LayoutResult result = base.Layout(layoutContext);

            // cannot place even the first ListItemRenderer
            if (true.Equals(GetPropertyAsBoolean(Property.FORCED_PLACEMENT)) && null != result.GetCauseOfNothing())
            {
                if (LayoutResult.FULL == result.GetStatus())
                {
                    result = CorrectListSplitting(this, null, result.GetCauseOfNothing(), result.GetOccupiedArea());
                }
                else
                {
                    if (LayoutResult.PARTIAL == result.GetStatus())
                    {
                        result = CorrectListSplitting(result.GetSplitRenderer(), result.GetOverflowRenderer(), result.GetCauseOfNothing
                                                          (), result.GetOccupiedArea());
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 26
0
        public static MinMaxWidth CountDefaultMinMaxWidth(IRenderer renderer)
        {
            LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(1, new Rectangle(GetInfWidth(), GetInfHeight
                                                                                                        ()))));

            return(result.GetStatus() == LayoutResult.NOTHING ? new MinMaxWidth() : new MinMaxWidth(0, result.GetOccupiedArea
                                                                                                        ().GetBBox().GetWidth(), 0));
        }