Example #1
0
        public virtual void OrphansPropertyOneInheritedOneRedefined()
        {
            IList <IElement> elements = ConvertToElements("orphansPropertyOneInheritedOneRedefined");
            Div div = (Div)elements[0];

            NUnit.Framework.Assert.IsNotNull(div);
            ParagraphOrphansControl divOrphansControl = div.GetProperty <ParagraphOrphansControl>(Property.ORPHANS_CONTROL
                                                                                                  );

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

            NUnit.Framework.Assert.IsNotNull(paragraph);
            ParagraphOrphansControl paragraphOrphansControl = paragraph.GetProperty <ParagraphOrphansControl>(Property.
                                                                                                              ORPHANS_CONTROL);

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

            NUnit.Framework.Assert.IsNotNull(anotherParagraph);
            ParagraphOrphansControl anotherParagraphOrphansControl = anotherParagraph.GetProperty <ParagraphOrphansControl
                                                                                                   >(Property.ORPHANS_CONTROL);

            NUnit.Framework.Assert.IsNotNull(anotherParagraphOrphansControl);
            NUnit.Framework.Assert.AreEqual(4, anotherParagraphOrphansControl.GetMinOrphans());
        }
Example #2
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());
        }
Example #3
0
        public virtual void OrphansPropertyPresent()
        {
            IList <IElement> elements  = ConvertToElements("orphansPropertyPresent");
            Paragraph        paragraph = (Paragraph)elements[0];

            NUnit.Framework.Assert.IsNotNull(paragraph);
            ParagraphOrphansControl orphansControl = paragraph.GetProperty <ParagraphOrphansControl>(Property.ORPHANS_CONTROL
                                                                                                     );

            NUnit.Framework.Assert.IsNotNull(orphansControl);
            NUnit.Framework.Assert.AreEqual(3, orphansControl.GetMinOrphans());
        }
        /// <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));
        }
        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));
            }
        }
Example #6
0
 /// <summary>
 /// Sets orphans restriction on a
 /// <see cref="Paragraph"/>.
 /// </summary>
 /// <param name="orphansControl">
 /// an instance of
 /// <see cref="iText.Layout.Properties.ParagraphOrphansControl"/>.
 /// </param>
 /// <returns>
 /// this
 /// <see cref="Paragraph"/>
 /// instance.
 /// </returns>
 public virtual iText.Layout.Element.Paragraph SetOrphansControl(ParagraphOrphansControl orphansControl)
 {
     SetProperty(Property.ORPHANS_CONTROL, orphansControl);
     return(this);
 }
        public virtual void PrematureCallOfHandleViolatedOrphans()
        {
            ParagraphOrphansControl orphansControl = new ParagraphOrphansControl(2);

            orphansControl.HandleViolatedOrphans(new ParagraphRenderer(new Paragraph()), "");
        }