Skip to content

Commit 3744851

Browse files
Eugene BochiloiText-CI
authored andcommitted
Support collapsing margins for flex container
DEVSIX-5137 Autoported commit. Original commit hash: [1bae5b73b]
1 parent 4081707 commit 3744851

File tree

13 files changed

+207
-103
lines changed

13 files changed

+207
-103
lines changed

itext.tests/itext.layout.tests/itext/layout/element/FlexContainerTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,32 @@ public virtual void LinesMaxHeightShouldBeRespectedTest() {
769769
, "diff"));
770770
}
771771

772+
[NUnit.Framework.Test]
773+
public virtual void CollapsingMarginsFlexContainerTest() {
774+
String outFileName = destinationFolder + "collapsingMarginsFlexContainerTest" + testNumber + ".pdf";
775+
String cmpFileName = sourceFolder + "cmp_collapsingMarginsFlexContainerTest" + testNumber + ".pdf";
776+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
777+
Document document = new Document(pdfDocument);
778+
document.SetProperty(Property.COLLAPSING_MARGINS, true);
779+
Div flexContainer = CreateFlexContainer();
780+
flexContainer.SetProperty(Property.MARGIN_TOP, UnitValue.CreatePointValue(50));
781+
flexContainer.SetProperty(Property.BORDER, new SolidBorder(2));
782+
flexContainer.SetProperty(Property.BACKGROUND, new Background(ColorConstants.LIGHT_GRAY));
783+
Div child1 = CreateNewDiv();
784+
child1.SetBackgroundColor(ColorConstants.CYAN);
785+
child1.SetMargin(50);
786+
Div child2 = CreateNewDiv();
787+
child2.SetBackgroundColor(ColorConstants.CYAN);
788+
child2.SetMargin(50);
789+
flexContainer.Add(child1).Add(child2);
790+
Div flexContainersSibling = CreateNewDiv();
791+
flexContainersSibling.SetMarginBottom(40);
792+
document.Add(flexContainersSibling).Add(flexContainer);
793+
document.Close();
794+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
795+
, "diff"));
796+
}
797+
772798
private Div GetFlexContainer(OverflowPropertyValue? overflowX, Style style) {
773799
FlexContainer flexContainer = CreateFlexContainer();
774800
flexContainer.SetBackgroundColor(ColorConstants.GREEN).SetBorderRight(new SolidBorder(60));

itext.tests/itext.layout.tests/itext/layout/renderer/AbstractRendererUnitTest.cs

Lines changed: 109 additions & 93 deletions
Large diffs are not rendered by default.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using iText.IO.Source;
2+
using iText.Kernel.Geom;
3+
using iText.Kernel.Pdf;
4+
using iText.Kernel.Pdf.Canvas;
5+
using iText.Layout.Element;
6+
using iText.Layout.Layout;
7+
using iText.Layout.Properties;
8+
using iText.Test;
9+
using iText.Test.Attributes;
10+
11+
namespace iText.Layout.Renderer {
12+
public class BlockRendererUnitTest : ExtendedITextTest {
13+
[NUnit.Framework.Test]
14+
[LogMessage(iText.IO.LogMessageConstant.PAGE_WAS_FLUSHED_ACTION_WILL_NOT_BE_PERFORMED)]
15+
public virtual void ClippedAreaFlushedPageTest() {
16+
BlockRenderer blockRenderer = new DivRenderer(new Div());
17+
blockRenderer.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.HIDDEN);
18+
blockRenderer.occupiedArea = new LayoutArea(1, new Rectangle(100, 100));
19+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
20+
pdfDocument.AddNewPage();
21+
PdfCanvas pdfCanvas = new PdfCanvas(pdfDocument.AddNewPage());
22+
DrawContext context = new DrawContext(pdfDocument, pdfCanvas);
23+
pdfDocument.GetPage(1).Flush();
24+
blockRenderer.Draw(context);
25+
// This test checks that there is log message and there is no NPE so assertions are not required
26+
NUnit.Framework.Assert.IsTrue(true);
27+
}
28+
}
29+
}

itext/itext.io/itext/io/LogMessageConstant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ public const String IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_BASE_COLOR_SPACE_IN_INDEX
254254

255255
public const String PAGE_TREE_IS_BROKEN_FAILED_TO_RETRIEVE_PAGE = "Page tree is broken. Failed to retrieve page number {0}. Null will be returned.";
256256

257+
public const String PAGE_WAS_FLUSHED_ACTION_WILL_NOT_BE_PERFORMED = "Page was flushed. {0} will not be performed.";
258+
257259
public const String PASSED_PAGE_SHALL_BE_ON_WHICH_CANVAS_WILL_BE_RENDERED = "The page passed to Canvas#enableAutoTagging(PdfPage) method shall be the one on which this canvas will be rendered. However the actual passed PdfPage instance sets not such page. This might lead to creation of malformed PDF document.";
258260

259261
public const String PATH_KEY_IS_PRESENT_VERTICES_WILL_BE_IGNORED = "Path key is present. Vertices will be ignored";

itext/itext.layout/itext/layout/renderer/AbstractRenderer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,11 +1887,11 @@ protected internal virtual void ApplyAction(PdfDocument document) {
18871887
}
18881888

18891889
protected internal virtual void ApplyLinkAnnotation(PdfDocument document) {
1890+
ILog logger = LogManager.GetLogger(typeof(iText.Layout.Renderer.AbstractRenderer));
18901891
PdfLinkAnnotation linkAnnotation = this.GetProperty<PdfLinkAnnotation>(Property.LINK_ANNOTATION);
18911892
if (linkAnnotation != null) {
18921893
int pageNumber = occupiedArea.GetPageNumber();
18931894
if (pageNumber < 1 || pageNumber > document.GetNumberOfPages()) {
1894-
ILog logger = LogManager.GetLogger(typeof(iText.Layout.Renderer.AbstractRenderer));
18951895
String logMessageArg = "Property.LINK_ANNOTATION, which specifies a link associated with this element content area, see com.itextpdf.layout.element.Link.";
18961896
logger.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.UNABLE_TO_APPLY_PAGE_DEPENDENT_PROP_UNKNOWN_PAGE_ON_WHICH_ELEMENT_IS_DRAWN
18971897
, logMessageArg));
@@ -1905,7 +1905,15 @@ protected internal virtual void ApplyLinkAnnotation(PdfDocument document) {
19051905
Rectangle pdfBBox = CalculateAbsolutePdfBBox();
19061906
linkAnnotation.SetRectangle(new PdfArray(pdfBBox));
19071907
PdfPage page = document.GetPage(pageNumber);
1908-
page.AddAnnotation(linkAnnotation);
1908+
// TODO DEVSIX-1655 This check is necessary because, in some cases, our renderer's hierarchy may contain
1909+
// a renderer from the different page that was already flushed
1910+
if (page.IsFlushed()) {
1911+
logger.Error(MessageFormatUtil.Format(iText.IO.LogMessageConstant.PAGE_WAS_FLUSHED_ACTION_WILL_NOT_BE_PERFORMED
1912+
, "link annotation applying"));
1913+
}
1914+
else {
1915+
page.AddAnnotation(linkAnnotation);
1916+
}
19091917
}
19101918
}
19111919

itext/itext.layout/itext/layout/renderer/BlockFormattingContextUtil.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ public class BlockFormattingContextUtil {
7777
/// <returns>true if given renderer creates a new "Block formatting context" in terms of CSS, false otherwise.
7878
/// </returns>
7979
public static bool IsRendererCreateBfc(IRenderer renderer) {
80-
return (renderer is RootRenderer) || (renderer is CellRenderer) || IsInlineBlock(renderer) || FloatingHelper
81-
.IsRendererFloating(renderer) || IsAbsolutePosition(renderer) || IsFixedPosition(renderer) || IsCaption
82-
(renderer) || AbstractRenderer.IsOverflowProperty(OverflowPropertyValue.HIDDEN, renderer, Property.OVERFLOW_X
83-
) || AbstractRenderer.IsOverflowProperty(OverflowPropertyValue.HIDDEN, renderer, Property.OVERFLOW_Y);
80+
return (renderer is RootRenderer) || (renderer is CellRenderer) || IsInlineBlock(renderer) || renderer.GetParent
81+
() is FlexContainerRenderer || FloatingHelper.IsRendererFloating(renderer) || IsAbsolutePosition(renderer
82+
) || IsFixedPosition(renderer) || IsCaption(renderer) || AbstractRenderer.IsOverflowProperty(OverflowPropertyValue
83+
.HIDDEN, renderer, Property.OVERFLOW_X) || AbstractRenderer.IsOverflowProperty(OverflowPropertyValue.HIDDEN
84+
, renderer, Property.OVERFLOW_Y);
8485
}
8586

8687
private static bool IsInlineBlock(IRenderer renderer) {

0 commit comments

Comments
 (0)