Skip to content

Commit bba113a

Browse files
Egor MartsynkovskyiText-CI
authored andcommitted
Refactor method for duplication
Remove ToDo statements Add test for logging DEVSIX-6108 Autoported commit. Original commit hash: [06dd11de0]
1 parent cbc6a7d commit bba113a

File tree

3 files changed

+122
-41
lines changed

3 files changed

+122
-41
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: Bruno Lowagie, Paulo Soares, et al.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using iText.Kernel.Geom;
44+
using iText.Layout.Element;
45+
using iText.Layout.Properties;
46+
using iText.Layout.Renderer;
47+
using iText.Test;
48+
using iText.Test.Attributes;
49+
50+
namespace iText.Layout.Margincollapse {
51+
public class MarginsCollapseHandlerTest : ExtendedITextTest {
52+
[NUnit.Framework.Test]
53+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Count = 2)]
54+
public virtual void TestDefiningMarginCollapse() {
55+
// This test's aim is to test message logging.
56+
ParagraphRenderer paragraphRenderer = new ParagraphRenderer(new Paragraph());
57+
Rectangle rectangle = new Rectangle(0f, 0f);
58+
paragraphRenderer.GetModelElement().SetProperty(Property.MARGIN_TOP, UnitValue.CreatePercentValue(0f));
59+
paragraphRenderer.GetModelElement().SetProperty(Property.MARGIN_BOTTOM, UnitValue.CreatePercentValue(0f));
60+
MarginsCollapseHandler marginsCollapseHandler = new MarginsCollapseHandler(paragraphRenderer, null);
61+
NUnit.Framework.Assert.DoesNotThrow(() => marginsCollapseHandler.StartMarginsCollapse(rectangle));
62+
}
63+
64+
[NUnit.Framework.Test]
65+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Count = 2)]
66+
public virtual void TestHasPadding() {
67+
// This test's aim is to test message logging.
68+
ParagraphRenderer paragraphRenderer = new ParagraphRenderer(new Paragraph());
69+
Rectangle rectangle = new Rectangle(0f, 0f);
70+
paragraphRenderer.GetModelElement().SetProperty(Property.PADDING_TOP, UnitValue.CreatePercentValue(0f));
71+
paragraphRenderer.GetModelElement().SetProperty(Property.PADDING_BOTTOM, UnitValue.CreatePercentValue(0f));
72+
MarginsCollapseHandler marginsCollapseHandler = new MarginsCollapseHandler(paragraphRenderer, null);
73+
NUnit.Framework.Assert.DoesNotThrow(() => marginsCollapseHandler.StartMarginsCollapse(rectangle));
74+
}
75+
}
76+
}

itext/itext.layout/itext/layout/margincollapse/MarginsCollapseHandler.cs

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ public virtual void EndChildMarginsHandling(Rectangle layoutBox) {
188188
}
189189

190190
public virtual void StartMarginsCollapse(Rectangle parentBBox) {
191-
collapseInfo.GetCollapseBefore().JoinMargin(GetModelTopMargin(renderer));
192-
collapseInfo.GetCollapseAfter().JoinMargin(GetModelBottomMargin(renderer));
191+
collapseInfo.GetCollapseBefore().JoinMargin(DefineTopMarginValueForCollapse(renderer));
192+
collapseInfo.GetCollapseAfter().JoinMargin(DefineBottomMarginValueForCollapse(renderer));
193193
if (!FirstChildMarginAdjoinedToParent(renderer)) {
194194
float topIndent = collapseInfo.GetCollapseBefore().GetCollapsedMarginsSize();
195195
ApplyTopMargin(parentBBox, topIndent);
@@ -231,7 +231,7 @@ public virtual void EndMarginsCollapse(Rectangle layoutBox) {
231231
else {
232232
ownCollapseAfter = new MarginsCollapse();
233233
}
234-
ownCollapseAfter.JoinMargin(GetModelBottomMargin(renderer));
234+
ownCollapseAfter.JoinMargin(DefineBottomMarginValueForCollapse(renderer));
235235
collapseInfo.SetOwnCollapseAfter(ownCollapseAfter);
236236
if (collapseInfo.IsSelfCollapsing()) {
237237
if (prevChildMarginInfo != null) {
@@ -509,33 +509,19 @@ private static bool HasPositiveHeight(IRenderer renderer) {
509509
}
510510

511511
private static bool HasTopPadding(IRenderer renderer) {
512-
UnitValue padding = renderer.GetModelElement().GetProperty<UnitValue>(Property.PADDING_TOP);
513-
if (null != padding && !padding.IsPointValue()) {
514-
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Margincollapse.MarginsCollapseHandler));
515-
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED
516-
, Property.PADDING_TOP));
517-
}
518-
return padding != null && padding.GetValue() > 0;
512+
return iText.Layout.Margincollapse.MarginsCollapseHandler.HasPadding(renderer, Property.PADDING_TOP);
519513
}
520514

521515
private static bool HasBottomPadding(IRenderer renderer) {
522-
UnitValue padding = renderer.GetModelElement().GetProperty<UnitValue>(Property.PADDING_BOTTOM);
523-
if (null != padding && !padding.IsPointValue()) {
524-
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Margincollapse.MarginsCollapseHandler));
525-
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED
526-
, Property.PADDING_BOTTOM));
527-
}
528-
return padding != null && padding.GetValue() > 0;
516+
return iText.Layout.Margincollapse.MarginsCollapseHandler.HasPadding(renderer, Property.PADDING_BOTTOM);
529517
}
530518

531519
private static bool HasTopBorders(IRenderer renderer) {
532-
IPropertyContainer modelElement = renderer.GetModelElement();
533-
return modelElement.HasProperty(Property.BORDER_TOP) || modelElement.HasProperty(Property.BORDER);
520+
return iText.Layout.Margincollapse.MarginsCollapseHandler.HasBorders(renderer, Property.BORDER_TOP);
534521
}
535522

536523
private static bool HasBottomBorders(IRenderer renderer) {
537-
IPropertyContainer modelElement = renderer.GetModelElement();
538-
return modelElement.HasProperty(Property.BORDER_BOTTOM) || modelElement.HasProperty(Property.BORDER);
524+
return iText.Layout.Margincollapse.MarginsCollapseHandler.HasBorders(renderer, Property.BORDER_BOTTOM);
539525
}
540526

541527
private static bool RendererIsFloated(IRenderer renderer) {
@@ -546,42 +532,61 @@ private static bool RendererIsFloated(IRenderer renderer) {
546532
return floatPropertyValue != null && !floatPropertyValue.Equals(FloatPropertyValue.NONE);
547533
}
548534

549-
private static float GetModelTopMargin(IRenderer renderer) {
550-
UnitValue marginUV = renderer.GetModelElement().GetProperty<UnitValue>(Property.MARGIN_TOP);
551-
if (null != marginUV && !marginUV.IsPointValue()) {
552-
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Margincollapse.MarginsCollapseHandler));
553-
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED
554-
, Property.MARGIN_TOP));
555-
}
556-
// TODO Concerning "renderer instanceof CellRenderer" check: may be try to apply more general solution in future
557-
return marginUV != null && !(renderer is CellRenderer) ? marginUV.GetValue() : 0;
535+
private static float DefineTopMarginValueForCollapse(IRenderer renderer) {
536+
return iText.Layout.Margincollapse.MarginsCollapseHandler.DefineMarginValueForCollapse(renderer, Property.
537+
MARGIN_TOP);
558538
}
559539

560540
private static void IgnoreModelTopMargin(IRenderer renderer) {
561-
renderer.SetProperty(Property.MARGIN_TOP, UnitValue.CreatePointValue(0f));
541+
iText.Layout.Margincollapse.MarginsCollapseHandler.OverrideModelTopMargin(renderer, 0f);
562542
}
563543

564544
private static void OverrideModelTopMargin(IRenderer renderer, float collapsedMargins) {
565-
renderer.SetProperty(Property.MARGIN_TOP, UnitValue.CreatePointValue(collapsedMargins));
545+
iText.Layout.Margincollapse.MarginsCollapseHandler.OverrideModelMargin(renderer, Property.MARGIN_TOP, collapsedMargins
546+
);
566547
}
567548

568-
private static float GetModelBottomMargin(IRenderer renderer) {
569-
UnitValue marginUV = renderer.GetModelElement().GetProperty<UnitValue>(Property.MARGIN_BOTTOM);
549+
private static float DefineBottomMarginValueForCollapse(IRenderer renderer) {
550+
return iText.Layout.Margincollapse.MarginsCollapseHandler.DefineMarginValueForCollapse(renderer, Property.
551+
MARGIN_BOTTOM);
552+
}
553+
554+
private static void IgnoreModelBottomMargin(IRenderer renderer) {
555+
iText.Layout.Margincollapse.MarginsCollapseHandler.OverrideModelBottomMargin(renderer, 0f);
556+
}
557+
558+
private static void OverrideModelBottomMargin(IRenderer renderer, float collapsedMargins) {
559+
iText.Layout.Margincollapse.MarginsCollapseHandler.OverrideModelMargin(renderer, Property.MARGIN_BOTTOM, collapsedMargins
560+
);
561+
}
562+
563+
private static float DefineMarginValueForCollapse(IRenderer renderer, int property) {
564+
UnitValue marginUV = renderer.GetModelElement().GetProperty<UnitValue>(property);
570565
if (null != marginUV && !marginUV.IsPointValue()) {
571566
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Margincollapse.MarginsCollapseHandler));
572567
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED
573-
, Property.MARGIN_TOP));
568+
, property));
574569
}
575-
// TODO Concerning "renderer instanceof CellRenderer" check: may be try to apply more general solution in future
576570
return marginUV != null && !(renderer is CellRenderer) ? marginUV.GetValue() : 0;
577571
}
578572

579-
private static void IgnoreModelBottomMargin(IRenderer renderer) {
580-
renderer.SetProperty(Property.MARGIN_BOTTOM, UnitValue.CreatePointValue(0f));
573+
private static void OverrideModelMargin(IRenderer renderer, int property, float collapsedMargins) {
574+
renderer.SetProperty(property, UnitValue.CreatePointValue(collapsedMargins));
581575
}
582576

583-
private static void OverrideModelBottomMargin(IRenderer renderer, float collapsedMargins) {
584-
renderer.SetProperty(Property.MARGIN_BOTTOM, UnitValue.CreatePointValue(collapsedMargins));
577+
private static bool HasPadding(IRenderer renderer, int property) {
578+
UnitValue padding = renderer.GetModelElement().GetProperty<UnitValue>(property);
579+
if (null != padding && !padding.IsPointValue()) {
580+
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Layout.Margincollapse.MarginsCollapseHandler));
581+
logger.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED
582+
, property));
583+
}
584+
return padding != null && padding.GetValue() > 0;
585+
}
586+
587+
private static bool HasBorders(IRenderer renderer, int property) {
588+
IPropertyContainer modelElement = renderer.GetModelElement();
589+
return modelElement.HasProperty(property) || modelElement.HasProperty(Property.BORDER);
585590
}
586591
}
587592
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6adb924bcfe2f79d6ff0f025b9cc9e8f7ee1c2b6
1+
06dd11de03686fefd5ebd4d4d714550250cd3316

0 commit comments

Comments
 (0)