Ejemplo n.º 1
0
        public virtual void OrphansWidowsAwareAndDirectLayoutProduceSameResult()
        {
            PdfDocument pdfDocument                  = new PdfDocument(new PdfWriter(new MemoryStream()));
            Document    document                     = new Document(pdfDocument);
            int         minAllowedWidows             = 3;
            int         overflowedToNextPageLinesNum = 5;
            Paragraph   widowsParagraph              = new Paragraph(OrphansWidowsTestUtil.PARA_TEXT);
            IRenderer   paragraphRenderer            = widowsParagraph.CreateRendererSubTree().SetParent(document.GetRenderer());
            Rectangle   effectiveArea                = document.GetPageEffectiveArea(pdfDocument.GetDefaultPageSize());
            float       linesHeight                  = OrphansWidowsTestUtil.CalculateHeightForLinesNum(document, widowsParagraph, effectiveArea
                                                                                                        .GetWidth(), overflowedToNextPageLinesNum, false);
            Rectangle layoutAreaRect = new Rectangle(effectiveArea).SetHeight(linesHeight + OrphansWidowsTestUtil.LINES_SPACE_EPS
                                                                              );
            LayoutContext          layoutContext = new LayoutContext(new LayoutArea(1, layoutAreaRect));
            LayoutResult           noWidowsControlLayoutResult = paragraphRenderer.Layout(layoutContext);
            ParagraphWidowsControl widowsControl = new ParagraphWidowsControl(minAllowedWidows, 1, false);

            widowsParagraph.SetWidowsControl(widowsControl);
            LayoutResult widowsControlLayoutResult = paragraphRenderer.Layout(layoutContext);

            // toString() comparison is used since it contains report on status, areaBreak and occupiedArea
            NUnit.Framework.Assert.AreEqual(noWidowsControlLayoutResult.ToString(), widowsControlLayoutResult.ToString
                                                ());
            ParagraphRenderer firstSplitRenderer  = (ParagraphRenderer)noWidowsControlLayoutResult.GetSplitRenderer();
            ParagraphRenderer secondSplitRenderer = (ParagraphRenderer)widowsControlLayoutResult.GetSplitRenderer();

            NUnit.Framework.Assert.IsNotNull(firstSplitRenderer);
            NUnit.Framework.Assert.IsNotNull(secondSplitRenderer);
            NUnit.Framework.Assert.AreEqual(firstSplitRenderer.ToString(), secondSplitRenderer.ToString());
            NUnit.Framework.Assert.IsNotNull(noWidowsControlLayoutResult.GetOverflowRenderer());
            NUnit.Framework.Assert.IsNotNull(widowsControlLayoutResult.GetOverflowRenderer());
        }
Ejemplo n.º 2
0
        public virtual void WidowsPropertyOneInheritedOneRedefined()
        {
            IList <IElement> elements = ConvertToElements("widowsPropertyOneInheritedOneRedefined");
            Div div = (Div)elements[0];

            NUnit.Framework.Assert.IsNotNull(div);
            ParagraphWidowsControl divWidowsControl = div.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(divWidowsControl);
            NUnit.Framework.Assert.AreEqual(3, divWidowsControl.GetMinWidows());
            Paragraph paragraph = (Paragraph)div.GetChildren()[0];

            NUnit.Framework.Assert.IsNotNull(paragraph);
            ParagraphWidowsControl paragraphWidowsControl = paragraph.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                           );

            NUnit.Framework.Assert.IsNotNull(paragraphWidowsControl);
            NUnit.Framework.Assert.AreEqual(3, paragraphWidowsControl.GetMinWidows());
            Paragraph anotherParagraph = (Paragraph)div.GetChildren()[1];

            NUnit.Framework.Assert.IsNotNull(anotherParagraph);
            ParagraphWidowsControl anotherParagraphWidowsControl = anotherParagraph.GetProperty <ParagraphWidowsControl
                                                                                                 >(Property.WIDOWS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(anotherParagraphWidowsControl);
            NUnit.Framework.Assert.AreEqual(4, anotherParagraphWidowsControl.GetMinWidows());
        }
Ejemplo n.º 3
0
        public virtual void OrphansWidowsParallelInheritance()
        {
            IList <IElement> elements = ConvertToElements("orphansWidowsParallelInheritance");
            Div level1Div             = (Div)elements[0];

            NUnit.Framework.Assert.IsNotNull(level1Div);
            ParagraphOrphansControl level1DivOrphansControl = level1Div.GetProperty <ParagraphOrphansControl>(Property.
                                                                                                              ORPHANS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(level1DivOrphansControl);
            NUnit.Framework.Assert.AreEqual(3, level1DivOrphansControl.GetMinOrphans());
            ParagraphWidowsControl level1DivWidowsControl = level1Div.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                           );

            NUnit.Framework.Assert.IsNull(level1DivWidowsControl);
            Div level2Div = (Div)level1Div.GetChildren()[0];

            NUnit.Framework.Assert.IsNotNull(level2Div);
            ParagraphOrphansControl level2DivOrphansControl = level2Div.GetProperty <ParagraphOrphansControl>(Property.
                                                                                                              ORPHANS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(level2DivOrphansControl);
            NUnit.Framework.Assert.AreEqual(3, level2DivOrphansControl.GetMinOrphans());
            ParagraphWidowsControl level2DivWidowsControl = level2Div.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                           );

            NUnit.Framework.Assert.IsNotNull(level2DivWidowsControl);
            NUnit.Framework.Assert.AreEqual(5, level2DivWidowsControl.GetMinWidows());
            Paragraph paragraph1 = (Paragraph)level2Div.GetChildren()[0];

            NUnit.Framework.Assert.IsNotNull(paragraph1);
            ParagraphOrphansControl paragraph1OrphansControl = paragraph1.GetProperty <ParagraphOrphansControl>(Property
                                                                                                                .ORPHANS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(paragraph1OrphansControl);
            NUnit.Framework.Assert.AreEqual(3, paragraph1OrphansControl.GetMinOrphans());
            ParagraphWidowsControl paragraph1WidowsControl = paragraph1.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                             );

            NUnit.Framework.Assert.IsNotNull(paragraph1WidowsControl);
            NUnit.Framework.Assert.AreEqual(5, paragraph1WidowsControl.GetMinWidows());
            Paragraph paragraph2 = (Paragraph)level2Div.GetChildren()[1];

            NUnit.Framework.Assert.IsNotNull(paragraph2);
            ParagraphOrphansControl paragraph2OrphansControl = paragraph2.GetProperty <ParagraphOrphansControl>(Property
                                                                                                                .ORPHANS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(paragraph2OrphansControl);
            NUnit.Framework.Assert.AreEqual(4, paragraph2OrphansControl.GetMinOrphans());
            ParagraphWidowsControl paragraph2WidowsControl = paragraph2.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                             );

            NUnit.Framework.Assert.IsNotNull(paragraph2WidowsControl);
            NUnit.Framework.Assert.AreEqual(5, paragraph2WidowsControl.GetMinWidows());
        }
Ejemplo n.º 4
0
        public virtual void WidowsPropertyPresent()
        {
            IList <IElement> elements  = ConvertToElements("widowsPropertyPresent");
            Paragraph        paragraph = (Paragraph)elements[0];

            NUnit.Framework.Assert.IsNotNull(paragraph);
            ParagraphWidowsControl widowsControl = paragraph.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                  );

            NUnit.Framework.Assert.IsNotNull(widowsControl);
            NUnit.Framework.Assert.AreEqual(3, widowsControl.GetMinWidows());
        }
        /// <summary><inheritDoc/></summary>
        public override LayoutResult Layout(LayoutContext layoutContext)
        {
            ParagraphOrphansControl orphansControl = this.GetProperty <ParagraphOrphansControl>(Property.ORPHANS_CONTROL
                                                                                                );
            ParagraphWidowsControl widowsControl = this.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL);

            if (orphansControl != null || widowsControl != null)
            {
                return(OrphansWidowsLayoutHelper.OrphansWidowsAwareLayout(this, layoutContext, orphansControl, widowsControl
                                                                          ));
            }
            return(DirectLayout(layoutContext));
        }
Ejemplo n.º 6
0
            public override void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                       )
            {
                base.Apply(context, stylesContainer, tagWorker);
                Paragraph paragraph = (Paragraph)tagWorker.GetElementResult();
                ParagraphWidowsControl widowsControl = paragraph.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                      );

                if (widowsControl != null)
                {
                    widowsControl.SetMinAllowedWidows(widowsControl.GetMinWidows(), 3, true);
                    paragraph.SetProperty(Property.WIDOWS_CONTROL, widowsControl);
                }
            }
Ejemplo n.º 7
0
        public virtual void AlterOrphansWidowsTest()
        {
            ConverterProperties      converterProperties = new ConverterProperties();
            DefaultCssApplierFactory cssApplierFactory   = new OrphansWidowsUnitTest.CustomBlockCssApplierFactory();

            converterProperties.SetCssApplierFactory(cssApplierFactory);
            IList <IElement> elements = HtmlConverter.ConvertToElements(new FileStream(sourceFolder + "orphansWidows.html"
                                                                                       , FileMode.Open, FileAccess.Read), converterProperties);
            Div div = (Div)elements[0];

            NUnit.Framework.Assert.IsNotNull(div);
            Paragraph paragraph = (Paragraph)div.GetChildren()[0];

            NUnit.Framework.Assert.IsNotNull(paragraph);
            ParagraphWidowsControl paragraphWidowsControl = paragraph.GetProperty <ParagraphWidowsControl>(Property.WIDOWS_CONTROL
                                                                                                           );

            NUnit.Framework.Assert.IsNotNull(paragraphWidowsControl);
            NUnit.Framework.Assert.AreEqual(3, paragraphWidowsControl.GetMaxLinesToMove());
            NUnit.Framework.Assert.IsTrue(paragraphWidowsControl.IsOverflowOnWidowsViolation());
        }
        internal static LayoutResult OrphansWidowsAwareLayout(ParagraphRenderer renderer, LayoutContext context, ParagraphOrphansControl
                                                              orphansControl, ParagraphWidowsControl widowsControl)
        {
            OrphansWidowsLayoutHelper.OrphansWidowsLayoutAttempt layoutAttempt = AttemptLayout(renderer, context, context
                                                                                               .GetArea().Clone());
            if (context.IsClippedHeight() || renderer.IsPositioned() || layoutAttempt.attemptResult.GetStatus() != LayoutResult
                .PARTIAL || layoutAttempt.attemptResult.GetSplitRenderer() == null)
            {
                return(HandleAttemptAsSuccessful(layoutAttempt, context));
            }
            ParagraphRenderer splitRenderer = (ParagraphRenderer)layoutAttempt.attemptResult.GetSplitRenderer();
            bool orphansViolation           = orphansControl != null && splitRenderer != null && splitRenderer.GetLines().Count
                                              < orphansControl.GetMinOrphans() && !renderer.IsFirstOnRootArea();
            bool forcedPlacement = true.Equals(renderer.GetPropertyAsBoolean(Property.FORCED_PLACEMENT));

            if (orphansViolation && forcedPlacement)
            {
                orphansControl.HandleViolatedOrphans(splitRenderer, "Ignored orphans constraint due to forced placement.");
            }
            if (orphansViolation && !forcedPlacement)
            {
                layoutAttempt = null;
            }
            else
            {
                if (widowsControl != null && splitRenderer != null && layoutAttempt.attemptResult.GetOverflowRenderer() !=
                    null)
                {
                    ParagraphRenderer overflowRenderer = (ParagraphRenderer)layoutAttempt.attemptResult.GetOverflowRenderer();
                    // Excessively big value to check if widows constraint is violated;
                    // Make this value less in order to improve performance if you are sure
                    // that min number of widows will fit in this height. E.g. A4 page height is 842.
                    int        simulationHeight = 3500;
                    LayoutArea simulationArea   = new LayoutArea(context.GetArea().GetPageNumber(), context.GetArea().GetBBox().
                                                                 Clone().SetHeight(simulationHeight));
                    // collapsingMarginsInfo might affect available space, which is redundant in case we pass arbitrary space.
                    // floatedRendererAreas list on new area is considered empty. We don't know if there will be any, however their presence in any case will result in more widows, not less.
                    // clippedHeight is undefined for the next area, because it is defined by overflow part of the paragraph parent.
                    //               Even if it will be set to true in actual overflow-part layouting, stealing lines approach will result in
                    //               giving bigger part of MAX-HEIGHT to the overflow part and resulting in bigger number of widows, which is better.
                    //               However for possible other approaches which change content "length" (like word/char spacing adjusts),
                    //               if in actual overflow-part layouting clippedHeight will be true, those widows fixing attempts will result in worse results.
                    LayoutContext simulationContext = new LayoutContext(simulationArea);
                    LayoutResult  simulationResult  = overflowRenderer.DirectLayout(simulationContext);
                    if (simulationResult.GetStatus() == LayoutResult.FULL)
                    {
                        // simulationHeight is excessively big in order to allow to layout all of the content remaining in overflowRenderer:
                        // this way after all of the remaining content is layouted we can check if it has led to widows violation.
                        // To make this analysis possible, we expect to get result FULL.
                        // if result is PARTIAL: means that simulationHeight value isn't big enough to layout all of the content remaining in overflowRenderer.
                        // In this case we assume that widows aren't violated since the amount of the lines to fit the simulatedHeight is expected to be very large.
                        // if result is NOTHING: unexpected result, limitation of simulation approach. Retry again with forced placement set.
                        int extraWidows = widowsControl.GetMinWidows() - overflowRenderer.GetLines().Count;
                        if (extraWidows > 0)
                        {
                            int extraLinesToMove = orphansControl != null?Math.Max(orphansControl.GetMinOrphans(), 1) : 1;

                            if (extraWidows <= widowsControl.GetMaxLinesToMove() && splitRenderer.GetLines().Count - extraWidows >= extraLinesToMove
                                )
                            {
                                LineRenderer lastLine        = splitRenderer.GetLines()[splitRenderer.GetLines().Count - 1];
                                LineRenderer lastLineToLeave = splitRenderer.GetLines()[splitRenderer.GetLines().Count - extraWidows - 1];
                                float        d = lastLineToLeave.GetOccupiedArea().GetBBox().GetY() - lastLine.GetOccupiedArea().GetBBox().GetY()
                                                 - AbstractRenderer.EPS;
                                Rectangle smallerBBox = new Rectangle(context.GetArea().GetBBox());
                                smallerBBox.DecreaseHeight(d);
                                smallerBBox.MoveUp(d);
                                LayoutArea smallerAvailableArea = new LayoutArea(context.GetArea().GetPageNumber(), smallerBBox);
                                layoutAttempt = AttemptLayout(renderer, context, smallerAvailableArea);
                            }
                            else
                            {
                                if (forcedPlacement || renderer.IsFirstOnRootArea() || !widowsControl.IsOverflowOnWidowsViolation())
                                {
                                    if (forcedPlacement)
                                    {
                                        widowsControl.HandleViolatedWidows(overflowRenderer, "forced placement");
                                    }
                                    else
                                    {
                                        widowsControl.HandleViolatedWidows(overflowRenderer, "inability to fix it");
                                    }
                                }
                                else
                                {
                                    layoutAttempt = null;
                                }
                            }
                        }
                    }
                }
            }
            if (layoutAttempt != null)
            {
                return(HandleAttemptAsSuccessful(layoutAttempt, context));
            }
            else
            {
                return(new LayoutResult(LayoutResult.NOTHING, null, null, renderer));
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Sets widows restriction on a
 /// <see cref="Paragraph"/>.
 /// </summary>
 /// <param name="widowsControl">
 /// an instance of
 /// <see cref="iText.Layout.Properties.ParagraphWidowsControl"/>.
 /// </param>
 /// <returns>
 /// this
 /// <see cref="Paragraph"/>
 /// instance.
 /// </returns>
 public virtual iText.Layout.Element.Paragraph SetWidowsControl(ParagraphWidowsControl widowsControl)
 {
     SetProperty(Property.WIDOWS_CONTROL, widowsControl);
     return(this);
 }
Ejemplo n.º 10
0
        public virtual void PrematureCallOfHandleViolatedWidows()
        {
            ParagraphWidowsControl widowsControl = new ParagraphWidowsControl(2, 1, false);

            widowsControl.HandleViolatedWidows(new ParagraphRenderer(new Paragraph()), "");
        }